blob: 70d8d96d8f33a99c946f58266363815fd9ff342f [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
Adam Langleye9ada862015-05-11 17:20:37 -0700170#include "internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800171
172
David Benjamin6e899c72016-06-09 18:02:18 -0400173static int ssl3_send_client_hello(SSL *ssl);
174static int dtls1_get_hello_verify(SSL *ssl);
175static int ssl3_get_server_hello(SSL *ssl);
176static int ssl3_get_server_certificate(SSL *ssl);
177static int ssl3_get_cert_status(SSL *ssl);
178static int ssl3_verify_server_cert(SSL *ssl);
179static int ssl3_get_server_key_exchange(SSL *ssl);
180static int ssl3_get_certificate_request(SSL *ssl);
181static int ssl3_get_server_hello_done(SSL *ssl);
182static int ssl3_send_client_certificate(SSL *ssl);
183static int ssl3_send_client_key_exchange(SSL *ssl);
184static int ssl3_send_cert_verify(SSL *ssl);
185static int ssl3_send_next_proto(SSL *ssl);
186static int ssl3_send_channel_id(SSL *ssl);
187static int ssl3_get_new_session_ticket(SSL *ssl);
188
Adam Langley4139edb2016-01-13 15:00:54 -0800189int ssl3_connect(SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800190 int ret = -1;
David Benjamin6e899c72016-06-09 18:02:18 -0400191 int state, skip = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800192
Adam Langley4139edb2016-01-13 15:00:54 -0800193 assert(ssl->handshake_func == ssl3_connect);
194 assert(!ssl->server);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800195
Adam Langleyd9e397b2015-01-22 14:27:53 -0800196 for (;;) {
Adam Langley4139edb2016-01-13 15:00:54 -0800197 state = ssl->state;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800198
Adam Langley4139edb2016-01-13 15:00:54 -0800199 switch (ssl->state) {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400200 case SSL_ST_INIT:
201 ssl->state = SSL_ST_CONNECT;
202 skip = 1;
203 break;
204
Adam Langleyd9e397b2015-01-22 14:27:53 -0800205 case SSL_ST_CONNECT:
David Benjamin6e899c72016-06-09 18:02:18 -0400206 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langleye9ada862015-05-11 17:20:37 -0700207
David Benjaminc895d6b2016-08-11 13:26:41 -0400208 ssl->s3->hs = ssl_handshake_new(tls13_client_handshake);
209 if (ssl->s3->hs == NULL) {
210 ret = -1;
211 goto end;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800212 }
213
David Benjamind316cba2016-06-02 16:17:39 -0400214 if (!ssl_init_wbio_buffer(ssl)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800215 ret = -1;
216 goto end;
217 }
218
Adam Langley4139edb2016-01-13 15:00:54 -0800219 ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800220 break;
221
222 case SSL3_ST_CW_CLNT_HELLO_A:
223 case SSL3_ST_CW_CLNT_HELLO_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800224 ret = ssl3_send_client_hello(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800225 if (ret <= 0) {
226 goto end;
227 }
David Benjamin6e899c72016-06-09 18:02:18 -0400228
David Benjaminc895d6b2016-08-11 13:26:41 -0400229 if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) {
David Benjamin6e899c72016-06-09 18:02:18 -0400230 ssl->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
231 } else {
232 ssl->s3->tmp.next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
233 }
David Benjamind316cba2016-06-02 16:17:39 -0400234 ssl->state = SSL3_ST_CW_FLUSH;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800235 break;
236
David Benjamin6e899c72016-06-09 18:02:18 -0400237 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
David Benjaminc895d6b2016-08-11 13:26:41 -0400238 assert(SSL_is_dtls(ssl));
David Benjamin6e899c72016-06-09 18:02:18 -0400239 ret = dtls1_get_hello_verify(ssl);
240 if (ret <= 0) {
241 goto end;
242 }
243 if (ssl->d1->send_cookie) {
244 ssl->method->received_flight(ssl);
245 ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
246 } else {
247 ssl->state = SSL3_ST_CR_SRVR_HELLO_A;
248 }
249 break;
250
Adam Langleyd9e397b2015-01-22 14:27:53 -0800251 case SSL3_ST_CR_SRVR_HELLO_A:
Adam Langley4139edb2016-01-13 15:00:54 -0800252 ret = ssl3_get_server_hello(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -0400253 if (ssl->state == SSL_ST_TLS13) {
254 break;
255 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800256 if (ret <= 0) {
257 goto end;
258 }
259
David Benjaminc895d6b2016-08-11 13:26:41 -0400260 if (ssl->session != NULL) {
261 ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800262 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800263 ssl->state = SSL3_ST_CR_CERT_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800264 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800265 break;
266
267 case SSL3_ST_CR_CERT_A:
David Benjamind316cba2016-06-02 16:17:39 -0400268 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -0800269 ret = ssl3_get_server_certificate(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800270 if (ret <= 0) {
271 goto end;
272 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800273 } else {
274 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800275 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400276 ssl->state = SSL3_ST_CR_CERT_STATUS_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800277 break;
278
David Benjamin6e899c72016-06-09 18:02:18 -0400279 case SSL3_ST_CR_CERT_STATUS_A:
David Benjamin95add822016-10-19 01:09:12 -0400280 if (ssl->s3->hs->certificate_status_expected) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400281 ret = ssl3_get_cert_status(ssl);
282 if (ret <= 0) {
283 goto end;
284 }
285 } else {
286 skip = 1;
David Benjamin6e899c72016-06-09 18:02:18 -0400287 }
288 ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
289 break;
290
Kenny Rootb8494592015-09-25 02:29:14 +0000291 case SSL3_ST_VERIFY_SERVER_CERT:
David Benjaminc895d6b2016-08-11 13:26:41 -0400292 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
293 ret = ssl3_verify_server_cert(ssl);
294 if (ret <= 0) {
295 goto end;
296 }
297 } else {
298 skip = 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000299 }
Adam Langley4139edb2016-01-13 15:00:54 -0800300 ssl->state = SSL3_ST_CR_KEY_EXCH_A;
Kenny Rootb8494592015-09-25 02:29:14 +0000301 break;
302
Adam Langleyd9e397b2015-01-22 14:27:53 -0800303 case SSL3_ST_CR_KEY_EXCH_A:
Adam Langley4139edb2016-01-13 15:00:54 -0800304 ret = ssl3_get_server_key_exchange(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800305 if (ret <= 0) {
306 goto end;
307 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400308 ssl->state = SSL3_ST_CR_CERT_REQ_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800309 break;
310
311 case SSL3_ST_CR_CERT_REQ_A:
David Benjaminc895d6b2016-08-11 13:26:41 -0400312 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
313 ret = ssl3_get_certificate_request(ssl);
314 if (ret <= 0) {
315 goto end;
316 }
317 } else {
318 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800319 }
Adam Langley4139edb2016-01-13 15:00:54 -0800320 ssl->state = SSL3_ST_CR_SRVR_DONE_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800321 break;
322
323 case SSL3_ST_CR_SRVR_DONE_A:
David Benjamin6e899c72016-06-09 18:02:18 -0400324 ret = ssl3_get_server_hello_done(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800325 if (ret <= 0) {
326 goto end;
327 }
David Benjamin6e899c72016-06-09 18:02:18 -0400328 ssl->method->received_flight(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -0400329 ssl->state = SSL3_ST_CW_CERT_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800330 break;
331
332 case SSL3_ST_CW_CERT_A:
333 case SSL3_ST_CW_CERT_B:
334 case SSL3_ST_CW_CERT_C:
David Benjamin95add822016-10-19 01:09:12 -0400335 if (ssl->s3->hs->cert_request) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400336 ret = ssl3_send_client_certificate(ssl);
337 if (ret <= 0) {
338 goto end;
339 }
340 } else {
341 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800342 }
Adam Langley4139edb2016-01-13 15:00:54 -0800343 ssl->state = SSL3_ST_CW_KEY_EXCH_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800344 break;
345
346 case SSL3_ST_CW_KEY_EXCH_A:
347 case SSL3_ST_CW_KEY_EXCH_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800348 ret = ssl3_send_client_key_exchange(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800349 if (ret <= 0) {
350 goto end;
351 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400352 ssl->state = SSL3_ST_CW_CERT_VRFY_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800353 break;
354
355 case SSL3_ST_CW_CERT_VRFY_A:
356 case SSL3_ST_CW_CERT_VRFY_B:
Kenny Rootb8494592015-09-25 02:29:14 +0000357 case SSL3_ST_CW_CERT_VRFY_C:
David Benjamin95add822016-10-19 01:09:12 -0400358 if (ssl->s3->hs->cert_request) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400359 ret = ssl3_send_cert_verify(ssl);
360 if (ret <= 0) {
361 goto end;
362 }
363 } else {
364 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800365 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400366 ssl->state = SSL3_ST_CW_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800367 break;
368
David Benjaminc895d6b2016-08-11 13:26:41 -0400369 case SSL3_ST_CW_CHANGE:
370 ret = ssl->method->send_change_cipher_spec(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800371 if (ret <= 0) {
372 goto end;
373 }
374
David Benjaminc895d6b2016-08-11 13:26:41 -0400375 ssl->state = SSL3_ST_CW_NEXT_PROTO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800376
David Benjamin4969cc92016-04-22 15:02:23 -0400377 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800378 ret = -1;
379 goto end;
380 }
381
382 break;
383
384 case SSL3_ST_CW_NEXT_PROTO_A:
385 case SSL3_ST_CW_NEXT_PROTO_B:
David Benjamin95add822016-10-19 01:09:12 -0400386 if (ssl->s3->hs->next_proto_neg_seen) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400387 ret = ssl3_send_next_proto(ssl);
388 if (ret <= 0) {
389 goto end;
390 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800391 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -0400392 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800393 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400394 ssl->state = SSL3_ST_CW_CHANNEL_ID_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800395 break;
396
397 case SSL3_ST_CW_CHANNEL_ID_A:
398 case SSL3_ST_CW_CHANNEL_ID_B:
David Benjaminc895d6b2016-08-11 13:26:41 -0400399 if (ssl->s3->tlsext_channel_id_valid) {
400 ret = ssl3_send_channel_id(ssl);
401 if (ret <= 0) {
402 goto end;
403 }
404 } else {
405 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800406 }
Adam Langley4139edb2016-01-13 15:00:54 -0800407 ssl->state = SSL3_ST_CW_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800408 break;
409
410 case SSL3_ST_CW_FINISHED_A:
411 case SSL3_ST_CW_FINISHED_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800412 ret = ssl3_send_finished(ssl, SSL3_ST_CW_FINISHED_A,
David Benjamin4969cc92016-04-22 15:02:23 -0400413 SSL3_ST_CW_FINISHED_B);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800414 if (ret <= 0) {
415 goto end;
416 }
Adam Langley4139edb2016-01-13 15:00:54 -0800417 ssl->state = SSL3_ST_CW_FLUSH;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800418
David Benjaminc895d6b2016-08-11 13:26:41 -0400419 if (ssl->session != NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -0800420 ssl->s3->tmp.next_state = SSL_ST_OK;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800421 } else {
422 /* This is a non-resumption handshake. If it involves ChannelID, then
423 * record the handshake hashes at this point in the session so that
424 * any resumption of this session with ChannelID can sign those
425 * hashes. */
Adam Langley4139edb2016-01-13 15:00:54 -0800426 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +0000427 if (ret <= 0) {
428 goto end;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800429 }
Adam Langley4139edb2016-01-13 15:00:54 -0800430 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
431 ssl3_can_false_start(ssl) &&
Adam Langleye9ada862015-05-11 17:20:37 -0700432 /* No False Start on renegotiation (would complicate the state
433 * machine). */
Adam Langley4139edb2016-01-13 15:00:54 -0800434 !ssl->s3->initial_handshake_complete) {
435 ssl->s3->tmp.next_state = SSL3_ST_FALSE_START;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800436 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -0400437 ssl->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800438 }
439 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800440 break;
441
David Benjamin6e899c72016-06-09 18:02:18 -0400442 case SSL3_ST_FALSE_START:
David Benjaminc895d6b2016-08-11 13:26:41 -0400443 ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
David Benjamin95add822016-10-19 01:09:12 -0400444 ssl->s3->hs->in_false_start = 1;
David Benjamin6e899c72016-06-09 18:02:18 -0400445
446 ssl_free_wbio_buffer(ssl);
447 ret = 1;
448 goto end;
449
Adam Langleyd9e397b2015-01-22 14:27:53 -0800450 case SSL3_ST_CR_SESSION_TICKET_A:
Steven Valdez909b19f2016-11-21 15:35:44 -0500451 if (ssl->s3->hs->ticket_expected) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400452 ret = ssl3_get_new_session_ticket(ssl);
453 if (ret <= 0) {
454 goto end;
455 }
456 } else {
457 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800458 }
Adam Langley4139edb2016-01-13 15:00:54 -0800459 ssl->state = SSL3_ST_CR_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800460 break;
461
Adam Langleyd9e397b2015-01-22 14:27:53 -0800462 case SSL3_ST_CR_CHANGE:
David Benjaminc895d6b2016-08-11 13:26:41 -0400463 ret = ssl->method->read_change_cipher_spec(ssl);
Adam Langley4139edb2016-01-13 15:00:54 -0800464 if (ret <= 0) {
465 goto end;
466 }
467
David Benjamin4969cc92016-04-22 15:02:23 -0400468 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800469 ret = -1;
470 goto end;
471 }
Adam Langley4139edb2016-01-13 15:00:54 -0800472 ssl->state = SSL3_ST_CR_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800473 break;
474
475 case SSL3_ST_CR_FINISHED_A:
David Benjamind316cba2016-06-02 16:17:39 -0400476 ret = ssl3_get_finished(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800477 if (ret <= 0) {
478 goto end;
479 }
David Benjamin6e899c72016-06-09 18:02:18 -0400480 ssl->method->received_flight(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800481
David Benjaminc895d6b2016-08-11 13:26:41 -0400482 if (ssl->session != NULL) {
483 ssl->state = SSL3_ST_CW_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800484 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800485 ssl->state = SSL_ST_OK;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800486 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800487 break;
488
489 case SSL3_ST_CW_FLUSH:
Adam Langley4139edb2016-01-13 15:00:54 -0800490 if (BIO_flush(ssl->wbio) <= 0) {
David Benjamin4969cc92016-04-22 15:02:23 -0400491 ssl->rwstate = SSL_WRITING;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800492 ret = -1;
493 goto end;
494 }
Adam Langley4139edb2016-01-13 15:00:54 -0800495 ssl->state = ssl->s3->tmp.next_state;
David Benjamin6e899c72016-06-09 18:02:18 -0400496 if (ssl->state != SSL_ST_OK) {
497 ssl->method->expect_flight(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800498 }
David Benjamin6e899c72016-06-09 18:02:18 -0400499 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800500
David Benjaminc895d6b2016-08-11 13:26:41 -0400501 case SSL_ST_TLS13:
502 ret = tls13_handshake(ssl);
503 if (ret <= 0) {
504 goto end;
505 }
506 ssl->state = SSL_ST_OK;
507 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800508
David Benjaminc895d6b2016-08-11 13:26:41 -0400509 case SSL_ST_OK:
David Benjaminc895d6b2016-08-11 13:26:41 -0400510 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
511
512 SSL_SESSION_free(ssl->s3->established_session);
513 if (ssl->session != NULL) {
514 SSL_SESSION_up_ref(ssl->session);
515 ssl->s3->established_session = ssl->session;
516 } else {
517 /* We make a copy of the session in order to maintain the immutability
518 * of the new established_session due to False Start. The caller may
519 * have taken a reference to the temporary session. */
520 ssl->s3->established_session =
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400521 SSL_SESSION_dup(ssl->s3->new_session, SSL_SESSION_DUP_ALL);
David Benjaminc895d6b2016-08-11 13:26:41 -0400522 if (ssl->s3->established_session == NULL) {
523 /* Do not stay in SSL_ST_OK, to avoid confusing |SSL_in_init|
524 * callers. */
525 ssl->state = SSL_ST_ERROR;
526 skip = 1;
527 ret = -1;
528 goto end;
529 }
530 ssl->s3->established_session->not_resumable = 0;
531
532 SSL_SESSION_free(ssl->s3->new_session);
533 ssl->s3->new_session = NULL;
David Benjamin6e899c72016-06-09 18:02:18 -0400534 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800535
536 /* Remove write buffering now. */
Adam Langley4139edb2016-01-13 15:00:54 -0800537 ssl_free_wbio_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800538
Adam Langley4139edb2016-01-13 15:00:54 -0800539 const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
Adam Langley4139edb2016-01-13 15:00:54 -0800540 ssl->s3->initial_handshake_complete = 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000541 if (is_initial_handshake) {
542 /* Renegotiations do not participate in session resumption. */
Adam Langley4139edb2016-01-13 15:00:54 -0800543 ssl_update_cache(ssl, SSL_SESS_CACHE_CLIENT);
Kenny Rootb8494592015-09-25 02:29:14 +0000544 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800545
Steven Valdez909b19f2016-11-21 15:35:44 -0500546 ssl_handshake_free(ssl->s3->hs);
547 ssl->s3->hs = NULL;
548
David Benjamin6e899c72016-06-09 18:02:18 -0400549 ret = 1;
550 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800551 goto end;
552
David Benjaminc895d6b2016-08-11 13:26:41 -0400553 case SSL_ST_ERROR:
554 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
555 ret = -1;
556 goto end;
557
Adam Langleyd9e397b2015-01-22 14:27:53 -0800558 default:
Kenny Rootb8494592015-09-25 02:29:14 +0000559 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800560 ret = -1;
561 goto end;
562 }
563
David Benjamin6e899c72016-06-09 18:02:18 -0400564 if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) {
565 int new_state = ssl->state;
566 ssl->state = state;
567 ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
568 ssl->state = new_state;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800569 }
570 skip = 0;
571 }
572
573end:
David Benjamin6e899c72016-06-09 18:02:18 -0400574 ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800575 return ret;
576}
577
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400578uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index) {
579 /* Use the client_random for entropy. This both avoids calling |RAND_bytes| on
580 * a single byte repeatedly and ensures the values are deterministic. This
581 * allows the same ClientHello be sent twice for a HelloRetryRequest or the
582 * same group be advertised in both supported_groups and key_shares. */
583 uint16_t ret = ssl->s3->client_random[index];
584 /* This generates a random value of the form 0xωaωa, for all 0 ≤ ω < 16. */
585 ret = (ret & 0xf0) | 0x0a;
586 ret |= ret << 8;
587 return ret;
588}
589
David Benjaminc895d6b2016-08-11 13:26:41 -0400590static int ssl_write_client_cipher_list(SSL *ssl, CBB *out,
591 uint16_t min_version,
David Benjamin7c0d06c2016-08-11 13:26:41 -0400592 uint16_t max_version) {
Kenny Roote99801b2015-11-06 15:31:15 -0800593 /* Prepare disabled cipher masks. */
594 ssl_set_client_disabled(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800595
Kenny Roote99801b2015-11-06 15:31:15 -0800596 CBB child;
597 if (!CBB_add_u16_length_prefixed(out, &child)) {
598 return 0;
Adam Langleyfdeb4882015-10-30 13:15:30 -0700599 }
600
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400601 /* Add a fake cipher suite. See draft-davidben-tls-grease-01. */
602 if (ssl->ctx->grease_enabled &&
603 !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
604 return 0;
605 }
606
Steven Valdez909b19f2016-11-21 15:35:44 -0500607 /* Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
608 * hardware support. */
609 if (max_version >= TLS1_3_VERSION) {
610 if (!EVP_has_aes_hardware() &&
611 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
612 return 0;
Adam Langleyfad63272015-11-12 12:15:39 -0800613 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500614 if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
615 !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
616 return 0;
Kenny Roote99801b2015-11-06 15:31:15 -0800617 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500618 if (EVP_has_aes_hardware() &&
619 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
Kenny Roote99801b2015-11-06 15:31:15 -0800620 return 0;
621 }
622 }
623
Steven Valdez909b19f2016-11-21 15:35:44 -0500624 if (min_version < TLS1_3_VERSION) {
625 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
626 int any_enabled = 0;
627 for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
628 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
629 /* Skip disabled ciphers */
630 if ((cipher->algorithm_mkey & ssl->cert->mask_k) ||
631 (cipher->algorithm_auth & ssl->cert->mask_a)) {
632 continue;
633 }
634 if (SSL_CIPHER_get_min_version(cipher) > max_version ||
635 SSL_CIPHER_get_max_version(cipher) < min_version) {
636 continue;
637 }
638 any_enabled = 1;
639 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
640 return 0;
641 }
642 }
643
644 /* If all ciphers were disabled, return the error to the caller. */
645 if (!any_enabled && max_version < TLS1_3_VERSION) {
646 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
647 return 0;
648 }
Kenny Roote99801b2015-11-06 15:31:15 -0800649 }
650
651 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
652 * added. */
653 if (ssl->client_version == SSL3_VERSION &&
654 !ssl->s3->initial_handshake_complete) {
655 if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
656 return 0;
657 }
Kenny Roote99801b2015-11-06 15:31:15 -0800658 }
659
David Benjamin7c0d06c2016-08-11 13:26:41 -0400660 if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400661 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
662 return 0;
663 }
Kenny Roote99801b2015-11-06 15:31:15 -0800664 }
665
666 return CBB_flush(out);
667}
668
Steven Valdez909b19f2016-11-21 15:35:44 -0500669int ssl_write_client_hello(SSL *ssl) {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400670 uint16_t min_version, max_version;
671 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400672 return 0;
673 }
674
Steven Valdez909b19f2016-11-21 15:35:44 -0500675 CBB cbb, body;
676 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO)) {
677 goto err;
678 }
679
David Benjaminc895d6b2016-08-11 13:26:41 -0400680 /* Renegotiations do not participate in session resumption. */
681 int has_session = ssl->session != NULL &&
682 !ssl->s3->initial_handshake_complete;
683
684 CBB child;
Steven Valdez909b19f2016-11-21 15:35:44 -0500685 if (!CBB_add_u16(&body, ssl->client_version) ||
686 !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
687 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400688 (has_session &&
689 !CBB_add_bytes(&child, ssl->session->session_id,
690 ssl->session->session_id_length))) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500691 goto err;
David Benjaminc895d6b2016-08-11 13:26:41 -0400692 }
693
694 if (SSL_is_dtls(ssl)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500695 if (!CBB_add_u8_length_prefixed(&body, &child) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400696 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500697 goto err;
David Benjaminc895d6b2016-08-11 13:26:41 -0400698 }
699 }
700
701 size_t header_len =
702 SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
Steven Valdez909b19f2016-11-21 15:35:44 -0500703 if (!ssl_write_client_cipher_list(ssl, &body, min_version, max_version) ||
704 !CBB_add_u8(&body, 1 /* one compression method */) ||
705 !CBB_add_u8(&body, 0 /* null compression */) ||
706 !ssl_add_clienthello_tlsext(ssl, &body, header_len + CBB_len(&body))) {
707 goto err;
David Benjaminc895d6b2016-08-11 13:26:41 -0400708 }
709
Steven Valdez909b19f2016-11-21 15:35:44 -0500710 uint8_t *msg = NULL;
711 size_t len;
712 if (!ssl->method->finish_message(ssl, &cbb, &msg, &len)) {
713 goto err;
714 }
715
716 /* Now that the length prefixes have been computed, fill in the placeholder
717 * PSK binder. */
718 if (ssl->s3->hs->needs_psk_binder &&
719 !tls13_write_psk_binder(ssl, msg, len)) {
720 OPENSSL_free(msg);
721 goto err;
722 }
723
724 return ssl->method->queue_message(ssl, msg, len);
725
726 err:
727 CBB_cleanup(&cbb);
728 return 0;
David Benjaminc895d6b2016-08-11 13:26:41 -0400729}
730
David Benjamin6e899c72016-06-09 18:02:18 -0400731static int ssl3_send_client_hello(SSL *ssl) {
Kenny Roote99801b2015-11-06 15:31:15 -0800732 if (ssl->state == SSL3_ST_CW_CLNT_HELLO_B) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400733 return ssl->method->write_message(ssl);
Kenny Roote99801b2015-11-06 15:31:15 -0800734 }
735
David Benjamin6e899c72016-06-09 18:02:18 -0400736 /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we
737 * may send multiple ClientHellos if we receive HelloVerifyRequest. */
738 if (!ssl3_init_handshake_buffer(ssl)) {
Adam Langleyfad63272015-11-12 12:15:39 -0800739 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
740 return -1;
741 }
742
David Benjaminc895d6b2016-08-11 13:26:41 -0400743 uint16_t min_version, max_version;
744 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500745 return -1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400746 }
747
Kenny Roote99801b2015-11-06 15:31:15 -0800748 assert(ssl->state == SSL3_ST_CW_CLNT_HELLO_A);
749 if (!ssl->s3->have_version) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400750 ssl->version = ssl->method->version_to_wire(max_version);
Kenny Roote99801b2015-11-06 15:31:15 -0800751 /* Only set |ssl->client_version| on the initial handshake. Renegotiations,
752 * although locked to a version, reuse the value. When using the plain RSA
753 * key exchange, the ClientHello version is checked in the premaster secret.
754 * Some servers fail when this value changes. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400755 ssl->client_version = ssl->version;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400756
757 if (max_version >= TLS1_3_VERSION) {
758 ssl->client_version = ssl->method->version_to_wire(TLS1_2_VERSION);
759 }
Kenny Roote99801b2015-11-06 15:31:15 -0800760 }
761
David Benjamin4969cc92016-04-22 15:02:23 -0400762 /* If the configured session has expired or was created at a disabled
763 * version, drop it. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400764 if (ssl->session != NULL) {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400765 uint16_t session_version;
766 if (!ssl->method->version_from_wire(&session_version,
767 ssl->session->ssl_version) ||
768 (session_version < TLS1_3_VERSION &&
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400769 ssl->session->session_id_length == 0) ||
770 ssl->session->not_resumable ||
771 !ssl_session_is_time_valid(ssl, ssl->session) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400772 session_version < min_version || session_version > max_version) {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400773 ssl_set_session(ssl, NULL);
David Benjaminc895d6b2016-08-11 13:26:41 -0400774 }
Kenny Roote99801b2015-11-06 15:31:15 -0800775 }
776
777 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
778 * renegerate the client_random. The random must be reused. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400779 if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) &&
780 !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500781 return -1;
Kenny Roote99801b2015-11-06 15:31:15 -0800782 }
783
Steven Valdez909b19f2016-11-21 15:35:44 -0500784 if (!ssl_write_client_hello(ssl)) {
785 return -1;
Kenny Roote99801b2015-11-06 15:31:15 -0800786 }
787
788 ssl->state = SSL3_ST_CW_CLNT_HELLO_B;
David Benjaminc895d6b2016-08-11 13:26:41 -0400789 return ssl->method->write_message(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800790}
791
David Benjamin6e899c72016-06-09 18:02:18 -0400792static int dtls1_get_hello_verify(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400793 int al;
David Benjamin6e899c72016-06-09 18:02:18 -0400794 CBS hello_verify_request, cookie;
795 uint16_t server_version;
796
David Benjaminc895d6b2016-08-11 13:26:41 -0400797 int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
798 if (ret <= 0) {
799 return ret;
David Benjamin6e899c72016-06-09 18:02:18 -0400800 }
801
802 if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
803 ssl->d1->send_cookie = 0;
804 ssl->s3->tmp.reuse_message = 1;
805 return 1;
806 }
807
David Benjaminc895d6b2016-08-11 13:26:41 -0400808 CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num);
David Benjamin6e899c72016-06-09 18:02:18 -0400809
810 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
811 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
812 CBS_len(&hello_verify_request) != 0) {
813 al = SSL_AD_DECODE_ERROR;
814 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
815 goto f_err;
816 }
817
818 if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) {
819 al = SSL_AD_ILLEGAL_PARAMETER;
820 goto f_err;
821 }
822
823 memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
824 ssl->d1->cookie_len = CBS_len(&cookie);
825
826 ssl->d1->send_cookie = 1;
827 return 1;
828
829f_err:
830 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
831 return -1;
832}
833
834static int ssl3_get_server_hello(SSL *ssl) {
Adam Langley4139edb2016-01-13 15:00:54 -0800835 CERT *ct = ssl->cert;
David Benjaminc895d6b2016-08-11 13:26:41 -0400836 int al = SSL_AD_INTERNAL_ERROR;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800837 CBS server_hello, server_random, session_id;
David Benjamin7c0d06c2016-08-11 13:26:41 -0400838 uint16_t server_wire_version, cipher_suite;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800839 uint8_t compression_method;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800840
David Benjaminc895d6b2016-08-11 13:26:41 -0400841 int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
842 if (ret <= 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800843 uint32_t err = ERR_peek_error();
844 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
845 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
846 /* Add a dedicated error code to the queue for a handshake_failure alert
847 * in response to ClientHello. This matches NSS's client behavior and
848 * gives a better error on a (probable) failure to negotiate initial
849 * parameters. Note: this error code comes after the original one.
850 *
851 * See https://crbug.com/446505. */
Kenny Rootb8494592015-09-25 02:29:14 +0000852 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800853 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400854 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800855 }
856
David Benjaminc895d6b2016-08-11 13:26:41 -0400857 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO &&
858 ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
859 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
860 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
861 return -1;
862 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800863
David Benjaminc895d6b2016-08-11 13:26:41 -0400864 CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
865
866 if (!CBS_get_u16(&server_hello, &server_wire_version)) {
867 al = SSL_AD_DECODE_ERROR;
868 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
869 goto f_err;
870 }
871
David Benjamin7c0d06c2016-08-11 13:26:41 -0400872 uint16_t min_version, max_version, server_version;
873 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
874 !ssl->method->version_from_wire(&server_version, server_wire_version) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400875 server_version < min_version || server_version > max_version) {
876 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
877 al = SSL_AD_PROTOCOL_VERSION;
878 goto f_err;
879 }
880
881 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
882 if (!ssl->s3->have_version) {
883 ssl->version = server_wire_version;
884 ssl->s3->enc_method = ssl3_get_enc_method(server_version);
885 assert(ssl->s3->enc_method != NULL);
886 /* At this point, the connection's version is known and ssl->version is
887 * fixed. Begin enforcing the record-layer version. */
888 ssl->s3->have_version = 1;
889 } else if (server_wire_version != ssl->version) {
890 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
891 al = SSL_AD_PROTOCOL_VERSION;
892 goto f_err;
893 }
894
895 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
896 ssl->state = SSL_ST_TLS13;
897 return 1;
898 }
899
David Benjamin7c0d06c2016-08-11 13:26:41 -0400900 ssl_clear_tls13_state(ssl);
901
David Benjaminc895d6b2016-08-11 13:26:41 -0400902 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
903 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
904 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
905 return -1;
906 }
907
908 if (!CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -0800909 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
910 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
911 !CBS_get_u16(&server_hello, &cipher_suite) ||
912 !CBS_get_u8(&server_hello, &compression_method)) {
913 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000914 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800915 goto f_err;
916 }
917
Adam Langleyd9e397b2015-01-22 14:27:53 -0800918 /* Copy over the server random. */
Adam Langley4139edb2016-01-13 15:00:54 -0800919 memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800920
David Benjamin7c0d06c2016-08-11 13:26:41 -0400921 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
922 * 1.3 is finalized and we are not implementing a draft version. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400923
Adam Langley4139edb2016-01-13 15:00:54 -0800924 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400925 ssl->session->session_id_length != 0 &&
Adam Langley4139edb2016-01-13 15:00:54 -0800926 CBS_mem_equal(&session_id, ssl->session->session_id,
927 ssl->session->session_id_length)) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400928 ssl->s3->session_reused = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800929 } else {
930 /* The session wasn't resumed. Create a fresh SSL_SESSION to
931 * fill out. */
David Benjamin7c0d06c2016-08-11 13:26:41 -0400932 ssl_set_session(ssl, NULL);
Adam Langley4139edb2016-01-13 15:00:54 -0800933 if (!ssl_get_new_session(ssl, 0 /* client */)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800934 goto f_err;
935 }
936 /* Note: session_id could be empty. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400937 ssl->s3->new_session->session_id_length = CBS_len(&session_id);
938 memcpy(ssl->s3->new_session->session_id, CBS_data(&session_id),
Adam Langley4139edb2016-01-13 15:00:54 -0800939 CBS_len(&session_id));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800940 }
941
Steven Valdez909b19f2016-11-21 15:35:44 -0500942 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800943 if (c == NULL) {
944 /* unknown cipher */
945 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +0000946 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800947 goto f_err;
948 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500949
950 /* The cipher must be allowed in the selected version and enabled. */
Adam Langley4139edb2016-01-13 15:00:54 -0800951 if ((c->algorithm_mkey & ct->mask_k) || (c->algorithm_auth & ct->mask_a) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400952 SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
Steven Valdez909b19f2016-11-21 15:35:44 -0500953 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl) ||
954 !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, c)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800955 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +0000956 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800957 goto f_err;
958 }
959
David Benjaminc895d6b2016-08-11 13:26:41 -0400960 if (ssl->session != NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -0800961 if (ssl->session->ssl_version != ssl->version) {
Adam Langleye9ada862015-05-11 17:20:37 -0700962 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +0000963 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
Adam Langleye9ada862015-05-11 17:20:37 -0700964 goto f_err;
965 }
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400966 if (ssl->session->cipher != c) {
967 al = SSL_AD_ILLEGAL_PARAMETER;
968 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
969 goto f_err;
970 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400971 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
972 /* This is actually a client application bug. */
973 al = SSL_AD_ILLEGAL_PARAMETER;
974 OPENSSL_PUT_ERROR(SSL,
975 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
976 goto f_err;
977 }
David Benjamin4969cc92016-04-22 15:02:23 -0400978 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -0400979 ssl->s3->new_session->cipher = c;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800980 }
Adam Langley4139edb2016-01-13 15:00:54 -0800981 ssl->s3->tmp.new_cipher = c;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800982
Kenny Rootb8494592015-09-25 02:29:14 +0000983 /* Now that the cipher is known, initialize the handshake hash. */
Adam Langley4139edb2016-01-13 15:00:54 -0800984 if (!ssl3_init_handshake_hash(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000985 goto f_err;
986 }
987
David Benjaminc895d6b2016-08-11 13:26:41 -0400988 /* If doing a full handshake, the server may request a client certificate
989 * which requires hashing the handshake transcript. Otherwise, the handshake
990 * buffer may be released. */
991 if (ssl->session != NULL ||
David Benjamind316cba2016-06-02 16:17:39 -0400992 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -0800993 ssl3_free_handshake_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800994 }
995
996 /* Only the NULL compression algorithm is supported. */
997 if (compression_method != 0) {
998 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +0000999 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001000 goto f_err;
1001 }
1002
1003 /* TLS extensions */
Adam Langley4139edb2016-01-13 15:00:54 -08001004 if (!ssl_parse_serverhello_tlsext(ssl, &server_hello)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001005 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001006 goto err;
1007 }
1008
1009 /* There should be nothing left over in the record. */
1010 if (CBS_len(&server_hello) != 0) {
1011 /* wrong packet length */
1012 al = SSL_AD_DECODE_ERROR;
David Benjamin7c0d06c2016-08-11 13:26:41 -04001013 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001014 goto f_err;
1015 }
1016
David Benjaminc895d6b2016-08-11 13:26:41 -04001017 if (ssl->session != NULL &&
Adam Langley4139edb2016-01-13 15:00:54 -08001018 ssl->s3->tmp.extended_master_secret !=
1019 ssl->session->extended_master_secret) {
Adam Langleyf4e42722015-06-04 17:45:09 -07001020 al = SSL_AD_HANDSHAKE_FAILURE;
Adam Langley4139edb2016-01-13 15:00:54 -08001021 if (ssl->session->extended_master_secret) {
Kenny Rootb8494592015-09-25 02:29:14 +00001022 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyf4e42722015-06-04 17:45:09 -07001023 } else {
Kenny Rootb8494592015-09-25 02:29:14 +00001024 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyf4e42722015-06-04 17:45:09 -07001025 }
1026 goto f_err;
1027 }
1028
Adam Langleyd9e397b2015-01-22 14:27:53 -08001029 return 1;
1030
1031f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001032 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001033err:
1034 return -1;
1035}
1036
David Benjamin6e899c72016-06-09 18:02:18 -04001037static int ssl3_get_server_certificate(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001038 int ret =
1039 ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE, ssl_hash_message);
1040 if (ret <= 0) {
1041 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001042 }
1043
David Benjaminc895d6b2016-08-11 13:26:41 -04001044 CBS cbs;
1045 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
1046 uint8_t alert;
1047 STACK_OF(X509) *chain = ssl_parse_cert_chain(ssl, &alert, NULL, &cbs);
1048 if (chain == NULL) {
1049 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001050 goto err;
1051 }
1052
David Benjaminc895d6b2016-08-11 13:26:41 -04001053 if (sk_X509_num(chain) == 0 || CBS_len(&cbs) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001054 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04001055 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1056 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001057 }
1058
David Benjaminc895d6b2016-08-11 13:26:41 -04001059 X509 *leaf = sk_X509_value(chain, 0);
1060 if (!ssl_check_leaf_certificate(ssl, leaf)) {
1061 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1062 goto err;
Kenny Roota04d78d2015-09-25 00:26:37 +00001063 }
Kenny Rootb8494592015-09-25 02:29:14 +00001064
Steven Valdez909b19f2016-11-21 15:35:44 -05001065 /* NOTE: Unlike the server half, the client's copy of |x509_chain| includes
Kenny Rootb8494592015-09-25 02:29:14 +00001066 * the leaf. */
Steven Valdez909b19f2016-11-21 15:35:44 -05001067 sk_X509_pop_free(ssl->s3->new_session->x509_chain, X509_free);
1068 ssl->s3->new_session->x509_chain = chain;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001069
Steven Valdez909b19f2016-11-21 15:35:44 -05001070 X509_free(ssl->s3->new_session->x509_peer);
David Benjaminc895d6b2016-08-11 13:26:41 -04001071 X509_up_ref(leaf);
Steven Valdez909b19f2016-11-21 15:35:44 -05001072 ssl->s3->new_session->x509_peer = leaf;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001073
David Benjaminc895d6b2016-08-11 13:26:41 -04001074 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001075
1076err:
David Benjaminc895d6b2016-08-11 13:26:41 -04001077 sk_X509_pop_free(chain, X509_free);
1078 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001079}
1080
David Benjamin6e899c72016-06-09 18:02:18 -04001081static int ssl3_get_cert_status(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001082 int al;
David Benjamin6e899c72016-06-09 18:02:18 -04001083 CBS certificate_status, ocsp_response;
1084 uint8_t status_type;
1085
David Benjaminc895d6b2016-08-11 13:26:41 -04001086 int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1087 if (ret <= 0) {
1088 return ret;
David Benjamin6e899c72016-06-09 18:02:18 -04001089 }
1090
1091 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1092 /* A server may send status_request in ServerHello and then change
1093 * its mind about sending CertificateStatus. */
1094 ssl->s3->tmp.reuse_message = 1;
1095 return 1;
1096 }
1097
David Benjaminc895d6b2016-08-11 13:26:41 -04001098 CBS_init(&certificate_status, ssl->init_msg, ssl->init_num);
David Benjamin6e899c72016-06-09 18:02:18 -04001099 if (!CBS_get_u8(&certificate_status, &status_type) ||
1100 status_type != TLSEXT_STATUSTYPE_ocsp ||
1101 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1102 CBS_len(&ocsp_response) == 0 ||
1103 CBS_len(&certificate_status) != 0) {
1104 al = SSL_AD_DECODE_ERROR;
1105 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1106 goto f_err;
1107 }
1108
David Benjaminc895d6b2016-08-11 13:26:41 -04001109 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1110 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin6e899c72016-06-09 18:02:18 -04001111 al = SSL_AD_INTERNAL_ERROR;
1112 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1113 goto f_err;
1114 }
1115 return 1;
1116
1117f_err:
1118 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1119 return -1;
1120}
1121
1122static int ssl3_verify_server_cert(SSL *ssl) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001123 if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
Steven Valdez909b19f2016-11-21 15:35:44 -05001124 ssl->s3->new_session->x509_chain)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001125 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001126 }
1127
David Benjaminc895d6b2016-08-11 13:26:41 -04001128 return 1;
David Benjamin6e899c72016-06-09 18:02:18 -04001129}
1130
1131static int ssl3_get_server_key_exchange(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001132 int al;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001133 EVP_PKEY *pkey = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001134 DH *dh = NULL;
1135 EC_KEY *ecdh = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001136 EC_POINT *srvr_ecpoint = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001137
David Benjaminc895d6b2016-08-11 13:26:41 -04001138 int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1139 if (ret <= 0) {
1140 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001141 }
1142
Adam Langley4139edb2016-01-13 15:00:54 -08001143 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04001144 /* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */
Adam Langley4139edb2016-01-13 15:00:54 -08001145 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001146 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley4139edb2016-01-13 15:00:54 -08001147 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001148 return -1;
1149 }
1150
Adam Langley4139edb2016-01-13 15:00:54 -08001151 ssl->s3->tmp.reuse_message = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001152 return 1;
1153 }
1154
1155 /* Retain a copy of the original CBS to compute the signature over. */
David Benjamin4969cc92016-04-22 15:02:23 -04001156 CBS server_key_exchange;
David Benjaminc895d6b2016-08-11 13:26:41 -04001157 CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num);
David Benjamin4969cc92016-04-22 15:02:23 -04001158 CBS server_key_exchange_orig = server_key_exchange;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001159
David Benjamin4969cc92016-04-22 15:02:23 -04001160 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1161 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001162
1163 if (alg_a & SSL_aPSK) {
1164 CBS psk_identity_hint;
1165
1166 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1167 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1168 &psk_identity_hint)) {
1169 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001170 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001171 goto f_err;
1172 }
1173
1174 /* Store PSK identity hint for later use, hint is used in
1175 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1176 * identity hint can be as long as the maximum length of a PSK identity.
1177 * Also do not allow NULL characters; identities are saved as C strings.
1178 *
1179 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1180 * a specific identity. */
1181 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1182 CBS_contains_zero_byte(&psk_identity_hint)) {
1183 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00001184 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001185 goto f_err;
1186 }
1187
David Benjamin7c0d06c2016-08-11 13:26:41 -04001188 /* Save non-empty identity hints as a C string. Empty identity hints we
1189 * treat as missing. Plain PSK makes it possible to send either no hint
1190 * (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
1191 * empty hint. Having different capabilities is odd, so we interpret empty
1192 * and missing as identical. */
1193 if (CBS_len(&psk_identity_hint) != 0 &&
1194 !CBS_strdup(&psk_identity_hint, &ssl->s3->hs->peer_psk_identity_hint)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001195 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001196 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001197 goto f_err;
1198 }
1199 }
1200
Adam Langleye9ada862015-05-11 17:20:37 -07001201 if (alg_k & SSL_kDHE) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001202 CBS dh_p, dh_g, dh_Ys;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001203 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1204 CBS_len(&dh_p) == 0 ||
1205 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1206 CBS_len(&dh_g) == 0 ||
1207 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1208 CBS_len(&dh_Ys) == 0) {
1209 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001210 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001211 goto f_err;
1212 }
1213
1214 dh = DH_new();
1215 if (dh == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001216 goto err;
1217 }
1218
Adam Langley4139edb2016-01-13 15:00:54 -08001219 dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL);
1220 dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL);
1221 if (dh->p == NULL || dh->g == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001222 goto err;
1223 }
1224
David Benjaminc895d6b2016-08-11 13:26:41 -04001225 ssl->s3->new_session->key_exchange_info = DH_num_bits(dh);
1226 if (ssl->s3->new_session->key_exchange_info < 1024) {
Kenny Rootb8494592015-09-25 02:29:14 +00001227 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001228 goto err;
David Benjaminc895d6b2016-08-11 13:26:41 -04001229 } else if (ssl->s3->new_session->key_exchange_info > 4096) {
Adam Langley4139edb2016-01-13 15:00:54 -08001230 /* Overly large DHE groups are prohibitively expensive, so enforce a limit
1231 * to prevent a server from causing us to perform too expensive of a
1232 * computation. */
1233 OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001234 goto err;
1235 }
1236
David Benjamin95add822016-10-19 01:09:12 -04001237 SSL_ECDH_CTX_init_for_dhe(&ssl->s3->hs->ecdh_ctx, dh);
Adam Langley4139edb2016-01-13 15:00:54 -08001238 dh = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001239
Adam Langley4139edb2016-01-13 15:00:54 -08001240 /* Save the peer public key for later. */
David Benjamin95add822016-10-19 01:09:12 -04001241 if (!CBS_stow(&dh_Ys, &ssl->s3->hs->peer_key, &ssl->s3->hs->peer_key_len)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001242 goto err;
1243 }
Adam Langley4139edb2016-01-13 15:00:54 -08001244 } else if (alg_k & SSL_kECDHE) {
1245 /* Parse the server parameters. */
David Benjamind316cba2016-06-02 16:17:39 -04001246 uint8_t group_type;
1247 uint16_t group_id;
Adam Langley4139edb2016-01-13 15:00:54 -08001248 CBS point;
David Benjamind316cba2016-06-02 16:17:39 -04001249 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
1250 group_type != NAMED_CURVE_TYPE ||
1251 !CBS_get_u16(&server_key_exchange, &group_id) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001252 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001253 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001254 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001255 goto f_err;
1256 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001257 ssl->s3->new_session->key_exchange_info = group_id;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001258
David Benjamind316cba2016-06-02 16:17:39 -04001259 /* Ensure the group is consistent with preferences. */
1260 if (!tls1_check_group_id(ssl, group_id)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001261 al = SSL_AD_ILLEGAL_PARAMETER;
1262 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001263 goto f_err;
1264 }
Adam Langley4139edb2016-01-13 15:00:54 -08001265
1266 /* Initialize ECDH and save the peer public key for later. */
David Benjamin95add822016-10-19 01:09:12 -04001267 if (!SSL_ECDH_CTX_init(&ssl->s3->hs->ecdh_ctx, group_id) ||
1268 !CBS_stow(&point, &ssl->s3->hs->peer_key, &ssl->s3->hs->peer_key_len)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001269 goto err;
1270 }
David Benjamind316cba2016-06-02 16:17:39 -04001271 } else if (alg_k & SSL_kCECPQ1) {
David Benjamin95add822016-10-19 01:09:12 -04001272 SSL_ECDH_CTX_init_for_cecpq1(&ssl->s3->hs->ecdh_ctx);
David Benjamind316cba2016-06-02 16:17:39 -04001273 CBS key;
1274 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &key)) {
1275 al = SSL_AD_DECODE_ERROR;
1276 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1277 goto f_err;
1278 }
1279
David Benjamin95add822016-10-19 01:09:12 -04001280 if (!CBS_stow(&key, &ssl->s3->hs->peer_key, &ssl->s3->hs->peer_key_len)) {
David Benjamind316cba2016-06-02 16:17:39 -04001281 goto err;
1282 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001283 } else if (!(alg_k & SSL_kPSK)) {
1284 al = SSL_AD_UNEXPECTED_MESSAGE;
Kenny Rootb8494592015-09-25 02:29:14 +00001285 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001286 goto f_err;
1287 }
1288
1289 /* At this point, |server_key_exchange| contains the signature, if any, while
1290 * |server_key_exchange_orig| contains the entire message. From that, derive
1291 * a CBS containing just the parameter. */
David Benjamin4969cc92016-04-22 15:02:23 -04001292 CBS parameter;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001293 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1294 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1295
Kenny Rootb8494592015-09-25 02:29:14 +00001296 /* ServerKeyExchange should be signed by the server's public key. */
David Benjamind316cba2016-06-02 16:17:39 -04001297 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001298 pkey = X509_get_pubkey(ssl->s3->new_session->x509_peer);
Kenny Rootb8494592015-09-25 02:29:14 +00001299 if (pkey == NULL) {
1300 goto err;
1301 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001302
David Benjaminc895d6b2016-08-11 13:26:41 -04001303 uint16_t signature_algorithm = 0;
David Benjamin4969cc92016-04-22 15:02:23 -04001304 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001305 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001306 al = SSL_AD_DECODE_ERROR;
1307 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001308 goto f_err;
1309 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001310 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001311 goto f_err;
1312 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001313 ssl->s3->tmp.peer_signature_algorithm = signature_algorithm;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001314 } else if (pkey->type == EVP_PKEY_RSA) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001315 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
1316 } else if (pkey->type == EVP_PKEY_EC) {
1317 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001318 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -04001319 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1320 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1321 goto f_err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001322 }
1323
1324 /* The last field in |server_key_exchange| is the signature. */
Kenny Rootb8494592015-09-25 02:29:14 +00001325 CBS signature;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001326 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1327 CBS_len(&server_key_exchange) != 0) {
1328 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001329 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001330 goto f_err;
1331 }
1332
David Benjaminc895d6b2016-08-11 13:26:41 -04001333 CBB transcript;
1334 uint8_t *transcript_data;
1335 size_t transcript_len;
1336 if (!CBB_init(&transcript, 2*SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1337 !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1338 !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1339 !CBB_add_bytes(&transcript, CBS_data(&parameter), CBS_len(&parameter)) ||
1340 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1341 CBB_cleanup(&transcript);
1342 al = SSL_AD_INTERNAL_ERROR;
1343 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1344 goto f_err;
1345 }
1346
1347 int sig_ok = ssl_public_key_verify(
1348 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
1349 pkey, transcript_data, transcript_len);
1350 OPENSSL_free(transcript_data);
1351
David Benjamin4969cc92016-04-22 15:02:23 -04001352#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1353 sig_ok = 1;
1354 ERR_clear_error();
1355#endif
1356 if (!sig_ok) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001357 /* bad signature */
1358 al = SSL_AD_DECRYPT_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001359 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001360 goto f_err;
1361 }
1362 } else {
Kenny Rootb8494592015-09-25 02:29:14 +00001363 /* PSK ciphers are the only supported certificate-less ciphers. */
1364 assert(alg_a == SSL_aPSK);
1365
Adam Langleyd9e397b2015-01-22 14:27:53 -08001366 if (CBS_len(&server_key_exchange) > 0) {
1367 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001368 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001369 goto f_err;
1370 }
1371 }
1372 EVP_PKEY_free(pkey);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001373 return 1;
1374
1375f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001376 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001377err:
1378 EVP_PKEY_free(pkey);
Adam Langleye9ada862015-05-11 17:20:37 -07001379 DH_free(dh);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001380 EC_POINT_free(srvr_ecpoint);
Adam Langleye9ada862015-05-11 17:20:37 -07001381 EC_KEY_free(ecdh);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001382 return -1;
1383}
1384
David Benjamin6e899c72016-06-09 18:02:18 -04001385static int ssl3_get_certificate_request(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001386 int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1387 if (msg_ret <= 0) {
1388 return msg_ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001389 }
1390
David Benjamin6e899c72016-06-09 18:02:18 -04001391 if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) {
Adam Langley4139edb2016-01-13 15:00:54 -08001392 ssl->s3->tmp.reuse_message = 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001393 /* If we get here we don't need the handshake buffer as we won't be doing
1394 * client auth. */
Adam Langley4139edb2016-01-13 15:00:54 -08001395 ssl3_free_handshake_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001396 return 1;
1397 }
1398
Adam Langley4139edb2016-01-13 15:00:54 -08001399 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1400 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjamin4969cc92016-04-22 15:02:23 -04001401 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001402 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001403 }
1404
David Benjamin4969cc92016-04-22 15:02:23 -04001405 CBS cbs;
David Benjaminc895d6b2016-08-11 13:26:41 -04001406 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001407
David Benjaminc895d6b2016-08-11 13:26:41 -04001408 /* Get the certificate types. */
David Benjamin4969cc92016-04-22 15:02:23 -04001409 CBS certificate_types;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001410 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001411 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Kenny Rootb8494592015-09-25 02:29:14 +00001412 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04001413 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001414 }
1415
David Benjamin95add822016-10-19 01:09:12 -04001416 if (!CBS_stow(&certificate_types, &ssl->s3->hs->certificate_types,
1417 &ssl->s3->hs->num_certificate_types)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001418 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04001419 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001420 }
1421
David Benjamin4969cc92016-04-22 15:02:23 -04001422 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001423 CBS supported_signature_algorithms;
Kenny Rootb8494592015-09-25 02:29:14 +00001424 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001425 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
1426 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Kenny Rootb8494592015-09-25 02:29:14 +00001427 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04001428 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001429 }
1430 }
1431
David Benjaminc895d6b2016-08-11 13:26:41 -04001432 uint8_t alert;
1433 STACK_OF(X509_NAME) *ca_sk = ssl_parse_client_CA_list(ssl, &alert, &cbs);
1434 if (ca_sk == NULL) {
1435 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1436 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001437 }
1438
David Benjamin7c0d06c2016-08-11 13:26:41 -04001439 if (CBS_len(&cbs) != 0) {
1440 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1441 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1442 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1443 return -1;
1444 }
1445
David Benjamin95add822016-10-19 01:09:12 -04001446 ssl->s3->hs->cert_request = 1;
1447 sk_X509_NAME_pop_free(ssl->s3->hs->ca_names, X509_NAME_free);
1448 ssl->s3->hs->ca_names = ca_sk;
David Benjaminc895d6b2016-08-11 13:26:41 -04001449 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001450}
1451
David Benjamin6e899c72016-06-09 18:02:18 -04001452static int ssl3_get_server_hello_done(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001453 int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_SERVER_HELLO_DONE,
1454 ssl_hash_message);
1455 if (ret <= 0) {
1456 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001457 }
1458
David Benjaminc895d6b2016-08-11 13:26:41 -04001459 /* ServerHelloDone is empty. */
1460 if (ssl->init_num > 0) {
Adam Langley4139edb2016-01-13 15:00:54 -08001461 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin7c0d06c2016-08-11 13:26:41 -04001462 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001463 return -1;
1464 }
1465
1466 return 1;
1467}
1468
David Benjamin6e899c72016-06-09 18:02:18 -04001469static int ssl3_send_client_certificate(SSL *ssl) {
1470 if (ssl->state == SSL3_ST_CW_CERT_A) {
1471 /* Call cert_cb to update the certificate. */
1472 if (ssl->cert->cert_cb) {
1473 int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1474 if (ret < 0) {
1475 ssl->rwstate = SSL_X509_LOOKUP;
1476 return -1;
1477 }
1478 if (ret == 0) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001479 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
David Benjamin6e899c72016-06-09 18:02:18 -04001480 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1481 return -1;
1482 }
1483 }
1484
David Benjaminc895d6b2016-08-11 13:26:41 -04001485 ssl->state = SSL3_ST_CW_CERT_B;
David Benjamin6e899c72016-06-09 18:02:18 -04001486 }
1487
1488 if (ssl->state == SSL3_ST_CW_CERT_B) {
1489 /* Call client_cert_cb to update the certificate. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001490 int should_retry;
1491 if (!ssl_do_client_cert_cb(ssl, &should_retry)) {
1492 if (should_retry) {
1493 ssl->rwstate = SSL_X509_LOOKUP;
1494 }
David Benjamin6e899c72016-06-09 18:02:18 -04001495 return -1;
1496 }
1497
David Benjaminc895d6b2016-08-11 13:26:41 -04001498 if (!ssl_has_certificate(ssl)) {
David Benjamin95add822016-10-19 01:09:12 -04001499 ssl->s3->hs->cert_request = 0;
David Benjamin6e899c72016-06-09 18:02:18 -04001500 /* Without a client certificate, the handshake buffer may be released. */
1501 ssl3_free_handshake_buffer(ssl);
1502
1503 if (ssl->version == SSL3_VERSION) {
1504 /* In SSL 3.0, send no certificate by skipping both messages. */
David Benjamin6e899c72016-06-09 18:02:18 -04001505 ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
1506 return 1;
1507 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001508 }
David Benjamin6e899c72016-06-09 18:02:18 -04001509
David Benjaminc895d6b2016-08-11 13:26:41 -04001510 if (!ssl3_output_cert_chain(ssl)) {
David Benjamin6e899c72016-06-09 18:02:18 -04001511 return -1;
1512 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001513 ssl->state = SSL3_ST_CW_CERT_C;
David Benjamin6e899c72016-06-09 18:02:18 -04001514 }
1515
David Benjaminc895d6b2016-08-11 13:26:41 -04001516 assert(ssl->state == SSL3_ST_CW_CERT_C);
1517 return ssl->method->write_message(ssl);
David Benjamin6e899c72016-06-09 18:02:18 -04001518}
1519
Kenny Roote99801b2015-11-06 15:31:15 -08001520OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned),
1521 SIZE_T_IS_SMALLER_THAN_UNSIGNED);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001522
David Benjamin6e899c72016-06-09 18:02:18 -04001523static int ssl3_send_client_key_exchange(SSL *ssl) {
Adam Langley4139edb2016-01-13 15:00:54 -08001524 if (ssl->state == SSL3_ST_CW_KEY_EXCH_B) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001525 return ssl->method->write_message(ssl);
Adam Langley4139edb2016-01-13 15:00:54 -08001526 }
1527 assert(ssl->state == SSL3_ST_CW_KEY_EXCH_A);
1528
Adam Langleyd9e397b2015-01-22 14:27:53 -08001529 uint8_t *pms = NULL;
1530 size_t pms_len = 0;
David Benjaminc895d6b2016-08-11 13:26:41 -04001531 CBB cbb, body;
1532 if (!ssl->method->init_message(ssl, &cbb, &body,
1533 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001534 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001535 }
1536
Adam Langley4139edb2016-01-13 15:00:54 -08001537 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1538 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
1539
1540 /* If using a PSK key exchange, prepare the pre-shared key. */
1541 unsigned psk_len = 0;
1542 uint8_t psk[PSK_MAX_PSK_LEN];
1543 if (alg_a & SSL_aPSK) {
1544 if (ssl->psk_client_callback == NULL) {
1545 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1546 goto err;
1547 }
1548
1549 char identity[PSK_MAX_IDENTITY_LEN + 1];
1550 memset(identity, 0, sizeof(identity));
1551 psk_len = ssl->psk_client_callback(
David Benjamin7c0d06c2016-08-11 13:26:41 -04001552 ssl, ssl->s3->hs->peer_psk_identity_hint, identity, sizeof(identity),
Adam Langley4139edb2016-01-13 15:00:54 -08001553 psk, sizeof(psk));
1554 if (psk_len == 0) {
1555 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1556 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1557 goto err;
1558 }
1559 assert(psk_len <= PSK_MAX_PSK_LEN);
1560
David Benjaminc895d6b2016-08-11 13:26:41 -04001561 OPENSSL_free(ssl->s3->new_session->psk_identity);
1562 ssl->s3->new_session->psk_identity = BUF_strdup(identity);
1563 if (ssl->s3->new_session->psk_identity == NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -08001564 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1565 goto err;
1566 }
1567
1568 /* Write out psk_identity. */
1569 CBB child;
David Benjaminc895d6b2016-08-11 13:26:41 -04001570 if (!CBB_add_u16_length_prefixed(&body, &child) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001571 !CBB_add_bytes(&child, (const uint8_t *)identity,
1572 OPENSSL_strnlen(identity, sizeof(identity))) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001573 !CBB_flush(&body)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001574 goto err;
1575 }
1576 }
1577
1578 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1579 if (alg_k & SSL_kRSA) {
1580 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1581 pms = OPENSSL_malloc(pms_len);
1582 if (pms == NULL) {
1583 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1584 goto err;
1585 }
1586
Steven Valdez909b19f2016-11-21 15:35:44 -05001587 EVP_PKEY *pkey = X509_get_pubkey(ssl->s3->new_session->x509_peer);
Adam Langley4139edb2016-01-13 15:00:54 -08001588 if (pkey == NULL) {
1589 goto err;
1590 }
1591
1592 RSA *rsa = EVP_PKEY_get0_RSA(pkey);
1593 if (rsa == NULL) {
1594 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1595 EVP_PKEY_free(pkey);
1596 goto err;
1597 }
1598
Adam Langley4139edb2016-01-13 15:00:54 -08001599 EVP_PKEY_free(pkey);
1600
1601 pms[0] = ssl->client_version >> 8;
1602 pms[1] = ssl->client_version & 0xff;
1603 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1604 goto err;
1605 }
1606
David Benjaminc895d6b2016-08-11 13:26:41 -04001607 CBB child, *enc_pms = &body;
Adam Langley4139edb2016-01-13 15:00:54 -08001608 size_t enc_pms_len;
1609 /* In TLS, there is a length prefix. */
1610 if (ssl->version > SSL3_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001611 if (!CBB_add_u16_length_prefixed(&body, &child)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001612 goto err;
1613 }
1614 enc_pms = &child;
1615 }
1616
1617 uint8_t *ptr;
1618 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
1619 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
1620 RSA_PKCS1_PADDING) ||
1621 /* Log the premaster secret, if logging is enabled. */
1622 !ssl_log_rsa_client_key_exchange(ssl, ptr, enc_pms_len, pms, pms_len) ||
1623 !CBB_did_write(enc_pms, enc_pms_len) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001624 !CBB_flush(&body)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001625 goto err;
1626 }
David Benjamind316cba2016-06-02 16:17:39 -04001627 } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) {
1628 /* Generate a keypair and serialize the public half. */
Adam Langley4139edb2016-01-13 15:00:54 -08001629 CBB child;
David Benjamin95add822016-10-19 01:09:12 -04001630 if (!SSL_ECDH_CTX_add_key(&ssl->s3->hs->ecdh_ctx, &body, &child)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001631 goto err;
1632 }
1633
1634 /* Compute the premaster. */
1635 uint8_t alert;
David Benjamin95add822016-10-19 01:09:12 -04001636 if (!SSL_ECDH_CTX_accept(&ssl->s3->hs->ecdh_ctx, &child, &pms, &pms_len,
1637 &alert, ssl->s3->hs->peer_key,
1638 ssl->s3->hs->peer_key_len)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001639 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1640 goto err;
1641 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001642 if (!CBB_flush(&body)) {
David Benjamind316cba2016-06-02 16:17:39 -04001643 goto err;
1644 }
Adam Langley4139edb2016-01-13 15:00:54 -08001645
1646 /* The key exchange state may now be discarded. */
David Benjamin95add822016-10-19 01:09:12 -04001647 SSL_ECDH_CTX_cleanup(&ssl->s3->hs->ecdh_ctx);
1648 OPENSSL_free(ssl->s3->hs->peer_key);
1649 ssl->s3->hs->peer_key = NULL;
1650 ssl->s3->hs->peer_key_len = 0;
Adam Langley4139edb2016-01-13 15:00:54 -08001651 } else if (alg_k & SSL_kPSK) {
1652 /* For plain PSK, other_secret is a block of 0s with the same length as
1653 * the pre-shared key. */
1654 pms_len = psk_len;
1655 pms = OPENSSL_malloc(pms_len);
1656 if (pms == NULL) {
1657 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1658 goto err;
1659 }
1660 memset(pms, 0, pms_len);
1661 } else {
1662 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1663 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1664 goto err;
1665 }
1666
1667 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1668 * key. */
1669 if (alg_a & SSL_aPSK) {
1670 CBB pms_cbb, child;
1671 uint8_t *new_pms;
1672 size_t new_pms_len;
1673
1674 CBB_zero(&pms_cbb);
1675 if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) ||
1676 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1677 !CBB_add_bytes(&child, pms, pms_len) ||
1678 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1679 !CBB_add_bytes(&child, psk, psk_len) ||
1680 !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) {
1681 CBB_cleanup(&pms_cbb);
1682 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1683 goto err;
1684 }
1685 OPENSSL_cleanse(pms, pms_len);
1686 OPENSSL_free(pms);
1687 pms = new_pms;
1688 pms_len = new_pms_len;
1689 }
1690
1691 /* The message must be added to the finished hash before calculating the
1692 * master secret. */
Steven Valdez909b19f2016-11-21 15:35:44 -05001693 if (!ssl_complete_message(ssl, &cbb)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001694 goto err;
1695 }
1696 ssl->state = SSL3_ST_CW_KEY_EXCH_B;
1697
David Benjaminc895d6b2016-08-11 13:26:41 -04001698 ssl->s3->new_session->master_key_length =
1699 tls1_generate_master_secret(ssl, ssl->s3->new_session->master_key, pms,
1700 pms_len);
1701 if (ssl->s3->new_session->master_key_length == 0) {
Adam Langley4139edb2016-01-13 15:00:54 -08001702 goto err;
1703 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001704 ssl->s3->new_session->extended_master_secret =
1705 ssl->s3->tmp.extended_master_secret;
Adam Langley4139edb2016-01-13 15:00:54 -08001706 OPENSSL_cleanse(pms, pms_len);
1707 OPENSSL_free(pms);
1708
David Benjaminc895d6b2016-08-11 13:26:41 -04001709 return ssl->method->write_message(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001710
1711err:
David Benjamin4969cc92016-04-22 15:02:23 -04001712 CBB_cleanup(&cbb);
Adam Langley4139edb2016-01-13 15:00:54 -08001713 if (pms != NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001714 OPENSSL_cleanse(pms, pms_len);
1715 OPENSSL_free(pms);
1716 }
1717 return -1;
1718}
1719
David Benjamin6e899c72016-06-09 18:02:18 -04001720static int ssl3_send_cert_verify(SSL *ssl) {
Adam Langley4139edb2016-01-13 15:00:54 -08001721 if (ssl->state == SSL3_ST_CW_CERT_VRFY_C) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001722 return ssl->method->write_message(ssl);
Adam Langley4139edb2016-01-13 15:00:54 -08001723 }
1724
1725 assert(ssl_has_private_key(ssl));
1726
David Benjaminc895d6b2016-08-11 13:26:41 -04001727 CBB cbb, body, child;
1728 if (!ssl->method->init_message(ssl, &cbb, &body,
1729 SSL3_MT_CERTIFICATE_VERIFY)) {
1730 goto err;
1731 }
1732
1733 uint16_t signature_algorithm;
1734 if (!tls1_choose_signature_algorithm(ssl, &signature_algorithm)) {
1735 goto err;
1736 }
1737 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
1738 /* Write out the digest type in TLS 1.2. */
1739 if (!CBB_add_u16(&body, signature_algorithm)) {
1740 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1741 goto err;
1742 }
1743 }
1744
1745 /* Set aside space for the signature. */
Adam Langley4139edb2016-01-13 15:00:54 -08001746 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001747 uint8_t *ptr;
1748 if (!CBB_add_u16_length_prefixed(&body, &child) ||
1749 !CBB_reserve(&child, &ptr, max_sig_len)) {
1750 goto err;
1751 }
1752
1753 size_t sig_len = max_sig_len;
Adam Langley4139edb2016-01-13 15:00:54 -08001754 enum ssl_private_key_result_t sign_result;
1755 if (ssl->state == SSL3_ST_CW_CERT_VRFY_A) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001756 /* The SSL3 construction for CertificateVerify does not decompose into a
1757 * single final digest and signature, and must be special-cased. */
1758 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1759 if (ssl->cert->key_method != NULL) {
1760 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
Adam Langley4139edb2016-01-13 15:00:54 -08001761 goto err;
1762 }
Adam Langley4139edb2016-01-13 15:00:54 -08001763
David Benjaminc895d6b2016-08-11 13:26:41 -04001764 const EVP_MD *md;
1765 uint8_t digest[EVP_MAX_MD_SIZE];
1766 size_t digest_len;
1767 if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
1768 signature_algorithm)) {
1769 goto err;
1770 }
1771
1772 sign_result = ssl_private_key_success;
1773
1774 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
1775 if (pctx == NULL ||
1776 !EVP_PKEY_sign_init(pctx) ||
1777 !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
1778 !EVP_PKEY_sign(pctx, ptr, &sig_len, digest, digest_len)) {
1779 EVP_PKEY_CTX_free(pctx);
1780 sign_result = ssl_private_key_failure;
1781 goto err;
1782 }
1783 EVP_PKEY_CTX_free(pctx);
1784 } else {
1785 sign_result = ssl_private_key_sign(
1786 ssl, ptr, &sig_len, max_sig_len, signature_algorithm,
1787 (const uint8_t *)ssl->s3->handshake_buffer->data,
1788 ssl->s3->handshake_buffer->length);
Adam Langley4139edb2016-01-13 15:00:54 -08001789 }
1790
1791 /* The handshake buffer is no longer necessary. */
1792 ssl3_free_handshake_buffer(ssl);
Adam Langley4139edb2016-01-13 15:00:54 -08001793 } else {
1794 assert(ssl->state == SSL3_ST_CW_CERT_VRFY_B);
David Benjaminc895d6b2016-08-11 13:26:41 -04001795 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langley4139edb2016-01-13 15:00:54 -08001796 }
1797
1798 switch (sign_result) {
1799 case ssl_private_key_success:
Adam Langley4139edb2016-01-13 15:00:54 -08001800 break;
1801 case ssl_private_key_failure:
Adam Langley4139edb2016-01-13 15:00:54 -08001802 goto err;
1803 case ssl_private_key_retry:
1804 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
1805 ssl->state = SSL3_ST_CW_CERT_VRFY_B;
1806 goto err;
1807 }
1808
Adam Langley4139edb2016-01-13 15:00:54 -08001809 if (!CBB_did_write(&child, sig_len) ||
Steven Valdez909b19f2016-11-21 15:35:44 -05001810 !ssl_complete_message(ssl, &cbb)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001811 goto err;
1812 }
1813
1814 ssl->state = SSL3_ST_CW_CERT_VRFY_C;
David Benjaminc895d6b2016-08-11 13:26:41 -04001815 return ssl->method->write_message(ssl);
Adam Langley4139edb2016-01-13 15:00:54 -08001816
1817err:
1818 CBB_cleanup(&cbb);
1819 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001820}
1821
David Benjamin6e899c72016-06-09 18:02:18 -04001822static int ssl3_send_next_proto(SSL *ssl) {
Kenny Roote99801b2015-11-06 15:31:15 -08001823 if (ssl->state == SSL3_ST_CW_NEXT_PROTO_B) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001824 return ssl->method->write_message(ssl);
Adam Langleyfdeb4882015-10-30 13:15:30 -07001825 }
1826
Kenny Roote99801b2015-11-06 15:31:15 -08001827 assert(ssl->state == SSL3_ST_CW_NEXT_PROTO_A);
Kenny Root03bcf612015-11-05 20:20:27 +00001828
Kenny Roote99801b2015-11-06 15:31:15 -08001829 static const uint8_t kZero[32] = {0};
David Benjamin4969cc92016-04-22 15:02:23 -04001830 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
Kenny Root03bcf612015-11-05 20:20:27 +00001831
David Benjaminc895d6b2016-08-11 13:26:41 -04001832 CBB cbb, body, child;
1833 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) ||
1834 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamin4969cc92016-04-22 15:02:23 -04001835 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1836 ssl->s3->next_proto_negotiated_len) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001837 !CBB_add_u8_length_prefixed(&body, &child) ||
Kenny Roote99801b2015-11-06 15:31:15 -08001838 !CBB_add_bytes(&child, kZero, padding_len) ||
Steven Valdez909b19f2016-11-21 15:35:44 -05001839 !ssl_complete_message(ssl, &cbb)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001840 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1841 CBB_cleanup(&cbb);
Adam Langleyfdeb4882015-10-30 13:15:30 -07001842 return -1;
1843 }
Adam Langleyfdeb4882015-10-30 13:15:30 -07001844
Kenny Roote99801b2015-11-06 15:31:15 -08001845 ssl->state = SSL3_ST_CW_NEXT_PROTO_B;
David Benjaminc895d6b2016-08-11 13:26:41 -04001846 return ssl->method->write_message(ssl);
Kenny Roote99801b2015-11-06 15:31:15 -08001847}
1848
David Benjamin6e899c72016-06-09 18:02:18 -04001849static int ssl3_send_channel_id(SSL *ssl) {
Kenny Roote99801b2015-11-06 15:31:15 -08001850 if (ssl->state == SSL3_ST_CW_CHANNEL_ID_B) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001851 return ssl->method->write_message(ssl);
Kenny Roote99801b2015-11-06 15:31:15 -08001852 }
1853
1854 assert(ssl->state == SSL3_ST_CW_CHANNEL_ID_A);
1855
Steven Valdez909b19f2016-11-21 15:35:44 -05001856 if (!ssl_do_channel_id_callback(ssl)) {
1857 return -1;
Kenny Roote99801b2015-11-06 15:31:15 -08001858 }
1859
1860 if (ssl->tlsext_channel_id_private == NULL) {
1861 ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
1862 return -1;
1863 }
Kenny Roote99801b2015-11-06 15:31:15 -08001864
Steven Valdez909b19f2016-11-21 15:35:44 -05001865 CBB cbb, body;
1866 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
1867 !tls1_write_channel_id(ssl, &body) ||
1868 !ssl_complete_message(ssl, &cbb)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001869 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez909b19f2016-11-21 15:35:44 -05001870 CBB_cleanup(&cbb);
Kenny Rootb8494592015-09-25 02:29:14 +00001871 return -1;
Kenny Roota04d78d2015-09-25 00:26:37 +00001872 }
Kenny Rootb8494592015-09-25 02:29:14 +00001873
Kenny Roote99801b2015-11-06 15:31:15 -08001874 ssl->state = SSL3_ST_CW_CHANNEL_ID_B;
Steven Valdez909b19f2016-11-21 15:35:44 -05001875 return ssl->method->write_message(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001876}
1877
David Benjamin6e899c72016-06-09 18:02:18 -04001878static int ssl3_get_new_session_ticket(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001879 int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_NEW_SESSION_TICKET,
1880 ssl_hash_message);
1881 if (ret <= 0) {
1882 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001883 }
David Benjamin4969cc92016-04-22 15:02:23 -04001884
David Benjamin6e899c72016-06-09 18:02:18 -04001885 CBS new_session_ticket, ticket;
David Benjaminc895d6b2016-08-11 13:26:41 -04001886 uint32_t tlsext_tick_lifetime_hint;
1887 CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num);
1888 if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
David Benjamin6e899c72016-06-09 18:02:18 -04001889 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1890 CBS_len(&new_session_ticket) != 0) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001891 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin6e899c72016-06-09 18:02:18 -04001892 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04001893 return -1;
David Benjamin4969cc92016-04-22 15:02:23 -04001894 }
1895
David Benjamin6e899c72016-06-09 18:02:18 -04001896 if (CBS_len(&ticket) == 0) {
1897 /* RFC 5077 allows a server to change its mind and send no ticket after
Steven Valdez909b19f2016-11-21 15:35:44 -05001898 * negotiating the extension. The value of |ticket_expected| is checked in
1899 * |ssl_update_cache| so is cleared here to avoid an unnecessary update. */
1900 ssl->s3->hs->ticket_expected = 0;
David Benjamin6e899c72016-06-09 18:02:18 -04001901 return 1;
1902 }
1903
David Benjaminc895d6b2016-08-11 13:26:41 -04001904 int session_renewed = ssl->session != NULL;
1905 SSL_SESSION *session = ssl->s3->new_session;
1906 if (session_renewed) {
David Benjamin6e899c72016-06-09 18:02:18 -04001907 /* The server is sending a new ticket for an existing session. Sessions are
1908 * immutable once established, so duplicate all but the ticket of the
1909 * existing session. */
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001910 session = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
David Benjaminc895d6b2016-08-11 13:26:41 -04001911 if (session == NULL) {
David Benjamin6e899c72016-06-09 18:02:18 -04001912 /* This should never happen. */
1913 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1914 goto err;
1915 }
David Benjamin6e899c72016-06-09 18:02:18 -04001916 }
1917
Steven Valdez909b19f2016-11-21 15:35:44 -05001918 /* |tlsext_tick_lifetime_hint| is measured from when the ticket was issued. */
1919 ssl_session_refresh_time(ssl, session);
1920
David Benjaminc895d6b2016-08-11 13:26:41 -04001921 if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
David Benjamin6e899c72016-06-09 18:02:18 -04001922 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1923 goto err;
1924 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001925 session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
David Benjamin6e899c72016-06-09 18:02:18 -04001926
1927 /* Generate a session ID for this session based on the session ticket. We use
1928 * the session ID mechanism for detecting ticket resumption. This also fits in
1929 * with assumptions elsewhere in OpenSSL.*/
David Benjaminc895d6b2016-08-11 13:26:41 -04001930 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1931 session->session_id, &session->session_id_length,
1932 EVP_sha256(), NULL)) {
David Benjamin6e899c72016-06-09 18:02:18 -04001933 goto err;
1934 }
1935
David Benjaminc895d6b2016-08-11 13:26:41 -04001936 if (session_renewed) {
1937 session->not_resumable = 0;
1938 SSL_SESSION_free(ssl->session);
1939 ssl->session = session;
1940 }
1941
David Benjamin4969cc92016-04-22 15:02:23 -04001942 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001943
David Benjamin6e899c72016-06-09 18:02:18 -04001944err:
David Benjaminc895d6b2016-08-11 13:26:41 -04001945 if (session_renewed) {
1946 SSL_SESSION_free(session);
1947 }
David Benjamin6e899c72016-06-09 18:02:18 -04001948 return -1;
Kenny Rootb8494592015-09-25 02:29:14 +00001949}