blob: 0f0c8620aca5f981f1f1b5c6e53f4a0515e4b29f [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 *
113 * Portions of the attached software ("Contribution") are developed by
114 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115 *
116 * The Contribution is licensed pursuant to the OpenSSL open source
117 * license provided above.
118 *
119 * ECC cipher suite support in OpenSSL originally written by
120 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121 *
122 */
123/* ====================================================================
124 * Copyright 2005 Nokia. All rights reserved.
125 *
126 * The portions of the attached software ("Contribution") is developed by
127 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128 * license.
129 *
130 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132 * support (see RFC 4279) to OpenSSL.
133 *
134 * No patent licenses or other rights except those expressly stated in
135 * the OpenSSL open source license shall be deemed granted or received
136 * expressly, by implication, estoppel, or otherwise.
137 *
138 * No assurances are provided by Nokia that the Contribution does not
139 * infringe the patent or other intellectual property rights of any third
140 * party or that the license provides you with all the necessary rights
141 * to make use of the Contribution.
142 *
143 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147 * OTHERWISE. */
148
David Benjamin9e4e01e2015-09-15 01:48:04 -0400149#include <openssl/ssl.h>
150
David Benjamin880b14e2014-08-25 22:35:07 -0400151#include <assert.h>
David Benjamin22f9bcc2014-07-13 12:29:21 -0400152#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700153
154#include <openssl/bn.h>
155#include <openssl/buf.h>
David Benjamindc72ff72014-06-25 12:36:10 -0400156#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700157#include <openssl/cipher.h>
158#include <openssl/dh.h>
Adam Langley1258b6a2014-06-20 12:00:00 -0700159#include <openssl/ec.h>
160#include <openssl/ecdsa.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400161#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700162#include <openssl/evp.h>
163#include <openssl/hmac.h>
164#include <openssl/md5.h>
165#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400166#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700167#include <openssl/rand.h>
168#include <openssl/x509.h>
169
David Benjamin2ee94aa2015-04-07 22:38:30 -0400170#include "internal.h"
Adam Langleyaf6e45b2014-11-03 19:34:49 -0800171#include "../crypto/internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langleyfcf25832014-12-18 17:42:32 -0800173
David Benjaminc3c88822016-11-14 10:32:04 +0900174static int ssl3_get_client_hello(SSL_HANDSHAKE *hs);
175static int ssl3_send_server_hello(SSL_HANDSHAKE *hs);
176static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs);
177static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs);
178static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs);
179static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs);
180static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs);
181static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs);
182static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs);
183static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs);
184static int ssl3_get_next_proto(SSL_HANDSHAKE *hs);
185static int ssl3_get_channel_id(SSL_HANDSHAKE *hs);
186static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs);
David Benjamindf50eec2016-06-07 16:49:42 -0400187
David Benjamince8c9d22016-11-14 10:45:16 +0900188int ssl3_accept(SSL_HANDSHAKE *hs) {
189 SSL *const ssl = hs->ssl;
David Benjamin3fa27772015-04-17 22:32:19 -0400190 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -0800191 int ret = -1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400192 int state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700193
David Benjamin0d56f882015-12-19 17:05:56 -0500194 assert(ssl->handshake_func == ssl3_accept);
195 assert(ssl->server);
David Benjaminbeb47022014-11-30 02:58:52 -0500196
Adam Langleyfcf25832014-12-18 17:42:32 -0800197 for (;;) {
David Benjamin0d56f882015-12-19 17:05:56 -0500198 state = ssl->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700199
David Benjamin0d56f882015-12-19 17:05:56 -0500200 switch (ssl->state) {
David Benjaminfc0c9d92016-09-01 01:33:33 -0400201 case SSL_ST_INIT:
202 ssl->state = SSL_ST_ACCEPT;
203 skip = 1;
204 break;
205
Adam Langleyfcf25832014-12-18 17:42:32 -0800206 case SSL_ST_ACCEPT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400207 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700208
David Benjamin6a08da22015-05-08 22:58:12 -0400209 /* Enable a write buffer. This groups handshake messages within a flight
210 * into a single write. */
David Benjaminb095f0f2016-05-05 21:50:24 -0400211 if (!ssl_init_wbio_buffer(ssl)) {
David Benjamin6a08da22015-05-08 22:58:12 -0400212 ret = -1;
213 goto end;
214 }
215
David Benjamin0d56f882015-12-19 17:05:56 -0500216 if (!ssl3_init_handshake_buffer(ssl)) {
David Benjamin3570d732015-06-29 00:28:17 -0400217 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 ret = -1;
219 goto end;
220 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500221
David Benjamin0d56f882015-12-19 17:05:56 -0500222 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800223 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500224
Adam Langleyfcf25832014-12-18 17:42:32 -0800225 case SSL3_ST_SR_CLNT_HELLO_A:
226 case SSL3_ST_SR_CLNT_HELLO_B:
227 case SSL3_ST_SR_CLNT_HELLO_C:
David Benjamin25fe85b2016-08-09 20:00:32 -0400228 case SSL3_ST_SR_CLNT_HELLO_D:
David Benjamin4eb95cc2016-11-16 17:08:23 +0900229 case SSL3_ST_SR_CLNT_HELLO_E:
David Benjaminc3c88822016-11-14 10:32:04 +0900230 ret = ssl3_get_client_hello(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400231 if (ssl->state == SSL_ST_TLS13) {
232 break;
233 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800234 if (ret <= 0) {
235 goto end;
236 }
David Benjamindf50eec2016-06-07 16:49:42 -0400237 ssl->method->received_flight(ssl);
David Benjamin0d56f882015-12-19 17:05:56 -0500238 ssl->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500240
Adam Langleyfcf25832014-12-18 17:42:32 -0800241 case SSL3_ST_SW_SRVR_HELLO_A:
242 case SSL3_ST_SW_SRVR_HELLO_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900243 ret = ssl3_send_server_hello(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800244 if (ret <= 0) {
245 goto end;
246 }
Steven Valdez87eab492016-06-27 16:34:59 -0400247 if (ssl->session != NULL) {
David Benjamin47749a62016-06-13 13:56:33 -0400248 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500250 ssl->state = SSL3_ST_SW_CERT_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700253
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 case SSL3_ST_SW_CERT_A:
255 case SSL3_ST_SW_CERT_B:
David Benjamin3d458dc2016-09-12 22:40:27 +0000256 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900257 ret = ssl3_send_server_certificate(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 if (ret <= 0) {
259 goto end;
260 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 } else {
262 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 }
David Benjamin47749a62016-06-13 13:56:33 -0400264 ssl->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800265 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700266
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100267 case SSL3_ST_SW_CERT_STATUS_A:
268 case SSL3_ST_SW_CERT_STATUS_B:
David Benjamince8c9d22016-11-14 10:45:16 +0900269 if (hs->certificate_status_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900270 ret = ssl3_send_certificate_status(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400271 if (ret <= 0) {
272 goto end;
273 }
274 } else {
275 skip = 1;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100276 }
David Benjamin0d56f882015-12-19 17:05:56 -0500277 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100278 break;
279
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 case SSL3_ST_SW_KEY_EXCH_A:
281 case SSL3_ST_SW_KEY_EXCH_B:
nagendra modadugu601448a2015-07-24 09:31:31 -0700282 case SSL3_ST_SW_KEY_EXCH_C:
David Benjamin0d56f882015-12-19 17:05:56 -0500283 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700284
David Benjamindf50eec2016-06-07 16:49:42 -0400285 /* PSK ciphers send ServerKeyExchange if there is an identity hint. */
David Benjamin0d56f882015-12-19 17:05:56 -0500286 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
287 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900288 ret = ssl3_send_server_key_exchange(hs);
David Benjamin6eb000d2015-02-11 01:17:41 -0500289 if (ret <= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500291 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800292 } else {
293 skip = 1;
294 }
Adam Langley95c29f32014-06-20 12:00:00 -0700295
David Benjamin0d56f882015-12-19 17:05:56 -0500296 ssl->state = SSL3_ST_SW_CERT_REQ_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800297 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700298
Adam Langleyfcf25832014-12-18 17:42:32 -0800299 case SSL3_ST_SW_CERT_REQ_A:
300 case SSL3_ST_SW_CERT_REQ_B:
David Benjamince8c9d22016-11-14 10:45:16 +0900301 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900302 ret = ssl3_send_certificate_request(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800303 if (ret <= 0) {
304 goto end;
305 }
David Benjamin5c1ce292015-05-26 16:59:43 -0400306 } else {
307 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800308 }
David Benjamin0d56f882015-12-19 17:05:56 -0500309 ssl->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800310 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700311
Adam Langleyfcf25832014-12-18 17:42:32 -0800312 case SSL3_ST_SW_SRVR_DONE_A:
313 case SSL3_ST_SW_SRVR_DONE_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900314 ret = ssl3_send_server_hello_done(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800315 if (ret <= 0) {
316 goto end;
317 }
David Benjamin0d56f882015-12-19 17:05:56 -0500318 ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
319 ssl->state = SSL3_ST_SW_FLUSH;
Adam Langleyfcf25832014-12-18 17:42:32 -0800320 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700321
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 case SSL3_ST_SR_CERT_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900323 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900324 ret = ssl3_get_client_certificate(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 if (ret <= 0) {
326 goto end;
327 }
328 }
David Benjamin0d56f882015-12-19 17:05:56 -0500329 ssl->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700331
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 case SSL3_ST_SR_KEY_EXCH_A:
333 case SSL3_ST_SR_KEY_EXCH_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900334 ret = ssl3_get_client_key_exchange(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800335 if (ret <= 0) {
336 goto end;
337 }
David Benjamin0d56f882015-12-19 17:05:56 -0500338 ssl->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800339 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700340
Adam Langleyfcf25832014-12-18 17:42:32 -0800341 case SSL3_ST_SR_CERT_VRFY_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900342 ret = ssl3_get_cert_verify(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800343 if (ret <= 0) {
344 goto end;
345 }
Adam Langley95c29f32014-06-20 12:00:00 -0700346
David Benjamin0d56f882015-12-19 17:05:56 -0500347 ssl->state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800348 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700349
David Benjamina41280d2015-11-26 02:16:49 -0500350 case SSL3_ST_SR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400351 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500352 if (ret <= 0) {
353 goto end;
354 }
Adam Langley95c29f32014-06-20 12:00:00 -0700355
David Benjamin67739722016-11-17 17:03:59 +0900356 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 ret = -1;
358 goto end;
359 }
David Benjamina41280d2015-11-26 02:16:49 -0500360
David Benjamin47749a62016-06-13 13:56:33 -0400361 ssl->state = SSL3_ST_SR_NEXT_PROTO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800362 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700363
Adam Langleyfcf25832014-12-18 17:42:32 -0800364 case SSL3_ST_SR_NEXT_PROTO_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900365 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900366 ret = ssl3_get_next_proto(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400367 if (ret <= 0) {
368 goto end;
369 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 } else {
David Benjamin47749a62016-06-13 13:56:33 -0400371 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800372 }
David Benjamin47749a62016-06-13 13:56:33 -0400373 ssl->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700375
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 case SSL3_ST_SR_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400377 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900378 ret = ssl3_get_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400379 if (ret <= 0) {
380 goto end;
381 }
382 } else {
383 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800384 }
David Benjamin0d56f882015-12-19 17:05:56 -0500385 ssl->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700387
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 case SSL3_ST_SR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900389 ret = ssl3_get_finished(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 if (ret <= 0) {
391 goto end;
392 }
Adam Langley95c29f32014-06-20 12:00:00 -0700393
David Benjamindf50eec2016-06-07 16:49:42 -0400394 ssl->method->received_flight(ssl);
Steven Valdez87eab492016-06-27 16:34:59 -0400395 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500396 ssl->state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 } else {
David Benjamin47749a62016-06-13 13:56:33 -0400398 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 }
David Benjamindf50eec2016-06-07 16:49:42 -0400400
Steven Valdez87eab492016-06-27 16:34:59 -0400401 /* If this is a full handshake with ChannelID then record the handshake
402 * hashes in |ssl->s3->new_session| in case we need them to verify a
403 * ChannelID signature on a resumption of this session in the future. */
404 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) {
David Benjamin0d56f882015-12-19 17:05:56 -0500405 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 if (ret <= 0) {
407 goto end;
408 }
409 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800410 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700411
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 case SSL3_ST_SW_SESSION_TICKET_A:
413 case SSL3_ST_SW_SESSION_TICKET_B:
David Benjamince8c9d22016-11-14 10:45:16 +0900414 if (hs->ticket_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900415 ret = ssl3_send_new_session_ticket(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400416 if (ret <= 0) {
417 goto end;
418 }
419 } else {
420 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800421 }
David Benjamin352d0a92016-06-28 11:22:02 -0400422 ssl->state = SSL3_ST_SW_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700424
David Benjamin352d0a92016-06-28 11:22:02 -0400425 case SSL3_ST_SW_CHANGE:
426 ret = ssl->method->send_change_cipher_spec(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800427 if (ret <= 0) {
428 goto end;
429 }
David Benjamin0d56f882015-12-19 17:05:56 -0500430 ssl->state = SSL3_ST_SW_FINISHED_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700431
David Benjamin67739722016-11-17 17:03:59 +0900432 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800433 ret = -1;
434 goto end;
435 }
436 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700437
Adam Langleyfcf25832014-12-18 17:42:32 -0800438 case SSL3_ST_SW_FINISHED_A:
439 case SSL3_ST_SW_FINISHED_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900440 ret = ssl3_send_finished(hs, SSL3_ST_SW_FINISHED_A,
David Benjaminbaa12162015-12-29 19:13:58 -0500441 SSL3_ST_SW_FINISHED_B);
Adam Langleyfcf25832014-12-18 17:42:32 -0800442 if (ret <= 0) {
443 goto end;
444 }
David Benjamin0d56f882015-12-19 17:05:56 -0500445 ssl->state = SSL3_ST_SW_FLUSH;
Steven Valdez87eab492016-06-27 16:34:59 -0400446 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500447 ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800448 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500449 ssl->s3->tmp.next_state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800450 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800451 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700452
David Benjamin9f1dc822016-06-07 17:03:46 -0400453 case SSL3_ST_SW_FLUSH:
454 if (BIO_flush(ssl->wbio) <= 0) {
455 ssl->rwstate = SSL_WRITING;
456 ret = -1;
457 goto end;
458 }
459
460 ssl->state = ssl->s3->tmp.next_state;
461 if (ssl->state != SSL_ST_OK) {
462 ssl->method->expect_flight(ssl);
463 }
464 break;
465
Steven Valdez143e8b32016-07-11 13:19:03 -0400466 case SSL_ST_TLS13:
David Benjaminc3c88822016-11-14 10:32:04 +0900467 ret = tls13_handshake(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400468 if (ret <= 0) {
469 goto end;
470 }
471 ssl->state = SSL_ST_OK;
472 break;
473
Adam Langleyfcf25832014-12-18 17:42:32 -0800474 case SSL_ST_OK:
David Benjamin4497e582016-07-27 17:51:49 -0400475 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700476
Steven Valdez87eab492016-06-27 16:34:59 -0400477 /* If we aren't retaining peer certificates then we can discard it
478 * now. */
479 if (ssl->s3->new_session != NULL &&
David Benjaminbbaf3672016-11-17 10:53:09 +0900480 ssl->retain_only_sha256_of_client_certs) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800481 X509_free(ssl->s3->new_session->x509_peer);
482 ssl->s3->new_session->x509_peer = NULL;
483 sk_X509_pop_free(ssl->s3->new_session->x509_chain, X509_free);
484 ssl->s3->new_session->x509_chain = NULL;
Steven Valdez87eab492016-06-27 16:34:59 -0400485 }
486
487 SSL_SESSION_free(ssl->s3->established_session);
488 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400489 SSL_SESSION_up_ref(ssl->session);
490 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400491 } else {
492 ssl->s3->established_session = ssl->s3->new_session;
493 ssl->s3->established_session->not_resumable = 0;
494 ssl->s3->new_session = NULL;
495 }
496
Adam Langleyfcf25832014-12-18 17:42:32 -0800497 /* remove buffering on output */
David Benjamin0d56f882015-12-19 17:05:56 -0500498 ssl_free_wbio_buffer(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700499
David Benjamin78476f62016-11-12 11:20:55 +0900500 ssl->s3->initial_handshake_complete = 1;
501 ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER);
502
David Benjamin4e9cc712016-06-01 20:16:03 -0400503 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800504 ret = 1;
505 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700506
Adam Langleyfcf25832014-12-18 17:42:32 -0800507 default:
David Benjamin3570d732015-06-29 00:28:17 -0400508 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800509 ret = -1;
510 goto end;
511 }
Adam Langley95c29f32014-06-20 12:00:00 -0700512
David Benjamin4e9cc712016-06-01 20:16:03 -0400513 if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) {
514 int new_state = ssl->state;
David Benjamin0d56f882015-12-19 17:05:56 -0500515 ssl->state = state;
David Benjamin4e9cc712016-06-01 20:16:03 -0400516 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
David Benjamin0d56f882015-12-19 17:05:56 -0500517 ssl->state = new_state;
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 }
519 skip = 0;
520 }
521
Adam Langley95c29f32014-06-20 12:00:00 -0700522end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400523 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800524 return ret;
525}
Adam Langley95c29f32014-06-20 12:00:00 -0700526
David Benjamin1deb41b2016-08-09 19:36:38 -0400527int ssl_client_cipher_list_contains_cipher(
528 const struct ssl_early_callback_ctx *client_hello, uint16_t id) {
529 CBS cipher_suites;
530 CBS_init(&cipher_suites, client_hello->cipher_suites,
531 client_hello->cipher_suites_len);
532
533 while (CBS_len(&cipher_suites) > 0) {
534 uint16_t got_id;
535 if (!CBS_get_u16(&cipher_suites, &got_id)) {
536 return 0;
537 }
538
539 if (got_id == id) {
540 return 1;
541 }
542 }
543
544 return 0;
545}
546
David Benjamin25fe85b2016-08-09 20:00:32 -0400547static int negotiate_version(
David Benjamin9ef31f02016-10-31 18:01:13 -0400548 SSL *ssl, uint8_t *out_alert,
David Benjamin25fe85b2016-08-09 20:00:32 -0400549 const struct ssl_early_callback_ctx *client_hello) {
550 uint16_t min_version, max_version;
551 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
552 *out_alert = SSL_AD_PROTOCOL_VERSION;
553 return 0;
554 }
555
David Benjamin2dc02042016-09-19 19:57:37 -0400556 uint16_t version = 0;
Steven Valdezfdd10992016-09-15 16:27:05 -0400557 /* Check supported_versions extension if it is present. */
558 CBS supported_versions;
559 if (ssl_early_callback_get_extension(client_hello, &supported_versions,
560 TLSEXT_TYPE_supported_versions)) {
561 CBS versions;
562 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
563 CBS_len(&supported_versions) != 0 ||
564 CBS_len(&versions) == 0) {
565 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
566 *out_alert = SSL_AD_DECODE_ERROR;
567 return 0;
568 }
569
Brian Smithf85d3232016-10-28 10:34:06 -1000570 /* Choose the newest commonly-supported version advertised by the client.
571 * The client orders the versions according to its preferences, but we're
572 * not required to honor the client's preferences. */
Steven Valdezfdd10992016-09-15 16:27:05 -0400573 int found_version = 0;
574 while (CBS_len(&versions) != 0) {
575 uint16_t ext_version;
576 if (!CBS_get_u16(&versions, &ext_version)) {
577 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
578 *out_alert = SSL_AD_DECODE_ERROR;
579 return 0;
580 }
581 if (!ssl->method->version_from_wire(&ext_version, ext_version)) {
582 continue;
583 }
584 if (min_version <= ext_version &&
Brian Smithf85d3232016-10-28 10:34:06 -1000585 ext_version <= max_version &&
586 (!found_version || version < ext_version)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400587 version = ext_version;
588 found_version = 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400589 }
590 }
591
592 if (!found_version) {
593 goto unsupported_protocol;
David Benjamin2dc02042016-09-19 19:57:37 -0400594 }
595 } else {
Steven Valdezfdd10992016-09-15 16:27:05 -0400596 /* Process ClientHello.version instead. Note that versions beyond (D)TLS 1.2
597 * do not use this mechanism. */
598 if (SSL_is_dtls(ssl)) {
599 if (client_hello->version <= DTLS1_2_VERSION) {
600 version = TLS1_2_VERSION;
601 } else if (client_hello->version <= DTLS1_VERSION) {
602 version = TLS1_1_VERSION;
603 } else {
604 goto unsupported_protocol;
605 }
606 } else {
607 if (client_hello->version >= TLS1_2_VERSION) {
608 version = TLS1_2_VERSION;
609 } else if (client_hello->version >= TLS1_1_VERSION) {
610 version = TLS1_1_VERSION;
611 } else if (client_hello->version >= TLS1_VERSION) {
612 version = TLS1_VERSION;
613 } else if (client_hello->version >= SSL3_VERSION) {
614 version = SSL3_VERSION;
615 } else {
616 goto unsupported_protocol;
617 }
David Benjamin2dc02042016-09-19 19:57:37 -0400618 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400619
Steven Valdezfdd10992016-09-15 16:27:05 -0400620 /* Apply our minimum and maximum version. */
621 if (version > max_version) {
622 version = max_version;
623 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400624
Steven Valdezfdd10992016-09-15 16:27:05 -0400625 if (version < min_version) {
626 goto unsupported_protocol;
627 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400628 }
629
630 /* Handle FALLBACK_SCSV. */
631 if (ssl_client_cipher_list_contains_cipher(client_hello,
632 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
633 version < max_version) {
634 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
635 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
636 return 0;
637 }
638
David Benjamin2dc02042016-09-19 19:57:37 -0400639 ssl->client_version = client_hello->version;
David Benjamin25fe85b2016-08-09 20:00:32 -0400640 ssl->version = ssl->method->version_to_wire(version);
641 ssl->s3->enc_method = ssl3_get_enc_method(version);
642 assert(ssl->s3->enc_method != NULL);
643
644 /* At this point, the connection's version is known and |ssl->version| is
645 * fixed. Begin enforcing the record-layer version. */
646 ssl->s3->have_version = 1;
647
648 return 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400649
650unsupported_protocol:
651 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
652 *out_alert = SSL_AD_PROTOCOL_VERSION;
653 return 0;
David Benjamin25fe85b2016-08-09 20:00:32 -0400654}
655
David Benjaminc3c88822016-11-14 10:32:04 +0900656static int ssl3_get_client_hello(SSL_HANDSHAKE *hs) {
657 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -0400658 uint8_t al = SSL_AD_INTERNAL_ERROR;
659 int ret = -1;
David Benjamine3aa1d92015-06-16 15:34:50 -0400660 SSL_SESSION *session = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700661
David Benjamine14ff062016-08-09 16:21:24 -0400662 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_A) {
663 /* The first time around, read the ClientHello. */
664 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO,
665 ssl_hash_message);
666 if (msg_ret <= 0) {
667 return msg_ret;
David Benjamin09eb6552016-07-08 14:32:11 -0700668 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400669
David Benjamine14ff062016-08-09 16:21:24 -0400670 ssl->state = SSL3_ST_SR_CLNT_HELLO_B;
Adam Langleyfcf25832014-12-18 17:42:32 -0800671 }
Adam Langley95c29f32014-06-20 12:00:00 -0700672
David Benjamine14ff062016-08-09 16:21:24 -0400673 struct ssl_early_callback_ctx client_hello;
674 if (!ssl_early_callback_init(ssl, &client_hello, ssl->init_msg,
675 ssl->init_num)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800676 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400677 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800678 goto f_err;
679 }
Adam Langley95c29f32014-06-20 12:00:00 -0700680
David Benjamine14ff062016-08-09 16:21:24 -0400681 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_B) {
682 /* Unlike other callbacks, the early callback is not run a second time if
683 * paused. */
684 ssl->state = SSL3_ST_SR_CLNT_HELLO_C;
David Benjaminb6a0a512016-06-21 10:33:21 -0400685
David Benjamine14ff062016-08-09 16:21:24 -0400686 /* Run the early callback. */
687 if (ssl->ctx->select_certificate_cb != NULL) {
688 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
689 case 0:
690 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
691 goto err;
692
693 case -1:
694 /* Connection rejected. */
David Benjamin2c66e072016-09-16 15:58:00 -0400695 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamine14ff062016-08-09 16:21:24 -0400696 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
697 goto f_err;
698
699 default:
700 /* fallthrough */;
701 }
702 }
703 }
704
David Benjamin25fe85b2016-08-09 20:00:32 -0400705 /* Negotiate the protocol version if we have not done so yet. */
David Benjamin0d56f882015-12-19 17:05:56 -0500706 if (!ssl->s3->have_version) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400707 if (!negotiate_version(ssl, &al, &client_hello)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800708 goto f_err;
709 }
David Benjamin1deb41b2016-08-09 19:36:38 -0400710
David Benjamin25fe85b2016-08-09 20:00:32 -0400711 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
712 ssl->state = SSL_ST_TLS13;
David Benjaminc3c88822016-11-14 10:32:04 +0900713 hs->do_tls13_handshake = tls13_server_handshake;
David Benjamin25fe85b2016-08-09 20:00:32 -0400714 return 1;
715 }
716 }
717
718 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_C) {
719 /* Load the client random. */
720 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
721 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
722 return -1;
723 }
724 memcpy(ssl->s3->client_random, client_hello.random,
725 client_hello.random_len);
726
David Benjamin4eb95cc2016-11-16 17:08:23 +0900727 /* Only null compression is supported. */
728 if (memchr(client_hello.compression_methods, 0,
729 client_hello.compression_methods_len) == NULL) {
730 al = SSL_AD_ILLEGAL_PARAMETER;
731 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
732 goto f_err;
733 }
734
735 /* TLS extensions. */
David Benjamin8c880a22016-12-03 02:20:34 -0500736 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900737 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
738 goto err;
739 }
740
741 ssl->state = SSL3_ST_SR_CLNT_HELLO_D;
742 }
743
744 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_D) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400745 /* Call |cert_cb| to update server certificates if required. */
746 if (ssl->cert->cert_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500747 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800748 if (rv == 0) {
749 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400750 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800751 goto f_err;
752 }
753 if (rv < 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500754 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langleyfcf25832014-12-18 17:42:32 -0800755 goto err;
756 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800757 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800758
David Benjaminf01f42a2016-11-16 19:05:33 +0900759 /* Negotiate the cipher suite. This must be done after |cert_cb| so the
760 * certificate is finalized. */
761 ssl->s3->tmp.new_cipher =
762 ssl3_choose_cipher(ssl, &client_hello, ssl_get_cipher_preferences(ssl));
763 if (ssl->s3->tmp.new_cipher == NULL) {
764 al = SSL_AD_HANDSHAKE_FAILURE;
765 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
766 goto f_err;
767 }
768
David Benjamin34202b92016-11-16 19:07:53 +0900769 ssl->state = SSL3_ST_SR_CLNT_HELLO_E;
770 }
771
772 assert(ssl->state == SSL3_ST_SR_CLNT_HELLO_E);
773
774 /* Determine whether we are doing session resumption. */
775 int tickets_supported = 0, renew_ticket = 0;
776 switch (ssl_get_prev_session(ssl, &session, &tickets_supported, &renew_ticket,
777 &client_hello)) {
778 case ssl_session_success:
779 break;
780 case ssl_session_error:
781 goto err;
782 case ssl_session_retry:
783 ssl->rwstate = SSL_PENDING_SESSION;
784 goto err;
785 }
786
787 if (session != NULL) {
788 if (session->extended_master_secret &&
789 !ssl->s3->tmp.extended_master_secret) {
790 /* A ClientHello without EMS that attempts to resume a session with EMS
791 * is fatal to the connection. */
792 al = SSL_AD_HANDSHAKE_FAILURE;
793 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
794 goto f_err;
795 }
796
797 if (!ssl_session_is_resumable(ssl, session) ||
798 /* If the client offers the EMS extension, but the previous session
799 * didn't use it, then negotiate a new session. */
800 ssl->s3->tmp.extended_master_secret !=
801 session->extended_master_secret) {
802 SSL_SESSION_free(session);
803 session = NULL;
804 }
805 }
806
807 if (session != NULL) {
808 /* Use the old session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900809 hs->ticket_expected = renew_ticket;
David Benjamin34202b92016-11-16 19:07:53 +0900810 ssl->session = session;
811 session = NULL;
812 ssl->s3->session_reused = 1;
813 } else {
David Benjaminc3c88822016-11-14 10:32:04 +0900814 hs->ticket_expected = tickets_supported;
David Benjamin34202b92016-11-16 19:07:53 +0900815 ssl_set_session(ssl, NULL);
816 if (!ssl_get_new_session(ssl, 1 /* server */)) {
817 goto err;
818 }
819
820 /* Clear the session ID if we want the session to be single-use. */
821 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
822 ssl->s3->new_session->session_id_length = 0;
823 }
824 }
825
826 if (ssl->ctx->dos_protection_cb != NULL &&
827 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
828 /* Connection rejected for DOS reasons. */
829 al = SSL_AD_INTERNAL_ERROR;
830 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
831 goto f_err;
832 }
833
David Benjaminf01f42a2016-11-16 19:05:33 +0900834 if (ssl->session == NULL) {
835 ssl->s3->new_session->cipher = ssl->s3->tmp.new_cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -0400836
David Benjamin4eb95cc2016-11-16 17:08:23 +0900837 /* On new sessions, stash the SNI value in the session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900838 if (hs->hostname != NULL) {
839 ssl->s3->new_session->tlsext_hostname = BUF_strdup(hs->hostname);
David Benjamin4eb95cc2016-11-16 17:08:23 +0900840 if (ssl->s3->new_session->tlsext_hostname == NULL) {
841 al = SSL_AD_INTERNAL_ERROR;
842 goto f_err;
843 }
844 }
845
David Benjamin5c1ce292015-05-26 16:59:43 -0400846 /* Determine whether to request a client certificate. */
David Benjaminc3c88822016-11-14 10:32:04 +0900847 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjamin5c1ce292015-05-26 16:59:43 -0400848 /* Only request a certificate if Channel ID isn't negotiated. */
David Benjamin0d56f882015-12-19 17:05:56 -0500849 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
850 ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900851 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400852 }
David Benjaminc032dfa2016-05-12 14:54:57 -0400853 /* CertificateRequest may only be sent in certificate-based ciphers. */
David Benjamin3d458dc2016-09-12 22:40:27 +0000854 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900855 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400856 }
Adam Langley37646832016-08-01 16:16:46 -0700857
David Benjaminc3c88822016-11-14 10:32:04 +0900858 if (!hs->cert_request) {
Adam Langley37646832016-08-01 16:16:46 -0700859 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
David Benjamindd634eb2016-08-18 16:40:28 -0400860 * classed by them as a bug, but it's assumed by at least NGINX. */
Adam Langley37646832016-08-01 16:16:46 -0700861 ssl->s3->new_session->verify_result = X509_V_OK;
862 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800863 }
864
David Benjaminf01f42a2016-11-16 19:05:33 +0900865 /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
866 * deferred. Complete it now. */
David Benjamin9ef31f02016-10-31 18:01:13 -0400867 if (!ssl_negotiate_alpn(ssl, &al, &client_hello)) {
868 goto f_err;
869 }
870
David Benjaminf01f42a2016-11-16 19:05:33 +0900871 /* Now that all parameters are known, initialize the handshake hash. */
David Benjamin0d56f882015-12-19 17:05:56 -0500872 if (!ssl3_init_handshake_hash(ssl)) {
David Benjamin9550c3a2015-08-05 08:50:34 -0400873 goto f_err;
874 }
875
Steven Valdez2b8415e2016-06-30 13:27:23 -0400876 /* Release the handshake buffer if client authentication isn't required. */
David Benjaminc3c88822016-11-14 10:32:04 +0900877 if (!hs->cert_request) {
David Benjamin0d56f882015-12-19 17:05:56 -0500878 ssl3_free_handshake_buffer(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800879 }
880
David Benjamin0bd71eb2015-12-01 14:14:40 -0500881 ret = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800882
883 if (0) {
884 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -0500885 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -0800886 }
887
Adam Langley95c29f32014-06-20 12:00:00 -0700888err:
David Benjamine3aa1d92015-06-16 15:34:50 -0400889 SSL_SESSION_free(session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800890 return ret;
891}
Adam Langley95c29f32014-06-20 12:00:00 -0700892
David Benjaminc3c88822016-11-14 10:32:04 +0900893static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) {
894 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -0400895 if (ssl->state == SSL3_ST_SW_SRVR_HELLO_B) {
David Benjamin75836432016-06-17 18:48:29 -0400896 return ssl->method->write_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 }
Adam Langley95c29f32014-06-20 12:00:00 -0700898
David Benjamin56380462015-10-10 14:59:09 -0400899 assert(ssl->state == SSL3_ST_SW_SRVR_HELLO_A);
900
901 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
902 * known attack while we fix ChannelID itself. */
903 if (ssl->s3->tlsext_channel_id_valid &&
904 (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
905 ssl->s3->tlsext_channel_id_valid = 0;
906 }
907
908 /* If this is a resumption and the original handshake didn't support
909 * ChannelID then we didn't record the original handshake hashes in the
910 * session and so cannot resume with ChannelIDs. */
Steven Valdez87eab492016-06-27 16:34:59 -0400911 if (ssl->session != NULL &&
912 ssl->session->original_handshake_hash_len == 0) {
David Benjamin56380462015-10-10 14:59:09 -0400913 ssl->s3->tlsext_channel_id_valid = 0;
914 }
915
David Benjamin721e8b72016-08-03 13:13:17 -0400916 struct timeval now;
917 ssl_get_current_time(ssl, &now);
918 ssl->s3->server_random[0] = now.tv_sec >> 24;
919 ssl->s3->server_random[1] = now.tv_sec >> 16;
920 ssl->s3->server_random[2] = now.tv_sec >> 8;
921 ssl->s3->server_random[3] = now.tv_sec;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400922 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
David Benjamin56380462015-10-10 14:59:09 -0400923 return -1;
924 }
925
David Benjamin55108632016-08-11 22:01:18 -0400926 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
927 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -0400928
Steven Valdez87eab492016-06-27 16:34:59 -0400929 const SSL_SESSION *session = ssl->s3->new_session;
930 if (ssl->session != NULL) {
931 session = ssl->session;
932 }
933
David Benjamin75836432016-06-17 18:48:29 -0400934 CBB cbb, body, session_id;
935 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
936 !CBB_add_u16(&body, ssl->version) ||
937 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
938 !CBB_add_u8_length_prefixed(&body, &session_id) ||
Steven Valdez87eab492016-06-27 16:34:59 -0400939 !CBB_add_bytes(&session_id, session->session_id,
940 session->session_id_length) ||
David Benjamin75836432016-06-17 18:48:29 -0400941 !CBB_add_u16(&body, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
942 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500943 !ssl_add_serverhello_tlsext(hs, &body) ||
Steven Valdez5eead162016-11-11 22:23:25 -0500944 !ssl_complete_message(ssl, &cbb)) {
David Benjamin56380462015-10-10 14:59:09 -0400945 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
946 CBB_cleanup(&cbb);
947 return -1;
948 }
949
950 ssl->state = SSL3_ST_SW_SRVR_HELLO_B;
David Benjamin75836432016-06-17 18:48:29 -0400951 return ssl->method->write_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800952}
Adam Langley95c29f32014-06-20 12:00:00 -0700953
David Benjaminc3c88822016-11-14 10:32:04 +0900954static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) {
955 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -0400956 if (ssl->state == SSL3_ST_SW_CERT_B) {
957 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -0400958 }
959
David Benjamin32a66d52016-07-13 22:03:11 -0400960 if (!ssl_has_certificate(ssl)) {
961 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
962 return 0;
963 }
964
David Benjamin75836432016-06-17 18:48:29 -0400965 if (!ssl3_output_cert_chain(ssl)) {
966 return 0;
967 }
968 ssl->state = SSL3_ST_SW_CERT_B;
969 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -0400970}
971
David Benjaminc3c88822016-11-14 10:32:04 +0900972static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs) {
973 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -0400974 if (ssl->state == SSL3_ST_SW_CERT_STATUS_B) {
975 return ssl->method->write_message(ssl);
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100976 }
977
David Benjamin75836432016-06-17 18:48:29 -0400978 CBB cbb, body, ocsp_response;
979 if (!ssl->method->init_message(ssl, &cbb, &body,
980 SSL3_MT_CERTIFICATE_STATUS) ||
981 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
982 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
983 !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
984 ssl->ctx->ocsp_response_length) ||
Steven Valdez5eead162016-11-11 22:23:25 -0500985 !ssl_complete_message(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -0400986 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
987 CBB_cleanup(&cbb);
988 return -1;
989 }
990
991 ssl->state = SSL3_ST_SW_CERT_STATUS_B;
992 return ssl->method->write_message(ssl);
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100993}
994
David Benjaminc3c88822016-11-14 10:32:04 +0900995static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) {
996 SSL *const ssl = hs->ssl;
David Benjamin2a0b3912015-12-18 01:01:21 -0500997 if (ssl->state == SSL3_ST_SW_KEY_EXCH_C) {
David Benjamin75836432016-06-17 18:48:29 -0400998 return ssl->method->write_message(ssl);
David Benjamin1f9f9c42015-08-28 16:17:59 -0400999 }
1000
David Benjamin2a0b3912015-12-18 01:01:21 -05001001 CBB cbb, child;
David Benjaminc42acee2016-06-17 17:47:58 -04001002 CBB_zero(&cbb);
nagendra modadugu601448a2015-07-24 09:31:31 -07001003
David Benjaminc42acee2016-06-17 17:47:58 -04001004 /* Put together the parameters. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001005 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001006 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1007 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001008
David Benjaminc42acee2016-06-17 17:47:58 -04001009 /* Pre-allocate enough room to comfortably fit an ECDHE public key. */
1010 if (!CBB_init(&cbb, 128)) {
1011 goto err;
1012 }
1013
David Benjamin2a0b3912015-12-18 01:01:21 -05001014 /* PSK ciphers begin with an identity hint. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001015 if (alg_a & SSL_aPSK) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001016 size_t len =
1017 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
1018 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1019 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1020 len)) {
1021 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001022 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001023 }
Adam Langley95c29f32014-06-20 12:00:00 -07001024
David Benjamin7061e282015-03-19 11:10:48 -04001025 if (alg_k & SSL_kDHE) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001026 /* Determine the group to use. */
1027 DH *params = ssl->cert->dh_tmp;
1028 if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
1029 params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
Adam Langleyfcf25832014-12-18 17:42:32 -08001030 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001031 if (params == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001032 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001033 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001034 goto err;
1035 }
Steven Valdez87eab492016-06-27 16:34:59 -04001036 ssl->s3->new_session->key_exchange_info = DH_num_bits(params);
David Benjamin2a0b3912015-12-18 01:01:21 -05001037
David Benjamin974c7ba2015-12-19 16:58:04 -05001038 /* Set up DH, generate a key, and emit the public half. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001039 DH *dh = DHparams_dup(params);
David Benjamin974c7ba2015-12-19 16:58:04 -05001040 if (dh == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 goto err;
1042 }
David Benjamin9f226a52015-04-26 22:12:32 -04001043
David Benjaminc3c88822016-11-14 10:32:04 +09001044 SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
David Benjamin2a0b3912015-12-18 01:01:21 -05001045 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001046 !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001047 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001048 !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001049 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001050 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
David Benjamin9f226a52015-04-26 22:12:32 -04001051 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001052 }
David Benjamin7061e282015-03-19 11:10:48 -04001053 } else if (alg_k & SSL_kECDHE) {
Steven Valdezce902a92016-05-17 11:47:53 -04001054 /* Determine the group to use. */
1055 uint16_t group_id;
1056 if (!tls1_get_shared_group(ssl, &group_id)) {
David Benjamin3570d732015-06-29 00:28:17 -04001057 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001058 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001059 goto err;
1060 }
Steven Valdez87eab492016-06-27 16:34:59 -04001061 ssl->s3->new_session->key_exchange_info = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -07001062
David Benjamin4298d772015-12-19 00:18:25 -05001063 /* Set up ECDH, generate a key, and emit the public half. */
David Benjaminc3c88822016-11-14 10:32:04 +09001064 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001065 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
Steven Valdezce902a92016-05-17 11:47:53 -04001066 !CBB_add_u16(&cbb, group_id) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001067 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001068 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
Matt Braithwaite053931e2016-05-25 12:06:05 -07001069 goto err;
1070 }
1071 } else if (alg_k & SSL_kCECPQ1) {
David Benjaminc3c88822016-11-14 10:32:04 +09001072 SSL_ECDH_CTX_init_for_cecpq1(&hs->ecdh_ctx);
David Benjamin18a35182016-06-30 13:46:24 -04001073 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001074 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001075 goto err;
1076 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001077 } else {
1078 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001079 }
Adam Langley95c29f32014-06-20 12:00:00 -07001080
David Benjaminc3c88822016-11-14 10:32:04 +09001081 if (!CBB_finish(&cbb, &hs->server_params, &hs->server_params_len)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001082 goto err;
1083 }
David Benjaminc42acee2016-06-17 17:47:58 -04001084 }
1085
1086 /* Assemble the message. */
David Benjamin75836432016-06-17 18:48:29 -04001087 CBB body;
1088 if (!ssl->method->init_message(ssl, &cbb, &body,
1089 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001090 !CBB_add_bytes(&body, hs->server_params, hs->server_params_len)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001091 goto err;
1092 }
Adam Langley95c29f32014-06-20 12:00:00 -07001093
David Benjamin2a0b3912015-12-18 01:01:21 -05001094 /* Add a signature. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001095 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001096 if (!ssl_has_private_key(ssl)) {
1097 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001098 goto err;
1099 }
Adam Langley95c29f32014-06-20 12:00:00 -07001100
David Benjaminc42acee2016-06-17 17:47:58 -04001101 /* Determine the signature algorithm. */
David Benjaminea9a0d52016-07-08 15:52:59 -07001102 uint16_t signature_algorithm;
1103 if (!tls1_choose_signature_algorithm(ssl, &signature_algorithm)) {
1104 goto err;
1105 }
David Benjaminc42acee2016-06-17 17:47:58 -04001106 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001107 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001108 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1109 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1110 goto err;
1111 }
David Benjaminc42acee2016-06-17 17:47:58 -04001112 }
1113
1114 /* Add space for the signature. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001115 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjaminc42acee2016-06-17 17:47:58 -04001116 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001117 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -04001118 !CBB_reserve(&child, &ptr, max_sig_len)) {
1119 goto err;
1120 }
1121
David Benjamin2a0b3912015-12-18 01:01:21 -05001122 size_t sig_len;
1123 enum ssl_private_key_result_t sign_result;
1124 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001125 CBB transcript;
1126 uint8_t *transcript_data;
1127 size_t transcript_len;
1128 if (!CBB_init(&transcript,
David Benjaminc3c88822016-11-14 10:32:04 +09001129 2 * SSL3_RANDOM_SIZE + hs->server_params_len) ||
1130 !CBB_add_bytes(&transcript, ssl->s3->client_random,
1131 SSL3_RANDOM_SIZE) ||
1132 !CBB_add_bytes(&transcript, ssl->s3->server_random,
1133 SSL3_RANDOM_SIZE) ||
1134 !CBB_add_bytes(&transcript, hs->server_params,
1135 hs->server_params_len) ||
Steven Valdez2b8415e2016-06-30 13:27:23 -04001136 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1137 CBB_cleanup(&transcript);
1138 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1139 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdezf0451ca2016-06-29 13:16:27 -04001140 goto err;
1141 }
1142
Steven Valdezf0451ca2016-06-29 13:16:27 -04001143 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001144 signature_algorithm, transcript_data,
1145 transcript_len);
1146 OPENSSL_free(transcript_data);
David Benjamin1f9f9c42015-08-28 16:17:59 -04001147 } else {
David Benjamin2a0b3912015-12-18 01:01:21 -05001148 assert(ssl->state == SSL3_ST_SW_KEY_EXCH_B);
David Benjamind3440b42016-07-14 14:52:41 -04001149 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001150 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001151
1152 switch (sign_result) {
1153 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -05001154 if (!CBB_did_write(&child, sig_len)) {
1155 goto err;
1156 }
1157 break;
1158 case ssl_private_key_failure:
David Benjamin2a0b3912015-12-18 01:01:21 -05001159 goto err;
1160 case ssl_private_key_retry:
David Benjamin2a0b3912015-12-18 01:01:21 -05001161 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
1162 ssl->state = SSL3_ST_SW_KEY_EXCH_B;
1163 goto err;
1164 }
David Benjamin1f9f9c42015-08-28 16:17:59 -04001165 }
1166
Steven Valdez5eead162016-11-11 22:23:25 -05001167 if (!ssl_complete_message(ssl, &cbb)) {
David Benjamin1f9f9c42015-08-28 16:17:59 -04001168 goto err;
1169 }
David Benjaminc42acee2016-06-17 17:47:58 -04001170
David Benjaminc3c88822016-11-14 10:32:04 +09001171 OPENSSL_free(hs->server_params);
1172 hs->server_params = NULL;
1173 hs->server_params_len = 0;
David Benjaminc42acee2016-06-17 17:47:58 -04001174
David Benjamin2a0b3912015-12-18 01:01:21 -05001175 ssl->state = SSL3_ST_SW_KEY_EXCH_C;
David Benjamin75836432016-06-17 18:48:29 -04001176 return ssl->method->write_message(ssl);
nagendra modadugu601448a2015-07-24 09:31:31 -07001177
Adam Langley95c29f32014-06-20 12:00:00 -07001178err:
David Benjamin2a0b3912015-12-18 01:01:21 -05001179 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001180 return -1;
1181}
Adam Langley95c29f32014-06-20 12:00:00 -07001182
David Benjamin75836432016-06-17 18:48:29 -04001183static int add_cert_types(SSL *ssl, CBB *cbb) {
1184 /* Get configured signature algorithms. */
1185 int have_rsa_sign = 0;
1186 int have_ecdsa_sign = 0;
Steven Valdez02563852016-06-23 13:33:05 -04001187 const uint16_t *sig_algs;
David Benjamin3ef76972016-10-17 17:59:54 -04001188 size_t num_sig_algs = tls12_get_verify_sigalgs(ssl, &sig_algs);
1189 for (size_t i = 0; i < num_sig_algs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04001190 switch (sig_algs[i]) {
1191 case SSL_SIGN_RSA_PKCS1_SHA512:
1192 case SSL_SIGN_RSA_PKCS1_SHA384:
1193 case SSL_SIGN_RSA_PKCS1_SHA256:
1194 case SSL_SIGN_RSA_PKCS1_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001195 have_rsa_sign = 1;
1196 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001197
Steven Valdez02563852016-06-23 13:33:05 -04001198 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
1199 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
1200 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
1201 case SSL_SIGN_ECDSA_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001202 have_ecdsa_sign = 1;
1203 break;
Adam Langleyfcf25832014-12-18 17:42:32 -08001204 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001205 }
Adam Langley95c29f32014-06-20 12:00:00 -07001206
David Benjamin75836432016-06-17 18:48:29 -04001207 if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) {
1208 return 0;
1209 }
1210
1211 /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
1212 * check for SSL_kECDH or SSL_kECDHE. */
1213 if (ssl->version >= TLS1_VERSION && have_ecdsa_sign &&
1214 !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) {
1215 return 0;
1216 }
1217
1218 return 1;
1219}
1220
David Benjaminc3c88822016-11-14 10:32:04 +09001221static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs) {
1222 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001223 if (ssl->state == SSL3_ST_SW_CERT_REQ_B) {
1224 return ssl->method->write_message(ssl);
1225 }
1226
David Benjamin32a66d52016-07-13 22:03:11 -04001227 CBB cbb, body, cert_types, sigalgs_cbb;
David Benjamin75836432016-06-17 18:48:29 -04001228 if (!ssl->method->init_message(ssl, &cbb, &body,
1229 SSL3_MT_CERTIFICATE_REQUEST) ||
1230 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1231 !add_cert_types(ssl, &cert_types)) {
1232 goto err;
1233 }
1234
1235 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001236 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001237 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -04001238 if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001239 goto err;
1240 }
Steven Valdez02563852016-06-23 13:33:05 -04001241
David Benjamin0fc37ef2016-08-17 15:29:46 -04001242 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04001243 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
1244 goto err;
1245 }
1246 }
David Benjamin75836432016-06-17 18:48:29 -04001247 }
1248
David Benjamin32a66d52016-07-13 22:03:11 -04001249 if (!ssl_add_client_CA_list(ssl, &body) ||
Steven Valdez5eead162016-11-11 22:23:25 -05001250 !ssl_complete_message(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001251 goto err;
1252 }
1253
1254 ssl->state = SSL3_ST_SW_CERT_REQ_B;
1255 return ssl->method->write_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001256
Adam Langley95c29f32014-06-20 12:00:00 -07001257err:
David Benjamin75836432016-06-17 18:48:29 -04001258 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1259 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001260 return -1;
1261}
Adam Langley95c29f32014-06-20 12:00:00 -07001262
David Benjaminc3c88822016-11-14 10:32:04 +09001263static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) {
1264 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001265 if (ssl->state == SSL3_ST_SW_SRVR_DONE_B) {
1266 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001267 }
1268
David Benjamin75836432016-06-17 18:48:29 -04001269 CBB cbb, body;
1270 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
Steven Valdez5eead162016-11-11 22:23:25 -05001271 !ssl_complete_message(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001272 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1273 CBB_cleanup(&cbb);
1274 return -1;
1275 }
1276
1277 ssl->state = SSL3_ST_SW_SRVR_DONE_B;
1278 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001279}
1280
David Benjaminc3c88822016-11-14 10:32:04 +09001281static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) {
1282 SSL *const ssl = hs->ssl;
1283 assert(hs->cert_request);
David Benjamin9f1dc822016-06-07 17:03:46 -04001284
David Benjamin09eb6552016-07-08 14:32:11 -07001285 int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1286 if (msg_ret <= 0) {
1287 return msg_ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001288 }
1289
1290 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1291 if (ssl->version == SSL3_VERSION &&
1292 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Adam Langley37646832016-08-01 16:16:46 -07001293 /* In SSL 3.0, the Certificate message is omitted to signal no
1294 * certificate. */
David Benjaminda2630c2016-08-01 19:57:43 -04001295 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001296 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001297 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1298 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001299 }
1300
Adam Langley37646832016-08-01 16:16:46 -07001301 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001302 * classed by them as a bug, but it's assumed by at least NGINX. */
Adam Langley37646832016-08-01 16:16:46 -07001303 ssl->s3->new_session->verify_result = X509_V_OK;
David Benjamin9f1dc822016-06-07 17:03:46 -04001304 ssl->s3->tmp.reuse_message = 1;
1305 return 1;
1306 }
1307
David Benjamin9f1dc822016-06-07 17:03:46 -04001308 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin5c900c82016-07-13 23:03:26 -04001309 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1310 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001311 }
1312
David Benjamin5c900c82016-07-13 23:03:26 -04001313 CBS certificate_msg;
David Benjamin09eb6552016-07-08 14:32:11 -07001314 CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
David Benjamin5c900c82016-07-13 23:03:26 -04001315 uint8_t alert;
1316 STACK_OF(X509) *chain = ssl_parse_cert_chain(
David Benjaminbbaf3672016-11-17 10:53:09 +09001317 ssl, &alert, ssl->retain_only_sha256_of_client_certs
Steven Valdez87eab492016-06-27 16:34:59 -04001318 ? ssl->s3->new_session->peer_sha256
1319 : NULL,
David Benjamin5c900c82016-07-13 23:03:26 -04001320 &certificate_msg);
1321 if (chain == NULL) {
1322 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin9f1dc822016-06-07 17:03:46 -04001323 goto err;
1324 }
1325
David Benjamin5c900c82016-07-13 23:03:26 -04001326 if (CBS_len(&certificate_msg) != 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001327 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001328 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1329 goto err;
David Benjamin9f1dc822016-06-07 17:03:46 -04001330 }
1331
David Benjamin5c900c82016-07-13 23:03:26 -04001332 if (sk_X509_num(chain) == 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001333 /* No client certificate so the handshake buffer may be discarded. */
1334 ssl3_free_handshake_buffer(ssl);
1335
David Benjamine455e512016-08-01 20:11:13 -04001336 /* In SSL 3.0, sending no certificate is signaled by omitting the
1337 * Certificate message. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001338 if (ssl->version == SSL3_VERSION) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001339 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjamin5c900c82016-07-13 23:03:26 -04001340 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1341 goto err;
David Benjamine455e512016-08-01 20:11:13 -04001342 }
1343
1344 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001345 /* Fail for TLS only if we required a certificate */
1346 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001347 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1348 goto err;
David Benjamin9f1dc822016-06-07 17:03:46 -04001349 }
Adam Langley37646832016-08-01 16:16:46 -07001350
1351 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001352 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin7aa31d62016-08-08 21:38:32 -04001353 ssl->s3->new_session->verify_result = X509_V_OK;
David Benjamin9f1dc822016-06-07 17:03:46 -04001354 } else {
David Benjamin5c900c82016-07-13 23:03:26 -04001355 /* The hash would have been filled in. */
David Benjaminbbaf3672016-11-17 10:53:09 +09001356 if (ssl->retain_only_sha256_of_client_certs) {
Steven Valdez87eab492016-06-27 16:34:59 -04001357 ssl->s3->new_session->peer_sha256_valid = 1;
David Benjamin5c900c82016-07-13 23:03:26 -04001358 }
1359
David Benjamin7aa31d62016-08-08 21:38:32 -04001360 if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
1361 chain)) {
David Benjamin5c900c82016-07-13 23:03:26 -04001362 goto err;
David Benjamin9f1dc822016-06-07 17:03:46 -04001363 }
1364 }
1365
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001366 X509_free(ssl->s3->new_session->x509_peer);
1367 ssl->s3->new_session->x509_peer = sk_X509_shift(chain);
David Benjamin9f1dc822016-06-07 17:03:46 -04001368
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001369 sk_X509_pop_free(ssl->s3->new_session->x509_chain, X509_free);
1370 ssl->s3->new_session->x509_chain = chain;
1371 /* Inconsistency alert: x509_chain does *not* include the peer's own
David Benjamin9f1dc822016-06-07 17:03:46 -04001372 * certificate, while we do include it in s3_clnt.c */
1373
David Benjamin5c900c82016-07-13 23:03:26 -04001374 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001375
1376err:
David Benjamin5c900c82016-07-13 23:03:26 -04001377 sk_X509_pop_free(chain, X509_free);
1378 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001379}
1380
David Benjaminc3c88822016-11-14 10:32:04 +09001381static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) {
1382 SSL *const ssl = hs->ssl;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001383 int al;
Adam Langleyfcf25832014-12-18 17:42:32 -08001384 CBS client_key_exchange;
David Benjamin3fa27772015-04-17 22:32:19 -04001385 uint32_t alg_k;
1386 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001387 uint8_t *premaster_secret = NULL;
1388 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001389 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001390
David Benjamin4298d772015-12-19 00:18:25 -05001391 unsigned psk_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001392 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001393
David Benjamin1e6d6df2016-05-13 18:28:17 -04001394 if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin09eb6552016-07-08 14:32:11 -07001395 int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE,
1396 ssl_hash_message);
1397 if (ret <= 0) {
1398 return ret;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001399 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001400 }
Adam Langley95c29f32014-06-20 12:00:00 -07001401
David Benjamin0d56f882015-12-19 17:05:56 -05001402 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
1403 alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1404 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001405
Adam Langleyfcf25832014-12-18 17:42:32 -08001406 /* If using a PSK key exchange, prepare the pre-shared key. */
1407 if (alg_a & SSL_aPSK) {
1408 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001409
Adam Langleyfcf25832014-12-18 17:42:32 -08001410 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1411 * then this is the only field in the message. */
1412 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1413 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001414 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001415 al = SSL_AD_DECODE_ERROR;
1416 goto f_err;
1417 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001418
David Benjamin0d56f882015-12-19 17:05:56 -05001419 if (ssl->psk_server_callback == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001420 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001421 al = SSL_AD_INTERNAL_ERROR;
1422 goto f_err;
1423 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001424
Adam Langleyfcf25832014-12-18 17:42:32 -08001425 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1426 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001427 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001428 al = SSL_AD_ILLEGAL_PARAMETER;
1429 goto f_err;
1430 }
David Benjamin35c02302014-07-13 04:14:59 -04001431
Steven Valdez87eab492016-06-27 16:34:59 -04001432 if (!CBS_strdup(&psk_identity, &ssl->s3->new_session->psk_identity)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001433 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001434 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001435 goto f_err;
1436 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001437
Adam Langleyfcf25832014-12-18 17:42:32 -08001438 /* Look up the key for the identity. */
Steven Valdez87eab492016-06-27 16:34:59 -04001439 psk_len = ssl->psk_server_callback(ssl, ssl->s3->new_session->psk_identity,
1440 psk, sizeof(psk));
Adam Langleyfcf25832014-12-18 17:42:32 -08001441 if (psk_len > PSK_MAX_PSK_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04001442 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001443 al = SSL_AD_INTERNAL_ERROR;
1444 goto f_err;
1445 } else if (psk_len == 0) {
1446 /* PSK related to the given identity not found */
David Benjamin3570d732015-06-29 00:28:17 -04001447 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001448 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1449 goto f_err;
1450 }
1451 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001452
Adam Langleyfcf25832014-12-18 17:42:32 -08001453 /* Depending on the key exchange method, compute |premaster_secret| and
1454 * |premaster_secret_len|. */
1455 if (alg_k & SSL_kRSA) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001456 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamin0d56f882015-12-19 17:05:56 -05001457 const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001458 decrypt_buf = OPENSSL_malloc(rsa_size);
1459 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001460 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001461 goto err;
1462 }
David Benjamin35c02302014-07-13 04:14:59 -04001463
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001464 enum ssl_private_key_result_t decrypt_result;
David Benjamin3f5b43d2015-12-01 19:31:24 -05001465 size_t decrypt_len;
David Benjamin1e6d6df2016-05-13 18:28:17 -04001466 if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin0d56f882015-12-19 17:05:56 -05001467 if (!ssl_has_private_key(ssl) ||
David Benjamin0c0b7e12016-07-14 13:47:55 -04001468 ssl_private_key_type(ssl) != NID_rsaEncryption) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001469 al = SSL_AD_HANDSHAKE_FAILURE;
1470 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1471 goto f_err;
1472 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001473 CBS encrypted_premaster_secret;
David Benjamin0d56f882015-12-19 17:05:56 -05001474 if (ssl->version > SSL3_VERSION) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001475 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1476 &encrypted_premaster_secret) ||
1477 CBS_len(&client_key_exchange) != 0) {
David Benjaminef5e5152015-11-18 20:35:31 -05001478 al = SSL_AD_DECODE_ERROR;
1479 OPENSSL_PUT_ERROR(SSL,
1480 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1481 goto f_err;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001482 }
1483 } else {
1484 encrypted_premaster_secret = client_key_exchange;
1485 }
1486
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001487 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1488 * timing-sensitive code below. */
1489 decrypt_result = ssl_private_key_decrypt(
David Benjamin0d56f882015-12-19 17:05:56 -05001490 ssl, decrypt_buf, &decrypt_len, rsa_size,
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001491 CBS_data(&encrypted_premaster_secret),
1492 CBS_len(&encrypted_premaster_secret));
1493 } else {
David Benjamin1e6d6df2016-05-13 18:28:17 -04001494 assert(ssl->state == SSL3_ST_SR_KEY_EXCH_B);
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001495 /* Complete async decrypt. */
David Benjamind3440b42016-07-14 14:52:41 -04001496 decrypt_result =
1497 ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001498 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001499
1500 switch (decrypt_result) {
1501 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001502 break;
1503 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001504 goto err;
1505 case ssl_private_key_retry:
David Benjamin0d56f882015-12-19 17:05:56 -05001506 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamin1e6d6df2016-05-13 18:28:17 -04001507 ssl->state = SSL3_ST_SR_KEY_EXCH_B;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001508 goto err;
1509 }
1510
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001511 if (decrypt_len != rsa_size) {
1512 al = SSL_AD_DECRYPT_ERROR;
1513 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1514 goto f_err;
1515 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001516
David Benjamin3f5b43d2015-12-01 19:31:24 -05001517 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1518 * section 7.4.7.1. */
1519 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1520 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001521 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001522 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001523 goto err;
1524 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001525 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001526 goto err;
1527 }
1528
David Benjamin3f5b43d2015-12-01 19:31:24 -05001529 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1530 * publicly invalid. */
1531 if (decrypt_len < 11 + premaster_secret_len) {
1532 al = SSL_AD_DECRYPT_ERROR;
1533 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1534 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001535 }
David Benjamin35c02302014-07-13 04:14:59 -04001536
David Benjamin3f5b43d2015-12-01 19:31:24 -05001537 /* Check the padding. See RFC 3447, section 7.2.2. */
1538 size_t padding_len = decrypt_len - premaster_secret_len;
1539 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1540 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin54091232016-09-05 12:47:25 -04001541 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001542 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1543 }
1544 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1545
1546 /* The premaster secret must begin with |client_version|. This too must be
1547 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1548 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjamin0d56f882015-12-19 17:05:56 -05001549 (unsigned)(ssl->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001550 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjamin0d56f882015-12-19 17:05:56 -05001551 (unsigned)(ssl->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001552
1553 /* Select, in constant time, either the decrypted premaster or the random
1554 * premaster based on |good|. */
David Benjamin54091232016-09-05 12:47:25 -04001555 for (size_t i = 0; i < premaster_secret_len; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001556 premaster_secret[i] = constant_time_select_8(
1557 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1558 }
1559
1560 OPENSSL_free(decrypt_buf);
1561 decrypt_buf = NULL;
Matt Braithwaite053931e2016-05-25 12:06:05 -07001562 } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001563 /* Parse the ClientKeyExchange. */
David Benjamin974c7ba2015-12-19 16:58:04 -05001564 CBS peer_key;
David Benjaminc3c88822016-11-14 10:32:04 +09001565 if (!SSL_ECDH_CTX_get_key(&hs->ecdh_ctx, &client_key_exchange, &peer_key) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001566 CBS_len(&client_key_exchange) != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001567 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001568 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001569 goto f_err;
1570 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001571
David Benjamin4298d772015-12-19 00:18:25 -05001572 /* Compute the premaster. */
1573 uint8_t alert;
David Benjaminc3c88822016-11-14 10:32:04 +09001574 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, &premaster_secret,
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001575 &premaster_secret_len, &alert, CBS_data(&peer_key),
1576 CBS_len(&peer_key))) {
David Benjamin4298d772015-12-19 00:18:25 -05001577 al = alert;
1578 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001579 }
Adam Langley95c29f32014-06-20 12:00:00 -07001580
David Benjamin4298d772015-12-19 00:18:25 -05001581 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001582 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001583 } else if (alg_k & SSL_kPSK) {
1584 /* For plain PSK, other_secret is a block of 0s with the same length as the
1585 * pre-shared key. */
1586 premaster_secret_len = psk_len;
1587 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1588 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001589 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001590 goto err;
1591 }
1592 memset(premaster_secret, 0, premaster_secret_len);
1593 } else {
1594 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001595 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001596 goto f_err;
1597 }
Adam Langley95c29f32014-06-20 12:00:00 -07001598
Adam Langleyfcf25832014-12-18 17:42:32 -08001599 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1600 * pre-shared key. */
1601 if (alg_a & SSL_aPSK) {
1602 CBB new_premaster, child;
1603 uint8_t *new_data;
1604 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04001605
David Benjamina8653202015-06-28 01:26:10 -04001606 CBB_zero(&new_premaster);
1607 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1608 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001609 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1610 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1611 !CBB_add_bytes(&child, psk, psk_len) ||
1612 !CBB_finish(&new_premaster, &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001613 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001614 CBB_cleanup(&new_premaster);
1615 goto err;
1616 }
David Benjamin14c83e72014-07-13 04:54:57 -04001617
Adam Langleyfcf25832014-12-18 17:42:32 -08001618 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1619 OPENSSL_free(premaster_secret);
1620 premaster_secret = new_data;
1621 premaster_secret_len = new_len;
1622 }
David Benjamin14c83e72014-07-13 04:54:57 -04001623
Adam Langleyfcf25832014-12-18 17:42:32 -08001624 /* Compute the master secret */
Steven Valdez87eab492016-06-27 16:34:59 -04001625 ssl->s3->new_session->master_key_length = tls1_generate_master_secret(
1626 ssl, ssl->s3->new_session->master_key, premaster_secret,
1627 premaster_secret_len);
1628 if (ssl->s3->new_session->master_key_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001629 goto err;
1630 }
Steven Valdez87eab492016-06-27 16:34:59 -04001631 ssl->s3->new_session->extended_master_secret =
1632 ssl->s3->tmp.extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001633
Adam Langleyfcf25832014-12-18 17:42:32 -08001634 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1635 OPENSSL_free(premaster_secret);
1636 return 1;
1637
Adam Langley95c29f32014-06-20 12:00:00 -07001638f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001639 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001640err:
David Benjamin4298d772015-12-19 00:18:25 -05001641 if (premaster_secret != NULL) {
1642 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001643 OPENSSL_free(premaster_secret);
1644 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001645 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001646
Adam Langleyfcf25832014-12-18 17:42:32 -08001647 return -1;
1648}
Adam Langley95c29f32014-06-20 12:00:00 -07001649
David Benjaminc3c88822016-11-14 10:32:04 +09001650static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) {
1651 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001652 int al, ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001653 CBS certificate_verify, signature;
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001654 X509 *peer = ssl->s3->new_session->x509_peer;
Adam Langleyfcf25832014-12-18 17:42:32 -08001655 EVP_PKEY *pkey = NULL;
David Benjamin6553b372014-07-22 14:11:30 -04001656
Adam Langleyfcf25832014-12-18 17:42:32 -08001657 /* Only RSA and ECDSA client certificates are supported, so a
1658 * CertificateVerify is required if and only if there's a client certificate.
1659 * */
1660 if (peer == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05001661 ssl3_free_handshake_buffer(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001662 return 1;
1663 }
Adam Langley95c29f32014-06-20 12:00:00 -07001664
David Benjamin09eb6552016-07-08 14:32:11 -07001665 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE_VERIFY,
1666 ssl_dont_hash_message);
1667 if (msg_ret <= 0) {
1668 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001669 }
David Benjaminef865502014-08-24 02:48:34 -04001670
Adam Langleyfcf25832014-12-18 17:42:32 -08001671 /* Filter out unsupported certificate types. */
1672 pkey = X509_get_pubkey(peer);
David Benjamin9d0847a2015-02-16 03:57:55 -05001673 if (pkey == NULL) {
1674 goto err;
1675 }
David Benjamin6897dbe2014-07-12 20:18:28 -04001676
David Benjamin09eb6552016-07-08 14:32:11 -07001677 CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
David Benjamin6897dbe2014-07-12 20:18:28 -04001678
Adam Langleyfcf25832014-12-18 17:42:32 -08001679 /* Determine the digest type if needbe. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04001680 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001681 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001682 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001683 al = SSL_AD_DECODE_ERROR;
1684 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1685 goto f_err;
1686 }
David Benjamin887c3002016-07-08 16:15:32 -07001687 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001688 goto f_err;
1689 }
Steven Valdez02563852016-06-23 13:33:05 -04001690 ssl->s3->tmp.peer_signature_algorithm = signature_algorithm;
Steven Valdezf0451ca2016-06-29 13:16:27 -04001691 } else if (pkey->type == EVP_PKEY_RSA) {
1692 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
1693 } else if (pkey->type == EVP_PKEY_EC) {
1694 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
David Benjamin49ec9bb2016-07-14 00:11:26 -04001695 } else {
1696 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1697 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1698 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001699 }
David Benjamin854dd652014-08-26 00:32:30 -04001700
Adam Langleyfcf25832014-12-18 17:42:32 -08001701 /* Parse and verify the signature. */
1702 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1703 CBS_len(&certificate_verify) != 0) {
1704 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001705 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001706 goto f_err;
1707 }
Adam Langley95c29f32014-06-20 12:00:00 -07001708
Steven Valdez2b8415e2016-06-30 13:27:23 -04001709 int sig_ok;
1710 /* The SSL3 construction for CertificateVerify does not decompose into a
1711 * single final digest and signature, and must be special-cased. */
1712 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
David Benjamin0aa25bd2016-07-08 14:44:56 -07001713 const EVP_MD *md;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001714 uint8_t digest[EVP_MAX_MD_SIZE];
1715 size_t digest_len;
David Benjamin0aa25bd2016-07-08 14:44:56 -07001716 if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001717 signature_algorithm)) {
1718 goto err;
1719 }
1720
Steven Valdez2b8415e2016-06-30 13:27:23 -04001721 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
1722 sig_ok = pctx != NULL &&
1723 EVP_PKEY_verify_init(pctx) &&
1724 EVP_PKEY_CTX_set_signature_md(pctx, md) &&
1725 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1726 digest, digest_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001727 EVP_PKEY_CTX_free(pctx);
1728 } else {
1729 sig_ok = ssl_public_key_verify(
1730 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
1731 pkey, (const uint8_t *)ssl->s3->handshake_buffer->data,
1732 ssl->s3->handshake_buffer->length);
Adam Langleyfcf25832014-12-18 17:42:32 -08001733 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04001734
David Benjaminbf82aed2016-03-01 22:57:40 -05001735#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1736 sig_ok = 1;
1737 ERR_clear_error();
1738#endif
1739 if (!sig_ok) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001740 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001741 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001742 goto f_err;
1743 }
1744
Steven Valdez2b8415e2016-06-30 13:27:23 -04001745 /* The handshake buffer is no longer necessary, and we may hash the current
1746 * message.*/
1747 ssl3_free_handshake_buffer(ssl);
David Benjaminced94792016-11-14 17:12:11 +09001748 if (!ssl_hash_current_message(ssl)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001749 goto err;
1750 }
1751
Adam Langleyfcf25832014-12-18 17:42:32 -08001752 ret = 1;
1753
1754 if (0) {
1755 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001756 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08001757 }
1758
David Benjamin854dd652014-08-26 00:32:30 -04001759err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001760 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001761
Adam Langleyfcf25832014-12-18 17:42:32 -08001762 return ret;
1763}
Adam Langley95c29f32014-06-20 12:00:00 -07001764
Adam Langley95c29f32014-06-20 12:00:00 -07001765/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1766 * sets the next_proto member in s if found */
David Benjaminc3c88822016-11-14 10:32:04 +09001767static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) {
1768 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001769 int ret =
1770 ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message);
1771 if (ret <= 0) {
1772 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001773 }
Adam Langley95c29f32014-06-20 12:00:00 -07001774
David Benjamin09eb6552016-07-08 14:32:11 -07001775 CBS next_protocol, selected_protocol, padding;
1776 CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
Adam Langleyfcf25832014-12-18 17:42:32 -08001777 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1778 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001779 CBS_len(&next_protocol) != 0) {
1780 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1781 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1782 return 0;
1783 }
1784
1785 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin79978df2015-12-25 15:56:49 -05001786 &ssl->s3->next_proto_negotiated_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001787 return 0;
1788 }
Adam Langley95c29f32014-06-20 12:00:00 -07001789
Adam Langleyfcf25832014-12-18 17:42:32 -08001790 return 1;
1791}
Adam Langley95c29f32014-06-20 12:00:00 -07001792
Adam Langley1258b6a2014-06-20 12:00:00 -07001793/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
David Benjaminc3c88822016-11-14 10:32:04 +09001794static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) {
1795 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001796 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID,
1797 ssl_dont_hash_message);
1798 if (msg_ret <= 0) {
1799 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001800 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001801
Nick Harper60a85cb2016-09-23 16:25:11 -07001802 if (!tls1_verify_channel_id(ssl) ||
David Benjaminced94792016-11-14 17:12:11 +09001803 !ssl_hash_current_message(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001804 return -1;
1805 }
Nick Harper60a85cb2016-09-23 16:25:11 -07001806 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001807}
David Benjamin9f1dc822016-06-07 17:03:46 -04001808
David Benjaminc3c88822016-11-14 10:32:04 +09001809static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs) {
1810 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001811 if (ssl->state == SSL3_ST_SW_SESSION_TICKET_B) {
1812 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001813 }
1814
David Benjamin123db572016-11-03 16:59:25 -04001815 const SSL_SESSION *session;
1816 SSL_SESSION *session_copy = NULL;
1817 if (ssl->session == NULL) {
1818 /* Fix the timeout to measure from the ticket issuance time. */
1819 ssl_session_refresh_time(ssl, ssl->s3->new_session);
1820 session = ssl->s3->new_session;
1821 } else {
1822 /* We are renewing an existing session. Duplicate the session to adjust the
1823 * timeout. */
1824 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
1825 if (session_copy == NULL) {
1826 return -1;
1827 }
1828
1829 ssl_session_refresh_time(ssl, session_copy);
1830 session = session_copy;
1831 }
1832
David Benjamin75836432016-06-17 18:48:29 -04001833 CBB cbb, body, ticket;
David Benjamin123db572016-11-03 16:59:25 -04001834 int ok =
1835 ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) &&
1836 CBB_add_u32(&body, session->timeout) &&
1837 CBB_add_u16_length_prefixed(&body, &ticket) &&
1838 ssl_encrypt_ticket(ssl, &ticket, session) &&
Steven Valdez5eead162016-11-11 22:23:25 -05001839 ssl_complete_message(ssl, &cbb);
David Benjamin123db572016-11-03 16:59:25 -04001840
1841 SSL_SESSION_free(session_copy);
1842 CBB_cleanup(&cbb);
1843
1844 if (!ok) {
David Benjamine75cc272016-11-03 18:06:27 -04001845 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001846 }
1847
1848 ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001849 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001850}