blob: f5bf0dab09a8c3019c237412516befc87d72e5ee [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 Benjamindf50eec2016-06-07 16:49:42 -0400174static int ssl3_get_client_hello(SSL *ssl);
175static int ssl3_send_server_hello(SSL *ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -0400176static int ssl3_send_server_certificate(SSL *ssl);
David Benjamindf50eec2016-06-07 16:49:42 -0400177static int ssl3_send_certificate_status(SSL *ssl);
David Benjamindf50eec2016-06-07 16:49:42 -0400178static int ssl3_send_server_key_exchange(SSL *ssl);
179static int ssl3_send_certificate_request(SSL *ssl);
David Benjamin3dcec452016-06-07 17:13:28 -0400180static int ssl3_send_server_hello_done(SSL *ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -0400181static int ssl3_get_client_certificate(SSL *ssl);
David Benjamindf50eec2016-06-07 16:49:42 -0400182static int ssl3_get_client_key_exchange(SSL *ssl);
183static int ssl3_get_cert_verify(SSL *ssl);
David Benjamindf50eec2016-06-07 16:49:42 -0400184static int ssl3_get_next_proto(SSL *ssl);
185static int ssl3_get_channel_id(SSL *ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -0400186static int ssl3_send_new_session_ticket(SSL *ssl);
David Benjamindf50eec2016-06-07 16:49:42 -0400187
David Benjamin0d56f882015-12-19 17:05:56 -0500188int ssl3_accept(SSL *ssl) {
David Benjamin3fa27772015-04-17 22:32:19 -0400189 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 int ret = -1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400191 int state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700192
David Benjamin0d56f882015-12-19 17:05:56 -0500193 assert(ssl->handshake_func == ssl3_accept);
194 assert(ssl->server);
David Benjaminbeb47022014-11-30 02:58:52 -0500195
Adam Langleyfcf25832014-12-18 17:42:32 -0800196 for (;;) {
David Benjamin0d56f882015-12-19 17:05:56 -0500197 state = ssl->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700198
David Benjamin0d56f882015-12-19 17:05:56 -0500199 switch (ssl->state) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800200 case SSL_ST_ACCEPT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400201 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700202
Steven Valdez143e8b32016-07-11 13:19:03 -0400203 ssl->s3->hs = ssl_handshake_new(tls13_server_handshake);
David Benjamin481b9d22016-07-27 14:01:59 -0400204 if (ssl->s3->hs == NULL) {
David Benjamin97718f12016-07-08 09:16:50 -0700205 ret = -1;
206 goto end;
Adam Langleyfcf25832014-12-18 17:42:32 -0800207 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500208
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 Benjamin0d56f882015-12-19 17:05:56 -0500229 ret = ssl3_get_client_hello(ssl);
Steven Valdez143e8b32016-07-11 13:19:03 -0400230 if (ssl->state == SSL_ST_TLS13) {
231 break;
232 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800233 if (ret <= 0) {
234 goto end;
235 }
David Benjamindf50eec2016-06-07 16:49:42 -0400236 ssl->method->received_flight(ssl);
David Benjamin0d56f882015-12-19 17:05:56 -0500237 ssl->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800238 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500239
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 case SSL3_ST_SW_SRVR_HELLO_A:
241 case SSL3_ST_SW_SRVR_HELLO_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500242 ret = ssl3_send_server_hello(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800243 if (ret <= 0) {
244 goto end;
245 }
Steven Valdez87eab492016-06-27 16:34:59 -0400246 if (ssl->session != NULL) {
David Benjamin47749a62016-06-13 13:56:33 -0400247 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500249 ssl->state = SSL3_ST_SW_CERT_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800250 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700252
Adam Langleyfcf25832014-12-18 17:42:32 -0800253 case SSL3_ST_SW_CERT_A:
254 case SSL3_ST_SW_CERT_B:
David Benjaminc032dfa2016-05-12 14:54:57 -0400255 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500256 ret = ssl3_send_server_certificate(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 if (ret <= 0) {
258 goto end;
259 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 } else {
261 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800262 }
David Benjamin47749a62016-06-13 13:56:33 -0400263 ssl->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700265
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100266 case SSL3_ST_SW_CERT_STATUS_A:
267 case SSL3_ST_SW_CERT_STATUS_B:
David Benjamin47749a62016-06-13 13:56:33 -0400268 if (ssl->s3->tmp.certificate_status_expected) {
269 ret = ssl3_send_certificate_status(ssl);
270 if (ret <= 0) {
271 goto end;
272 }
273 } else {
274 skip = 1;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100275 }
David Benjamin0d56f882015-12-19 17:05:56 -0500276 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100277 break;
278
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 case SSL3_ST_SW_KEY_EXCH_A:
280 case SSL3_ST_SW_KEY_EXCH_B:
nagendra modadugu601448a2015-07-24 09:31:31 -0700281 case SSL3_ST_SW_KEY_EXCH_C:
David Benjamin0d56f882015-12-19 17:05:56 -0500282 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700283
David Benjamindf50eec2016-06-07 16:49:42 -0400284 /* PSK ciphers send ServerKeyExchange if there is an identity hint. */
David Benjamin0d56f882015-12-19 17:05:56 -0500285 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
286 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
287 ret = ssl3_send_server_key_exchange(ssl);
David Benjamin6eb000d2015-02-11 01:17:41 -0500288 if (ret <= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500290 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 } else {
292 skip = 1;
293 }
Adam Langley95c29f32014-06-20 12:00:00 -0700294
David Benjamin0d56f882015-12-19 17:05:56 -0500295 ssl->state = SSL3_ST_SW_CERT_REQ_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800296 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700297
Adam Langleyfcf25832014-12-18 17:42:32 -0800298 case SSL3_ST_SW_CERT_REQ_A:
299 case SSL3_ST_SW_CERT_REQ_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500300 if (ssl->s3->tmp.cert_request) {
301 ret = ssl3_send_certificate_request(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800302 if (ret <= 0) {
303 goto end;
304 }
David Benjamin5c1ce292015-05-26 16:59:43 -0400305 } else {
306 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 }
David Benjamin0d56f882015-12-19 17:05:56 -0500308 ssl->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800309 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700310
Adam Langleyfcf25832014-12-18 17:42:32 -0800311 case SSL3_ST_SW_SRVR_DONE_A:
312 case SSL3_ST_SW_SRVR_DONE_B:
David Benjamin3dcec452016-06-07 17:13:28 -0400313 ret = ssl3_send_server_hello_done(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800314 if (ret <= 0) {
315 goto end;
316 }
David Benjamin0d56f882015-12-19 17:05:56 -0500317 ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
318 ssl->state = SSL3_ST_SW_FLUSH;
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700320
Adam Langleyfcf25832014-12-18 17:42:32 -0800321 case SSL3_ST_SR_CERT_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500322 if (ssl->s3->tmp.cert_request) {
323 ret = ssl3_get_client_certificate(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800324 if (ret <= 0) {
325 goto end;
326 }
327 }
David Benjamin0d56f882015-12-19 17:05:56 -0500328 ssl->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800329 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700330
Adam Langleyfcf25832014-12-18 17:42:32 -0800331 case SSL3_ST_SR_KEY_EXCH_A:
332 case SSL3_ST_SR_KEY_EXCH_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500333 ret = ssl3_get_client_key_exchange(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800334 if (ret <= 0) {
335 goto end;
336 }
David Benjamin0d56f882015-12-19 17:05:56 -0500337 ssl->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800338 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700339
Adam Langleyfcf25832014-12-18 17:42:32 -0800340 case SSL3_ST_SR_CERT_VRFY_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500341 ret = ssl3_get_cert_verify(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800342 if (ret <= 0) {
343 goto end;
344 }
Adam Langley95c29f32014-06-20 12:00:00 -0700345
David Benjamin0d56f882015-12-19 17:05:56 -0500346 ssl->state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800347 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700348
David Benjamina41280d2015-11-26 02:16:49 -0500349 case SSL3_ST_SR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400350 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500351 if (ret <= 0) {
352 goto end;
353 }
Adam Langley95c29f32014-06-20 12:00:00 -0700354
David Benjamin57997da2015-12-25 15:48:39 -0500355 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_READ)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800356 ret = -1;
357 goto end;
358 }
David Benjamina41280d2015-11-26 02:16:49 -0500359
David Benjamin47749a62016-06-13 13:56:33 -0400360 ssl->state = SSL3_ST_SR_NEXT_PROTO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800361 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700362
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 case SSL3_ST_SR_NEXT_PROTO_A:
David Benjamin47749a62016-06-13 13:56:33 -0400364 if (ssl->s3->next_proto_neg_seen) {
365 ret = ssl3_get_next_proto(ssl);
366 if (ret <= 0) {
367 goto end;
368 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 } else {
David Benjamin47749a62016-06-13 13:56:33 -0400370 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 }
David Benjamin47749a62016-06-13 13:56:33 -0400372 ssl->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700374
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 case SSL3_ST_SR_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400376 if (ssl->s3->tlsext_channel_id_valid) {
377 ret = ssl3_get_channel_id(ssl);
378 if (ret <= 0) {
379 goto end;
380 }
381 } else {
382 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 }
David Benjamin0d56f882015-12-19 17:05:56 -0500384 ssl->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800385 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700386
Adam Langleyfcf25832014-12-18 17:42:32 -0800387 case SSL3_ST_SR_FINISHED_A:
David Benjamin1e6d6df2016-05-13 18:28:17 -0400388 ret = ssl3_get_finished(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800389 if (ret <= 0) {
390 goto end;
391 }
Adam Langley95c29f32014-06-20 12:00:00 -0700392
David Benjamindf50eec2016-06-07 16:49:42 -0400393 ssl->method->received_flight(ssl);
Steven Valdez87eab492016-06-27 16:34:59 -0400394 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500395 ssl->state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 } else {
David Benjamin47749a62016-06-13 13:56:33 -0400397 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800398 }
David Benjamindf50eec2016-06-07 16:49:42 -0400399
Steven Valdez87eab492016-06-27 16:34:59 -0400400 /* If this is a full handshake with ChannelID then record the handshake
401 * hashes in |ssl->s3->new_session| in case we need them to verify a
402 * ChannelID signature on a resumption of this session in the future. */
403 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) {
David Benjamin0d56f882015-12-19 17:05:56 -0500404 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800405 if (ret <= 0) {
406 goto end;
407 }
408 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800409 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700410
Adam Langleyfcf25832014-12-18 17:42:32 -0800411 case SSL3_ST_SW_SESSION_TICKET_A:
412 case SSL3_ST_SW_SESSION_TICKET_B:
David Benjamin47749a62016-06-13 13:56:33 -0400413 if (ssl->tlsext_ticket_expected) {
414 ret = ssl3_send_new_session_ticket(ssl);
415 if (ret <= 0) {
416 goto end;
417 }
418 } else {
419 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 }
David Benjamin352d0a92016-06-28 11:22:02 -0400421 ssl->state = SSL3_ST_SW_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700423
David Benjamin352d0a92016-06-28 11:22:02 -0400424 case SSL3_ST_SW_CHANGE:
425 ret = ssl->method->send_change_cipher_spec(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800426 if (ret <= 0) {
427 goto end;
428 }
David Benjamin0d56f882015-12-19 17:05:56 -0500429 ssl->state = SSL3_ST_SW_FINISHED_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700430
David Benjamin57997da2015-12-25 15:48:39 -0500431 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800432 ret = -1;
433 goto end;
434 }
435 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700436
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 case SSL3_ST_SW_FINISHED_A:
438 case SSL3_ST_SW_FINISHED_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500439 ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A,
David Benjaminbaa12162015-12-29 19:13:58 -0500440 SSL3_ST_SW_FINISHED_B);
Adam Langleyfcf25832014-12-18 17:42:32 -0800441 if (ret <= 0) {
442 goto end;
443 }
David Benjamin0d56f882015-12-19 17:05:56 -0500444 ssl->state = SSL3_ST_SW_FLUSH;
Steven Valdez87eab492016-06-27 16:34:59 -0400445 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500446 ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800447 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500448 ssl->s3->tmp.next_state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800449 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800450 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700451
David Benjamin9f1dc822016-06-07 17:03:46 -0400452 case SSL3_ST_SW_FLUSH:
453 if (BIO_flush(ssl->wbio) <= 0) {
454 ssl->rwstate = SSL_WRITING;
455 ret = -1;
456 goto end;
457 }
458
459 ssl->state = ssl->s3->tmp.next_state;
460 if (ssl->state != SSL_ST_OK) {
461 ssl->method->expect_flight(ssl);
462 }
463 break;
464
Steven Valdez143e8b32016-07-11 13:19:03 -0400465 case SSL_ST_TLS13:
466 ret = tls13_handshake(ssl);
467 if (ret <= 0) {
468 goto end;
469 }
470 ssl->state = SSL_ST_OK;
471 break;
472
Adam Langleyfcf25832014-12-18 17:42:32 -0800473 case SSL_ST_OK:
David Benjamin4497e582016-07-27 17:51:49 -0400474 /* Clean a few things up. */
David Benjamin0d56f882015-12-19 17:05:56 -0500475 ssl3_cleanup_key_block(ssl);
David Benjamin4497e582016-07-27 17:51:49 -0400476 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700477
Steven Valdez87eab492016-06-27 16:34:59 -0400478 /* If we aren't retaining peer certificates then we can discard it
479 * now. */
480 if (ssl->s3->new_session != NULL &&
481 ssl->ctx->retain_only_sha256_of_client_certs) {
482 X509_free(ssl->s3->new_session->peer);
483 ssl->s3->new_session->peer = NULL;
484 sk_X509_pop_free(ssl->s3->new_session->cert_chain, X509_free);
485 ssl->s3->new_session->cert_chain = NULL;
486 }
487
488 SSL_SESSION_free(ssl->s3->established_session);
489 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400490 SSL_SESSION_up_ref(ssl->session);
491 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400492 } else {
493 ssl->s3->established_session = ssl->s3->new_session;
494 ssl->s3->established_session->not_resumable = 0;
495 ssl->s3->new_session = NULL;
496 }
497
Adam Langleyfcf25832014-12-18 17:42:32 -0800498 /* remove buffering on output */
David Benjamin0d56f882015-12-19 17:05:56 -0500499 ssl_free_wbio_buffer(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Steven Valdez143e8b32016-07-11 13:19:03 -0400501 ssl_handshake_free(ssl->s3->hs);
502 ssl->s3->hs = NULL;
503
David Benjamin0d56f882015-12-19 17:05:56 -0500504 ssl->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700505
David Benjamin0d56f882015-12-19 17:05:56 -0500506 ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER);
Adam Langley95c29f32014-06-20 12:00:00 -0700507
David Benjamin4e9cc712016-06-01 20:16:03 -0400508 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley75872532014-06-20 12:00:00 -0700509
Adam Langleyfcf25832014-12-18 17:42:32 -0800510 ret = 1;
511 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700512
Adam Langleyfcf25832014-12-18 17:42:32 -0800513 default:
David Benjamin3570d732015-06-29 00:28:17 -0400514 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800515 ret = -1;
516 goto end;
517 }
Adam Langley95c29f32014-06-20 12:00:00 -0700518
David Benjamin4e9cc712016-06-01 20:16:03 -0400519 if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) {
520 int new_state = ssl->state;
David Benjamin0d56f882015-12-19 17:05:56 -0500521 ssl->state = state;
David Benjamin4e9cc712016-06-01 20:16:03 -0400522 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
David Benjamin0d56f882015-12-19 17:05:56 -0500523 ssl->state = new_state;
Adam Langleyfcf25832014-12-18 17:42:32 -0800524 }
525 skip = 0;
526 }
527
Adam Langley95c29f32014-06-20 12:00:00 -0700528end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400529 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800530 return ret;
531}
Adam Langley95c29f32014-06-20 12:00:00 -0700532
David Benjamin1deb41b2016-08-09 19:36:38 -0400533int ssl_client_cipher_list_contains_cipher(
534 const struct ssl_early_callback_ctx *client_hello, uint16_t id) {
535 CBS cipher_suites;
536 CBS_init(&cipher_suites, client_hello->cipher_suites,
537 client_hello->cipher_suites_len);
538
539 while (CBS_len(&cipher_suites) > 0) {
540 uint16_t got_id;
541 if (!CBS_get_u16(&cipher_suites, &got_id)) {
542 return 0;
543 }
544
545 if (got_id == id) {
546 return 1;
547 }
548 }
549
550 return 0;
551}
552
David Benjamin25fe85b2016-08-09 20:00:32 -0400553static int negotiate_version(
554 SSL *ssl, int *out_alert,
555 const struct ssl_early_callback_ctx *client_hello) {
556 uint16_t min_version, max_version;
557 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
558 *out_alert = SSL_AD_PROTOCOL_VERSION;
559 return 0;
560 }
561
562 uint16_t client_version =
563 ssl->method->version_from_wire(client_hello->version);
564 ssl->client_version = client_hello->version;
565
566 /* Select the version to use. */
567 uint16_t version = client_version;
568 if (version > max_version) {
569 version = max_version;
570 }
571
572 if (version < min_version) {
573 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
574 *out_alert = SSL_AD_PROTOCOL_VERSION;
575 return 0;
576 }
577
578 /* Handle FALLBACK_SCSV. */
579 if (ssl_client_cipher_list_contains_cipher(client_hello,
580 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
581 version < max_version) {
582 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
583 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
584 return 0;
585 }
586
587 ssl->version = ssl->method->version_to_wire(version);
588 ssl->s3->enc_method = ssl3_get_enc_method(version);
589 assert(ssl->s3->enc_method != NULL);
590
591 /* At this point, the connection's version is known and |ssl->version| is
592 * fixed. Begin enforcing the record-layer version. */
593 ssl->s3->have_version = 1;
594
595 return 1;
596}
597
David Benjamindf50eec2016-06-07 16:49:42 -0400598static int ssl3_get_client_hello(SSL *ssl) {
David Benjamin09eb6552016-07-08 14:32:11 -0700599 int al = SSL_AD_INTERNAL_ERROR, ret = -1;
David Benjamine3aa1d92015-06-16 15:34:50 -0400600 SSL_SESSION *session = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700601
David Benjamine14ff062016-08-09 16:21:24 -0400602 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_A) {
603 /* The first time around, read the ClientHello. */
604 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO,
605 ssl_hash_message);
606 if (msg_ret <= 0) {
607 return msg_ret;
David Benjamin09eb6552016-07-08 14:32:11 -0700608 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400609
David Benjamine14ff062016-08-09 16:21:24 -0400610 ssl->state = SSL3_ST_SR_CLNT_HELLO_B;
Adam Langleyfcf25832014-12-18 17:42:32 -0800611 }
Adam Langley95c29f32014-06-20 12:00:00 -0700612
David Benjamine14ff062016-08-09 16:21:24 -0400613 struct ssl_early_callback_ctx client_hello;
614 if (!ssl_early_callback_init(ssl, &client_hello, ssl->init_msg,
615 ssl->init_num)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800616 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400617 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800618 goto f_err;
619 }
Adam Langley95c29f32014-06-20 12:00:00 -0700620
David Benjamine14ff062016-08-09 16:21:24 -0400621 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_B) {
622 /* Unlike other callbacks, the early callback is not run a second time if
623 * paused. */
624 ssl->state = SSL3_ST_SR_CLNT_HELLO_C;
David Benjaminb6a0a512016-06-21 10:33:21 -0400625
David Benjamine14ff062016-08-09 16:21:24 -0400626 /* Run the early callback. */
627 if (ssl->ctx->select_certificate_cb != NULL) {
628 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
629 case 0:
630 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
631 goto err;
632
633 case -1:
634 /* Connection rejected. */
635 al = SSL_AD_ACCESS_DENIED;
636 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
637 goto f_err;
638
639 default:
640 /* fallthrough */;
641 }
642 }
643 }
644
David Benjamin25fe85b2016-08-09 20:00:32 -0400645 /* Negotiate the protocol version if we have not done so yet. */
David Benjamin0d56f882015-12-19 17:05:56 -0500646 if (!ssl->s3->have_version) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400647 if (!negotiate_version(ssl, &al, &client_hello)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800648 goto f_err;
649 }
David Benjamin1deb41b2016-08-09 19:36:38 -0400650
David Benjamin25fe85b2016-08-09 20:00:32 -0400651 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
652 ssl->state = SSL_ST_TLS13;
653 return 1;
654 }
655 }
656
657 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_C) {
658 /* Load the client random. */
659 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
660 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
661 return -1;
662 }
663 memcpy(ssl->s3->client_random, client_hello.random,
664 client_hello.random_len);
665
666 /* Determine whether we are doing session resumption. */
667 int send_new_ticket = 0;
668 switch (
669 ssl_get_prev_session(ssl, &session, &send_new_ticket, &client_hello)) {
670 case ssl_session_success:
671 break;
672 case ssl_session_error:
673 goto err;
674 case ssl_session_retry:
675 ssl->rwstate = SSL_PENDING_SESSION;
676 goto err;
677 }
678 ssl->tlsext_ticket_expected = send_new_ticket;
679
680 /* The EMS state is needed when making the resumption decision, but
681 * extensions are not normally parsed until later. This detects the EMS
682 * extension for the resumption decision and it's checked against the result
683 * of the normal parse later in this function. */
684 CBS ems;
685 int have_extended_master_secret =
686 ssl->version != SSL3_VERSION &&
687 ssl_early_callback_get_extension(&client_hello, &ems,
688 TLSEXT_TYPE_extended_master_secret) &&
689 CBS_len(&ems) == 0;
690
691 int has_session = 0;
692 if (session != NULL) {
693 if (session->extended_master_secret &&
694 !have_extended_master_secret) {
695 /* A ClientHello without EMS that attempts to resume a session with EMS
696 * is fatal to the connection. */
697 al = SSL_AD_HANDSHAKE_FAILURE;
698 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
699 goto f_err;
700 }
701
702 has_session =
703 /* Only resume if the session's version matches the negotiated
704 * version: most clients do not accept a mismatch. */
705 ssl->version == session->ssl_version &&
706 /* If the client offers the EMS extension, but the previous session
707 * didn't use it, then negotiate a new session. */
708 have_extended_master_secret == session->extended_master_secret;
709 }
710
711 if (has_session) {
712 /* Use the old session. */
713 ssl->session = session;
714 session = NULL;
715 ssl->verify_result = ssl->session->verify_result;
Steven Valdez4aa154e2016-07-29 14:32:55 -0400716 ssl->s3->session_reused = 1;
David Benjamin25fe85b2016-08-09 20:00:32 -0400717 } else {
718 SSL_set_session(ssl, NULL);
719 if (!ssl_get_new_session(ssl, 1 /* server */)) {
720 goto err;
721 }
722
723 /* Clear the session ID if we want the session to be single-use. */
724 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
725 ssl->s3->new_session->session_id_length = 0;
726 }
727 }
728
729 if (ssl->ctx->dos_protection_cb != NULL &&
730 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
731 /* Connection rejected for DOS reasons. */
732 al = SSL_AD_ACCESS_DENIED;
733 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
David Benjamin1deb41b2016-08-09 19:36:38 -0400734 goto f_err;
735 }
736
David Benjamin25fe85b2016-08-09 20:00:32 -0400737 /* Only null compression is supported. */
738 if (memchr(client_hello.compression_methods, 0,
739 client_hello.compression_methods_len) == NULL) {
740 al = SSL_AD_ILLEGAL_PARAMETER;
741 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
742 goto f_err;
743 }
David Benjamin8b8c0062014-11-23 02:47:52 -0500744
David Benjamin25fe85b2016-08-09 20:00:32 -0400745 /* TLS extensions. */
746 if (!ssl_parse_clienthello_tlsext(ssl, &client_hello)) {
747 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
David Benjamine3aa1d92015-06-16 15:34:50 -0400748 goto err;
David Benjamin25fe85b2016-08-09 20:00:32 -0400749 }
David Benjamin4b30b282015-05-26 16:37:58 -0400750
David Benjamin25fe85b2016-08-09 20:00:32 -0400751 if (have_extended_master_secret != ssl->s3->tmp.extended_master_secret) {
752 al = SSL_AD_INTERNAL_ERROR;
753 OPENSSL_PUT_ERROR(SSL, SSL_R_EMS_STATE_INCONSISTENT);
Adam Langleyba5934b2015-06-02 10:50:35 -0700754 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800755 }
Adam Langleyba5934b2015-06-02 10:50:35 -0700756
David Benjamin25fe85b2016-08-09 20:00:32 -0400757 ssl->state = SSL3_ST_SR_CLNT_HELLO_D;
Adam Langleyba5934b2015-06-02 10:50:35 -0700758 }
759
David Benjamin25fe85b2016-08-09 20:00:32 -0400760 /* Determine the remaining connection parameters. This is a separate state so
761 * |cert_cb| does not cause earlier logic to run multiple times. */
762 assert(ssl->state == SSL3_ST_SR_CLNT_HELLO_D);
763
764 if (ssl->session != NULL) {
765 /* Check that the cipher is in the list. */
766 if (!ssl_client_cipher_list_contains_cipher(
767 &client_hello, (uint16_t)ssl->session->cipher->id)) {
768 al = SSL_AD_ILLEGAL_PARAMETER;
769 OPENSSL_PUT_ERROR(SSL, SSL_R_REQUIRED_CIPHER_MISSING);
770 goto f_err;
771 }
772
773 ssl->s3->tmp.new_cipher = ssl->session->cipher;
774 ssl->s3->tmp.cert_request = 0;
David Benjamin56380462015-10-10 14:59:09 -0400775 } else {
David Benjamin25fe85b2016-08-09 20:00:32 -0400776 /* Call |cert_cb| to update server certificates if required. */
777 if (ssl->cert->cert_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500778 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800779 if (rv == 0) {
780 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400781 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800782 goto f_err;
783 }
784 if (rv < 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500785 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langleyfcf25832014-12-18 17:42:32 -0800786 goto err;
787 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800788 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800789
David Benjamin25fe85b2016-08-09 20:00:32 -0400790 const SSL_CIPHER *c =
791 ssl3_choose_cipher(ssl, &client_hello, ssl_get_cipher_preferences(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -0800792 if (c == NULL) {
793 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -0400794 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
Adam Langleyfcf25832014-12-18 17:42:32 -0800795 goto f_err;
796 }
David Benjaminfddbadc2016-08-09 19:53:00 -0400797
Steven Valdez87eab492016-06-27 16:34:59 -0400798 ssl->s3->new_session->cipher = c;
David Benjamin0d56f882015-12-19 17:05:56 -0500799 ssl->s3->tmp.new_cipher = c;
David Benjamin5c1ce292015-05-26 16:59:43 -0400800
801 /* Determine whether to request a client certificate. */
David Benjamin0d56f882015-12-19 17:05:56 -0500802 ssl->s3->tmp.cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjamin5c1ce292015-05-26 16:59:43 -0400803 /* Only request a certificate if Channel ID isn't negotiated. */
David Benjamin0d56f882015-12-19 17:05:56 -0500804 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
805 ssl->s3->tlsext_channel_id_valid) {
806 ssl->s3->tmp.cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400807 }
David Benjaminc032dfa2016-05-12 14:54:57 -0400808 /* CertificateRequest may only be sent in certificate-based ciphers. */
809 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500810 ssl->s3->tmp.cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400811 }
Adam Langley37646832016-08-01 16:16:46 -0700812
813 if (!ssl->s3->tmp.cert_request) {
814 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
David Benjamindd634eb2016-08-18 16:40:28 -0400815 * classed by them as a bug, but it's assumed by at least NGINX. */
Adam Langley37646832016-08-01 16:16:46 -0700816 ssl->verify_result = X509_V_OK;
817 ssl->s3->new_session->verify_result = X509_V_OK;
818 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800819 }
820
David Benjamin9550c3a2015-08-05 08:50:34 -0400821 /* Now that the cipher is known, initialize the handshake hash. */
David Benjamin0d56f882015-12-19 17:05:56 -0500822 if (!ssl3_init_handshake_hash(ssl)) {
David Benjamin9550c3a2015-08-05 08:50:34 -0400823 goto f_err;
824 }
825
Steven Valdez2b8415e2016-06-30 13:27:23 -0400826 /* Release the handshake buffer if client authentication isn't required. */
827 if (!ssl->s3->tmp.cert_request) {
David Benjamin0d56f882015-12-19 17:05:56 -0500828 ssl3_free_handshake_buffer(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800829 }
830
David Benjamin0bd71eb2015-12-01 14:14:40 -0500831 ret = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800832
833 if (0) {
834 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -0500835 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -0800836 }
837
Adam Langley95c29f32014-06-20 12:00:00 -0700838err:
David Benjamine3aa1d92015-06-16 15:34:50 -0400839 SSL_SESSION_free(session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800840 return ret;
841}
Adam Langley95c29f32014-06-20 12:00:00 -0700842
David Benjamindf50eec2016-06-07 16:49:42 -0400843static int ssl3_send_server_hello(SSL *ssl) {
David Benjamin56380462015-10-10 14:59:09 -0400844 if (ssl->state == SSL3_ST_SW_SRVR_HELLO_B) {
David Benjamin75836432016-06-17 18:48:29 -0400845 return ssl->method->write_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800846 }
Adam Langley95c29f32014-06-20 12:00:00 -0700847
David Benjamin56380462015-10-10 14:59:09 -0400848 assert(ssl->state == SSL3_ST_SW_SRVR_HELLO_A);
849
850 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
851 * known attack while we fix ChannelID itself. */
852 if (ssl->s3->tlsext_channel_id_valid &&
853 (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
854 ssl->s3->tlsext_channel_id_valid = 0;
855 }
856
857 /* If this is a resumption and the original handshake didn't support
858 * ChannelID then we didn't record the original handshake hashes in the
859 * session and so cannot resume with ChannelIDs. */
Steven Valdez87eab492016-06-27 16:34:59 -0400860 if (ssl->session != NULL &&
861 ssl->session->original_handshake_hash_len == 0) {
David Benjamin56380462015-10-10 14:59:09 -0400862 ssl->s3->tlsext_channel_id_valid = 0;
863 }
864
David Benjamin721e8b72016-08-03 13:13:17 -0400865 struct timeval now;
866 ssl_get_current_time(ssl, &now);
867 ssl->s3->server_random[0] = now.tv_sec >> 24;
868 ssl->s3->server_random[1] = now.tv_sec >> 16;
869 ssl->s3->server_random[2] = now.tv_sec >> 8;
870 ssl->s3->server_random[3] = now.tv_sec;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400871 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
David Benjamin56380462015-10-10 14:59:09 -0400872 return -1;
873 }
874
David Benjamin1f61f0d2016-07-10 12:20:35 -0400875 /* Fill in the TLS 1.2 downgrade signal. See draft-ietf-tls-tls13-14.
876 *
877 * TODO(davidben): Also implement the TLS 1.1 sentinel when things have
878 * settled down. */
879 uint16_t min_version, max_version;
880 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
881 return -1;
882 }
883 if (max_version >= TLS1_3_VERSION &&
884 ssl3_protocol_version(ssl) <= TLS1_2_VERSION) {
885 static const uint8_t kDowngradeTLS12[8] = {0x44, 0x4f, 0x57, 0x4e,
886 0x47, 0x52, 0x44, 0x01};
887 memcpy(ssl->s3->server_random + SSL3_RANDOM_SIZE - 8, kDowngradeTLS12, 8);
888 }
889
Steven Valdez87eab492016-06-27 16:34:59 -0400890 const SSL_SESSION *session = ssl->s3->new_session;
891 if (ssl->session != NULL) {
892 session = ssl->session;
893 }
894
David Benjamin75836432016-06-17 18:48:29 -0400895 CBB cbb, body, session_id;
896 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
897 !CBB_add_u16(&body, ssl->version) ||
898 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
899 !CBB_add_u8_length_prefixed(&body, &session_id) ||
Steven Valdez87eab492016-06-27 16:34:59 -0400900 !CBB_add_bytes(&session_id, session->session_id,
901 session->session_id_length) ||
David Benjamin75836432016-06-17 18:48:29 -0400902 !CBB_add_u16(&body, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
903 !CBB_add_u8(&body, 0 /* no compression */) ||
904 !ssl_add_serverhello_tlsext(ssl, &body) ||
905 !ssl->method->finish_message(ssl, &cbb)) {
David Benjamin56380462015-10-10 14:59:09 -0400906 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
907 CBB_cleanup(&cbb);
908 return -1;
909 }
910
911 ssl->state = SSL3_ST_SW_SRVR_HELLO_B;
David Benjamin75836432016-06-17 18:48:29 -0400912 return ssl->method->write_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800913}
Adam Langley95c29f32014-06-20 12:00:00 -0700914
David Benjamin9f1dc822016-06-07 17:03:46 -0400915static int ssl3_send_server_certificate(SSL *ssl) {
David Benjamin75836432016-06-17 18:48:29 -0400916 if (ssl->state == SSL3_ST_SW_CERT_B) {
917 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -0400918 }
919
David Benjamin32a66d52016-07-13 22:03:11 -0400920 if (!ssl_has_certificate(ssl)) {
921 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
922 return 0;
923 }
924
David Benjamin75836432016-06-17 18:48:29 -0400925 if (!ssl3_output_cert_chain(ssl)) {
926 return 0;
927 }
928 ssl->state = SSL3_ST_SW_CERT_B;
929 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -0400930}
931
David Benjamindf50eec2016-06-07 16:49:42 -0400932static int ssl3_send_certificate_status(SSL *ssl) {
David Benjamin75836432016-06-17 18:48:29 -0400933 if (ssl->state == SSL3_ST_SW_CERT_STATUS_B) {
934 return ssl->method->write_message(ssl);
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100935 }
936
David Benjamin75836432016-06-17 18:48:29 -0400937 CBB cbb, body, ocsp_response;
938 if (!ssl->method->init_message(ssl, &cbb, &body,
939 SSL3_MT_CERTIFICATE_STATUS) ||
940 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
941 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
942 !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
943 ssl->ctx->ocsp_response_length) ||
944 !ssl->method->finish_message(ssl, &cbb)) {
945 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
946 CBB_cleanup(&cbb);
947 return -1;
948 }
949
950 ssl->state = SSL3_ST_SW_CERT_STATUS_B;
951 return ssl->method->write_message(ssl);
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100952}
953
David Benjamindf50eec2016-06-07 16:49:42 -0400954static int ssl3_send_server_key_exchange(SSL *ssl) {
David Benjamin2a0b3912015-12-18 01:01:21 -0500955 if (ssl->state == SSL3_ST_SW_KEY_EXCH_C) {
David Benjamin75836432016-06-17 18:48:29 -0400956 return ssl->method->write_message(ssl);
David Benjamin1f9f9c42015-08-28 16:17:59 -0400957 }
958
David Benjamin2a0b3912015-12-18 01:01:21 -0500959 CBB cbb, child;
David Benjaminc42acee2016-06-17 17:47:58 -0400960 CBB_zero(&cbb);
nagendra modadugu601448a2015-07-24 09:31:31 -0700961
David Benjaminc42acee2016-06-17 17:47:58 -0400962 /* Put together the parameters. */
David Benjamin2a0b3912015-12-18 01:01:21 -0500963 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
David Benjamin2a0b3912015-12-18 01:01:21 -0500964 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
965 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700966
David Benjaminc42acee2016-06-17 17:47:58 -0400967 /* Pre-allocate enough room to comfortably fit an ECDHE public key. */
968 if (!CBB_init(&cbb, 128)) {
969 goto err;
970 }
971
David Benjamin2a0b3912015-12-18 01:01:21 -0500972 /* PSK ciphers begin with an identity hint. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800973 if (alg_a & SSL_aPSK) {
David Benjamin2a0b3912015-12-18 01:01:21 -0500974 size_t len =
975 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
976 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
977 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
978 len)) {
979 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800980 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800981 }
Adam Langley95c29f32014-06-20 12:00:00 -0700982
David Benjamin7061e282015-03-19 11:10:48 -0400983 if (alg_k & SSL_kDHE) {
David Benjamin2a0b3912015-12-18 01:01:21 -0500984 /* Determine the group to use. */
985 DH *params = ssl->cert->dh_tmp;
986 if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
987 params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
Adam Langleyfcf25832014-12-18 17:42:32 -0800988 }
David Benjamin2a0b3912015-12-18 01:01:21 -0500989 if (params == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400990 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -0500991 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800992 goto err;
993 }
Steven Valdez87eab492016-06-27 16:34:59 -0400994 ssl->s3->new_session->key_exchange_info = DH_num_bits(params);
David Benjamin2a0b3912015-12-18 01:01:21 -0500995
David Benjamin974c7ba2015-12-19 16:58:04 -0500996 /* Set up DH, generate a key, and emit the public half. */
David Benjamin2a0b3912015-12-18 01:01:21 -0500997 DH *dh = DHparams_dup(params);
David Benjamin974c7ba2015-12-19 16:58:04 -0500998 if (dh == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800999 goto err;
1000 }
David Benjamin9f226a52015-04-26 22:12:32 -04001001
David Benjamin974c7ba2015-12-19 16:58:04 -05001002 SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh);
David Benjamin2a0b3912015-12-18 01:01:21 -05001003 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001004 !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001005 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001006 !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001007 !CBB_add_u16_length_prefixed(&cbb, &child) ||
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001008 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
David Benjamin9f226a52015-04-26 22:12:32 -04001009 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001010 }
David Benjamin7061e282015-03-19 11:10:48 -04001011 } else if (alg_k & SSL_kECDHE) {
Steven Valdezce902a92016-05-17 11:47:53 -04001012 /* Determine the group to use. */
1013 uint16_t group_id;
1014 if (!tls1_get_shared_group(ssl, &group_id)) {
David Benjamin3570d732015-06-29 00:28:17 -04001015 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001016 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001017 goto err;
1018 }
Steven Valdez87eab492016-06-27 16:34:59 -04001019 ssl->s3->new_session->key_exchange_info = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -07001020
David Benjamin4298d772015-12-19 00:18:25 -05001021 /* Set up ECDH, generate a key, and emit the public half. */
Steven Valdezce902a92016-05-17 11:47:53 -04001022 if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, group_id) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001023 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
Steven Valdezce902a92016-05-17 11:47:53 -04001024 !CBB_add_u16(&cbb, group_id) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001025 !CBB_add_u8_length_prefixed(&cbb, &child) ||
1026 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
1027 goto err;
1028 }
1029 } else if (alg_k & SSL_kCECPQ1) {
David Benjamin18a35182016-06-30 13:46:24 -04001030 SSL_ECDH_CTX_init_for_cecpq1(&ssl->s3->tmp.ecdh_ctx);
1031 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001032 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001033 goto err;
1034 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001035 } else {
1036 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001037 }
Adam Langley95c29f32014-06-20 12:00:00 -07001038
David Benjaminc42acee2016-06-17 17:47:58 -04001039 size_t len;
1040 if (!CBB_finish(&cbb, &ssl->s3->tmp.server_params, &len) ||
1041 len > 0xffffffffu) {
1042 OPENSSL_free(ssl->s3->tmp.server_params);
1043 ssl->s3->tmp.server_params = NULL;
1044 goto err;
1045 }
1046 ssl->s3->tmp.server_params_len = (uint32_t)len;
1047 }
1048
1049 /* Assemble the message. */
David Benjamin75836432016-06-17 18:48:29 -04001050 CBB body;
1051 if (!ssl->method->init_message(ssl, &cbb, &body,
1052 SSL3_MT_SERVER_KEY_EXCHANGE) ||
1053 !CBB_add_bytes(&body, ssl->s3->tmp.server_params,
David Benjaminc42acee2016-06-17 17:47:58 -04001054 ssl->s3->tmp.server_params_len)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001055 goto err;
1056 }
Adam Langley95c29f32014-06-20 12:00:00 -07001057
David Benjamin2a0b3912015-12-18 01:01:21 -05001058 /* Add a signature. */
David Benjaminc032dfa2016-05-12 14:54:57 -04001059 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001060 if (!ssl_has_private_key(ssl)) {
1061 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001062 goto err;
1063 }
Adam Langley95c29f32014-06-20 12:00:00 -07001064
David Benjaminc42acee2016-06-17 17:47:58 -04001065 /* Determine the signature algorithm. */
David Benjaminea9a0d52016-07-08 15:52:59 -07001066 uint16_t signature_algorithm;
1067 if (!tls1_choose_signature_algorithm(ssl, &signature_algorithm)) {
1068 goto err;
1069 }
David Benjaminc42acee2016-06-17 17:47:58 -04001070 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001071 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001072 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1073 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1074 goto err;
1075 }
David Benjaminc42acee2016-06-17 17:47:58 -04001076 }
1077
1078 /* Add space for the signature. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001079 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjaminc42acee2016-06-17 17:47:58 -04001080 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001081 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -04001082 !CBB_reserve(&child, &ptr, max_sig_len)) {
1083 goto err;
1084 }
1085
David Benjamin2a0b3912015-12-18 01:01:21 -05001086 size_t sig_len;
1087 enum ssl_private_key_result_t sign_result;
1088 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001089 CBB transcript;
1090 uint8_t *transcript_data;
1091 size_t transcript_len;
1092 if (!CBB_init(&transcript,
1093 2*SSL3_RANDOM_SIZE + ssl->s3->tmp.server_params_len) ||
1094 !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1095 !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1096 !CBB_add_bytes(&transcript, ssl->s3->tmp.server_params,
1097 ssl->s3->tmp.server_params_len) ||
1098 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1099 CBB_cleanup(&transcript);
1100 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1101 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdezf0451ca2016-06-29 13:16:27 -04001102 goto err;
1103 }
1104
Steven Valdezf0451ca2016-06-29 13:16:27 -04001105 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001106 signature_algorithm, transcript_data,
1107 transcript_len);
1108 OPENSSL_free(transcript_data);
David Benjamin1f9f9c42015-08-28 16:17:59 -04001109 } else {
David Benjamin2a0b3912015-12-18 01:01:21 -05001110 assert(ssl->state == SSL3_ST_SW_KEY_EXCH_B);
David Benjamind3440b42016-07-14 14:52:41 -04001111 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001112 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001113
1114 switch (sign_result) {
1115 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -05001116 if (!CBB_did_write(&child, sig_len)) {
1117 goto err;
1118 }
1119 break;
1120 case ssl_private_key_failure:
David Benjamin2a0b3912015-12-18 01:01:21 -05001121 goto err;
1122 case ssl_private_key_retry:
David Benjamin2a0b3912015-12-18 01:01:21 -05001123 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
1124 ssl->state = SSL3_ST_SW_KEY_EXCH_B;
1125 goto err;
1126 }
David Benjamin1f9f9c42015-08-28 16:17:59 -04001127 }
1128
David Benjamin75836432016-06-17 18:48:29 -04001129 if (!ssl->method->finish_message(ssl, &cbb)) {
David Benjamin1f9f9c42015-08-28 16:17:59 -04001130 goto err;
1131 }
David Benjaminc42acee2016-06-17 17:47:58 -04001132
1133 OPENSSL_free(ssl->s3->tmp.server_params);
1134 ssl->s3->tmp.server_params = NULL;
1135 ssl->s3->tmp.server_params_len = 0;
1136
David Benjamin2a0b3912015-12-18 01:01:21 -05001137 ssl->state = SSL3_ST_SW_KEY_EXCH_C;
David Benjamin75836432016-06-17 18:48:29 -04001138 return ssl->method->write_message(ssl);
nagendra modadugu601448a2015-07-24 09:31:31 -07001139
Adam Langley95c29f32014-06-20 12:00:00 -07001140err:
David Benjamin2a0b3912015-12-18 01:01:21 -05001141 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001142 return -1;
1143}
Adam Langley95c29f32014-06-20 12:00:00 -07001144
David Benjamin75836432016-06-17 18:48:29 -04001145static int add_cert_types(SSL *ssl, CBB *cbb) {
1146 /* Get configured signature algorithms. */
1147 int have_rsa_sign = 0;
1148 int have_ecdsa_sign = 0;
Steven Valdez02563852016-06-23 13:33:05 -04001149 const uint16_t *sig_algs;
1150 size_t sig_algs_len = tls12_get_psigalgs(ssl, &sig_algs);
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 size_t i;
Steven Valdez02563852016-06-23 13:33:05 -04001152 for (i = 0; i < sig_algs_len; i++) {
1153 switch (sig_algs[i]) {
1154 case SSL_SIGN_RSA_PKCS1_SHA512:
1155 case SSL_SIGN_RSA_PKCS1_SHA384:
1156 case SSL_SIGN_RSA_PKCS1_SHA256:
1157 case SSL_SIGN_RSA_PKCS1_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001158 have_rsa_sign = 1;
1159 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001160
Steven Valdez02563852016-06-23 13:33:05 -04001161 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
1162 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
1163 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
1164 case SSL_SIGN_ECDSA_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001165 have_ecdsa_sign = 1;
1166 break;
Adam Langleyfcf25832014-12-18 17:42:32 -08001167 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001168 }
Adam Langley95c29f32014-06-20 12:00:00 -07001169
David Benjamin75836432016-06-17 18:48:29 -04001170 if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) {
1171 return 0;
1172 }
1173
1174 /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
1175 * check for SSL_kECDH or SSL_kECDHE. */
1176 if (ssl->version >= TLS1_VERSION && have_ecdsa_sign &&
1177 !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) {
1178 return 0;
1179 }
1180
1181 return 1;
1182}
1183
1184static int ssl3_send_certificate_request(SSL *ssl) {
1185 if (ssl->state == SSL3_ST_SW_CERT_REQ_B) {
1186 return ssl->method->write_message(ssl);
1187 }
1188
David Benjamin32a66d52016-07-13 22:03:11 -04001189 CBB cbb, body, cert_types, sigalgs_cbb;
David Benjamin75836432016-06-17 18:48:29 -04001190 if (!ssl->method->init_message(ssl, &cbb, &body,
1191 SSL3_MT_CERTIFICATE_REQUEST) ||
1192 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1193 !add_cert_types(ssl, &cert_types)) {
1194 goto err;
1195 }
1196
1197 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001198 const uint16_t *sigalgs;
David Benjamin75836432016-06-17 18:48:29 -04001199 size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -04001200 if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001201 goto err;
1202 }
Steven Valdez02563852016-06-23 13:33:05 -04001203
1204 size_t i;
1205 for (i = 0; i < sigalgs_len; i++) {
1206 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
1207 goto err;
1208 }
1209 }
David Benjamin75836432016-06-17 18:48:29 -04001210 }
1211
David Benjamin32a66d52016-07-13 22:03:11 -04001212 if (!ssl_add_client_CA_list(ssl, &body) ||
1213 !ssl->method->finish_message(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001214 goto err;
1215 }
1216
1217 ssl->state = SSL3_ST_SW_CERT_REQ_B;
1218 return ssl->method->write_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001219
Adam Langley95c29f32014-06-20 12:00:00 -07001220err:
David Benjamin75836432016-06-17 18:48:29 -04001221 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1222 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001223 return -1;
1224}
Adam Langley95c29f32014-06-20 12:00:00 -07001225
David Benjamin3dcec452016-06-07 17:13:28 -04001226static int ssl3_send_server_hello_done(SSL *ssl) {
David Benjamin75836432016-06-17 18:48:29 -04001227 if (ssl->state == SSL3_ST_SW_SRVR_DONE_B) {
1228 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001229 }
1230
David Benjamin75836432016-06-17 18:48:29 -04001231 CBB cbb, body;
1232 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
1233 !ssl->method->finish_message(ssl, &cbb)) {
1234 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1235 CBB_cleanup(&cbb);
1236 return -1;
1237 }
1238
1239 ssl->state = SSL3_ST_SW_SRVR_DONE_B;
1240 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001241}
1242
1243static int ssl3_get_client_certificate(SSL *ssl) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001244 assert(ssl->s3->tmp.cert_request);
David Benjamin9f1dc822016-06-07 17:03:46 -04001245
David Benjamin09eb6552016-07-08 14:32:11 -07001246 int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1247 if (msg_ret <= 0) {
1248 return msg_ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001249 }
1250
1251 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1252 if (ssl->version == SSL3_VERSION &&
1253 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Adam Langley37646832016-08-01 16:16:46 -07001254 /* In SSL 3.0, the Certificate message is omitted to signal no
1255 * certificate. */
David Benjaminda2630c2016-08-01 19:57:43 -04001256 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001257 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001258 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1259 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001260 }
1261
Adam Langley37646832016-08-01 16:16:46 -07001262 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001263 * classed by them as a bug, but it's assumed by at least NGINX. */
Adam Langley37646832016-08-01 16:16:46 -07001264 ssl->verify_result = X509_V_OK;
1265 ssl->s3->new_session->verify_result = X509_V_OK;
David Benjamin9f1dc822016-06-07 17:03:46 -04001266 ssl->s3->tmp.reuse_message = 1;
1267 return 1;
1268 }
1269
David Benjamin9f1dc822016-06-07 17:03:46 -04001270 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin5c900c82016-07-13 23:03:26 -04001271 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1272 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001273 }
1274
David Benjamin5c900c82016-07-13 23:03:26 -04001275 CBS certificate_msg;
David Benjamin09eb6552016-07-08 14:32:11 -07001276 CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
David Benjamin5c900c82016-07-13 23:03:26 -04001277 uint8_t alert;
1278 STACK_OF(X509) *chain = ssl_parse_cert_chain(
Steven Valdez87eab492016-06-27 16:34:59 -04001279 ssl, &alert, ssl->ctx->retain_only_sha256_of_client_certs
1280 ? ssl->s3->new_session->peer_sha256
1281 : NULL,
David Benjamin5c900c82016-07-13 23:03:26 -04001282 &certificate_msg);
1283 if (chain == NULL) {
1284 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin9f1dc822016-06-07 17:03:46 -04001285 goto err;
1286 }
1287
David Benjamin5c900c82016-07-13 23:03:26 -04001288 if (CBS_len(&certificate_msg) != 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001289 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001290 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1291 goto err;
David Benjamin9f1dc822016-06-07 17:03:46 -04001292 }
1293
David Benjamin5c900c82016-07-13 23:03:26 -04001294 if (sk_X509_num(chain) == 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001295 /* No client certificate so the handshake buffer may be discarded. */
1296 ssl3_free_handshake_buffer(ssl);
1297
David Benjamine455e512016-08-01 20:11:13 -04001298 /* In SSL 3.0, sending no certificate is signaled by omitting the
1299 * Certificate message. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001300 if (ssl->version == SSL3_VERSION) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001301 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjamin5c900c82016-07-13 23:03:26 -04001302 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1303 goto err;
David Benjamine455e512016-08-01 20:11:13 -04001304 }
1305
1306 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001307 /* Fail for TLS only if we required a certificate */
1308 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001309 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1310 goto err;
David Benjamin9f1dc822016-06-07 17:03:46 -04001311 }
Adam Langley37646832016-08-01 16:16:46 -07001312
1313 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001314 * classed by them as a bug, but it's assumed by at least NGINX. */
Adam Langley37646832016-08-01 16:16:46 -07001315 ssl->verify_result = X509_V_OK;
David Benjamin9f1dc822016-06-07 17:03:46 -04001316 } else {
David Benjamin5c900c82016-07-13 23:03:26 -04001317 /* The hash would have been filled in. */
1318 if (ssl->ctx->retain_only_sha256_of_client_certs) {
Steven Valdez87eab492016-06-27 16:34:59 -04001319 ssl->s3->new_session->peer_sha256_valid = 1;
David Benjamin5c900c82016-07-13 23:03:26 -04001320 }
1321
David Benjamine455e512016-08-01 20:11:13 -04001322 if (!ssl_verify_cert_chain(ssl, chain)) {
David Benjamin5c900c82016-07-13 23:03:26 -04001323 goto err;
David Benjamin9f1dc822016-06-07 17:03:46 -04001324 }
1325 }
1326
Steven Valdez87eab492016-06-27 16:34:59 -04001327 X509_free(ssl->s3->new_session->peer);
1328 ssl->s3->new_session->peer = sk_X509_shift(chain);
1329 ssl->s3->new_session->verify_result = ssl->verify_result;
David Benjamin9f1dc822016-06-07 17:03:46 -04001330
Steven Valdez87eab492016-06-27 16:34:59 -04001331 sk_X509_pop_free(ssl->s3->new_session->cert_chain, X509_free);
1332 ssl->s3->new_session->cert_chain = chain;
David Benjamin9f1dc822016-06-07 17:03:46 -04001333 /* Inconsistency alert: cert_chain does *not* include the peer's own
1334 * certificate, while we do include it in s3_clnt.c */
1335
David Benjamin5c900c82016-07-13 23:03:26 -04001336 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001337
1338err:
David Benjamin5c900c82016-07-13 23:03:26 -04001339 sk_X509_pop_free(chain, X509_free);
1340 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001341}
1342
David Benjamindf50eec2016-06-07 16:49:42 -04001343static int ssl3_get_client_key_exchange(SSL *ssl) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001344 int al;
Adam Langleyfcf25832014-12-18 17:42:32 -08001345 CBS client_key_exchange;
David Benjamin3fa27772015-04-17 22:32:19 -04001346 uint32_t alg_k;
1347 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001348 uint8_t *premaster_secret = NULL;
1349 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001350 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001351
David Benjamin4298d772015-12-19 00:18:25 -05001352 unsigned psk_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001353 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001354
David Benjamin1e6d6df2016-05-13 18:28:17 -04001355 if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin09eb6552016-07-08 14:32:11 -07001356 int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE,
1357 ssl_hash_message);
1358 if (ret <= 0) {
1359 return ret;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001360 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001361 }
Adam Langley95c29f32014-06-20 12:00:00 -07001362
David Benjamin0d56f882015-12-19 17:05:56 -05001363 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
1364 alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1365 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001366
Adam Langleyfcf25832014-12-18 17:42:32 -08001367 /* If using a PSK key exchange, prepare the pre-shared key. */
1368 if (alg_a & SSL_aPSK) {
1369 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001370
Adam Langleyfcf25832014-12-18 17:42:32 -08001371 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1372 * then this is the only field in the message. */
1373 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1374 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001375 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001376 al = SSL_AD_DECODE_ERROR;
1377 goto f_err;
1378 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001379
David Benjamin0d56f882015-12-19 17:05:56 -05001380 if (ssl->psk_server_callback == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001381 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001382 al = SSL_AD_INTERNAL_ERROR;
1383 goto f_err;
1384 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001385
Adam Langleyfcf25832014-12-18 17:42:32 -08001386 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1387 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001388 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001389 al = SSL_AD_ILLEGAL_PARAMETER;
1390 goto f_err;
1391 }
David Benjamin35c02302014-07-13 04:14:59 -04001392
Steven Valdez87eab492016-06-27 16:34:59 -04001393 if (!CBS_strdup(&psk_identity, &ssl->s3->new_session->psk_identity)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001394 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001395 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001396 goto f_err;
1397 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001398
Adam Langleyfcf25832014-12-18 17:42:32 -08001399 /* Look up the key for the identity. */
Steven Valdez87eab492016-06-27 16:34:59 -04001400 psk_len = ssl->psk_server_callback(ssl, ssl->s3->new_session->psk_identity,
1401 psk, sizeof(psk));
Adam Langleyfcf25832014-12-18 17:42:32 -08001402 if (psk_len > PSK_MAX_PSK_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04001403 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001404 al = SSL_AD_INTERNAL_ERROR;
1405 goto f_err;
1406 } else if (psk_len == 0) {
1407 /* PSK related to the given identity not found */
David Benjamin3570d732015-06-29 00:28:17 -04001408 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001409 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1410 goto f_err;
1411 }
1412 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001413
Adam Langleyfcf25832014-12-18 17:42:32 -08001414 /* Depending on the key exchange method, compute |premaster_secret| and
1415 * |premaster_secret_len|. */
1416 if (alg_k & SSL_kRSA) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001417 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamin0d56f882015-12-19 17:05:56 -05001418 const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001419 decrypt_buf = OPENSSL_malloc(rsa_size);
1420 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001421 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001422 goto err;
1423 }
David Benjamin35c02302014-07-13 04:14:59 -04001424
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001425 enum ssl_private_key_result_t decrypt_result;
David Benjamin3f5b43d2015-12-01 19:31:24 -05001426 size_t decrypt_len;
David Benjamin1e6d6df2016-05-13 18:28:17 -04001427 if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin0d56f882015-12-19 17:05:56 -05001428 if (!ssl_has_private_key(ssl) ||
David Benjamin0c0b7e12016-07-14 13:47:55 -04001429 ssl_private_key_type(ssl) != NID_rsaEncryption) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001430 al = SSL_AD_HANDSHAKE_FAILURE;
1431 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1432 goto f_err;
1433 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001434 CBS encrypted_premaster_secret;
David Benjamin0d56f882015-12-19 17:05:56 -05001435 if (ssl->version > SSL3_VERSION) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001436 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1437 &encrypted_premaster_secret) ||
1438 CBS_len(&client_key_exchange) != 0) {
David Benjaminef5e5152015-11-18 20:35:31 -05001439 al = SSL_AD_DECODE_ERROR;
1440 OPENSSL_PUT_ERROR(SSL,
1441 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1442 goto f_err;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001443 }
1444 } else {
1445 encrypted_premaster_secret = client_key_exchange;
1446 }
1447
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001448 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1449 * timing-sensitive code below. */
1450 decrypt_result = ssl_private_key_decrypt(
David Benjamin0d56f882015-12-19 17:05:56 -05001451 ssl, decrypt_buf, &decrypt_len, rsa_size,
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001452 CBS_data(&encrypted_premaster_secret),
1453 CBS_len(&encrypted_premaster_secret));
1454 } else {
David Benjamin1e6d6df2016-05-13 18:28:17 -04001455 assert(ssl->state == SSL3_ST_SR_KEY_EXCH_B);
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001456 /* Complete async decrypt. */
David Benjamind3440b42016-07-14 14:52:41 -04001457 decrypt_result =
1458 ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001459 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001460
1461 switch (decrypt_result) {
1462 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001463 break;
1464 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001465 goto err;
1466 case ssl_private_key_retry:
David Benjamin0d56f882015-12-19 17:05:56 -05001467 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamin1e6d6df2016-05-13 18:28:17 -04001468 ssl->state = SSL3_ST_SR_KEY_EXCH_B;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001469 goto err;
1470 }
1471
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001472 if (decrypt_len != rsa_size) {
1473 al = SSL_AD_DECRYPT_ERROR;
1474 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1475 goto f_err;
1476 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001477
David Benjamin3f5b43d2015-12-01 19:31:24 -05001478 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1479 * section 7.4.7.1. */
1480 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1481 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001482 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001483 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001484 goto err;
1485 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001486 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001487 goto err;
1488 }
1489
David Benjamin3f5b43d2015-12-01 19:31:24 -05001490 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1491 * publicly invalid. */
1492 if (decrypt_len < 11 + premaster_secret_len) {
1493 al = SSL_AD_DECRYPT_ERROR;
1494 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1495 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001496 }
David Benjamin35c02302014-07-13 04:14:59 -04001497
David Benjamin3f5b43d2015-12-01 19:31:24 -05001498 /* Check the padding. See RFC 3447, section 7.2.2. */
1499 size_t padding_len = decrypt_len - premaster_secret_len;
1500 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1501 constant_time_eq_int_8(decrypt_buf[1], 2);
1502 size_t i;
1503 for (i = 2; i < padding_len - 1; i++) {
1504 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1505 }
1506 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1507
1508 /* The premaster secret must begin with |client_version|. This too must be
1509 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1510 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjamin0d56f882015-12-19 17:05:56 -05001511 (unsigned)(ssl->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001512 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjamin0d56f882015-12-19 17:05:56 -05001513 (unsigned)(ssl->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001514
1515 /* Select, in constant time, either the decrypted premaster or the random
1516 * premaster based on |good|. */
1517 for (i = 0; i < premaster_secret_len; i++) {
1518 premaster_secret[i] = constant_time_select_8(
1519 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1520 }
1521
1522 OPENSSL_free(decrypt_buf);
1523 decrypt_buf = NULL;
Matt Braithwaite053931e2016-05-25 12:06:05 -07001524 } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001525 /* Parse the ClientKeyExchange. */
David Benjamin974c7ba2015-12-19 16:58:04 -05001526 CBS peer_key;
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001527 if (!SSL_ECDH_CTX_get_key(&ssl->s3->tmp.ecdh_ctx, &client_key_exchange,
1528 &peer_key) ||
1529 CBS_len(&client_key_exchange) != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001530 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001531 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001532 goto f_err;
1533 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001534
David Benjamin4298d772015-12-19 00:18:25 -05001535 /* Compute the premaster. */
1536 uint8_t alert;
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001537 if (!SSL_ECDH_CTX_finish(&ssl->s3->tmp.ecdh_ctx, &premaster_secret,
1538 &premaster_secret_len, &alert, CBS_data(&peer_key),
1539 CBS_len(&peer_key))) {
David Benjamin4298d772015-12-19 00:18:25 -05001540 al = alert;
1541 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001542 }
Adam Langley95c29f32014-06-20 12:00:00 -07001543
David Benjamin4298d772015-12-19 00:18:25 -05001544 /* The key exchange state may now be discarded. */
David Benjamin0d56f882015-12-19 17:05:56 -05001545 SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001546 } else if (alg_k & SSL_kPSK) {
1547 /* For plain PSK, other_secret is a block of 0s with the same length as the
1548 * pre-shared key. */
1549 premaster_secret_len = psk_len;
1550 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1551 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001552 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001553 goto err;
1554 }
1555 memset(premaster_secret, 0, premaster_secret_len);
1556 } else {
1557 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001558 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001559 goto f_err;
1560 }
Adam Langley95c29f32014-06-20 12:00:00 -07001561
Adam Langleyfcf25832014-12-18 17:42:32 -08001562 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1563 * pre-shared key. */
1564 if (alg_a & SSL_aPSK) {
1565 CBB new_premaster, child;
1566 uint8_t *new_data;
1567 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04001568
David Benjamina8653202015-06-28 01:26:10 -04001569 CBB_zero(&new_premaster);
1570 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1571 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001572 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1573 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1574 !CBB_add_bytes(&child, psk, psk_len) ||
1575 !CBB_finish(&new_premaster, &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001576 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001577 CBB_cleanup(&new_premaster);
1578 goto err;
1579 }
David Benjamin14c83e72014-07-13 04:54:57 -04001580
Adam Langleyfcf25832014-12-18 17:42:32 -08001581 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1582 OPENSSL_free(premaster_secret);
1583 premaster_secret = new_data;
1584 premaster_secret_len = new_len;
1585 }
David Benjamin14c83e72014-07-13 04:54:57 -04001586
Adam Langleyfcf25832014-12-18 17:42:32 -08001587 /* Compute the master secret */
Steven Valdez87eab492016-06-27 16:34:59 -04001588 ssl->s3->new_session->master_key_length = tls1_generate_master_secret(
1589 ssl, ssl->s3->new_session->master_key, premaster_secret,
1590 premaster_secret_len);
1591 if (ssl->s3->new_session->master_key_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001592 goto err;
1593 }
Steven Valdez87eab492016-06-27 16:34:59 -04001594 ssl->s3->new_session->extended_master_secret =
1595 ssl->s3->tmp.extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001596
Adam Langleyfcf25832014-12-18 17:42:32 -08001597 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1598 OPENSSL_free(premaster_secret);
1599 return 1;
1600
Adam Langley95c29f32014-06-20 12:00:00 -07001601f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001602 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001603err:
David Benjamin4298d772015-12-19 00:18:25 -05001604 if (premaster_secret != NULL) {
1605 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001606 OPENSSL_free(premaster_secret);
1607 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001608 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001609
Adam Langleyfcf25832014-12-18 17:42:32 -08001610 return -1;
1611}
Adam Langley95c29f32014-06-20 12:00:00 -07001612
David Benjamindf50eec2016-06-07 16:49:42 -04001613static int ssl3_get_cert_verify(SSL *ssl) {
David Benjamin09eb6552016-07-08 14:32:11 -07001614 int al, ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001615 CBS certificate_verify, signature;
Steven Valdez87eab492016-06-27 16:34:59 -04001616 X509 *peer = ssl->s3->new_session->peer;
Adam Langleyfcf25832014-12-18 17:42:32 -08001617 EVP_PKEY *pkey = NULL;
David Benjamin6553b372014-07-22 14:11:30 -04001618
Adam Langleyfcf25832014-12-18 17:42:32 -08001619 /* Only RSA and ECDSA client certificates are supported, so a
1620 * CertificateVerify is required if and only if there's a client certificate.
1621 * */
1622 if (peer == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05001623 ssl3_free_handshake_buffer(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001624 return 1;
1625 }
Adam Langley95c29f32014-06-20 12:00:00 -07001626
David Benjamin09eb6552016-07-08 14:32:11 -07001627 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE_VERIFY,
1628 ssl_dont_hash_message);
1629 if (msg_ret <= 0) {
1630 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001631 }
David Benjaminef865502014-08-24 02:48:34 -04001632
Adam Langleyfcf25832014-12-18 17:42:32 -08001633 /* Filter out unsupported certificate types. */
1634 pkey = X509_get_pubkey(peer);
David Benjamin9d0847a2015-02-16 03:57:55 -05001635 if (pkey == NULL) {
1636 goto err;
1637 }
David Benjamin6897dbe2014-07-12 20:18:28 -04001638
David Benjamin09eb6552016-07-08 14:32:11 -07001639 CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
David Benjamin6897dbe2014-07-12 20:18:28 -04001640
Adam Langleyfcf25832014-12-18 17:42:32 -08001641 /* Determine the digest type if needbe. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04001642 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001643 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001644 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001645 al = SSL_AD_DECODE_ERROR;
1646 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1647 goto f_err;
1648 }
David Benjamin887c3002016-07-08 16:15:32 -07001649 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001650 goto f_err;
1651 }
Steven Valdez02563852016-06-23 13:33:05 -04001652 ssl->s3->tmp.peer_signature_algorithm = signature_algorithm;
Steven Valdezf0451ca2016-06-29 13:16:27 -04001653 } else if (pkey->type == EVP_PKEY_RSA) {
1654 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
1655 } else if (pkey->type == EVP_PKEY_EC) {
1656 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
David Benjamin49ec9bb2016-07-14 00:11:26 -04001657 } else {
1658 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1659 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1660 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001661 }
David Benjamin854dd652014-08-26 00:32:30 -04001662
Adam Langleyfcf25832014-12-18 17:42:32 -08001663 /* Parse and verify the signature. */
1664 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1665 CBS_len(&certificate_verify) != 0) {
1666 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001667 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001668 goto f_err;
1669 }
Adam Langley95c29f32014-06-20 12:00:00 -07001670
Steven Valdez2b8415e2016-06-30 13:27:23 -04001671 int sig_ok;
1672 /* The SSL3 construction for CertificateVerify does not decompose into a
1673 * single final digest and signature, and must be special-cased. */
1674 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
David Benjamin0aa25bd2016-07-08 14:44:56 -07001675 const EVP_MD *md;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001676 uint8_t digest[EVP_MAX_MD_SIZE];
1677 size_t digest_len;
David Benjamin0aa25bd2016-07-08 14:44:56 -07001678 if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001679 signature_algorithm)) {
1680 goto err;
1681 }
1682
Steven Valdez2b8415e2016-06-30 13:27:23 -04001683 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
1684 sig_ok = pctx != NULL &&
1685 EVP_PKEY_verify_init(pctx) &&
1686 EVP_PKEY_CTX_set_signature_md(pctx, md) &&
1687 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1688 digest, digest_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001689 EVP_PKEY_CTX_free(pctx);
1690 } else {
1691 sig_ok = ssl_public_key_verify(
1692 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
1693 pkey, (const uint8_t *)ssl->s3->handshake_buffer->data,
1694 ssl->s3->handshake_buffer->length);
Adam Langleyfcf25832014-12-18 17:42:32 -08001695 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04001696
David Benjaminbf82aed2016-03-01 22:57:40 -05001697#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1698 sig_ok = 1;
1699 ERR_clear_error();
1700#endif
1701 if (!sig_ok) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001702 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001703 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001704 goto f_err;
1705 }
1706
Steven Valdez2b8415e2016-06-30 13:27:23 -04001707 /* The handshake buffer is no longer necessary, and we may hash the current
1708 * message.*/
1709 ssl3_free_handshake_buffer(ssl);
David Benjamin528bd262016-07-08 09:34:05 -07001710 if (!ssl->method->hash_current_message(ssl)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001711 goto err;
1712 }
1713
Adam Langleyfcf25832014-12-18 17:42:32 -08001714 ret = 1;
1715
1716 if (0) {
1717 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001718 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08001719 }
1720
David Benjamin854dd652014-08-26 00:32:30 -04001721err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001722 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001723
Adam Langleyfcf25832014-12-18 17:42:32 -08001724 return ret;
1725}
Adam Langley95c29f32014-06-20 12:00:00 -07001726
Adam Langley95c29f32014-06-20 12:00:00 -07001727/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1728 * sets the next_proto member in s if found */
David Benjamindf50eec2016-06-07 16:49:42 -04001729static int ssl3_get_next_proto(SSL *ssl) {
David Benjamin09eb6552016-07-08 14:32:11 -07001730 int ret =
1731 ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message);
1732 if (ret <= 0) {
1733 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001734 }
Adam Langley95c29f32014-06-20 12:00:00 -07001735
David Benjamin09eb6552016-07-08 14:32:11 -07001736 CBS next_protocol, selected_protocol, padding;
1737 CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
Adam Langleyfcf25832014-12-18 17:42:32 -08001738 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1739 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
1740 CBS_len(&next_protocol) != 0 ||
David Benjamin79978df2015-12-25 15:56:49 -05001741 !CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
1742 &ssl->s3->next_proto_negotiated_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001743 return 0;
1744 }
Adam Langley95c29f32014-06-20 12:00:00 -07001745
Adam Langleyfcf25832014-12-18 17:42:32 -08001746 return 1;
1747}
Adam Langley95c29f32014-06-20 12:00:00 -07001748
Adam Langley1258b6a2014-06-20 12:00:00 -07001749/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
David Benjamindf50eec2016-06-07 16:49:42 -04001750static int ssl3_get_channel_id(SSL *ssl) {
David Benjamin09eb6552016-07-08 14:32:11 -07001751 int ret = -1;
David Benjamind6a4ae92015-08-06 11:10:51 -04001752 uint8_t channel_id_hash[EVP_MAX_MD_SIZE];
1753 size_t channel_id_hash_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08001754 const uint8_t *p;
Adam Langley49c7af12015-07-10 14:33:46 -07001755 uint16_t extension_type;
Adam Langleyfcf25832014-12-18 17:42:32 -08001756 EC_GROUP *p256 = NULL;
1757 EC_KEY *key = NULL;
1758 EC_POINT *point = NULL;
1759 ECDSA_SIG sig;
1760 BIGNUM x, y;
1761 CBS encrypted_extensions, extension;
Adam Langley1258b6a2014-06-20 12:00:00 -07001762
David Benjamin09eb6552016-07-08 14:32:11 -07001763 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID,
1764 ssl_dont_hash_message);
1765 if (msg_ret <= 0) {
1766 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001767 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001768
Adam Langleyfcf25832014-12-18 17:42:32 -08001769 /* Before incorporating the EncryptedExtensions message to the handshake
1770 * hash, compute the hash that should have been signed. */
David Benjamin0d56f882015-12-19 17:05:56 -05001771 if (!tls1_channel_id_hash(ssl, channel_id_hash, &channel_id_hash_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001772 return -1;
1773 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001774 assert(channel_id_hash_len == SHA256_DIGEST_LENGTH);
David Benjamin880b14e2014-08-25 22:35:07 -04001775
David Benjamin528bd262016-07-08 09:34:05 -07001776 if (!ssl->method->hash_current_message(ssl)) {
David Benjaminfbdfefb2015-02-16 19:33:53 -05001777 return -1;
1778 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001779
David Benjamin09eb6552016-07-08 14:32:11 -07001780 CBS_init(&encrypted_extensions, ssl->init_msg, ssl->init_num);
Adam Langley1258b6a2014-06-20 12:00:00 -07001781
Adam Langleyfcf25832014-12-18 17:42:32 -08001782 /* EncryptedExtensions could include multiple extensions, but the only
David Benjamin8e710ca2016-06-13 14:09:37 -04001783 * extension that could be negotiated is Channel ID, so there can only be one
1784 * entry. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001785 if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
1786 !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
1787 CBS_len(&encrypted_extensions) != 0 ||
Adam Langley49c7af12015-07-10 14:33:46 -07001788 extension_type != TLSEXT_TYPE_channel_id ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001789 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
David Benjamin3570d732015-06-29 00:28:17 -04001790 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_MESSAGE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001791 return -1;
1792 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001793
Adam Langleyfcf25832014-12-18 17:42:32 -08001794 p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
1795 if (!p256) {
David Benjamin3570d732015-06-29 00:28:17 -04001796 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
Adam Langleyfcf25832014-12-18 17:42:32 -08001797 return -1;
1798 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001799
Adam Langleyfcf25832014-12-18 17:42:32 -08001800 BN_init(&x);
1801 BN_init(&y);
1802 sig.r = BN_new();
1803 sig.s = BN_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001804 if (sig.r == NULL || sig.s == NULL) {
1805 goto err;
1806 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001807
Adam Langleyfcf25832014-12-18 17:42:32 -08001808 p = CBS_data(&extension);
1809 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
1810 BN_bin2bn(p + 32, 32, &y) == NULL ||
1811 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
1812 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
1813 goto err;
1814 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001815
Adam Langleyfcf25832014-12-18 17:42:32 -08001816 point = EC_POINT_new(p256);
David Benjamin0d56f882015-12-19 17:05:56 -05001817 if (!point ||
1818 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001819 goto err;
1820 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001821
Adam Langleyfcf25832014-12-18 17:42:32 -08001822 key = EC_KEY_new();
1823 if (!key || !EC_KEY_set_group(key, p256) ||
1824 !EC_KEY_set_public_key(key, point)) {
1825 goto err;
1826 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001827
Adam Langleyfcf25832014-12-18 17:42:32 -08001828 /* We stored the handshake hash in |tlsext_channel_id| the first time that we
1829 * were called. */
1830 if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) {
David Benjamin3570d732015-06-29 00:28:17 -04001831 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
David Benjamin0d56f882015-12-19 17:05:56 -05001832 ssl->s3->tlsext_channel_id_valid = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001833 goto err;
1834 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001835
David Benjamin0d56f882015-12-19 17:05:56 -05001836 memcpy(ssl->s3->tlsext_channel_id, p, 64);
Adam Langleyfcf25832014-12-18 17:42:32 -08001837 ret = 1;
Adam Langley1258b6a2014-06-20 12:00:00 -07001838
1839err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001840 BN_free(&x);
1841 BN_free(&y);
1842 BN_free(sig.r);
1843 BN_free(sig.s);
David Benjamin2755a3e2015-04-22 16:17:58 -04001844 EC_KEY_free(key);
1845 EC_POINT_free(point);
1846 EC_GROUP_free(p256);
Adam Langleyfcf25832014-12-18 17:42:32 -08001847 return ret;
1848}
David Benjamin9f1dc822016-06-07 17:03:46 -04001849
David Benjamin9f1dc822016-06-07 17:03:46 -04001850static int ssl3_send_new_session_ticket(SSL *ssl) {
David Benjamin75836432016-06-17 18:48:29 -04001851 if (ssl->state == SSL3_ST_SW_SESSION_TICKET_B) {
1852 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001853 }
1854
David Benjamin75836432016-06-17 18:48:29 -04001855 CBB cbb, body, ticket;
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001856 if (!ssl->method->init_message(ssl, &cbb, &body,
1857 SSL3_MT_NEW_SESSION_TICKET) ||
David Benjamin75836432016-06-17 18:48:29 -04001858 /* Ticket lifetime hint (advisory only): We leave this unspecified for
1859 * resumed session (for simplicity), and guess that tickets for new
1860 * sessions will live as long as their sessions. */
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001861 !CBB_add_u32(&body,
1862 ssl->session != NULL ? 0 : ssl->s3->new_session->timeout) ||
1863 !CBB_add_u16_length_prefixed(&body, &ticket) ||
1864 !ssl_encrypt_ticket(ssl, &ticket, ssl->session != NULL
1865 ? ssl->session
1866 : ssl->s3->new_session) ||
David Benjamin75836432016-06-17 18:48:29 -04001867 !ssl->method->finish_message(ssl, &cbb)) {
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001868 return 0;
David Benjamin75836432016-06-17 18:48:29 -04001869 }
1870
1871 ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001872 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001873}