blob: b7fc545f6c023d78b33796b20f4470d2bc7b44fb [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>
David Benjaminf0ae1702015-04-07 23:05:04 -0400160#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700161#include <openssl/evp.h>
162#include <openssl/hmac.h>
163#include <openssl/md5.h>
164#include <openssl/mem.h>
165#include <openssl/obj.h>
166#include <openssl/rand.h>
Adam Langley1258b6a2014-06-20 12:00:00 -0700167#include <openssl/sha.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700168#include <openssl/x509.h>
169
David Benjamin2ee94aa2015-04-07 22:38:30 -0400170#include "internal.h"
Adam Langleyaf6e45b2014-11-03 19:34:49 -0800171#include "../crypto/internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700172#include "../crypto/dh/internal.h"
173
Adam Langleyfcf25832014-12-18 17:42:32 -0800174
David Benjamin4b755cb2014-12-12 03:58:07 -0500175/* INITIAL_SNIFF_BUFFER_SIZE is the number of bytes read in the initial sniff
176 * buffer. */
177#define INITIAL_SNIFF_BUFFER_SIZE 8
178
Adam Langleyfcf25832014-12-18 17:42:32 -0800179int ssl3_accept(SSL *s) {
180 BUF_MEM *buf = NULL;
David Benjamin3fa27772015-04-17 22:32:19 -0400181 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -0800182 void (*cb)(const SSL *ssl, int type, int val) = NULL;
183 int ret = -1;
184 int new_state, state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700185
Adam Langleyfcf25832014-12-18 17:42:32 -0800186 assert(s->handshake_func == ssl3_accept);
187 assert(s->server);
188 assert(!SSL_IS_DTLS(s));
David Benjaminbeb47022014-11-30 02:58:52 -0500189
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 ERR_clear_error();
191 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700192
Adam Langleyfcf25832014-12-18 17:42:32 -0800193 if (s->info_callback != NULL) {
194 cb = s->info_callback;
195 } else if (s->ctx->info_callback != NULL) {
196 cb = s->ctx->info_callback;
197 }
Adam Langley95c29f32014-06-20 12:00:00 -0700198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 s->in_handshake++;
Adam Langley95c29f32014-06-20 12:00:00 -0700200
Adam Langleyfcf25832014-12-18 17:42:32 -0800201 if (s->cert == NULL) {
202 OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_NO_CERTIFICATE_SET);
203 return -1;
204 }
Adam Langley95c29f32014-06-20 12:00:00 -0700205
Adam Langleyfcf25832014-12-18 17:42:32 -0800206 for (;;) {
207 state = s->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700208
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 switch (s->state) {
210 case SSL_ST_RENEGOTIATE:
211 /* This state is the renegotiate entry point. It sends a HelloRequest
212 * and nothing else. */
213 s->renegotiate = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700214
Adam Langleyfcf25832014-12-18 17:42:32 -0800215 if (cb != NULL) {
216 cb(s, SSL_CB_HANDSHAKE_START, 1);
217 }
Adam Langley95c29f32014-06-20 12:00:00 -0700218
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 if (s->init_buf == NULL) {
220 buf = BUF_MEM_new();
221 if (!buf || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
222 ret = -1;
223 goto end;
224 }
225 s->init_buf = buf;
226 buf = NULL;
227 }
228 s->init_num = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500229
Adam Langleyfcf25832014-12-18 17:42:32 -0800230 if (!ssl3_setup_buffers(s)) {
231 ret = -1;
232 goto end;
233 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500234
Adam Langleyfcf25832014-12-18 17:42:32 -0800235 if (!s->s3->send_connection_binding &&
236 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
237 /* Server attempting to renegotiate with client that doesn't support
238 * secure renegotiation. */
239 OPENSSL_PUT_ERROR(SSL, ssl3_accept,
240 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
241 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
242 ret = -1;
243 goto end;
244 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500245
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 s->state = SSL3_ST_SW_HELLO_REQ_A;
247 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 case SSL3_ST_SW_HELLO_REQ_A:
250 case SSL3_ST_SW_HELLO_REQ_B:
251 s->shutdown = 0;
252 ret = ssl3_send_hello_request(s);
253 if (ret <= 0) {
254 goto end;
255 }
256 s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C;
257 s->state = SSL3_ST_SW_FLUSH;
258 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700259
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 if (!ssl3_init_finished_mac(s)) {
261 OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR);
262 ret = -1;
263 goto end;
264 }
265 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700266
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 case SSL3_ST_SW_HELLO_REQ_C:
268 s->state = SSL_ST_OK;
269 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700270
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 case SSL_ST_ACCEPT:
272 case SSL_ST_BEFORE | SSL_ST_ACCEPT:
273 /* This state is the entry point for the handshake itself (initial and
274 * renegotiation). */
275 if (cb != NULL) {
276 cb(s, SSL_CB_HANDSHAKE_START, 1);
277 }
Adam Langley95c29f32014-06-20 12:00:00 -0700278
David Benjaminb16346b2015-04-08 19:16:58 -0400279 if ((s->version >> 8) != 3) {
280 /* TODO(davidben): Some consumers clear |s->version| to break the
281 * handshake in a callback. Remove this when they're using proper
282 * APIs. */
283 OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR);
284 ret = -1;
285 goto end;
286 }
287
Adam Langleyfcf25832014-12-18 17:42:32 -0800288 if (s->init_buf == NULL) {
289 buf = BUF_MEM_new();
290 if (!buf || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
291 ret = -1;
292 goto end;
293 }
294 s->init_buf = buf;
295 buf = NULL;
296 }
297 s->init_num = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500298
Adam Langleyfcf25832014-12-18 17:42:32 -0800299 if (!ssl3_init_finished_mac(s)) {
300 OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR);
301 ret = -1;
302 goto end;
303 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500304
Adam Langleyfcf25832014-12-18 17:42:32 -0800305 if (!s->s3->have_version) {
306 /* This is the initial handshake. The record layer has not been
307 * initialized yet. Sniff for a V2ClientHello before reading a
308 * ClientHello normally. */
309 assert(s->s3->rbuf.buf == NULL);
310 assert(s->s3->wbuf.buf == NULL);
311 s->state = SSL3_ST_SR_INITIAL_BYTES;
312 } else {
313 /* Enable a write buffer. This groups handshake messages within a
314 * flight into a single write. */
315 if (!ssl3_setup_buffers(s) || !ssl_init_wbio_buffer(s, 1)) {
316 ret = -1;
317 goto end;
318 }
319 s->state = SSL3_ST_SR_CLNT_HELLO_A;
320 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800321 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500322
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 case SSL3_ST_SR_INITIAL_BYTES:
324 ret = ssl3_get_initial_bytes(s);
325 if (ret <= 0) {
326 goto end;
327 }
328 /* ssl3_get_initial_bytes sets s->state to one of
329 * SSL3_ST_SR_V2_CLIENT_HELLO or SSL3_ST_SR_CLNT_HELLO_A on success. */
330 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500331
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 case SSL3_ST_SR_V2_CLIENT_HELLO:
333 ret = ssl3_get_v2_client_hello(s);
334 if (ret <= 0) {
335 goto end;
336 }
337 s->state = SSL3_ST_SR_CLNT_HELLO_A;
338 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500339
Adam Langleyfcf25832014-12-18 17:42:32 -0800340 case SSL3_ST_SR_CLNT_HELLO_A:
341 case SSL3_ST_SR_CLNT_HELLO_B:
342 case SSL3_ST_SR_CLNT_HELLO_C:
343 case SSL3_ST_SR_CLNT_HELLO_D:
344 s->shutdown = 0;
345 ret = ssl3_get_client_hello(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800346 if (ret <= 0) {
347 goto end;
348 }
349 s->renegotiate = 2;
350 s->state = SSL3_ST_SW_SRVR_HELLO_A;
351 s->init_num = 0;
352 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500353
Adam Langleyfcf25832014-12-18 17:42:32 -0800354 case SSL3_ST_SW_SRVR_HELLO_A:
355 case SSL3_ST_SW_SRVR_HELLO_B:
356 ret = ssl3_send_server_hello(s);
357 if (ret <= 0) {
358 goto end;
359 }
360 if (s->hit) {
361 if (s->tlsext_ticket_expected) {
362 s->state = SSL3_ST_SW_SESSION_TICKET_A;
363 } else {
364 s->state = SSL3_ST_SW_CHANGE_A;
365 }
366 } else {
367 s->state = SSL3_ST_SW_CERT_A;
368 }
369 s->init_num = 0;
370 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700371
Adam Langleyfcf25832014-12-18 17:42:32 -0800372 case SSL3_ST_SW_CERT_A:
373 case SSL3_ST_SW_CERT_B:
374 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
375 ret = ssl3_send_server_certificate(s);
376 if (ret <= 0) {
377 goto end;
378 }
379 if (s->s3->tmp.certificate_status_expected) {
380 s->state = SSL3_ST_SW_CERT_STATUS_A;
381 } else {
382 s->state = SSL3_ST_SW_KEY_EXCH_A;
383 }
384 } else {
385 skip = 1;
386 s->state = SSL3_ST_SW_KEY_EXCH_A;
387 }
388 s->init_num = 0;
389 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700390
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 case SSL3_ST_SW_KEY_EXCH_A:
392 case SSL3_ST_SW_KEY_EXCH_B:
393 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700394
Adam Langleyfcf25832014-12-18 17:42:32 -0800395 /* Send a ServerKeyExchange message if:
396 * - The key exchange is ephemeral or anonymous
397 * Diffie-Hellman.
398 * - There is a PSK identity hint.
399 *
400 * TODO(davidben): This logic is currently duplicated in d1_srvr.c. Fix
401 * this. In the meantime, keep them in sync. */
402 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) ||
403 ((alg_a & SSL_aPSK) && s->psk_identity_hint)) {
404 ret = ssl3_send_server_key_exchange(s);
David Benjamin6eb000d2015-02-11 01:17:41 -0500405 if (ret <= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500407 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800408 } else {
409 skip = 1;
410 }
Adam Langley95c29f32014-06-20 12:00:00 -0700411
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 s->state = SSL3_ST_SW_CERT_REQ_A;
413 s->init_num = 0;
414 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700415
Adam Langleyfcf25832014-12-18 17:42:32 -0800416 case SSL3_ST_SW_CERT_REQ_A:
417 case SSL3_ST_SW_CERT_REQ_B:
418 if (/* don't request cert unless asked for it: */
419 !(s->verify_mode & SSL_VERIFY_PEER) ||
420 /* Don't request a certificate if an obc was presented */
421 ((s->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
422 s->s3->tlsext_channel_id_valid) ||
423 /* if SSL_VERIFY_CLIENT_ONCE is set,
424 * don't request cert during re-negotiation: */
425 ((s->session->peer != NULL) &&
426 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800427 /* With normal PSK Certificates and
428 * Certificate Requests are omitted */
429 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
430 /* no cert request */
431 skip = 1;
432 s->s3->tmp.cert_request = 0;
433 s->state = SSL3_ST_SW_SRVR_DONE_A;
434 if (s->s3->handshake_buffer &&
435 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
436 return -1;
437 }
438 } else {
439 s->s3->tmp.cert_request = 1;
440 ret = ssl3_send_certificate_request(s);
441 if (ret <= 0) {
442 goto end;
443 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800444 s->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800445 s->init_num = 0;
446 }
447 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700448
Adam Langleyfcf25832014-12-18 17:42:32 -0800449 case SSL3_ST_SW_SRVR_DONE_A:
450 case SSL3_ST_SW_SRVR_DONE_B:
451 ret = ssl3_send_server_done(s);
452 if (ret <= 0) {
453 goto end;
454 }
455 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
456 s->state = SSL3_ST_SW_FLUSH;
457 s->init_num = 0;
458 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700459
Adam Langleyfcf25832014-12-18 17:42:32 -0800460 case SSL3_ST_SW_FLUSH:
461 /* This code originally checked to see if any data was pending using
462 * BIO_CTRL_INFO and then flushed. This caused problems as documented
463 * in PR#1939. The proposed fix doesn't completely resolve this issue
464 * as buggy implementations of BIO_CTRL_PENDING still exist. So instead
465 * we just flush unconditionally. */
466 s->rwstate = SSL_WRITING;
467 if (BIO_flush(s->wbio) <= 0) {
468 ret = -1;
469 goto end;
470 }
471 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700472
Adam Langleyfcf25832014-12-18 17:42:32 -0800473 s->state = s->s3->tmp.next_state;
474 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700475
Adam Langleyfcf25832014-12-18 17:42:32 -0800476 case SSL3_ST_SR_CERT_A:
477 case SSL3_ST_SR_CERT_B:
478 if (s->s3->tmp.cert_request) {
479 ret = ssl3_get_client_certificate(s);
480 if (ret <= 0) {
481 goto end;
482 }
483 }
484 s->init_num = 0;
485 s->state = SSL3_ST_SR_KEY_EXCH_A;
486 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700487
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 case SSL3_ST_SR_KEY_EXCH_A:
489 case SSL3_ST_SR_KEY_EXCH_B:
490 ret = ssl3_get_client_key_exchange(s);
491 if (ret <= 0) {
492 goto end;
493 }
494 s->state = SSL3_ST_SR_CERT_VRFY_A;
495 s->init_num = 0;
496 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700497
Adam Langleyfcf25832014-12-18 17:42:32 -0800498 case SSL3_ST_SR_CERT_VRFY_A:
499 case SSL3_ST_SR_CERT_VRFY_B:
500 ret = ssl3_get_cert_verify(s);
501 if (ret <= 0) {
502 goto end;
503 }
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Adam Langleyfcf25832014-12-18 17:42:32 -0800505 s->state = SSL3_ST_SR_CHANGE;
506 s->init_num = 0;
507 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700508
Adam Langleyfcf25832014-12-18 17:42:32 -0800509 case SSL3_ST_SR_CHANGE: {
510 char next_proto_neg = 0;
511 char channel_id = 0;
512 next_proto_neg = s->s3->next_proto_neg_seen;
513 channel_id = s->s3->tlsext_channel_id_valid;
Adam Langley95c29f32014-06-20 12:00:00 -0700514
Adam Langleyfcf25832014-12-18 17:42:32 -0800515 /* At this point, the next message must be entirely behind a
516 * ChangeCipherSpec. */
517 if (!ssl3_expect_change_cipher_spec(s)) {
518 ret = -1;
519 goto end;
520 }
521 if (next_proto_neg) {
522 s->state = SSL3_ST_SR_NEXT_PROTO_A;
523 } else if (channel_id) {
524 s->state = SSL3_ST_SR_CHANNEL_ID_A;
525 } else {
526 s->state = SSL3_ST_SR_FINISHED_A;
527 }
528 break;
529 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700530
Adam Langleyfcf25832014-12-18 17:42:32 -0800531 case SSL3_ST_SR_NEXT_PROTO_A:
532 case SSL3_ST_SR_NEXT_PROTO_B:
533 ret = ssl3_get_next_proto(s);
534 if (ret <= 0) {
535 goto end;
536 }
537 s->init_num = 0;
538 if (s->s3->tlsext_channel_id_valid) {
539 s->state = SSL3_ST_SR_CHANNEL_ID_A;
540 } else {
541 s->state = SSL3_ST_SR_FINISHED_A;
542 }
543 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700544
Adam Langleyfcf25832014-12-18 17:42:32 -0800545 case SSL3_ST_SR_CHANNEL_ID_A:
546 case SSL3_ST_SR_CHANNEL_ID_B:
547 ret = ssl3_get_channel_id(s);
548 if (ret <= 0) {
549 goto end;
550 }
551 s->init_num = 0;
552 s->state = SSL3_ST_SR_FINISHED_A;
553 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700554
Adam Langleyfcf25832014-12-18 17:42:32 -0800555 case SSL3_ST_SR_FINISHED_A:
556 case SSL3_ST_SR_FINISHED_B:
557 ret =
558 ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, SSL3_ST_SR_FINISHED_B);
559 if (ret <= 0) {
560 goto end;
561 }
Adam Langley95c29f32014-06-20 12:00:00 -0700562
Adam Langleyfcf25832014-12-18 17:42:32 -0800563 if (s->hit) {
564 s->state = SSL_ST_OK;
565 } else if (s->tlsext_ticket_expected) {
566 s->state = SSL3_ST_SW_SESSION_TICKET_A;
567 } else {
568 s->state = SSL3_ST_SW_CHANGE_A;
569 }
570 /* If this is a full handshake with ChannelID then record the hashshake
571 * hashes in |s->session| in case we need them to verify a ChannelID
572 * signature on a resumption of this session in the future. */
573 if (!s->hit && s->s3->tlsext_channel_id_new) {
574 ret = tls1_record_handshake_hashes_for_channel_id(s);
575 if (ret <= 0) {
576 goto end;
577 }
578 }
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_SESSION_TICKET_A:
583 case SSL3_ST_SW_SESSION_TICKET_B:
584 ret = ssl3_send_new_session_ticket(s);
585 if (ret <= 0) {
586 goto end;
587 }
588 s->state = SSL3_ST_SW_CHANGE_A;
589 s->init_num = 0;
590 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700591
Adam Langleyfcf25832014-12-18 17:42:32 -0800592 case SSL3_ST_SW_CHANGE_A:
593 case SSL3_ST_SW_CHANGE_B:
594 s->session->cipher = s->s3->tmp.new_cipher;
595 if (!s->enc_method->setup_key_block(s)) {
596 ret = -1;
597 goto end;
598 }
Adam Langley95c29f32014-06-20 12:00:00 -0700599
Adam Langleyfcf25832014-12-18 17:42:32 -0800600 ret = ssl3_send_change_cipher_spec(s, SSL3_ST_SW_CHANGE_A,
601 SSL3_ST_SW_CHANGE_B);
602 if (ret <= 0) {
603 goto end;
604 }
605 s->state = SSL3_ST_SW_FINISHED_A;
606 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700607
Adam Langleyfcf25832014-12-18 17:42:32 -0800608 if (!s->enc_method->change_cipher_state(
609 s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
610 ret = -1;
611 goto end;
612 }
613 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700614
Adam Langleyfcf25832014-12-18 17:42:32 -0800615 case SSL3_ST_SW_FINISHED_A:
616 case SSL3_ST_SW_FINISHED_B:
617 ret =
618 ssl3_send_finished(s, SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B,
619 s->enc_method->server_finished_label,
620 s->enc_method->server_finished_label_len);
621 if (ret <= 0) {
622 goto end;
623 }
624 s->state = SSL3_ST_SW_FLUSH;
625 if (s->hit) {
626 s->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
627 } else {
628 s->s3->tmp.next_state = SSL_ST_OK;
629 }
630 s->init_num = 0;
631 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700632
Adam Langleyfcf25832014-12-18 17:42:32 -0800633 case SSL_ST_OK:
634 /* clean a few things up */
635 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700636
Adam Langleyfcf25832014-12-18 17:42:32 -0800637 BUF_MEM_free(s->init_buf);
638 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700639
Adam Langleyfcf25832014-12-18 17:42:32 -0800640 /* remove buffering on output */
641 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700642
Adam Langleyfcf25832014-12-18 17:42:32 -0800643 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700644
Adam Langleyfcf25832014-12-18 17:42:32 -0800645 /* If we aren't retaining peer certificates then we can discard it
646 * now. */
David Benjamin2755a3e2015-04-22 16:17:58 -0400647 if (s->ctx->retain_only_sha256_of_client_certs) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800648 X509_free(s->session->peer);
649 s->session->peer = NULL;
650 }
Adam Langley95c29f32014-06-20 12:00:00 -0700651
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 if (s->renegotiate == 2) {
653 /* skipped if we just sent a HelloRequest */
654 s->renegotiate = 0;
655 s->new_session = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700656
Adam Langleyfcf25832014-12-18 17:42:32 -0800657 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
Adam Langley95c29f32014-06-20 12:00:00 -0700658
Adam Langleyfcf25832014-12-18 17:42:32 -0800659 if (cb != NULL) {
660 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
661 }
662 }
Adam Langley75872532014-06-20 12:00:00 -0700663
Adam Langleyfcf25832014-12-18 17:42:32 -0800664 ret = 1;
665 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700666
Adam Langleyfcf25832014-12-18 17:42:32 -0800667 default:
668 OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNKNOWN_STATE);
669 ret = -1;
670 goto end;
671 }
Adam Langley95c29f32014-06-20 12:00:00 -0700672
Adam Langleyfcf25832014-12-18 17:42:32 -0800673 if (!s->s3->tmp.reuse_message && !skip && cb != NULL && s->state != state) {
674 new_state = s->state;
675 s->state = state;
676 cb(s, SSL_CB_ACCEPT_LOOP, 1);
677 s->state = new_state;
678 }
679 skip = 0;
680 }
681
Adam Langley95c29f32014-06-20 12:00:00 -0700682end:
Adam Langleyfcf25832014-12-18 17:42:32 -0800683 s->in_handshake--;
David Benjamin2755a3e2015-04-22 16:17:58 -0400684 BUF_MEM_free(buf);
Adam Langleyfcf25832014-12-18 17:42:32 -0800685 if (cb != NULL) {
686 cb(s, SSL_CB_ACCEPT_EXIT, ret);
687 }
688 return ret;
689}
Adam Langley95c29f32014-06-20 12:00:00 -0700690
Adam Langleyfcf25832014-12-18 17:42:32 -0800691static int ssl3_read_sniff_buffer(SSL *s, size_t n) {
692 if (s->s3->sniff_buffer == NULL) {
693 s->s3->sniff_buffer = BUF_MEM_new();
694 }
695 if (s->s3->sniff_buffer == NULL || !BUF_MEM_grow(s->s3->sniff_buffer, n)) {
696 return -1;
697 }
Adam Langley95c29f32014-06-20 12:00:00 -0700698
Adam Langleyfcf25832014-12-18 17:42:32 -0800699 while (s->s3->sniff_buffer_len < n) {
700 int ret;
David Benjamin4b755cb2014-12-12 03:58:07 -0500701
Adam Langleyfcf25832014-12-18 17:42:32 -0800702 s->rwstate = SSL_READING;
703 ret = BIO_read(s->rbio, s->s3->sniff_buffer->data + s->s3->sniff_buffer_len,
704 n - s->s3->sniff_buffer_len);
705 if (ret <= 0) {
706 return ret;
707 }
708 s->rwstate = SSL_NOTHING;
709 s->s3->sniff_buffer_len += ret;
710 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500711
Adam Langleyfcf25832014-12-18 17:42:32 -0800712 return 1;
713}
David Benjamin4b755cb2014-12-12 03:58:07 -0500714
Adam Langleyfcf25832014-12-18 17:42:32 -0800715int ssl3_get_initial_bytes(SSL *s) {
716 int ret;
717 const uint8_t *p;
David Benjamin4b755cb2014-12-12 03:58:07 -0500718
Adam Langleyfcf25832014-12-18 17:42:32 -0800719 /* Read the first 8 bytes. To recognize a ClientHello or V2ClientHello only
720 * needs the first 6 bytes, but 8 is needed to recognize CONNECT below. */
721 ret = ssl3_read_sniff_buffer(s, INITIAL_SNIFF_BUFFER_SIZE);
722 if (ret <= 0) {
723 return ret;
724 }
725 assert(s->s3->sniff_buffer_len >= INITIAL_SNIFF_BUFFER_SIZE);
726 p = (const uint8_t *)s->s3->sniff_buffer->data;
David Benjamin4b755cb2014-12-12 03:58:07 -0500727
Adam Langleyfcf25832014-12-18 17:42:32 -0800728 /* Some dedicated error codes for protocol mixups should the application wish
729 * to interpret them differently. (These do not overlap with ClientHello or
730 * V2ClientHello.) */
731 if (strncmp("GET ", (const char *)p, 4) == 0 ||
732 strncmp("POST ", (const char *)p, 5) == 0 ||
733 strncmp("HEAD ", (const char *)p, 5) == 0 ||
734 strncmp("PUT ", (const char *)p, 4) == 0) {
735 OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_HTTP_REQUEST);
736 return -1;
737 }
738 if (strncmp("CONNECT ", (const char *)p, 8) == 0) {
739 OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_HTTPS_PROXY_REQUEST);
740 return -1;
741 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500742
Adam Langleyfcf25832014-12-18 17:42:32 -0800743 /* Determine if this is a ClientHello or V2ClientHello. */
744 if ((p[0] & 0x80) && p[2] == SSL2_MT_CLIENT_HELLO &&
745 p[3] >= SSL3_VERSION_MAJOR) {
746 /* This is a V2ClientHello. */
747 s->state = SSL3_ST_SR_V2_CLIENT_HELLO;
748 return 1;
749 }
750 if (p[0] == SSL3_RT_HANDSHAKE && p[1] >= SSL3_VERSION_MAJOR &&
751 p[5] == SSL3_MT_CLIENT_HELLO) {
752 /* This is a ClientHello. Initialize the record layer with the already
753 * consumed data and continue the handshake. */
754 if (!ssl3_setup_buffers(s) || !ssl_init_wbio_buffer(s, 1)) {
755 return -1;
756 }
757 assert(s->rstate == SSL_ST_READ_HEADER);
758 memcpy(s->s3->rbuf.buf, p, s->s3->sniff_buffer_len);
759 s->s3->rbuf.offset = 0;
760 s->s3->rbuf.left = s->s3->sniff_buffer_len;
761 s->packet_length = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500762
Adam Langleyfcf25832014-12-18 17:42:32 -0800763 BUF_MEM_free(s->s3->sniff_buffer);
764 s->s3->sniff_buffer = NULL;
765 s->s3->sniff_buffer_len = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500766
Adam Langleyfcf25832014-12-18 17:42:32 -0800767 s->state = SSL3_ST_SR_CLNT_HELLO_A;
768 return 1;
769 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500770
Adam Langleyfcf25832014-12-18 17:42:32 -0800771 OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_UNKNOWN_PROTOCOL);
772 return -1;
773}
David Benjamin4b755cb2014-12-12 03:58:07 -0500774
Adam Langleyfcf25832014-12-18 17:42:32 -0800775int ssl3_get_v2_client_hello(SSL *s) {
776 const uint8_t *p;
777 int ret;
778 CBS v2_client_hello, cipher_specs, session_id, challenge;
779 size_t msg_length, rand_len, len;
780 uint8_t msg_type;
781 uint16_t version, cipher_spec_length, session_id_length, challenge_length;
782 CBB client_hello, hello_body, cipher_suites;
783 uint8_t random[SSL3_RANDOM_SIZE];
David Benjamin4b755cb2014-12-12 03:58:07 -0500784
Adam Langleyfcf25832014-12-18 17:42:32 -0800785 /* Read the remainder of the V2ClientHello. We have previously read 8 bytes
786 * in ssl3_get_initial_bytes. */
787 assert(s->s3->sniff_buffer_len >= INITIAL_SNIFF_BUFFER_SIZE);
788 p = (const uint8_t *)s->s3->sniff_buffer->data;
789 msg_length = ((p[0] & 0x7f) << 8) | p[1];
790 if (msg_length > (1024 * 4)) {
791 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_RECORD_TOO_LARGE);
792 return -1;
793 }
794 if (msg_length < INITIAL_SNIFF_BUFFER_SIZE - 2) {
795 /* Reject lengths that are too short early. We have already read 8 bytes,
796 * so we should not attempt to process an (invalid) V2ClientHello which
797 * would be shorter than that. */
798 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello,
799 SSL_R_RECORD_LENGTH_MISMATCH);
800 return -1;
801 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500802
Adam Langleyfcf25832014-12-18 17:42:32 -0800803 ret = ssl3_read_sniff_buffer(s, msg_length + 2);
804 if (ret <= 0) {
805 return ret;
806 }
807 assert(s->s3->sniff_buffer_len == msg_length + 2);
808 CBS_init(&v2_client_hello, (const uint8_t *)s->s3->sniff_buffer->data + 2,
809 msg_length);
David Benjamin4b755cb2014-12-12 03:58:07 -0500810
Adam Langleyfcf25832014-12-18 17:42:32 -0800811 /* The V2ClientHello without the length is incorporated into the Finished
812 * hash. */
David Benjaminfbdfefb2015-02-16 19:33:53 -0500813 if (!ssl3_finish_mac(s, CBS_data(&v2_client_hello),
814 CBS_len(&v2_client_hello))) {
815 return -1;
816 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800817 if (s->msg_callback) {
818 s->msg_callback(0, SSL2_VERSION, 0, CBS_data(&v2_client_hello),
819 CBS_len(&v2_client_hello), s, s->msg_callback_arg);
820 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500821
Adam Langleyfcf25832014-12-18 17:42:32 -0800822 if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
823 !CBS_get_u16(&v2_client_hello, &version) ||
824 !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
825 !CBS_get_u16(&v2_client_hello, &session_id_length) ||
826 !CBS_get_u16(&v2_client_hello, &challenge_length) ||
827 !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
828 !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
829 !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
830 CBS_len(&v2_client_hello) != 0) {
831 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_DECODE_ERROR);
832 return -1;
833 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500834
Adam Langleyfcf25832014-12-18 17:42:32 -0800835 /* msg_type has already been checked. */
836 assert(msg_type == SSL2_MT_CLIENT_HELLO);
David Benjamin4b755cb2014-12-12 03:58:07 -0500837
Adam Langleyfcf25832014-12-18 17:42:32 -0800838 /* The client_random is the V2ClientHello challenge. Truncate or
839 * left-pad with zeros as needed. */
840 memset(random, 0, SSL3_RANDOM_SIZE);
841 rand_len = CBS_len(&challenge);
842 if (rand_len > SSL3_RANDOM_SIZE) {
843 rand_len = SSL3_RANDOM_SIZE;
844 }
845 memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
846 rand_len);
David Benjamin4b755cb2014-12-12 03:58:07 -0500847
Adam Langleyfcf25832014-12-18 17:42:32 -0800848 /* Write out an equivalent SSLv3 ClientHello. */
849 if (!CBB_init_fixed(&client_hello, (uint8_t *)s->init_buf->data,
850 s->init_buf->max)) {
851 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_MALLOC_FAILURE);
852 return -1;
853 }
854 if (!CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
855 !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
856 !CBB_add_u16(&hello_body, version) ||
857 !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
858 /* No session id. */
859 !CBB_add_u8(&hello_body, 0) ||
860 !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
861 CBB_cleanup(&client_hello);
862 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
863 return -1;
864 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500865
Adam Langleyfcf25832014-12-18 17:42:32 -0800866 /* Copy the cipher suites. */
867 while (CBS_len(&cipher_specs) > 0) {
868 uint32_t cipher_spec;
869 if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
870 CBB_cleanup(&client_hello);
871 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_DECODE_ERROR);
872 return -1;
873 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500874
Adam Langleyfcf25832014-12-18 17:42:32 -0800875 /* Skip SSLv2 ciphers. */
876 if ((cipher_spec & 0xff0000) != 0) {
877 continue;
878 }
879 if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
880 CBB_cleanup(&client_hello);
881 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
882 return -1;
883 }
884 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500885
Adam Langleyfcf25832014-12-18 17:42:32 -0800886 /* Add the null compression scheme and finish. */
887 if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) ||
888 !CBB_finish(&client_hello, NULL, &len)) {
889 CBB_cleanup(&client_hello);
890 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
891 return -1;
892 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500893
Adam Langleyfcf25832014-12-18 17:42:32 -0800894 /* Mark the message for "re"-use by the version-specific method. */
895 s->s3->tmp.reuse_message = 1;
896 s->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO;
897 /* The handshake message header is 4 bytes. */
898 s->s3->tmp.message_size = len - 4;
David Benjamin4b755cb2014-12-12 03:58:07 -0500899
Adam Langleyfcf25832014-12-18 17:42:32 -0800900 /* Initialize the record layer. */
901 if (!ssl3_setup_buffers(s) || !ssl_init_wbio_buffer(s, 1)) {
902 return -1;
903 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500904
Adam Langleyfcf25832014-12-18 17:42:32 -0800905 /* Drop the sniff buffer. */
906 BUF_MEM_free(s->s3->sniff_buffer);
907 s->s3->sniff_buffer = NULL;
908 s->s3->sniff_buffer_len = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500909
Adam Langleyfcf25832014-12-18 17:42:32 -0800910 return 1;
911}
David Benjamin4b755cb2014-12-12 03:58:07 -0500912
Adam Langleyfcf25832014-12-18 17:42:32 -0800913int ssl3_send_hello_request(SSL *s) {
914 if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
David Benjaminfbdfefb2015-02-16 19:33:53 -0500915 if (!ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0)) {
916 return -1;
917 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800918 s->state = SSL3_ST_SW_HELLO_REQ_B;
919 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500920
Adam Langleyfcf25832014-12-18 17:42:32 -0800921 /* SSL3_ST_SW_HELLO_REQ_B */
922 return ssl_do_write(s);
923}
Adam Langley95c29f32014-06-20 12:00:00 -0700924
Adam Langleyfcf25832014-12-18 17:42:32 -0800925int ssl3_get_client_hello(SSL *s) {
926 int i, ok, al = SSL_AD_INTERNAL_ERROR, ret = -1;
927 long n;
928 const SSL_CIPHER *c;
929 STACK_OF(SSL_CIPHER) *ciphers = NULL;
930 struct ssl_early_callback_ctx early_ctx;
931 CBS client_hello;
932 uint16_t client_version;
933 CBS client_random, session_id, cipher_suites, compression_methods;
Adam Langley95c29f32014-06-20 12:00:00 -0700934
Adam Langleyfcf25832014-12-18 17:42:32 -0800935 /* We do this so that we will respond with our native type. If we are TLSv1
936 * and we get SSLv3, we will respond with TLSv1, This down switching should
937 * be handled by a different method. If we are SSLv3, we will respond with
938 * SSLv3, even if prompted with TLSv1. */
939 switch (s->state) {
940 case SSL3_ST_SR_CLNT_HELLO_A:
941 case SSL3_ST_SR_CLNT_HELLO_B:
942 n = s->method->ssl_get_message(
943 s, SSL3_ST_SR_CLNT_HELLO_A, SSL3_ST_SR_CLNT_HELLO_B,
944 SSL3_MT_CLIENT_HELLO, SSL3_RT_MAX_PLAIN_LENGTH,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500945 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700946
Adam Langleyfcf25832014-12-18 17:42:32 -0800947 if (!ok) {
948 return n;
949 }
Adam Langley95c29f32014-06-20 12:00:00 -0700950
Adam Langleyfcf25832014-12-18 17:42:32 -0800951 s->state = SSL3_ST_SR_CLNT_HELLO_C;
952 /* fallthrough */
953 case SSL3_ST_SR_CLNT_HELLO_C:
954 case SSL3_ST_SR_CLNT_HELLO_D:
955 /* We have previously parsed the ClientHello message, and can't call
956 * ssl_get_message again without hashing the message into the Finished
957 * digest again. */
958 n = s->init_num;
Adam Langleydc9b1412014-06-20 12:00:00 -0700959
Adam Langleyfcf25832014-12-18 17:42:32 -0800960 memset(&early_ctx, 0, sizeof(early_ctx));
961 early_ctx.ssl = s;
962 early_ctx.client_hello = s->init_msg;
963 early_ctx.client_hello_len = n;
964 if (!ssl_early_callback_init(&early_ctx)) {
965 al = SSL_AD_DECODE_ERROR;
966 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
967 SSL_R_CLIENTHELLO_PARSE_FAILED);
968 goto f_err;
969 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400970
Adam Langleyfcf25832014-12-18 17:42:32 -0800971 if (s->state == SSL3_ST_SR_CLNT_HELLO_C &&
972 s->ctx->select_certificate_cb != NULL) {
973 s->state = SSL3_ST_SR_CLNT_HELLO_D;
974 switch (s->ctx->select_certificate_cb(&early_ctx)) {
975 case 0:
David Benjamin2fff5bf2015-02-09 04:32:28 -0500976 s->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
977 goto err;
Adam Langleydc9b1412014-06-20 12:00:00 -0700978
Adam Langleyfcf25832014-12-18 17:42:32 -0800979 case -1:
980 /* Connection rejected. */
981 al = SSL_AD_ACCESS_DENIED;
982 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
983 SSL_R_CONNECTION_REJECTED);
984 goto f_err;
Adam Langleydc9b1412014-06-20 12:00:00 -0700985
Adam Langleyfcf25832014-12-18 17:42:32 -0800986 default:
987 /* fallthrough */;
988 }
989 }
990 s->state = SSL3_ST_SR_CLNT_HELLO_D;
991 break;
Adam Langleyb2ce0582014-06-20 12:00:00 -0700992
Adam Langleyfcf25832014-12-18 17:42:32 -0800993 default:
994 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNKNOWN_STATE);
995 return -1;
996 }
Adam Langley95c29f32014-06-20 12:00:00 -0700997
Adam Langleyfcf25832014-12-18 17:42:32 -0800998 CBS_init(&client_hello, s->init_msg, n);
999 if (!CBS_get_u16(&client_hello, &client_version) ||
1000 !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) ||
1001 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
1002 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1003 al = SSL_AD_DECODE_ERROR;
1004 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
1005 goto f_err;
1006 }
Adam Langley95c29f32014-06-20 12:00:00 -07001007
Adam Langleyfcf25832014-12-18 17:42:32 -08001008 /* use version from inside client hello, not from record header (may differ:
1009 * see RFC 2246, Appendix E, second paragraph) */
1010 s->client_version = client_version;
Adam Langley95c29f32014-06-20 12:00:00 -07001011
Adam Langleyfcf25832014-12-18 17:42:32 -08001012 /* Load the client random. */
1013 memcpy(s->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -07001014
Adam Langleyfcf25832014-12-18 17:42:32 -08001015 if (SSL_IS_DTLS(s)) {
1016 CBS cookie;
Adam Langley95c29f32014-06-20 12:00:00 -07001017
Adam Langleyfcf25832014-12-18 17:42:32 -08001018 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
1019 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
1020 al = SSL_AD_DECODE_ERROR;
1021 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
1022 goto f_err;
1023 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001025
Adam Langleyfcf25832014-12-18 17:42:32 -08001026 if (!s->s3->have_version) {
1027 /* Select version to use */
1028 uint16_t version = ssl3_get_mutual_version(s, client_version);
1029 if (version == 0) {
1030 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL);
1031 s->version = s->client_version;
1032 al = SSL_AD_PROTOCOL_VERSION;
1033 goto f_err;
1034 }
1035 s->version = version;
1036 s->enc_method = ssl3_get_enc_method(version);
1037 assert(s->enc_method != NULL);
1038 /* At this point, the connection's version is known and |s->version| is
1039 * fixed. Begin enforcing the record-layer version. */
1040 s->s3->have_version = 1;
1041 } else if (SSL_IS_DTLS(s) ? (s->client_version > s->version)
1042 : (s->client_version < s->version)) {
1043 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER);
1044 al = SSL_AD_PROTOCOL_VERSION;
1045 goto f_err;
1046 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001047
Adam Langleyfcf25832014-12-18 17:42:32 -08001048 s->hit = 0;
1049 /* Versions before 0.9.7 always allow clients to resume sessions in
1050 * renegotiation. 0.9.7 and later allow this by default, but optionally
1051 * ignore resumption requests with flag
1052 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather than
1053 * a change to default behavior so that applications relying on this for
1054 * security won't even compile against older library versions).
1055 *
1056 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1057 * request renegotiation but not a new session (s->new_session remains
1058 * unset): for servers, this essentially just means that the
1059 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be ignored. */
1060 if (s->new_session &&
1061 (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)) {
1062 if (!ssl_get_new_session(s, 1)) {
1063 goto err;
1064 }
1065 } else {
1066 i = ssl_get_prev_session(s, &early_ctx);
1067 if (i == PENDING_SESSION) {
David Benjamin2fff5bf2015-02-09 04:32:28 -05001068 s->rwstate = SSL_PENDING_SESSION;
Adam Langleyfcf25832014-12-18 17:42:32 -08001069 goto err;
1070 } else if (i == -1) {
1071 goto err;
1072 }
Adam Langley95c29f32014-06-20 12:00:00 -07001073
Adam Langleyfcf25832014-12-18 17:42:32 -08001074 /* Only resume if the session's version matches the negotiated version:
1075 * most clients do not accept a mismatch. */
1076 if (i == 1 && s->version == s->session->ssl_version) {
1077 s->hit = 1;
1078 } else {
1079 /* No session was found or it was unacceptable. */
1080 if (!ssl_get_new_session(s, 1)) {
1081 goto err;
1082 }
1083 }
1084 }
David Benjamin22f9bcc2014-07-13 12:29:21 -04001085
Adam Langley524e7172015-02-20 16:04:00 -08001086 if (s->ctx->dos_protection_cb != NULL && s->ctx->dos_protection_cb(&early_ctx) == 0) {
1087 /* Connection rejected for DOS reasons. */
1088 al = SSL_AD_ACCESS_DENIED;
1089 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CONNECTION_REJECTED);
1090 goto f_err;
1091 }
1092
Adam Langleyfcf25832014-12-18 17:42:32 -08001093 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamin93de5e52015-04-17 22:33:25 -04001094 CBS_len(&cipher_suites) == 0 ||
1095 CBS_len(&cipher_suites) % 2 != 0 ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
1097 CBS_len(&compression_methods) == 0) {
1098 al = SSL_AD_DECODE_ERROR;
1099 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
1100 goto f_err;
1101 }
David Benjamin22f9bcc2014-07-13 12:29:21 -04001102
Adam Langleyfcf25832014-12-18 17:42:32 -08001103 ciphers = ssl_bytes_to_cipher_list(s, &cipher_suites);
1104 if (ciphers == NULL) {
1105 goto err;
1106 }
Adam Langley95c29f32014-06-20 12:00:00 -07001107
Adam Langleyfcf25832014-12-18 17:42:32 -08001108 /* If it is a hit, check that the cipher is in the list. */
David Benjamin93de5e52015-04-17 22:33:25 -04001109 if (s->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001110 size_t j;
1111 int found_cipher = 0;
David Benjamin3fa27772015-04-17 22:32:19 -04001112 uint32_t id = s->session->cipher->id;
Adam Langley95c29f32014-06-20 12:00:00 -07001113
Adam Langleyfcf25832014-12-18 17:42:32 -08001114 for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++) {
1115 c = sk_SSL_CIPHER_value(ciphers, j);
1116 if (c->id == id) {
1117 found_cipher = 1;
1118 break;
1119 }
1120 }
Adam Langley95c29f32014-06-20 12:00:00 -07001121
Adam Langleyfcf25832014-12-18 17:42:32 -08001122 if (!found_cipher) {
1123 /* we need to have the cipher in the cipher list if we are asked to reuse
1124 * it */
1125 al = SSL_AD_ILLEGAL_PARAMETER;
1126 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
1127 SSL_R_REQUIRED_CIPHER_MISSING);
1128 goto f_err;
1129 }
1130 }
Adam Langley95c29f32014-06-20 12:00:00 -07001131
Adam Langleyfcf25832014-12-18 17:42:32 -08001132 /* Only null compression is supported. */
1133 if (memchr(CBS_data(&compression_methods), 0,
1134 CBS_len(&compression_methods)) == NULL) {
1135 al = SSL_AD_ILLEGAL_PARAMETER;
1136 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
1137 SSL_R_NO_COMPRESSION_SPECIFIED);
1138 goto f_err;
1139 }
David Benjamindc72ff72014-06-25 12:36:10 -04001140
Adam Langleyfcf25832014-12-18 17:42:32 -08001141 /* TLS extensions. */
1142 if (s->version >= SSL3_VERSION &&
1143 !ssl_parse_clienthello_tlsext(s, &client_hello)) {
1144 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_PARSE_TLSEXT);
1145 goto err;
1146 }
Adam Langley95c29f32014-06-20 12:00:00 -07001147
Adam Langleyfcf25832014-12-18 17:42:32 -08001148 /* There should be nothing left over in the record. */
1149 if (CBS_len(&client_hello) != 0) {
1150 /* wrong packet length */
1151 al = SSL_AD_DECODE_ERROR;
1152 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_BAD_PACKET_LENGTH);
1153 goto f_err;
1154 }
Adam Langley95c29f32014-06-20 12:00:00 -07001155
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 /* Given ciphers and SSL_get_ciphers, we must pick a cipher */
1157 if (!s->hit) {
1158 if (ciphers == NULL) {
1159 al = SSL_AD_ILLEGAL_PARAMETER;
1160 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_PASSED);
1161 goto f_err;
1162 }
Adam Langley95c29f32014-06-20 12:00:00 -07001163
Adam Langleyfcf25832014-12-18 17:42:32 -08001164 /* Let cert callback update server certificates if required */
1165 if (s->cert->cert_cb) {
1166 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1167 if (rv == 0) {
1168 al = SSL_AD_INTERNAL_ERROR;
1169 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CERT_CB_ERROR);
1170 goto f_err;
1171 }
1172 if (rv < 0) {
1173 s->rwstate = SSL_X509_LOOKUP;
1174 goto err;
1175 }
1176 s->rwstate = SSL_NOTHING;
1177 }
1178 c = ssl3_choose_cipher(s, ciphers, ssl_get_cipher_preferences(s));
1179
1180 if (c == NULL) {
1181 al = SSL_AD_HANDSHAKE_FAILURE;
1182 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER);
1183 goto f_err;
1184 }
1185 s->s3->tmp.new_cipher = c;
1186 } else {
1187 /* Session-id reuse */
1188 s->s3->tmp.new_cipher = s->session->cipher;
1189 }
1190
1191 if ((!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) &&
1192 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1193 goto f_err;
1194 }
1195
1196 /* we now have the following setup;
1197 * client_random
1198 * cipher_list - our prefered list of ciphers
1199 * ciphers - the clients prefered list of ciphers
1200 * compression - basically ignored right now
1201 * ssl version is set - sslv3
1202 * s->session - The ssl session has been setup.
1203 * s->hit - session reuse flag
1204 * s->tmp.new_cipher - the new cipher to use. */
1205
1206 if (ret < 0) {
1207 ret = -ret;
1208 }
1209
1210 if (0) {
1211 f_err:
1212 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1213 }
1214
Adam Langley95c29f32014-06-20 12:00:00 -07001215err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001216 sk_SSL_CIPHER_free(ciphers);
Adam Langleyfcf25832014-12-18 17:42:32 -08001217 return ret;
1218}
Adam Langley95c29f32014-06-20 12:00:00 -07001219
Adam Langleyfcf25832014-12-18 17:42:32 -08001220int ssl3_send_server_hello(SSL *s) {
1221 uint8_t *buf;
1222 uint8_t *p, *d;
1223 int sl;
1224 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001225
Adam Langleyfcf25832014-12-18 17:42:32 -08001226 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1227 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1228 * known attack while we fix ChannelID itself. */
1229 if (s->s3->tlsext_channel_id_valid &&
David Benjamin7061e282015-03-19 11:10:48 -04001230 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001231 s->s3->tlsext_channel_id_valid = 0;
1232 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001233
Adam Langleyfcf25832014-12-18 17:42:32 -08001234 /* If this is a resumption and the original handshake didn't support
1235 * ChannelID then we didn't record the original handshake hashes in the
1236 * session and so cannot resume with ChannelIDs. */
1237 if (s->hit && s->s3->tlsext_channel_id_new &&
1238 s->session->original_handshake_hash_len == 0) {
1239 s->s3->tlsext_channel_id_valid = 0;
1240 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001241
Adam Langleyfcf25832014-12-18 17:42:32 -08001242 buf = (uint8_t *)s->init_buf->data;
1243 /* Do the message type and length last */
1244 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001245
Adam Langleyfcf25832014-12-18 17:42:32 -08001246 *(p++) = s->version >> 8;
1247 *(p++) = s->version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -07001248
Adam Langleyfcf25832014-12-18 17:42:32 -08001249 /* Random stuff */
1250 if (!ssl_fill_hello_random(s, 1, s->s3->server_random, SSL3_RANDOM_SIZE)) {
1251 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1252 return -1;
1253 }
1254 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1255 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -07001256
Adam Langleyfcf25832014-12-18 17:42:32 -08001257 /* There are several cases for the session ID to send
1258 * back in the server hello:
1259 * - For session reuse from the session cache, we send back the old session
1260 * ID.
1261 * - If stateless session reuse (using a session ticket) is successful, we
1262 * send back the client's "session ID" (which doesn't actually identify
1263 * the session).
1264 * - If it is a new session, we send back the new session ID.
1265 * - However, if we want the new session to be single-use, we send back a
1266 * 0-length session ID.
1267 * s->hit is non-zero in either case of session reuse, so the following
1268 * won't overwrite an ID that we're supposed to send back. */
1269 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) && !s->hit) {
1270 s->session->session_id_length = 0;
1271 }
Adam Langley95c29f32014-06-20 12:00:00 -07001272
Adam Langleyfcf25832014-12-18 17:42:32 -08001273 sl = s->session->session_id_length;
1274 if (sl > (int)sizeof(s->session->session_id)) {
1275 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1276 return -1;
1277 }
1278 *(p++) = sl;
1279 memcpy(p, s->session->session_id, sl);
1280 p += sl;
Adam Langley95c29f32014-06-20 12:00:00 -07001281
Adam Langleyfcf25832014-12-18 17:42:32 -08001282 /* put the cipher */
1283 s2n(ssl3_get_cipher_value(s->s3->tmp.new_cipher), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001284
Adam Langleyfcf25832014-12-18 17:42:32 -08001285 /* put the compression method */
1286 *(p++) = 0;
1287 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1288 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, SSL_R_SERVERHELLO_TLSEXT);
1289 return -1;
1290 }
1291 p = ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH);
1292 if (p == NULL) {
1293 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1294 return -1;
1295 }
Adam Langley95c29f32014-06-20 12:00:00 -07001296
Adam Langleyfcf25832014-12-18 17:42:32 -08001297 /* do the header */
1298 l = (p - d);
David Benjaminfbdfefb2015-02-16 19:33:53 -05001299 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l)) {
1300 return -1;
1301 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001302 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1303 }
Adam Langley95c29f32014-06-20 12:00:00 -07001304
Adam Langleyfcf25832014-12-18 17:42:32 -08001305 /* SSL3_ST_SW_SRVR_HELLO_B */
1306 return ssl_do_write(s);
1307}
Adam Langley95c29f32014-06-20 12:00:00 -07001308
Adam Langleyfcf25832014-12-18 17:42:32 -08001309int ssl3_send_server_done(SSL *s) {
1310 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
David Benjaminfbdfefb2015-02-16 19:33:53 -05001311 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0)) {
1312 return -1;
1313 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001314 s->state = SSL3_ST_SW_SRVR_DONE_B;
1315 }
Adam Langley95c29f32014-06-20 12:00:00 -07001316
Adam Langleyfcf25832014-12-18 17:42:32 -08001317 /* SSL3_ST_SW_SRVR_DONE_B */
1318 return ssl_do_write(s);
1319}
Adam Langley95c29f32014-06-20 12:00:00 -07001320
Adam Langleyfcf25832014-12-18 17:42:32 -08001321int ssl3_send_server_key_exchange(SSL *s) {
1322 DH *dh = NULL, *dhp;
David Benjaminc0f763b2015-03-27 02:05:39 -04001323 EC_KEY *ecdh = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001324 uint8_t *encodedPoint = NULL;
1325 int encodedlen = 0;
David Benjamin70bd80a2014-12-27 03:06:46 -05001326 uint16_t curve_id = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001327 BN_CTX *bn_ctx = NULL;
1328 const char *psk_identity_hint = NULL;
1329 size_t psk_identity_hint_len = 0;
1330 EVP_PKEY *pkey;
1331 uint8_t *p, *d;
1332 int al, i;
David Benjamin3fa27772015-04-17 22:32:19 -04001333 uint32_t alg_k;
1334 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001335 int n;
1336 CERT *cert;
1337 BIGNUM *r[4];
1338 int nr[4], kn;
1339 BUF_MEM *buf;
1340 EVP_MD_CTX md_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07001341
Adam Langleyfcf25832014-12-18 17:42:32 -08001342 EVP_MD_CTX_init(&md_ctx);
1343 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1344 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1345 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1346 cert = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07001347
Adam Langleyfcf25832014-12-18 17:42:32 -08001348 buf = s->init_buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001349
Adam Langleyfcf25832014-12-18 17:42:32 -08001350 r[0] = r[1] = r[2] = r[3] = NULL;
1351 n = 0;
1352 if (alg_a & SSL_aPSK) {
1353 /* size for PSK identity hint */
1354 psk_identity_hint = s->psk_identity_hint;
1355 if (psk_identity_hint) {
1356 psk_identity_hint_len = strlen(psk_identity_hint);
1357 } else {
1358 psk_identity_hint_len = 0;
1359 }
1360 n += 2 + psk_identity_hint_len;
1361 }
Adam Langley95c29f32014-06-20 12:00:00 -07001362
David Benjamin7061e282015-03-19 11:10:48 -04001363 if (alg_k & SSL_kDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001364 dhp = cert->dh_tmp;
1365 if (dhp == NULL && s->cert->dh_tmp_cb != NULL) {
1366 dhp = s->cert->dh_tmp_cb(s, 0, 1024);
1367 }
1368 if (dhp == NULL) {
1369 al = SSL_AD_HANDSHAKE_FAILURE;
1370 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1371 SSL_R_MISSING_TMP_DH_KEY);
1372 goto f_err;
1373 }
Adam Langley95c29f32014-06-20 12:00:00 -07001374
Adam Langleyfcf25832014-12-18 17:42:32 -08001375 if (s->s3->tmp.dh != NULL) {
1376 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1377 ERR_R_INTERNAL_ERROR);
1378 goto err;
1379 }
Adam Langley95c29f32014-06-20 12:00:00 -07001380
Adam Langleyfcf25832014-12-18 17:42:32 -08001381 dh = DHparams_dup(dhp);
1382 if (dh == NULL) {
1383 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1384 goto err;
1385 }
Adam Langley95c29f32014-06-20 12:00:00 -07001386
Adam Langleyfcf25832014-12-18 17:42:32 -08001387 s->s3->tmp.dh = dh;
1388 if (dhp->pub_key == NULL || dhp->priv_key == NULL ||
1389 (s->options & SSL_OP_SINGLE_DH_USE)) {
1390 if (!DH_generate_key(dh)) {
1391 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1392 goto err;
1393 }
1394 } else {
1395 dh->pub_key = BN_dup(dhp->pub_key);
1396 dh->priv_key = BN_dup(dhp->priv_key);
1397 if (dh->pub_key == NULL || dh->priv_key == NULL) {
1398 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1399 goto err;
1400 }
1401 }
Adam Langley95c29f32014-06-20 12:00:00 -07001402
Adam Langleyfcf25832014-12-18 17:42:32 -08001403 r[0] = dh->p;
1404 r[1] = dh->g;
1405 r[2] = dh->pub_key;
David Benjamin7061e282015-03-19 11:10:48 -04001406 } else if (alg_k & SSL_kECDHE) {
David Benjaminc0f763b2015-03-27 02:05:39 -04001407 /* Determine the curve to use. */
1408 int nid = NID_undef;
David Benjamindd978782015-04-24 15:20:13 -04001409 if (cert->ecdh_nid != NID_undef) {
David Benjaminc0f763b2015-03-27 02:05:39 -04001410 nid = cert->ecdh_nid;
1411 } else if (cert->ecdh_tmp_cb != NULL) {
1412 /* Note: |ecdh_tmp_cb| does NOT pass ownership of the result
1413 * to the caller. */
1414 EC_KEY *template = s->cert->ecdh_tmp_cb(s, 0, 1024);
1415 if (template != NULL && EC_KEY_get0_group(template) != NULL) {
1416 nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(template));
Adam Langleyfcf25832014-12-18 17:42:32 -08001417 }
David Benjamindd978782015-04-24 15:20:13 -04001418 } else {
1419 nid = tls1_get_shared_curve(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08001420 }
David Benjaminc0f763b2015-03-27 02:05:39 -04001421 if (nid == NID_undef) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001422 al = SSL_AD_HANDSHAKE_FAILURE;
1423 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1424 SSL_R_MISSING_TMP_ECDH_KEY);
1425 goto f_err;
1426 }
Adam Langley95c29f32014-06-20 12:00:00 -07001427
Adam Langleyfcf25832014-12-18 17:42:32 -08001428 if (s->s3->tmp.ecdh != NULL) {
1429 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1430 ERR_R_INTERNAL_ERROR);
1431 goto err;
1432 }
David Benjaminc0f763b2015-03-27 02:05:39 -04001433 ecdh = EC_KEY_new_by_curve_name(nid);
1434 if (ecdh == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001435 goto err;
1436 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001437 s->s3->tmp.ecdh = ecdh;
Adam Langley95c29f32014-06-20 12:00:00 -07001438
David Benjaminc0f763b2015-03-27 02:05:39 -04001439 if (!EC_KEY_generate_key(ecdh)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001440 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1441 goto err;
1442 }
Adam Langley95c29f32014-06-20 12:00:00 -07001443
David Benjamin70bd80a2014-12-27 03:06:46 -05001444 /* We only support ephemeral ECDH keys over named (not generic) curves. */
David Benjaminc0f763b2015-03-27 02:05:39 -04001445 const EC_GROUP *group = EC_KEY_get0_group(ecdh);
David Benjamin70bd80a2014-12-27 03:06:46 -05001446 if (!tls1_ec_nid2curve_id(&curve_id, EC_GROUP_get_curve_name(group))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001447 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1448 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1449 goto err;
1450 }
Adam Langley95c29f32014-06-20 12:00:00 -07001451
Adam Langleyfcf25832014-12-18 17:42:32 -08001452 /* Encode the public key. First check the size of encoding and allocate
1453 * memory accordingly. */
1454 encodedlen =
1455 EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
1456 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001457
Adam Langleyfcf25832014-12-18 17:42:32 -08001458 encodedPoint = (uint8_t *)OPENSSL_malloc(encodedlen * sizeof(uint8_t));
1459 bn_ctx = BN_CTX_new();
1460 if (encodedPoint == NULL || bn_ctx == NULL) {
1461 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1462 ERR_R_MALLOC_FAILURE);
1463 goto err;
1464 }
Adam Langley95c29f32014-06-20 12:00:00 -07001465
Adam Langleyfcf25832014-12-18 17:42:32 -08001466 encodedlen = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
1467 POINT_CONVERSION_UNCOMPRESSED,
1468 encodedPoint, encodedlen, bn_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07001469
Adam Langleyfcf25832014-12-18 17:42:32 -08001470 if (encodedlen == 0) {
1471 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1472 goto err;
1473 }
Adam Langley95c29f32014-06-20 12:00:00 -07001474
Adam Langleyfcf25832014-12-18 17:42:32 -08001475 BN_CTX_free(bn_ctx);
1476 bn_ctx = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001477
Adam Langleyfcf25832014-12-18 17:42:32 -08001478 /* We only support named (not generic) curves in ECDH ephemeral key
1479 * exchanges. In this situation, we need four additional bytes to encode
1480 * the entire ServerECDHParams structure. */
1481 n += 4 + encodedlen;
Adam Langley95c29f32014-06-20 12:00:00 -07001482
Adam Langleyfcf25832014-12-18 17:42:32 -08001483 /* We'll generate the serverKeyExchange message explicitly so we can set
1484 * these to NULLs */
1485 r[0] = NULL;
1486 r[1] = NULL;
1487 r[2] = NULL;
1488 r[3] = NULL;
1489 } else if (!(alg_k & SSL_kPSK)) {
1490 al = SSL_AD_HANDSHAKE_FAILURE;
1491 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1492 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1493 goto f_err;
1494 }
Adam Langley95c29f32014-06-20 12:00:00 -07001495
Adam Langleyfcf25832014-12-18 17:42:32 -08001496 for (i = 0; i < 4 && r[i] != NULL; i++) {
1497 nr[i] = BN_num_bytes(r[i]);
1498 n += 2 + nr[i];
1499 }
Adam Langley95c29f32014-06-20 12:00:00 -07001500
Adam Langleyfcf25832014-12-18 17:42:32 -08001501 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1502 pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher);
1503 if (pkey == NULL) {
1504 al = SSL_AD_DECODE_ERROR;
1505 goto f_err;
1506 }
1507 kn = EVP_PKEY_size(pkey);
1508 } else {
1509 pkey = NULL;
1510 kn = 0;
1511 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001512
Adam Langleyfcf25832014-12-18 17:42:32 -08001513 if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) {
1514 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_BUF);
1515 goto err;
1516 }
1517 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001518
Adam Langleyfcf25832014-12-18 17:42:32 -08001519 for (i = 0; i < 4 && r[i] != NULL; i++) {
1520 s2n(nr[i], p);
1521 BN_bn2bin(r[i], p);
1522 p += nr[i];
1523 }
David Benjamin00505ec2014-11-26 16:38:00 -05001524
David Benjamin7061e282015-03-19 11:10:48 -04001525 /* Note: ECDHE PSK ciphersuites use SSL_kECDHE and SSL_aPSK. When one of
Adam Langleyfcf25832014-12-18 17:42:32 -08001526 * them is used, the server key exchange record needs to have both the
1527 * psk_identity_hint and the ServerECDHParams. */
1528 if (alg_a & SSL_aPSK) {
1529 /* copy PSK identity hint (if provided) */
1530 s2n(psk_identity_hint_len, p);
1531 if (psk_identity_hint_len > 0) {
1532 memcpy(p, psk_identity_hint, psk_identity_hint_len);
1533 p += psk_identity_hint_len;
1534 }
1535 }
David Benjamin00505ec2014-11-26 16:38:00 -05001536
David Benjamin7061e282015-03-19 11:10:48 -04001537 if (alg_k & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001538 /* We only support named (not generic) curves. In this situation, the
1539 * serverKeyExchange message has:
1540 * [1 byte CurveType], [2 byte CurveName]
1541 * [1 byte length of encoded point], followed by
1542 * the actual encoded point itself. */
David Benjamin70bd80a2014-12-27 03:06:46 -05001543 *(p++) = NAMED_CURVE_TYPE;
1544 *(p++) = (uint8_t)(curve_id >> 8);
1545 *(p++) = (uint8_t)(curve_id & 0xff);
1546 *(p++) = encodedlen;
1547 memcpy(p, encodedPoint, encodedlen);
1548 p += encodedlen;
Adam Langleyfcf25832014-12-18 17:42:32 -08001549 OPENSSL_free(encodedPoint);
1550 encodedPoint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001551 }
Adam Langley95c29f32014-06-20 12:00:00 -07001552
Adam Langleyfcf25832014-12-18 17:42:32 -08001553 /* not anonymous */
1554 if (pkey != NULL) {
1555 /* n is the length of the params, they start at &(d[4]) and p points to
1556 * the space at the end. */
1557 const EVP_MD *md;
1558 size_t sig_len = EVP_PKEY_size(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001559
Adam Langleyfcf25832014-12-18 17:42:32 -08001560 /* Determine signature algorithm. */
1561 if (SSL_USE_SIGALGS(s)) {
1562 md = tls1_choose_signing_digest(s, pkey);
1563 if (!tls12_get_sigandhash(p, pkey, md)) {
1564 /* Should never happen */
1565 al = SSL_AD_INTERNAL_ERROR;
1566 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1567 ERR_R_INTERNAL_ERROR);
1568 goto f_err;
1569 }
1570 p += 2;
1571 } else if (pkey->type == EVP_PKEY_RSA) {
1572 md = EVP_md5_sha1();
1573 } else {
1574 md = EVP_sha1();
1575 }
1576
1577 if (!EVP_DigestSignInit(&md_ctx, NULL, md, NULL, pkey) ||
1578 !EVP_DigestSignUpdate(&md_ctx, s->s3->client_random,
1579 SSL3_RANDOM_SIZE) ||
1580 !EVP_DigestSignUpdate(&md_ctx, s->s3->server_random,
1581 SSL3_RANDOM_SIZE) ||
1582 !EVP_DigestSignUpdate(&md_ctx, d, n) ||
1583 !EVP_DigestSignFinal(&md_ctx, &p[2], &sig_len)) {
1584 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_EVP);
1585 goto err;
1586 }
1587
1588 s2n(sig_len, p);
1589 n += sig_len + 2;
1590 if (SSL_USE_SIGALGS(s)) {
1591 n += 2;
1592 }
1593 }
1594
David Benjaminfbdfefb2015-02-16 19:33:53 -05001595 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n)) {
1596 goto err;
1597 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001598 }
1599
1600 s->state = SSL3_ST_SW_KEY_EXCH_B;
1601 EVP_MD_CTX_cleanup(&md_ctx);
1602 return ssl_do_write(s);
1603
Adam Langley95c29f32014-06-20 12:00:00 -07001604f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001605 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001606err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001607 OPENSSL_free(encodedPoint);
Adam Langleyfcf25832014-12-18 17:42:32 -08001608 BN_CTX_free(bn_ctx);
1609 EVP_MD_CTX_cleanup(&md_ctx);
1610 return -1;
1611}
Adam Langley95c29f32014-06-20 12:00:00 -07001612
Adam Langleyfcf25832014-12-18 17:42:32 -08001613int ssl3_send_certificate_request(SSL *s) {
1614 uint8_t *p, *d;
1615 size_t i;
1616 int j, nl, off, n;
1617 STACK_OF(X509_NAME) *sk = NULL;
1618 X509_NAME *name;
1619 BUF_MEM *buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001620
Adam Langleyfcf25832014-12-18 17:42:32 -08001621 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
1622 buf = s->init_buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001623
Adam Langleyfcf25832014-12-18 17:42:32 -08001624 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001625
Adam Langleyfcf25832014-12-18 17:42:32 -08001626 /* get the list of acceptable cert types */
1627 p++;
1628 n = ssl3_get_req_cert_type(s, p);
1629 d[0] = n;
1630 p += n;
1631 n++;
Adam Langley95c29f32014-06-20 12:00:00 -07001632
Adam Langleyfcf25832014-12-18 17:42:32 -08001633 if (SSL_USE_SIGALGS(s)) {
1634 const uint8_t *psigs;
1635 nl = tls12_get_psigalgs(s, &psigs);
1636 s2n(nl, p);
1637 memcpy(p, psigs, nl);
1638 p += nl;
1639 n += nl + 2;
1640 }
Adam Langley95c29f32014-06-20 12:00:00 -07001641
Adam Langleyfcf25832014-12-18 17:42:32 -08001642 off = n;
1643 p += 2;
1644 n += 2;
Adam Langley95c29f32014-06-20 12:00:00 -07001645
Adam Langleyfcf25832014-12-18 17:42:32 -08001646 sk = SSL_get_client_CA_list(s);
1647 nl = 0;
1648 if (sk != NULL) {
1649 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1650 name = sk_X509_NAME_value(sk, i);
1651 j = i2d_X509_NAME(name, NULL);
1652 if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
1653 OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB);
1654 goto err;
1655 }
1656 p = ssl_handshake_start(s) + n;
1657 s2n(j, p);
1658 i2d_X509_NAME(name, &p);
1659 n += 2 + j;
1660 nl += 2 + j;
1661 }
1662 }
Adam Langley95c29f32014-06-20 12:00:00 -07001663
Adam Langleyfcf25832014-12-18 17:42:32 -08001664 /* else no CA names */
1665 p = ssl_handshake_start(s) + off;
1666 s2n(nl, p);
1667
David Benjaminfbdfefb2015-02-16 19:33:53 -05001668 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n)) {
1669 goto err;
1670 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001671 s->state = SSL3_ST_SW_CERT_REQ_B;
1672 }
Adam Langley95c29f32014-06-20 12:00:00 -07001673
Adam Langleyfcf25832014-12-18 17:42:32 -08001674 /* SSL3_ST_SW_CERT_REQ_B */
1675 return ssl_do_write(s);
1676
Adam Langley95c29f32014-06-20 12:00:00 -07001677err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001678 return -1;
1679}
Adam Langley95c29f32014-06-20 12:00:00 -07001680
Adam Langleyfcf25832014-12-18 17:42:32 -08001681int ssl3_get_client_key_exchange(SSL *s) {
1682 int al, ok;
1683 long n;
1684 CBS client_key_exchange;
David Benjamin3fa27772015-04-17 22:32:19 -04001685 uint32_t alg_k;
1686 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001687 uint8_t *premaster_secret = NULL;
1688 size_t premaster_secret_len = 0;
1689 RSA *rsa = NULL;
1690 uint8_t *decrypt_buf = NULL;
1691 EVP_PKEY *pkey = NULL;
1692 BIGNUM *pub = NULL;
1693 DH *dh_srvr;
Adam Langley95c29f32014-06-20 12:00:00 -07001694
Adam Langleyfcf25832014-12-18 17:42:32 -08001695 EC_KEY *srvr_ecdh = NULL;
1696 EVP_PKEY *clnt_pub_pkey = NULL;
1697 EC_POINT *clnt_ecpoint = NULL;
1698 BN_CTX *bn_ctx = NULL;
1699 unsigned int psk_len = 0;
1700 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001701
Adam Langleyfcf25832014-12-18 17:42:32 -08001702 n = s->method->ssl_get_message(s, SSL3_ST_SR_KEY_EXCH_A,
1703 SSL3_ST_SR_KEY_EXCH_B,
1704 SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, /* ??? */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001705 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001706
Adam Langleyfcf25832014-12-18 17:42:32 -08001707 if (!ok) {
1708 return n;
1709 }
Adam Langley95c29f32014-06-20 12:00:00 -07001710
Adam Langleyfcf25832014-12-18 17:42:32 -08001711 CBS_init(&client_key_exchange, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001712
Adam Langleyfcf25832014-12-18 17:42:32 -08001713 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1714 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001715
Adam Langleyfcf25832014-12-18 17:42:32 -08001716 /* If using a PSK key exchange, prepare the pre-shared key. */
1717 if (alg_a & SSL_aPSK) {
1718 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001719
Adam Langleyfcf25832014-12-18 17:42:32 -08001720 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1721 * then this is the only field in the message. */
1722 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1723 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
1724 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR);
1725 al = SSL_AD_DECODE_ERROR;
1726 goto f_err;
1727 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001728
Adam Langleyfcf25832014-12-18 17:42:32 -08001729 if (s->psk_server_callback == NULL) {
1730 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1731 SSL_R_PSK_NO_SERVER_CB);
1732 al = SSL_AD_INTERNAL_ERROR;
1733 goto f_err;
1734 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001735
Adam Langleyfcf25832014-12-18 17:42:32 -08001736 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1737 CBS_contains_zero_byte(&psk_identity)) {
1738 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1739 SSL_R_DATA_LENGTH_TOO_LONG);
1740 al = SSL_AD_ILLEGAL_PARAMETER;
1741 goto f_err;
1742 }
David Benjamin35c02302014-07-13 04:14:59 -04001743
Adam Langleyfcf25832014-12-18 17:42:32 -08001744 if (!CBS_strdup(&psk_identity, &s->session->psk_identity)) {
1745 al = SSL_AD_INTERNAL_ERROR;
1746 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1747 ERR_R_MALLOC_FAILURE);
1748 goto f_err;
1749 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001750
Adam Langleyfcf25832014-12-18 17:42:32 -08001751 /* Look up the key for the identity. */
1752 psk_len =
1753 s->psk_server_callback(s, s->session->psk_identity, psk, sizeof(psk));
1754 if (psk_len > PSK_MAX_PSK_LEN) {
1755 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1756 ERR_R_INTERNAL_ERROR);
1757 al = SSL_AD_INTERNAL_ERROR;
1758 goto f_err;
1759 } else if (psk_len == 0) {
1760 /* PSK related to the given identity not found */
1761 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1762 SSL_R_PSK_IDENTITY_NOT_FOUND);
1763 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1764 goto f_err;
1765 }
1766 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001767
Adam Langleyfcf25832014-12-18 17:42:32 -08001768 /* Depending on the key exchange method, compute |premaster_secret| and
1769 * |premaster_secret_len|. */
1770 if (alg_k & SSL_kRSA) {
1771 CBS encrypted_premaster_secret;
1772 uint8_t rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
1773 uint8_t good;
1774 size_t rsa_size, decrypt_len, premaster_index, j;
Adam Langley95c29f32014-06-20 12:00:00 -07001775
Adam Langleyfcf25832014-12-18 17:42:32 -08001776 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1777 if (pkey == NULL || pkey->type != EVP_PKEY_RSA || pkey->pkey.rsa == NULL) {
1778 al = SSL_AD_HANDSHAKE_FAILURE;
1779 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1780 SSL_R_MISSING_RSA_CERTIFICATE);
1781 goto f_err;
1782 }
1783 rsa = pkey->pkey.rsa;
Adam Langley95c29f32014-06-20 12:00:00 -07001784
Adam Langleyfcf25832014-12-18 17:42:32 -08001785 /* TLS and [incidentally] DTLS{0xFEFF} */
1786 if (s->version > SSL3_VERSION) {
1787 CBS copy = client_key_exchange;
1788 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1789 &encrypted_premaster_secret) ||
1790 CBS_len(&client_key_exchange) != 0) {
1791 if (!(s->options & SSL_OP_TLS_D5_BUG)) {
1792 al = SSL_AD_DECODE_ERROR;
1793 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1794 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1795 goto f_err;
1796 } else {
1797 encrypted_premaster_secret = copy;
1798 }
1799 }
1800 } else {
1801 encrypted_premaster_secret = client_key_exchange;
1802 }
Adam Langley95c29f32014-06-20 12:00:00 -07001803
Adam Langleyfcf25832014-12-18 17:42:32 -08001804 /* Reject overly short RSA keys because we want to be sure that the buffer
1805 * size makes it safe to iterate over the entire size of a premaster secret
1806 * (SSL_MAX_MASTER_KEY_LENGTH). The actual expected size is larger due to
1807 * RSA padding, but the bound is sufficient to be safe. */
1808 rsa_size = RSA_size(rsa);
1809 if (rsa_size < SSL_MAX_MASTER_KEY_LENGTH) {
1810 al = SSL_AD_DECRYPT_ERROR;
1811 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1812 SSL_R_DECRYPTION_FAILED);
1813 goto f_err;
1814 }
Adam Langley95c29f32014-06-20 12:00:00 -07001815
Adam Langleyfcf25832014-12-18 17:42:32 -08001816 /* We must not leak whether a decryption failure occurs because of
1817 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
1818 * section 7.4.7.1). The code follows that advice of the TLS RFC and
1819 * generates a random premaster secret for the case that the decrypt fails.
1820 * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */
1821 if (!RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret))) {
1822 goto err;
1823 }
David Benjamin35c02302014-07-13 04:14:59 -04001824
Adam Langleyfcf25832014-12-18 17:42:32 -08001825 /* Allocate a buffer large enough for an RSA decryption. */
1826 decrypt_buf = OPENSSL_malloc(rsa_size);
1827 if (decrypt_buf == NULL) {
1828 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1829 ERR_R_MALLOC_FAILURE);
1830 goto err;
1831 }
David Benjamin35c02302014-07-13 04:14:59 -04001832
Adam Langleyfcf25832014-12-18 17:42:32 -08001833 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1834 * timing-sensitive code below. */
1835 if (!RSA_decrypt(rsa, &decrypt_len, decrypt_buf, rsa_size,
1836 CBS_data(&encrypted_premaster_secret),
1837 CBS_len(&encrypted_premaster_secret), RSA_NO_PADDING)) {
1838 goto err;
1839 }
1840 if (decrypt_len != rsa_size) {
1841 /* This should never happen, but do a check so we do not read
1842 * uninitialized memory. */
1843 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1844 ERR_R_INTERNAL_ERROR);
1845 goto err;
1846 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001847
Adam Langleyfcf25832014-12-18 17:42:32 -08001848 /* Remove the PKCS#1 padding and adjust |decrypt_len| as appropriate.
1849 * |good| will be 0xff if the premaster is acceptable and zero otherwise.
1850 * */
1851 good =
1852 constant_time_eq_int_8(RSA_message_index_PKCS1_type_2(
1853 decrypt_buf, decrypt_len, &premaster_index),
1854 1);
1855 decrypt_len = decrypt_len - premaster_index;
David Benjamin0aa07672014-07-24 15:34:14 -04001856
Adam Langleyfcf25832014-12-18 17:42:32 -08001857 /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. */
1858 good &= constant_time_eq_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
Adam Langleyacff73f2014-06-20 12:00:00 -07001859
Adam Langleyfcf25832014-12-18 17:42:32 -08001860 /* Copy over the unpadded premaster. Whatever the value of
1861 * |decrypt_good_mask|, copy as if the premaster were the right length. It
1862 * is important the memory access pattern be constant. */
1863 premaster_secret =
1864 BUF_memdup(decrypt_buf + (rsa_size - SSL_MAX_MASTER_KEY_LENGTH),
1865 SSL_MAX_MASTER_KEY_LENGTH);
1866 if (premaster_secret == NULL) {
1867 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1868 ERR_R_MALLOC_FAILURE);
1869 goto err;
1870 }
1871 OPENSSL_free(decrypt_buf);
1872 decrypt_buf = NULL;
Adam Langleyacff73f2014-06-20 12:00:00 -07001873
Adam Langleyfcf25832014-12-18 17:42:32 -08001874 /* If the version in the decrypted pre-master secret is correct then
1875 * version_good will be 0xff, otherwise it'll be zero. The
1876 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
1877 * (http://eprint.iacr.org/2003/052/) exploits the version number check as
1878 * a "bad version oracle". Thus version checks are done in constant time
1879 * and are treated like any other decryption error. */
1880 good &= constant_time_eq_8(premaster_secret[0],
1881 (unsigned)(s->client_version >> 8));
1882 good &= constant_time_eq_8(premaster_secret[1],
1883 (unsigned)(s->client_version & 0xff));
Adam Langleyacff73f2014-06-20 12:00:00 -07001884
Adam Langleyfcf25832014-12-18 17:42:32 -08001885 /* Now copy rand_premaster_secret over premaster_secret using
1886 * decrypt_good_mask. */
1887 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
1888 premaster_secret[j] = constant_time_select_8(good, premaster_secret[j],
1889 rand_premaster_secret[j]);
1890 }
David Benjamin35c02302014-07-13 04:14:59 -04001891
Adam Langleyfcf25832014-12-18 17:42:32 -08001892 premaster_secret_len = sizeof(rand_premaster_secret);
David Benjamin7061e282015-03-19 11:10:48 -04001893 } else if (alg_k & SSL_kDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001894 CBS dh_Yc;
1895 int dh_len;
David Benjamin35c02302014-07-13 04:14:59 -04001896
Adam Langleyfcf25832014-12-18 17:42:32 -08001897 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &dh_Yc) ||
1898 CBS_len(&dh_Yc) == 0 || CBS_len(&client_key_exchange) != 0) {
1899 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1900 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1901 al = SSL_R_DECODE_ERROR;
1902 goto f_err;
1903 }
Adam Langley95c29f32014-06-20 12:00:00 -07001904
Adam Langleyfcf25832014-12-18 17:42:32 -08001905 if (s->s3->tmp.dh == NULL) {
1906 al = SSL_AD_HANDSHAKE_FAILURE;
1907 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1908 SSL_R_MISSING_TMP_DH_KEY);
1909 goto f_err;
1910 }
1911 dh_srvr = s->s3->tmp.dh;
Adam Langley95c29f32014-06-20 12:00:00 -07001912
Adam Langleyfcf25832014-12-18 17:42:32 -08001913 pub = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL);
1914 if (pub == NULL) {
1915 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_BN_LIB);
1916 goto err;
1917 }
Adam Langley95c29f32014-06-20 12:00:00 -07001918
Adam Langleyfcf25832014-12-18 17:42:32 -08001919 /* Allocate a buffer for the premaster secret. */
1920 premaster_secret = OPENSSL_malloc(DH_size(dh_srvr));
1921 if (premaster_secret == NULL) {
1922 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1923 ERR_R_MALLOC_FAILURE);
David Benjaminc35fb012015-02-12 00:48:45 -05001924 BN_clear_free(pub);
Adam Langleyfcf25832014-12-18 17:42:32 -08001925 goto err;
1926 }
Adam Langley95c29f32014-06-20 12:00:00 -07001927
Adam Langleyfcf25832014-12-18 17:42:32 -08001928 dh_len = DH_compute_key(premaster_secret, pub, dh_srvr);
1929 if (dh_len <= 0) {
1930 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_DH_LIB);
1931 BN_clear_free(pub);
1932 goto err;
1933 }
David Benjamin14c83e72014-07-13 04:54:57 -04001934
Adam Langleyfcf25832014-12-18 17:42:32 -08001935 DH_free(s->s3->tmp.dh);
1936 s->s3->tmp.dh = NULL;
1937 BN_clear_free(pub);
1938 pub = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001939
Adam Langleyfcf25832014-12-18 17:42:32 -08001940 premaster_secret_len = dh_len;
David Benjamin7061e282015-03-19 11:10:48 -04001941 } else if (alg_k & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001942 int field_size = 0, ecdh_len;
1943 const EC_KEY *tkey;
1944 const EC_GROUP *group;
1945 const BIGNUM *priv_key;
1946 CBS ecdh_Yc;
Adam Langley95c29f32014-06-20 12:00:00 -07001947
Adam Langleyfcf25832014-12-18 17:42:32 -08001948 /* initialize structures for server's ECDH key pair */
1949 srvr_ecdh = EC_KEY_new();
1950 if (srvr_ecdh == NULL) {
1951 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1952 ERR_R_MALLOC_FAILURE);
1953 goto err;
1954 }
Adam Langley95c29f32014-06-20 12:00:00 -07001955
Adam Langleyfcf25832014-12-18 17:42:32 -08001956 /* Use the ephermeral values we saved when generating the ServerKeyExchange
1957 * msg. */
1958 tkey = s->s3->tmp.ecdh;
Adam Langley95c29f32014-06-20 12:00:00 -07001959
Adam Langleyfcf25832014-12-18 17:42:32 -08001960 group = EC_KEY_get0_group(tkey);
1961 priv_key = EC_KEY_get0_private_key(tkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001962
Adam Langleyfcf25832014-12-18 17:42:32 -08001963 if (!EC_KEY_set_group(srvr_ecdh, group) ||
1964 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
1965 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
1966 goto err;
1967 }
Adam Langley95c29f32014-06-20 12:00:00 -07001968
Adam Langleyfcf25832014-12-18 17:42:32 -08001969 /* Let's get client's public key */
1970 clnt_ecpoint = EC_POINT_new(group);
1971 if (clnt_ecpoint == NULL) {
1972 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1973 ERR_R_MALLOC_FAILURE);
1974 goto err;
1975 }
Adam Langley95c29f32014-06-20 12:00:00 -07001976
Adam Langleyfcf25832014-12-18 17:42:32 -08001977 /* Get client's public key from encoded point in the ClientKeyExchange
1978 * message. */
1979 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ecdh_Yc) ||
1980 CBS_len(&client_key_exchange) != 0) {
1981 al = SSL_AD_DECODE_ERROR;
1982 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR);
1983 goto f_err;
1984 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001985
Adam Langleyfcf25832014-12-18 17:42:32 -08001986 bn_ctx = BN_CTX_new();
1987 if (bn_ctx == NULL) {
1988 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1989 ERR_R_MALLOC_FAILURE);
1990 goto err;
1991 }
David Benjamin35c02302014-07-13 04:14:59 -04001992
Adam Langleyfcf25832014-12-18 17:42:32 -08001993 if (!EC_POINT_oct2point(group, clnt_ecpoint, CBS_data(&ecdh_Yc),
1994 CBS_len(&ecdh_Yc), bn_ctx)) {
1995 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
1996 goto err;
1997 }
Adam Langley95c29f32014-06-20 12:00:00 -07001998
Adam Langleyfcf25832014-12-18 17:42:32 -08001999 /* Allocate a buffer for both the secret and the PSK. */
2000 field_size = EC_GROUP_get_degree(group);
2001 if (field_size <= 0) {
2002 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
2003 goto err;
2004 }
David Benjamin35c02302014-07-13 04:14:59 -04002005
Adam Langleyfcf25832014-12-18 17:42:32 -08002006 ecdh_len = (field_size + 7) / 8;
2007 premaster_secret = OPENSSL_malloc(ecdh_len);
2008 if (premaster_secret == NULL) {
2009 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2010 ERR_R_MALLOC_FAILURE);
2011 goto err;
2012 }
David Benjamin35c02302014-07-13 04:14:59 -04002013
Adam Langleyfcf25832014-12-18 17:42:32 -08002014 /* Compute the shared pre-master secret */
2015 ecdh_len = ECDH_compute_key(premaster_secret, ecdh_len, clnt_ecpoint,
2016 srvr_ecdh, NULL);
2017 if (ecdh_len <= 0) {
2018 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
2019 goto err;
2020 }
Adam Langley95c29f32014-06-20 12:00:00 -07002021
Adam Langleyfcf25832014-12-18 17:42:32 -08002022 EVP_PKEY_free(clnt_pub_pkey);
2023 clnt_pub_pkey = NULL;
2024 EC_POINT_free(clnt_ecpoint);
2025 clnt_ecpoint = NULL;
2026 EC_KEY_free(srvr_ecdh);
2027 srvr_ecdh = NULL;
2028 BN_CTX_free(bn_ctx);
2029 bn_ctx = NULL;
2030 EC_KEY_free(s->s3->tmp.ecdh);
2031 s->s3->tmp.ecdh = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002032
Adam Langleyfcf25832014-12-18 17:42:32 -08002033 premaster_secret_len = ecdh_len;
2034 } else if (alg_k & SSL_kPSK) {
2035 /* For plain PSK, other_secret is a block of 0s with the same length as the
2036 * pre-shared key. */
2037 premaster_secret_len = psk_len;
2038 premaster_secret = OPENSSL_malloc(premaster_secret_len);
2039 if (premaster_secret == NULL) {
2040 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2041 ERR_R_MALLOC_FAILURE);
2042 goto err;
2043 }
2044 memset(premaster_secret, 0, premaster_secret_len);
2045 } else {
2046 al = SSL_AD_HANDSHAKE_FAILURE;
2047 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2048 SSL_R_UNKNOWN_CIPHER_TYPE);
2049 goto f_err;
2050 }
Adam Langley95c29f32014-06-20 12:00:00 -07002051
Adam Langleyfcf25832014-12-18 17:42:32 -08002052 /* For a PSK cipher suite, the actual pre-master secret is combined with the
2053 * pre-shared key. */
2054 if (alg_a & SSL_aPSK) {
2055 CBB new_premaster, child;
2056 uint8_t *new_data;
2057 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04002058
Adam Langleyfcf25832014-12-18 17:42:32 -08002059 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len)) {
2060 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2061 ERR_R_MALLOC_FAILURE);
2062 goto err;
2063 }
2064 if (!CBB_add_u16_length_prefixed(&new_premaster, &child) ||
2065 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
2066 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
2067 !CBB_add_bytes(&child, psk, psk_len) ||
2068 !CBB_finish(&new_premaster, &new_data, &new_len)) {
2069 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2070 ERR_R_INTERNAL_ERROR);
2071 CBB_cleanup(&new_premaster);
2072 goto err;
2073 }
David Benjamin14c83e72014-07-13 04:54:57 -04002074
Adam Langleyfcf25832014-12-18 17:42:32 -08002075 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2076 OPENSSL_free(premaster_secret);
2077 premaster_secret = new_data;
2078 premaster_secret_len = new_len;
2079 }
David Benjamin14c83e72014-07-13 04:54:57 -04002080
Adam Langleyfcf25832014-12-18 17:42:32 -08002081 /* Compute the master secret */
2082 s->session->master_key_length = s->enc_method->generate_master_secret(
2083 s, s->session->master_key, premaster_secret, premaster_secret_len);
2084 if (s->session->master_key_length == 0) {
2085 goto err;
2086 }
2087 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04002088
Adam Langleyfcf25832014-12-18 17:42:32 -08002089 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2090 OPENSSL_free(premaster_secret);
2091 return 1;
2092
Adam Langley95c29f32014-06-20 12:00:00 -07002093f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002094 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07002095err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002096 if (premaster_secret) {
2097 if (premaster_secret_len) {
2098 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2099 }
2100 OPENSSL_free(premaster_secret);
2101 }
David Benjamin2755a3e2015-04-22 16:17:58 -04002102 OPENSSL_free(decrypt_buf);
Adam Langleyfcf25832014-12-18 17:42:32 -08002103 EVP_PKEY_free(clnt_pub_pkey);
2104 EC_POINT_free(clnt_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04002105 EC_KEY_free(srvr_ecdh);
Adam Langleyfcf25832014-12-18 17:42:32 -08002106 BN_CTX_free(bn_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002107
Adam Langleyfcf25832014-12-18 17:42:32 -08002108 return -1;
2109}
Adam Langley95c29f32014-06-20 12:00:00 -07002110
Adam Langleyfcf25832014-12-18 17:42:32 -08002111int ssl3_get_cert_verify(SSL *s) {
2112 int al, ok, ret = 0;
2113 long n;
2114 CBS certificate_verify, signature;
2115 X509 *peer = s->session->peer;
2116 EVP_PKEY *pkey = NULL;
2117 const EVP_MD *md = NULL;
2118 uint8_t digest[EVP_MAX_MD_SIZE];
2119 size_t digest_length;
2120 EVP_PKEY_CTX *pctx = NULL;
David Benjamin6553b372014-07-22 14:11:30 -04002121
Adam Langleyfcf25832014-12-18 17:42:32 -08002122 /* Only RSA and ECDSA client certificates are supported, so a
2123 * CertificateVerify is required if and only if there's a client certificate.
2124 * */
2125 if (peer == NULL) {
2126 if (s->s3->handshake_buffer &&
2127 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2128 return -1;
2129 }
2130 return 1;
2131 }
Adam Langley95c29f32014-06-20 12:00:00 -07002132
Adam Langleyfcf25832014-12-18 17:42:32 -08002133 n = s->method->ssl_get_message(
2134 s, SSL3_ST_SR_CERT_VRFY_A, SSL3_ST_SR_CERT_VRFY_B,
2135 SSL3_MT_CERTIFICATE_VERIFY, SSL3_RT_MAX_PLAIN_LENGTH,
David Benjamin5ca39fb2015-03-01 23:57:54 -05002136 ssl_dont_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002137
Adam Langleyfcf25832014-12-18 17:42:32 -08002138 if (!ok) {
2139 return n;
2140 }
David Benjaminef865502014-08-24 02:48:34 -04002141
Adam Langleyfcf25832014-12-18 17:42:32 -08002142 /* Filter out unsupported certificate types. */
2143 pkey = X509_get_pubkey(peer);
David Benjamin9d0847a2015-02-16 03:57:55 -05002144 if (pkey == NULL) {
2145 goto err;
2146 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002147 if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) ||
2148 (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) {
2149 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
2150 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify,
2151 SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
2152 goto f_err;
2153 }
David Benjamin6897dbe2014-07-12 20:18:28 -04002154
Adam Langleyfcf25832014-12-18 17:42:32 -08002155 CBS_init(&certificate_verify, s->init_msg, n);
David Benjamin6897dbe2014-07-12 20:18:28 -04002156
Adam Langleyfcf25832014-12-18 17:42:32 -08002157 /* Determine the digest type if needbe. */
2158 if (SSL_USE_SIGALGS(s) &&
2159 !tls12_check_peer_sigalg(&md, &al, s, &certificate_verify, pkey)) {
2160 goto f_err;
2161 }
David Benjamin854dd652014-08-26 00:32:30 -04002162
Adam Langleyfcf25832014-12-18 17:42:32 -08002163 /* Compute the digest. */
2164 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2165 goto err;
2166 }
David Benjamin854dd652014-08-26 00:32:30 -04002167
Adam Langleyfcf25832014-12-18 17:42:32 -08002168 /* The handshake buffer is no longer necessary, and we may hash the current
2169 * message.*/
2170 if (s->s3->handshake_buffer &&
2171 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2172 goto err;
2173 }
David Benjaminfbdfefb2015-02-16 19:33:53 -05002174 if (!ssl3_hash_current_message(s)) {
2175 goto err;
2176 }
Adam Langley95c29f32014-06-20 12:00:00 -07002177
Adam Langleyfcf25832014-12-18 17:42:32 -08002178 /* Parse and verify the signature. */
2179 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
2180 CBS_len(&certificate_verify) != 0) {
2181 al = SSL_AD_DECODE_ERROR;
2182 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_DECODE_ERROR);
2183 goto f_err;
2184 }
Adam Langley95c29f32014-06-20 12:00:00 -07002185
Adam Langleyfcf25832014-12-18 17:42:32 -08002186 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2187 if (pctx == NULL) {
2188 goto err;
2189 }
2190 if (!EVP_PKEY_verify_init(pctx) ||
2191 !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2192 !EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), digest,
2193 digest_length)) {
2194 al = SSL_AD_DECRYPT_ERROR;
2195 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_SIGNATURE);
2196 goto f_err;
2197 }
2198
2199 ret = 1;
2200
2201 if (0) {
2202 f_err:
2203 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2204 }
2205
David Benjamin854dd652014-08-26 00:32:30 -04002206err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002207 EVP_PKEY_CTX_free(pctx);
2208 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07002209
Adam Langleyfcf25832014-12-18 17:42:32 -08002210 return ret;
2211}
Adam Langley95c29f32014-06-20 12:00:00 -07002212
Adam Langleyfcf25832014-12-18 17:42:32 -08002213int ssl3_get_client_certificate(SSL *s) {
2214 int i, ok, al, ret = -1;
2215 X509 *x = NULL;
2216 unsigned long n;
2217 STACK_OF(X509) *sk = NULL;
2218 SHA256_CTX sha256;
2219 CBS certificate_msg, certificate_list;
2220 int is_first_certificate = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002221
Adam Langleyfcf25832014-12-18 17:42:32 -08002222 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 -05002223 s->max_cert_list, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002224
Adam Langleyfcf25832014-12-18 17:42:32 -08002225 if (!ok) {
2226 return n;
2227 }
Adam Langley95c29f32014-06-20 12:00:00 -07002228
Adam Langleyfcf25832014-12-18 17:42:32 -08002229 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
2230 if ((s->verify_mode & SSL_VERIFY_PEER) &&
2231 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2232 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2233 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2234 al = SSL_AD_HANDSHAKE_FAILURE;
2235 goto f_err;
2236 }
David Benjamind5b1f842014-07-11 23:43:48 -04002237
Adam Langleyfcf25832014-12-18 17:42:32 -08002238 /* If tls asked for a client cert, the client must return a 0 list */
2239 if (s->version > SSL3_VERSION && s->s3->tmp.cert_request) {
2240 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2241 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
2242 al = SSL_AD_UNEXPECTED_MESSAGE;
2243 goto f_err;
2244 }
2245 s->s3->tmp.reuse_message = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002246
Adam Langleyfcf25832014-12-18 17:42:32 -08002247 return 1;
2248 }
Adam Langley95c29f32014-06-20 12:00:00 -07002249
Adam Langleyfcf25832014-12-18 17:42:32 -08002250 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
2251 al = SSL_AD_UNEXPECTED_MESSAGE;
2252 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2253 SSL_R_WRONG_MESSAGE_TYPE);
2254 goto f_err;
2255 }
David Benjamind5b1f842014-07-11 23:43:48 -04002256
Adam Langleyfcf25832014-12-18 17:42:32 -08002257 CBS_init(&certificate_msg, s->init_msg, n);
David Benjamind5b1f842014-07-11 23:43:48 -04002258
Adam Langleyfcf25832014-12-18 17:42:32 -08002259 sk = sk_X509_new_null();
2260 if (sk == NULL) {
2261 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2262 goto err;
2263 }
Adam Langley95c29f32014-06-20 12:00:00 -07002264
Adam Langleyfcf25832014-12-18 17:42:32 -08002265 if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) ||
2266 CBS_len(&certificate_msg) != 0) {
2267 al = SSL_AD_DECODE_ERROR;
2268 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR);
2269 goto f_err;
2270 }
Adam Langley95c29f32014-06-20 12:00:00 -07002271
Adam Langleyfcf25832014-12-18 17:42:32 -08002272 while (CBS_len(&certificate_list) > 0) {
2273 CBS certificate;
2274 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07002275
Adam Langleyfcf25832014-12-18 17:42:32 -08002276 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
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 if (is_first_certificate && s->ctx->retain_only_sha256_of_client_certs) {
2283 /* If this is the first certificate, and we don't want to keep peer
2284 * certificates in memory, then we hash it right away. */
2285 SHA256_Init(&sha256);
2286 SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate));
2287 SHA256_Final(s->session->peer_sha256, &sha256);
2288 s->session->peer_sha256_valid = 1;
2289 }
2290 is_first_certificate = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002291
Adam Langleyfcf25832014-12-18 17:42:32 -08002292 data = CBS_data(&certificate);
2293 x = d2i_X509(NULL, &data, CBS_len(&certificate));
2294 if (x == NULL) {
2295 al = SSL_AD_BAD_CERTIFICATE;
2296 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_ASN1_LIB);
2297 goto f_err;
2298 }
2299 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
2300 al = SSL_AD_DECODE_ERROR;
2301 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2302 SSL_R_CERT_LENGTH_MISMATCH);
2303 goto f_err;
2304 }
2305 if (!sk_X509_push(sk, x)) {
2306 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2307 goto err;
2308 }
2309 x = NULL;
2310 }
2311
2312 if (sk_X509_num(sk) <= 0) {
2313 /* TLS does not mind 0 certs returned */
2314 if (s->version == SSL3_VERSION) {
2315 al = SSL_AD_HANDSHAKE_FAILURE;
2316 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2317 SSL_R_NO_CERTIFICATES_RETURNED);
2318 goto f_err;
2319 }
2320 /* Fail for TLS only if we required a certificate */
2321 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
2322 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2323 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2324 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2325 al = SSL_AD_HANDSHAKE_FAILURE;
2326 goto f_err;
2327 }
2328 /* No client certificate so digest cached records */
2329 if (s->s3->handshake_buffer &&
2330 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2331 al = SSL_AD_INTERNAL_ERROR;
2332 goto f_err;
2333 }
2334 } else {
2335 i = ssl_verify_cert_chain(s, sk);
2336 if (i <= 0) {
2337 al = ssl_verify_alarm_type(s->verify_result);
2338 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2339 SSL_R_CERTIFICATE_VERIFY_FAILED);
2340 goto f_err;
2341 }
2342 }
2343
David Benjamin2755a3e2015-04-22 16:17:58 -04002344 X509_free(s->session->peer);
Adam Langleyfcf25832014-12-18 17:42:32 -08002345 s->session->peer = sk_X509_shift(sk);
2346 s->session->verify_result = s->verify_result;
2347
2348 /* With the current implementation, sess_cert will always be NULL when we
2349 * arrive here. */
2350 if (s->session->sess_cert == NULL) {
2351 s->session->sess_cert = ssl_sess_cert_new();
2352 if (s->session->sess_cert == NULL) {
2353 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2354 goto err;
2355 }
2356 }
David Benjamin2755a3e2015-04-22 16:17:58 -04002357 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
Adam Langleyfcf25832014-12-18 17:42:32 -08002358 s->session->sess_cert->cert_chain = sk;
2359 /* Inconsistency alert: cert_chain does *not* include the peer's own
2360 * certificate, while we do include it in s3_clnt.c */
2361
2362 sk = NULL;
2363
2364 ret = 1;
2365
2366 if (0) {
2367 f_err:
2368 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2369 }
2370
Adam Langley95c29f32014-06-20 12:00:00 -07002371err:
David Benjamin2755a3e2015-04-22 16:17:58 -04002372 X509_free(x);
2373 sk_X509_pop_free(sk, X509_free);
Adam Langleyfcf25832014-12-18 17:42:32 -08002374 return ret;
2375}
Adam Langley95c29f32014-06-20 12:00:00 -07002376
Adam Langleyfcf25832014-12-18 17:42:32 -08002377int ssl3_send_server_certificate(SSL *s) {
2378 CERT_PKEY *cpk;
Adam Langley95c29f32014-06-20 12:00:00 -07002379
Adam Langleyfcf25832014-12-18 17:42:32 -08002380 if (s->state == SSL3_ST_SW_CERT_A) {
2381 cpk = ssl_get_server_send_pkey(s);
2382 if (cpk == NULL) {
2383 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_certificate,
2384 ERR_R_INTERNAL_ERROR);
2385 return 0;
2386 }
Adam Langley95c29f32014-06-20 12:00:00 -07002387
David Benjamin9d0847a2015-02-16 03:57:55 -05002388 if (!ssl3_output_cert_chain(s, cpk)) {
2389 return 0;
2390 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002391 s->state = SSL3_ST_SW_CERT_B;
2392 }
Adam Langley95c29f32014-06-20 12:00:00 -07002393
Adam Langleyfcf25832014-12-18 17:42:32 -08002394 /* SSL3_ST_SW_CERT_B */
2395 return ssl_do_write(s);
2396}
Adam Langley95c29f32014-06-20 12:00:00 -07002397
Adam Langley95c29f32014-06-20 12:00:00 -07002398/* send a new session ticket (not necessarily for a new session) */
Adam Langleyfcf25832014-12-18 17:42:32 -08002399int ssl3_send_new_session_ticket(SSL *s) {
David Benjaminbcd37452015-03-09 14:48:28 -04002400 int ret = -1;
2401 uint8_t *session = NULL;
2402 size_t session_len;
2403 EVP_CIPHER_CTX ctx;
2404 HMAC_CTX hctx;
2405
2406 EVP_CIPHER_CTX_init(&ctx);
2407 HMAC_CTX_init(&hctx);
2408
Adam Langleyfcf25832014-12-18 17:42:32 -08002409 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002410 uint8_t *p, *macstart;
2411 int len;
2412 unsigned int hlen;
Adam Langleyfcf25832014-12-18 17:42:32 -08002413 SSL_CTX *tctx = s->initial_ctx;
2414 uint8_t iv[EVP_MAX_IV_LENGTH];
2415 uint8_t key_name[16];
2416 /* The maximum overhead of encrypting the session is 16 (key name) + IV +
2417 * one block of encryption overhead + HMAC. */
2418 const size_t max_ticket_overhead =
2419 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -07002420
Adam Langleyfcf25832014-12-18 17:42:32 -08002421 /* Serialize the SSL_SESSION to be encoded into the ticket. */
2422 if (!SSL_SESSION_to_bytes_for_ticket(s->session, &session, &session_len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002423 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002424 }
Adam Langley95c29f32014-06-20 12:00:00 -07002425
Adam Langleyfcf25832014-12-18 17:42:32 -08002426 /* If the session is too long, emit a dummy value rather than abort the
2427 * connection. */
2428 if (session_len > 0xFFFF - max_ticket_overhead) {
2429 static const char kTicketPlaceholder[] = "TICKET TOO LARGE";
2430 const size_t placeholder_len = strlen(kTicketPlaceholder);
David Benjamin3cac4502014-10-21 01:46:30 -04002431
Adam Langleyfcf25832014-12-18 17:42:32 -08002432 OPENSSL_free(session);
David Benjaminbcd37452015-03-09 14:48:28 -04002433 session = NULL;
David Benjamin3cac4502014-10-21 01:46:30 -04002434
Adam Langleyfcf25832014-12-18 17:42:32 -08002435 p = ssl_handshake_start(s);
2436 /* Emit ticket_lifetime_hint. */
2437 l2n(0, p);
2438 /* Emit ticket. */
2439 s2n(placeholder_len, p);
2440 memcpy(p, kTicketPlaceholder, placeholder_len);
2441 p += placeholder_len;
David Benjamin3cac4502014-10-21 01:46:30 -04002442
Adam Langleyfcf25832014-12-18 17:42:32 -08002443 len = p - ssl_handshake_start(s);
David Benjaminfbdfefb2015-02-16 19:33:53 -05002444 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002445 goto err;
David Benjaminfbdfefb2015-02-16 19:33:53 -05002446 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002447 s->state = SSL3_ST_SW_SESSION_TICKET_B;
2448 return ssl_do_write(s);
2449 }
Adam Langley95c29f32014-06-20 12:00:00 -07002450
Adam Langleyfcf25832014-12-18 17:42:32 -08002451 /* Grow buffer if need be: the length calculation is as follows:
2452 * handshake_header_length + 4 (ticket lifetime hint) + 2 (ticket length) +
2453 * max_ticket_overhead + * session_length */
2454 if (!BUF_MEM_grow(s->init_buf, SSL_HM_HEADER_LENGTH(s) + 6 +
2455 max_ticket_overhead + session_len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002456 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002457 }
2458 p = ssl_handshake_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002459 /* Initialize HMAC and cipher contexts. If callback present it does all the
2460 * work otherwise use generated values from parent ctx. */
2461 if (tctx->tlsext_ticket_key_cb) {
2462 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, &hctx, 1) < 0) {
David Benjaminbcd37452015-03-09 14:48:28 -04002463 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002464 }
2465 } else {
2466 if (!RAND_bytes(iv, 16) ||
2467 !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2468 tctx->tlsext_tick_aes_key, iv) ||
2469 !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2470 NULL)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002471 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002472 }
2473 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
2474 }
Adam Langley95c29f32014-06-20 12:00:00 -07002475
Adam Langleyfcf25832014-12-18 17:42:32 -08002476 /* Ticket lifetime hint (advisory only): We leave this unspecified for
2477 * resumed session (for simplicity), and guess that tickets for new
2478 * sessions will live as long as their sessions. */
2479 l2n(s->hit ? 0 : s->session->timeout, p);
Adam Langley95c29f32014-06-20 12:00:00 -07002480
Adam Langleyfcf25832014-12-18 17:42:32 -08002481 /* Skip ticket length for now */
2482 p += 2;
2483 /* Output key name */
2484 macstart = p;
2485 memcpy(p, key_name, 16);
2486 p += 16;
2487 /* output IV */
2488 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
2489 p += EVP_CIPHER_CTX_iv_length(&ctx);
2490 /* Encrypt session data */
David Benjaminbcd37452015-03-09 14:48:28 -04002491 if (!EVP_EncryptUpdate(&ctx, p, &len, session, session_len)) {
2492 goto err;
2493 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002494 p += len;
David Benjaminbcd37452015-03-09 14:48:28 -04002495 if (!EVP_EncryptFinal_ex(&ctx, p, &len)) {
2496 goto err;
2497 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002498 p += len;
Adam Langley95c29f32014-06-20 12:00:00 -07002499
David Benjaminbcd37452015-03-09 14:48:28 -04002500 if (!HMAC_Update(&hctx, macstart, p - macstart) ||
2501 !HMAC_Final(&hctx, p, &hlen)) {
2502 goto err;
2503 }
Adam Langley95c29f32014-06-20 12:00:00 -07002504
Adam Langleyfcf25832014-12-18 17:42:32 -08002505 p += hlen;
2506 /* Now write out lengths: p points to end of data written */
2507 /* Total length */
2508 len = p - ssl_handshake_start(s);
2509 /* Skip ticket lifetime hint */
2510 p = ssl_handshake_start(s) + 4;
2511 s2n(len - 6, p);
David Benjaminfbdfefb2015-02-16 19:33:53 -05002512 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002513 goto err;
David Benjaminfbdfefb2015-02-16 19:33:53 -05002514 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002515 s->state = SSL3_ST_SW_SESSION_TICKET_B;
Adam Langleyfcf25832014-12-18 17:42:32 -08002516 }
Adam Langley95c29f32014-06-20 12:00:00 -07002517
Adam Langleyfcf25832014-12-18 17:42:32 -08002518 /* SSL3_ST_SW_SESSION_TICKET_B */
David Benjaminbcd37452015-03-09 14:48:28 -04002519 ret = ssl_do_write(s);
2520
2521err:
David Benjamin2755a3e2015-04-22 16:17:58 -04002522 OPENSSL_free(session);
David Benjaminbcd37452015-03-09 14:48:28 -04002523 EVP_CIPHER_CTX_cleanup(&ctx);
2524 HMAC_CTX_cleanup(&hctx);
2525 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002526}
Adam Langley95c29f32014-06-20 12:00:00 -07002527
Adam Langley95c29f32014-06-20 12:00:00 -07002528/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
2529 * sets the next_proto member in s if found */
Adam Langleyfcf25832014-12-18 17:42:32 -08002530int ssl3_get_next_proto(SSL *s) {
2531 int ok;
2532 long n;
2533 CBS next_protocol, selected_protocol, padding;
Adam Langley95c29f32014-06-20 12:00:00 -07002534
Adam Langleyfcf25832014-12-18 17:42:32 -08002535 /* Clients cannot send a NextProtocol message if we didn't see the extension
2536 * in their ClientHello */
2537 if (!s->s3->next_proto_neg_seen) {
2538 OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto,
2539 SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
2540 return -1;
2541 }
Adam Langley95c29f32014-06-20 12:00:00 -07002542
Adam Langleyfcf25832014-12-18 17:42:32 -08002543 n = s->method->ssl_get_message(s, SSL3_ST_SR_NEXT_PROTO_A,
2544 SSL3_ST_SR_NEXT_PROTO_B, SSL3_MT_NEXT_PROTO,
2545 514, /* See the payload format below */
David Benjamin5ca39fb2015-03-01 23:57:54 -05002546 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002547
Adam Langleyfcf25832014-12-18 17:42:32 -08002548 if (!ok) {
2549 return n;
2550 }
Adam Langley95c29f32014-06-20 12:00:00 -07002551
Adam Langleyfcf25832014-12-18 17:42:32 -08002552 /* s->state doesn't reflect whether ChangeCipherSpec has been received in
2553 * this handshake, but s->s3->change_cipher_spec does (will be reset by
2554 * ssl3_get_finished).
2555 *
2556 * TODO(davidben): Is this check now redundant with
2557 * SSL3_FLAGS_EXPECT_CCS? */
2558 if (!s->s3->change_cipher_spec) {
2559 OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto,
2560 SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
2561 return -1;
2562 }
Adam Langley95c29f32014-06-20 12:00:00 -07002563
Adam Langleyfcf25832014-12-18 17:42:32 -08002564 CBS_init(&next_protocol, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07002565
Adam Langleyfcf25832014-12-18 17:42:32 -08002566 /* The payload looks like:
2567 * uint8 proto_len;
2568 * uint8 proto[proto_len];
2569 * uint8 padding_len;
2570 * uint8 padding[padding_len]; */
2571 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
2572 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
2573 CBS_len(&next_protocol) != 0 ||
2574 !CBS_stow(&selected_protocol, &s->next_proto_negotiated,
2575 &s->next_proto_negotiated_len)) {
2576 return 0;
2577 }
Adam Langley95c29f32014-06-20 12:00:00 -07002578
Adam Langleyfcf25832014-12-18 17:42:32 -08002579 return 1;
2580}
Adam Langley95c29f32014-06-20 12:00:00 -07002581
Adam Langley1258b6a2014-06-20 12:00:00 -07002582/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002583int ssl3_get_channel_id(SSL *s) {
2584 int ret = -1, ok;
2585 long n;
2586 EVP_MD_CTX md_ctx;
2587 uint8_t channel_id_hash[SHA256_DIGEST_LENGTH];
2588 unsigned int channel_id_hash_len;
2589 const uint8_t *p;
2590 uint16_t extension_type, expected_extension_type;
2591 EC_GROUP *p256 = NULL;
2592 EC_KEY *key = NULL;
2593 EC_POINT *point = NULL;
2594 ECDSA_SIG sig;
2595 BIGNUM x, y;
2596 CBS encrypted_extensions, extension;
Adam Langley1258b6a2014-06-20 12:00:00 -07002597
Adam Langleyfcf25832014-12-18 17:42:32 -08002598 n = s->method->ssl_get_message(
2599 s, SSL3_ST_SR_CHANNEL_ID_A, SSL3_ST_SR_CHANNEL_ID_B,
2600 SSL3_MT_ENCRYPTED_EXTENSIONS, 2 + 2 + TLSEXT_CHANNEL_ID_SIZE,
David Benjamin5ca39fb2015-03-01 23:57:54 -05002601 ssl_dont_hash_message, &ok);
Adam Langley1258b6a2014-06-20 12:00:00 -07002602
Adam Langleyfcf25832014-12-18 17:42:32 -08002603 if (!ok) {
2604 return n;
2605 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002606
Adam Langleyfcf25832014-12-18 17:42:32 -08002607 /* Before incorporating the EncryptedExtensions message to the handshake
2608 * hash, compute the hash that should have been signed. */
2609 channel_id_hash_len = sizeof(channel_id_hash);
2610 EVP_MD_CTX_init(&md_ctx);
2611 if (!EVP_DigestInit_ex(&md_ctx, EVP_sha256(), NULL) ||
2612 !tls1_channel_id_hash(&md_ctx, s) ||
2613 !EVP_DigestFinal(&md_ctx, channel_id_hash, &channel_id_hash_len)) {
2614 EVP_MD_CTX_cleanup(&md_ctx);
2615 return -1;
2616 }
2617 EVP_MD_CTX_cleanup(&md_ctx);
2618 assert(channel_id_hash_len == SHA256_DIGEST_LENGTH);
David Benjamin880b14e2014-08-25 22:35:07 -04002619
David Benjaminfbdfefb2015-02-16 19:33:53 -05002620 if (!ssl3_hash_current_message(s)) {
2621 return -1;
2622 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002623
Adam Langleyfcf25832014-12-18 17:42:32 -08002624 /* s->state doesn't reflect whether ChangeCipherSpec has been received in
2625 * this handshake, but s->s3->change_cipher_spec does (will be reset by
2626 * ssl3_get_finished).
2627 *
2628 * TODO(davidben): Is this check now redundant with SSL3_FLAGS_EXPECT_CCS? */
2629 if (!s->s3->change_cipher_spec) {
2630 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id,
2631 SSL_R_GOT_CHANNEL_ID_BEFORE_A_CCS);
2632 return -1;
2633 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002634
Adam Langleyfcf25832014-12-18 17:42:32 -08002635 CBS_init(&encrypted_extensions, s->init_msg, n);
Adam Langley1258b6a2014-06-20 12:00:00 -07002636
Adam Langleyfcf25832014-12-18 17:42:32 -08002637 /* EncryptedExtensions could include multiple extensions, but the only
2638 * extension that could be negotiated is ChannelID, so there can only be one
2639 * entry.
2640 *
2641 * The payload looks like:
2642 * uint16 extension_type
2643 * uint16 extension_len;
2644 * uint8 x[32];
2645 * uint8 y[32];
2646 * uint8 r[32];
2647 * uint8 s[32]; */
2648 expected_extension_type = TLSEXT_TYPE_channel_id;
2649 if (s->s3->tlsext_channel_id_new) {
2650 expected_extension_type = TLSEXT_TYPE_channel_id_new;
2651 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002652
Adam Langleyfcf25832014-12-18 17:42:32 -08002653 if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
2654 !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
2655 CBS_len(&encrypted_extensions) != 0 ||
2656 extension_type != expected_extension_type ||
2657 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
2658 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_INVALID_MESSAGE);
2659 return -1;
2660 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002661
Adam Langleyfcf25832014-12-18 17:42:32 -08002662 p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
2663 if (!p256) {
2664 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_NO_P256_SUPPORT);
2665 return -1;
2666 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002667
Adam Langleyfcf25832014-12-18 17:42:32 -08002668 BN_init(&x);
2669 BN_init(&y);
2670 sig.r = BN_new();
2671 sig.s = BN_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05002672 if (sig.r == NULL || sig.s == NULL) {
2673 goto err;
2674 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002675
Adam Langleyfcf25832014-12-18 17:42:32 -08002676 p = CBS_data(&extension);
2677 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
2678 BN_bin2bn(p + 32, 32, &y) == NULL ||
2679 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
2680 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
2681 goto err;
2682 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002683
Adam Langleyfcf25832014-12-18 17:42:32 -08002684 point = EC_POINT_new(p256);
2685 if (!point || !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
2686 goto err;
2687 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002688
Adam Langleyfcf25832014-12-18 17:42:32 -08002689 key = EC_KEY_new();
2690 if (!key || !EC_KEY_set_group(key, p256) ||
2691 !EC_KEY_set_public_key(key, point)) {
2692 goto err;
2693 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002694
Adam Langleyfcf25832014-12-18 17:42:32 -08002695 /* We stored the handshake hash in |tlsext_channel_id| the first time that we
2696 * were called. */
2697 if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) {
2698 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id,
2699 SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
2700 s->s3->tlsext_channel_id_valid = 0;
2701 goto err;
2702 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002703
Adam Langleyfcf25832014-12-18 17:42:32 -08002704 memcpy(s->s3->tlsext_channel_id, p, 64);
2705 ret = 1;
Adam Langley1258b6a2014-06-20 12:00:00 -07002706
2707err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002708 BN_free(&x);
2709 BN_free(&y);
2710 BN_free(sig.r);
2711 BN_free(sig.s);
David Benjamin2755a3e2015-04-22 16:17:58 -04002712 EC_KEY_free(key);
2713 EC_POINT_free(point);
2714 EC_GROUP_free(p256);
Adam Langleyfcf25832014-12-18 17:42:32 -08002715 return ret;
2716}