blob: 9f92849591019eee38378003a356a11d8a67dda0 [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 }
Adam Langley95c29f32014-06-20 12:00:00 -0700211
David Benjaminb095f0f2016-05-05 21:50:24 -0400212 if (!ssl_init_wbio_buffer(ssl)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800213 ret = -1;
214 goto end;
215 }
Adam Langley95c29f32014-06-20 12:00:00 -0700216
David Benjamin0d56f882015-12-19 17:05:56 -0500217 if (!ssl3_init_handshake_buffer(ssl)) {
David Benjamin3570d732015-06-29 00:28:17 -0400218 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800219 ret = -1;
220 goto end;
221 }
Adam Langley95c29f32014-06-20 12:00:00 -0700222
David Benjamin0d56f882015-12-19 17:05:56 -0500223 ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
224 ssl->init_num = 0;
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;
David Benjamin0d56f882015-12-19 17:05:56 -0500235 ssl->init_num = 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800236 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700237
Adam Langley1bea1732014-12-17 19:06:57 -0800238 case SSL3_ST_CR_SRVR_HELLO_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500239 ret = ssl3_get_server_hello(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800240 if (ret <= 0) {
241 goto end;
242 }
Adam Langley95c29f32014-06-20 12:00:00 -0700243
David Benjamin0d56f882015-12-19 17:05:56 -0500244 if (ssl->hit) {
245 ssl->state = SSL3_ST_CR_CHANGE;
246 if (ssl->tlsext_ticket_expected) {
Adam Langley1bea1732014-12-17 19:06:57 -0800247 /* receive renewed session ticket */
David Benjamin0d56f882015-12-19 17:05:56 -0500248 ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800249 }
250 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500251 ssl->state = SSL3_ST_CR_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800252 }
David Benjamin0d56f882015-12-19 17:05:56 -0500253 ssl->init_num = 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800254 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400255
Adam Langley1bea1732014-12-17 19:06:57 -0800256 case SSL3_ST_CR_CERT_A:
David Benjaminc032dfa2016-05-12 14:54:57 -0400257 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500258 ret = ssl3_get_server_certificate(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800259 if (ret <= 0) {
260 goto end;
261 }
David Benjamin0d56f882015-12-19 17:05:56 -0500262 if (ssl->s3->tmp.certificate_status_expected) {
263 ssl->state = SSL3_ST_CR_CERT_STATUS_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800264 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500265 ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
Adam Langley1bea1732014-12-17 19:06:57 -0800266 }
267 } else {
268 skip = 1;
David Benjamin0d56f882015-12-19 17:05:56 -0500269 ssl->state = SSL3_ST_CR_KEY_EXCH_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800270 }
David Benjamin0d56f882015-12-19 17:05:56 -0500271 ssl->init_num = 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800272 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700273
Paul Lietar8f1c2682015-08-18 12:21:54 +0100274 case SSL3_ST_VERIFY_SERVER_CERT:
David Benjamin0d56f882015-12-19 17:05:56 -0500275 ret = ssl3_verify_server_cert(ssl);
Paul Lietar8f1c2682015-08-18 12:21:54 +0100276 if (ret <= 0) {
277 goto end;
278 }
279
David Benjamin0d56f882015-12-19 17:05:56 -0500280 ssl->state = SSL3_ST_CR_KEY_EXCH_A;
281 ssl->init_num = 0;
Paul Lietar8f1c2682015-08-18 12:21:54 +0100282 break;
283
Adam Langley1bea1732014-12-17 19:06:57 -0800284 case SSL3_ST_CR_KEY_EXCH_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500285 ret = ssl3_get_server_key_exchange(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800286 if (ret <= 0) {
287 goto end;
288 }
David Benjaminc032dfa2016-05-12 14:54:57 -0400289 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
290 ssl->state = SSL3_ST_CR_CERT_REQ_A;
291 } else {
292 ssl->state = SSL3_ST_CR_SRVR_DONE_A;
293 }
David Benjamin0d56f882015-12-19 17:05:56 -0500294 ssl->init_num = 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800295 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700296
Adam Langley1bea1732014-12-17 19:06:57 -0800297 case SSL3_ST_CR_CERT_REQ_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500298 ret = ssl3_get_certificate_request(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800299 if (ret <= 0) {
300 goto end;
301 }
David Benjamin0d56f882015-12-19 17:05:56 -0500302 ssl->state = SSL3_ST_CR_SRVR_DONE_A;
303 ssl->init_num = 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800304 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700305
Adam Langley1bea1732014-12-17 19:06:57 -0800306 case SSL3_ST_CR_SRVR_DONE_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500307 ret = ssl3_get_server_done(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800308 if (ret <= 0) {
309 goto end;
310 }
David Benjamin0d56f882015-12-19 17:05:56 -0500311 if (ssl->s3->tmp.cert_req) {
312 ssl->state = SSL3_ST_CW_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800313 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500314 ssl->state = SSL3_ST_CW_KEY_EXCH_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800315 }
David Benjamin0d56f882015-12-19 17:05:56 -0500316 ssl->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Adam Langley1bea1732014-12-17 19:06:57 -0800318 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700319
Adam Langley1bea1732014-12-17 19:06:57 -0800320 case SSL3_ST_CW_CERT_A:
321 case SSL3_ST_CW_CERT_B:
322 case SSL3_ST_CW_CERT_C:
323 case SSL3_ST_CW_CERT_D:
David Benjamin0d56f882015-12-19 17:05:56 -0500324 ret = ssl3_send_client_certificate(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800325 if (ret <= 0) {
326 goto end;
327 }
David Benjamin0d56f882015-12-19 17:05:56 -0500328 ssl->state = SSL3_ST_CW_KEY_EXCH_A;
329 ssl->init_num = 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800330 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700331
Adam Langley1bea1732014-12-17 19:06:57 -0800332 case SSL3_ST_CW_KEY_EXCH_A:
333 case SSL3_ST_CW_KEY_EXCH_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500334 ret = ssl3_send_client_key_exchange(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800335 if (ret <= 0) {
336 goto end;
337 }
338 /* For TLS, cert_req is set to 2, so a cert chain
339 * of nothing is sent, but no verify packet is sent */
David Benjamin0d56f882015-12-19 17:05:56 -0500340 if (ssl->s3->tmp.cert_req == 1) {
341 ssl->state = SSL3_ST_CW_CERT_VRFY_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800342 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500343 ssl->state = SSL3_ST_CW_CHANGE_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800344 }
Adam Langley95c29f32014-06-20 12:00:00 -0700345
David Benjamin0d56f882015-12-19 17:05:56 -0500346 ssl->init_num = 0;
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_CERT_VRFY_A:
350 case SSL3_ST_CW_CERT_VRFY_B:
David Benjaminb4d65fd2015-05-29 17:11:21 -0400351 case SSL3_ST_CW_CERT_VRFY_C:
David Benjamin0d56f882015-12-19 17:05:56 -0500352 ret = ssl3_send_cert_verify(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800353 if (ret <= 0) {
354 goto end;
355 }
David Benjamin0d56f882015-12-19 17:05:56 -0500356 ssl->state = SSL3_ST_CW_CHANGE_A;
357 ssl->init_num = 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800358 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700359
Adam Langley1bea1732014-12-17 19:06:57 -0800360 case SSL3_ST_CW_CHANGE_A:
361 case SSL3_ST_CW_CHANGE_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500362 ret = ssl3_send_change_cipher_spec(ssl, SSL3_ST_CW_CHANGE_A,
Adam Langley1bea1732014-12-17 19:06:57 -0800363 SSL3_ST_CW_CHANGE_B);
364 if (ret <= 0) {
365 goto end;
366 }
Adam Langley95c29f32014-06-20 12:00:00 -0700367
David Benjamin0d56f882015-12-19 17:05:56 -0500368 ssl->state = SSL3_ST_CW_FINISHED_A;
369 if (ssl->s3->tlsext_channel_id_valid) {
370 ssl->state = SSL3_ST_CW_CHANNEL_ID_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800371 }
David Benjamin0d56f882015-12-19 17:05:56 -0500372 if (ssl->s3->next_proto_neg_seen) {
373 ssl->state = SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800374 }
David Benjamin0d56f882015-12-19 17:05:56 -0500375 ssl->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700376
David Benjamin57997da2015-12-25 15:48:39 -0500377 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800378 ret = -1;
379 goto end;
380 }
Adam Langley95c29f32014-06-20 12:00:00 -0700381
Adam Langley1bea1732014-12-17 19:06:57 -0800382 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700383
Adam Langley1bea1732014-12-17 19:06:57 -0800384 case SSL3_ST_CW_NEXT_PROTO_A:
385 case SSL3_ST_CW_NEXT_PROTO_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500386 ret = ssl3_send_next_proto(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800387 if (ret <= 0) {
388 goto end;
389 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700390
David Benjamin0d56f882015-12-19 17:05:56 -0500391 if (ssl->s3->tlsext_channel_id_valid) {
392 ssl->state = SSL3_ST_CW_CHANNEL_ID_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800393 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500394 ssl->state = SSL3_ST_CW_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800395 }
396 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700397
Adam Langley1bea1732014-12-17 19:06:57 -0800398 case SSL3_ST_CW_CHANNEL_ID_A:
399 case SSL3_ST_CW_CHANNEL_ID_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500400 ret = ssl3_send_channel_id(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800401 if (ret <= 0) {
402 goto end;
403 }
David Benjamin0d56f882015-12-19 17:05:56 -0500404 ssl->state = SSL3_ST_CW_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800405 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700406
Adam Langley1bea1732014-12-17 19:06:57 -0800407 case SSL3_ST_CW_FINISHED_A:
408 case SSL3_ST_CW_FINISHED_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500409 ret = ssl3_send_finished(ssl, SSL3_ST_CW_FINISHED_A,
David Benjaminbaa12162015-12-29 19:13:58 -0500410 SSL3_ST_CW_FINISHED_B);
Adam Langley1bea1732014-12-17 19:06:57 -0800411 if (ret <= 0) {
412 goto end;
413 }
David Benjamin0d56f882015-12-19 17:05:56 -0500414 ssl->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700415
David Benjamin0d56f882015-12-19 17:05:56 -0500416 if (ssl->hit) {
417 ssl->s3->tmp.next_state = SSL_ST_OK;
Adam Langley1bea1732014-12-17 19:06:57 -0800418 } else {
419 /* This is a non-resumption handshake. If it involves ChannelID, then
420 * record the handshake hashes at this point in the session so that
421 * any resumption of this session with ChannelID can sign those
422 * hashes. */
David Benjamin0d56f882015-12-19 17:05:56 -0500423 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Adam Langley49c7af12015-07-10 14:33:46 -0700424 if (ret <= 0) {
425 goto end;
Adam Langley1bea1732014-12-17 19:06:57 -0800426 }
David Benjamin0d56f882015-12-19 17:05:56 -0500427 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
428 ssl3_can_false_start(ssl) &&
David Benjamined7c4752015-02-16 19:16:46 -0500429 /* No False Start on renegotiation (would complicate the state
430 * machine). */
David Benjamin0d56f882015-12-19 17:05:56 -0500431 !ssl->s3->initial_handshake_complete) {
432 ssl->s3->tmp.next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800433 } else {
434 /* Allow NewSessionTicket if ticket expected */
David Benjamin0d56f882015-12-19 17:05:56 -0500435 if (ssl->tlsext_ticket_expected) {
436 ssl->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800437 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500438 ssl->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800439 }
440 }
441 }
David Benjamin0d56f882015-12-19 17:05:56 -0500442 ssl->init_num = 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800443 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700444
Adam Langley1bea1732014-12-17 19:06:57 -0800445 case SSL3_ST_CR_SESSION_TICKET_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500446 ret = ssl3_get_new_session_ticket(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800447 if (ret <= 0) {
448 goto end;
449 }
David Benjamin0d56f882015-12-19 17:05:56 -0500450 ssl->state = SSL3_ST_CR_CHANGE;
451 ssl->init_num = 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800452 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700453
Adam Langley1bea1732014-12-17 19:06:57 -0800454 case SSL3_ST_CR_CERT_STATUS_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500455 ret = ssl3_get_cert_status(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800456 if (ret <= 0) {
457 goto end;
458 }
David Benjamin0d56f882015-12-19 17:05:56 -0500459 ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
460 ssl->init_num = 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800461 break;
David Benjamincb5abad2014-07-25 12:14:28 -0400462
Adam Langley1bea1732014-12-17 19:06:57 -0800463 case SSL3_ST_CR_CHANGE:
David Benjamin0d56f882015-12-19 17:05:56 -0500464 ret = ssl->method->ssl_read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500465 if (ret <= 0) {
466 goto end;
467 }
468
David Benjamin57997da2015-12-25 15:48:39 -0500469 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800470 ret = -1;
471 goto end;
472 }
David Benjamin0d56f882015-12-19 17:05:56 -0500473 ssl->state = SSL3_ST_CR_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800474 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700475
Adam Langley1bea1732014-12-17 19:06:57 -0800476 case SSL3_ST_CR_FINISHED_A:
David Benjamin1e6d6df2016-05-13 18:28:17 -0400477 ret = ssl3_get_finished(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800478 if (ret <= 0) {
479 goto end;
480 }
Adam Langley95c29f32014-06-20 12:00:00 -0700481
David Benjamin0d56f882015-12-19 17:05:56 -0500482 if (ssl->hit) {
483 ssl->state = SSL3_ST_CW_CHANGE_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800484 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500485 ssl->state = SSL_ST_OK;
Adam Langley1bea1732014-12-17 19:06:57 -0800486 }
David Benjamin0d56f882015-12-19 17:05:56 -0500487 ssl->init_num = 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800488 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700489
Adam Langley1bea1732014-12-17 19:06:57 -0800490 case SSL3_ST_CW_FLUSH:
David Benjamin0d56f882015-12-19 17:05:56 -0500491 if (BIO_flush(ssl->wbio) <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500492 ssl->rwstate = SSL_WRITING;
Adam Langley1bea1732014-12-17 19:06:57 -0800493 ret = -1;
494 goto end;
495 }
David Benjamin0d56f882015-12-19 17:05:56 -0500496 ssl->state = ssl->s3->tmp.next_state;
Adam Langley1bea1732014-12-17 19:06:57 -0800497 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700498
David Benjamined7c4752015-02-16 19:16:46 -0500499 case SSL3_ST_FALSE_START:
Adam Langley1bea1732014-12-17 19:06:57 -0800500 /* Allow NewSessionTicket if ticket expected */
David Benjamin0d56f882015-12-19 17:05:56 -0500501 if (ssl->tlsext_ticket_expected) {
502 ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800503 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500504 ssl->state = SSL3_ST_CR_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800505 }
David Benjamin0d56f882015-12-19 17:05:56 -0500506 ssl->s3->tmp.in_false_start = 1;
Adam Langleyadb739e2014-06-20 12:00:00 -0700507
David Benjamin0d56f882015-12-19 17:05:56 -0500508 ssl_free_wbio_buffer(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800509 ret = 1;
510 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700511
Adam Langley1bea1732014-12-17 19:06:57 -0800512 case SSL_ST_OK:
513 /* clean a few things up */
David Benjamin0d56f882015-12-19 17:05:56 -0500514 ssl3_cleanup_key_block(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700515
David Benjamin0d56f882015-12-19 17:05:56 -0500516 BUF_MEM_free(ssl->init_buf);
517 ssl->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700518
Adam Langley1bea1732014-12-17 19:06:57 -0800519 /* Remove write buffering now. */
David Benjamin0d56f882015-12-19 17:05:56 -0500520 ssl_free_wbio_buffer(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700521
David Benjamin0d56f882015-12-19 17:05:56 -0500522 const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
David Benjaminba4594a2015-06-18 18:36:15 -0400523
David Benjamin0d56f882015-12-19 17:05:56 -0500524 ssl->init_num = 0;
525 ssl->s3->tmp.in_false_start = 0;
526 ssl->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700527
David Benjaminba4594a2015-06-18 18:36:15 -0400528 if (is_initial_handshake) {
529 /* Renegotiations do not participate in session resumption. */
David Benjamin0d56f882015-12-19 17:05:56 -0500530 ssl_update_cache(ssl, SSL_SESS_CACHE_CLIENT);
David Benjaminba4594a2015-06-18 18:36:15 -0400531 }
Adam Langley95c29f32014-06-20 12:00:00 -0700532
Adam Langley1bea1732014-12-17 19:06:57 -0800533 ret = 1;
David Benjamin0d56f882015-12-19 17:05:56 -0500534 /* ssl->server=0; */
Adam Langley95c29f32014-06-20 12:00:00 -0700535
Adam Langley1bea1732014-12-17 19:06:57 -0800536 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500537 cb(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800538 }
Adam Langley95c29f32014-06-20 12:00:00 -0700539
Adam Langley1bea1732014-12-17 19:06:57 -0800540 goto end;
541
542 default:
David Benjamin3570d732015-06-29 00:28:17 -0400543 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley1bea1732014-12-17 19:06:57 -0800544 ret = -1;
545 goto end;
546 }
547
David Benjamin0d56f882015-12-19 17:05:56 -0500548 if (!ssl->s3->tmp.reuse_message && !skip) {
549 if (cb != NULL && ssl->state != state) {
550 new_state = ssl->state;
551 ssl->state = state;
552 cb(ssl, SSL_CB_CONNECT_LOOP, 1);
553 ssl->state = new_state;
Adam Langley1bea1732014-12-17 19:06:57 -0800554 }
555 }
556 skip = 0;
557 }
558
Adam Langley95c29f32014-06-20 12:00:00 -0700559end:
David Benjamin2755a3e2015-04-22 16:17:58 -0400560 BUF_MEM_free(buf);
Adam Langley1bea1732014-12-17 19:06:57 -0800561 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500562 cb(ssl, SSL_CB_CONNECT_EXIT, ret);
Adam Langley1bea1732014-12-17 19:06:57 -0800563 }
564 return ret;
565}
Adam Langley95c29f32014-06-20 12:00:00 -0700566
David Benjamine8d53502015-10-10 14:13:23 -0400567static int ssl3_write_client_cipher_list(SSL *ssl, CBB *out) {
568 /* Prepare disabled cipher masks. */
569 ssl_set_client_disabled(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700570
David Benjamine8d53502015-10-10 14:13:23 -0400571 CBB child;
572 if (!CBB_add_u16_length_prefixed(out, &child)) {
573 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800574 }
575
David Benjamine8d53502015-10-10 14:13:23 -0400576 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
577
578 int any_enabled = 0;
579 size_t i;
580 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
581 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
582 /* Skip disabled ciphers */
David Benjaminc7817d82015-11-05 18:28:33 -0500583 if ((cipher->algorithm_mkey & ssl->cert->mask_k) ||
584 (cipher->algorithm_auth & ssl->cert->mask_a)) {
585 continue;
586 }
587 if (SSL_CIPHER_get_min_version(cipher) >
588 ssl3_version_from_wire(ssl, ssl->client_version)) {
David Benjamine8d53502015-10-10 14:13:23 -0400589 continue;
590 }
591 any_enabled = 1;
592 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
593 return 0;
594 }
595 }
596
597 /* If all ciphers were disabled, return the error to the caller. */
598 if (!any_enabled) {
599 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
600 return 0;
601 }
602
603 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
604 * added. */
605 if (ssl->client_version == SSL3_VERSION &&
606 !ssl->s3->initial_handshake_complete) {
607 if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
608 return 0;
609 }
610 /* The renegotiation extension is required to be at index zero. */
611 ssl->s3->tmp.extensions.sent |= (1u << 0);
612 }
613
614 if ((ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) &&
615 !CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
616 return 0;
617 }
618
619 return CBB_flush(out);
620}
621
622int ssl3_send_client_hello(SSL *ssl) {
623 if (ssl->state == SSL3_ST_CW_CLNT_HELLO_B) {
624 return ssl_do_write(ssl);
625 }
626
David Benjamin13e81fc2015-11-02 17:16:13 -0500627 /* In DTLS, reset the handshake buffer each time a new ClientHello is
628 * assembled. We may send multiple if we receive HelloVerifyRequest. */
629 if (SSL_IS_DTLS(ssl) && !ssl3_init_handshake_buffer(ssl)) {
630 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
631 return -1;
632 }
633
David Benjamine8d53502015-10-10 14:13:23 -0400634 CBB cbb;
635 CBB_zero(&cbb);
636
637 assert(ssl->state == SSL3_ST_CW_CLNT_HELLO_A);
638 if (!ssl->s3->have_version) {
639 uint16_t max_version = ssl3_get_max_client_version(ssl);
640 /* Disabling all versions is silly: return an error. */
641 if (max_version == 0) {
642 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
643 goto err;
644 }
645
646 ssl->version = max_version;
647 /* Only set |ssl->client_version| on the initial handshake. Renegotiations,
648 * although locked to a version, reuse the value. When using the plain RSA
649 * key exchange, the ClientHello version is checked in the premaster secret.
650 * Some servers fail when this value changes. */
651 ssl->client_version = max_version;
652 }
653
David Benjamine29ea162016-03-23 16:10:44 -0400654 /* If the configured session has expired or was created at a disabled
655 * version, drop it. */
David Benjamine8d53502015-10-10 14:13:23 -0400656 if (ssl->session != NULL &&
657 (ssl->session->session_id_length == 0 || ssl->session->not_resumable ||
David Benjamin1269ddd2015-10-18 15:18:55 -0400658 ssl->session->timeout < (long)(time(NULL) - ssl->session->time) ||
David Benjamine29ea162016-03-23 16:10:44 -0400659 !ssl3_is_version_enabled(ssl, ssl->session->ssl_version))) {
David Benjamine8d53502015-10-10 14:13:23 -0400660 SSL_set_session(ssl, NULL);
661 }
662
663 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
664 * renegerate the client_random. The random must be reused. */
665 if ((!SSL_IS_DTLS(ssl) || !ssl->d1->send_cookie) &&
666 !ssl_fill_hello_random(ssl->s3->client_random,
667 sizeof(ssl->s3->client_random), 0 /* client */)) {
668 goto err;
669 }
670
671 /* Renegotiations do not participate in session resumption. */
672 int has_session = ssl->session != NULL &&
673 !ssl->s3->initial_handshake_complete;
674
675 CBB child;
676 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
677 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
678 !CBB_add_u16(&cbb, ssl->client_version) ||
679 !CBB_add_bytes(&cbb, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
680 !CBB_add_u8_length_prefixed(&cbb, &child) ||
681 (has_session &&
682 !CBB_add_bytes(&child, ssl->session->session_id,
683 ssl->session->session_id_length))) {
684 goto err;
685 }
686
687 if (SSL_IS_DTLS(ssl)) {
688 if (!CBB_add_u8_length_prefixed(&cbb, &child) ||
689 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
690 goto err;
691 }
692 }
693
694 size_t length;
695 if (!ssl3_write_client_cipher_list(ssl, &cbb) ||
696 !CBB_add_u8(&cbb, 1 /* one compression method */) ||
697 !CBB_add_u8(&cbb, 0 /* null compression */) ||
698 !ssl_add_clienthello_tlsext(ssl, &cbb,
699 CBB_len(&cbb) + SSL_HM_HEADER_LENGTH(ssl)) ||
700 !CBB_finish(&cbb, NULL, &length) ||
701 !ssl_set_handshake_header(ssl, SSL3_MT_CLIENT_HELLO, length)) {
702 goto err;
703 }
704
705 ssl->state = SSL3_ST_CW_CLNT_HELLO_B;
706 return ssl_do_write(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800707
Adam Langley95c29f32014-06-20 12:00:00 -0700708err:
David Benjamine8d53502015-10-10 14:13:23 -0400709 CBB_cleanup(&cbb);
Adam Langley1bea1732014-12-17 19:06:57 -0800710 return -1;
711}
Adam Langley95c29f32014-06-20 12:00:00 -0700712
David Benjamin0d56f882015-12-19 17:05:56 -0500713int ssl3_get_server_hello(SSL *ssl) {
David Benjamin60da0cd2015-05-03 15:21:28 -0400714 STACK_OF(SSL_CIPHER) *sk;
Adam Langley1bea1732014-12-17 19:06:57 -0800715 const SSL_CIPHER *c;
David Benjamin0d56f882015-12-19 17:05:56 -0500716 CERT *ct = ssl->cert;
Adam Langley1bea1732014-12-17 19:06:57 -0800717 int al = SSL_AD_INTERNAL_ERROR, ok;
718 long n;
719 CBS server_hello, server_random, session_id;
720 uint16_t server_version, cipher_suite;
721 uint8_t compression_method;
Adam Langley95c29f32014-06-20 12:00:00 -0700722
David Benjamin1e6d6df2016-05-13 18:28:17 -0400723 n = ssl->method->ssl_get_message(ssl, SSL3_MT_SERVER_HELLO, ssl_hash_message,
724 &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700725
Adam Langley1bea1732014-12-17 19:06:57 -0800726 if (!ok) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500727 uint32_t err = ERR_peek_error();
728 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
729 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
730 /* Add a dedicated error code to the queue for a handshake_failure alert
731 * in response to ClientHello. This matches NSS's client behavior and
732 * gives a better error on a (probable) failure to negotiate initial
733 * parameters. Note: this error code comes after the original one.
734 *
735 * See https://crbug.com/446505. */
David Benjamin3570d732015-06-29 00:28:17 -0400736 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
David Benjamin780d6dd2015-01-06 12:03:19 -0500737 }
Adam Langley1bea1732014-12-17 19:06:57 -0800738 return n;
739 }
Adam Langley95c29f32014-06-20 12:00:00 -0700740
David Benjamin0d56f882015-12-19 17:05:56 -0500741 CBS_init(&server_hello, ssl->init_msg, n);
David Benjamina03d95d2014-07-12 19:49:07 -0400742
Adam Langley1bea1732014-12-17 19:06:57 -0800743 if (!CBS_get_u16(&server_hello, &server_version) ||
744 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
745 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
746 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
747 !CBS_get_u16(&server_hello, &cipher_suite) ||
748 !CBS_get_u8(&server_hello, &compression_method)) {
749 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400750 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800751 goto f_err;
752 }
David Benjamina03d95d2014-07-12 19:49:07 -0400753
David Benjamin0d56f882015-12-19 17:05:56 -0500754 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
755 if (!ssl->s3->have_version) {
756 if (!ssl3_is_version_enabled(ssl, server_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400757 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
David Benjamin0d56f882015-12-19 17:05:56 -0500758 ssl->version = server_version;
Adam Langley1bea1732014-12-17 19:06:57 -0800759 /* Mark the version as fixed so the record-layer version is not clamped
760 * to TLS 1.0. */
David Benjamin0d56f882015-12-19 17:05:56 -0500761 ssl->s3->have_version = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800762 al = SSL_AD_PROTOCOL_VERSION;
763 goto f_err;
764 }
David Benjamin0d56f882015-12-19 17:05:56 -0500765 ssl->version = server_version;
David Benjaminb83003e2015-12-30 17:39:26 -0500766 ssl->s3->enc_method = ssl3_get_enc_method(server_version);
767 assert(ssl->s3->enc_method != NULL);
David Benjamin0d56f882015-12-19 17:05:56 -0500768 /* At this point, the connection's version is known and ssl->version is
Adam Langley1bea1732014-12-17 19:06:57 -0800769 * fixed. Begin enforcing the record-layer version. */
David Benjamin0d56f882015-12-19 17:05:56 -0500770 ssl->s3->have_version = 1;
771 } else if (server_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400772 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
Adam Langley1bea1732014-12-17 19:06:57 -0800773 al = SSL_AD_PROTOCOL_VERSION;
774 goto f_err;
775 }
Adam Langley95c29f32014-06-20 12:00:00 -0700776
Adam Langley1bea1732014-12-17 19:06:57 -0800777 /* Copy over the server random. */
David Benjamin0d56f882015-12-19 17:05:56 -0500778 memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700779
David Benjamin0d56f882015-12-19 17:05:56 -0500780 assert(ssl->session == NULL || ssl->session->session_id_length > 0);
781 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
782 CBS_mem_equal(&session_id, ssl->session->session_id,
783 ssl->session->session_id_length)) {
784 if (ssl->sid_ctx_length != ssl->session->sid_ctx_length ||
785 memcmp(ssl->session->sid_ctx, ssl->sid_ctx, ssl->sid_ctx_length)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800786 /* actually a client application bug */
787 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400788 OPENSSL_PUT_ERROR(SSL,
Adam Langley1bea1732014-12-17 19:06:57 -0800789 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
790 goto f_err;
791 }
David Benjamin0d56f882015-12-19 17:05:56 -0500792 ssl->hit = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800793 } else {
794 /* The session wasn't resumed. Create a fresh SSL_SESSION to
795 * fill out. */
David Benjamin0d56f882015-12-19 17:05:56 -0500796 ssl->hit = 0;
797 if (!ssl_get_new_session(ssl, 0 /* client */)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800798 goto f_err;
799 }
800 /* Note: session_id could be empty. */
David Benjamin0d56f882015-12-19 17:05:56 -0500801 ssl->session->session_id_length = CBS_len(&session_id);
802 memcpy(ssl->session->session_id, CBS_data(&session_id),
803 CBS_len(&session_id));
Adam Langley1bea1732014-12-17 19:06:57 -0800804 }
David Benjamina03d95d2014-07-12 19:49:07 -0400805
David Benjamina1c90a52015-05-30 17:03:14 -0400806 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langley1bea1732014-12-17 19:06:57 -0800807 if (c == NULL) {
808 /* unknown cipher */
809 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400810 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
Adam Langley1bea1732014-12-17 19:06:57 -0800811 goto f_err;
812 }
Adam Langley1bea1732014-12-17 19:06:57 -0800813 /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
814 * the server selected it, it's an error. */
David Benjamin0d56f882015-12-19 17:05:56 -0500815 if ((c->algorithm_mkey & ct->mask_k) || (c->algorithm_auth & ct->mask_a) ||
David Benjaminb9e4fa52015-12-29 23:58:17 -0500816 SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800817 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400818 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
Adam Langley1bea1732014-12-17 19:06:57 -0800819 goto f_err;
820 }
Adam Langley95c29f32014-06-20 12:00:00 -0700821
David Benjamin0d56f882015-12-19 17:05:56 -0500822 sk = ssl_get_ciphers_by_id(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800823 if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
824 /* we did not say we would use this cipher */
825 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400826 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
Adam Langley1bea1732014-12-17 19:06:57 -0800827 goto f_err;
828 }
Adam Langley95c29f32014-06-20 12:00:00 -0700829
David Benjamin0d56f882015-12-19 17:05:56 -0500830 if (ssl->hit) {
831 if (ssl->session->cipher != c) {
David Benjaminece3de92015-03-16 18:02:20 -0400832 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400833 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
David Benjaminece3de92015-03-16 18:02:20 -0400834 goto f_err;
835 }
David Benjamin0d56f882015-12-19 17:05:56 -0500836 if (ssl->session->ssl_version != ssl->version) {
David Benjaminece3de92015-03-16 18:02:20 -0400837 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400838 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjaminece3de92015-03-16 18:02:20 -0400839 goto f_err;
840 }
David Benjamin0623bce2015-12-25 15:40:14 -0500841 } else {
842 ssl->session->cipher = c;
Adam Langley1bea1732014-12-17 19:06:57 -0800843 }
David Benjamin0d56f882015-12-19 17:05:56 -0500844 ssl->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500845
David Benjamin9550c3a2015-08-05 08:50:34 -0400846 /* Now that the cipher is known, initialize the handshake hash. */
David Benjamin0d56f882015-12-19 17:05:56 -0500847 if (!ssl3_init_handshake_hash(ssl)) {
David Benjamin9550c3a2015-08-05 08:50:34 -0400848 goto f_err;
849 }
850
David Benjamin5f04b652015-05-26 17:30:10 -0400851 /* If doing a full handshake with TLS 1.2, the server may request a client
852 * certificate which requires hashing the handshake transcript under a
David Benjamin9550c3a2015-08-05 08:50:34 -0400853 * different hash. Otherwise, the handshake buffer may be released. */
David Benjaminc032dfa2016-05-12 14:54:57 -0400854 if (ssl->hit || ssl3_protocol_version(ssl) < TLS1_2_VERSION ||
855 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500856 ssl3_free_handshake_buffer(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800857 }
David Benjamina03d95d2014-07-12 19:49:07 -0400858
Adam Langley1bea1732014-12-17 19:06:57 -0800859 /* Only the NULL compression algorithm is supported. */
860 if (compression_method != 0) {
861 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400862 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
Adam Langley1bea1732014-12-17 19:06:57 -0800863 goto f_err;
864 }
Adam Langley95c29f32014-06-20 12:00:00 -0700865
Adam Langley1bea1732014-12-17 19:06:57 -0800866 /* TLS extensions */
David Benjamin0d56f882015-12-19 17:05:56 -0500867 if (!ssl_parse_serverhello_tlsext(ssl, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -0400868 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Adam Langley1bea1732014-12-17 19:06:57 -0800869 goto err;
870 }
Adam Langley95c29f32014-06-20 12:00:00 -0700871
Adam Langley1bea1732014-12-17 19:06:57 -0800872 /* There should be nothing left over in the record. */
873 if (CBS_len(&server_hello) != 0) {
874 /* wrong packet length */
875 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400876 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
Adam Langley1bea1732014-12-17 19:06:57 -0800877 goto f_err;
878 }
Adam Langley95c29f32014-06-20 12:00:00 -0700879
David Benjamin0d56f882015-12-19 17:05:56 -0500880 if (ssl->hit &&
881 ssl->s3->tmp.extended_master_secret !=
882 ssl->session->extended_master_secret) {
Adam Langleyba5934b2015-06-02 10:50:35 -0700883 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin0d56f882015-12-19 17:05:56 -0500884 if (ssl->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -0400885 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700886 } else {
David Benjamin3570d732015-06-29 00:28:17 -0400887 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700888 }
889 goto f_err;
890 }
891
Adam Langley1bea1732014-12-17 19:06:57 -0800892 return 1;
893
Adam Langley95c29f32014-06-20 12:00:00 -0700894f_err:
David Benjamin0d56f882015-12-19 17:05:56 -0500895 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700896err:
Adam Langley1bea1732014-12-17 19:06:57 -0800897 return -1;
898}
Adam Langley95c29f32014-06-20 12:00:00 -0700899
David Benjamin99fdfb92015-11-02 12:11:35 -0500900/* ssl3_check_leaf_certificate returns one if |leaf| is a suitable leaf server
901 * certificate for |ssl|. Otherwise, it returns zero and pushes an error on the
902 * error queue. */
903static int ssl3_check_leaf_certificate(SSL *ssl, X509 *leaf) {
David Benjamin436bf822015-06-07 19:50:37 -0400904 int ret = 0;
905 EVP_PKEY *pkey = X509_get_pubkey(leaf);
David Benjamind1c1c8e2015-06-08 18:36:21 -0400906 if (pkey == NULL) {
David Benjamin436bf822015-06-07 19:50:37 -0400907 goto err;
908 }
909
910 /* Check the certificate's type matches the cipher. */
David Benjamin99fdfb92015-11-02 12:11:35 -0500911 const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher;
David Benjamind1d80782015-07-05 11:54:09 -0400912 int expected_type = ssl_cipher_get_key_type(cipher);
913 assert(expected_type != EVP_PKEY_NONE);
914 if (pkey->type != expected_type) {
David Benjamin3570d732015-06-29 00:28:17 -0400915 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
David Benjamin436bf822015-06-07 19:50:37 -0400916 goto err;
917 }
918
David Benjamin436bf822015-06-07 19:50:37 -0400919 if (cipher->algorithm_auth & SSL_aECDSA) {
David Benjamin99fdfb92015-11-02 12:11:35 -0500920 /* TODO(davidben): This behavior is preserved from upstream. Should key
921 * usages be checked in other cases as well? */
David Benjamin436bf822015-06-07 19:50:37 -0400922 /* This call populates the ex_flags field correctly */
923 X509_check_purpose(leaf, -1, 0);
924 if ((leaf->ex_flags & EXFLAG_KUSAGE) &&
925 !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) {
David Benjamin3570d732015-06-29 00:28:17 -0400926 OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
David Benjamin436bf822015-06-07 19:50:37 -0400927 goto err;
928 }
David Benjamin99fdfb92015-11-02 12:11:35 -0500929
930 if (!tls1_check_ec_cert(ssl, leaf)) {
931 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
932 goto err;
933 }
David Benjamin436bf822015-06-07 19:50:37 -0400934 }
935
936 ret = 1;
937
938err:
939 EVP_PKEY_free(pkey);
940 return ret;
941}
942
David Benjamin0d56f882015-12-19 17:05:56 -0500943int ssl3_get_server_certificate(SSL *ssl) {
Paul Lietar8f1c2682015-08-18 12:21:54 +0100944 int al, ok, ret = -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800945 unsigned long n;
946 X509 *x = NULL;
947 STACK_OF(X509) *sk = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -0800948 EVP_PKEY *pkey = NULL;
949 CBS cbs, certificate_list;
950 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700951
David Benjamin1e6d6df2016-05-13 18:28:17 -0400952 n = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE, ssl_hash_message,
953 &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700954
Adam Langley1bea1732014-12-17 19:06:57 -0800955 if (!ok) {
956 return n;
957 }
Adam Langley95c29f32014-06-20 12:00:00 -0700958
David Benjamin0d56f882015-12-19 17:05:56 -0500959 CBS_init(&cbs, ssl->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700960
Adam Langley1bea1732014-12-17 19:06:57 -0800961 sk = sk_X509_new_null();
962 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400963 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -0800964 goto err;
965 }
Adam Langley95c29f32014-06-20 12:00:00 -0700966
Adam Langley1bea1732014-12-17 19:06:57 -0800967 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
David Benjamin8923c0b2015-06-07 11:42:34 -0400968 CBS_len(&certificate_list) == 0 ||
Adam Langley1bea1732014-12-17 19:06:57 -0800969 CBS_len(&cbs) != 0) {
970 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400971 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800972 goto f_err;
973 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400974
Adam Langley1bea1732014-12-17 19:06:57 -0800975 while (CBS_len(&certificate_list) > 0) {
976 CBS certificate;
977 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
978 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400979 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
Adam Langley1bea1732014-12-17 19:06:57 -0800980 goto f_err;
981 }
David Benjaminaf073652015-11-12 23:09:30 -0800982 /* A u24 length cannot overflow a long. */
Adam Langley1bea1732014-12-17 19:06:57 -0800983 data = CBS_data(&certificate);
David Benjaminaf073652015-11-12 23:09:30 -0800984 x = d2i_X509(NULL, &data, (long)CBS_len(&certificate));
Adam Langley1bea1732014-12-17 19:06:57 -0800985 if (x == NULL) {
986 al = SSL_AD_BAD_CERTIFICATE;
David Benjamin3570d732015-06-29 00:28:17 -0400987 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -0800988 goto f_err;
989 }
990 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
991 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400992 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
Adam Langley1bea1732014-12-17 19:06:57 -0800993 goto f_err;
994 }
995 if (!sk_X509_push(sk, x)) {
David Benjamin3570d732015-06-29 00:28:17 -0400996 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -0800997 goto err;
998 }
999 x = NULL;
1000 }
Adam Langley95c29f32014-06-20 12:00:00 -07001001
David Benjamin436bf822015-06-07 19:50:37 -04001002 X509 *leaf = sk_X509_value(sk, 0);
David Benjamin0d56f882015-12-19 17:05:56 -05001003 if (!ssl3_check_leaf_certificate(ssl, leaf)) {
David Benjamin436bf822015-06-07 19:50:37 -04001004 al = SSL_AD_ILLEGAL_PARAMETER;
1005 goto f_err;
1006 }
1007
David Benjamin436bf822015-06-07 19:50:37 -04001008 /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes
1009 * the leaf. */
David Benjamin0d56f882015-12-19 17:05:56 -05001010 sk_X509_pop_free(ssl->session->cert_chain, X509_free);
1011 ssl->session->cert_chain = sk;
Adam Langley1bea1732014-12-17 19:06:57 -08001012 sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001013
David Benjamin0d56f882015-12-19 17:05:56 -05001014 X509_free(ssl->session->peer);
1015 ssl->session->peer = X509_up_ref(leaf);
David Benjamind26aea62014-07-12 00:13:56 -04001016
David Benjamin0d56f882015-12-19 17:05:56 -05001017 ssl->session->verify_result = ssl->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001018
Adam Langley1bea1732014-12-17 19:06:57 -08001019 ret = 1;
1020
1021 if (0) {
1022 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001023 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley1bea1732014-12-17 19:06:57 -08001024 }
1025
1026err:
1027 EVP_PKEY_free(pkey);
1028 X509_free(x);
1029 sk_X509_pop_free(sk, X509_free);
1030 return ret;
1031}
1032
David Benjamin0d56f882015-12-19 17:05:56 -05001033int ssl3_get_server_key_exchange(SSL *ssl) {
Adam Langley1bea1732014-12-17 19:06:57 -08001034 EVP_MD_CTX md_ctx;
1035 int al, ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001036 EVP_PKEY *pkey = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001037 DH *dh = NULL;
1038 EC_KEY *ecdh = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001039 EC_POINT *srvr_ecpoint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001040
David Benjamin1e6d6df2016-05-13 18:28:17 -04001041 long n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok);
Adam Langley1bea1732014-12-17 19:06:57 -08001042 if (!ok) {
1043 return n;
1044 }
1045
David Benjamin0d56f882015-12-19 17:05:56 -05001046 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1047 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -04001048 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin0d56f882015-12-19 17:05:56 -05001049 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001050 return -1;
1051 }
1052
David Benjamin26416e92015-08-22 16:04:17 -04001053 /* In plain PSK ciphersuite, ServerKeyExchange may be omitted to send no
1054 * identity hint. */
David Benjamin0d56f882015-12-19 17:05:56 -05001055 if (ssl->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
Adam Langley1bea1732014-12-17 19:06:57 -08001056 /* TODO(davidben): This should be reset in one place with the rest of the
1057 * handshake state. */
David Benjamin0d56f882015-12-19 17:05:56 -05001058 OPENSSL_free(ssl->s3->tmp.peer_psk_identity_hint);
1059 ssl->s3->tmp.peer_psk_identity_hint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001060 }
David Benjamin0d56f882015-12-19 17:05:56 -05001061 ssl->s3->tmp.reuse_message = 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001062 return 1;
1063 }
1064
1065 /* Retain a copy of the original CBS to compute the signature over. */
David Benjaminb35d6842015-12-29 17:08:08 -05001066 CBS server_key_exchange;
David Benjamin0d56f882015-12-19 17:05:56 -05001067 CBS_init(&server_key_exchange, ssl->init_msg, n);
David Benjaminb35d6842015-12-29 17:08:08 -05001068 CBS server_key_exchange_orig = server_key_exchange;
Adam Langley1bea1732014-12-17 19:06:57 -08001069
David Benjaminb35d6842015-12-29 17:08:08 -05001070 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1071 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1bea1732014-12-17 19:06:57 -08001072 EVP_MD_CTX_init(&md_ctx);
1073
1074 if (alg_a & SSL_aPSK) {
1075 CBS psk_identity_hint;
1076
1077 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1078 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1079 &psk_identity_hint)) {
1080 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001081 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001082 goto f_err;
1083 }
1084
1085 /* Store PSK identity hint for later use, hint is used in
1086 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1087 * identity hint can be as long as the maximum length of a PSK identity.
1088 * Also do not allow NULL characters; identities are saved as C strings.
1089 *
1090 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1091 * a specific identity. */
1092 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1093 CBS_contains_zero_byte(&psk_identity_hint)) {
1094 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001095 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langley1bea1732014-12-17 19:06:57 -08001096 goto f_err;
1097 }
1098
1099 /* Save the identity hint as a C string. */
David Benjamin0d56f882015-12-19 17:05:56 -05001100 if (!CBS_strdup(&psk_identity_hint, &ssl->s3->tmp.peer_psk_identity_hint)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001101 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001102 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001103 goto f_err;
1104 }
1105 }
1106
David Benjamin7061e282015-03-19 11:10:48 -04001107 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001108 CBS dh_p, dh_g, dh_Ys;
Adam Langley1bea1732014-12-17 19:06:57 -08001109 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1110 CBS_len(&dh_p) == 0 ||
1111 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1112 CBS_len(&dh_g) == 0 ||
1113 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1114 CBS_len(&dh_Ys) == 0) {
1115 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001116 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001117 goto f_err;
1118 }
1119
1120 dh = DH_new();
1121 if (dh == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001122 goto err;
1123 }
1124
David Benjamin48cce662015-12-17 01:33:08 -05001125 dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL);
1126 dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL);
David Benjamin974c7ba2015-12-19 16:58:04 -05001127 if (dh->p == NULL || dh->g == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001128 goto err;
1129 }
1130
David Benjamin0d56f882015-12-19 17:05:56 -05001131 ssl->session->key_exchange_info = DH_num_bits(dh);
1132 if (ssl->session->key_exchange_info < 1024) {
David Benjamin3570d732015-06-29 00:28:17 -04001133 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH);
Adam Langley1bea1732014-12-17 19:06:57 -08001134 goto err;
David Benjamin0d56f882015-12-19 17:05:56 -05001135 } else if (ssl->session->key_exchange_info > 4096) {
David Benjamincd24a392015-11-11 13:23:05 -08001136 /* Overly large DHE groups are prohibitively expensive, so enforce a limit
1137 * to prevent a server from causing us to perform too expensive of a
1138 * computation. */
1139 OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG);
1140 goto err;
1141 }
David Benjamin974c7ba2015-12-19 16:58:04 -05001142
David Benjamin0d56f882015-12-19 17:05:56 -05001143 SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001144 dh = NULL;
David Benjamin974c7ba2015-12-19 16:58:04 -05001145
1146 /* Save the peer public key for later. */
1147 size_t peer_key_len;
David Benjamin0d56f882015-12-19 17:05:56 -05001148 if (!CBS_stow(&dh_Ys, &ssl->s3->tmp.peer_key, &peer_key_len)) {
David Benjamin974c7ba2015-12-19 16:58:04 -05001149 goto err;
1150 }
1151 /* |dh_Ys| has a u16 length prefix, so this fits in a |uint16_t|. */
David Benjamin0d56f882015-12-19 17:05:56 -05001152 assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
1153 ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
David Benjamin7061e282015-03-19 11:10:48 -04001154 } else if (alg_k & SSL_kECDHE) {
David Benjamin4298d772015-12-19 00:18:25 -05001155 /* Parse the server parameters. */
1156 uint8_t curve_type;
David Benjamin48cce662015-12-17 01:33:08 -05001157 uint16_t curve_id;
David Benjamin48cce662015-12-17 01:33:08 -05001158 CBS point;
David Benjamin4298d772015-12-19 00:18:25 -05001159 if (!CBS_get_u8(&server_key_exchange, &curve_type) ||
1160 curve_type != NAMED_CURVE_TYPE ||
1161 !CBS_get_u16(&server_key_exchange, &curve_id) ||
1162 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001163 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001164 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001165 goto f_err;
1166 }
David Benjamin0d56f882015-12-19 17:05:56 -05001167 ssl->session->key_exchange_info = curve_id;
Adam Langley1bea1732014-12-17 19:06:57 -08001168
David Benjamin4298d772015-12-19 00:18:25 -05001169 /* Ensure the curve is consistent with preferences. */
David Benjamin0d56f882015-12-19 17:05:56 -05001170 if (!tls1_check_curve_id(ssl, curve_id)) {
David Benjamin4298d772015-12-19 00:18:25 -05001171 al = SSL_AD_ILLEGAL_PARAMETER;
1172 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langley1bea1732014-12-17 19:06:57 -08001173 goto f_err;
1174 }
David Benjamin4298d772015-12-19 00:18:25 -05001175
1176 /* Initialize ECDH and save the peer public key for later. */
1177 size_t peer_key_len;
David Benjamin0d56f882015-12-19 17:05:56 -05001178 if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, curve_id) ||
1179 !CBS_stow(&point, &ssl->s3->tmp.peer_key, &peer_key_len)) {
David Benjaminc1cc8582015-12-17 01:22:15 -05001180 goto err;
1181 }
David Benjamin974c7ba2015-12-19 16:58:04 -05001182 /* |point| has a u8 length prefix, so this fits in a |uint16_t|. */
David Benjamin0d56f882015-12-19 17:05:56 -05001183 assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
1184 ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
Adam Langley1bea1732014-12-17 19:06:57 -08001185 } else if (!(alg_k & SSL_kPSK)) {
1186 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -04001187 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001188 goto f_err;
1189 }
1190
1191 /* At this point, |server_key_exchange| contains the signature, if any, while
1192 * |server_key_exchange_orig| contains the entire message. From that, derive
1193 * a CBS containing just the parameter. */
David Benjaminb35d6842015-12-29 17:08:08 -05001194 CBS parameter;
Adam Langley1bea1732014-12-17 19:06:57 -08001195 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1196 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1197
David Benjaminf4958e72015-06-07 11:00:53 -04001198 /* ServerKeyExchange should be signed by the server's public key. */
David Benjaminc032dfa2016-05-12 14:54:57 -04001199 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001200 pkey = X509_get_pubkey(ssl->session->peer);
David Benjaminf4958e72015-06-07 11:00:53 -04001201 if (pkey == NULL) {
1202 goto err;
1203 }
Adam Langley1bea1732014-12-17 19:06:57 -08001204
David Benjaminb35d6842015-12-29 17:08:08 -05001205 const EVP_MD *md = NULL;
David Benjamina1e9cab2015-12-30 00:08:49 -05001206 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjamin6e807652015-11-02 12:02:20 -05001207 uint8_t hash, signature;
1208 if (!CBS_get_u8(&server_key_exchange, &hash) ||
1209 !CBS_get_u8(&server_key_exchange, &signature)) {
1210 al = SSL_AD_DECODE_ERROR;
1211 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001212 goto f_err;
1213 }
David Benjamin0d56f882015-12-19 17:05:56 -05001214 if (!tls12_check_peer_sigalg(ssl, &md, &al, hash, signature, pkey)) {
David Benjamin6e807652015-11-02 12:02:20 -05001215 goto f_err;
1216 }
David Benjamin0d56f882015-12-19 17:05:56 -05001217 ssl->s3->tmp.server_key_exchange_hash = hash;
Adam Langley1bea1732014-12-17 19:06:57 -08001218 } else if (pkey->type == EVP_PKEY_RSA) {
1219 md = EVP_md5_sha1();
1220 } else {
1221 md = EVP_sha1();
1222 }
1223
1224 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001225 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001226 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1227 CBS_len(&server_key_exchange) != 0) {
1228 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001229 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001230 goto f_err;
1231 }
1232
David Benjaminbf82aed2016-03-01 22:57:40 -05001233 int sig_ok = EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) &&
1234 EVP_DigestVerifyUpdate(&md_ctx, ssl->s3->client_random,
1235 SSL3_RANDOM_SIZE) &&
1236 EVP_DigestVerifyUpdate(&md_ctx, ssl->s3->server_random,
1237 SSL3_RANDOM_SIZE) &&
1238 EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1239 CBS_len(&parameter)) &&
1240 EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1241 CBS_len(&signature));
1242#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1243 sig_ok = 1;
1244 ERR_clear_error();
1245#endif
1246 if (!sig_ok) {
Adam Langley1bea1732014-12-17 19:06:57 -08001247 /* bad signature */
1248 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001249 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001250 goto f_err;
1251 }
1252 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001253 /* PSK ciphers are the only supported certificate-less ciphers. */
1254 assert(alg_a == SSL_aPSK);
1255
Adam Langley1bea1732014-12-17 19:06:57 -08001256 if (CBS_len(&server_key_exchange) > 0) {
1257 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001258 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001259 goto f_err;
1260 }
1261 }
1262 EVP_PKEY_free(pkey);
1263 EVP_MD_CTX_cleanup(&md_ctx);
1264 return 1;
1265
Adam Langley95c29f32014-06-20 12:00:00 -07001266f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001267 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001268err:
Adam Langley1bea1732014-12-17 19:06:57 -08001269 EVP_PKEY_free(pkey);
David Benjamin2755a3e2015-04-22 16:17:58 -04001270 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001271 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001272 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001273 EVP_MD_CTX_cleanup(&md_ctx);
1274 return -1;
1275}
Adam Langley95c29f32014-06-20 12:00:00 -07001276
Adam Langley1bea1732014-12-17 19:06:57 -08001277static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1278 return X509_NAME_cmp(*a, *b);
1279}
Adam Langley95c29f32014-06-20 12:00:00 -07001280
David Benjamin0d56f882015-12-19 17:05:56 -05001281int ssl3_get_certificate_request(SSL *ssl) {
Adam Langley1bea1732014-12-17 19:06:57 -08001282 int ok, ret = 0;
Adam Langley1bea1732014-12-17 19:06:57 -08001283 X509_NAME *xn = NULL;
1284 STACK_OF(X509_NAME) *ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001285
David Benjamin1e6d6df2016-05-13 18:28:17 -04001286 long n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001287
Adam Langley1bea1732014-12-17 19:06:57 -08001288 if (!ok) {
1289 return n;
1290 }
David Benjamin688d8df2014-11-02 23:06:42 -05001291
David Benjamin0d56f882015-12-19 17:05:56 -05001292 ssl->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001293
David Benjamin0d56f882015-12-19 17:05:56 -05001294 if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1295 ssl->s3->tmp.reuse_message = 1;
David Benjamin9550c3a2015-08-05 08:50:34 -04001296 /* If we get here we don't need the handshake buffer as we won't be doing
1297 * client auth. */
David Benjamin0d56f882015-12-19 17:05:56 -05001298 ssl3_free_handshake_buffer(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001299 return 1;
1300 }
David Benjamined439582014-07-14 19:13:02 -04001301
David Benjamin0d56f882015-12-19 17:05:56 -05001302 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1303 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjamindf28c3a2016-03-10 16:11:51 -05001304 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001305 goto err;
1306 }
Adam Langley95c29f32014-06-20 12:00:00 -07001307
David Benjamina5177cb2016-03-11 19:38:17 -05001308 CBS cbs;
David Benjamin0d56f882015-12-19 17:05:56 -05001309 CBS_init(&cbs, ssl->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001310
Adam Langley1bea1732014-12-17 19:06:57 -08001311 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1312 if (ca_sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001313 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001314 goto err;
1315 }
Adam Langley95c29f32014-06-20 12:00:00 -07001316
Adam Langley1bea1732014-12-17 19:06:57 -08001317 /* get the certificate types */
David Benjamina5177cb2016-03-11 19:38:17 -05001318 CBS certificate_types;
Adam Langley1bea1732014-12-17 19:06:57 -08001319 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001320 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001321 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001322 goto err;
1323 }
David Benjamined439582014-07-14 19:13:02 -04001324
David Benjamin0d56f882015-12-19 17:05:56 -05001325 if (!CBS_stow(&certificate_types, &ssl->s3->tmp.certificate_types,
1326 &ssl->s3->tmp.num_certificate_types)) {
1327 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001328 goto err;
1329 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001330
David Benjamina1e9cab2015-12-30 00:08:49 -05001331 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley1bea1732014-12-17 19:06:57 -08001332 CBS supported_signature_algorithms;
Steven Valdez0d62f262015-09-04 12:41:04 -04001333 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001334 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
1335 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001336 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001337 goto err;
1338 }
Adam Langley1bea1732014-12-17 19:06:57 -08001339 }
David Benjamined439582014-07-14 19:13:02 -04001340
Adam Langley1bea1732014-12-17 19:06:57 -08001341 /* get the CA RDNs */
David Benjamina5177cb2016-03-11 19:38:17 -05001342 CBS certificate_authorities;
Adam Langley1bea1732014-12-17 19:06:57 -08001343 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001344 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001345 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
Adam Langley1bea1732014-12-17 19:06:57 -08001346 goto err;
1347 }
Adam Langley95c29f32014-06-20 12:00:00 -07001348
Adam Langley1bea1732014-12-17 19:06:57 -08001349 while (CBS_len(&certificate_authorities) > 0) {
1350 CBS distinguished_name;
1351 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1352 &distinguished_name)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001353 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001354 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
Adam Langley1bea1732014-12-17 19:06:57 -08001355 goto err;
1356 }
Adam Langley95c29f32014-06-20 12:00:00 -07001357
David Benjamina5177cb2016-03-11 19:38:17 -05001358 const uint8_t *data = CBS_data(&distinguished_name);
David Benjaminaf073652015-11-12 23:09:30 -08001359 /* A u16 length cannot overflow a long. */
1360 xn = d2i_X509_NAME(NULL, &data, (long)CBS_len(&distinguished_name));
David Benjamina5177cb2016-03-11 19:38:17 -05001361 if (xn == NULL ||
1362 data != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001363 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamina5177cb2016-03-11 19:38:17 -05001364 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001365 goto err;
1366 }
Adam Langley95c29f32014-06-20 12:00:00 -07001367
Adam Langley1bea1732014-12-17 19:06:57 -08001368 if (!sk_X509_NAME_push(ca_sk, xn)) {
David Benjamin3570d732015-06-29 00:28:17 -04001369 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001370 goto err;
1371 }
David Benjamin6b6e0b22016-03-11 19:30:29 -05001372 xn = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001373 }
Adam Langley95c29f32014-06-20 12:00:00 -07001374
Adam Langley1bea1732014-12-17 19:06:57 -08001375 /* we should setup a certificate to return.... */
David Benjamin0d56f882015-12-19 17:05:56 -05001376 ssl->s3->tmp.cert_req = 1;
1377 sk_X509_NAME_pop_free(ssl->s3->tmp.ca_names, X509_NAME_free);
1378 ssl->s3->tmp.ca_names = ca_sk;
Adam Langley1bea1732014-12-17 19:06:57 -08001379 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001380
Adam Langley1bea1732014-12-17 19:06:57 -08001381 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001382
Adam Langley1bea1732014-12-17 19:06:57 -08001383err:
David Benjamin6b6e0b22016-03-11 19:30:29 -05001384 X509_NAME_free(xn);
David Benjamin2755a3e2015-04-22 16:17:58 -04001385 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001386 return ret;
1387}
Adam Langley95c29f32014-06-20 12:00:00 -07001388
David Benjamin0d56f882015-12-19 17:05:56 -05001389int ssl3_get_new_session_ticket(SSL *ssl) {
David Benjamin68070622015-02-08 23:44:59 -05001390 int ok, al;
David Benjamin1e6d6df2016-05-13 18:28:17 -04001391 long n = ssl->method->ssl_get_message(ssl, SSL3_MT_NEWSESSION_TICKET,
1392 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001393
Adam Langley1bea1732014-12-17 19:06:57 -08001394 if (!ok) {
1395 return n;
1396 }
Adam Langley95c29f32014-06-20 12:00:00 -07001397
David Benjamindd6fed92015-10-23 17:41:12 -04001398 CBS new_session_ticket, ticket;
1399 uint32_t ticket_lifetime_hint;
David Benjamin0d56f882015-12-19 17:05:56 -05001400 CBS_init(&new_session_ticket, ssl->init_msg, n);
David Benjamindd6fed92015-10-23 17:41:12 -04001401 if (!CBS_get_u32(&new_session_ticket, &ticket_lifetime_hint) ||
1402 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1403 CBS_len(&new_session_ticket) != 0) {
1404 al = SSL_AD_DECODE_ERROR;
1405 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1406 goto f_err;
1407 }
1408
1409 if (CBS_len(&ticket) == 0) {
1410 /* RFC 5077 allows a server to change its mind and send no ticket after
Adam Langley09d68c92015-10-28 18:08:10 -07001411 * negotiating the extension. The value of |tlsext_ticket_expected| is
1412 * checked in |ssl_update_cache| so is cleared here to avoid an unnecessary
1413 * update. */
David Benjamin0d56f882015-12-19 17:05:56 -05001414 ssl->tlsext_ticket_expected = 0;
David Benjamindd6fed92015-10-23 17:41:12 -04001415 return 1;
1416 }
1417
David Benjamin0d56f882015-12-19 17:05:56 -05001418 if (ssl->hit) {
David Benjamin95d31822015-06-15 19:53:32 -04001419 /* The server is sending a new ticket for an existing session. Sessions are
1420 * immutable once established, so duplicate all but the ticket of the
1421 * existing session. */
1422 uint8_t *bytes;
1423 size_t bytes_len;
David Benjamin0d56f882015-12-19 17:05:56 -05001424 if (!SSL_SESSION_to_bytes_for_ticket(ssl->session, &bytes, &bytes_len)) {
David Benjamin95d31822015-06-15 19:53:32 -04001425 goto err;
1426 }
1427 SSL_SESSION *new_session = SSL_SESSION_from_bytes(bytes, bytes_len);
1428 OPENSSL_free(bytes);
1429 if (new_session == NULL) {
1430 /* This should never happen. */
David Benjamin3570d732015-06-29 00:28:17 -04001431 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin95d31822015-06-15 19:53:32 -04001432 goto err;
1433 }
1434
David Benjamin0d56f882015-12-19 17:05:56 -05001435 SSL_SESSION_free(ssl->session);
1436 ssl->session = new_session;
David Benjamin95d31822015-06-15 19:53:32 -04001437 }
1438
David Benjamin0d56f882015-12-19 17:05:56 -05001439 if (!CBS_stow(&ticket, &ssl->session->tlsext_tick,
1440 &ssl->session->tlsext_ticklen)) {
David Benjamin3570d732015-06-29 00:28:17 -04001441 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001442 goto err;
1443 }
David Benjamin0d56f882015-12-19 17:05:56 -05001444 ssl->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07001445
David Benjamin68070622015-02-08 23:44:59 -05001446 /* Generate a session ID for this session based on the session ticket. We use
1447 * the session ID mechanism for detecting ticket resumption. This also fits in
1448 * with assumptions elsewhere in OpenSSL.*/
David Benjamin0d56f882015-12-19 17:05:56 -05001449 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), ssl->session->session_id,
1450 &ssl->session->session_id_length, EVP_sha256(), NULL)) {
David Benjamin68070622015-02-08 23:44:59 -05001451 goto err;
1452 }
1453
1454 return 1;
David Benjamined439582014-07-14 19:13:02 -04001455
Adam Langley95c29f32014-06-20 12:00:00 -07001456f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001457 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001458err:
Adam Langley1bea1732014-12-17 19:06:57 -08001459 return -1;
1460}
Adam Langley95c29f32014-06-20 12:00:00 -07001461
David Benjamin0d56f882015-12-19 17:05:56 -05001462int ssl3_get_cert_status(SSL *ssl) {
Adam Langley1bea1732014-12-17 19:06:57 -08001463 int ok, al;
1464 long n;
1465 CBS certificate_status, ocsp_response;
1466 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001467
David Benjamin1e6d6df2016-05-13 18:28:17 -04001468 n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001469
Adam Langley1bea1732014-12-17 19:06:57 -08001470 if (!ok) {
1471 return n;
1472 }
Adam Langley95c29f32014-06-20 12:00:00 -07001473
David Benjamin0d56f882015-12-19 17:05:56 -05001474 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
David Benjamindcd979f2015-04-20 18:26:52 -04001475 /* A server may send status_request in ServerHello and then change
1476 * its mind about sending CertificateStatus. */
David Benjamin0d56f882015-12-19 17:05:56 -05001477 ssl->s3->tmp.reuse_message = 1;
David Benjamindcd979f2015-04-20 18:26:52 -04001478 return 1;
1479 }
1480
David Benjamin0d56f882015-12-19 17:05:56 -05001481 CBS_init(&certificate_status, ssl->init_msg, n);
Adam Langley1bea1732014-12-17 19:06:57 -08001482 if (!CBS_get_u8(&certificate_status, &status_type) ||
1483 status_type != TLSEXT_STATUSTYPE_ocsp ||
1484 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1485 CBS_len(&ocsp_response) == 0 ||
1486 CBS_len(&certificate_status) != 0) {
1487 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001488 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001489 goto f_err;
1490 }
Adam Langley95c29f32014-06-20 12:00:00 -07001491
David Benjamin0d56f882015-12-19 17:05:56 -05001492 if (!CBS_stow(&ocsp_response, &ssl->session->ocsp_response,
1493 &ssl->session->ocsp_response_length)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001494 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001495 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001496 goto f_err;
1497 }
1498 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001499
Adam Langley95c29f32014-06-20 12:00:00 -07001500f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001501 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley1bea1732014-12-17 19:06:57 -08001502 return -1;
1503}
Adam Langley95c29f32014-06-20 12:00:00 -07001504
David Benjamin0d56f882015-12-19 17:05:56 -05001505int ssl3_get_server_done(SSL *ssl) {
Adam Langley1bea1732014-12-17 19:06:57 -08001506 int ok;
1507 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001508
David Benjamin1e6d6df2016-05-13 18:28:17 -04001509 n = ssl->method->ssl_get_message(ssl, SSL3_MT_SERVER_DONE, ssl_hash_message,
1510 &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001511
Adam Langley1bea1732014-12-17 19:06:57 -08001512 if (!ok) {
1513 return n;
1514 }
David Benjamin46062682014-07-14 19:14:32 -04001515
Adam Langley1bea1732014-12-17 19:06:57 -08001516 if (n > 0) {
1517 /* should contain no data */
David Benjamin0d56f882015-12-19 17:05:56 -05001518 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001519 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
Adam Langley1bea1732014-12-17 19:06:57 -08001520 return -1;
1521 }
David Benjamin46062682014-07-14 19:14:32 -04001522
Adam Langley1bea1732014-12-17 19:06:57 -08001523 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001524}
Adam Langley1258b6a2014-06-20 12:00:00 -07001525
Brian Smith274341d2015-10-08 17:10:15 -10001526OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned),
1527 SIZE_T_IS_SMALLER_THAN_UNSIGNED);
Adam Langley1258b6a2014-06-20 12:00:00 -07001528
David Benjaminae0eaaa2015-12-17 02:01:25 -05001529int ssl3_send_client_key_exchange(SSL *ssl) {
1530 if (ssl->state == SSL3_ST_CW_KEY_EXCH_B) {
1531 return ssl_do_write(ssl);
1532 }
1533 assert(ssl->state == SSL3_ST_CW_KEY_EXCH_A);
1534
Adam Langley1bea1732014-12-17 19:06:57 -08001535 uint8_t *pms = NULL;
1536 size_t pms_len = 0;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001537 CBB cbb;
1538 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1539 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) {
1540 goto err;
1541 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001542
David Benjaminae0eaaa2015-12-17 02:01:25 -05001543 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1544 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001545
David Benjaminae0eaaa2015-12-17 02:01:25 -05001546 /* If using a PSK key exchange, prepare the pre-shared key. */
1547 unsigned psk_len = 0;
1548 uint8_t psk[PSK_MAX_PSK_LEN];
1549 if (alg_a & SSL_aPSK) {
1550 if (ssl->psk_client_callback == NULL) {
1551 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1552 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001553 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001554
David Benjaminae0eaaa2015-12-17 02:01:25 -05001555 char identity[PSK_MAX_IDENTITY_LEN + 1];
1556 memset(identity, 0, sizeof(identity));
1557 psk_len = ssl->psk_client_callback(
1558 ssl, ssl->s3->tmp.peer_psk_identity_hint, identity, sizeof(identity),
1559 psk, sizeof(psk));
1560 if (psk_len == 0) {
1561 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1562 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1563 goto err;
1564 }
1565 assert(psk_len <= PSK_MAX_PSK_LEN);
Adam Langley1258b6a2014-06-20 12:00:00 -07001566
David Benjaminae0eaaa2015-12-17 02:01:25 -05001567 OPENSSL_free(ssl->session->psk_identity);
1568 ssl->session->psk_identity = BUF_strdup(identity);
1569 if (ssl->session->psk_identity == NULL) {
1570 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1571 goto err;
1572 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001573
David Benjaminae0eaaa2015-12-17 02:01:25 -05001574 /* Write out psk_identity. */
1575 CBB child;
1576 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1577 !CBB_add_bytes(&child, (const uint8_t *)identity,
1578 OPENSSL_strnlen(identity, sizeof(identity))) ||
1579 !CBB_flush(&cbb)) {
1580 goto err;
1581 }
1582 }
David Benjamin758d1272015-11-20 17:47:25 -05001583
David Benjaminae0eaaa2015-12-17 02:01:25 -05001584 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1585 if (alg_k & SSL_kRSA) {
1586 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1587 pms = OPENSSL_malloc(pms_len);
1588 if (pms == NULL) {
1589 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1590 goto err;
1591 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001592
David Benjaminae0eaaa2015-12-17 02:01:25 -05001593 EVP_PKEY *pkey = X509_get_pubkey(ssl->session->peer);
1594 if (pkey == NULL) {
1595 goto err;
1596 }
1597
1598 RSA *rsa = EVP_PKEY_get0_RSA(pkey);
1599 if (rsa == NULL) {
1600 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001601 EVP_PKEY_free(pkey);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001602 goto err;
1603 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001604
David Benjaminae0eaaa2015-12-17 02:01:25 -05001605 ssl->session->key_exchange_info = EVP_PKEY_bits(pkey);
1606 EVP_PKEY_free(pkey);
1607
1608 pms[0] = ssl->client_version >> 8;
1609 pms[1] = ssl->client_version & 0xff;
1610 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1611 goto err;
1612 }
1613
1614 CBB child, *enc_pms = &cbb;
1615 size_t enc_pms_len;
1616 /* In TLS, there is a length prefix. */
1617 if (ssl->version > SSL3_VERSION) {
1618 if (!CBB_add_u16_length_prefixed(&cbb, &child)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001619 goto err;
1620 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001621 enc_pms = &child;
1622 }
Adam Langley1bea1732014-12-17 19:06:57 -08001623
David Benjaminae0eaaa2015-12-17 02:01:25 -05001624 uint8_t *ptr;
1625 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
1626 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
1627 RSA_PKCS1_PADDING) ||
1628 /* Log the premaster secret, if logging is enabled. */
1629 !ssl_log_rsa_client_key_exchange(ssl, ptr, enc_pms_len, pms, pms_len) ||
1630 !CBB_did_write(enc_pms, enc_pms_len) ||
1631 !CBB_flush(&cbb)) {
1632 goto err;
1633 }
David Benjamin974c7ba2015-12-19 16:58:04 -05001634 } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
1635 /* Generate a keypair and serialize the public half. ECDHE uses a u8 length
1636 * prefix while DHE uses u16. */
David Benjaminae0eaaa2015-12-17 02:01:25 -05001637 CBB child;
David Benjamin974c7ba2015-12-19 16:58:04 -05001638 int child_ok;
1639 if (alg_k & SSL_kECDHE) {
1640 child_ok = CBB_add_u8_length_prefixed(&cbb, &child);
1641 } else {
1642 child_ok = CBB_add_u16_length_prefixed(&cbb, &child);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001643 }
1644
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001645 if (!child_ok) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001646 goto err;
1647 }
1648
David Benjamin4298d772015-12-19 00:18:25 -05001649 /* Compute the premaster. */
1650 uint8_t alert;
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001651 if (!SSL_ECDH_CTX_accept(&ssl->s3->tmp.ecdh_ctx, &child, &pms, &pms_len,
1652 &alert, ssl->s3->tmp.peer_key,
1653 ssl->s3->tmp.peer_key_len)) {
David Benjamin4298d772015-12-19 00:18:25 -05001654 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1655 goto err;
1656 }
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001657 if (!CBB_flush(&cbb)) {
1658 goto err;
1659 }
David Benjamin4298d772015-12-19 00:18:25 -05001660
1661 /* The key exchange state may now be discarded. */
1662 SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx);
1663 OPENSSL_free(ssl->s3->tmp.peer_key);
1664 ssl->s3->tmp.peer_key = NULL;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001665 } else if (alg_k & SSL_kPSK) {
1666 /* For plain PSK, other_secret is a block of 0s with the same length as
1667 * the pre-shared key. */
1668 pms_len = psk_len;
1669 pms = OPENSSL_malloc(pms_len);
1670 if (pms == NULL) {
1671 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1672 goto err;
1673 }
1674 memset(pms, 0, pms_len);
1675 } else {
1676 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1677 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1678 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001679 }
1680
David Benjaminae0eaaa2015-12-17 02:01:25 -05001681 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1682 * key. */
1683 if (alg_a & SSL_aPSK) {
1684 CBB pms_cbb, child;
1685 uint8_t *new_pms;
1686 size_t new_pms_len;
1687
1688 CBB_zero(&pms_cbb);
1689 if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) ||
1690 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1691 !CBB_add_bytes(&child, pms, pms_len) ||
1692 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1693 !CBB_add_bytes(&child, psk, psk_len) ||
1694 !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) {
1695 CBB_cleanup(&pms_cbb);
1696 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1697 goto err;
1698 }
1699 OPENSSL_cleanse(pms, pms_len);
1700 OPENSSL_free(pms);
1701 pms = new_pms;
1702 pms_len = new_pms_len;
1703 }
1704
1705 /* The message must be added to the finished hash before calculating the
1706 * master secret. */
1707 size_t length;
1708 if (!CBB_finish(&cbb, NULL, &length) ||
1709 !ssl_set_handshake_header(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE, length)) {
1710 goto err;
1711 }
1712 ssl->state = SSL3_ST_CW_KEY_EXCH_B;
1713
David Benjaminf8d80712015-12-29 18:56:28 -05001714 ssl->session->master_key_length =
1715 tls1_generate_master_secret(ssl, ssl->session->master_key, pms, pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001716 if (ssl->session->master_key_length == 0) {
1717 goto err;
1718 }
1719 ssl->session->extended_master_secret = ssl->s3->tmp.extended_master_secret;
1720 OPENSSL_cleanse(pms, pms_len);
1721 OPENSSL_free(pms);
1722
Adam Langley1bea1732014-12-17 19:06:57 -08001723 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjaminae0eaaa2015-12-17 02:01:25 -05001724 return ssl_do_write(ssl);
Adam Langley1258b6a2014-06-20 12:00:00 -07001725
1726err:
David Benjamin2b07fa42016-03-02 00:23:57 -05001727 CBB_cleanup(&cbb);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001728 if (pms != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001729 OPENSSL_cleanse(pms, pms_len);
1730 OPENSSL_free(pms);
1731 }
1732 return -1;
1733}
Adam Langley1258b6a2014-06-20 12:00:00 -07001734
David Benjaminfc825122015-12-18 01:57:43 -05001735int ssl3_send_cert_verify(SSL *ssl) {
1736 if (ssl->state == SSL3_ST_CW_CERT_VRFY_C) {
1737 return ssl_do_write(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001738 }
1739
David Benjaminfc825122015-12-18 01:57:43 -05001740 CBB cbb, child;
1741 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1742 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) {
1743 goto err;
1744 }
1745
1746 assert(ssl_has_private_key(ssl));
1747
1748 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
1749 size_t sig_len;
1750 enum ssl_private_key_result_t sign_result;
1751 if (ssl->state == SSL3_ST_CW_CERT_VRFY_A) {
1752 /* Select and write out the digest type in TLS 1.2. */
1753 const EVP_MD *md = NULL;
David Benjamina1e9cab2015-12-30 00:08:49 -05001754 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminfc825122015-12-18 01:57:43 -05001755 md = tls1_choose_signing_digest(ssl);
1756 if (!tls12_add_sigandhash(ssl, &cbb, md)) {
1757 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1758 goto err;
1759 }
1760 }
1761
1762 /* Compute the digest. In TLS 1.1 and below, the digest type is also
1763 * selected here. */
1764 uint8_t digest[EVP_MAX_MD_SIZE];
1765 size_t digest_len;
1766 if (!ssl3_cert_verify_hash(ssl, digest, &digest_len, &md,
1767 ssl_private_key_type(ssl))) {
1768 goto err;
1769 }
1770
1771 /* The handshake buffer is no longer necessary. */
1772 ssl3_free_handshake_buffer(ssl);
1773
1774 /* Sign the digest. */
1775 uint8_t *ptr;
1776 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1777 !CBB_reserve(&child, &ptr, max_sig_len)) {
1778 goto err;
1779 }
1780 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, md,
1781 digest, digest_len);
1782 } else {
1783 assert(ssl->state == SSL3_ST_CW_CERT_VRFY_B);
1784
1785 /* Skip over the already written signature algorithm and retry the
1786 * signature. */
1787 uint8_t *ptr;
David Benjamina1e9cab2015-12-30 00:08:49 -05001788 if ((ssl3_protocol_version(ssl) >= TLS1_2_VERSION &&
1789 !CBB_did_write(&cbb, 2)) ||
David Benjaminfc825122015-12-18 01:57:43 -05001790 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1791 !CBB_reserve(&child, &ptr, max_sig_len)) {
1792 goto err;
1793 }
1794 sign_result =
1795 ssl_private_key_sign_complete(ssl, ptr, &sig_len, max_sig_len);
1796 }
1797
1798 switch (sign_result) {
1799 case ssl_private_key_success:
David Benjaminfc825122015-12-18 01:57:43 -05001800 break;
1801 case ssl_private_key_failure:
David Benjaminfc825122015-12-18 01:57:43 -05001802 goto err;
1803 case ssl_private_key_retry:
1804 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
1805 ssl->state = SSL3_ST_CW_CERT_VRFY_B;
1806 goto err;
1807 }
1808
1809 size_t length;
1810 if (!CBB_did_write(&child, sig_len) ||
1811 !CBB_finish(&cbb, NULL, &length) ||
1812 !ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_VERIFY, length)) {
1813 goto err;
1814 }
1815
1816 ssl->state = SSL3_ST_CW_CERT_VRFY_C;
1817 return ssl_do_write(ssl);
1818
1819err:
1820 CBB_cleanup(&cbb);
1821 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001822}
1823
1824/* ssl3_has_client_certificate returns true if a client certificate is
1825 * configured. */
David Benjamind1d80782015-07-05 11:54:09 -04001826static int ssl3_has_client_certificate(SSL *ssl) {
nagendra modadugu601448a2015-07-24 09:31:31 -07001827 return ssl->cert && ssl->cert->x509 && ssl_has_private_key(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001828}
1829
David Benjamin0d56f882015-12-19 17:05:56 -05001830int ssl3_send_client_certificate(SSL *ssl) {
David Benjamin0d56f882015-12-19 17:05:56 -05001831 if (ssl->state == SSL3_ST_CW_CERT_A) {
David Benjamin454aa4c2016-03-10 08:43:27 -05001832 /* Call cert_cb to update the certificate. */
David Benjamin0d56f882015-12-19 17:05:56 -05001833 if (ssl->cert->cert_cb) {
David Benjamin454aa4c2016-03-10 08:43:27 -05001834 int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1835 if (ret < 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001836 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langley1bea1732014-12-17 19:06:57 -08001837 return -1;
1838 }
David Benjamin454aa4c2016-03-10 08:43:27 -05001839 if (ret == 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001840 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin454aa4c2016-03-10 08:43:27 -05001841 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001842 }
Adam Langley1bea1732014-12-17 19:06:57 -08001843 }
1844
David Benjamin0d56f882015-12-19 17:05:56 -05001845 if (ssl3_has_client_certificate(ssl)) {
1846 ssl->state = SSL3_ST_CW_CERT_C;
Adam Langley1bea1732014-12-17 19:06:57 -08001847 } else {
David Benjamin0d56f882015-12-19 17:05:56 -05001848 ssl->state = SSL3_ST_CW_CERT_B;
Adam Langley1bea1732014-12-17 19:06:57 -08001849 }
1850 }
1851
David Benjamin0d56f882015-12-19 17:05:56 -05001852 if (ssl->state == SSL3_ST_CW_CERT_B) {
David Benjamin454aa4c2016-03-10 08:43:27 -05001853 /* Call client_cert_cb to update the certificate. */
1854 X509 *x509 = NULL;
1855 EVP_PKEY *pkey = NULL;
1856 int ret = ssl_do_client_cert_cb(ssl, &x509, &pkey);
1857 if (ret < 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001858 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langley1bea1732014-12-17 19:06:57 -08001859 return -1;
1860 }
Adam Langley1bea1732014-12-17 19:06:57 -08001861
David Benjamin454aa4c2016-03-10 08:43:27 -05001862 int setup_error = ret == 1 && (!SSL_use_certificate(ssl, x509) ||
1863 !SSL_use_PrivateKey(ssl, pkey));
David Benjamin2755a3e2015-04-22 16:17:58 -04001864 X509_free(x509);
1865 EVP_PKEY_free(pkey);
David Benjamin454aa4c2016-03-10 08:43:27 -05001866 if (setup_error) {
1867 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1868 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001869 }
1870
David Benjamin0d56f882015-12-19 17:05:56 -05001871 ssl->state = SSL3_ST_CW_CERT_C;
Adam Langley1bea1732014-12-17 19:06:57 -08001872 }
1873
David Benjamin0d56f882015-12-19 17:05:56 -05001874 if (ssl->state == SSL3_ST_CW_CERT_C) {
David Benjamin454aa4c2016-03-10 08:43:27 -05001875 if (!ssl3_has_client_certificate(ssl)) {
1876 /* Without a client certificate, the handshake buffer may be released. */
1877 ssl3_free_handshake_buffer(ssl);
1878
1879 if (ssl->version == SSL3_VERSION) {
1880 /* In SSL 3.0, send no certificate by skipping both messages. */
1881 ssl->s3->tmp.cert_req = 0;
1882 ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
1883 return 1;
1884 }
1885
1886 /* In TLS, send an empty Certificate message. */
1887 ssl->s3->tmp.cert_req = 2;
David Benjamin0d56f882015-12-19 17:05:56 -05001888 uint8_t *p = ssl_handshake_start(ssl);
David Benjamind1d80782015-07-05 11:54:09 -04001889 l2n3(0, p);
David Benjamin0d56f882015-12-19 17:05:56 -05001890 if (!ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE, 3)) {
David Benjamind1d80782015-07-05 11:54:09 -04001891 return -1;
1892 }
David Benjamin0d56f882015-12-19 17:05:56 -05001893 } else if (!ssl3_output_cert_chain(ssl)) {
David Benjamin9d0847a2015-02-16 03:57:55 -05001894 return -1;
1895 }
David Benjamin0d56f882015-12-19 17:05:56 -05001896 ssl->state = SSL3_ST_CW_CERT_D;
Adam Langley1bea1732014-12-17 19:06:57 -08001897 }
1898
David Benjamin454aa4c2016-03-10 08:43:27 -05001899 assert(ssl->state == SSL3_ST_CW_CERT_D);
David Benjamin0d56f882015-12-19 17:05:56 -05001900 return ssl_do_write(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001901}
1902
David Benjamine97b9622015-10-10 14:59:09 -04001903int ssl3_send_next_proto(SSL *ssl) {
1904 if (ssl->state == SSL3_ST_CW_NEXT_PROTO_B) {
1905 return ssl_do_write(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001906 }
1907
David Benjamine97b9622015-10-10 14:59:09 -04001908 assert(ssl->state == SSL3_ST_CW_NEXT_PROTO_A);
1909
1910 static const uint8_t kZero[32] = {0};
David Benjamin79978df2015-12-25 15:56:49 -05001911 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
David Benjamine97b9622015-10-10 14:59:09 -04001912
1913 CBB cbb, child;
1914 size_t length;
1915 CBB_zero(&cbb);
1916 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1917 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1918 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjamin79978df2015-12-25 15:56:49 -05001919 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1920 ssl->s3->next_proto_negotiated_len) ||
David Benjamine97b9622015-10-10 14:59:09 -04001921 !CBB_add_u8_length_prefixed(&cbb, &child) ||
1922 !CBB_add_bytes(&child, kZero, padding_len) ||
1923 !CBB_finish(&cbb, NULL, &length) ||
1924 !ssl_set_handshake_header(ssl, SSL3_MT_NEXT_PROTO, length)) {
1925 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1926 CBB_cleanup(&cbb);
1927 return -1;
1928 }
1929
1930 ssl->state = SSL3_ST_CW_NEXT_PROTO_B;
1931 return ssl_do_write(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001932}
1933
David Benjamin96e97b12015-10-10 16:30:22 -04001934int ssl3_send_channel_id(SSL *ssl) {
1935 if (ssl->state == SSL3_ST_CW_CHANNEL_ID_B) {
1936 return ssl_do_write(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001937 }
1938
David Benjamin96e97b12015-10-10 16:30:22 -04001939 assert(ssl->state == SSL3_ST_CW_CHANNEL_ID_A);
1940
1941 if (ssl->tlsext_channel_id_private == NULL &&
1942 ssl->ctx->channel_id_cb != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001943 EVP_PKEY *key = NULL;
David Benjamin96e97b12015-10-10 16:30:22 -04001944 ssl->ctx->channel_id_cb(ssl, &key);
1945 if (key != NULL &&
1946 !SSL_set1_tls_channel_id(ssl, key)) {
1947 EVP_PKEY_free(key);
1948 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001949 }
David Benjamin96e97b12015-10-10 16:30:22 -04001950 EVP_PKEY_free(key);
Adam Langley1bea1732014-12-17 19:06:57 -08001951 }
1952
David Benjamin96e97b12015-10-10 16:30:22 -04001953 if (ssl->tlsext_channel_id_private == NULL) {
1954 ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
Adam Langley1bea1732014-12-17 19:06:57 -08001955 return -1;
1956 }
Adam Langley1bea1732014-12-17 19:06:57 -08001957
David Benjamin758d1272015-11-20 17:47:25 -05001958 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
1959 if (ec_key == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001960 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin961ad6a2015-06-12 01:40:23 -04001961 return -1;
1962 }
David Benjamin961ad6a2015-06-12 01:40:23 -04001963
David Benjamin96e97b12015-10-10 16:30:22 -04001964 int ret = -1;
David Benjamin96e97b12015-10-10 16:30:22 -04001965 BIGNUM *x = BN_new();
1966 BIGNUM *y = BN_new();
1967 ECDSA_SIG *sig = NULL;
1968 if (x == NULL || y == NULL ||
1969 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
1970 EC_KEY_get0_public_key(ec_key),
1971 x, y, NULL)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001972 goto err;
1973 }
1974
David Benjamin961ad6a2015-06-12 01:40:23 -04001975 uint8_t digest[EVP_MAX_MD_SIZE];
David Benjamind6a4ae92015-08-06 11:10:51 -04001976 size_t digest_len;
David Benjamin96e97b12015-10-10 16:30:22 -04001977 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001978 goto err;
1979 }
1980
David Benjamin961ad6a2015-06-12 01:40:23 -04001981 sig = ECDSA_do_sign(digest, digest_len, ec_key);
Adam Langley1bea1732014-12-17 19:06:57 -08001982 if (sig == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001983 goto err;
1984 }
1985
David Benjamin96e97b12015-10-10 16:30:22 -04001986 CBB cbb, child;
1987 size_t length;
1988 CBB_zero(&cbb);
1989 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1990 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1991 !CBB_add_u16(&cbb, TLSEXT_TYPE_channel_id) ||
1992 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001993 !BN_bn2cbb_padded(&child, 32, x) ||
1994 !BN_bn2cbb_padded(&child, 32, y) ||
1995 !BN_bn2cbb_padded(&child, 32, sig->r) ||
1996 !BN_bn2cbb_padded(&child, 32, sig->s) ||
David Benjamin96e97b12015-10-10 16:30:22 -04001997 !CBB_finish(&cbb, NULL, &length) ||
1998 !ssl_set_handshake_header(ssl, SSL3_MT_ENCRYPTED_EXTENSIONS, length)) {
David Benjamin3570d732015-06-29 00:28:17 -04001999 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin96e97b12015-10-10 16:30:22 -04002000 CBB_cleanup(&cbb);
Adam Langley1bea1732014-12-17 19:06:57 -08002001 goto err;
2002 }
2003
David Benjamin96e97b12015-10-10 16:30:22 -04002004 ssl->state = SSL3_ST_CW_CHANNEL_ID_B;
2005 ret = ssl_do_write(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08002006
2007err:
David Benjamin96e97b12015-10-10 16:30:22 -04002008 BN_free(x);
2009 BN_free(y);
David Benjamin2755a3e2015-04-22 16:17:58 -04002010 ECDSA_SIG_free(sig);
Adam Langley1bea1732014-12-17 19:06:57 -08002011 return ret;
2012}
2013
David Benjaminfd8e69f2015-10-03 10:49:20 -04002014int ssl_do_client_cert_cb(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey) {
2015 if (ssl->ctx->client_cert_cb == NULL) {
2016 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -08002017 }
David Benjamin454aa4c2016-03-10 08:43:27 -05002018
2019 int ret = ssl->ctx->client_cert_cb(ssl, out_x509, out_pkey);
2020 if (ret <= 0) {
2021 return ret;
2022 }
2023
2024 assert(*out_x509 != NULL);
2025 assert(*out_pkey != NULL);
2026 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08002027}
Paul Lietar8f1c2682015-08-18 12:21:54 +01002028
David Benjamin0d56f882015-12-19 17:05:56 -05002029int ssl3_verify_server_cert(SSL *ssl) {
2030 int ret = ssl_verify_cert_chain(ssl, ssl->session->cert_chain);
2031 if (ssl->verify_mode != SSL_VERIFY_NONE && ret <= 0) {
2032 int al = ssl_verify_alarm_type(ssl->verify_result);
2033 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Paul Lietar8f1c2682015-08-18 12:21:54 +01002034 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
2035 } else {
2036 ret = 1;
David Benjamin0d56f882015-12-19 17:05:56 -05002037 ERR_clear_error(); /* but we keep ssl->verify_result */
Paul Lietar8f1c2682015-08-18 12:21:54 +01002038 }
2039
2040 return ret;
2041}