blob: 82e4e3512e2cb09f661fa20e7e797de003089552 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 *
113 * Portions of the attached software ("Contribution") are developed by
114 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115 *
116 * The Contribution is licensed pursuant to the OpenSSL open source
117 * license provided above.
118 *
119 * ECC cipher suite support in OpenSSL originally written by
120 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121 *
122 */
123/* ====================================================================
124 * Copyright 2005 Nokia. All rights reserved.
125 *
126 * The portions of the attached software ("Contribution") is developed by
127 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128 * license.
129 *
130 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132 * support (see RFC 4279) to OpenSSL.
133 *
134 * No patent licenses or other rights except those expressly stated in
135 * the OpenSSL open source license shall be deemed granted or received
136 * expressly, by implication, estoppel, or otherwise.
137 *
138 * No assurances are provided by Nokia that the Contribution does not
139 * infringe the patent or other intellectual property rights of any third
140 * party or that the license provides you with all the necessary rights
141 * to make use of the Contribution.
142 *
143 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147 * OTHERWISE.
148 */
149
David Benjamin9e4e01e2015-09-15 01:48:04 -0400150#include <openssl/ssl.h>
151
David Benjamin81ea0bf2014-11-23 04:20:17 -0500152#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400153#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700154
David Benjamin961ad6a2015-06-12 01:40:23 -0400155#include <openssl/bn.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700156#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400157#include <openssl/bytestring.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400158#include <openssl/dh.h>
159#include <openssl/ec_key.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>
Adam Langley95c29f32014-06-20 12:00:00 -0700163#include <openssl/md5.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400164#include <openssl/mem.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400165#include <openssl/rand.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700166#include <openssl/x509.h>
David Benjamin436bf822015-06-07 19:50:37 -0400167#include <openssl/x509v3.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700168
David Benjamin2ee94aa2015-04-07 22:38:30 -0400169#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700170#include "../crypto/dh/internal.h"
171
Adam Langley95c29f32014-06-20 12:00:00 -0700172
David Benjamin0d56f882015-12-19 17:05:56 -0500173int ssl3_connect(SSL *ssl) {
Adam Langley1bea1732014-12-17 19:06:57 -0800174 BUF_MEM *buf = NULL;
David Benjamin82170242015-10-17 22:51:17 -0400175 void (*cb)(const SSL *ssl, int type, int value) = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -0800176 int ret = -1;
177 int new_state, state, skip = 0;
David Benjaminbeb47022014-11-30 02:58:52 -0500178
David Benjamin0d56f882015-12-19 17:05:56 -0500179 assert(ssl->handshake_func == ssl3_connect);
180 assert(!ssl->server);
181 assert(!SSL_IS_DTLS(ssl));
Adam Langley95c29f32014-06-20 12:00:00 -0700182
Adam Langley1bea1732014-12-17 19:06:57 -0800183 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700184
David Benjamin0d56f882015-12-19 17:05:56 -0500185 if (ssl->info_callback != NULL) {
186 cb = ssl->info_callback;
187 } else if (ssl->ctx->info_callback != NULL) {
188 cb = ssl->ctx->info_callback;
Adam Langley1bea1732014-12-17 19:06:57 -0800189 }
Adam Langley95c29f32014-06-20 12:00:00 -0700190
Adam Langley1bea1732014-12-17 19:06:57 -0800191 for (;;) {
David Benjamin0d56f882015-12-19 17:05:56 -0500192 state = ssl->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700193
David Benjamin0d56f882015-12-19 17:05:56 -0500194 switch (ssl->state) {
Adam Langley1bea1732014-12-17 19:06:57 -0800195 case SSL_ST_CONNECT:
David Benjamin6eb000d2015-02-11 01:17:41 -0500196 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500197 cb(ssl, SSL_CB_HANDSHAKE_START, 1);
David Benjamin6eb000d2015-02-11 01:17:41 -0500198 }
Adam Langley95c29f32014-06-20 12:00:00 -0700199
David Benjamin0d56f882015-12-19 17:05:56 -0500200 if (ssl->init_buf == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -0800201 buf = BUF_MEM_new();
202 if (buf == NULL ||
David Benjamina6338be2016-05-13 18:12:19 -0400203 !BUF_MEM_reserve(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800204 ret = -1;
205 goto end;
206 }
Adam Langley95c29f32014-06-20 12:00:00 -0700207
David Benjamin0d56f882015-12-19 17:05:56 -0500208 ssl->init_buf = buf;
Adam Langley1bea1732014-12-17 19:06:57 -0800209 buf = NULL;
210 }
David Benjamin1d64afd2016-05-15 13:46:07 -0400211 ssl->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700212
David Benjaminb095f0f2016-05-05 21:50:24 -0400213 if (!ssl_init_wbio_buffer(ssl)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800214 ret = -1;
215 goto end;
216 }
Adam Langley95c29f32014-06-20 12:00:00 -0700217
David Benjamin0d56f882015-12-19 17:05:56 -0500218 if (!ssl3_init_handshake_buffer(ssl)) {
David Benjamin3570d732015-06-29 00:28:17 -0400219 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800220 ret = -1;
221 goto end;
222 }
Adam Langley95c29f32014-06-20 12:00:00 -0700223
David Benjamin0d56f882015-12-19 17:05:56 -0500224 ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800225 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700226
Adam Langley1bea1732014-12-17 19:06:57 -0800227 case SSL3_ST_CW_CLNT_HELLO_A:
228 case SSL3_ST_CW_CLNT_HELLO_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500229 ret = ssl3_send_client_hello(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800230 if (ret <= 0) {
231 goto end;
232 }
David Benjaminb095f0f2016-05-05 21:50:24 -0400233 ssl->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
234 ssl->state = SSL3_ST_CW_FLUSH;
Adam Langley1bea1732014-12-17 19:06:57 -0800235 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700236
Adam Langley1bea1732014-12-17 19:06:57 -0800237 case SSL3_ST_CR_SRVR_HELLO_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500238 ret = ssl3_get_server_hello(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800239 if (ret <= 0) {
240 goto end;
241 }
Adam Langley95c29f32014-06-20 12:00:00 -0700242
David Benjamin0d56f882015-12-19 17:05:56 -0500243 if (ssl->hit) {
244 ssl->state = SSL3_ST_CR_CHANGE;
245 if (ssl->tlsext_ticket_expected) {
Adam Langley1bea1732014-12-17 19:06:57 -0800246 /* receive renewed session ticket */
David Benjamin0d56f882015-12-19 17:05:56 -0500247 ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800248 }
249 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500250 ssl->state = SSL3_ST_CR_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800251 }
Adam Langley1bea1732014-12-17 19:06:57 -0800252 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400253
Adam Langley1bea1732014-12-17 19:06:57 -0800254 case SSL3_ST_CR_CERT_A:
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_get_server_certificate(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800257 if (ret <= 0) {
258 goto end;
259 }
David Benjamin0d56f882015-12-19 17:05:56 -0500260 if (ssl->s3->tmp.certificate_status_expected) {
261 ssl->state = SSL3_ST_CR_CERT_STATUS_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800262 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500263 ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
Adam Langley1bea1732014-12-17 19:06:57 -0800264 }
265 } else {
266 skip = 1;
David Benjamin0d56f882015-12-19 17:05:56 -0500267 ssl->state = SSL3_ST_CR_KEY_EXCH_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800268 }
Adam Langley1bea1732014-12-17 19:06:57 -0800269 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700270
Paul Lietar8f1c2682015-08-18 12:21:54 +0100271 case SSL3_ST_VERIFY_SERVER_CERT:
David Benjamin0d56f882015-12-19 17:05:56 -0500272 ret = ssl3_verify_server_cert(ssl);
Paul Lietar8f1c2682015-08-18 12:21:54 +0100273 if (ret <= 0) {
274 goto end;
275 }
276
David Benjamin0d56f882015-12-19 17:05:56 -0500277 ssl->state = SSL3_ST_CR_KEY_EXCH_A;
Paul Lietar8f1c2682015-08-18 12:21:54 +0100278 break;
279
Adam Langley1bea1732014-12-17 19:06:57 -0800280 case SSL3_ST_CR_KEY_EXCH_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500281 ret = ssl3_get_server_key_exchange(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800282 if (ret <= 0) {
283 goto end;
284 }
David Benjaminc032dfa2016-05-12 14:54:57 -0400285 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
286 ssl->state = SSL3_ST_CR_CERT_REQ_A;
287 } else {
288 ssl->state = SSL3_ST_CR_SRVR_DONE_A;
289 }
Adam Langley1bea1732014-12-17 19:06:57 -0800290 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700291
Adam Langley1bea1732014-12-17 19:06:57 -0800292 case SSL3_ST_CR_CERT_REQ_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500293 ret = ssl3_get_certificate_request(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800294 if (ret <= 0) {
295 goto end;
296 }
David Benjamin0d56f882015-12-19 17:05:56 -0500297 ssl->state = SSL3_ST_CR_SRVR_DONE_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800298 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700299
Adam Langley1bea1732014-12-17 19:06:57 -0800300 case SSL3_ST_CR_SRVR_DONE_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500301 ret = ssl3_get_server_done(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800302 if (ret <= 0) {
303 goto end;
304 }
David Benjamin0d56f882015-12-19 17:05:56 -0500305 if (ssl->s3->tmp.cert_req) {
306 ssl->state = SSL3_ST_CW_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800307 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500308 ssl->state = SSL3_ST_CW_KEY_EXCH_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800309 }
Adam Langley95c29f32014-06-20 12:00:00 -0700310
Adam Langley1bea1732014-12-17 19:06:57 -0800311 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700312
Adam Langley1bea1732014-12-17 19:06:57 -0800313 case SSL3_ST_CW_CERT_A:
314 case SSL3_ST_CW_CERT_B:
315 case SSL3_ST_CW_CERT_C:
316 case SSL3_ST_CW_CERT_D:
David Benjamin0d56f882015-12-19 17:05:56 -0500317 ret = ssl3_send_client_certificate(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800318 if (ret <= 0) {
319 goto end;
320 }
David Benjamin0d56f882015-12-19 17:05:56 -0500321 ssl->state = SSL3_ST_CW_KEY_EXCH_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800322 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700323
Adam Langley1bea1732014-12-17 19:06:57 -0800324 case SSL3_ST_CW_KEY_EXCH_A:
325 case SSL3_ST_CW_KEY_EXCH_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500326 ret = ssl3_send_client_key_exchange(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800327 if (ret <= 0) {
328 goto end;
329 }
330 /* For TLS, cert_req is set to 2, so a cert chain
331 * of nothing is sent, but no verify packet is sent */
David Benjamin0d56f882015-12-19 17:05:56 -0500332 if (ssl->s3->tmp.cert_req == 1) {
333 ssl->state = SSL3_ST_CW_CERT_VRFY_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800334 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500335 ssl->state = SSL3_ST_CW_CHANGE_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800336 }
Adam Langley1bea1732014-12-17 19:06:57 -0800337 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700338
Adam Langley1bea1732014-12-17 19:06:57 -0800339 case SSL3_ST_CW_CERT_VRFY_A:
340 case SSL3_ST_CW_CERT_VRFY_B:
David Benjaminb4d65fd2015-05-29 17:11:21 -0400341 case SSL3_ST_CW_CERT_VRFY_C:
David Benjamin0d56f882015-12-19 17:05:56 -0500342 ret = ssl3_send_cert_verify(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800343 if (ret <= 0) {
344 goto end;
345 }
David Benjamin0d56f882015-12-19 17:05:56 -0500346 ssl->state = SSL3_ST_CW_CHANGE_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800347 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700348
Adam Langley1bea1732014-12-17 19:06:57 -0800349 case SSL3_ST_CW_CHANGE_A:
350 case SSL3_ST_CW_CHANGE_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500351 ret = ssl3_send_change_cipher_spec(ssl, SSL3_ST_CW_CHANGE_A,
Adam Langley1bea1732014-12-17 19:06:57 -0800352 SSL3_ST_CW_CHANGE_B);
353 if (ret <= 0) {
354 goto end;
355 }
Adam Langley95c29f32014-06-20 12:00:00 -0700356
David Benjamin0d56f882015-12-19 17:05:56 -0500357 ssl->state = SSL3_ST_CW_FINISHED_A;
358 if (ssl->s3->tlsext_channel_id_valid) {
359 ssl->state = SSL3_ST_CW_CHANNEL_ID_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800360 }
David Benjamin0d56f882015-12-19 17:05:56 -0500361 if (ssl->s3->next_proto_neg_seen) {
362 ssl->state = SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800363 }
Adam Langley95c29f32014-06-20 12:00:00 -0700364
David Benjamin57997da2015-12-25 15:48:39 -0500365 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800366 ret = -1;
367 goto end;
368 }
Adam Langley95c29f32014-06-20 12:00:00 -0700369
Adam Langley1bea1732014-12-17 19:06:57 -0800370 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700371
Adam Langley1bea1732014-12-17 19:06:57 -0800372 case SSL3_ST_CW_NEXT_PROTO_A:
373 case SSL3_ST_CW_NEXT_PROTO_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500374 ret = ssl3_send_next_proto(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800375 if (ret <= 0) {
376 goto end;
377 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700378
David Benjamin0d56f882015-12-19 17:05:56 -0500379 if (ssl->s3->tlsext_channel_id_valid) {
380 ssl->state = SSL3_ST_CW_CHANNEL_ID_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800381 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500382 ssl->state = SSL3_ST_CW_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800383 }
384 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Adam Langley1bea1732014-12-17 19:06:57 -0800386 case SSL3_ST_CW_CHANNEL_ID_A:
387 case SSL3_ST_CW_CHANNEL_ID_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500388 ret = ssl3_send_channel_id(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800389 if (ret <= 0) {
390 goto end;
391 }
David Benjamin0d56f882015-12-19 17:05:56 -0500392 ssl->state = SSL3_ST_CW_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800393 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700394
Adam Langley1bea1732014-12-17 19:06:57 -0800395 case SSL3_ST_CW_FINISHED_A:
396 case SSL3_ST_CW_FINISHED_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500397 ret = ssl3_send_finished(ssl, SSL3_ST_CW_FINISHED_A,
David Benjaminbaa12162015-12-29 19:13:58 -0500398 SSL3_ST_CW_FINISHED_B);
Adam Langley1bea1732014-12-17 19:06:57 -0800399 if (ret <= 0) {
400 goto end;
401 }
David Benjamin0d56f882015-12-19 17:05:56 -0500402 ssl->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700403
David Benjamin0d56f882015-12-19 17:05:56 -0500404 if (ssl->hit) {
405 ssl->s3->tmp.next_state = SSL_ST_OK;
Adam Langley1bea1732014-12-17 19:06:57 -0800406 } else {
407 /* This is a non-resumption handshake. If it involves ChannelID, then
408 * record the handshake hashes at this point in the session so that
409 * any resumption of this session with ChannelID can sign those
410 * hashes. */
David Benjamin0d56f882015-12-19 17:05:56 -0500411 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Adam Langley49c7af12015-07-10 14:33:46 -0700412 if (ret <= 0) {
413 goto end;
Adam Langley1bea1732014-12-17 19:06:57 -0800414 }
David Benjamin0d56f882015-12-19 17:05:56 -0500415 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
416 ssl3_can_false_start(ssl) &&
David Benjamined7c4752015-02-16 19:16:46 -0500417 /* No False Start on renegotiation (would complicate the state
418 * machine). */
David Benjamin0d56f882015-12-19 17:05:56 -0500419 !ssl->s3->initial_handshake_complete) {
420 ssl->s3->tmp.next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800421 } else {
422 /* Allow NewSessionTicket if ticket expected */
David Benjamin0d56f882015-12-19 17:05:56 -0500423 if (ssl->tlsext_ticket_expected) {
424 ssl->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800425 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500426 ssl->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800427 }
428 }
429 }
Adam Langley1bea1732014-12-17 19:06:57 -0800430 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700431
Adam Langley1bea1732014-12-17 19:06:57 -0800432 case SSL3_ST_CR_SESSION_TICKET_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500433 ret = ssl3_get_new_session_ticket(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800434 if (ret <= 0) {
435 goto end;
436 }
David Benjamin0d56f882015-12-19 17:05:56 -0500437 ssl->state = SSL3_ST_CR_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800438 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700439
Adam Langley1bea1732014-12-17 19:06:57 -0800440 case SSL3_ST_CR_CERT_STATUS_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500441 ret = ssl3_get_cert_status(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800442 if (ret <= 0) {
443 goto end;
444 }
David Benjamin0d56f882015-12-19 17:05:56 -0500445 ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
Adam Langley1bea1732014-12-17 19:06:57 -0800446 break;
David Benjamincb5abad2014-07-25 12:14:28 -0400447
Adam Langley1bea1732014-12-17 19:06:57 -0800448 case SSL3_ST_CR_CHANGE:
David Benjamin0d56f882015-12-19 17:05:56 -0500449 ret = ssl->method->ssl_read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500450 if (ret <= 0) {
451 goto end;
452 }
453
David Benjamin57997da2015-12-25 15:48:39 -0500454 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800455 ret = -1;
456 goto end;
457 }
David Benjamin0d56f882015-12-19 17:05:56 -0500458 ssl->state = SSL3_ST_CR_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800459 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700460
Adam Langley1bea1732014-12-17 19:06:57 -0800461 case SSL3_ST_CR_FINISHED_A:
David Benjamin1e6d6df2016-05-13 18:28:17 -0400462 ret = ssl3_get_finished(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800463 if (ret <= 0) {
464 goto end;
465 }
Adam Langley95c29f32014-06-20 12:00:00 -0700466
David Benjamin0d56f882015-12-19 17:05:56 -0500467 if (ssl->hit) {
468 ssl->state = SSL3_ST_CW_CHANGE_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800469 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500470 ssl->state = SSL_ST_OK;
Adam Langley1bea1732014-12-17 19:06:57 -0800471 }
Adam Langley1bea1732014-12-17 19:06:57 -0800472 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700473
Adam Langley1bea1732014-12-17 19:06:57 -0800474 case SSL3_ST_CW_FLUSH:
David Benjamin0d56f882015-12-19 17:05:56 -0500475 if (BIO_flush(ssl->wbio) <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500476 ssl->rwstate = SSL_WRITING;
Adam Langley1bea1732014-12-17 19:06:57 -0800477 ret = -1;
478 goto end;
479 }
David Benjamin0d56f882015-12-19 17:05:56 -0500480 ssl->state = ssl->s3->tmp.next_state;
Adam Langley1bea1732014-12-17 19:06:57 -0800481 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700482
David Benjamined7c4752015-02-16 19:16:46 -0500483 case SSL3_ST_FALSE_START:
Adam Langley1bea1732014-12-17 19:06:57 -0800484 /* Allow NewSessionTicket if ticket expected */
David Benjamin0d56f882015-12-19 17:05:56 -0500485 if (ssl->tlsext_ticket_expected) {
486 ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800487 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500488 ssl->state = SSL3_ST_CR_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800489 }
David Benjamin0d56f882015-12-19 17:05:56 -0500490 ssl->s3->tmp.in_false_start = 1;
Adam Langleyadb739e2014-06-20 12:00:00 -0700491
David Benjamin0d56f882015-12-19 17:05:56 -0500492 ssl_free_wbio_buffer(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800493 ret = 1;
494 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700495
Adam Langley1bea1732014-12-17 19:06:57 -0800496 case SSL_ST_OK:
497 /* clean a few things up */
David Benjamin0d56f882015-12-19 17:05:56 -0500498 ssl3_cleanup_key_block(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700499
David Benjamin0d56f882015-12-19 17:05:56 -0500500 BUF_MEM_free(ssl->init_buf);
501 ssl->init_buf = NULL;
David Benjamin1d64afd2016-05-15 13:46:07 -0400502 ssl->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700503
Adam Langley1bea1732014-12-17 19:06:57 -0800504 /* Remove write buffering now. */
David Benjamin0d56f882015-12-19 17:05:56 -0500505 ssl_free_wbio_buffer(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700506
David Benjamin0d56f882015-12-19 17:05:56 -0500507 const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
David Benjaminba4594a2015-06-18 18:36:15 -0400508
David Benjamin0d56f882015-12-19 17:05:56 -0500509 ssl->s3->tmp.in_false_start = 0;
510 ssl->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700511
David Benjaminba4594a2015-06-18 18:36:15 -0400512 if (is_initial_handshake) {
513 /* Renegotiations do not participate in session resumption. */
David Benjamin0d56f882015-12-19 17:05:56 -0500514 ssl_update_cache(ssl, SSL_SESS_CACHE_CLIENT);
David Benjaminba4594a2015-06-18 18:36:15 -0400515 }
Adam Langley95c29f32014-06-20 12:00:00 -0700516
Adam Langley1bea1732014-12-17 19:06:57 -0800517 ret = 1;
David Benjamin0d56f882015-12-19 17:05:56 -0500518 /* ssl->server=0; */
Adam Langley95c29f32014-06-20 12:00:00 -0700519
Adam Langley1bea1732014-12-17 19:06:57 -0800520 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500521 cb(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800522 }
Adam Langley95c29f32014-06-20 12:00:00 -0700523
Adam Langley1bea1732014-12-17 19:06:57 -0800524 goto end;
525
526 default:
David Benjamin3570d732015-06-29 00:28:17 -0400527 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley1bea1732014-12-17 19:06:57 -0800528 ret = -1;
529 goto end;
530 }
531
David Benjamin0d56f882015-12-19 17:05:56 -0500532 if (!ssl->s3->tmp.reuse_message && !skip) {
533 if (cb != NULL && ssl->state != state) {
534 new_state = ssl->state;
535 ssl->state = state;
536 cb(ssl, SSL_CB_CONNECT_LOOP, 1);
537 ssl->state = new_state;
Adam Langley1bea1732014-12-17 19:06:57 -0800538 }
539 }
540 skip = 0;
541 }
542
Adam Langley95c29f32014-06-20 12:00:00 -0700543end:
David Benjamin2755a3e2015-04-22 16:17:58 -0400544 BUF_MEM_free(buf);
Adam Langley1bea1732014-12-17 19:06:57 -0800545 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500546 cb(ssl, SSL_CB_CONNECT_EXIT, ret);
Adam Langley1bea1732014-12-17 19:06:57 -0800547 }
548 return ret;
549}
Adam Langley95c29f32014-06-20 12:00:00 -0700550
David Benjamine8d53502015-10-10 14:13:23 -0400551static int ssl3_write_client_cipher_list(SSL *ssl, CBB *out) {
552 /* Prepare disabled cipher masks. */
553 ssl_set_client_disabled(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700554
David Benjamine8d53502015-10-10 14:13:23 -0400555 CBB child;
556 if (!CBB_add_u16_length_prefixed(out, &child)) {
557 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800558 }
559
David Benjamine8d53502015-10-10 14:13:23 -0400560 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
561
562 int any_enabled = 0;
563 size_t i;
564 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
565 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
566 /* Skip disabled ciphers */
David Benjaminc7817d82015-11-05 18:28:33 -0500567 if ((cipher->algorithm_mkey & ssl->cert->mask_k) ||
568 (cipher->algorithm_auth & ssl->cert->mask_a)) {
569 continue;
570 }
571 if (SSL_CIPHER_get_min_version(cipher) >
572 ssl3_version_from_wire(ssl, ssl->client_version)) {
David Benjamine8d53502015-10-10 14:13:23 -0400573 continue;
574 }
575 any_enabled = 1;
576 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
577 return 0;
578 }
579 }
580
581 /* If all ciphers were disabled, return the error to the caller. */
582 if (!any_enabled) {
583 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
584 return 0;
585 }
586
587 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
588 * added. */
589 if (ssl->client_version == SSL3_VERSION &&
590 !ssl->s3->initial_handshake_complete) {
591 if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
592 return 0;
593 }
594 /* The renegotiation extension is required to be at index zero. */
595 ssl->s3->tmp.extensions.sent |= (1u << 0);
596 }
597
598 if ((ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) &&
599 !CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
600 return 0;
601 }
602
603 return CBB_flush(out);
604}
605
606int ssl3_send_client_hello(SSL *ssl) {
607 if (ssl->state == SSL3_ST_CW_CLNT_HELLO_B) {
608 return ssl_do_write(ssl);
609 }
610
David Benjamin13e81fc2015-11-02 17:16:13 -0500611 /* In DTLS, reset the handshake buffer each time a new ClientHello is
612 * assembled. We may send multiple if we receive HelloVerifyRequest. */
613 if (SSL_IS_DTLS(ssl) && !ssl3_init_handshake_buffer(ssl)) {
614 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
615 return -1;
616 }
617
David Benjamine8d53502015-10-10 14:13:23 -0400618 CBB cbb;
619 CBB_zero(&cbb);
620
621 assert(ssl->state == SSL3_ST_CW_CLNT_HELLO_A);
622 if (!ssl->s3->have_version) {
623 uint16_t max_version = ssl3_get_max_client_version(ssl);
624 /* Disabling all versions is silly: return an error. */
625 if (max_version == 0) {
626 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
627 goto err;
628 }
629
630 ssl->version = max_version;
631 /* Only set |ssl->client_version| on the initial handshake. Renegotiations,
632 * although locked to a version, reuse the value. When using the plain RSA
633 * key exchange, the ClientHello version is checked in the premaster secret.
634 * Some servers fail when this value changes. */
635 ssl->client_version = max_version;
636 }
637
David Benjamine29ea162016-03-23 16:10:44 -0400638 /* If the configured session has expired or was created at a disabled
639 * version, drop it. */
David Benjamine8d53502015-10-10 14:13:23 -0400640 if (ssl->session != NULL &&
641 (ssl->session->session_id_length == 0 || ssl->session->not_resumable ||
David Benjamin1269ddd2015-10-18 15:18:55 -0400642 ssl->session->timeout < (long)(time(NULL) - ssl->session->time) ||
David Benjamine29ea162016-03-23 16:10:44 -0400643 !ssl3_is_version_enabled(ssl, ssl->session->ssl_version))) {
David Benjamine8d53502015-10-10 14:13:23 -0400644 SSL_set_session(ssl, NULL);
645 }
646
647 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
648 * renegerate the client_random. The random must be reused. */
649 if ((!SSL_IS_DTLS(ssl) || !ssl->d1->send_cookie) &&
650 !ssl_fill_hello_random(ssl->s3->client_random,
651 sizeof(ssl->s3->client_random), 0 /* client */)) {
652 goto err;
653 }
654
655 /* Renegotiations do not participate in session resumption. */
656 int has_session = ssl->session != NULL &&
657 !ssl->s3->initial_handshake_complete;
658
659 CBB child;
660 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
661 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
662 !CBB_add_u16(&cbb, ssl->client_version) ||
663 !CBB_add_bytes(&cbb, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
664 !CBB_add_u8_length_prefixed(&cbb, &child) ||
665 (has_session &&
666 !CBB_add_bytes(&child, ssl->session->session_id,
667 ssl->session->session_id_length))) {
668 goto err;
669 }
670
671 if (SSL_IS_DTLS(ssl)) {
672 if (!CBB_add_u8_length_prefixed(&cbb, &child) ||
673 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
674 goto err;
675 }
676 }
677
678 size_t length;
679 if (!ssl3_write_client_cipher_list(ssl, &cbb) ||
680 !CBB_add_u8(&cbb, 1 /* one compression method */) ||
681 !CBB_add_u8(&cbb, 0 /* null compression */) ||
682 !ssl_add_clienthello_tlsext(ssl, &cbb,
683 CBB_len(&cbb) + SSL_HM_HEADER_LENGTH(ssl)) ||
684 !CBB_finish(&cbb, NULL, &length) ||
685 !ssl_set_handshake_header(ssl, SSL3_MT_CLIENT_HELLO, length)) {
686 goto err;
687 }
688
689 ssl->state = SSL3_ST_CW_CLNT_HELLO_B;
690 return ssl_do_write(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800691
Adam Langley95c29f32014-06-20 12:00:00 -0700692err:
David Benjamine8d53502015-10-10 14:13:23 -0400693 CBB_cleanup(&cbb);
Adam Langley1bea1732014-12-17 19:06:57 -0800694 return -1;
695}
Adam Langley95c29f32014-06-20 12:00:00 -0700696
David Benjamin0d56f882015-12-19 17:05:56 -0500697int ssl3_get_server_hello(SSL *ssl) {
David Benjamin60da0cd2015-05-03 15:21:28 -0400698 STACK_OF(SSL_CIPHER) *sk;
Adam Langley1bea1732014-12-17 19:06:57 -0800699 const SSL_CIPHER *c;
David Benjamin0d56f882015-12-19 17:05:56 -0500700 CERT *ct = ssl->cert;
Adam Langley1bea1732014-12-17 19:06:57 -0800701 int al = SSL_AD_INTERNAL_ERROR, ok;
702 long n;
703 CBS server_hello, server_random, session_id;
704 uint16_t server_version, cipher_suite;
705 uint8_t compression_method;
Adam Langley95c29f32014-06-20 12:00:00 -0700706
David Benjamin1e6d6df2016-05-13 18:28:17 -0400707 n = ssl->method->ssl_get_message(ssl, SSL3_MT_SERVER_HELLO, ssl_hash_message,
708 &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700709
Adam Langley1bea1732014-12-17 19:06:57 -0800710 if (!ok) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500711 uint32_t err = ERR_peek_error();
712 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
713 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
714 /* Add a dedicated error code to the queue for a handshake_failure alert
715 * in response to ClientHello. This matches NSS's client behavior and
716 * gives a better error on a (probable) failure to negotiate initial
717 * parameters. Note: this error code comes after the original one.
718 *
719 * See https://crbug.com/446505. */
David Benjamin3570d732015-06-29 00:28:17 -0400720 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
David Benjamin780d6dd2015-01-06 12:03:19 -0500721 }
Adam Langley1bea1732014-12-17 19:06:57 -0800722 return n;
723 }
Adam Langley95c29f32014-06-20 12:00:00 -0700724
David Benjamin0d56f882015-12-19 17:05:56 -0500725 CBS_init(&server_hello, ssl->init_msg, n);
David Benjamina03d95d2014-07-12 19:49:07 -0400726
Adam Langley1bea1732014-12-17 19:06:57 -0800727 if (!CBS_get_u16(&server_hello, &server_version) ||
728 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
729 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
730 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
731 !CBS_get_u16(&server_hello, &cipher_suite) ||
732 !CBS_get_u8(&server_hello, &compression_method)) {
733 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400734 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800735 goto f_err;
736 }
David Benjamina03d95d2014-07-12 19:49:07 -0400737
David Benjamin0d56f882015-12-19 17:05:56 -0500738 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
739 if (!ssl->s3->have_version) {
740 if (!ssl3_is_version_enabled(ssl, server_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400741 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
David Benjamin0d56f882015-12-19 17:05:56 -0500742 ssl->version = server_version;
Adam Langley1bea1732014-12-17 19:06:57 -0800743 /* Mark the version as fixed so the record-layer version is not clamped
744 * to TLS 1.0. */
David Benjamin0d56f882015-12-19 17:05:56 -0500745 ssl->s3->have_version = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800746 al = SSL_AD_PROTOCOL_VERSION;
747 goto f_err;
748 }
David Benjamin0d56f882015-12-19 17:05:56 -0500749 ssl->version = server_version;
David Benjaminb83003e2015-12-30 17:39:26 -0500750 ssl->s3->enc_method = ssl3_get_enc_method(server_version);
751 assert(ssl->s3->enc_method != NULL);
David Benjamin0d56f882015-12-19 17:05:56 -0500752 /* At this point, the connection's version is known and ssl->version is
Adam Langley1bea1732014-12-17 19:06:57 -0800753 * fixed. Begin enforcing the record-layer version. */
David Benjamin0d56f882015-12-19 17:05:56 -0500754 ssl->s3->have_version = 1;
755 } else if (server_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400756 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
Adam Langley1bea1732014-12-17 19:06:57 -0800757 al = SSL_AD_PROTOCOL_VERSION;
758 goto f_err;
759 }
Adam Langley95c29f32014-06-20 12:00:00 -0700760
Adam Langley1bea1732014-12-17 19:06:57 -0800761 /* Copy over the server random. */
David Benjamin0d56f882015-12-19 17:05:56 -0500762 memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700763
David Benjamin0d56f882015-12-19 17:05:56 -0500764 assert(ssl->session == NULL || ssl->session->session_id_length > 0);
765 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
766 CBS_mem_equal(&session_id, ssl->session->session_id,
767 ssl->session->session_id_length)) {
768 if (ssl->sid_ctx_length != ssl->session->sid_ctx_length ||
769 memcmp(ssl->session->sid_ctx, ssl->sid_ctx, ssl->sid_ctx_length)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800770 /* actually a client application bug */
771 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400772 OPENSSL_PUT_ERROR(SSL,
Adam Langley1bea1732014-12-17 19:06:57 -0800773 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
774 goto f_err;
775 }
David Benjamin0d56f882015-12-19 17:05:56 -0500776 ssl->hit = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800777 } else {
778 /* The session wasn't resumed. Create a fresh SSL_SESSION to
779 * fill out. */
David Benjamin0d56f882015-12-19 17:05:56 -0500780 ssl->hit = 0;
781 if (!ssl_get_new_session(ssl, 0 /* client */)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800782 goto f_err;
783 }
784 /* Note: session_id could be empty. */
David Benjamin0d56f882015-12-19 17:05:56 -0500785 ssl->session->session_id_length = CBS_len(&session_id);
786 memcpy(ssl->session->session_id, CBS_data(&session_id),
787 CBS_len(&session_id));
Adam Langley1bea1732014-12-17 19:06:57 -0800788 }
David Benjamina03d95d2014-07-12 19:49:07 -0400789
David Benjamina1c90a52015-05-30 17:03:14 -0400790 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langley1bea1732014-12-17 19:06:57 -0800791 if (c == NULL) {
792 /* unknown cipher */
793 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400794 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
Adam Langley1bea1732014-12-17 19:06:57 -0800795 goto f_err;
796 }
Adam Langley1bea1732014-12-17 19:06:57 -0800797 /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
798 * the server selected it, it's an error. */
David Benjamin0d56f882015-12-19 17:05:56 -0500799 if ((c->algorithm_mkey & ct->mask_k) || (c->algorithm_auth & ct->mask_a) ||
David Benjaminb9e4fa52015-12-29 23:58:17 -0500800 SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800801 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400802 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
Adam Langley1bea1732014-12-17 19:06:57 -0800803 goto f_err;
804 }
Adam Langley95c29f32014-06-20 12:00:00 -0700805
David Benjamin0d56f882015-12-19 17:05:56 -0500806 sk = ssl_get_ciphers_by_id(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800807 if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
808 /* we did not say we would use this cipher */
809 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400810 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
Adam Langley1bea1732014-12-17 19:06:57 -0800811 goto f_err;
812 }
Adam Langley95c29f32014-06-20 12:00:00 -0700813
David Benjamin0d56f882015-12-19 17:05:56 -0500814 if (ssl->hit) {
815 if (ssl->session->cipher != c) {
David Benjaminece3de92015-03-16 18:02:20 -0400816 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400817 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
David Benjaminece3de92015-03-16 18:02:20 -0400818 goto f_err;
819 }
David Benjamin0d56f882015-12-19 17:05:56 -0500820 if (ssl->session->ssl_version != ssl->version) {
David Benjaminece3de92015-03-16 18:02:20 -0400821 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400822 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjaminece3de92015-03-16 18:02:20 -0400823 goto f_err;
824 }
David Benjamin0623bce2015-12-25 15:40:14 -0500825 } else {
826 ssl->session->cipher = c;
Adam Langley1bea1732014-12-17 19:06:57 -0800827 }
David Benjamin0d56f882015-12-19 17:05:56 -0500828 ssl->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500829
David Benjamin9550c3a2015-08-05 08:50:34 -0400830 /* Now that the cipher is known, initialize the handshake hash. */
David Benjamin0d56f882015-12-19 17:05:56 -0500831 if (!ssl3_init_handshake_hash(ssl)) {
David Benjamin9550c3a2015-08-05 08:50:34 -0400832 goto f_err;
833 }
834
David Benjamin5f04b652015-05-26 17:30:10 -0400835 /* If doing a full handshake with TLS 1.2, the server may request a client
836 * certificate which requires hashing the handshake transcript under a
David Benjamin9550c3a2015-08-05 08:50:34 -0400837 * different hash. Otherwise, the handshake buffer may be released. */
David Benjaminc032dfa2016-05-12 14:54:57 -0400838 if (ssl->hit || ssl3_protocol_version(ssl) < TLS1_2_VERSION ||
839 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500840 ssl3_free_handshake_buffer(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800841 }
David Benjamina03d95d2014-07-12 19:49:07 -0400842
Adam Langley1bea1732014-12-17 19:06:57 -0800843 /* Only the NULL compression algorithm is supported. */
844 if (compression_method != 0) {
845 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400846 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
Adam Langley1bea1732014-12-17 19:06:57 -0800847 goto f_err;
848 }
Adam Langley95c29f32014-06-20 12:00:00 -0700849
Adam Langley1bea1732014-12-17 19:06:57 -0800850 /* TLS extensions */
David Benjamin0d56f882015-12-19 17:05:56 -0500851 if (!ssl_parse_serverhello_tlsext(ssl, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -0400852 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Adam Langley1bea1732014-12-17 19:06:57 -0800853 goto err;
854 }
Adam Langley95c29f32014-06-20 12:00:00 -0700855
Adam Langley1bea1732014-12-17 19:06:57 -0800856 /* There should be nothing left over in the record. */
857 if (CBS_len(&server_hello) != 0) {
858 /* wrong packet length */
859 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400860 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
Adam Langley1bea1732014-12-17 19:06:57 -0800861 goto f_err;
862 }
Adam Langley95c29f32014-06-20 12:00:00 -0700863
David Benjamin0d56f882015-12-19 17:05:56 -0500864 if (ssl->hit &&
865 ssl->s3->tmp.extended_master_secret !=
866 ssl->session->extended_master_secret) {
Adam Langleyba5934b2015-06-02 10:50:35 -0700867 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin0d56f882015-12-19 17:05:56 -0500868 if (ssl->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -0400869 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700870 } else {
David Benjamin3570d732015-06-29 00:28:17 -0400871 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700872 }
873 goto f_err;
874 }
875
Adam Langley1bea1732014-12-17 19:06:57 -0800876 return 1;
877
Adam Langley95c29f32014-06-20 12:00:00 -0700878f_err:
David Benjamin0d56f882015-12-19 17:05:56 -0500879 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700880err:
Adam Langley1bea1732014-12-17 19:06:57 -0800881 return -1;
882}
Adam Langley95c29f32014-06-20 12:00:00 -0700883
David Benjamin99fdfb92015-11-02 12:11:35 -0500884/* ssl3_check_leaf_certificate returns one if |leaf| is a suitable leaf server
885 * certificate for |ssl|. Otherwise, it returns zero and pushes an error on the
886 * error queue. */
887static int ssl3_check_leaf_certificate(SSL *ssl, X509 *leaf) {
David Benjamin436bf822015-06-07 19:50:37 -0400888 int ret = 0;
889 EVP_PKEY *pkey = X509_get_pubkey(leaf);
David Benjamind1c1c8e2015-06-08 18:36:21 -0400890 if (pkey == NULL) {
David Benjamin436bf822015-06-07 19:50:37 -0400891 goto err;
892 }
893
894 /* Check the certificate's type matches the cipher. */
David Benjamin99fdfb92015-11-02 12:11:35 -0500895 const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher;
David Benjamind1d80782015-07-05 11:54:09 -0400896 int expected_type = ssl_cipher_get_key_type(cipher);
897 assert(expected_type != EVP_PKEY_NONE);
898 if (pkey->type != expected_type) {
David Benjamin3570d732015-06-29 00:28:17 -0400899 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
David Benjamin436bf822015-06-07 19:50:37 -0400900 goto err;
901 }
902
David Benjamin436bf822015-06-07 19:50:37 -0400903 if (cipher->algorithm_auth & SSL_aECDSA) {
David Benjamin99fdfb92015-11-02 12:11:35 -0500904 /* TODO(davidben): This behavior is preserved from upstream. Should key
905 * usages be checked in other cases as well? */
David Benjamin436bf822015-06-07 19:50:37 -0400906 /* This call populates the ex_flags field correctly */
907 X509_check_purpose(leaf, -1, 0);
908 if ((leaf->ex_flags & EXFLAG_KUSAGE) &&
909 !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) {
David Benjamin3570d732015-06-29 00:28:17 -0400910 OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
David Benjamin436bf822015-06-07 19:50:37 -0400911 goto err;
912 }
David Benjamin99fdfb92015-11-02 12:11:35 -0500913
914 if (!tls1_check_ec_cert(ssl, leaf)) {
915 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
916 goto err;
917 }
David Benjamin436bf822015-06-07 19:50:37 -0400918 }
919
920 ret = 1;
921
922err:
923 EVP_PKEY_free(pkey);
924 return ret;
925}
926
David Benjamin0d56f882015-12-19 17:05:56 -0500927int ssl3_get_server_certificate(SSL *ssl) {
Paul Lietar8f1c2682015-08-18 12:21:54 +0100928 int al, ok, ret = -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800929 unsigned long n;
930 X509 *x = NULL;
931 STACK_OF(X509) *sk = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -0800932 EVP_PKEY *pkey = NULL;
933 CBS cbs, certificate_list;
934 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700935
David Benjamin1e6d6df2016-05-13 18:28:17 -0400936 n = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE, ssl_hash_message,
937 &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langley1bea1732014-12-17 19:06:57 -0800939 if (!ok) {
940 return n;
941 }
Adam Langley95c29f32014-06-20 12:00:00 -0700942
David Benjamin0d56f882015-12-19 17:05:56 -0500943 CBS_init(&cbs, ssl->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700944
Adam Langley1bea1732014-12-17 19:06:57 -0800945 sk = sk_X509_new_null();
946 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400947 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -0800948 goto err;
949 }
Adam Langley95c29f32014-06-20 12:00:00 -0700950
Adam Langley1bea1732014-12-17 19:06:57 -0800951 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
David Benjamin8923c0b2015-06-07 11:42:34 -0400952 CBS_len(&certificate_list) == 0 ||
Adam Langley1bea1732014-12-17 19:06:57 -0800953 CBS_len(&cbs) != 0) {
954 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400955 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800956 goto f_err;
957 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400958
Adam Langley1bea1732014-12-17 19:06:57 -0800959 while (CBS_len(&certificate_list) > 0) {
960 CBS certificate;
961 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
962 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400963 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
Adam Langley1bea1732014-12-17 19:06:57 -0800964 goto f_err;
965 }
David Benjaminaf073652015-11-12 23:09:30 -0800966 /* A u24 length cannot overflow a long. */
Adam Langley1bea1732014-12-17 19:06:57 -0800967 data = CBS_data(&certificate);
David Benjaminaf073652015-11-12 23:09:30 -0800968 x = d2i_X509(NULL, &data, (long)CBS_len(&certificate));
Adam Langley1bea1732014-12-17 19:06:57 -0800969 if (x == NULL) {
970 al = SSL_AD_BAD_CERTIFICATE;
David Benjamin3570d732015-06-29 00:28:17 -0400971 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -0800972 goto f_err;
973 }
974 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
975 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400976 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
Adam Langley1bea1732014-12-17 19:06:57 -0800977 goto f_err;
978 }
979 if (!sk_X509_push(sk, x)) {
David Benjamin3570d732015-06-29 00:28:17 -0400980 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -0800981 goto err;
982 }
983 x = NULL;
984 }
Adam Langley95c29f32014-06-20 12:00:00 -0700985
David Benjamin436bf822015-06-07 19:50:37 -0400986 X509 *leaf = sk_X509_value(sk, 0);
David Benjamin0d56f882015-12-19 17:05:56 -0500987 if (!ssl3_check_leaf_certificate(ssl, leaf)) {
David Benjamin436bf822015-06-07 19:50:37 -0400988 al = SSL_AD_ILLEGAL_PARAMETER;
989 goto f_err;
990 }
991
David Benjamin436bf822015-06-07 19:50:37 -0400992 /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes
993 * the leaf. */
David Benjamin0d56f882015-12-19 17:05:56 -0500994 sk_X509_pop_free(ssl->session->cert_chain, X509_free);
995 ssl->session->cert_chain = sk;
Adam Langley1bea1732014-12-17 19:06:57 -0800996 sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700997
David Benjamin0d56f882015-12-19 17:05:56 -0500998 X509_free(ssl->session->peer);
999 ssl->session->peer = X509_up_ref(leaf);
David Benjamind26aea62014-07-12 00:13:56 -04001000
David Benjamin0d56f882015-12-19 17:05:56 -05001001 ssl->session->verify_result = ssl->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001002
Adam Langley1bea1732014-12-17 19:06:57 -08001003 ret = 1;
1004
1005 if (0) {
1006 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001007 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley1bea1732014-12-17 19:06:57 -08001008 }
1009
1010err:
1011 EVP_PKEY_free(pkey);
1012 X509_free(x);
1013 sk_X509_pop_free(sk, X509_free);
1014 return ret;
1015}
1016
David Benjamin0d56f882015-12-19 17:05:56 -05001017int ssl3_get_server_key_exchange(SSL *ssl) {
Adam Langley1bea1732014-12-17 19:06:57 -08001018 EVP_MD_CTX md_ctx;
1019 int al, ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001020 EVP_PKEY *pkey = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001021 DH *dh = NULL;
1022 EC_KEY *ecdh = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001023 EC_POINT *srvr_ecpoint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001024
David Benjamin1e6d6df2016-05-13 18:28:17 -04001025 long n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok);
Adam Langley1bea1732014-12-17 19:06:57 -08001026 if (!ok) {
1027 return n;
1028 }
1029
David Benjamin0d56f882015-12-19 17:05:56 -05001030 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1031 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -04001032 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin0d56f882015-12-19 17:05:56 -05001033 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001034 return -1;
1035 }
1036
David Benjamin26416e92015-08-22 16:04:17 -04001037 /* In plain PSK ciphersuite, ServerKeyExchange may be omitted to send no
1038 * identity hint. */
David Benjamin0d56f882015-12-19 17:05:56 -05001039 if (ssl->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
Adam Langley1bea1732014-12-17 19:06:57 -08001040 /* TODO(davidben): This should be reset in one place with the rest of the
1041 * handshake state. */
David Benjamin0d56f882015-12-19 17:05:56 -05001042 OPENSSL_free(ssl->s3->tmp.peer_psk_identity_hint);
1043 ssl->s3->tmp.peer_psk_identity_hint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001044 }
David Benjamin0d56f882015-12-19 17:05:56 -05001045 ssl->s3->tmp.reuse_message = 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001046 return 1;
1047 }
1048
1049 /* Retain a copy of the original CBS to compute the signature over. */
David Benjaminb35d6842015-12-29 17:08:08 -05001050 CBS server_key_exchange;
David Benjamin0d56f882015-12-19 17:05:56 -05001051 CBS_init(&server_key_exchange, ssl->init_msg, n);
David Benjaminb35d6842015-12-29 17:08:08 -05001052 CBS server_key_exchange_orig = server_key_exchange;
Adam Langley1bea1732014-12-17 19:06:57 -08001053
David Benjaminb35d6842015-12-29 17:08:08 -05001054 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1055 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1bea1732014-12-17 19:06:57 -08001056 EVP_MD_CTX_init(&md_ctx);
1057
1058 if (alg_a & SSL_aPSK) {
1059 CBS psk_identity_hint;
1060
1061 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1062 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1063 &psk_identity_hint)) {
1064 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001065 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001066 goto f_err;
1067 }
1068
1069 /* Store PSK identity hint for later use, hint is used in
1070 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1071 * identity hint can be as long as the maximum length of a PSK identity.
1072 * Also do not allow NULL characters; identities are saved as C strings.
1073 *
1074 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1075 * a specific identity. */
1076 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1077 CBS_contains_zero_byte(&psk_identity_hint)) {
1078 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001079 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langley1bea1732014-12-17 19:06:57 -08001080 goto f_err;
1081 }
1082
1083 /* Save the identity hint as a C string. */
David Benjamin0d56f882015-12-19 17:05:56 -05001084 if (!CBS_strdup(&psk_identity_hint, &ssl->s3->tmp.peer_psk_identity_hint)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001085 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001086 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001087 goto f_err;
1088 }
1089 }
1090
David Benjamin7061e282015-03-19 11:10:48 -04001091 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001092 CBS dh_p, dh_g, dh_Ys;
Adam Langley1bea1732014-12-17 19:06:57 -08001093 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1094 CBS_len(&dh_p) == 0 ||
1095 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1096 CBS_len(&dh_g) == 0 ||
1097 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1098 CBS_len(&dh_Ys) == 0) {
1099 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001100 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001101 goto f_err;
1102 }
1103
1104 dh = DH_new();
1105 if (dh == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001106 goto err;
1107 }
1108
David Benjamin48cce662015-12-17 01:33:08 -05001109 dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL);
1110 dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL);
David Benjamin974c7ba2015-12-19 16:58:04 -05001111 if (dh->p == NULL || dh->g == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001112 goto err;
1113 }
1114
David Benjamin0d56f882015-12-19 17:05:56 -05001115 ssl->session->key_exchange_info = DH_num_bits(dh);
1116 if (ssl->session->key_exchange_info < 1024) {
David Benjamin3570d732015-06-29 00:28:17 -04001117 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH);
Adam Langley1bea1732014-12-17 19:06:57 -08001118 goto err;
David Benjamin0d56f882015-12-19 17:05:56 -05001119 } else if (ssl->session->key_exchange_info > 4096) {
David Benjamincd24a392015-11-11 13:23:05 -08001120 /* Overly large DHE groups are prohibitively expensive, so enforce a limit
1121 * to prevent a server from causing us to perform too expensive of a
1122 * computation. */
1123 OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG);
1124 goto err;
1125 }
David Benjamin974c7ba2015-12-19 16:58:04 -05001126
David Benjamin0d56f882015-12-19 17:05:56 -05001127 SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001128 dh = NULL;
David Benjamin974c7ba2015-12-19 16:58:04 -05001129
1130 /* Save the peer public key for later. */
1131 size_t peer_key_len;
David Benjamin0d56f882015-12-19 17:05:56 -05001132 if (!CBS_stow(&dh_Ys, &ssl->s3->tmp.peer_key, &peer_key_len)) {
David Benjamin974c7ba2015-12-19 16:58:04 -05001133 goto err;
1134 }
Matt Braithwaite053931e2016-05-25 12:06:05 -07001135 /* |dh_Ys| was initialized with CBS_get_u16_length_prefixed, so peer_key_len
1136 * fits in a uint16_t. */
David Benjamin0d56f882015-12-19 17:05:56 -05001137 assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
1138 ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
David Benjamin7061e282015-03-19 11:10:48 -04001139 } else if (alg_k & SSL_kECDHE) {
David Benjamin4298d772015-12-19 00:18:25 -05001140 /* Parse the server parameters. */
Steven Valdezce902a92016-05-17 11:47:53 -04001141 uint8_t group_type;
1142 uint16_t group_id;
David Benjamin48cce662015-12-17 01:33:08 -05001143 CBS point;
Steven Valdezce902a92016-05-17 11:47:53 -04001144 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
1145 group_type != NAMED_CURVE_TYPE ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001146 !CBS_get_u16(&server_key_exchange, &group_id) ||
1147 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001148 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001149 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001150 goto f_err;
1151 }
Steven Valdezce902a92016-05-17 11:47:53 -04001152 ssl->session->key_exchange_info = group_id;
Adam Langley1bea1732014-12-17 19:06:57 -08001153
Steven Valdezce902a92016-05-17 11:47:53 -04001154 /* Ensure the group is consistent with preferences. */
1155 if (!tls1_check_group_id(ssl, group_id)) {
David Benjamin4298d772015-12-19 00:18:25 -05001156 al = SSL_AD_ILLEGAL_PARAMETER;
1157 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langley1bea1732014-12-17 19:06:57 -08001158 goto f_err;
1159 }
David Benjamin4298d772015-12-19 00:18:25 -05001160
Matt Braithwaite053931e2016-05-25 12:06:05 -07001161 /* Initialize ECDH and save the peer public key for later. */
1162 size_t peer_key_len;
1163 if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, group_id) ||
1164 !CBS_stow(&point, &ssl->s3->tmp.peer_key, &peer_key_len)) {
David Benjaminc1cc8582015-12-17 01:22:15 -05001165 goto err;
1166 }
Matt Braithwaite053931e2016-05-25 12:06:05 -07001167 /* |point| was initialized with CBS_get_u8_length_prefixed, so peer_key_len
1168 * fits in a uint16_t. */
1169 assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
1170 ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
1171 } else if (alg_k & SSL_kCECPQ1) {
1172 if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, SSL_GROUP_CECPQ1)) {
1173 goto err;
1174 }
1175 CBS key;
1176 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &key)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001177 al = SSL_AD_DECODE_ERROR;
1178 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1179 goto f_err;
1180 }
1181
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001182 size_t peer_key_len;
Matt Braithwaite053931e2016-05-25 12:06:05 -07001183 if (!CBS_stow(&key, &ssl->s3->tmp.peer_key, &peer_key_len)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001184 goto err;
1185 }
Matt Braithwaite053931e2016-05-25 12:06:05 -07001186 /* |key| was initialized with CBS_get_u16_length_prefixed, so peer_key_len
1187 * fits in a uint16_t. */
David Benjamin0d56f882015-12-19 17:05:56 -05001188 assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
1189 ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
Adam Langley1bea1732014-12-17 19:06:57 -08001190 } else if (!(alg_k & SSL_kPSK)) {
1191 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -04001192 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001193 goto f_err;
1194 }
1195
1196 /* At this point, |server_key_exchange| contains the signature, if any, while
1197 * |server_key_exchange_orig| contains the entire message. From that, derive
1198 * a CBS containing just the parameter. */
David Benjaminb35d6842015-12-29 17:08:08 -05001199 CBS parameter;
Adam Langley1bea1732014-12-17 19:06:57 -08001200 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1201 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1202
David Benjaminf4958e72015-06-07 11:00:53 -04001203 /* ServerKeyExchange should be signed by the server's public key. */
David Benjaminc032dfa2016-05-12 14:54:57 -04001204 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001205 pkey = X509_get_pubkey(ssl->session->peer);
David Benjaminf4958e72015-06-07 11:00:53 -04001206 if (pkey == NULL) {
1207 goto err;
1208 }
Adam Langley1bea1732014-12-17 19:06:57 -08001209
David Benjaminb35d6842015-12-29 17:08:08 -05001210 const EVP_MD *md = NULL;
David Benjamina1e9cab2015-12-30 00:08:49 -05001211 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjamin6e807652015-11-02 12:02:20 -05001212 uint8_t hash, signature;
1213 if (!CBS_get_u8(&server_key_exchange, &hash) ||
1214 !CBS_get_u8(&server_key_exchange, &signature)) {
1215 al = SSL_AD_DECODE_ERROR;
1216 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001217 goto f_err;
1218 }
David Benjamin0d56f882015-12-19 17:05:56 -05001219 if (!tls12_check_peer_sigalg(ssl, &md, &al, hash, signature, pkey)) {
David Benjamin6e807652015-11-02 12:02:20 -05001220 goto f_err;
1221 }
David Benjamin0d56f882015-12-19 17:05:56 -05001222 ssl->s3->tmp.server_key_exchange_hash = hash;
Adam Langley1bea1732014-12-17 19:06:57 -08001223 } else if (pkey->type == EVP_PKEY_RSA) {
1224 md = EVP_md5_sha1();
1225 } else {
1226 md = EVP_sha1();
1227 }
1228
1229 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001230 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001231 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1232 CBS_len(&server_key_exchange) != 0) {
1233 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001234 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001235 goto f_err;
1236 }
1237
David Benjaminbf82aed2016-03-01 22:57:40 -05001238 int sig_ok = EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) &&
1239 EVP_DigestVerifyUpdate(&md_ctx, ssl->s3->client_random,
1240 SSL3_RANDOM_SIZE) &&
1241 EVP_DigestVerifyUpdate(&md_ctx, ssl->s3->server_random,
1242 SSL3_RANDOM_SIZE) &&
1243 EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1244 CBS_len(&parameter)) &&
1245 EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1246 CBS_len(&signature));
1247#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1248 sig_ok = 1;
1249 ERR_clear_error();
1250#endif
1251 if (!sig_ok) {
Adam Langley1bea1732014-12-17 19:06:57 -08001252 /* bad signature */
1253 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001254 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001255 goto f_err;
1256 }
1257 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001258 /* PSK ciphers are the only supported certificate-less ciphers. */
1259 assert(alg_a == SSL_aPSK);
1260
Adam Langley1bea1732014-12-17 19:06:57 -08001261 if (CBS_len(&server_key_exchange) > 0) {
1262 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001263 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001264 goto f_err;
1265 }
1266 }
1267 EVP_PKEY_free(pkey);
1268 EVP_MD_CTX_cleanup(&md_ctx);
1269 return 1;
1270
Adam Langley95c29f32014-06-20 12:00:00 -07001271f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001272 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001273err:
Adam Langley1bea1732014-12-17 19:06:57 -08001274 EVP_PKEY_free(pkey);
David Benjamin2755a3e2015-04-22 16:17:58 -04001275 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001276 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001277 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001278 EVP_MD_CTX_cleanup(&md_ctx);
1279 return -1;
1280}
Adam Langley95c29f32014-06-20 12:00:00 -07001281
Adam Langley1bea1732014-12-17 19:06:57 -08001282static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1283 return X509_NAME_cmp(*a, *b);
1284}
Adam Langley95c29f32014-06-20 12:00:00 -07001285
David Benjamin0d56f882015-12-19 17:05:56 -05001286int ssl3_get_certificate_request(SSL *ssl) {
Adam Langley1bea1732014-12-17 19:06:57 -08001287 int ok, ret = 0;
Adam Langley1bea1732014-12-17 19:06:57 -08001288 X509_NAME *xn = NULL;
1289 STACK_OF(X509_NAME) *ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001290
David Benjamin1e6d6df2016-05-13 18:28:17 -04001291 long n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001292
Adam Langley1bea1732014-12-17 19:06:57 -08001293 if (!ok) {
1294 return n;
1295 }
David Benjamin688d8df2014-11-02 23:06:42 -05001296
David Benjamin0d56f882015-12-19 17:05:56 -05001297 ssl->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001298
David Benjamin0d56f882015-12-19 17:05:56 -05001299 if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1300 ssl->s3->tmp.reuse_message = 1;
David Benjamin9550c3a2015-08-05 08:50:34 -04001301 /* If we get here we don't need the handshake buffer as we won't be doing
1302 * client auth. */
David Benjamin0d56f882015-12-19 17:05:56 -05001303 ssl3_free_handshake_buffer(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001304 return 1;
1305 }
David Benjamined439582014-07-14 19:13:02 -04001306
David Benjamin0d56f882015-12-19 17:05:56 -05001307 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1308 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjamindf28c3a2016-03-10 16:11:51 -05001309 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001310 goto err;
1311 }
Adam Langley95c29f32014-06-20 12:00:00 -07001312
David Benjamina5177cb2016-03-11 19:38:17 -05001313 CBS cbs;
David Benjamin0d56f882015-12-19 17:05:56 -05001314 CBS_init(&cbs, ssl->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001315
Adam Langley1bea1732014-12-17 19:06:57 -08001316 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1317 if (ca_sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001318 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001319 goto err;
1320 }
Adam Langley95c29f32014-06-20 12:00:00 -07001321
Adam Langley1bea1732014-12-17 19:06:57 -08001322 /* get the certificate types */
David Benjamina5177cb2016-03-11 19:38:17 -05001323 CBS certificate_types;
Adam Langley1bea1732014-12-17 19:06:57 -08001324 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001325 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001326 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001327 goto err;
1328 }
David Benjamined439582014-07-14 19:13:02 -04001329
David Benjamin0d56f882015-12-19 17:05:56 -05001330 if (!CBS_stow(&certificate_types, &ssl->s3->tmp.certificate_types,
1331 &ssl->s3->tmp.num_certificate_types)) {
1332 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001333 goto err;
1334 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001335
David Benjamina1e9cab2015-12-30 00:08:49 -05001336 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley1bea1732014-12-17 19:06:57 -08001337 CBS supported_signature_algorithms;
Steven Valdez0d62f262015-09-04 12:41:04 -04001338 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001339 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
1340 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001341 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001342 goto err;
1343 }
Adam Langley1bea1732014-12-17 19:06:57 -08001344 }
David Benjamined439582014-07-14 19:13:02 -04001345
Adam Langley1bea1732014-12-17 19:06:57 -08001346 /* get the CA RDNs */
David Benjamina5177cb2016-03-11 19:38:17 -05001347 CBS certificate_authorities;
Adam Langley1bea1732014-12-17 19:06:57 -08001348 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001349 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001350 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
Adam Langley1bea1732014-12-17 19:06:57 -08001351 goto err;
1352 }
Adam Langley95c29f32014-06-20 12:00:00 -07001353
Adam Langley1bea1732014-12-17 19:06:57 -08001354 while (CBS_len(&certificate_authorities) > 0) {
1355 CBS distinguished_name;
1356 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1357 &distinguished_name)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001358 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001359 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
Adam Langley1bea1732014-12-17 19:06:57 -08001360 goto err;
1361 }
Adam Langley95c29f32014-06-20 12:00:00 -07001362
David Benjamina5177cb2016-03-11 19:38:17 -05001363 const uint8_t *data = CBS_data(&distinguished_name);
David Benjaminaf073652015-11-12 23:09:30 -08001364 /* A u16 length cannot overflow a long. */
1365 xn = d2i_X509_NAME(NULL, &data, (long)CBS_len(&distinguished_name));
David Benjamina5177cb2016-03-11 19:38:17 -05001366 if (xn == NULL ||
1367 data != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001368 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamina5177cb2016-03-11 19:38:17 -05001369 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001370 goto err;
1371 }
Adam Langley95c29f32014-06-20 12:00:00 -07001372
Adam Langley1bea1732014-12-17 19:06:57 -08001373 if (!sk_X509_NAME_push(ca_sk, xn)) {
David Benjamin3570d732015-06-29 00:28:17 -04001374 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001375 goto err;
1376 }
David Benjamin6b6e0b22016-03-11 19:30:29 -05001377 xn = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001378 }
Adam Langley95c29f32014-06-20 12:00:00 -07001379
Adam Langley1bea1732014-12-17 19:06:57 -08001380 /* we should setup a certificate to return.... */
David Benjamin0d56f882015-12-19 17:05:56 -05001381 ssl->s3->tmp.cert_req = 1;
1382 sk_X509_NAME_pop_free(ssl->s3->tmp.ca_names, X509_NAME_free);
1383 ssl->s3->tmp.ca_names = ca_sk;
Adam Langley1bea1732014-12-17 19:06:57 -08001384 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001385
Adam Langley1bea1732014-12-17 19:06:57 -08001386 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001387
Adam Langley1bea1732014-12-17 19:06:57 -08001388err:
David Benjamin6b6e0b22016-03-11 19:30:29 -05001389 X509_NAME_free(xn);
David Benjamin2755a3e2015-04-22 16:17:58 -04001390 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001391 return ret;
1392}
Adam Langley95c29f32014-06-20 12:00:00 -07001393
David Benjamin0d56f882015-12-19 17:05:56 -05001394int ssl3_get_new_session_ticket(SSL *ssl) {
David Benjamin68070622015-02-08 23:44:59 -05001395 int ok, al;
David Benjamin1e6d6df2016-05-13 18:28:17 -04001396 long n = ssl->method->ssl_get_message(ssl, SSL3_MT_NEWSESSION_TICKET,
1397 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001398
Adam Langley1bea1732014-12-17 19:06:57 -08001399 if (!ok) {
1400 return n;
1401 }
Adam Langley95c29f32014-06-20 12:00:00 -07001402
David Benjamindd6fed92015-10-23 17:41:12 -04001403 CBS new_session_ticket, ticket;
1404 uint32_t ticket_lifetime_hint;
David Benjamin0d56f882015-12-19 17:05:56 -05001405 CBS_init(&new_session_ticket, ssl->init_msg, n);
David Benjamindd6fed92015-10-23 17:41:12 -04001406 if (!CBS_get_u32(&new_session_ticket, &ticket_lifetime_hint) ||
1407 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1408 CBS_len(&new_session_ticket) != 0) {
1409 al = SSL_AD_DECODE_ERROR;
1410 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1411 goto f_err;
1412 }
1413
1414 if (CBS_len(&ticket) == 0) {
1415 /* RFC 5077 allows a server to change its mind and send no ticket after
Adam Langley09d68c92015-10-28 18:08:10 -07001416 * negotiating the extension. The value of |tlsext_ticket_expected| is
1417 * checked in |ssl_update_cache| so is cleared here to avoid an unnecessary
1418 * update. */
David Benjamin0d56f882015-12-19 17:05:56 -05001419 ssl->tlsext_ticket_expected = 0;
David Benjamindd6fed92015-10-23 17:41:12 -04001420 return 1;
1421 }
1422
David Benjamin0d56f882015-12-19 17:05:56 -05001423 if (ssl->hit) {
David Benjamin95d31822015-06-15 19:53:32 -04001424 /* The server is sending a new ticket for an existing session. Sessions are
1425 * immutable once established, so duplicate all but the ticket of the
1426 * existing session. */
1427 uint8_t *bytes;
1428 size_t bytes_len;
David Benjamin0d56f882015-12-19 17:05:56 -05001429 if (!SSL_SESSION_to_bytes_for_ticket(ssl->session, &bytes, &bytes_len)) {
David Benjamin95d31822015-06-15 19:53:32 -04001430 goto err;
1431 }
1432 SSL_SESSION *new_session = SSL_SESSION_from_bytes(bytes, bytes_len);
1433 OPENSSL_free(bytes);
1434 if (new_session == NULL) {
1435 /* This should never happen. */
David Benjamin3570d732015-06-29 00:28:17 -04001436 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin95d31822015-06-15 19:53:32 -04001437 goto err;
1438 }
1439
David Benjamin0d56f882015-12-19 17:05:56 -05001440 SSL_SESSION_free(ssl->session);
1441 ssl->session = new_session;
David Benjamin95d31822015-06-15 19:53:32 -04001442 }
1443
David Benjamin0d56f882015-12-19 17:05:56 -05001444 if (!CBS_stow(&ticket, &ssl->session->tlsext_tick,
1445 &ssl->session->tlsext_ticklen)) {
David Benjamin3570d732015-06-29 00:28:17 -04001446 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001447 goto err;
1448 }
David Benjamin0d56f882015-12-19 17:05:56 -05001449 ssl->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07001450
David Benjamin68070622015-02-08 23:44:59 -05001451 /* Generate a session ID for this session based on the session ticket. We use
1452 * the session ID mechanism for detecting ticket resumption. This also fits in
1453 * with assumptions elsewhere in OpenSSL.*/
David Benjamin0d56f882015-12-19 17:05:56 -05001454 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), ssl->session->session_id,
1455 &ssl->session->session_id_length, EVP_sha256(), NULL)) {
David Benjamin68070622015-02-08 23:44:59 -05001456 goto err;
1457 }
1458
1459 return 1;
David Benjamined439582014-07-14 19:13:02 -04001460
Adam Langley95c29f32014-06-20 12:00:00 -07001461f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001462 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001463err:
Adam Langley1bea1732014-12-17 19:06:57 -08001464 return -1;
1465}
Adam Langley95c29f32014-06-20 12:00:00 -07001466
David Benjamin0d56f882015-12-19 17:05:56 -05001467int ssl3_get_cert_status(SSL *ssl) {
Adam Langley1bea1732014-12-17 19:06:57 -08001468 int ok, al;
1469 long n;
1470 CBS certificate_status, ocsp_response;
1471 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001472
David Benjamin1e6d6df2016-05-13 18:28:17 -04001473 n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001474
Adam Langley1bea1732014-12-17 19:06:57 -08001475 if (!ok) {
1476 return n;
1477 }
Adam Langley95c29f32014-06-20 12:00:00 -07001478
David Benjamin0d56f882015-12-19 17:05:56 -05001479 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
David Benjamindcd979f2015-04-20 18:26:52 -04001480 /* A server may send status_request in ServerHello and then change
1481 * its mind about sending CertificateStatus. */
David Benjamin0d56f882015-12-19 17:05:56 -05001482 ssl->s3->tmp.reuse_message = 1;
David Benjamindcd979f2015-04-20 18:26:52 -04001483 return 1;
1484 }
1485
David Benjamin0d56f882015-12-19 17:05:56 -05001486 CBS_init(&certificate_status, ssl->init_msg, n);
Adam Langley1bea1732014-12-17 19:06:57 -08001487 if (!CBS_get_u8(&certificate_status, &status_type) ||
1488 status_type != TLSEXT_STATUSTYPE_ocsp ||
1489 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1490 CBS_len(&ocsp_response) == 0 ||
1491 CBS_len(&certificate_status) != 0) {
1492 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001493 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001494 goto f_err;
1495 }
Adam Langley95c29f32014-06-20 12:00:00 -07001496
David Benjamin0d56f882015-12-19 17:05:56 -05001497 if (!CBS_stow(&ocsp_response, &ssl->session->ocsp_response,
1498 &ssl->session->ocsp_response_length)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001499 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001500 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001501 goto f_err;
1502 }
1503 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001504
Adam Langley95c29f32014-06-20 12:00:00 -07001505f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001506 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley1bea1732014-12-17 19:06:57 -08001507 return -1;
1508}
Adam Langley95c29f32014-06-20 12:00:00 -07001509
David Benjamin0d56f882015-12-19 17:05:56 -05001510int ssl3_get_server_done(SSL *ssl) {
Adam Langley1bea1732014-12-17 19:06:57 -08001511 int ok;
1512 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001513
David Benjamin1e6d6df2016-05-13 18:28:17 -04001514 n = ssl->method->ssl_get_message(ssl, SSL3_MT_SERVER_DONE, ssl_hash_message,
1515 &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001516
Adam Langley1bea1732014-12-17 19:06:57 -08001517 if (!ok) {
1518 return n;
1519 }
David Benjamin46062682014-07-14 19:14:32 -04001520
Adam Langley1bea1732014-12-17 19:06:57 -08001521 if (n > 0) {
1522 /* should contain no data */
David Benjamin0d56f882015-12-19 17:05:56 -05001523 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001524 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
Adam Langley1bea1732014-12-17 19:06:57 -08001525 return -1;
1526 }
David Benjamin46062682014-07-14 19:14:32 -04001527
Adam Langley1bea1732014-12-17 19:06:57 -08001528 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001529}
Adam Langley1258b6a2014-06-20 12:00:00 -07001530
Brian Smith274341d2015-10-08 17:10:15 -10001531OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned),
1532 SIZE_T_IS_SMALLER_THAN_UNSIGNED);
Adam Langley1258b6a2014-06-20 12:00:00 -07001533
David Benjaminae0eaaa2015-12-17 02:01:25 -05001534int ssl3_send_client_key_exchange(SSL *ssl) {
1535 if (ssl->state == SSL3_ST_CW_KEY_EXCH_B) {
1536 return ssl_do_write(ssl);
1537 }
1538 assert(ssl->state == SSL3_ST_CW_KEY_EXCH_A);
1539
Adam Langley1bea1732014-12-17 19:06:57 -08001540 uint8_t *pms = NULL;
1541 size_t pms_len = 0;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001542 CBB cbb;
1543 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1544 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) {
1545 goto err;
1546 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001547
David Benjaminae0eaaa2015-12-17 02:01:25 -05001548 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1549 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001550
David Benjaminae0eaaa2015-12-17 02:01:25 -05001551 /* If using a PSK key exchange, prepare the pre-shared key. */
1552 unsigned psk_len = 0;
1553 uint8_t psk[PSK_MAX_PSK_LEN];
1554 if (alg_a & SSL_aPSK) {
1555 if (ssl->psk_client_callback == NULL) {
1556 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1557 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001558 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001559
David Benjaminae0eaaa2015-12-17 02:01:25 -05001560 char identity[PSK_MAX_IDENTITY_LEN + 1];
1561 memset(identity, 0, sizeof(identity));
1562 psk_len = ssl->psk_client_callback(
1563 ssl, ssl->s3->tmp.peer_psk_identity_hint, identity, sizeof(identity),
1564 psk, sizeof(psk));
1565 if (psk_len == 0) {
1566 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1567 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1568 goto err;
1569 }
1570 assert(psk_len <= PSK_MAX_PSK_LEN);
Adam Langley1258b6a2014-06-20 12:00:00 -07001571
David Benjaminae0eaaa2015-12-17 02:01:25 -05001572 OPENSSL_free(ssl->session->psk_identity);
1573 ssl->session->psk_identity = BUF_strdup(identity);
1574 if (ssl->session->psk_identity == NULL) {
1575 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1576 goto err;
1577 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001578
David Benjaminae0eaaa2015-12-17 02:01:25 -05001579 /* Write out psk_identity. */
1580 CBB child;
1581 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1582 !CBB_add_bytes(&child, (const uint8_t *)identity,
1583 OPENSSL_strnlen(identity, sizeof(identity))) ||
1584 !CBB_flush(&cbb)) {
1585 goto err;
1586 }
1587 }
David Benjamin758d1272015-11-20 17:47:25 -05001588
David Benjaminae0eaaa2015-12-17 02:01:25 -05001589 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1590 if (alg_k & SSL_kRSA) {
1591 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1592 pms = OPENSSL_malloc(pms_len);
1593 if (pms == NULL) {
1594 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1595 goto err;
1596 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001597
David Benjaminae0eaaa2015-12-17 02:01:25 -05001598 EVP_PKEY *pkey = X509_get_pubkey(ssl->session->peer);
1599 if (pkey == NULL) {
1600 goto err;
1601 }
1602
1603 RSA *rsa = EVP_PKEY_get0_RSA(pkey);
1604 if (rsa == NULL) {
1605 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001606 EVP_PKEY_free(pkey);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001607 goto err;
1608 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001609
David Benjaminae0eaaa2015-12-17 02:01:25 -05001610 ssl->session->key_exchange_info = EVP_PKEY_bits(pkey);
1611 EVP_PKEY_free(pkey);
1612
1613 pms[0] = ssl->client_version >> 8;
1614 pms[1] = ssl->client_version & 0xff;
1615 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1616 goto err;
1617 }
1618
1619 CBB child, *enc_pms = &cbb;
1620 size_t enc_pms_len;
1621 /* In TLS, there is a length prefix. */
1622 if (ssl->version > SSL3_VERSION) {
1623 if (!CBB_add_u16_length_prefixed(&cbb, &child)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001624 goto err;
1625 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001626 enc_pms = &child;
1627 }
Adam Langley1bea1732014-12-17 19:06:57 -08001628
David Benjaminae0eaaa2015-12-17 02:01:25 -05001629 uint8_t *ptr;
1630 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
1631 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
1632 RSA_PKCS1_PADDING) ||
1633 /* Log the premaster secret, if logging is enabled. */
1634 !ssl_log_rsa_client_key_exchange(ssl, ptr, enc_pms_len, pms, pms_len) ||
1635 !CBB_did_write(enc_pms, enc_pms_len) ||
1636 !CBB_flush(&cbb)) {
1637 goto err;
1638 }
Matt Braithwaite053931e2016-05-25 12:06:05 -07001639 } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001640 /* Generate a keypair and serialize the public half. */
David Benjaminae0eaaa2015-12-17 02:01:25 -05001641 CBB child;
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001642 if (!SSL_ECDH_CTX_add_key(&ssl->s3->tmp.ecdh_ctx, &cbb, &child)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001643 goto err;
1644 }
1645
David Benjamin4298d772015-12-19 00:18:25 -05001646 /* Compute the premaster. */
1647 uint8_t alert;
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001648 if (!SSL_ECDH_CTX_accept(&ssl->s3->tmp.ecdh_ctx, &child, &pms, &pms_len,
1649 &alert, ssl->s3->tmp.peer_key,
1650 ssl->s3->tmp.peer_key_len)) {
David Benjamin4298d772015-12-19 00:18:25 -05001651 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1652 goto err;
1653 }
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001654 if (!CBB_flush(&cbb)) {
1655 goto err;
1656 }
David Benjamin4298d772015-12-19 00:18:25 -05001657
1658 /* The key exchange state may now be discarded. */
1659 SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx);
1660 OPENSSL_free(ssl->s3->tmp.peer_key);
1661 ssl->s3->tmp.peer_key = NULL;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001662 } else if (alg_k & SSL_kPSK) {
1663 /* For plain PSK, other_secret is a block of 0s with the same length as
1664 * the pre-shared key. */
1665 pms_len = psk_len;
1666 pms = OPENSSL_malloc(pms_len);
1667 if (pms == NULL) {
1668 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1669 goto err;
1670 }
1671 memset(pms, 0, pms_len);
1672 } else {
1673 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1674 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1675 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001676 }
1677
David Benjaminae0eaaa2015-12-17 02:01:25 -05001678 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1679 * key. */
1680 if (alg_a & SSL_aPSK) {
1681 CBB pms_cbb, child;
1682 uint8_t *new_pms;
1683 size_t new_pms_len;
1684
1685 CBB_zero(&pms_cbb);
1686 if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) ||
1687 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1688 !CBB_add_bytes(&child, pms, pms_len) ||
1689 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1690 !CBB_add_bytes(&child, psk, psk_len) ||
1691 !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) {
1692 CBB_cleanup(&pms_cbb);
1693 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1694 goto err;
1695 }
1696 OPENSSL_cleanse(pms, pms_len);
1697 OPENSSL_free(pms);
1698 pms = new_pms;
1699 pms_len = new_pms_len;
1700 }
1701
1702 /* The message must be added to the finished hash before calculating the
1703 * master secret. */
1704 size_t length;
1705 if (!CBB_finish(&cbb, NULL, &length) ||
1706 !ssl_set_handshake_header(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE, length)) {
1707 goto err;
1708 }
1709 ssl->state = SSL3_ST_CW_KEY_EXCH_B;
1710
David Benjaminf8d80712015-12-29 18:56:28 -05001711 ssl->session->master_key_length =
1712 tls1_generate_master_secret(ssl, ssl->session->master_key, pms, pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001713 if (ssl->session->master_key_length == 0) {
1714 goto err;
1715 }
1716 ssl->session->extended_master_secret = ssl->s3->tmp.extended_master_secret;
1717 OPENSSL_cleanse(pms, pms_len);
1718 OPENSSL_free(pms);
1719
Adam Langley1bea1732014-12-17 19:06:57 -08001720 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjaminae0eaaa2015-12-17 02:01:25 -05001721 return ssl_do_write(ssl);
Adam Langley1258b6a2014-06-20 12:00:00 -07001722
1723err:
David Benjamin2b07fa42016-03-02 00:23:57 -05001724 CBB_cleanup(&cbb);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001725 if (pms != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001726 OPENSSL_cleanse(pms, pms_len);
1727 OPENSSL_free(pms);
1728 }
1729 return -1;
1730}
Adam Langley1258b6a2014-06-20 12:00:00 -07001731
David Benjaminfc825122015-12-18 01:57:43 -05001732int ssl3_send_cert_verify(SSL *ssl) {
1733 if (ssl->state == SSL3_ST_CW_CERT_VRFY_C) {
1734 return ssl_do_write(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001735 }
1736
David Benjaminfc825122015-12-18 01:57:43 -05001737 CBB cbb, child;
1738 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1739 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) {
1740 goto err;
1741 }
1742
1743 assert(ssl_has_private_key(ssl));
1744
1745 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
1746 size_t sig_len;
1747 enum ssl_private_key_result_t sign_result;
1748 if (ssl->state == SSL3_ST_CW_CERT_VRFY_A) {
1749 /* Select and write out the digest type in TLS 1.2. */
1750 const EVP_MD *md = NULL;
David Benjamina1e9cab2015-12-30 00:08:49 -05001751 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminfc825122015-12-18 01:57:43 -05001752 md = tls1_choose_signing_digest(ssl);
1753 if (!tls12_add_sigandhash(ssl, &cbb, md)) {
1754 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1755 goto err;
1756 }
1757 }
1758
1759 /* Compute the digest. In TLS 1.1 and below, the digest type is also
1760 * selected here. */
1761 uint8_t digest[EVP_MAX_MD_SIZE];
1762 size_t digest_len;
1763 if (!ssl3_cert_verify_hash(ssl, digest, &digest_len, &md,
1764 ssl_private_key_type(ssl))) {
1765 goto err;
1766 }
1767
1768 /* The handshake buffer is no longer necessary. */
1769 ssl3_free_handshake_buffer(ssl);
1770
1771 /* Sign the digest. */
1772 uint8_t *ptr;
1773 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1774 !CBB_reserve(&child, &ptr, max_sig_len)) {
1775 goto err;
1776 }
1777 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, md,
1778 digest, digest_len);
1779 } else {
1780 assert(ssl->state == SSL3_ST_CW_CERT_VRFY_B);
1781
1782 /* Skip over the already written signature algorithm and retry the
1783 * signature. */
1784 uint8_t *ptr;
David Benjamina1e9cab2015-12-30 00:08:49 -05001785 if ((ssl3_protocol_version(ssl) >= TLS1_2_VERSION &&
1786 !CBB_did_write(&cbb, 2)) ||
David Benjaminfc825122015-12-18 01:57:43 -05001787 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1788 !CBB_reserve(&child, &ptr, max_sig_len)) {
1789 goto err;
1790 }
1791 sign_result =
1792 ssl_private_key_sign_complete(ssl, ptr, &sig_len, max_sig_len);
1793 }
1794
1795 switch (sign_result) {
1796 case ssl_private_key_success:
David Benjaminfc825122015-12-18 01:57:43 -05001797 break;
1798 case ssl_private_key_failure:
David Benjaminfc825122015-12-18 01:57:43 -05001799 goto err;
1800 case ssl_private_key_retry:
1801 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
1802 ssl->state = SSL3_ST_CW_CERT_VRFY_B;
1803 goto err;
1804 }
1805
1806 size_t length;
1807 if (!CBB_did_write(&child, sig_len) ||
1808 !CBB_finish(&cbb, NULL, &length) ||
1809 !ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_VERIFY, length)) {
1810 goto err;
1811 }
1812
1813 ssl->state = SSL3_ST_CW_CERT_VRFY_C;
1814 return ssl_do_write(ssl);
1815
1816err:
1817 CBB_cleanup(&cbb);
1818 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001819}
1820
1821/* ssl3_has_client_certificate returns true if a client certificate is
1822 * configured. */
David Benjamind1d80782015-07-05 11:54:09 -04001823static int ssl3_has_client_certificate(SSL *ssl) {
nagendra modadugu601448a2015-07-24 09:31:31 -07001824 return ssl->cert && ssl->cert->x509 && ssl_has_private_key(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001825}
1826
David Benjamin0d56f882015-12-19 17:05:56 -05001827int ssl3_send_client_certificate(SSL *ssl) {
David Benjamin0d56f882015-12-19 17:05:56 -05001828 if (ssl->state == SSL3_ST_CW_CERT_A) {
David Benjamin454aa4c2016-03-10 08:43:27 -05001829 /* Call cert_cb to update the certificate. */
David Benjamin0d56f882015-12-19 17:05:56 -05001830 if (ssl->cert->cert_cb) {
David Benjamin454aa4c2016-03-10 08:43:27 -05001831 int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1832 if (ret < 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001833 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langley1bea1732014-12-17 19:06:57 -08001834 return -1;
1835 }
David Benjamin454aa4c2016-03-10 08:43:27 -05001836 if (ret == 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001837 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin454aa4c2016-03-10 08:43:27 -05001838 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001839 }
Adam Langley1bea1732014-12-17 19:06:57 -08001840 }
1841
David Benjamin0d56f882015-12-19 17:05:56 -05001842 if (ssl3_has_client_certificate(ssl)) {
1843 ssl->state = SSL3_ST_CW_CERT_C;
Adam Langley1bea1732014-12-17 19:06:57 -08001844 } else {
David Benjamin0d56f882015-12-19 17:05:56 -05001845 ssl->state = SSL3_ST_CW_CERT_B;
Adam Langley1bea1732014-12-17 19:06:57 -08001846 }
1847 }
1848
David Benjamin0d56f882015-12-19 17:05:56 -05001849 if (ssl->state == SSL3_ST_CW_CERT_B) {
David Benjamin454aa4c2016-03-10 08:43:27 -05001850 /* Call client_cert_cb to update the certificate. */
1851 X509 *x509 = NULL;
1852 EVP_PKEY *pkey = NULL;
1853 int ret = ssl_do_client_cert_cb(ssl, &x509, &pkey);
1854 if (ret < 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001855 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langley1bea1732014-12-17 19:06:57 -08001856 return -1;
1857 }
Adam Langley1bea1732014-12-17 19:06:57 -08001858
David Benjamin454aa4c2016-03-10 08:43:27 -05001859 int setup_error = ret == 1 && (!SSL_use_certificate(ssl, x509) ||
1860 !SSL_use_PrivateKey(ssl, pkey));
David Benjamin2755a3e2015-04-22 16:17:58 -04001861 X509_free(x509);
1862 EVP_PKEY_free(pkey);
David Benjamin454aa4c2016-03-10 08:43:27 -05001863 if (setup_error) {
1864 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1865 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001866 }
1867
David Benjamin0d56f882015-12-19 17:05:56 -05001868 ssl->state = SSL3_ST_CW_CERT_C;
Adam Langley1bea1732014-12-17 19:06:57 -08001869 }
1870
David Benjamin0d56f882015-12-19 17:05:56 -05001871 if (ssl->state == SSL3_ST_CW_CERT_C) {
David Benjamin454aa4c2016-03-10 08:43:27 -05001872 if (!ssl3_has_client_certificate(ssl)) {
1873 /* Without a client certificate, the handshake buffer may be released. */
1874 ssl3_free_handshake_buffer(ssl);
1875
1876 if (ssl->version == SSL3_VERSION) {
1877 /* In SSL 3.0, send no certificate by skipping both messages. */
1878 ssl->s3->tmp.cert_req = 0;
1879 ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
1880 return 1;
1881 }
1882
1883 /* In TLS, send an empty Certificate message. */
1884 ssl->s3->tmp.cert_req = 2;
David Benjamin0d56f882015-12-19 17:05:56 -05001885 uint8_t *p = ssl_handshake_start(ssl);
David Benjamind1d80782015-07-05 11:54:09 -04001886 l2n3(0, p);
David Benjamin0d56f882015-12-19 17:05:56 -05001887 if (!ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE, 3)) {
David Benjamind1d80782015-07-05 11:54:09 -04001888 return -1;
1889 }
David Benjamin0d56f882015-12-19 17:05:56 -05001890 } else if (!ssl3_output_cert_chain(ssl)) {
David Benjamin9d0847a2015-02-16 03:57:55 -05001891 return -1;
1892 }
David Benjamin0d56f882015-12-19 17:05:56 -05001893 ssl->state = SSL3_ST_CW_CERT_D;
Adam Langley1bea1732014-12-17 19:06:57 -08001894 }
1895
David Benjamin454aa4c2016-03-10 08:43:27 -05001896 assert(ssl->state == SSL3_ST_CW_CERT_D);
David Benjamin0d56f882015-12-19 17:05:56 -05001897 return ssl_do_write(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001898}
1899
David Benjamine97b9622015-10-10 14:59:09 -04001900int ssl3_send_next_proto(SSL *ssl) {
1901 if (ssl->state == SSL3_ST_CW_NEXT_PROTO_B) {
1902 return ssl_do_write(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001903 }
1904
David Benjamine97b9622015-10-10 14:59:09 -04001905 assert(ssl->state == SSL3_ST_CW_NEXT_PROTO_A);
1906
1907 static const uint8_t kZero[32] = {0};
David Benjamin79978df2015-12-25 15:56:49 -05001908 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
David Benjamine97b9622015-10-10 14:59:09 -04001909
1910 CBB cbb, child;
1911 size_t length;
1912 CBB_zero(&cbb);
1913 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1914 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1915 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjamin79978df2015-12-25 15:56:49 -05001916 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1917 ssl->s3->next_proto_negotiated_len) ||
David Benjamine97b9622015-10-10 14:59:09 -04001918 !CBB_add_u8_length_prefixed(&cbb, &child) ||
1919 !CBB_add_bytes(&child, kZero, padding_len) ||
1920 !CBB_finish(&cbb, NULL, &length) ||
1921 !ssl_set_handshake_header(ssl, SSL3_MT_NEXT_PROTO, length)) {
1922 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1923 CBB_cleanup(&cbb);
1924 return -1;
1925 }
1926
1927 ssl->state = SSL3_ST_CW_NEXT_PROTO_B;
1928 return ssl_do_write(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001929}
1930
David Benjamin96e97b12015-10-10 16:30:22 -04001931int ssl3_send_channel_id(SSL *ssl) {
1932 if (ssl->state == SSL3_ST_CW_CHANNEL_ID_B) {
1933 return ssl_do_write(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001934 }
1935
David Benjamin96e97b12015-10-10 16:30:22 -04001936 assert(ssl->state == SSL3_ST_CW_CHANNEL_ID_A);
1937
1938 if (ssl->tlsext_channel_id_private == NULL &&
1939 ssl->ctx->channel_id_cb != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001940 EVP_PKEY *key = NULL;
David Benjamin96e97b12015-10-10 16:30:22 -04001941 ssl->ctx->channel_id_cb(ssl, &key);
1942 if (key != NULL &&
1943 !SSL_set1_tls_channel_id(ssl, key)) {
1944 EVP_PKEY_free(key);
1945 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001946 }
David Benjamin96e97b12015-10-10 16:30:22 -04001947 EVP_PKEY_free(key);
Adam Langley1bea1732014-12-17 19:06:57 -08001948 }
1949
David Benjamin96e97b12015-10-10 16:30:22 -04001950 if (ssl->tlsext_channel_id_private == NULL) {
1951 ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
Adam Langley1bea1732014-12-17 19:06:57 -08001952 return -1;
1953 }
Adam Langley1bea1732014-12-17 19:06:57 -08001954
David Benjamin758d1272015-11-20 17:47:25 -05001955 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
1956 if (ec_key == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001957 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin961ad6a2015-06-12 01:40:23 -04001958 return -1;
1959 }
David Benjamin961ad6a2015-06-12 01:40:23 -04001960
David Benjamin96e97b12015-10-10 16:30:22 -04001961 int ret = -1;
David Benjamin96e97b12015-10-10 16:30:22 -04001962 BIGNUM *x = BN_new();
1963 BIGNUM *y = BN_new();
1964 ECDSA_SIG *sig = NULL;
1965 if (x == NULL || y == NULL ||
1966 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
1967 EC_KEY_get0_public_key(ec_key),
1968 x, y, NULL)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001969 goto err;
1970 }
1971
David Benjamin961ad6a2015-06-12 01:40:23 -04001972 uint8_t digest[EVP_MAX_MD_SIZE];
David Benjamind6a4ae92015-08-06 11:10:51 -04001973 size_t digest_len;
David Benjamin96e97b12015-10-10 16:30:22 -04001974 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001975 goto err;
1976 }
1977
David Benjamin961ad6a2015-06-12 01:40:23 -04001978 sig = ECDSA_do_sign(digest, digest_len, ec_key);
Adam Langley1bea1732014-12-17 19:06:57 -08001979 if (sig == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001980 goto err;
1981 }
1982
David Benjamin96e97b12015-10-10 16:30:22 -04001983 CBB cbb, child;
1984 size_t length;
1985 CBB_zero(&cbb);
1986 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1987 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1988 !CBB_add_u16(&cbb, TLSEXT_TYPE_channel_id) ||
1989 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001990 !BN_bn2cbb_padded(&child, 32, x) ||
1991 !BN_bn2cbb_padded(&child, 32, y) ||
1992 !BN_bn2cbb_padded(&child, 32, sig->r) ||
1993 !BN_bn2cbb_padded(&child, 32, sig->s) ||
David Benjamin96e97b12015-10-10 16:30:22 -04001994 !CBB_finish(&cbb, NULL, &length) ||
Steven Valdez1eca1d32016-05-23 16:29:25 -04001995 !ssl_set_handshake_header(ssl, SSL3_MT_CHANNEL_ID_ENCRYPTED_EXTENSIONS,
1996 length)) {
David Benjamin3570d732015-06-29 00:28:17 -04001997 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin96e97b12015-10-10 16:30:22 -04001998 CBB_cleanup(&cbb);
Adam Langley1bea1732014-12-17 19:06:57 -08001999 goto err;
2000 }
2001
David Benjamin96e97b12015-10-10 16:30:22 -04002002 ssl->state = SSL3_ST_CW_CHANNEL_ID_B;
2003 ret = ssl_do_write(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08002004
2005err:
David Benjamin96e97b12015-10-10 16:30:22 -04002006 BN_free(x);
2007 BN_free(y);
David Benjamin2755a3e2015-04-22 16:17:58 -04002008 ECDSA_SIG_free(sig);
Adam Langley1bea1732014-12-17 19:06:57 -08002009 return ret;
2010}
2011
David Benjaminfd8e69f2015-10-03 10:49:20 -04002012int ssl_do_client_cert_cb(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey) {
2013 if (ssl->ctx->client_cert_cb == NULL) {
2014 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -08002015 }
David Benjamin454aa4c2016-03-10 08:43:27 -05002016
2017 int ret = ssl->ctx->client_cert_cb(ssl, out_x509, out_pkey);
2018 if (ret <= 0) {
2019 return ret;
2020 }
2021
2022 assert(*out_x509 != NULL);
2023 assert(*out_pkey != NULL);
2024 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08002025}
Paul Lietar8f1c2682015-08-18 12:21:54 +01002026
David Benjamin0d56f882015-12-19 17:05:56 -05002027int ssl3_verify_server_cert(SSL *ssl) {
2028 int ret = ssl_verify_cert_chain(ssl, ssl->session->cert_chain);
2029 if (ssl->verify_mode != SSL_VERIFY_NONE && ret <= 0) {
2030 int al = ssl_verify_alarm_type(ssl->verify_result);
2031 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Paul Lietar8f1c2682015-08-18 12:21:54 +01002032 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
2033 } else {
2034 ret = 1;
David Benjamin0d56f882015-12-19 17:05:56 -05002035 ERR_clear_error(); /* but we keep ssl->verify_result */
Paul Lietar8f1c2682015-08-18 12:21:54 +01002036 }
2037
2038 return ret;
2039}