blob: eb458f245d12f5f24f448430e03026fdc53fce10 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 *
113 * Portions of the attached software ("Contribution") are developed by
114 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115 *
116 * The Contribution is licensed pursuant to the OpenSSL open source
117 * license provided above.
118 *
119 * ECC cipher suite support in OpenSSL originally written by
120 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121 *
122 */
123/* ====================================================================
124 * Copyright 2005 Nokia. All rights reserved.
125 *
126 * The portions of the attached software ("Contribution") is developed by
127 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128 * license.
129 *
130 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132 * support (see RFC 4279) to OpenSSL.
133 *
134 * No patent licenses or other rights except those expressly stated in
135 * the OpenSSL open source license shall be deemed granted or received
136 * expressly, by implication, estoppel, or otherwise.
137 *
138 * No assurances are provided by Nokia that the Contribution does not
139 * infringe the patent or other intellectual property rights of any third
140 * party or that the license provides you with all the necessary rights
141 * to make use of the Contribution.
142 *
143 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147 * OTHERWISE. */
148
David Benjamin880b14e2014-08-25 22:35:07 -0400149#include <assert.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700150#include <stdio.h>
David Benjamin22f9bcc2014-07-13 12:29:21 -0400151#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700152
153#include <openssl/bn.h>
154#include <openssl/buf.h>
David Benjamindc72ff72014-06-25 12:36:10 -0400155#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700156#include <openssl/cipher.h>
157#include <openssl/dh.h>
Adam Langley1258b6a2014-06-20 12:00:00 -0700158#include <openssl/ec.h>
159#include <openssl/ecdsa.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700160#include <openssl/evp.h>
161#include <openssl/hmac.h>
162#include <openssl/md5.h>
163#include <openssl/mem.h>
164#include <openssl/obj.h>
165#include <openssl/rand.h>
Adam Langley1258b6a2014-06-20 12:00:00 -0700166#include <openssl/sha.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700167#include <openssl/x509.h>
168
169#include "ssl_locl.h"
Adam Langleyaf6e45b2014-11-03 19:34:49 -0800170#include "../crypto/internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700171#include "../crypto/dh/internal.h"
172
Adam Langleyfcf25832014-12-18 17:42:32 -0800173
David Benjamin4b755cb2014-12-12 03:58:07 -0500174/* INITIAL_SNIFF_BUFFER_SIZE is the number of bytes read in the initial sniff
175 * buffer. */
176#define INITIAL_SNIFF_BUFFER_SIZE 8
177
Adam Langleyfcf25832014-12-18 17:42:32 -0800178int ssl3_accept(SSL *s) {
179 BUF_MEM *buf = NULL;
180 unsigned long alg_a;
181 void (*cb)(const SSL *ssl, int type, int val) = NULL;
182 int ret = -1;
183 int new_state, state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700184
Adam Langleyfcf25832014-12-18 17:42:32 -0800185 assert(s->handshake_func == ssl3_accept);
186 assert(s->server);
187 assert(!SSL_IS_DTLS(s));
David Benjaminbeb47022014-11-30 02:58:52 -0500188
Adam Langleyfcf25832014-12-18 17:42:32 -0800189 ERR_clear_error();
190 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700191
Adam Langleyfcf25832014-12-18 17:42:32 -0800192 if (s->info_callback != NULL) {
193 cb = s->info_callback;
194 } else if (s->ctx->info_callback != NULL) {
195 cb = s->ctx->info_callback;
196 }
Adam Langley95c29f32014-06-20 12:00:00 -0700197
Adam Langleyfcf25832014-12-18 17:42:32 -0800198 s->in_handshake++;
Adam Langley95c29f32014-06-20 12:00:00 -0700199
Adam Langleyfcf25832014-12-18 17:42:32 -0800200 if (s->cert == NULL) {
201 OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_NO_CERTIFICATE_SET);
202 return -1;
203 }
Adam Langley95c29f32014-06-20 12:00:00 -0700204
Adam Langleyfcf25832014-12-18 17:42:32 -0800205 for (;;) {
206 state = s->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700207
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 switch (s->state) {
209 case SSL_ST_RENEGOTIATE:
210 /* This state is the renegotiate entry point. It sends a HelloRequest
211 * and nothing else. */
212 s->renegotiate = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700213
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 if (cb != NULL) {
215 cb(s, SSL_CB_HANDSHAKE_START, 1);
216 }
Adam Langley95c29f32014-06-20 12:00:00 -0700217
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 if (s->init_buf == NULL) {
219 buf = BUF_MEM_new();
220 if (!buf || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
221 ret = -1;
222 goto end;
223 }
224 s->init_buf = buf;
225 buf = NULL;
226 }
227 s->init_num = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500228
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 if (!ssl3_setup_buffers(s)) {
230 ret = -1;
231 goto end;
232 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500233
Adam Langleyfcf25832014-12-18 17:42:32 -0800234 if (!s->s3->send_connection_binding &&
235 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
236 /* Server attempting to renegotiate with client that doesn't support
237 * secure renegotiation. */
238 OPENSSL_PUT_ERROR(SSL, ssl3_accept,
239 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
240 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
241 ret = -1;
242 goto end;
243 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 s->state = SSL3_ST_SW_HELLO_REQ_A;
246 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 case SSL3_ST_SW_HELLO_REQ_A:
249 case SSL3_ST_SW_HELLO_REQ_B:
250 s->shutdown = 0;
251 ret = ssl3_send_hello_request(s);
252 if (ret <= 0) {
253 goto end;
254 }
255 s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C;
256 s->state = SSL3_ST_SW_FLUSH;
257 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700258
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 if (!ssl3_init_finished_mac(s)) {
260 OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR);
261 ret = -1;
262 goto end;
263 }
264 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 case SSL3_ST_SW_HELLO_REQ_C:
267 s->state = SSL_ST_OK;
268 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700269
Adam Langleyfcf25832014-12-18 17:42:32 -0800270 case SSL_ST_ACCEPT:
271 case SSL_ST_BEFORE | SSL_ST_ACCEPT:
272 /* This state is the entry point for the handshake itself (initial and
273 * renegotiation). */
274 if (cb != NULL) {
275 cb(s, SSL_CB_HANDSHAKE_START, 1);
276 }
Adam Langley95c29f32014-06-20 12:00:00 -0700277
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 if (s->init_buf == NULL) {
279 buf = BUF_MEM_new();
280 if (!buf || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
281 ret = -1;
282 goto end;
283 }
284 s->init_buf = buf;
285 buf = NULL;
286 }
287 s->init_num = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500288
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 if (!ssl3_init_finished_mac(s)) {
290 OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR);
291 ret = -1;
292 goto end;
293 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500294
Adam Langleyfcf25832014-12-18 17:42:32 -0800295 if (!s->s3->have_version) {
296 /* This is the initial handshake. The record layer has not been
297 * initialized yet. Sniff for a V2ClientHello before reading a
298 * ClientHello normally. */
299 assert(s->s3->rbuf.buf == NULL);
300 assert(s->s3->wbuf.buf == NULL);
301 s->state = SSL3_ST_SR_INITIAL_BYTES;
302 } else {
303 /* Enable a write buffer. This groups handshake messages within a
304 * flight into a single write. */
305 if (!ssl3_setup_buffers(s) || !ssl_init_wbio_buffer(s, 1)) {
306 ret = -1;
307 goto end;
308 }
309 s->state = SSL3_ST_SR_CLNT_HELLO_A;
310 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800311 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500312
Adam Langleyfcf25832014-12-18 17:42:32 -0800313 case SSL3_ST_SR_INITIAL_BYTES:
314 ret = ssl3_get_initial_bytes(s);
315 if (ret <= 0) {
316 goto end;
317 }
318 /* ssl3_get_initial_bytes sets s->state to one of
319 * SSL3_ST_SR_V2_CLIENT_HELLO or SSL3_ST_SR_CLNT_HELLO_A on success. */
320 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500321
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 case SSL3_ST_SR_V2_CLIENT_HELLO:
323 ret = ssl3_get_v2_client_hello(s);
324 if (ret <= 0) {
325 goto end;
326 }
327 s->state = SSL3_ST_SR_CLNT_HELLO_A;
328 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 case SSL3_ST_SR_CLNT_HELLO_A:
331 case SSL3_ST_SR_CLNT_HELLO_B:
332 case SSL3_ST_SR_CLNT_HELLO_C:
333 case SSL3_ST_SR_CLNT_HELLO_D:
334 s->shutdown = 0;
335 ret = ssl3_get_client_hello(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800336 if (ret <= 0) {
337 goto end;
338 }
339 s->renegotiate = 2;
340 s->state = SSL3_ST_SW_SRVR_HELLO_A;
341 s->init_num = 0;
342 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500343
Adam Langleyfcf25832014-12-18 17:42:32 -0800344 case SSL3_ST_SW_SRVR_HELLO_A:
345 case SSL3_ST_SW_SRVR_HELLO_B:
346 ret = ssl3_send_server_hello(s);
347 if (ret <= 0) {
348 goto end;
349 }
350 if (s->hit) {
351 if (s->tlsext_ticket_expected) {
352 s->state = SSL3_ST_SW_SESSION_TICKET_A;
353 } else {
354 s->state = SSL3_ST_SW_CHANGE_A;
355 }
356 } else {
357 s->state = SSL3_ST_SW_CERT_A;
358 }
359 s->init_num = 0;
360 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700361
Adam Langleyfcf25832014-12-18 17:42:32 -0800362 case SSL3_ST_SW_CERT_A:
363 case SSL3_ST_SW_CERT_B:
364 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
365 ret = ssl3_send_server_certificate(s);
366 if (ret <= 0) {
367 goto end;
368 }
369 if (s->s3->tmp.certificate_status_expected) {
370 s->state = SSL3_ST_SW_CERT_STATUS_A;
371 } else {
372 s->state = SSL3_ST_SW_KEY_EXCH_A;
373 }
374 } else {
375 skip = 1;
376 s->state = SSL3_ST_SW_KEY_EXCH_A;
377 }
378 s->init_num = 0;
379 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 case SSL3_ST_SW_KEY_EXCH_A:
382 case SSL3_ST_SW_KEY_EXCH_B:
383 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700384
Adam Langleyfcf25832014-12-18 17:42:32 -0800385 /* Send a ServerKeyExchange message if:
386 * - The key exchange is ephemeral or anonymous
387 * Diffie-Hellman.
388 * - There is a PSK identity hint.
389 *
390 * TODO(davidben): This logic is currently duplicated in d1_srvr.c. Fix
391 * this. In the meantime, keep them in sync. */
392 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) ||
393 ((alg_a & SSL_aPSK) && s->psk_identity_hint)) {
394 ret = ssl3_send_server_key_exchange(s);
David Benjamin6eb000d2015-02-11 01:17:41 -0500395 if (ret <= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500397 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800398 } else {
399 skip = 1;
400 }
Adam Langley95c29f32014-06-20 12:00:00 -0700401
Adam Langleyfcf25832014-12-18 17:42:32 -0800402 s->state = SSL3_ST_SW_CERT_REQ_A;
403 s->init_num = 0;
404 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 case SSL3_ST_SW_CERT_REQ_A:
407 case SSL3_ST_SW_CERT_REQ_B:
408 if (/* don't request cert unless asked for it: */
409 !(s->verify_mode & SSL_VERIFY_PEER) ||
410 /* Don't request a certificate if an obc was presented */
411 ((s->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
412 s->s3->tlsext_channel_id_valid) ||
413 /* if SSL_VERIFY_CLIENT_ONCE is set,
414 * don't request cert during re-negotiation: */
415 ((s->session->peer != NULL) &&
416 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800417 /* With normal PSK Certificates and
418 * Certificate Requests are omitted */
419 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
420 /* no cert request */
421 skip = 1;
422 s->s3->tmp.cert_request = 0;
423 s->state = SSL3_ST_SW_SRVR_DONE_A;
424 if (s->s3->handshake_buffer &&
425 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
426 return -1;
427 }
428 } else {
429 s->s3->tmp.cert_request = 1;
430 ret = ssl3_send_certificate_request(s);
431 if (ret <= 0) {
432 goto end;
433 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800434 s->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800435 s->init_num = 0;
436 }
437 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700438
Adam Langleyfcf25832014-12-18 17:42:32 -0800439 case SSL3_ST_SW_SRVR_DONE_A:
440 case SSL3_ST_SW_SRVR_DONE_B:
441 ret = ssl3_send_server_done(s);
442 if (ret <= 0) {
443 goto end;
444 }
445 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
446 s->state = SSL3_ST_SW_FLUSH;
447 s->init_num = 0;
448 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700449
Adam Langleyfcf25832014-12-18 17:42:32 -0800450 case SSL3_ST_SW_FLUSH:
451 /* This code originally checked to see if any data was pending using
452 * BIO_CTRL_INFO and then flushed. This caused problems as documented
453 * in PR#1939. The proposed fix doesn't completely resolve this issue
454 * as buggy implementations of BIO_CTRL_PENDING still exist. So instead
455 * we just flush unconditionally. */
456 s->rwstate = SSL_WRITING;
457 if (BIO_flush(s->wbio) <= 0) {
458 ret = -1;
459 goto end;
460 }
461 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700462
Adam Langleyfcf25832014-12-18 17:42:32 -0800463 s->state = s->s3->tmp.next_state;
464 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700465
Adam Langleyfcf25832014-12-18 17:42:32 -0800466 case SSL3_ST_SR_CERT_A:
467 case SSL3_ST_SR_CERT_B:
468 if (s->s3->tmp.cert_request) {
469 ret = ssl3_get_client_certificate(s);
470 if (ret <= 0) {
471 goto end;
472 }
473 }
474 s->init_num = 0;
475 s->state = SSL3_ST_SR_KEY_EXCH_A;
476 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700477
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 case SSL3_ST_SR_KEY_EXCH_A:
479 case SSL3_ST_SR_KEY_EXCH_B:
480 ret = ssl3_get_client_key_exchange(s);
481 if (ret <= 0) {
482 goto end;
483 }
484 s->state = SSL3_ST_SR_CERT_VRFY_A;
485 s->init_num = 0;
486 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700487
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 case SSL3_ST_SR_CERT_VRFY_A:
489 case SSL3_ST_SR_CERT_VRFY_B:
490 ret = ssl3_get_cert_verify(s);
491 if (ret <= 0) {
492 goto end;
493 }
Adam Langley95c29f32014-06-20 12:00:00 -0700494
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 s->state = SSL3_ST_SR_CHANGE;
496 s->init_num = 0;
497 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700498
Adam Langleyfcf25832014-12-18 17:42:32 -0800499 case SSL3_ST_SR_CHANGE: {
500 char next_proto_neg = 0;
501 char channel_id = 0;
502 next_proto_neg = s->s3->next_proto_neg_seen;
503 channel_id = s->s3->tlsext_channel_id_valid;
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Adam Langleyfcf25832014-12-18 17:42:32 -0800505 /* At this point, the next message must be entirely behind a
506 * ChangeCipherSpec. */
507 if (!ssl3_expect_change_cipher_spec(s)) {
508 ret = -1;
509 goto end;
510 }
511 if (next_proto_neg) {
512 s->state = SSL3_ST_SR_NEXT_PROTO_A;
513 } else if (channel_id) {
514 s->state = SSL3_ST_SR_CHANNEL_ID_A;
515 } else {
516 s->state = SSL3_ST_SR_FINISHED_A;
517 }
518 break;
519 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700520
Adam Langleyfcf25832014-12-18 17:42:32 -0800521 case SSL3_ST_SR_NEXT_PROTO_A:
522 case SSL3_ST_SR_NEXT_PROTO_B:
523 ret = ssl3_get_next_proto(s);
524 if (ret <= 0) {
525 goto end;
526 }
527 s->init_num = 0;
528 if (s->s3->tlsext_channel_id_valid) {
529 s->state = SSL3_ST_SR_CHANNEL_ID_A;
530 } else {
531 s->state = SSL3_ST_SR_FINISHED_A;
532 }
533 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700534
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 case SSL3_ST_SR_CHANNEL_ID_A:
536 case SSL3_ST_SR_CHANNEL_ID_B:
537 ret = ssl3_get_channel_id(s);
538 if (ret <= 0) {
539 goto end;
540 }
541 s->init_num = 0;
542 s->state = SSL3_ST_SR_FINISHED_A;
543 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700544
Adam Langleyfcf25832014-12-18 17:42:32 -0800545 case SSL3_ST_SR_FINISHED_A:
546 case SSL3_ST_SR_FINISHED_B:
547 ret =
548 ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, SSL3_ST_SR_FINISHED_B);
549 if (ret <= 0) {
550 goto end;
551 }
Adam Langley95c29f32014-06-20 12:00:00 -0700552
Adam Langleyfcf25832014-12-18 17:42:32 -0800553 if (s->hit) {
554 s->state = SSL_ST_OK;
555 } else if (s->tlsext_ticket_expected) {
556 s->state = SSL3_ST_SW_SESSION_TICKET_A;
557 } else {
558 s->state = SSL3_ST_SW_CHANGE_A;
559 }
560 /* If this is a full handshake with ChannelID then record the hashshake
561 * hashes in |s->session| in case we need them to verify a ChannelID
562 * signature on a resumption of this session in the future. */
563 if (!s->hit && s->s3->tlsext_channel_id_new) {
564 ret = tls1_record_handshake_hashes_for_channel_id(s);
565 if (ret <= 0) {
566 goto end;
567 }
568 }
569 s->init_num = 0;
570 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700571
Adam Langleyfcf25832014-12-18 17:42:32 -0800572 case SSL3_ST_SW_SESSION_TICKET_A:
573 case SSL3_ST_SW_SESSION_TICKET_B:
574 ret = ssl3_send_new_session_ticket(s);
575 if (ret <= 0) {
576 goto end;
577 }
578 s->state = SSL3_ST_SW_CHANGE_A;
579 s->init_num = 0;
580 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700581
Adam Langleyfcf25832014-12-18 17:42:32 -0800582 case SSL3_ST_SW_CHANGE_A:
583 case SSL3_ST_SW_CHANGE_B:
584 s->session->cipher = s->s3->tmp.new_cipher;
585 if (!s->enc_method->setup_key_block(s)) {
586 ret = -1;
587 goto end;
588 }
Adam Langley95c29f32014-06-20 12:00:00 -0700589
Adam Langleyfcf25832014-12-18 17:42:32 -0800590 ret = ssl3_send_change_cipher_spec(s, SSL3_ST_SW_CHANGE_A,
591 SSL3_ST_SW_CHANGE_B);
592 if (ret <= 0) {
593 goto end;
594 }
595 s->state = SSL3_ST_SW_FINISHED_A;
596 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700597
Adam Langleyfcf25832014-12-18 17:42:32 -0800598 if (!s->enc_method->change_cipher_state(
599 s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
600 ret = -1;
601 goto end;
602 }
603 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700604
Adam Langleyfcf25832014-12-18 17:42:32 -0800605 case SSL3_ST_SW_FINISHED_A:
606 case SSL3_ST_SW_FINISHED_B:
607 ret =
608 ssl3_send_finished(s, SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B,
609 s->enc_method->server_finished_label,
610 s->enc_method->server_finished_label_len);
611 if (ret <= 0) {
612 goto end;
613 }
614 s->state = SSL3_ST_SW_FLUSH;
615 if (s->hit) {
616 s->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
617 } else {
618 s->s3->tmp.next_state = SSL_ST_OK;
619 }
620 s->init_num = 0;
621 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700622
Adam Langleyfcf25832014-12-18 17:42:32 -0800623 case SSL_ST_OK:
624 /* clean a few things up */
625 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700626
Adam Langleyfcf25832014-12-18 17:42:32 -0800627 BUF_MEM_free(s->init_buf);
628 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700629
Adam Langleyfcf25832014-12-18 17:42:32 -0800630 /* remove buffering on output */
631 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700632
Adam Langleyfcf25832014-12-18 17:42:32 -0800633 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700634
Adam Langleyfcf25832014-12-18 17:42:32 -0800635 /* If we aren't retaining peer certificates then we can discard it
636 * now. */
637 if (s->session->peer && s->ctx->retain_only_sha256_of_client_certs) {
638 X509_free(s->session->peer);
639 s->session->peer = NULL;
640 }
Adam Langley95c29f32014-06-20 12:00:00 -0700641
Adam Langleyfcf25832014-12-18 17:42:32 -0800642 if (s->renegotiate == 2) {
643 /* skipped if we just sent a HelloRequest */
644 s->renegotiate = 0;
645 s->new_session = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700646
Adam Langleyfcf25832014-12-18 17:42:32 -0800647 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
Adam Langley95c29f32014-06-20 12:00:00 -0700648
Adam Langleyfcf25832014-12-18 17:42:32 -0800649 if (cb != NULL) {
650 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
651 }
652 }
Adam Langley75872532014-06-20 12:00:00 -0700653
Adam Langleyfcf25832014-12-18 17:42:32 -0800654 ret = 1;
655 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700656
Adam Langleyfcf25832014-12-18 17:42:32 -0800657 default:
658 OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNKNOWN_STATE);
659 ret = -1;
660 goto end;
661 }
Adam Langley95c29f32014-06-20 12:00:00 -0700662
Adam Langleyfcf25832014-12-18 17:42:32 -0800663 if (!s->s3->tmp.reuse_message && !skip && cb != NULL && s->state != state) {
664 new_state = s->state;
665 s->state = state;
666 cb(s, SSL_CB_ACCEPT_LOOP, 1);
667 s->state = new_state;
668 }
669 skip = 0;
670 }
671
Adam Langley95c29f32014-06-20 12:00:00 -0700672end:
Adam Langleyfcf25832014-12-18 17:42:32 -0800673 s->in_handshake--;
674 if (buf != NULL) {
675 BUF_MEM_free(buf);
676 }
677 if (cb != NULL) {
678 cb(s, SSL_CB_ACCEPT_EXIT, ret);
679 }
680 return ret;
681}
Adam Langley95c29f32014-06-20 12:00:00 -0700682
Adam Langleyfcf25832014-12-18 17:42:32 -0800683static int ssl3_read_sniff_buffer(SSL *s, size_t n) {
684 if (s->s3->sniff_buffer == NULL) {
685 s->s3->sniff_buffer = BUF_MEM_new();
686 }
687 if (s->s3->sniff_buffer == NULL || !BUF_MEM_grow(s->s3->sniff_buffer, n)) {
688 return -1;
689 }
Adam Langley95c29f32014-06-20 12:00:00 -0700690
Adam Langleyfcf25832014-12-18 17:42:32 -0800691 while (s->s3->sniff_buffer_len < n) {
692 int ret;
David Benjamin4b755cb2014-12-12 03:58:07 -0500693
Adam Langleyfcf25832014-12-18 17:42:32 -0800694 s->rwstate = SSL_READING;
695 ret = BIO_read(s->rbio, s->s3->sniff_buffer->data + s->s3->sniff_buffer_len,
696 n - s->s3->sniff_buffer_len);
697 if (ret <= 0) {
698 return ret;
699 }
700 s->rwstate = SSL_NOTHING;
701 s->s3->sniff_buffer_len += ret;
702 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500703
Adam Langleyfcf25832014-12-18 17:42:32 -0800704 return 1;
705}
David Benjamin4b755cb2014-12-12 03:58:07 -0500706
Adam Langleyfcf25832014-12-18 17:42:32 -0800707int ssl3_get_initial_bytes(SSL *s) {
708 int ret;
709 const uint8_t *p;
David Benjamin4b755cb2014-12-12 03:58:07 -0500710
Adam Langleyfcf25832014-12-18 17:42:32 -0800711 /* Read the first 8 bytes. To recognize a ClientHello or V2ClientHello only
712 * needs the first 6 bytes, but 8 is needed to recognize CONNECT below. */
713 ret = ssl3_read_sniff_buffer(s, INITIAL_SNIFF_BUFFER_SIZE);
714 if (ret <= 0) {
715 return ret;
716 }
717 assert(s->s3->sniff_buffer_len >= INITIAL_SNIFF_BUFFER_SIZE);
718 p = (const uint8_t *)s->s3->sniff_buffer->data;
David Benjamin4b755cb2014-12-12 03:58:07 -0500719
Adam Langleyfcf25832014-12-18 17:42:32 -0800720 /* Some dedicated error codes for protocol mixups should the application wish
721 * to interpret them differently. (These do not overlap with ClientHello or
722 * V2ClientHello.) */
723 if (strncmp("GET ", (const char *)p, 4) == 0 ||
724 strncmp("POST ", (const char *)p, 5) == 0 ||
725 strncmp("HEAD ", (const char *)p, 5) == 0 ||
726 strncmp("PUT ", (const char *)p, 4) == 0) {
727 OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_HTTP_REQUEST);
728 return -1;
729 }
730 if (strncmp("CONNECT ", (const char *)p, 8) == 0) {
731 OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_HTTPS_PROXY_REQUEST);
732 return -1;
733 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500734
Adam Langleyfcf25832014-12-18 17:42:32 -0800735 /* Determine if this is a ClientHello or V2ClientHello. */
736 if ((p[0] & 0x80) && p[2] == SSL2_MT_CLIENT_HELLO &&
737 p[3] >= SSL3_VERSION_MAJOR) {
738 /* This is a V2ClientHello. */
739 s->state = SSL3_ST_SR_V2_CLIENT_HELLO;
740 return 1;
741 }
742 if (p[0] == SSL3_RT_HANDSHAKE && p[1] >= SSL3_VERSION_MAJOR &&
743 p[5] == SSL3_MT_CLIENT_HELLO) {
744 /* This is a ClientHello. Initialize the record layer with the already
745 * consumed data and continue the handshake. */
746 if (!ssl3_setup_buffers(s) || !ssl_init_wbio_buffer(s, 1)) {
747 return -1;
748 }
749 assert(s->rstate == SSL_ST_READ_HEADER);
750 memcpy(s->s3->rbuf.buf, p, s->s3->sniff_buffer_len);
751 s->s3->rbuf.offset = 0;
752 s->s3->rbuf.left = s->s3->sniff_buffer_len;
753 s->packet_length = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500754
Adam Langleyfcf25832014-12-18 17:42:32 -0800755 BUF_MEM_free(s->s3->sniff_buffer);
756 s->s3->sniff_buffer = NULL;
757 s->s3->sniff_buffer_len = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500758
Adam Langleyfcf25832014-12-18 17:42:32 -0800759 s->state = SSL3_ST_SR_CLNT_HELLO_A;
760 return 1;
761 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500762
Adam Langleyfcf25832014-12-18 17:42:32 -0800763 OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_UNKNOWN_PROTOCOL);
764 return -1;
765}
David Benjamin4b755cb2014-12-12 03:58:07 -0500766
Adam Langleyfcf25832014-12-18 17:42:32 -0800767int ssl3_get_v2_client_hello(SSL *s) {
768 const uint8_t *p;
769 int ret;
770 CBS v2_client_hello, cipher_specs, session_id, challenge;
771 size_t msg_length, rand_len, len;
772 uint8_t msg_type;
773 uint16_t version, cipher_spec_length, session_id_length, challenge_length;
774 CBB client_hello, hello_body, cipher_suites;
775 uint8_t random[SSL3_RANDOM_SIZE];
David Benjamin4b755cb2014-12-12 03:58:07 -0500776
Adam Langleyfcf25832014-12-18 17:42:32 -0800777 /* Read the remainder of the V2ClientHello. We have previously read 8 bytes
778 * in ssl3_get_initial_bytes. */
779 assert(s->s3->sniff_buffer_len >= INITIAL_SNIFF_BUFFER_SIZE);
780 p = (const uint8_t *)s->s3->sniff_buffer->data;
781 msg_length = ((p[0] & 0x7f) << 8) | p[1];
782 if (msg_length > (1024 * 4)) {
783 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_RECORD_TOO_LARGE);
784 return -1;
785 }
786 if (msg_length < INITIAL_SNIFF_BUFFER_SIZE - 2) {
787 /* Reject lengths that are too short early. We have already read 8 bytes,
788 * so we should not attempt to process an (invalid) V2ClientHello which
789 * would be shorter than that. */
790 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello,
791 SSL_R_RECORD_LENGTH_MISMATCH);
792 return -1;
793 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500794
Adam Langleyfcf25832014-12-18 17:42:32 -0800795 ret = ssl3_read_sniff_buffer(s, msg_length + 2);
796 if (ret <= 0) {
797 return ret;
798 }
799 assert(s->s3->sniff_buffer_len == msg_length + 2);
800 CBS_init(&v2_client_hello, (const uint8_t *)s->s3->sniff_buffer->data + 2,
801 msg_length);
David Benjamin4b755cb2014-12-12 03:58:07 -0500802
Adam Langleyfcf25832014-12-18 17:42:32 -0800803 /* The V2ClientHello without the length is incorporated into the Finished
804 * hash. */
David Benjaminfbdfefb2015-02-16 19:33:53 -0500805 if (!ssl3_finish_mac(s, CBS_data(&v2_client_hello),
806 CBS_len(&v2_client_hello))) {
807 return -1;
808 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800809 if (s->msg_callback) {
810 s->msg_callback(0, SSL2_VERSION, 0, CBS_data(&v2_client_hello),
811 CBS_len(&v2_client_hello), s, s->msg_callback_arg);
812 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500813
Adam Langleyfcf25832014-12-18 17:42:32 -0800814 if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
815 !CBS_get_u16(&v2_client_hello, &version) ||
816 !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
817 !CBS_get_u16(&v2_client_hello, &session_id_length) ||
818 !CBS_get_u16(&v2_client_hello, &challenge_length) ||
819 !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
820 !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
821 !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
822 CBS_len(&v2_client_hello) != 0) {
823 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_DECODE_ERROR);
824 return -1;
825 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500826
Adam Langleyfcf25832014-12-18 17:42:32 -0800827 /* msg_type has already been checked. */
828 assert(msg_type == SSL2_MT_CLIENT_HELLO);
David Benjamin4b755cb2014-12-12 03:58:07 -0500829
Adam Langleyfcf25832014-12-18 17:42:32 -0800830 /* The client_random is the V2ClientHello challenge. Truncate or
831 * left-pad with zeros as needed. */
832 memset(random, 0, SSL3_RANDOM_SIZE);
833 rand_len = CBS_len(&challenge);
834 if (rand_len > SSL3_RANDOM_SIZE) {
835 rand_len = SSL3_RANDOM_SIZE;
836 }
837 memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
838 rand_len);
David Benjamin4b755cb2014-12-12 03:58:07 -0500839
Adam Langleyfcf25832014-12-18 17:42:32 -0800840 /* Write out an equivalent SSLv3 ClientHello. */
841 if (!CBB_init_fixed(&client_hello, (uint8_t *)s->init_buf->data,
842 s->init_buf->max)) {
843 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_MALLOC_FAILURE);
844 return -1;
845 }
846 if (!CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
847 !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
848 !CBB_add_u16(&hello_body, version) ||
849 !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
850 /* No session id. */
851 !CBB_add_u8(&hello_body, 0) ||
852 !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
853 CBB_cleanup(&client_hello);
854 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
855 return -1;
856 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500857
Adam Langleyfcf25832014-12-18 17:42:32 -0800858 /* Copy the cipher suites. */
859 while (CBS_len(&cipher_specs) > 0) {
860 uint32_t cipher_spec;
861 if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
862 CBB_cleanup(&client_hello);
863 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_DECODE_ERROR);
864 return -1;
865 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500866
Adam Langleyfcf25832014-12-18 17:42:32 -0800867 /* Skip SSLv2 ciphers. */
868 if ((cipher_spec & 0xff0000) != 0) {
869 continue;
870 }
871 if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
872 CBB_cleanup(&client_hello);
873 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
874 return -1;
875 }
876 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500877
Adam Langleyfcf25832014-12-18 17:42:32 -0800878 /* Add the null compression scheme and finish. */
879 if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) ||
880 !CBB_finish(&client_hello, NULL, &len)) {
881 CBB_cleanup(&client_hello);
882 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
883 return -1;
884 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500885
Adam Langleyfcf25832014-12-18 17:42:32 -0800886 /* Mark the message for "re"-use by the version-specific method. */
887 s->s3->tmp.reuse_message = 1;
888 s->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO;
889 /* The handshake message header is 4 bytes. */
890 s->s3->tmp.message_size = len - 4;
David Benjamin4b755cb2014-12-12 03:58:07 -0500891
Adam Langleyfcf25832014-12-18 17:42:32 -0800892 /* Initialize the record layer. */
893 if (!ssl3_setup_buffers(s) || !ssl_init_wbio_buffer(s, 1)) {
894 return -1;
895 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500896
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 /* Drop the sniff buffer. */
898 BUF_MEM_free(s->s3->sniff_buffer);
899 s->s3->sniff_buffer = NULL;
900 s->s3->sniff_buffer_len = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500901
Adam Langleyfcf25832014-12-18 17:42:32 -0800902 return 1;
903}
David Benjamin4b755cb2014-12-12 03:58:07 -0500904
Adam Langleyfcf25832014-12-18 17:42:32 -0800905int ssl3_send_hello_request(SSL *s) {
906 if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
David Benjaminfbdfefb2015-02-16 19:33:53 -0500907 if (!ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0)) {
908 return -1;
909 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800910 s->state = SSL3_ST_SW_HELLO_REQ_B;
911 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500912
Adam Langleyfcf25832014-12-18 17:42:32 -0800913 /* SSL3_ST_SW_HELLO_REQ_B */
914 return ssl_do_write(s);
915}
Adam Langley95c29f32014-06-20 12:00:00 -0700916
Adam Langleyfcf25832014-12-18 17:42:32 -0800917int ssl3_get_client_hello(SSL *s) {
918 int i, ok, al = SSL_AD_INTERNAL_ERROR, ret = -1;
919 long n;
920 const SSL_CIPHER *c;
921 STACK_OF(SSL_CIPHER) *ciphers = NULL;
922 struct ssl_early_callback_ctx early_ctx;
923 CBS client_hello;
924 uint16_t client_version;
925 CBS client_random, session_id, cipher_suites, compression_methods;
Adam Langley95c29f32014-06-20 12:00:00 -0700926
Adam Langleyfcf25832014-12-18 17:42:32 -0800927 /* We do this so that we will respond with our native type. If we are TLSv1
928 * and we get SSLv3, we will respond with TLSv1, This down switching should
929 * be handled by a different method. If we are SSLv3, we will respond with
930 * SSLv3, even if prompted with TLSv1. */
931 switch (s->state) {
932 case SSL3_ST_SR_CLNT_HELLO_A:
933 case SSL3_ST_SR_CLNT_HELLO_B:
934 n = s->method->ssl_get_message(
935 s, SSL3_ST_SR_CLNT_HELLO_A, SSL3_ST_SR_CLNT_HELLO_B,
936 SSL3_MT_CLIENT_HELLO, SSL3_RT_MAX_PLAIN_LENGTH,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500937 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langleyfcf25832014-12-18 17:42:32 -0800939 if (!ok) {
940 return n;
941 }
Adam Langley95c29f32014-06-20 12:00:00 -0700942
Adam Langleyfcf25832014-12-18 17:42:32 -0800943 s->state = SSL3_ST_SR_CLNT_HELLO_C;
944 /* fallthrough */
945 case SSL3_ST_SR_CLNT_HELLO_C:
946 case SSL3_ST_SR_CLNT_HELLO_D:
947 /* We have previously parsed the ClientHello message, and can't call
948 * ssl_get_message again without hashing the message into the Finished
949 * digest again. */
950 n = s->init_num;
Adam Langleydc9b1412014-06-20 12:00:00 -0700951
Adam Langleyfcf25832014-12-18 17:42:32 -0800952 memset(&early_ctx, 0, sizeof(early_ctx));
953 early_ctx.ssl = s;
954 early_ctx.client_hello = s->init_msg;
955 early_ctx.client_hello_len = n;
956 if (!ssl_early_callback_init(&early_ctx)) {
957 al = SSL_AD_DECODE_ERROR;
958 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
959 SSL_R_CLIENTHELLO_PARSE_FAILED);
960 goto f_err;
961 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400962
Adam Langleyfcf25832014-12-18 17:42:32 -0800963 if (s->state == SSL3_ST_SR_CLNT_HELLO_C &&
964 s->ctx->select_certificate_cb != NULL) {
965 s->state = SSL3_ST_SR_CLNT_HELLO_D;
966 switch (s->ctx->select_certificate_cb(&early_ctx)) {
967 case 0:
David Benjamin2fff5bf2015-02-09 04:32:28 -0500968 s->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
969 goto err;
Adam Langleydc9b1412014-06-20 12:00:00 -0700970
Adam Langleyfcf25832014-12-18 17:42:32 -0800971 case -1:
972 /* Connection rejected. */
973 al = SSL_AD_ACCESS_DENIED;
974 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
975 SSL_R_CONNECTION_REJECTED);
976 goto f_err;
Adam Langleydc9b1412014-06-20 12:00:00 -0700977
Adam Langleyfcf25832014-12-18 17:42:32 -0800978 default:
979 /* fallthrough */;
980 }
981 }
982 s->state = SSL3_ST_SR_CLNT_HELLO_D;
983 break;
Adam Langleyb2ce0582014-06-20 12:00:00 -0700984
Adam Langleyfcf25832014-12-18 17:42:32 -0800985 default:
986 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNKNOWN_STATE);
987 return -1;
988 }
Adam Langley95c29f32014-06-20 12:00:00 -0700989
Adam Langleyfcf25832014-12-18 17:42:32 -0800990 CBS_init(&client_hello, s->init_msg, n);
991 if (!CBS_get_u16(&client_hello, &client_version) ||
992 !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) ||
993 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
994 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
995 al = SSL_AD_DECODE_ERROR;
996 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
997 goto f_err;
998 }
Adam Langley95c29f32014-06-20 12:00:00 -0700999
Adam Langleyfcf25832014-12-18 17:42:32 -08001000 /* use version from inside client hello, not from record header (may differ:
1001 * see RFC 2246, Appendix E, second paragraph) */
1002 s->client_version = client_version;
Adam Langley95c29f32014-06-20 12:00:00 -07001003
Adam Langleyfcf25832014-12-18 17:42:32 -08001004 /* Load the client random. */
1005 memcpy(s->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -07001006
Adam Langleyfcf25832014-12-18 17:42:32 -08001007 if (SSL_IS_DTLS(s)) {
1008 CBS cookie;
Adam Langley95c29f32014-06-20 12:00:00 -07001009
Adam Langleyfcf25832014-12-18 17:42:32 -08001010 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
1011 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
1012 al = SSL_AD_DECODE_ERROR;
1013 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
1014 goto f_err;
1015 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001016 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001017
Adam Langleyfcf25832014-12-18 17:42:32 -08001018 if (!s->s3->have_version) {
1019 /* Select version to use */
1020 uint16_t version = ssl3_get_mutual_version(s, client_version);
1021 if (version == 0) {
1022 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL);
1023 s->version = s->client_version;
1024 al = SSL_AD_PROTOCOL_VERSION;
1025 goto f_err;
1026 }
1027 s->version = version;
1028 s->enc_method = ssl3_get_enc_method(version);
1029 assert(s->enc_method != NULL);
1030 /* At this point, the connection's version is known and |s->version| is
1031 * fixed. Begin enforcing the record-layer version. */
1032 s->s3->have_version = 1;
1033 } else if (SSL_IS_DTLS(s) ? (s->client_version > s->version)
1034 : (s->client_version < s->version)) {
1035 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER);
1036 al = SSL_AD_PROTOCOL_VERSION;
1037 goto f_err;
1038 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001039
Adam Langleyfcf25832014-12-18 17:42:32 -08001040 s->hit = 0;
1041 /* Versions before 0.9.7 always allow clients to resume sessions in
1042 * renegotiation. 0.9.7 and later allow this by default, but optionally
1043 * ignore resumption requests with flag
1044 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather than
1045 * a change to default behavior so that applications relying on this for
1046 * security won't even compile against older library versions).
1047 *
1048 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1049 * request renegotiation but not a new session (s->new_session remains
1050 * unset): for servers, this essentially just means that the
1051 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be ignored. */
1052 if (s->new_session &&
1053 (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)) {
1054 if (!ssl_get_new_session(s, 1)) {
1055 goto err;
1056 }
1057 } else {
1058 i = ssl_get_prev_session(s, &early_ctx);
1059 if (i == PENDING_SESSION) {
David Benjamin2fff5bf2015-02-09 04:32:28 -05001060 s->rwstate = SSL_PENDING_SESSION;
Adam Langleyfcf25832014-12-18 17:42:32 -08001061 goto err;
1062 } else if (i == -1) {
1063 goto err;
1064 }
Adam Langley95c29f32014-06-20 12:00:00 -07001065
Adam Langleyfcf25832014-12-18 17:42:32 -08001066 /* Only resume if the session's version matches the negotiated version:
1067 * most clients do not accept a mismatch. */
1068 if (i == 1 && s->version == s->session->ssl_version) {
1069 s->hit = 1;
1070 } else {
1071 /* No session was found or it was unacceptable. */
1072 if (!ssl_get_new_session(s, 1)) {
1073 goto err;
1074 }
1075 }
1076 }
David Benjamin22f9bcc2014-07-13 12:29:21 -04001077
Adam Langley524e7172015-02-20 16:04:00 -08001078 if (s->ctx->dos_protection_cb != NULL && s->ctx->dos_protection_cb(&early_ctx) == 0) {
1079 /* Connection rejected for DOS reasons. */
1080 al = SSL_AD_ACCESS_DENIED;
1081 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CONNECTION_REJECTED);
1082 goto f_err;
1083 }
1084
Adam Langleyfcf25832014-12-18 17:42:32 -08001085 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
1086 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
1087 CBS_len(&compression_methods) == 0) {
1088 al = SSL_AD_DECODE_ERROR;
1089 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
1090 goto f_err;
1091 }
David Benjamin22f9bcc2014-07-13 12:29:21 -04001092
Adam Langleyfcf25832014-12-18 17:42:32 -08001093 /* TODO(davidben): Per spec, cipher_suites can never be empty (specified at
1094 * the ClientHello structure level). This logic allows it to be empty if
1095 * resuming a session. Can we always require non-empty? If a client sends
1096 * empty cipher_suites because it's resuming a session, it could always fail
1097 * to resume a session, so it's unlikely to actually work. */
1098 if (CBS_len(&cipher_suites) == 0 && CBS_len(&session_id) != 0) {
1099 /* We need a cipher if we are not resuming a session. */
1100 al = SSL_AD_ILLEGAL_PARAMETER;
1101 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_SPECIFIED);
1102 goto f_err;
1103 }
Adam Langley95c29f32014-06-20 12:00:00 -07001104
Adam Langleyfcf25832014-12-18 17:42:32 -08001105 ciphers = ssl_bytes_to_cipher_list(s, &cipher_suites);
1106 if (ciphers == NULL) {
1107 goto err;
1108 }
Adam Langley95c29f32014-06-20 12:00:00 -07001109
Adam Langleyfcf25832014-12-18 17:42:32 -08001110 /* If it is a hit, check that the cipher is in the list. */
1111 if (s->hit && CBS_len(&cipher_suites) > 0) {
1112 size_t j;
1113 int found_cipher = 0;
1114 unsigned long id = s->session->cipher->id;
Adam Langley95c29f32014-06-20 12:00:00 -07001115
Adam Langleyfcf25832014-12-18 17:42:32 -08001116 for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++) {
1117 c = sk_SSL_CIPHER_value(ciphers, j);
1118 if (c->id == id) {
1119 found_cipher = 1;
1120 break;
1121 }
1122 }
Adam Langley95c29f32014-06-20 12:00:00 -07001123
Adam Langleyfcf25832014-12-18 17:42:32 -08001124 if (!found_cipher) {
1125 /* we need to have the cipher in the cipher list if we are asked to reuse
1126 * it */
1127 al = SSL_AD_ILLEGAL_PARAMETER;
1128 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
1129 SSL_R_REQUIRED_CIPHER_MISSING);
1130 goto f_err;
1131 }
1132 }
Adam Langley95c29f32014-06-20 12:00:00 -07001133
Adam Langleyfcf25832014-12-18 17:42:32 -08001134 /* Only null compression is supported. */
1135 if (memchr(CBS_data(&compression_methods), 0,
1136 CBS_len(&compression_methods)) == NULL) {
1137 al = SSL_AD_ILLEGAL_PARAMETER;
1138 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
1139 SSL_R_NO_COMPRESSION_SPECIFIED);
1140 goto f_err;
1141 }
David Benjamindc72ff72014-06-25 12:36:10 -04001142
Adam Langleyfcf25832014-12-18 17:42:32 -08001143 /* TLS extensions. */
1144 if (s->version >= SSL3_VERSION &&
1145 !ssl_parse_clienthello_tlsext(s, &client_hello)) {
1146 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_PARSE_TLSEXT);
1147 goto err;
1148 }
Adam Langley95c29f32014-06-20 12:00:00 -07001149
Adam Langleyfcf25832014-12-18 17:42:32 -08001150 /* There should be nothing left over in the record. */
1151 if (CBS_len(&client_hello) != 0) {
1152 /* wrong packet length */
1153 al = SSL_AD_DECODE_ERROR;
1154 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_BAD_PACKET_LENGTH);
1155 goto f_err;
1156 }
Adam Langley95c29f32014-06-20 12:00:00 -07001157
Adam Langleyfcf25832014-12-18 17:42:32 -08001158 /* Given ciphers and SSL_get_ciphers, we must pick a cipher */
1159 if (!s->hit) {
1160 if (ciphers == NULL) {
1161 al = SSL_AD_ILLEGAL_PARAMETER;
1162 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_PASSED);
1163 goto f_err;
1164 }
Adam Langley95c29f32014-06-20 12:00:00 -07001165
Adam Langleyfcf25832014-12-18 17:42:32 -08001166 /* Let cert callback update server certificates if required */
1167 if (s->cert->cert_cb) {
1168 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1169 if (rv == 0) {
1170 al = SSL_AD_INTERNAL_ERROR;
1171 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CERT_CB_ERROR);
1172 goto f_err;
1173 }
1174 if (rv < 0) {
1175 s->rwstate = SSL_X509_LOOKUP;
1176 goto err;
1177 }
1178 s->rwstate = SSL_NOTHING;
1179 }
1180 c = ssl3_choose_cipher(s, ciphers, ssl_get_cipher_preferences(s));
1181
1182 if (c == NULL) {
1183 al = SSL_AD_HANDSHAKE_FAILURE;
1184 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER);
1185 goto f_err;
1186 }
1187 s->s3->tmp.new_cipher = c;
1188 } else {
1189 /* Session-id reuse */
1190 s->s3->tmp.new_cipher = s->session->cipher;
1191 }
1192
1193 if ((!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) &&
1194 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1195 goto f_err;
1196 }
1197
1198 /* we now have the following setup;
1199 * client_random
1200 * cipher_list - our prefered list of ciphers
1201 * ciphers - the clients prefered list of ciphers
1202 * compression - basically ignored right now
1203 * ssl version is set - sslv3
1204 * s->session - The ssl session has been setup.
1205 * s->hit - session reuse flag
1206 * s->tmp.new_cipher - the new cipher to use. */
1207
1208 if (ret < 0) {
1209 ret = -ret;
1210 }
1211
1212 if (0) {
1213 f_err:
1214 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1215 }
1216
Adam Langley95c29f32014-06-20 12:00:00 -07001217err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001218 if (ciphers != NULL) {
1219 sk_SSL_CIPHER_free(ciphers);
1220 }
1221 return ret;
1222}
Adam Langley95c29f32014-06-20 12:00:00 -07001223
Adam Langleyfcf25832014-12-18 17:42:32 -08001224int ssl3_send_server_hello(SSL *s) {
1225 uint8_t *buf;
1226 uint8_t *p, *d;
1227 int sl;
1228 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001229
Adam Langleyfcf25832014-12-18 17:42:32 -08001230 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1231 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1232 * known attack while we fix ChannelID itself. */
1233 if (s->s3->tlsext_channel_id_valid &&
David Benjamin7061e282015-03-19 11:10:48 -04001234 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001235 s->s3->tlsext_channel_id_valid = 0;
1236 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001237
Adam Langleyfcf25832014-12-18 17:42:32 -08001238 /* If this is a resumption and the original handshake didn't support
1239 * ChannelID then we didn't record the original handshake hashes in the
1240 * session and so cannot resume with ChannelIDs. */
1241 if (s->hit && s->s3->tlsext_channel_id_new &&
1242 s->session->original_handshake_hash_len == 0) {
1243 s->s3->tlsext_channel_id_valid = 0;
1244 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001245
Adam Langleyfcf25832014-12-18 17:42:32 -08001246 buf = (uint8_t *)s->init_buf->data;
1247 /* Do the message type and length last */
1248 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001249
Adam Langleyfcf25832014-12-18 17:42:32 -08001250 *(p++) = s->version >> 8;
1251 *(p++) = s->version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -07001252
Adam Langleyfcf25832014-12-18 17:42:32 -08001253 /* Random stuff */
1254 if (!ssl_fill_hello_random(s, 1, s->s3->server_random, SSL3_RANDOM_SIZE)) {
1255 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1256 return -1;
1257 }
1258 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1259 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -07001260
Adam Langleyfcf25832014-12-18 17:42:32 -08001261 /* There are several cases for the session ID to send
1262 * back in the server hello:
1263 * - For session reuse from the session cache, we send back the old session
1264 * ID.
1265 * - If stateless session reuse (using a session ticket) is successful, we
1266 * send back the client's "session ID" (which doesn't actually identify
1267 * the session).
1268 * - If it is a new session, we send back the new session ID.
1269 * - However, if we want the new session to be single-use, we send back a
1270 * 0-length session ID.
1271 * s->hit is non-zero in either case of session reuse, so the following
1272 * won't overwrite an ID that we're supposed to send back. */
1273 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) && !s->hit) {
1274 s->session->session_id_length = 0;
1275 }
Adam Langley95c29f32014-06-20 12:00:00 -07001276
Adam Langleyfcf25832014-12-18 17:42:32 -08001277 sl = s->session->session_id_length;
1278 if (sl > (int)sizeof(s->session->session_id)) {
1279 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1280 return -1;
1281 }
1282 *(p++) = sl;
1283 memcpy(p, s->session->session_id, sl);
1284 p += sl;
Adam Langley95c29f32014-06-20 12:00:00 -07001285
Adam Langleyfcf25832014-12-18 17:42:32 -08001286 /* put the cipher */
1287 s2n(ssl3_get_cipher_value(s->s3->tmp.new_cipher), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001288
Adam Langleyfcf25832014-12-18 17:42:32 -08001289 /* put the compression method */
1290 *(p++) = 0;
1291 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1292 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, SSL_R_SERVERHELLO_TLSEXT);
1293 return -1;
1294 }
1295 p = ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH);
1296 if (p == NULL) {
1297 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1298 return -1;
1299 }
Adam Langley95c29f32014-06-20 12:00:00 -07001300
Adam Langleyfcf25832014-12-18 17:42:32 -08001301 /* do the header */
1302 l = (p - d);
David Benjaminfbdfefb2015-02-16 19:33:53 -05001303 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l)) {
1304 return -1;
1305 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001306 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1307 }
Adam Langley95c29f32014-06-20 12:00:00 -07001308
Adam Langleyfcf25832014-12-18 17:42:32 -08001309 /* SSL3_ST_SW_SRVR_HELLO_B */
1310 return ssl_do_write(s);
1311}
Adam Langley95c29f32014-06-20 12:00:00 -07001312
Adam Langleyfcf25832014-12-18 17:42:32 -08001313int ssl3_send_server_done(SSL *s) {
1314 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
David Benjaminfbdfefb2015-02-16 19:33:53 -05001315 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0)) {
1316 return -1;
1317 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001318 s->state = SSL3_ST_SW_SRVR_DONE_B;
1319 }
Adam Langley95c29f32014-06-20 12:00:00 -07001320
Adam Langleyfcf25832014-12-18 17:42:32 -08001321 /* SSL3_ST_SW_SRVR_DONE_B */
1322 return ssl_do_write(s);
1323}
Adam Langley95c29f32014-06-20 12:00:00 -07001324
Adam Langleyfcf25832014-12-18 17:42:32 -08001325int ssl3_send_server_key_exchange(SSL *s) {
1326 DH *dh = NULL, *dhp;
David Benjaminc0f763b2015-03-27 02:05:39 -04001327 EC_KEY *ecdh = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001328 uint8_t *encodedPoint = NULL;
1329 int encodedlen = 0;
David Benjamin70bd80a2014-12-27 03:06:46 -05001330 uint16_t curve_id = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001331 BN_CTX *bn_ctx = NULL;
1332 const char *psk_identity_hint = NULL;
1333 size_t psk_identity_hint_len = 0;
1334 EVP_PKEY *pkey;
1335 uint8_t *p, *d;
1336 int al, i;
1337 unsigned long alg_k;
1338 unsigned long alg_a;
1339 int n;
1340 CERT *cert;
1341 BIGNUM *r[4];
1342 int nr[4], kn;
1343 BUF_MEM *buf;
1344 EVP_MD_CTX md_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07001345
Adam Langleyfcf25832014-12-18 17:42:32 -08001346 EVP_MD_CTX_init(&md_ctx);
1347 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1348 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1349 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1350 cert = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07001351
Adam Langleyfcf25832014-12-18 17:42:32 -08001352 buf = s->init_buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001353
Adam Langleyfcf25832014-12-18 17:42:32 -08001354 r[0] = r[1] = r[2] = r[3] = NULL;
1355 n = 0;
1356 if (alg_a & SSL_aPSK) {
1357 /* size for PSK identity hint */
1358 psk_identity_hint = s->psk_identity_hint;
1359 if (psk_identity_hint) {
1360 psk_identity_hint_len = strlen(psk_identity_hint);
1361 } else {
1362 psk_identity_hint_len = 0;
1363 }
1364 n += 2 + psk_identity_hint_len;
1365 }
Adam Langley95c29f32014-06-20 12:00:00 -07001366
David Benjamin7061e282015-03-19 11:10:48 -04001367 if (alg_k & SSL_kDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001368 dhp = cert->dh_tmp;
1369 if (dhp == NULL && s->cert->dh_tmp_cb != NULL) {
1370 dhp = s->cert->dh_tmp_cb(s, 0, 1024);
1371 }
1372 if (dhp == NULL) {
1373 al = SSL_AD_HANDSHAKE_FAILURE;
1374 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1375 SSL_R_MISSING_TMP_DH_KEY);
1376 goto f_err;
1377 }
Adam Langley95c29f32014-06-20 12:00:00 -07001378
Adam Langleyfcf25832014-12-18 17:42:32 -08001379 if (s->s3->tmp.dh != NULL) {
1380 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1381 ERR_R_INTERNAL_ERROR);
1382 goto err;
1383 }
Adam Langley95c29f32014-06-20 12:00:00 -07001384
Adam Langleyfcf25832014-12-18 17:42:32 -08001385 dh = DHparams_dup(dhp);
1386 if (dh == NULL) {
1387 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1388 goto err;
1389 }
Adam Langley95c29f32014-06-20 12:00:00 -07001390
Adam Langleyfcf25832014-12-18 17:42:32 -08001391 s->s3->tmp.dh = dh;
1392 if (dhp->pub_key == NULL || dhp->priv_key == NULL ||
1393 (s->options & SSL_OP_SINGLE_DH_USE)) {
1394 if (!DH_generate_key(dh)) {
1395 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1396 goto err;
1397 }
1398 } else {
1399 dh->pub_key = BN_dup(dhp->pub_key);
1400 dh->priv_key = BN_dup(dhp->priv_key);
1401 if (dh->pub_key == NULL || dh->priv_key == NULL) {
1402 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1403 goto err;
1404 }
1405 }
Adam Langley95c29f32014-06-20 12:00:00 -07001406
Adam Langleyfcf25832014-12-18 17:42:32 -08001407 r[0] = dh->p;
1408 r[1] = dh->g;
1409 r[2] = dh->pub_key;
David Benjamin7061e282015-03-19 11:10:48 -04001410 } else if (alg_k & SSL_kECDHE) {
David Benjaminc0f763b2015-03-27 02:05:39 -04001411 /* Determine the curve to use. */
1412 int nid = NID_undef;
1413 if (cert->ecdh_tmp_auto) {
1414 nid = tls1_get_shared_curve(s);
1415 } else if (cert->ecdh_nid != NID_undef) {
1416 nid = cert->ecdh_nid;
1417 } else if (cert->ecdh_tmp_cb != NULL) {
1418 /* Note: |ecdh_tmp_cb| does NOT pass ownership of the result
1419 * to the caller. */
1420 EC_KEY *template = s->cert->ecdh_tmp_cb(s, 0, 1024);
1421 if (template != NULL && EC_KEY_get0_group(template) != NULL) {
1422 nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(template));
Adam Langleyfcf25832014-12-18 17:42:32 -08001423 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001424 }
David Benjaminc0f763b2015-03-27 02:05:39 -04001425 if (nid == NID_undef) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001426 al = SSL_AD_HANDSHAKE_FAILURE;
1427 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1428 SSL_R_MISSING_TMP_ECDH_KEY);
1429 goto f_err;
1430 }
Adam Langley95c29f32014-06-20 12:00:00 -07001431
Adam Langleyfcf25832014-12-18 17:42:32 -08001432 if (s->s3->tmp.ecdh != NULL) {
1433 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1434 ERR_R_INTERNAL_ERROR);
1435 goto err;
1436 }
David Benjaminc0f763b2015-03-27 02:05:39 -04001437 ecdh = EC_KEY_new_by_curve_name(nid);
1438 if (ecdh == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001439 goto err;
1440 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001441 s->s3->tmp.ecdh = ecdh;
Adam Langley95c29f32014-06-20 12:00:00 -07001442
David Benjaminc0f763b2015-03-27 02:05:39 -04001443 if (!EC_KEY_generate_key(ecdh)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001444 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1445 goto err;
1446 }
Adam Langley95c29f32014-06-20 12:00:00 -07001447
David Benjamin70bd80a2014-12-27 03:06:46 -05001448 /* We only support ephemeral ECDH keys over named (not generic) curves. */
David Benjaminc0f763b2015-03-27 02:05:39 -04001449 const EC_GROUP *group = EC_KEY_get0_group(ecdh);
David Benjamin70bd80a2014-12-27 03:06:46 -05001450 if (!tls1_ec_nid2curve_id(&curve_id, EC_GROUP_get_curve_name(group))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001451 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1452 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1453 goto err;
1454 }
Adam Langley95c29f32014-06-20 12:00:00 -07001455
Adam Langleyfcf25832014-12-18 17:42:32 -08001456 /* Encode the public key. First check the size of encoding and allocate
1457 * memory accordingly. */
1458 encodedlen =
1459 EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
1460 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001461
Adam Langleyfcf25832014-12-18 17:42:32 -08001462 encodedPoint = (uint8_t *)OPENSSL_malloc(encodedlen * sizeof(uint8_t));
1463 bn_ctx = BN_CTX_new();
1464 if (encodedPoint == NULL || bn_ctx == NULL) {
1465 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1466 ERR_R_MALLOC_FAILURE);
1467 goto err;
1468 }
Adam Langley95c29f32014-06-20 12:00:00 -07001469
Adam Langleyfcf25832014-12-18 17:42:32 -08001470 encodedlen = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
1471 POINT_CONVERSION_UNCOMPRESSED,
1472 encodedPoint, encodedlen, bn_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07001473
Adam Langleyfcf25832014-12-18 17:42:32 -08001474 if (encodedlen == 0) {
1475 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1476 goto err;
1477 }
Adam Langley95c29f32014-06-20 12:00:00 -07001478
Adam Langleyfcf25832014-12-18 17:42:32 -08001479 BN_CTX_free(bn_ctx);
1480 bn_ctx = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001481
Adam Langleyfcf25832014-12-18 17:42:32 -08001482 /* We only support named (not generic) curves in ECDH ephemeral key
1483 * exchanges. In this situation, we need four additional bytes to encode
1484 * the entire ServerECDHParams structure. */
1485 n += 4 + encodedlen;
Adam Langley95c29f32014-06-20 12:00:00 -07001486
Adam Langleyfcf25832014-12-18 17:42:32 -08001487 /* We'll generate the serverKeyExchange message explicitly so we can set
1488 * these to NULLs */
1489 r[0] = NULL;
1490 r[1] = NULL;
1491 r[2] = NULL;
1492 r[3] = NULL;
1493 } else if (!(alg_k & SSL_kPSK)) {
1494 al = SSL_AD_HANDSHAKE_FAILURE;
1495 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1496 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1497 goto f_err;
1498 }
Adam Langley95c29f32014-06-20 12:00:00 -07001499
Adam Langleyfcf25832014-12-18 17:42:32 -08001500 for (i = 0; i < 4 && r[i] != NULL; i++) {
1501 nr[i] = BN_num_bytes(r[i]);
1502 n += 2 + nr[i];
1503 }
Adam Langley95c29f32014-06-20 12:00:00 -07001504
Adam Langleyfcf25832014-12-18 17:42:32 -08001505 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1506 pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher);
1507 if (pkey == NULL) {
1508 al = SSL_AD_DECODE_ERROR;
1509 goto f_err;
1510 }
1511 kn = EVP_PKEY_size(pkey);
1512 } else {
1513 pkey = NULL;
1514 kn = 0;
1515 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001516
Adam Langleyfcf25832014-12-18 17:42:32 -08001517 if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) {
1518 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_BUF);
1519 goto err;
1520 }
1521 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001522
Adam Langleyfcf25832014-12-18 17:42:32 -08001523 for (i = 0; i < 4 && r[i] != NULL; i++) {
1524 s2n(nr[i], p);
1525 BN_bn2bin(r[i], p);
1526 p += nr[i];
1527 }
David Benjamin00505ec2014-11-26 16:38:00 -05001528
David Benjamin7061e282015-03-19 11:10:48 -04001529 /* Note: ECDHE PSK ciphersuites use SSL_kECDHE and SSL_aPSK. When one of
Adam Langleyfcf25832014-12-18 17:42:32 -08001530 * them is used, the server key exchange record needs to have both the
1531 * psk_identity_hint and the ServerECDHParams. */
1532 if (alg_a & SSL_aPSK) {
1533 /* copy PSK identity hint (if provided) */
1534 s2n(psk_identity_hint_len, p);
1535 if (psk_identity_hint_len > 0) {
1536 memcpy(p, psk_identity_hint, psk_identity_hint_len);
1537 p += psk_identity_hint_len;
1538 }
1539 }
David Benjamin00505ec2014-11-26 16:38:00 -05001540
David Benjamin7061e282015-03-19 11:10:48 -04001541 if (alg_k & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001542 /* We only support named (not generic) curves. In this situation, the
1543 * serverKeyExchange message has:
1544 * [1 byte CurveType], [2 byte CurveName]
1545 * [1 byte length of encoded point], followed by
1546 * the actual encoded point itself. */
David Benjamin70bd80a2014-12-27 03:06:46 -05001547 *(p++) = NAMED_CURVE_TYPE;
1548 *(p++) = (uint8_t)(curve_id >> 8);
1549 *(p++) = (uint8_t)(curve_id & 0xff);
1550 *(p++) = encodedlen;
1551 memcpy(p, encodedPoint, encodedlen);
1552 p += encodedlen;
Adam Langleyfcf25832014-12-18 17:42:32 -08001553 OPENSSL_free(encodedPoint);
1554 encodedPoint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001555 }
Adam Langley95c29f32014-06-20 12:00:00 -07001556
Adam Langleyfcf25832014-12-18 17:42:32 -08001557 /* not anonymous */
1558 if (pkey != NULL) {
1559 /* n is the length of the params, they start at &(d[4]) and p points to
1560 * the space at the end. */
1561 const EVP_MD *md;
1562 size_t sig_len = EVP_PKEY_size(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001563
Adam Langleyfcf25832014-12-18 17:42:32 -08001564 /* Determine signature algorithm. */
1565 if (SSL_USE_SIGALGS(s)) {
1566 md = tls1_choose_signing_digest(s, pkey);
1567 if (!tls12_get_sigandhash(p, pkey, md)) {
1568 /* Should never happen */
1569 al = SSL_AD_INTERNAL_ERROR;
1570 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1571 ERR_R_INTERNAL_ERROR);
1572 goto f_err;
1573 }
1574 p += 2;
1575 } else if (pkey->type == EVP_PKEY_RSA) {
1576 md = EVP_md5_sha1();
1577 } else {
1578 md = EVP_sha1();
1579 }
1580
1581 if (!EVP_DigestSignInit(&md_ctx, NULL, md, NULL, pkey) ||
1582 !EVP_DigestSignUpdate(&md_ctx, s->s3->client_random,
1583 SSL3_RANDOM_SIZE) ||
1584 !EVP_DigestSignUpdate(&md_ctx, s->s3->server_random,
1585 SSL3_RANDOM_SIZE) ||
1586 !EVP_DigestSignUpdate(&md_ctx, d, n) ||
1587 !EVP_DigestSignFinal(&md_ctx, &p[2], &sig_len)) {
1588 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_EVP);
1589 goto err;
1590 }
1591
1592 s2n(sig_len, p);
1593 n += sig_len + 2;
1594 if (SSL_USE_SIGALGS(s)) {
1595 n += 2;
1596 }
1597 }
1598
David Benjaminfbdfefb2015-02-16 19:33:53 -05001599 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n)) {
1600 goto err;
1601 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001602 }
1603
1604 s->state = SSL3_ST_SW_KEY_EXCH_B;
1605 EVP_MD_CTX_cleanup(&md_ctx);
1606 return ssl_do_write(s);
1607
Adam Langley95c29f32014-06-20 12:00:00 -07001608f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001609 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001610err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001611 if (encodedPoint != NULL) {
1612 OPENSSL_free(encodedPoint);
1613 }
1614 BN_CTX_free(bn_ctx);
1615 EVP_MD_CTX_cleanup(&md_ctx);
1616 return -1;
1617}
Adam Langley95c29f32014-06-20 12:00:00 -07001618
Adam Langleyfcf25832014-12-18 17:42:32 -08001619int ssl3_send_certificate_request(SSL *s) {
1620 uint8_t *p, *d;
1621 size_t i;
1622 int j, nl, off, n;
1623 STACK_OF(X509_NAME) *sk = NULL;
1624 X509_NAME *name;
1625 BUF_MEM *buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001626
Adam Langleyfcf25832014-12-18 17:42:32 -08001627 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
1628 buf = s->init_buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001629
Adam Langleyfcf25832014-12-18 17:42:32 -08001630 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001631
Adam Langleyfcf25832014-12-18 17:42:32 -08001632 /* get the list of acceptable cert types */
1633 p++;
1634 n = ssl3_get_req_cert_type(s, p);
1635 d[0] = n;
1636 p += n;
1637 n++;
Adam Langley95c29f32014-06-20 12:00:00 -07001638
Adam Langleyfcf25832014-12-18 17:42:32 -08001639 if (SSL_USE_SIGALGS(s)) {
1640 const uint8_t *psigs;
1641 nl = tls12_get_psigalgs(s, &psigs);
1642 s2n(nl, p);
1643 memcpy(p, psigs, nl);
1644 p += nl;
1645 n += nl + 2;
1646 }
Adam Langley95c29f32014-06-20 12:00:00 -07001647
Adam Langleyfcf25832014-12-18 17:42:32 -08001648 off = n;
1649 p += 2;
1650 n += 2;
Adam Langley95c29f32014-06-20 12:00:00 -07001651
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 sk = SSL_get_client_CA_list(s);
1653 nl = 0;
1654 if (sk != NULL) {
1655 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1656 name = sk_X509_NAME_value(sk, i);
1657 j = i2d_X509_NAME(name, NULL);
1658 if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
1659 OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB);
1660 goto err;
1661 }
1662 p = ssl_handshake_start(s) + n;
1663 s2n(j, p);
1664 i2d_X509_NAME(name, &p);
1665 n += 2 + j;
1666 nl += 2 + j;
1667 }
1668 }
Adam Langley95c29f32014-06-20 12:00:00 -07001669
Adam Langleyfcf25832014-12-18 17:42:32 -08001670 /* else no CA names */
1671 p = ssl_handshake_start(s) + off;
1672 s2n(nl, p);
1673
David Benjaminfbdfefb2015-02-16 19:33:53 -05001674 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n)) {
1675 goto err;
1676 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001677 s->state = SSL3_ST_SW_CERT_REQ_B;
1678 }
Adam Langley95c29f32014-06-20 12:00:00 -07001679
Adam Langleyfcf25832014-12-18 17:42:32 -08001680 /* SSL3_ST_SW_CERT_REQ_B */
1681 return ssl_do_write(s);
1682
Adam Langley95c29f32014-06-20 12:00:00 -07001683err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001684 return -1;
1685}
Adam Langley95c29f32014-06-20 12:00:00 -07001686
Adam Langleyfcf25832014-12-18 17:42:32 -08001687int ssl3_get_client_key_exchange(SSL *s) {
1688 int al, ok;
1689 long n;
1690 CBS client_key_exchange;
1691 unsigned long alg_k;
1692 unsigned long alg_a;
1693 uint8_t *premaster_secret = NULL;
1694 size_t premaster_secret_len = 0;
1695 RSA *rsa = NULL;
1696 uint8_t *decrypt_buf = NULL;
1697 EVP_PKEY *pkey = NULL;
1698 BIGNUM *pub = NULL;
1699 DH *dh_srvr;
Adam Langley95c29f32014-06-20 12:00:00 -07001700
Adam Langleyfcf25832014-12-18 17:42:32 -08001701 EC_KEY *srvr_ecdh = NULL;
1702 EVP_PKEY *clnt_pub_pkey = NULL;
1703 EC_POINT *clnt_ecpoint = NULL;
1704 BN_CTX *bn_ctx = NULL;
1705 unsigned int psk_len = 0;
1706 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001707
Adam Langleyfcf25832014-12-18 17:42:32 -08001708 n = s->method->ssl_get_message(s, SSL3_ST_SR_KEY_EXCH_A,
1709 SSL3_ST_SR_KEY_EXCH_B,
1710 SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, /* ??? */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001711 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001712
Adam Langleyfcf25832014-12-18 17:42:32 -08001713 if (!ok) {
1714 return n;
1715 }
Adam Langley95c29f32014-06-20 12:00:00 -07001716
Adam Langleyfcf25832014-12-18 17:42:32 -08001717 CBS_init(&client_key_exchange, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001718
Adam Langleyfcf25832014-12-18 17:42:32 -08001719 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1720 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001721
Adam Langleyfcf25832014-12-18 17:42:32 -08001722 /* If using a PSK key exchange, prepare the pre-shared key. */
1723 if (alg_a & SSL_aPSK) {
1724 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001725
Adam Langleyfcf25832014-12-18 17:42:32 -08001726 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1727 * then this is the only field in the message. */
1728 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1729 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
1730 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR);
1731 al = SSL_AD_DECODE_ERROR;
1732 goto f_err;
1733 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001734
Adam Langleyfcf25832014-12-18 17:42:32 -08001735 if (s->psk_server_callback == NULL) {
1736 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1737 SSL_R_PSK_NO_SERVER_CB);
1738 al = SSL_AD_INTERNAL_ERROR;
1739 goto f_err;
1740 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001741
Adam Langleyfcf25832014-12-18 17:42:32 -08001742 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1743 CBS_contains_zero_byte(&psk_identity)) {
1744 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1745 SSL_R_DATA_LENGTH_TOO_LONG);
1746 al = SSL_AD_ILLEGAL_PARAMETER;
1747 goto f_err;
1748 }
David Benjamin35c02302014-07-13 04:14:59 -04001749
Adam Langleyfcf25832014-12-18 17:42:32 -08001750 if (!CBS_strdup(&psk_identity, &s->session->psk_identity)) {
1751 al = SSL_AD_INTERNAL_ERROR;
1752 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1753 ERR_R_MALLOC_FAILURE);
1754 goto f_err;
1755 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001756
Adam Langleyfcf25832014-12-18 17:42:32 -08001757 /* Look up the key for the identity. */
1758 psk_len =
1759 s->psk_server_callback(s, s->session->psk_identity, psk, sizeof(psk));
1760 if (psk_len > PSK_MAX_PSK_LEN) {
1761 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1762 ERR_R_INTERNAL_ERROR);
1763 al = SSL_AD_INTERNAL_ERROR;
1764 goto f_err;
1765 } else if (psk_len == 0) {
1766 /* PSK related to the given identity not found */
1767 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1768 SSL_R_PSK_IDENTITY_NOT_FOUND);
1769 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1770 goto f_err;
1771 }
1772 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001773
Adam Langleyfcf25832014-12-18 17:42:32 -08001774 /* Depending on the key exchange method, compute |premaster_secret| and
1775 * |premaster_secret_len|. */
1776 if (alg_k & SSL_kRSA) {
1777 CBS encrypted_premaster_secret;
1778 uint8_t rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
1779 uint8_t good;
1780 size_t rsa_size, decrypt_len, premaster_index, j;
Adam Langley95c29f32014-06-20 12:00:00 -07001781
Adam Langleyfcf25832014-12-18 17:42:32 -08001782 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1783 if (pkey == NULL || pkey->type != EVP_PKEY_RSA || pkey->pkey.rsa == NULL) {
1784 al = SSL_AD_HANDSHAKE_FAILURE;
1785 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1786 SSL_R_MISSING_RSA_CERTIFICATE);
1787 goto f_err;
1788 }
1789 rsa = pkey->pkey.rsa;
Adam Langley95c29f32014-06-20 12:00:00 -07001790
Adam Langleyfcf25832014-12-18 17:42:32 -08001791 /* TLS and [incidentally] DTLS{0xFEFF} */
1792 if (s->version > SSL3_VERSION) {
1793 CBS copy = client_key_exchange;
1794 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1795 &encrypted_premaster_secret) ||
1796 CBS_len(&client_key_exchange) != 0) {
1797 if (!(s->options & SSL_OP_TLS_D5_BUG)) {
1798 al = SSL_AD_DECODE_ERROR;
1799 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1800 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1801 goto f_err;
1802 } else {
1803 encrypted_premaster_secret = copy;
1804 }
1805 }
1806 } else {
1807 encrypted_premaster_secret = client_key_exchange;
1808 }
Adam Langley95c29f32014-06-20 12:00:00 -07001809
Adam Langleyfcf25832014-12-18 17:42:32 -08001810 /* Reject overly short RSA keys because we want to be sure that the buffer
1811 * size makes it safe to iterate over the entire size of a premaster secret
1812 * (SSL_MAX_MASTER_KEY_LENGTH). The actual expected size is larger due to
1813 * RSA padding, but the bound is sufficient to be safe. */
1814 rsa_size = RSA_size(rsa);
1815 if (rsa_size < SSL_MAX_MASTER_KEY_LENGTH) {
1816 al = SSL_AD_DECRYPT_ERROR;
1817 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1818 SSL_R_DECRYPTION_FAILED);
1819 goto f_err;
1820 }
Adam Langley95c29f32014-06-20 12:00:00 -07001821
Adam Langleyfcf25832014-12-18 17:42:32 -08001822 /* We must not leak whether a decryption failure occurs because of
1823 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
1824 * section 7.4.7.1). The code follows that advice of the TLS RFC and
1825 * generates a random premaster secret for the case that the decrypt fails.
1826 * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */
1827 if (!RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret))) {
1828 goto err;
1829 }
David Benjamin35c02302014-07-13 04:14:59 -04001830
Adam Langleyfcf25832014-12-18 17:42:32 -08001831 /* Allocate a buffer large enough for an RSA decryption. */
1832 decrypt_buf = OPENSSL_malloc(rsa_size);
1833 if (decrypt_buf == NULL) {
1834 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1835 ERR_R_MALLOC_FAILURE);
1836 goto err;
1837 }
David Benjamin35c02302014-07-13 04:14:59 -04001838
Adam Langleyfcf25832014-12-18 17:42:32 -08001839 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1840 * timing-sensitive code below. */
1841 if (!RSA_decrypt(rsa, &decrypt_len, decrypt_buf, rsa_size,
1842 CBS_data(&encrypted_premaster_secret),
1843 CBS_len(&encrypted_premaster_secret), RSA_NO_PADDING)) {
1844 goto err;
1845 }
1846 if (decrypt_len != rsa_size) {
1847 /* This should never happen, but do a check so we do not read
1848 * uninitialized memory. */
1849 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1850 ERR_R_INTERNAL_ERROR);
1851 goto err;
1852 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001853
Adam Langleyfcf25832014-12-18 17:42:32 -08001854 /* Remove the PKCS#1 padding and adjust |decrypt_len| as appropriate.
1855 * |good| will be 0xff if the premaster is acceptable and zero otherwise.
1856 * */
1857 good =
1858 constant_time_eq_int_8(RSA_message_index_PKCS1_type_2(
1859 decrypt_buf, decrypt_len, &premaster_index),
1860 1);
1861 decrypt_len = decrypt_len - premaster_index;
David Benjamin0aa07672014-07-24 15:34:14 -04001862
Adam Langleyfcf25832014-12-18 17:42:32 -08001863 /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. */
1864 good &= constant_time_eq_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
Adam Langleyacff73f2014-06-20 12:00:00 -07001865
Adam Langleyfcf25832014-12-18 17:42:32 -08001866 /* Copy over the unpadded premaster. Whatever the value of
1867 * |decrypt_good_mask|, copy as if the premaster were the right length. It
1868 * is important the memory access pattern be constant. */
1869 premaster_secret =
1870 BUF_memdup(decrypt_buf + (rsa_size - SSL_MAX_MASTER_KEY_LENGTH),
1871 SSL_MAX_MASTER_KEY_LENGTH);
1872 if (premaster_secret == NULL) {
1873 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1874 ERR_R_MALLOC_FAILURE);
1875 goto err;
1876 }
1877 OPENSSL_free(decrypt_buf);
1878 decrypt_buf = NULL;
Adam Langleyacff73f2014-06-20 12:00:00 -07001879
Adam Langleyfcf25832014-12-18 17:42:32 -08001880 /* If the version in the decrypted pre-master secret is correct then
1881 * version_good will be 0xff, otherwise it'll be zero. The
1882 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
1883 * (http://eprint.iacr.org/2003/052/) exploits the version number check as
1884 * a "bad version oracle". Thus version checks are done in constant time
1885 * and are treated like any other decryption error. */
1886 good &= constant_time_eq_8(premaster_secret[0],
1887 (unsigned)(s->client_version >> 8));
1888 good &= constant_time_eq_8(premaster_secret[1],
1889 (unsigned)(s->client_version & 0xff));
Adam Langleyacff73f2014-06-20 12:00:00 -07001890
Adam Langleyfcf25832014-12-18 17:42:32 -08001891 /* Now copy rand_premaster_secret over premaster_secret using
1892 * decrypt_good_mask. */
1893 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
1894 premaster_secret[j] = constant_time_select_8(good, premaster_secret[j],
1895 rand_premaster_secret[j]);
1896 }
David Benjamin35c02302014-07-13 04:14:59 -04001897
Adam Langleyfcf25832014-12-18 17:42:32 -08001898 premaster_secret_len = sizeof(rand_premaster_secret);
David Benjamin7061e282015-03-19 11:10:48 -04001899 } else if (alg_k & SSL_kDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001900 CBS dh_Yc;
1901 int dh_len;
David Benjamin35c02302014-07-13 04:14:59 -04001902
Adam Langleyfcf25832014-12-18 17:42:32 -08001903 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &dh_Yc) ||
1904 CBS_len(&dh_Yc) == 0 || CBS_len(&client_key_exchange) != 0) {
1905 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1906 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1907 al = SSL_R_DECODE_ERROR;
1908 goto f_err;
1909 }
Adam Langley95c29f32014-06-20 12:00:00 -07001910
Adam Langleyfcf25832014-12-18 17:42:32 -08001911 if (s->s3->tmp.dh == NULL) {
1912 al = SSL_AD_HANDSHAKE_FAILURE;
1913 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1914 SSL_R_MISSING_TMP_DH_KEY);
1915 goto f_err;
1916 }
1917 dh_srvr = s->s3->tmp.dh;
Adam Langley95c29f32014-06-20 12:00:00 -07001918
Adam Langleyfcf25832014-12-18 17:42:32 -08001919 pub = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL);
1920 if (pub == NULL) {
1921 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_BN_LIB);
1922 goto err;
1923 }
Adam Langley95c29f32014-06-20 12:00:00 -07001924
Adam Langleyfcf25832014-12-18 17:42:32 -08001925 /* Allocate a buffer for the premaster secret. */
1926 premaster_secret = OPENSSL_malloc(DH_size(dh_srvr));
1927 if (premaster_secret == NULL) {
1928 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1929 ERR_R_MALLOC_FAILURE);
David Benjaminc35fb012015-02-12 00:48:45 -05001930 BN_clear_free(pub);
Adam Langleyfcf25832014-12-18 17:42:32 -08001931 goto err;
1932 }
Adam Langley95c29f32014-06-20 12:00:00 -07001933
Adam Langleyfcf25832014-12-18 17:42:32 -08001934 dh_len = DH_compute_key(premaster_secret, pub, dh_srvr);
1935 if (dh_len <= 0) {
1936 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_DH_LIB);
1937 BN_clear_free(pub);
1938 goto err;
1939 }
David Benjamin14c83e72014-07-13 04:54:57 -04001940
Adam Langleyfcf25832014-12-18 17:42:32 -08001941 DH_free(s->s3->tmp.dh);
1942 s->s3->tmp.dh = NULL;
1943 BN_clear_free(pub);
1944 pub = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001945
Adam Langleyfcf25832014-12-18 17:42:32 -08001946 premaster_secret_len = dh_len;
David Benjamin7061e282015-03-19 11:10:48 -04001947 } else if (alg_k & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001948 int field_size = 0, ecdh_len;
1949 const EC_KEY *tkey;
1950 const EC_GROUP *group;
1951 const BIGNUM *priv_key;
1952 CBS ecdh_Yc;
Adam Langley95c29f32014-06-20 12:00:00 -07001953
Adam Langleyfcf25832014-12-18 17:42:32 -08001954 /* initialize structures for server's ECDH key pair */
1955 srvr_ecdh = EC_KEY_new();
1956 if (srvr_ecdh == NULL) {
1957 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1958 ERR_R_MALLOC_FAILURE);
1959 goto err;
1960 }
Adam Langley95c29f32014-06-20 12:00:00 -07001961
Adam Langleyfcf25832014-12-18 17:42:32 -08001962 /* Use the ephermeral values we saved when generating the ServerKeyExchange
1963 * msg. */
1964 tkey = s->s3->tmp.ecdh;
Adam Langley95c29f32014-06-20 12:00:00 -07001965
Adam Langleyfcf25832014-12-18 17:42:32 -08001966 group = EC_KEY_get0_group(tkey);
1967 priv_key = EC_KEY_get0_private_key(tkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001968
Adam Langleyfcf25832014-12-18 17:42:32 -08001969 if (!EC_KEY_set_group(srvr_ecdh, group) ||
1970 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
1971 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
1972 goto err;
1973 }
Adam Langley95c29f32014-06-20 12:00:00 -07001974
Adam Langleyfcf25832014-12-18 17:42:32 -08001975 /* Let's get client's public key */
1976 clnt_ecpoint = EC_POINT_new(group);
1977 if (clnt_ecpoint == NULL) {
1978 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1979 ERR_R_MALLOC_FAILURE);
1980 goto err;
1981 }
Adam Langley95c29f32014-06-20 12:00:00 -07001982
Adam Langleyfcf25832014-12-18 17:42:32 -08001983 /* Get client's public key from encoded point in the ClientKeyExchange
1984 * message. */
1985 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ecdh_Yc) ||
1986 CBS_len(&client_key_exchange) != 0) {
1987 al = SSL_AD_DECODE_ERROR;
1988 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR);
1989 goto f_err;
1990 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001991
Adam Langleyfcf25832014-12-18 17:42:32 -08001992 bn_ctx = BN_CTX_new();
1993 if (bn_ctx == NULL) {
1994 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1995 ERR_R_MALLOC_FAILURE);
1996 goto err;
1997 }
David Benjamin35c02302014-07-13 04:14:59 -04001998
Adam Langleyfcf25832014-12-18 17:42:32 -08001999 if (!EC_POINT_oct2point(group, clnt_ecpoint, CBS_data(&ecdh_Yc),
2000 CBS_len(&ecdh_Yc), bn_ctx)) {
2001 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
2002 goto err;
2003 }
Adam Langley95c29f32014-06-20 12:00:00 -07002004
Adam Langleyfcf25832014-12-18 17:42:32 -08002005 /* Allocate a buffer for both the secret and the PSK. */
2006 field_size = EC_GROUP_get_degree(group);
2007 if (field_size <= 0) {
2008 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
2009 goto err;
2010 }
David Benjamin35c02302014-07-13 04:14:59 -04002011
Adam Langleyfcf25832014-12-18 17:42:32 -08002012 ecdh_len = (field_size + 7) / 8;
2013 premaster_secret = OPENSSL_malloc(ecdh_len);
2014 if (premaster_secret == NULL) {
2015 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2016 ERR_R_MALLOC_FAILURE);
2017 goto err;
2018 }
David Benjamin35c02302014-07-13 04:14:59 -04002019
Adam Langleyfcf25832014-12-18 17:42:32 -08002020 /* Compute the shared pre-master secret */
2021 ecdh_len = ECDH_compute_key(premaster_secret, ecdh_len, clnt_ecpoint,
2022 srvr_ecdh, NULL);
2023 if (ecdh_len <= 0) {
2024 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
2025 goto err;
2026 }
Adam Langley95c29f32014-06-20 12:00:00 -07002027
Adam Langleyfcf25832014-12-18 17:42:32 -08002028 EVP_PKEY_free(clnt_pub_pkey);
2029 clnt_pub_pkey = NULL;
2030 EC_POINT_free(clnt_ecpoint);
2031 clnt_ecpoint = NULL;
2032 EC_KEY_free(srvr_ecdh);
2033 srvr_ecdh = NULL;
2034 BN_CTX_free(bn_ctx);
2035 bn_ctx = NULL;
2036 EC_KEY_free(s->s3->tmp.ecdh);
2037 s->s3->tmp.ecdh = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002038
Adam Langleyfcf25832014-12-18 17:42:32 -08002039 premaster_secret_len = ecdh_len;
2040 } else if (alg_k & SSL_kPSK) {
2041 /* For plain PSK, other_secret is a block of 0s with the same length as the
2042 * pre-shared key. */
2043 premaster_secret_len = psk_len;
2044 premaster_secret = OPENSSL_malloc(premaster_secret_len);
2045 if (premaster_secret == NULL) {
2046 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2047 ERR_R_MALLOC_FAILURE);
2048 goto err;
2049 }
2050 memset(premaster_secret, 0, premaster_secret_len);
2051 } else {
2052 al = SSL_AD_HANDSHAKE_FAILURE;
2053 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2054 SSL_R_UNKNOWN_CIPHER_TYPE);
2055 goto f_err;
2056 }
Adam Langley95c29f32014-06-20 12:00:00 -07002057
Adam Langleyfcf25832014-12-18 17:42:32 -08002058 /* For a PSK cipher suite, the actual pre-master secret is combined with the
2059 * pre-shared key. */
2060 if (alg_a & SSL_aPSK) {
2061 CBB new_premaster, child;
2062 uint8_t *new_data;
2063 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04002064
Adam Langleyfcf25832014-12-18 17:42:32 -08002065 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len)) {
2066 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2067 ERR_R_MALLOC_FAILURE);
2068 goto err;
2069 }
2070 if (!CBB_add_u16_length_prefixed(&new_premaster, &child) ||
2071 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
2072 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
2073 !CBB_add_bytes(&child, psk, psk_len) ||
2074 !CBB_finish(&new_premaster, &new_data, &new_len)) {
2075 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2076 ERR_R_INTERNAL_ERROR);
2077 CBB_cleanup(&new_premaster);
2078 goto err;
2079 }
David Benjamin14c83e72014-07-13 04:54:57 -04002080
Adam Langleyfcf25832014-12-18 17:42:32 -08002081 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2082 OPENSSL_free(premaster_secret);
2083 premaster_secret = new_data;
2084 premaster_secret_len = new_len;
2085 }
David Benjamin14c83e72014-07-13 04:54:57 -04002086
Adam Langleyfcf25832014-12-18 17:42:32 -08002087 /* Compute the master secret */
2088 s->session->master_key_length = s->enc_method->generate_master_secret(
2089 s, s->session->master_key, premaster_secret, premaster_secret_len);
2090 if (s->session->master_key_length == 0) {
2091 goto err;
2092 }
2093 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04002094
Adam Langleyfcf25832014-12-18 17:42:32 -08002095 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2096 OPENSSL_free(premaster_secret);
2097 return 1;
2098
Adam Langley95c29f32014-06-20 12:00:00 -07002099f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002100 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07002101err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002102 if (premaster_secret) {
2103 if (premaster_secret_len) {
2104 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2105 }
2106 OPENSSL_free(premaster_secret);
2107 }
2108 if (decrypt_buf) {
2109 OPENSSL_free(decrypt_buf);
2110 }
2111 EVP_PKEY_free(clnt_pub_pkey);
2112 EC_POINT_free(clnt_ecpoint);
2113 if (srvr_ecdh != NULL) {
2114 EC_KEY_free(srvr_ecdh);
2115 }
2116 BN_CTX_free(bn_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002117
Adam Langleyfcf25832014-12-18 17:42:32 -08002118 return -1;
2119}
Adam Langley95c29f32014-06-20 12:00:00 -07002120
Adam Langleyfcf25832014-12-18 17:42:32 -08002121int ssl3_get_cert_verify(SSL *s) {
2122 int al, ok, ret = 0;
2123 long n;
2124 CBS certificate_verify, signature;
2125 X509 *peer = s->session->peer;
2126 EVP_PKEY *pkey = NULL;
2127 const EVP_MD *md = NULL;
2128 uint8_t digest[EVP_MAX_MD_SIZE];
2129 size_t digest_length;
2130 EVP_PKEY_CTX *pctx = NULL;
David Benjamin6553b372014-07-22 14:11:30 -04002131
Adam Langleyfcf25832014-12-18 17:42:32 -08002132 /* Only RSA and ECDSA client certificates are supported, so a
2133 * CertificateVerify is required if and only if there's a client certificate.
2134 * */
2135 if (peer == NULL) {
2136 if (s->s3->handshake_buffer &&
2137 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2138 return -1;
2139 }
2140 return 1;
2141 }
Adam Langley95c29f32014-06-20 12:00:00 -07002142
Adam Langleyfcf25832014-12-18 17:42:32 -08002143 n = s->method->ssl_get_message(
2144 s, SSL3_ST_SR_CERT_VRFY_A, SSL3_ST_SR_CERT_VRFY_B,
2145 SSL3_MT_CERTIFICATE_VERIFY, SSL3_RT_MAX_PLAIN_LENGTH,
David Benjamin5ca39fb2015-03-01 23:57:54 -05002146 ssl_dont_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002147
Adam Langleyfcf25832014-12-18 17:42:32 -08002148 if (!ok) {
2149 return n;
2150 }
David Benjaminef865502014-08-24 02:48:34 -04002151
Adam Langleyfcf25832014-12-18 17:42:32 -08002152 /* Filter out unsupported certificate types. */
2153 pkey = X509_get_pubkey(peer);
David Benjamin9d0847a2015-02-16 03:57:55 -05002154 if (pkey == NULL) {
2155 goto err;
2156 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002157 if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) ||
2158 (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) {
2159 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
2160 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify,
2161 SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
2162 goto f_err;
2163 }
David Benjamin6897dbe2014-07-12 20:18:28 -04002164
Adam Langleyfcf25832014-12-18 17:42:32 -08002165 CBS_init(&certificate_verify, s->init_msg, n);
David Benjamin6897dbe2014-07-12 20:18:28 -04002166
Adam Langleyfcf25832014-12-18 17:42:32 -08002167 /* Determine the digest type if needbe. */
2168 if (SSL_USE_SIGALGS(s) &&
2169 !tls12_check_peer_sigalg(&md, &al, s, &certificate_verify, pkey)) {
2170 goto f_err;
2171 }
David Benjamin854dd652014-08-26 00:32:30 -04002172
Adam Langleyfcf25832014-12-18 17:42:32 -08002173 /* Compute the digest. */
2174 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2175 goto err;
2176 }
David Benjamin854dd652014-08-26 00:32:30 -04002177
Adam Langleyfcf25832014-12-18 17:42:32 -08002178 /* The handshake buffer is no longer necessary, and we may hash the current
2179 * message.*/
2180 if (s->s3->handshake_buffer &&
2181 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2182 goto err;
2183 }
David Benjaminfbdfefb2015-02-16 19:33:53 -05002184 if (!ssl3_hash_current_message(s)) {
2185 goto err;
2186 }
Adam Langley95c29f32014-06-20 12:00:00 -07002187
Adam Langleyfcf25832014-12-18 17:42:32 -08002188 /* Parse and verify the signature. */
2189 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
2190 CBS_len(&certificate_verify) != 0) {
2191 al = SSL_AD_DECODE_ERROR;
2192 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_DECODE_ERROR);
2193 goto f_err;
2194 }
Adam Langley95c29f32014-06-20 12:00:00 -07002195
Adam Langleyfcf25832014-12-18 17:42:32 -08002196 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2197 if (pctx == NULL) {
2198 goto err;
2199 }
2200 if (!EVP_PKEY_verify_init(pctx) ||
2201 !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2202 !EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), digest,
2203 digest_length)) {
2204 al = SSL_AD_DECRYPT_ERROR;
2205 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_SIGNATURE);
2206 goto f_err;
2207 }
2208
2209 ret = 1;
2210
2211 if (0) {
2212 f_err:
2213 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2214 }
2215
David Benjamin854dd652014-08-26 00:32:30 -04002216err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002217 EVP_PKEY_CTX_free(pctx);
2218 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07002219
Adam Langleyfcf25832014-12-18 17:42:32 -08002220 return ret;
2221}
Adam Langley95c29f32014-06-20 12:00:00 -07002222
Adam Langleyfcf25832014-12-18 17:42:32 -08002223int ssl3_get_client_certificate(SSL *s) {
2224 int i, ok, al, ret = -1;
2225 X509 *x = NULL;
2226 unsigned long n;
2227 STACK_OF(X509) *sk = NULL;
2228 SHA256_CTX sha256;
2229 CBS certificate_msg, certificate_list;
2230 int is_first_certificate = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002231
Adam Langleyfcf25832014-12-18 17:42:32 -08002232 n = s->method->ssl_get_message(s, SSL3_ST_SR_CERT_A, SSL3_ST_SR_CERT_B, -1,
David Benjamin5ca39fb2015-03-01 23:57:54 -05002233 s->max_cert_list, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002234
Adam Langleyfcf25832014-12-18 17:42:32 -08002235 if (!ok) {
2236 return n;
2237 }
Adam Langley95c29f32014-06-20 12:00:00 -07002238
Adam Langleyfcf25832014-12-18 17:42:32 -08002239 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
2240 if ((s->verify_mode & SSL_VERIFY_PEER) &&
2241 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2242 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2243 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2244 al = SSL_AD_HANDSHAKE_FAILURE;
2245 goto f_err;
2246 }
David Benjamind5b1f842014-07-11 23:43:48 -04002247
Adam Langleyfcf25832014-12-18 17:42:32 -08002248 /* If tls asked for a client cert, the client must return a 0 list */
2249 if (s->version > SSL3_VERSION && s->s3->tmp.cert_request) {
2250 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2251 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
2252 al = SSL_AD_UNEXPECTED_MESSAGE;
2253 goto f_err;
2254 }
2255 s->s3->tmp.reuse_message = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002256
Adam Langleyfcf25832014-12-18 17:42:32 -08002257 return 1;
2258 }
Adam Langley95c29f32014-06-20 12:00:00 -07002259
Adam Langleyfcf25832014-12-18 17:42:32 -08002260 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
2261 al = SSL_AD_UNEXPECTED_MESSAGE;
2262 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2263 SSL_R_WRONG_MESSAGE_TYPE);
2264 goto f_err;
2265 }
David Benjamind5b1f842014-07-11 23:43:48 -04002266
Adam Langleyfcf25832014-12-18 17:42:32 -08002267 CBS_init(&certificate_msg, s->init_msg, n);
David Benjamind5b1f842014-07-11 23:43:48 -04002268
Adam Langleyfcf25832014-12-18 17:42:32 -08002269 sk = sk_X509_new_null();
2270 if (sk == NULL) {
2271 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2272 goto err;
2273 }
Adam Langley95c29f32014-06-20 12:00:00 -07002274
Adam Langleyfcf25832014-12-18 17:42:32 -08002275 if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) ||
2276 CBS_len(&certificate_msg) != 0) {
2277 al = SSL_AD_DECODE_ERROR;
2278 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR);
2279 goto f_err;
2280 }
Adam Langley95c29f32014-06-20 12:00:00 -07002281
Adam Langleyfcf25832014-12-18 17:42:32 -08002282 while (CBS_len(&certificate_list) > 0) {
2283 CBS certificate;
2284 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07002285
Adam Langleyfcf25832014-12-18 17:42:32 -08002286 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
2287 al = SSL_AD_DECODE_ERROR;
2288 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR);
2289 goto f_err;
2290 }
Adam Langley95c29f32014-06-20 12:00:00 -07002291
Adam Langleyfcf25832014-12-18 17:42:32 -08002292 if (is_first_certificate && s->ctx->retain_only_sha256_of_client_certs) {
2293 /* If this is the first certificate, and we don't want to keep peer
2294 * certificates in memory, then we hash it right away. */
2295 SHA256_Init(&sha256);
2296 SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate));
2297 SHA256_Final(s->session->peer_sha256, &sha256);
2298 s->session->peer_sha256_valid = 1;
2299 }
2300 is_first_certificate = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002301
Adam Langleyfcf25832014-12-18 17:42:32 -08002302 data = CBS_data(&certificate);
2303 x = d2i_X509(NULL, &data, CBS_len(&certificate));
2304 if (x == NULL) {
2305 al = SSL_AD_BAD_CERTIFICATE;
2306 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_ASN1_LIB);
2307 goto f_err;
2308 }
2309 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
2310 al = SSL_AD_DECODE_ERROR;
2311 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2312 SSL_R_CERT_LENGTH_MISMATCH);
2313 goto f_err;
2314 }
2315 if (!sk_X509_push(sk, x)) {
2316 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2317 goto err;
2318 }
2319 x = NULL;
2320 }
2321
2322 if (sk_X509_num(sk) <= 0) {
2323 /* TLS does not mind 0 certs returned */
2324 if (s->version == SSL3_VERSION) {
2325 al = SSL_AD_HANDSHAKE_FAILURE;
2326 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2327 SSL_R_NO_CERTIFICATES_RETURNED);
2328 goto f_err;
2329 }
2330 /* Fail for TLS only if we required a certificate */
2331 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
2332 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2333 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2334 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2335 al = SSL_AD_HANDSHAKE_FAILURE;
2336 goto f_err;
2337 }
2338 /* No client certificate so digest cached records */
2339 if (s->s3->handshake_buffer &&
2340 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2341 al = SSL_AD_INTERNAL_ERROR;
2342 goto f_err;
2343 }
2344 } else {
2345 i = ssl_verify_cert_chain(s, sk);
2346 if (i <= 0) {
2347 al = ssl_verify_alarm_type(s->verify_result);
2348 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2349 SSL_R_CERTIFICATE_VERIFY_FAILED);
2350 goto f_err;
2351 }
2352 }
2353
2354 if (s->session->peer != NULL) {
2355 /* This should not be needed */
2356 X509_free(s->session->peer);
2357 }
2358
2359 s->session->peer = sk_X509_shift(sk);
2360 s->session->verify_result = s->verify_result;
2361
2362 /* With the current implementation, sess_cert will always be NULL when we
2363 * arrive here. */
2364 if (s->session->sess_cert == NULL) {
2365 s->session->sess_cert = ssl_sess_cert_new();
2366 if (s->session->sess_cert == NULL) {
2367 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2368 goto err;
2369 }
2370 }
2371 if (s->session->sess_cert->cert_chain != NULL) {
2372 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
2373 }
2374 s->session->sess_cert->cert_chain = sk;
2375 /* Inconsistency alert: cert_chain does *not* include the peer's own
2376 * certificate, while we do include it in s3_clnt.c */
2377
2378 sk = NULL;
2379
2380 ret = 1;
2381
2382 if (0) {
2383 f_err:
2384 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2385 }
2386
Adam Langley95c29f32014-06-20 12:00:00 -07002387err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002388 if (x != NULL) {
2389 X509_free(x);
2390 }
2391 if (sk != NULL) {
2392 sk_X509_pop_free(sk, X509_free);
2393 }
2394 return ret;
2395}
Adam Langley95c29f32014-06-20 12:00:00 -07002396
Adam Langleyfcf25832014-12-18 17:42:32 -08002397int ssl3_send_server_certificate(SSL *s) {
2398 CERT_PKEY *cpk;
Adam Langley95c29f32014-06-20 12:00:00 -07002399
Adam Langleyfcf25832014-12-18 17:42:32 -08002400 if (s->state == SSL3_ST_SW_CERT_A) {
2401 cpk = ssl_get_server_send_pkey(s);
2402 if (cpk == NULL) {
2403 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_certificate,
2404 ERR_R_INTERNAL_ERROR);
2405 return 0;
2406 }
Adam Langley95c29f32014-06-20 12:00:00 -07002407
David Benjamin9d0847a2015-02-16 03:57:55 -05002408 if (!ssl3_output_cert_chain(s, cpk)) {
2409 return 0;
2410 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002411 s->state = SSL3_ST_SW_CERT_B;
2412 }
Adam Langley95c29f32014-06-20 12:00:00 -07002413
Adam Langleyfcf25832014-12-18 17:42:32 -08002414 /* SSL3_ST_SW_CERT_B */
2415 return ssl_do_write(s);
2416}
Adam Langley95c29f32014-06-20 12:00:00 -07002417
Adam Langley95c29f32014-06-20 12:00:00 -07002418/* send a new session ticket (not necessarily for a new session) */
Adam Langleyfcf25832014-12-18 17:42:32 -08002419int ssl3_send_new_session_ticket(SSL *s) {
David Benjaminbcd37452015-03-09 14:48:28 -04002420 int ret = -1;
2421 uint8_t *session = NULL;
2422 size_t session_len;
2423 EVP_CIPHER_CTX ctx;
2424 HMAC_CTX hctx;
2425
2426 EVP_CIPHER_CTX_init(&ctx);
2427 HMAC_CTX_init(&hctx);
2428
Adam Langleyfcf25832014-12-18 17:42:32 -08002429 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002430 uint8_t *p, *macstart;
2431 int len;
2432 unsigned int hlen;
Adam Langleyfcf25832014-12-18 17:42:32 -08002433 SSL_CTX *tctx = s->initial_ctx;
2434 uint8_t iv[EVP_MAX_IV_LENGTH];
2435 uint8_t key_name[16];
2436 /* The maximum overhead of encrypting the session is 16 (key name) + IV +
2437 * one block of encryption overhead + HMAC. */
2438 const size_t max_ticket_overhead =
2439 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -07002440
Adam Langleyfcf25832014-12-18 17:42:32 -08002441 /* Serialize the SSL_SESSION to be encoded into the ticket. */
2442 if (!SSL_SESSION_to_bytes_for_ticket(s->session, &session, &session_len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002443 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002444 }
Adam Langley95c29f32014-06-20 12:00:00 -07002445
Adam Langleyfcf25832014-12-18 17:42:32 -08002446 /* If the session is too long, emit a dummy value rather than abort the
2447 * connection. */
2448 if (session_len > 0xFFFF - max_ticket_overhead) {
2449 static const char kTicketPlaceholder[] = "TICKET TOO LARGE";
2450 const size_t placeholder_len = strlen(kTicketPlaceholder);
David Benjamin3cac4502014-10-21 01:46:30 -04002451
Adam Langleyfcf25832014-12-18 17:42:32 -08002452 OPENSSL_free(session);
David Benjaminbcd37452015-03-09 14:48:28 -04002453 session = NULL;
David Benjamin3cac4502014-10-21 01:46:30 -04002454
Adam Langleyfcf25832014-12-18 17:42:32 -08002455 p = ssl_handshake_start(s);
2456 /* Emit ticket_lifetime_hint. */
2457 l2n(0, p);
2458 /* Emit ticket. */
2459 s2n(placeholder_len, p);
2460 memcpy(p, kTicketPlaceholder, placeholder_len);
2461 p += placeholder_len;
David Benjamin3cac4502014-10-21 01:46:30 -04002462
Adam Langleyfcf25832014-12-18 17:42:32 -08002463 len = p - ssl_handshake_start(s);
David Benjaminfbdfefb2015-02-16 19:33:53 -05002464 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002465 goto err;
David Benjaminfbdfefb2015-02-16 19:33:53 -05002466 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002467 s->state = SSL3_ST_SW_SESSION_TICKET_B;
2468 return ssl_do_write(s);
2469 }
Adam Langley95c29f32014-06-20 12:00:00 -07002470
Adam Langleyfcf25832014-12-18 17:42:32 -08002471 /* Grow buffer if need be: the length calculation is as follows:
2472 * handshake_header_length + 4 (ticket lifetime hint) + 2 (ticket length) +
2473 * max_ticket_overhead + * session_length */
2474 if (!BUF_MEM_grow(s->init_buf, SSL_HM_HEADER_LENGTH(s) + 6 +
2475 max_ticket_overhead + session_len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002476 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002477 }
2478 p = ssl_handshake_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002479 /* Initialize HMAC and cipher contexts. If callback present it does all the
2480 * work otherwise use generated values from parent ctx. */
2481 if (tctx->tlsext_ticket_key_cb) {
2482 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, &hctx, 1) < 0) {
David Benjaminbcd37452015-03-09 14:48:28 -04002483 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002484 }
2485 } else {
2486 if (!RAND_bytes(iv, 16) ||
2487 !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2488 tctx->tlsext_tick_aes_key, iv) ||
2489 !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2490 NULL)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002491 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002492 }
2493 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
2494 }
Adam Langley95c29f32014-06-20 12:00:00 -07002495
Adam Langleyfcf25832014-12-18 17:42:32 -08002496 /* Ticket lifetime hint (advisory only): We leave this unspecified for
2497 * resumed session (for simplicity), and guess that tickets for new
2498 * sessions will live as long as their sessions. */
2499 l2n(s->hit ? 0 : s->session->timeout, p);
Adam Langley95c29f32014-06-20 12:00:00 -07002500
Adam Langleyfcf25832014-12-18 17:42:32 -08002501 /* Skip ticket length for now */
2502 p += 2;
2503 /* Output key name */
2504 macstart = p;
2505 memcpy(p, key_name, 16);
2506 p += 16;
2507 /* output IV */
2508 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
2509 p += EVP_CIPHER_CTX_iv_length(&ctx);
2510 /* Encrypt session data */
David Benjaminbcd37452015-03-09 14:48:28 -04002511 if (!EVP_EncryptUpdate(&ctx, p, &len, session, session_len)) {
2512 goto err;
2513 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002514 p += len;
David Benjaminbcd37452015-03-09 14:48:28 -04002515 if (!EVP_EncryptFinal_ex(&ctx, p, &len)) {
2516 goto err;
2517 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002518 p += len;
Adam Langley95c29f32014-06-20 12:00:00 -07002519
David Benjaminbcd37452015-03-09 14:48:28 -04002520 if (!HMAC_Update(&hctx, macstart, p - macstart) ||
2521 !HMAC_Final(&hctx, p, &hlen)) {
2522 goto err;
2523 }
Adam Langley95c29f32014-06-20 12:00:00 -07002524
Adam Langleyfcf25832014-12-18 17:42:32 -08002525 p += hlen;
2526 /* Now write out lengths: p points to end of data written */
2527 /* Total length */
2528 len = p - ssl_handshake_start(s);
2529 /* Skip ticket lifetime hint */
2530 p = ssl_handshake_start(s) + 4;
2531 s2n(len - 6, p);
David Benjaminfbdfefb2015-02-16 19:33:53 -05002532 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002533 goto err;
David Benjaminfbdfefb2015-02-16 19:33:53 -05002534 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002535 s->state = SSL3_ST_SW_SESSION_TICKET_B;
Adam Langleyfcf25832014-12-18 17:42:32 -08002536 }
Adam Langley95c29f32014-06-20 12:00:00 -07002537
Adam Langleyfcf25832014-12-18 17:42:32 -08002538 /* SSL3_ST_SW_SESSION_TICKET_B */
David Benjaminbcd37452015-03-09 14:48:28 -04002539 ret = ssl_do_write(s);
2540
2541err:
2542 if (session != NULL) {
2543 OPENSSL_free(session);
2544 }
2545 EVP_CIPHER_CTX_cleanup(&ctx);
2546 HMAC_CTX_cleanup(&hctx);
2547 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002548}
Adam Langley95c29f32014-06-20 12:00:00 -07002549
Adam Langley95c29f32014-06-20 12:00:00 -07002550/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
2551 * sets the next_proto member in s if found */
Adam Langleyfcf25832014-12-18 17:42:32 -08002552int ssl3_get_next_proto(SSL *s) {
2553 int ok;
2554 long n;
2555 CBS next_protocol, selected_protocol, padding;
Adam Langley95c29f32014-06-20 12:00:00 -07002556
Adam Langleyfcf25832014-12-18 17:42:32 -08002557 /* Clients cannot send a NextProtocol message if we didn't see the extension
2558 * in their ClientHello */
2559 if (!s->s3->next_proto_neg_seen) {
2560 OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto,
2561 SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
2562 return -1;
2563 }
Adam Langley95c29f32014-06-20 12:00:00 -07002564
Adam Langleyfcf25832014-12-18 17:42:32 -08002565 n = s->method->ssl_get_message(s, SSL3_ST_SR_NEXT_PROTO_A,
2566 SSL3_ST_SR_NEXT_PROTO_B, SSL3_MT_NEXT_PROTO,
2567 514, /* See the payload format below */
David Benjamin5ca39fb2015-03-01 23:57:54 -05002568 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002569
Adam Langleyfcf25832014-12-18 17:42:32 -08002570 if (!ok) {
2571 return n;
2572 }
Adam Langley95c29f32014-06-20 12:00:00 -07002573
Adam Langleyfcf25832014-12-18 17:42:32 -08002574 /* s->state doesn't reflect whether ChangeCipherSpec has been received in
2575 * this handshake, but s->s3->change_cipher_spec does (will be reset by
2576 * ssl3_get_finished).
2577 *
2578 * TODO(davidben): Is this check now redundant with
2579 * SSL3_FLAGS_EXPECT_CCS? */
2580 if (!s->s3->change_cipher_spec) {
2581 OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto,
2582 SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
2583 return -1;
2584 }
Adam Langley95c29f32014-06-20 12:00:00 -07002585
Adam Langleyfcf25832014-12-18 17:42:32 -08002586 CBS_init(&next_protocol, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07002587
Adam Langleyfcf25832014-12-18 17:42:32 -08002588 /* The payload looks like:
2589 * uint8 proto_len;
2590 * uint8 proto[proto_len];
2591 * uint8 padding_len;
2592 * uint8 padding[padding_len]; */
2593 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
2594 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
2595 CBS_len(&next_protocol) != 0 ||
2596 !CBS_stow(&selected_protocol, &s->next_proto_negotiated,
2597 &s->next_proto_negotiated_len)) {
2598 return 0;
2599 }
Adam Langley95c29f32014-06-20 12:00:00 -07002600
Adam Langleyfcf25832014-12-18 17:42:32 -08002601 return 1;
2602}
Adam Langley95c29f32014-06-20 12:00:00 -07002603
Adam Langley1258b6a2014-06-20 12:00:00 -07002604/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002605int ssl3_get_channel_id(SSL *s) {
2606 int ret = -1, ok;
2607 long n;
2608 EVP_MD_CTX md_ctx;
2609 uint8_t channel_id_hash[SHA256_DIGEST_LENGTH];
2610 unsigned int channel_id_hash_len;
2611 const uint8_t *p;
2612 uint16_t extension_type, expected_extension_type;
2613 EC_GROUP *p256 = NULL;
2614 EC_KEY *key = NULL;
2615 EC_POINT *point = NULL;
2616 ECDSA_SIG sig;
2617 BIGNUM x, y;
2618 CBS encrypted_extensions, extension;
Adam Langley1258b6a2014-06-20 12:00:00 -07002619
Adam Langleyfcf25832014-12-18 17:42:32 -08002620 n = s->method->ssl_get_message(
2621 s, SSL3_ST_SR_CHANNEL_ID_A, SSL3_ST_SR_CHANNEL_ID_B,
2622 SSL3_MT_ENCRYPTED_EXTENSIONS, 2 + 2 + TLSEXT_CHANNEL_ID_SIZE,
David Benjamin5ca39fb2015-03-01 23:57:54 -05002623 ssl_dont_hash_message, &ok);
Adam Langley1258b6a2014-06-20 12:00:00 -07002624
Adam Langleyfcf25832014-12-18 17:42:32 -08002625 if (!ok) {
2626 return n;
2627 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002628
Adam Langleyfcf25832014-12-18 17:42:32 -08002629 /* Before incorporating the EncryptedExtensions message to the handshake
2630 * hash, compute the hash that should have been signed. */
2631 channel_id_hash_len = sizeof(channel_id_hash);
2632 EVP_MD_CTX_init(&md_ctx);
2633 if (!EVP_DigestInit_ex(&md_ctx, EVP_sha256(), NULL) ||
2634 !tls1_channel_id_hash(&md_ctx, s) ||
2635 !EVP_DigestFinal(&md_ctx, channel_id_hash, &channel_id_hash_len)) {
2636 EVP_MD_CTX_cleanup(&md_ctx);
2637 return -1;
2638 }
2639 EVP_MD_CTX_cleanup(&md_ctx);
2640 assert(channel_id_hash_len == SHA256_DIGEST_LENGTH);
David Benjamin880b14e2014-08-25 22:35:07 -04002641
David Benjaminfbdfefb2015-02-16 19:33:53 -05002642 if (!ssl3_hash_current_message(s)) {
2643 return -1;
2644 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002645
Adam Langleyfcf25832014-12-18 17:42:32 -08002646 /* s->state doesn't reflect whether ChangeCipherSpec has been received in
2647 * this handshake, but s->s3->change_cipher_spec does (will be reset by
2648 * ssl3_get_finished).
2649 *
2650 * TODO(davidben): Is this check now redundant with SSL3_FLAGS_EXPECT_CCS? */
2651 if (!s->s3->change_cipher_spec) {
2652 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id,
2653 SSL_R_GOT_CHANNEL_ID_BEFORE_A_CCS);
2654 return -1;
2655 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002656
Adam Langleyfcf25832014-12-18 17:42:32 -08002657 CBS_init(&encrypted_extensions, s->init_msg, n);
Adam Langley1258b6a2014-06-20 12:00:00 -07002658
Adam Langleyfcf25832014-12-18 17:42:32 -08002659 /* EncryptedExtensions could include multiple extensions, but the only
2660 * extension that could be negotiated is ChannelID, so there can only be one
2661 * entry.
2662 *
2663 * The payload looks like:
2664 * uint16 extension_type
2665 * uint16 extension_len;
2666 * uint8 x[32];
2667 * uint8 y[32];
2668 * uint8 r[32];
2669 * uint8 s[32]; */
2670 expected_extension_type = TLSEXT_TYPE_channel_id;
2671 if (s->s3->tlsext_channel_id_new) {
2672 expected_extension_type = TLSEXT_TYPE_channel_id_new;
2673 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002674
Adam Langleyfcf25832014-12-18 17:42:32 -08002675 if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
2676 !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
2677 CBS_len(&encrypted_extensions) != 0 ||
2678 extension_type != expected_extension_type ||
2679 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
2680 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_INVALID_MESSAGE);
2681 return -1;
2682 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002683
Adam Langleyfcf25832014-12-18 17:42:32 -08002684 p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
2685 if (!p256) {
2686 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_NO_P256_SUPPORT);
2687 return -1;
2688 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002689
Adam Langleyfcf25832014-12-18 17:42:32 -08002690 BN_init(&x);
2691 BN_init(&y);
2692 sig.r = BN_new();
2693 sig.s = BN_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05002694 if (sig.r == NULL || sig.s == NULL) {
2695 goto err;
2696 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002697
Adam Langleyfcf25832014-12-18 17:42:32 -08002698 p = CBS_data(&extension);
2699 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
2700 BN_bin2bn(p + 32, 32, &y) == NULL ||
2701 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
2702 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
2703 goto err;
2704 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002705
Adam Langleyfcf25832014-12-18 17:42:32 -08002706 point = EC_POINT_new(p256);
2707 if (!point || !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
2708 goto err;
2709 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002710
Adam Langleyfcf25832014-12-18 17:42:32 -08002711 key = EC_KEY_new();
2712 if (!key || !EC_KEY_set_group(key, p256) ||
2713 !EC_KEY_set_public_key(key, point)) {
2714 goto err;
2715 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002716
Adam Langleyfcf25832014-12-18 17:42:32 -08002717 /* We stored the handshake hash in |tlsext_channel_id| the first time that we
2718 * were called. */
2719 if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) {
2720 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id,
2721 SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
2722 s->s3->tlsext_channel_id_valid = 0;
2723 goto err;
2724 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002725
Adam Langleyfcf25832014-12-18 17:42:32 -08002726 memcpy(s->s3->tlsext_channel_id, p, 64);
2727 ret = 1;
Adam Langley1258b6a2014-06-20 12:00:00 -07002728
2729err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002730 BN_free(&x);
2731 BN_free(&y);
2732 BN_free(sig.r);
2733 BN_free(sig.s);
2734 if (key) {
2735 EC_KEY_free(key);
2736 }
2737 if (point) {
2738 EC_POINT_free(point);
2739 }
2740 if (p256) {
2741 EC_GROUP_free(p256);
2742 }
2743 return ret;
2744}