blob: a5280e94b1c8de7786e67507f952f52fc0cb740d [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 (!s->s3->send_connection_binding &&
231 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
232 /* Server attempting to renegotiate with client that doesn't support
233 * secure renegotiation. */
234 OPENSSL_PUT_ERROR(SSL, ssl3_accept,
235 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
236 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
237 ret = -1;
238 goto end;
239 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500240
Adam Langleyfcf25832014-12-18 17:42:32 -0800241 s->state = SSL3_ST_SW_HELLO_REQ_A;
242 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700243
Adam Langleyfcf25832014-12-18 17:42:32 -0800244 case SSL3_ST_SW_HELLO_REQ_A:
245 case SSL3_ST_SW_HELLO_REQ_B:
246 s->shutdown = 0;
247 ret = ssl3_send_hello_request(s);
248 if (ret <= 0) {
249 goto end;
250 }
251 s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C;
252 s->state = SSL3_ST_SW_FLUSH;
253 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700254
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 if (!ssl3_init_finished_mac(s)) {
256 OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR);
257 ret = -1;
258 goto end;
259 }
260 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700261
Adam Langleyfcf25832014-12-18 17:42:32 -0800262 case SSL3_ST_SW_HELLO_REQ_C:
263 s->state = SSL_ST_OK;
264 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 case SSL_ST_ACCEPT:
267 case SSL_ST_BEFORE | SSL_ST_ACCEPT:
268 /* This state is the entry point for the handshake itself (initial and
269 * renegotiation). */
270 if (cb != NULL) {
271 cb(s, SSL_CB_HANDSHAKE_START, 1);
272 }
Adam Langley95c29f32014-06-20 12:00:00 -0700273
David Benjaminb16346b2015-04-08 19:16:58 -0400274 if ((s->version >> 8) != 3) {
275 /* TODO(davidben): Some consumers clear |s->version| to break the
276 * handshake in a callback. Remove this when they're using proper
277 * APIs. */
278 OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR);
279 ret = -1;
280 goto end;
281 }
282
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 if (s->init_buf == NULL) {
284 buf = BUF_MEM_new();
285 if (!buf || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
286 ret = -1;
287 goto end;
288 }
289 s->init_buf = buf;
290 buf = NULL;
291 }
292 s->init_num = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500293
David Benjamin6a08da22015-05-08 22:58:12 -0400294 /* Enable a write buffer. This groups handshake messages within a flight
295 * into a single write. */
296 if (!ssl_init_wbio_buffer(s, 1)) {
297 ret = -1;
298 goto end;
299 }
300
Adam Langleyfcf25832014-12-18 17:42:32 -0800301 if (!ssl3_init_finished_mac(s)) {
302 OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR);
303 ret = -1;
304 goto end;
305 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500306
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 if (!s->s3->have_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800308 s->state = SSL3_ST_SR_INITIAL_BYTES;
309 } else {
Adam Langleyfcf25832014-12-18 17:42:32 -0800310 s->state = SSL3_ST_SR_CLNT_HELLO_A;
311 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800312 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500313
Adam Langleyfcf25832014-12-18 17:42:32 -0800314 case SSL3_ST_SR_INITIAL_BYTES:
315 ret = ssl3_get_initial_bytes(s);
316 if (ret <= 0) {
317 goto end;
318 }
319 /* ssl3_get_initial_bytes sets s->state to one of
320 * SSL3_ST_SR_V2_CLIENT_HELLO or SSL3_ST_SR_CLNT_HELLO_A on success. */
321 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500322
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 case SSL3_ST_SR_V2_CLIENT_HELLO:
324 ret = ssl3_get_v2_client_hello(s);
325 if (ret <= 0) {
326 goto end;
327 }
328 s->state = SSL3_ST_SR_CLNT_HELLO_A;
329 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500330
Adam Langleyfcf25832014-12-18 17:42:32 -0800331 case SSL3_ST_SR_CLNT_HELLO_A:
332 case SSL3_ST_SR_CLNT_HELLO_B:
333 case SSL3_ST_SR_CLNT_HELLO_C:
334 case SSL3_ST_SR_CLNT_HELLO_D:
335 s->shutdown = 0;
336 ret = ssl3_get_client_hello(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800337 if (ret <= 0) {
338 goto end;
339 }
340 s->renegotiate = 2;
341 s->state = SSL3_ST_SW_SRVR_HELLO_A;
342 s->init_num = 0;
343 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500344
Adam Langleyfcf25832014-12-18 17:42:32 -0800345 case SSL3_ST_SW_SRVR_HELLO_A:
346 case SSL3_ST_SW_SRVR_HELLO_B:
347 ret = ssl3_send_server_hello(s);
348 if (ret <= 0) {
349 goto end;
350 }
351 if (s->hit) {
352 if (s->tlsext_ticket_expected) {
353 s->state = SSL3_ST_SW_SESSION_TICKET_A;
354 } else {
355 s->state = SSL3_ST_SW_CHANGE_A;
356 }
357 } else {
358 s->state = SSL3_ST_SW_CERT_A;
359 }
360 s->init_num = 0;
361 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700362
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 case SSL3_ST_SW_CERT_A:
364 case SSL3_ST_SW_CERT_B:
365 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
366 ret = ssl3_send_server_certificate(s);
367 if (ret <= 0) {
368 goto end;
369 }
370 if (s->s3->tmp.certificate_status_expected) {
371 s->state = SSL3_ST_SW_CERT_STATUS_A;
372 } else {
373 s->state = SSL3_ST_SW_KEY_EXCH_A;
374 }
375 } else {
376 skip = 1;
377 s->state = SSL3_ST_SW_KEY_EXCH_A;
378 }
379 s->init_num = 0;
380 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700381
Adam Langleyfcf25832014-12-18 17:42:32 -0800382 case SSL3_ST_SW_KEY_EXCH_A:
383 case SSL3_ST_SW_KEY_EXCH_B:
384 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 /* Send a ServerKeyExchange message if:
387 * - The key exchange is ephemeral or anonymous
388 * Diffie-Hellman.
389 * - There is a PSK identity hint.
390 *
391 * TODO(davidben): This logic is currently duplicated in d1_srvr.c. Fix
392 * this. In the meantime, keep them in sync. */
393 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) ||
394 ((alg_a & SSL_aPSK) && s->psk_identity_hint)) {
395 ret = ssl3_send_server_key_exchange(s);
David Benjamin6eb000d2015-02-11 01:17:41 -0500396 if (ret <= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500398 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 } else {
400 skip = 1;
401 }
Adam Langley95c29f32014-06-20 12:00:00 -0700402
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 s->state = SSL3_ST_SW_CERT_REQ_A;
404 s->init_num = 0;
405 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700406
Adam Langleyfcf25832014-12-18 17:42:32 -0800407 case SSL3_ST_SW_CERT_REQ_A:
408 case SSL3_ST_SW_CERT_REQ_B:
409 if (/* don't request cert unless asked for it: */
410 !(s->verify_mode & SSL_VERIFY_PEER) ||
411 /* Don't request a certificate if an obc was presented */
412 ((s->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
413 s->s3->tlsext_channel_id_valid) ||
414 /* if SSL_VERIFY_CLIENT_ONCE is set,
415 * don't request cert during re-negotiation: */
416 ((s->session->peer != NULL) &&
417 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 /* With normal PSK Certificates and
419 * Certificate Requests are omitted */
420 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
421 /* no cert request */
422 skip = 1;
423 s->s3->tmp.cert_request = 0;
424 s->state = SSL3_ST_SW_SRVR_DONE_A;
425 if (s->s3->handshake_buffer &&
426 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
427 return -1;
428 }
429 } else {
430 s->s3->tmp.cert_request = 1;
431 ret = ssl3_send_certificate_request(s);
432 if (ret <= 0) {
433 goto end;
434 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800435 s->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800436 s->init_num = 0;
437 }
438 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700439
Adam Langleyfcf25832014-12-18 17:42:32 -0800440 case SSL3_ST_SW_SRVR_DONE_A:
441 case SSL3_ST_SW_SRVR_DONE_B:
442 ret = ssl3_send_server_done(s);
443 if (ret <= 0) {
444 goto end;
445 }
446 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
447 s->state = SSL3_ST_SW_FLUSH;
448 s->init_num = 0;
449 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700450
Adam Langleyfcf25832014-12-18 17:42:32 -0800451 case SSL3_ST_SW_FLUSH:
452 /* This code originally checked to see if any data was pending using
453 * BIO_CTRL_INFO and then flushed. This caused problems as documented
454 * in PR#1939. The proposed fix doesn't completely resolve this issue
455 * as buggy implementations of BIO_CTRL_PENDING still exist. So instead
456 * we just flush unconditionally. */
457 s->rwstate = SSL_WRITING;
458 if (BIO_flush(s->wbio) <= 0) {
459 ret = -1;
460 goto end;
461 }
462 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700463
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 s->state = s->s3->tmp.next_state;
465 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700466
Adam Langleyfcf25832014-12-18 17:42:32 -0800467 case SSL3_ST_SR_CERT_A:
468 case SSL3_ST_SR_CERT_B:
469 if (s->s3->tmp.cert_request) {
470 ret = ssl3_get_client_certificate(s);
471 if (ret <= 0) {
472 goto end;
473 }
474 }
475 s->init_num = 0;
476 s->state = SSL3_ST_SR_KEY_EXCH_A;
477 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700478
Adam Langleyfcf25832014-12-18 17:42:32 -0800479 case SSL3_ST_SR_KEY_EXCH_A:
480 case SSL3_ST_SR_KEY_EXCH_B:
481 ret = ssl3_get_client_key_exchange(s);
482 if (ret <= 0) {
483 goto end;
484 }
485 s->state = SSL3_ST_SR_CERT_VRFY_A;
486 s->init_num = 0;
487 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700488
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 case SSL3_ST_SR_CERT_VRFY_A:
490 case SSL3_ST_SR_CERT_VRFY_B:
491 ret = ssl3_get_cert_verify(s);
492 if (ret <= 0) {
493 goto end;
494 }
Adam Langley95c29f32014-06-20 12:00:00 -0700495
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 s->state = SSL3_ST_SR_CHANGE;
497 s->init_num = 0;
498 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700499
Adam Langleyfcf25832014-12-18 17:42:32 -0800500 case SSL3_ST_SR_CHANGE: {
501 char next_proto_neg = 0;
502 char channel_id = 0;
503 next_proto_neg = s->s3->next_proto_neg_seen;
504 channel_id = s->s3->tlsext_channel_id_valid;
Adam Langley95c29f32014-06-20 12:00:00 -0700505
Adam Langleyfcf25832014-12-18 17:42:32 -0800506 /* At this point, the next message must be entirely behind a
507 * ChangeCipherSpec. */
508 if (!ssl3_expect_change_cipher_spec(s)) {
509 ret = -1;
510 goto end;
511 }
512 if (next_proto_neg) {
513 s->state = SSL3_ST_SR_NEXT_PROTO_A;
514 } else if (channel_id) {
515 s->state = SSL3_ST_SR_CHANNEL_ID_A;
516 } else {
517 s->state = SSL3_ST_SR_FINISHED_A;
518 }
519 break;
520 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700521
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 case SSL3_ST_SR_NEXT_PROTO_A:
523 case SSL3_ST_SR_NEXT_PROTO_B:
524 ret = ssl3_get_next_proto(s);
525 if (ret <= 0) {
526 goto end;
527 }
528 s->init_num = 0;
529 if (s->s3->tlsext_channel_id_valid) {
530 s->state = SSL3_ST_SR_CHANNEL_ID_A;
531 } else {
532 s->state = SSL3_ST_SR_FINISHED_A;
533 }
534 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700535
Adam Langleyfcf25832014-12-18 17:42:32 -0800536 case SSL3_ST_SR_CHANNEL_ID_A:
537 case SSL3_ST_SR_CHANNEL_ID_B:
538 ret = ssl3_get_channel_id(s);
539 if (ret <= 0) {
540 goto end;
541 }
542 s->init_num = 0;
543 s->state = SSL3_ST_SR_FINISHED_A;
544 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700545
Adam Langleyfcf25832014-12-18 17:42:32 -0800546 case SSL3_ST_SR_FINISHED_A:
547 case SSL3_ST_SR_FINISHED_B:
548 ret =
549 ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, SSL3_ST_SR_FINISHED_B);
550 if (ret <= 0) {
551 goto end;
552 }
Adam Langley95c29f32014-06-20 12:00:00 -0700553
Adam Langleyfcf25832014-12-18 17:42:32 -0800554 if (s->hit) {
555 s->state = SSL_ST_OK;
556 } else if (s->tlsext_ticket_expected) {
557 s->state = SSL3_ST_SW_SESSION_TICKET_A;
558 } else {
559 s->state = SSL3_ST_SW_CHANGE_A;
560 }
561 /* If this is a full handshake with ChannelID then record the hashshake
562 * hashes in |s->session| in case we need them to verify a ChannelID
563 * signature on a resumption of this session in the future. */
564 if (!s->hit && s->s3->tlsext_channel_id_new) {
565 ret = tls1_record_handshake_hashes_for_channel_id(s);
566 if (ret <= 0) {
567 goto end;
568 }
569 }
570 s->init_num = 0;
571 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700572
Adam Langleyfcf25832014-12-18 17:42:32 -0800573 case SSL3_ST_SW_SESSION_TICKET_A:
574 case SSL3_ST_SW_SESSION_TICKET_B:
575 ret = ssl3_send_new_session_ticket(s);
576 if (ret <= 0) {
577 goto end;
578 }
579 s->state = SSL3_ST_SW_CHANGE_A;
580 s->init_num = 0;
581 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700582
Adam Langleyfcf25832014-12-18 17:42:32 -0800583 case SSL3_ST_SW_CHANGE_A:
584 case SSL3_ST_SW_CHANGE_B:
585 s->session->cipher = s->s3->tmp.new_cipher;
586 if (!s->enc_method->setup_key_block(s)) {
587 ret = -1;
588 goto end;
589 }
Adam Langley95c29f32014-06-20 12:00:00 -0700590
Adam Langleyfcf25832014-12-18 17:42:32 -0800591 ret = ssl3_send_change_cipher_spec(s, SSL3_ST_SW_CHANGE_A,
592 SSL3_ST_SW_CHANGE_B);
593 if (ret <= 0) {
594 goto end;
595 }
596 s->state = SSL3_ST_SW_FINISHED_A;
597 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700598
Adam Langleyfcf25832014-12-18 17:42:32 -0800599 if (!s->enc_method->change_cipher_state(
600 s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
601 ret = -1;
602 goto end;
603 }
604 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700605
Adam Langleyfcf25832014-12-18 17:42:32 -0800606 case SSL3_ST_SW_FINISHED_A:
607 case SSL3_ST_SW_FINISHED_B:
608 ret =
609 ssl3_send_finished(s, SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B,
610 s->enc_method->server_finished_label,
611 s->enc_method->server_finished_label_len);
612 if (ret <= 0) {
613 goto end;
614 }
615 s->state = SSL3_ST_SW_FLUSH;
616 if (s->hit) {
617 s->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
618 } else {
619 s->s3->tmp.next_state = SSL_ST_OK;
620 }
621 s->init_num = 0;
622 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700623
Adam Langleyfcf25832014-12-18 17:42:32 -0800624 case SSL_ST_OK:
625 /* clean a few things up */
626 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700627
Adam Langleyfcf25832014-12-18 17:42:32 -0800628 BUF_MEM_free(s->init_buf);
629 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700630
Adam Langleyfcf25832014-12-18 17:42:32 -0800631 /* remove buffering on output */
632 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700633
Adam Langleyfcf25832014-12-18 17:42:32 -0800634 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 /* If we aren't retaining peer certificates then we can discard it
637 * now. */
David Benjamin2755a3e2015-04-22 16:17:58 -0400638 if (s->ctx->retain_only_sha256_of_client_certs) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800639 X509_free(s->session->peer);
640 s->session->peer = NULL;
641 }
Adam Langley95c29f32014-06-20 12:00:00 -0700642
Adam Langleyfcf25832014-12-18 17:42:32 -0800643 if (s->renegotiate == 2) {
644 /* skipped if we just sent a HelloRequest */
645 s->renegotiate = 0;
David Benjamine6df0542015-05-12 22:02:08 -0400646 s->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langleyfcf25832014-12-18 17:42:32 -0800648 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
Adam Langley95c29f32014-06-20 12:00:00 -0700649
Adam Langleyfcf25832014-12-18 17:42:32 -0800650 if (cb != NULL) {
651 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
652 }
653 }
Adam Langley75872532014-06-20 12:00:00 -0700654
Adam Langleyfcf25832014-12-18 17:42:32 -0800655 ret = 1;
656 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700657
Adam Langleyfcf25832014-12-18 17:42:32 -0800658 default:
659 OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNKNOWN_STATE);
660 ret = -1;
661 goto end;
662 }
Adam Langley95c29f32014-06-20 12:00:00 -0700663
Adam Langleyfcf25832014-12-18 17:42:32 -0800664 if (!s->s3->tmp.reuse_message && !skip && cb != NULL && s->state != state) {
665 new_state = s->state;
666 s->state = state;
667 cb(s, SSL_CB_ACCEPT_LOOP, 1);
668 s->state = new_state;
669 }
670 skip = 0;
671 }
672
Adam Langley95c29f32014-06-20 12:00:00 -0700673end:
Adam Langleyfcf25832014-12-18 17:42:32 -0800674 s->in_handshake--;
David Benjamin2755a3e2015-04-22 16:17:58 -0400675 BUF_MEM_free(buf);
Adam Langleyfcf25832014-12-18 17:42:32 -0800676 if (cb != NULL) {
677 cb(s, SSL_CB_ACCEPT_EXIT, ret);
678 }
679 return ret;
680}
Adam Langley95c29f32014-06-20 12:00:00 -0700681
Adam Langleyfcf25832014-12-18 17:42:32 -0800682static int ssl3_read_sniff_buffer(SSL *s, size_t n) {
683 if (s->s3->sniff_buffer == NULL) {
684 s->s3->sniff_buffer = BUF_MEM_new();
685 }
686 if (s->s3->sniff_buffer == NULL || !BUF_MEM_grow(s->s3->sniff_buffer, n)) {
687 return -1;
688 }
Adam Langley95c29f32014-06-20 12:00:00 -0700689
Adam Langleyfcf25832014-12-18 17:42:32 -0800690 while (s->s3->sniff_buffer_len < n) {
691 int ret;
David Benjamin4b755cb2014-12-12 03:58:07 -0500692
Adam Langleyfcf25832014-12-18 17:42:32 -0800693 s->rwstate = SSL_READING;
694 ret = BIO_read(s->rbio, s->s3->sniff_buffer->data + s->s3->sniff_buffer_len,
695 n - s->s3->sniff_buffer_len);
696 if (ret <= 0) {
697 return ret;
698 }
699 s->rwstate = SSL_NOTHING;
700 s->s3->sniff_buffer_len += ret;
701 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500702
Adam Langleyfcf25832014-12-18 17:42:32 -0800703 return 1;
704}
David Benjamin4b755cb2014-12-12 03:58:07 -0500705
Adam Langleyfcf25832014-12-18 17:42:32 -0800706int ssl3_get_initial_bytes(SSL *s) {
707 int ret;
708 const uint8_t *p;
David Benjamin4b755cb2014-12-12 03:58:07 -0500709
Adam Langleyfcf25832014-12-18 17:42:32 -0800710 /* Read the first 8 bytes. To recognize a ClientHello or V2ClientHello only
711 * needs the first 6 bytes, but 8 is needed to recognize CONNECT below. */
712 ret = ssl3_read_sniff_buffer(s, INITIAL_SNIFF_BUFFER_SIZE);
713 if (ret <= 0) {
714 return ret;
715 }
716 assert(s->s3->sniff_buffer_len >= INITIAL_SNIFF_BUFFER_SIZE);
717 p = (const uint8_t *)s->s3->sniff_buffer->data;
David Benjamin4b755cb2014-12-12 03:58:07 -0500718
Adam Langleyfcf25832014-12-18 17:42:32 -0800719 /* Some dedicated error codes for protocol mixups should the application wish
720 * to interpret them differently. (These do not overlap with ClientHello or
721 * V2ClientHello.) */
722 if (strncmp("GET ", (const char *)p, 4) == 0 ||
723 strncmp("POST ", (const char *)p, 5) == 0 ||
724 strncmp("HEAD ", (const char *)p, 5) == 0 ||
725 strncmp("PUT ", (const char *)p, 4) == 0) {
726 OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_HTTP_REQUEST);
727 return -1;
728 }
729 if (strncmp("CONNECT ", (const char *)p, 8) == 0) {
730 OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_HTTPS_PROXY_REQUEST);
731 return -1;
732 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500733
Adam Langleyfcf25832014-12-18 17:42:32 -0800734 /* Determine if this is a ClientHello or V2ClientHello. */
735 if ((p[0] & 0x80) && p[2] == SSL2_MT_CLIENT_HELLO &&
736 p[3] >= SSL3_VERSION_MAJOR) {
737 /* This is a V2ClientHello. */
738 s->state = SSL3_ST_SR_V2_CLIENT_HELLO;
739 return 1;
740 }
741 if (p[0] == SSL3_RT_HANDSHAKE && p[1] >= SSL3_VERSION_MAJOR &&
742 p[5] == SSL3_MT_CLIENT_HELLO) {
743 /* This is a ClientHello. Initialize the record layer with the already
744 * consumed data and continue the handshake. */
David Benjamin6a08da22015-05-08 22:58:12 -0400745 if (!ssl3_setup_read_buffer(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800746 return -1;
747 }
748 assert(s->rstate == SSL_ST_READ_HEADER);
David Benjamin6a08da22015-05-08 22:58:12 -0400749 /* There cannot have already been data in the record layer. */
750 assert(s->s3->rbuf.left == 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800751 memcpy(s->s3->rbuf.buf, p, s->s3->sniff_buffer_len);
752 s->s3->rbuf.offset = 0;
753 s->s3->rbuf.left = s->s3->sniff_buffer_len;
754 s->packet_length = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500755
Adam Langleyfcf25832014-12-18 17:42:32 -0800756 BUF_MEM_free(s->s3->sniff_buffer);
757 s->s3->sniff_buffer = NULL;
758 s->s3->sniff_buffer_len = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500759
Adam Langleyfcf25832014-12-18 17:42:32 -0800760 s->state = SSL3_ST_SR_CLNT_HELLO_A;
761 return 1;
762 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500763
Adam Langleyfcf25832014-12-18 17:42:32 -0800764 OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_UNKNOWN_PROTOCOL);
765 return -1;
766}
David Benjamin4b755cb2014-12-12 03:58:07 -0500767
Adam Langleyfcf25832014-12-18 17:42:32 -0800768int ssl3_get_v2_client_hello(SSL *s) {
769 const uint8_t *p;
770 int ret;
771 CBS v2_client_hello, cipher_specs, session_id, challenge;
772 size_t msg_length, rand_len, len;
773 uint8_t msg_type;
774 uint16_t version, cipher_spec_length, session_id_length, challenge_length;
775 CBB client_hello, hello_body, cipher_suites;
776 uint8_t random[SSL3_RANDOM_SIZE];
David Benjamin4b755cb2014-12-12 03:58:07 -0500777
Adam Langleyfcf25832014-12-18 17:42:32 -0800778 /* Read the remainder of the V2ClientHello. We have previously read 8 bytes
779 * in ssl3_get_initial_bytes. */
780 assert(s->s3->sniff_buffer_len >= INITIAL_SNIFF_BUFFER_SIZE);
781 p = (const uint8_t *)s->s3->sniff_buffer->data;
782 msg_length = ((p[0] & 0x7f) << 8) | p[1];
783 if (msg_length > (1024 * 4)) {
784 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_RECORD_TOO_LARGE);
785 return -1;
786 }
787 if (msg_length < INITIAL_SNIFF_BUFFER_SIZE - 2) {
788 /* Reject lengths that are too short early. We have already read 8 bytes,
789 * so we should not attempt to process an (invalid) V2ClientHello which
790 * would be shorter than that. */
791 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello,
792 SSL_R_RECORD_LENGTH_MISMATCH);
793 return -1;
794 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500795
Adam Langleyfcf25832014-12-18 17:42:32 -0800796 ret = ssl3_read_sniff_buffer(s, msg_length + 2);
797 if (ret <= 0) {
798 return ret;
799 }
800 assert(s->s3->sniff_buffer_len == msg_length + 2);
801 CBS_init(&v2_client_hello, (const uint8_t *)s->s3->sniff_buffer->data + 2,
802 msg_length);
David Benjamin4b755cb2014-12-12 03:58:07 -0500803
Adam Langleyfcf25832014-12-18 17:42:32 -0800804 /* The V2ClientHello without the length is incorporated into the Finished
805 * hash. */
David Benjaminfbdfefb2015-02-16 19:33:53 -0500806 if (!ssl3_finish_mac(s, CBS_data(&v2_client_hello),
807 CBS_len(&v2_client_hello))) {
808 return -1;
809 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800810 if (s->msg_callback) {
811 s->msg_callback(0, SSL2_VERSION, 0, CBS_data(&v2_client_hello),
812 CBS_len(&v2_client_hello), s, s->msg_callback_arg);
813 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500814
Adam Langleyfcf25832014-12-18 17:42:32 -0800815 if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
816 !CBS_get_u16(&v2_client_hello, &version) ||
817 !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
818 !CBS_get_u16(&v2_client_hello, &session_id_length) ||
819 !CBS_get_u16(&v2_client_hello, &challenge_length) ||
820 !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
821 !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
822 !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
823 CBS_len(&v2_client_hello) != 0) {
824 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_DECODE_ERROR);
825 return -1;
826 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500827
Adam Langleyfcf25832014-12-18 17:42:32 -0800828 /* msg_type has already been checked. */
829 assert(msg_type == SSL2_MT_CLIENT_HELLO);
David Benjamin4b755cb2014-12-12 03:58:07 -0500830
Adam Langleyfcf25832014-12-18 17:42:32 -0800831 /* The client_random is the V2ClientHello challenge. Truncate or
832 * left-pad with zeros as needed. */
833 memset(random, 0, SSL3_RANDOM_SIZE);
834 rand_len = CBS_len(&challenge);
835 if (rand_len > SSL3_RANDOM_SIZE) {
836 rand_len = SSL3_RANDOM_SIZE;
837 }
838 memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
839 rand_len);
David Benjamin4b755cb2014-12-12 03:58:07 -0500840
Adam Langleyfcf25832014-12-18 17:42:32 -0800841 /* Write out an equivalent SSLv3 ClientHello. */
842 if (!CBB_init_fixed(&client_hello, (uint8_t *)s->init_buf->data,
843 s->init_buf->max)) {
844 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_MALLOC_FAILURE);
845 return -1;
846 }
847 if (!CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
848 !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
849 !CBB_add_u16(&hello_body, version) ||
850 !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
851 /* No session id. */
852 !CBB_add_u8(&hello_body, 0) ||
853 !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
854 CBB_cleanup(&client_hello);
855 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
856 return -1;
857 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500858
Adam Langleyfcf25832014-12-18 17:42:32 -0800859 /* Copy the cipher suites. */
860 while (CBS_len(&cipher_specs) > 0) {
861 uint32_t cipher_spec;
862 if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
863 CBB_cleanup(&client_hello);
864 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_DECODE_ERROR);
865 return -1;
866 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500867
Adam Langleyfcf25832014-12-18 17:42:32 -0800868 /* Skip SSLv2 ciphers. */
869 if ((cipher_spec & 0xff0000) != 0) {
870 continue;
871 }
872 if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
873 CBB_cleanup(&client_hello);
874 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
875 return -1;
876 }
877 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500878
Adam Langleyfcf25832014-12-18 17:42:32 -0800879 /* Add the null compression scheme and finish. */
880 if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) ||
881 !CBB_finish(&client_hello, NULL, &len)) {
882 CBB_cleanup(&client_hello);
883 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
884 return -1;
885 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500886
Adam Langleyfcf25832014-12-18 17:42:32 -0800887 /* Mark the message for "re"-use by the version-specific method. */
888 s->s3->tmp.reuse_message = 1;
889 s->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO;
890 /* The handshake message header is 4 bytes. */
891 s->s3->tmp.message_size = len - 4;
David Benjamin4b755cb2014-12-12 03:58:07 -0500892
Adam Langleyfcf25832014-12-18 17:42:32 -0800893 /* Drop the sniff buffer. */
894 BUF_MEM_free(s->s3->sniff_buffer);
895 s->s3->sniff_buffer = NULL;
896 s->s3->sniff_buffer_len = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500897
Adam Langleyfcf25832014-12-18 17:42:32 -0800898 return 1;
899}
David Benjamin4b755cb2014-12-12 03:58:07 -0500900
Adam Langleyfcf25832014-12-18 17:42:32 -0800901int ssl3_send_hello_request(SSL *s) {
902 if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
David Benjaminfbdfefb2015-02-16 19:33:53 -0500903 if (!ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0)) {
904 return -1;
905 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800906 s->state = SSL3_ST_SW_HELLO_REQ_B;
907 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500908
Adam Langleyfcf25832014-12-18 17:42:32 -0800909 /* SSL3_ST_SW_HELLO_REQ_B */
910 return ssl_do_write(s);
911}
Adam Langley95c29f32014-06-20 12:00:00 -0700912
Adam Langleyfcf25832014-12-18 17:42:32 -0800913int ssl3_get_client_hello(SSL *s) {
914 int i, ok, al = SSL_AD_INTERNAL_ERROR, ret = -1;
915 long n;
916 const SSL_CIPHER *c;
917 STACK_OF(SSL_CIPHER) *ciphers = NULL;
918 struct ssl_early_callback_ctx early_ctx;
919 CBS client_hello;
920 uint16_t client_version;
921 CBS client_random, session_id, cipher_suites, compression_methods;
Adam Langley95c29f32014-06-20 12:00:00 -0700922
Adam Langleyfcf25832014-12-18 17:42:32 -0800923 /* We do this so that we will respond with our native type. If we are TLSv1
924 * and we get SSLv3, we will respond with TLSv1, This down switching should
925 * be handled by a different method. If we are SSLv3, we will respond with
926 * SSLv3, even if prompted with TLSv1. */
927 switch (s->state) {
928 case SSL3_ST_SR_CLNT_HELLO_A:
929 case SSL3_ST_SR_CLNT_HELLO_B:
930 n = s->method->ssl_get_message(
931 s, SSL3_ST_SR_CLNT_HELLO_A, SSL3_ST_SR_CLNT_HELLO_B,
932 SSL3_MT_CLIENT_HELLO, SSL3_RT_MAX_PLAIN_LENGTH,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500933 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700934
Adam Langleyfcf25832014-12-18 17:42:32 -0800935 if (!ok) {
936 return n;
937 }
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langleyfcf25832014-12-18 17:42:32 -0800939 s->state = SSL3_ST_SR_CLNT_HELLO_C;
940 /* fallthrough */
941 case SSL3_ST_SR_CLNT_HELLO_C:
942 case SSL3_ST_SR_CLNT_HELLO_D:
943 /* We have previously parsed the ClientHello message, and can't call
944 * ssl_get_message again without hashing the message into the Finished
945 * digest again. */
946 n = s->init_num;
Adam Langleydc9b1412014-06-20 12:00:00 -0700947
Adam Langleyfcf25832014-12-18 17:42:32 -0800948 memset(&early_ctx, 0, sizeof(early_ctx));
949 early_ctx.ssl = s;
950 early_ctx.client_hello = s->init_msg;
951 early_ctx.client_hello_len = n;
952 if (!ssl_early_callback_init(&early_ctx)) {
953 al = SSL_AD_DECODE_ERROR;
954 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
955 SSL_R_CLIENTHELLO_PARSE_FAILED);
956 goto f_err;
957 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400958
Adam Langleyfcf25832014-12-18 17:42:32 -0800959 if (s->state == SSL3_ST_SR_CLNT_HELLO_C &&
960 s->ctx->select_certificate_cb != NULL) {
961 s->state = SSL3_ST_SR_CLNT_HELLO_D;
962 switch (s->ctx->select_certificate_cb(&early_ctx)) {
963 case 0:
David Benjamin2fff5bf2015-02-09 04:32:28 -0500964 s->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
965 goto err;
Adam Langleydc9b1412014-06-20 12:00:00 -0700966
Adam Langleyfcf25832014-12-18 17:42:32 -0800967 case -1:
968 /* Connection rejected. */
969 al = SSL_AD_ACCESS_DENIED;
970 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
971 SSL_R_CONNECTION_REJECTED);
972 goto f_err;
Adam Langleydc9b1412014-06-20 12:00:00 -0700973
Adam Langleyfcf25832014-12-18 17:42:32 -0800974 default:
975 /* fallthrough */;
976 }
977 }
978 s->state = SSL3_ST_SR_CLNT_HELLO_D;
979 break;
Adam Langleyb2ce0582014-06-20 12:00:00 -0700980
Adam Langleyfcf25832014-12-18 17:42:32 -0800981 default:
982 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNKNOWN_STATE);
983 return -1;
984 }
Adam Langley95c29f32014-06-20 12:00:00 -0700985
Adam Langleyfcf25832014-12-18 17:42:32 -0800986 CBS_init(&client_hello, s->init_msg, n);
987 if (!CBS_get_u16(&client_hello, &client_version) ||
988 !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) ||
989 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
990 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
991 al = SSL_AD_DECODE_ERROR;
992 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
993 goto f_err;
994 }
Adam Langley95c29f32014-06-20 12:00:00 -0700995
Adam Langleyfcf25832014-12-18 17:42:32 -0800996 /* use version from inside client hello, not from record header (may differ:
997 * see RFC 2246, Appendix E, second paragraph) */
998 s->client_version = client_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700999
Adam Langleyfcf25832014-12-18 17:42:32 -08001000 /* Load the client random. */
1001 memcpy(s->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -07001002
Adam Langleyfcf25832014-12-18 17:42:32 -08001003 if (SSL_IS_DTLS(s)) {
1004 CBS cookie;
Adam Langley95c29f32014-06-20 12:00:00 -07001005
Adam Langleyfcf25832014-12-18 17:42:32 -08001006 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
1007 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
1008 al = SSL_AD_DECODE_ERROR;
1009 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
1010 goto f_err;
1011 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001012 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001013
David Benjamine6df0542015-05-12 22:02:08 -04001014 /* Note: This codepath may run twice if |ssl_get_prev_session| completes
1015 * asynchronously.
1016 *
1017 * TODO(davidben): Clean up the order of events around ClientHello
1018 * processing. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001019 if (!s->s3->have_version) {
1020 /* Select version to use */
1021 uint16_t version = ssl3_get_mutual_version(s, client_version);
1022 if (version == 0) {
1023 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL);
1024 s->version = s->client_version;
1025 al = SSL_AD_PROTOCOL_VERSION;
1026 goto f_err;
1027 }
1028 s->version = version;
1029 s->enc_method = ssl3_get_enc_method(version);
1030 assert(s->enc_method != NULL);
1031 /* At this point, the connection's version is known and |s->version| is
1032 * fixed. Begin enforcing the record-layer version. */
1033 s->s3->have_version = 1;
1034 } else if (SSL_IS_DTLS(s) ? (s->client_version > s->version)
1035 : (s->client_version < s->version)) {
1036 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER);
1037 al = SSL_AD_PROTOCOL_VERSION;
1038 goto f_err;
1039 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001040
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 s->hit = 0;
David Benjamin4b27d9f2015-05-12 22:42:52 -04001042 if (s->s3->initial_handshake_complete) {
1043 /* Renegotiations do not participate in session resumption. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001044 if (!ssl_get_new_session(s, 1)) {
1045 goto err;
1046 }
1047 } else {
1048 i = ssl_get_prev_session(s, &early_ctx);
1049 if (i == PENDING_SESSION) {
David Benjamin2fff5bf2015-02-09 04:32:28 -05001050 s->rwstate = SSL_PENDING_SESSION;
Adam Langleyfcf25832014-12-18 17:42:32 -08001051 goto err;
1052 } else if (i == -1) {
1053 goto err;
1054 }
Adam Langley95c29f32014-06-20 12:00:00 -07001055
Adam Langleyfcf25832014-12-18 17:42:32 -08001056 /* Only resume if the session's version matches the negotiated version:
1057 * most clients do not accept a mismatch. */
1058 if (i == 1 && s->version == s->session->ssl_version) {
1059 s->hit = 1;
1060 } else {
1061 /* No session was found or it was unacceptable. */
1062 if (!ssl_get_new_session(s, 1)) {
1063 goto err;
1064 }
1065 }
1066 }
David Benjamin22f9bcc2014-07-13 12:29:21 -04001067
Adam Langley524e7172015-02-20 16:04:00 -08001068 if (s->ctx->dos_protection_cb != NULL && s->ctx->dos_protection_cb(&early_ctx) == 0) {
1069 /* Connection rejected for DOS reasons. */
1070 al = SSL_AD_ACCESS_DENIED;
1071 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CONNECTION_REJECTED);
1072 goto f_err;
1073 }
1074
Adam Langleyfcf25832014-12-18 17:42:32 -08001075 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamin93de5e52015-04-17 22:33:25 -04001076 CBS_len(&cipher_suites) == 0 ||
1077 CBS_len(&cipher_suites) % 2 != 0 ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001078 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
1079 CBS_len(&compression_methods) == 0) {
1080 al = SSL_AD_DECODE_ERROR;
1081 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
1082 goto f_err;
1083 }
David Benjamin22f9bcc2014-07-13 12:29:21 -04001084
Adam Langleyfcf25832014-12-18 17:42:32 -08001085 ciphers = ssl_bytes_to_cipher_list(s, &cipher_suites);
1086 if (ciphers == NULL) {
1087 goto err;
1088 }
Adam Langley95c29f32014-06-20 12:00:00 -07001089
Adam Langleyfcf25832014-12-18 17:42:32 -08001090 /* If it is a hit, check that the cipher is in the list. */
David Benjamin93de5e52015-04-17 22:33:25 -04001091 if (s->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001092 size_t j;
1093 int found_cipher = 0;
David Benjamin3fa27772015-04-17 22:32:19 -04001094 uint32_t id = s->session->cipher->id;
Adam Langley95c29f32014-06-20 12:00:00 -07001095
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++) {
1097 c = sk_SSL_CIPHER_value(ciphers, j);
1098 if (c->id == id) {
1099 found_cipher = 1;
1100 break;
1101 }
1102 }
Adam Langley95c29f32014-06-20 12:00:00 -07001103
Adam Langleyfcf25832014-12-18 17:42:32 -08001104 if (!found_cipher) {
1105 /* we need to have the cipher in the cipher list if we are asked to reuse
1106 * it */
1107 al = SSL_AD_ILLEGAL_PARAMETER;
1108 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
1109 SSL_R_REQUIRED_CIPHER_MISSING);
1110 goto f_err;
1111 }
1112 }
Adam Langley95c29f32014-06-20 12:00:00 -07001113
Adam Langleyfcf25832014-12-18 17:42:32 -08001114 /* Only null compression is supported. */
1115 if (memchr(CBS_data(&compression_methods), 0,
1116 CBS_len(&compression_methods)) == NULL) {
1117 al = SSL_AD_ILLEGAL_PARAMETER;
1118 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
1119 SSL_R_NO_COMPRESSION_SPECIFIED);
1120 goto f_err;
1121 }
David Benjamindc72ff72014-06-25 12:36:10 -04001122
Adam Langleyfcf25832014-12-18 17:42:32 -08001123 /* TLS extensions. */
1124 if (s->version >= SSL3_VERSION &&
1125 !ssl_parse_clienthello_tlsext(s, &client_hello)) {
1126 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_PARSE_TLSEXT);
1127 goto err;
1128 }
Adam Langley95c29f32014-06-20 12:00:00 -07001129
Adam Langleyfcf25832014-12-18 17:42:32 -08001130 /* There should be nothing left over in the record. */
1131 if (CBS_len(&client_hello) != 0) {
1132 /* wrong packet length */
1133 al = SSL_AD_DECODE_ERROR;
1134 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_BAD_PACKET_LENGTH);
1135 goto f_err;
1136 }
Adam Langley95c29f32014-06-20 12:00:00 -07001137
Adam Langleyfcf25832014-12-18 17:42:32 -08001138 /* Given ciphers and SSL_get_ciphers, we must pick a cipher */
1139 if (!s->hit) {
1140 if (ciphers == NULL) {
1141 al = SSL_AD_ILLEGAL_PARAMETER;
1142 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_PASSED);
1143 goto f_err;
1144 }
Adam Langley95c29f32014-06-20 12:00:00 -07001145
Adam Langleyfcf25832014-12-18 17:42:32 -08001146 /* Let cert callback update server certificates if required */
1147 if (s->cert->cert_cb) {
1148 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1149 if (rv == 0) {
1150 al = SSL_AD_INTERNAL_ERROR;
1151 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CERT_CB_ERROR);
1152 goto f_err;
1153 }
1154 if (rv < 0) {
1155 s->rwstate = SSL_X509_LOOKUP;
1156 goto err;
1157 }
1158 s->rwstate = SSL_NOTHING;
1159 }
1160 c = ssl3_choose_cipher(s, ciphers, ssl_get_cipher_preferences(s));
1161
1162 if (c == NULL) {
1163 al = SSL_AD_HANDSHAKE_FAILURE;
1164 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER);
1165 goto f_err;
1166 }
1167 s->s3->tmp.new_cipher = c;
1168 } else {
1169 /* Session-id reuse */
1170 s->s3->tmp.new_cipher = s->session->cipher;
1171 }
1172
1173 if ((!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) &&
1174 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1175 goto f_err;
1176 }
1177
1178 /* we now have the following setup;
1179 * client_random
1180 * cipher_list - our prefered list of ciphers
1181 * ciphers - the clients prefered list of ciphers
1182 * compression - basically ignored right now
1183 * ssl version is set - sslv3
1184 * s->session - The ssl session has been setup.
1185 * s->hit - session reuse flag
1186 * s->tmp.new_cipher - the new cipher to use. */
1187
1188 if (ret < 0) {
1189 ret = -ret;
1190 }
1191
1192 if (0) {
1193 f_err:
1194 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1195 }
1196
Adam Langley95c29f32014-06-20 12:00:00 -07001197err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001198 sk_SSL_CIPHER_free(ciphers);
Adam Langleyfcf25832014-12-18 17:42:32 -08001199 return ret;
1200}
Adam Langley95c29f32014-06-20 12:00:00 -07001201
Adam Langleyfcf25832014-12-18 17:42:32 -08001202int ssl3_send_server_hello(SSL *s) {
1203 uint8_t *buf;
1204 uint8_t *p, *d;
1205 int sl;
1206 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001207
Adam Langleyfcf25832014-12-18 17:42:32 -08001208 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1209 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1210 * known attack while we fix ChannelID itself. */
1211 if (s->s3->tlsext_channel_id_valid &&
David Benjamin7061e282015-03-19 11:10:48 -04001212 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001213 s->s3->tlsext_channel_id_valid = 0;
1214 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001215
Adam Langleyfcf25832014-12-18 17:42:32 -08001216 /* If this is a resumption and the original handshake didn't support
1217 * ChannelID then we didn't record the original handshake hashes in the
1218 * session and so cannot resume with ChannelIDs. */
1219 if (s->hit && s->s3->tlsext_channel_id_new &&
1220 s->session->original_handshake_hash_len == 0) {
1221 s->s3->tlsext_channel_id_valid = 0;
1222 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001223
Adam Langleyfcf25832014-12-18 17:42:32 -08001224 buf = (uint8_t *)s->init_buf->data;
1225 /* Do the message type and length last */
1226 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001227
Adam Langleyfcf25832014-12-18 17:42:32 -08001228 *(p++) = s->version >> 8;
1229 *(p++) = s->version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -07001230
Adam Langleyfcf25832014-12-18 17:42:32 -08001231 /* Random stuff */
1232 if (!ssl_fill_hello_random(s, 1, s->s3->server_random, SSL3_RANDOM_SIZE)) {
1233 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1234 return -1;
1235 }
1236 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1237 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -07001238
Adam Langleyfcf25832014-12-18 17:42:32 -08001239 /* There are several cases for the session ID to send
1240 * back in the server hello:
1241 * - For session reuse from the session cache, we send back the old session
1242 * ID.
1243 * - If stateless session reuse (using a session ticket) is successful, we
1244 * send back the client's "session ID" (which doesn't actually identify
1245 * the session).
1246 * - If it is a new session, we send back the new session ID.
1247 * - However, if we want the new session to be single-use, we send back a
1248 * 0-length session ID.
1249 * s->hit is non-zero in either case of session reuse, so the following
1250 * won't overwrite an ID that we're supposed to send back. */
1251 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) && !s->hit) {
1252 s->session->session_id_length = 0;
1253 }
Adam Langley95c29f32014-06-20 12:00:00 -07001254
Adam Langleyfcf25832014-12-18 17:42:32 -08001255 sl = s->session->session_id_length;
1256 if (sl > (int)sizeof(s->session->session_id)) {
1257 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1258 return -1;
1259 }
1260 *(p++) = sl;
1261 memcpy(p, s->session->session_id, sl);
1262 p += sl;
Adam Langley95c29f32014-06-20 12:00:00 -07001263
Adam Langleyfcf25832014-12-18 17:42:32 -08001264 /* put the cipher */
1265 s2n(ssl3_get_cipher_value(s->s3->tmp.new_cipher), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001266
Adam Langleyfcf25832014-12-18 17:42:32 -08001267 /* put the compression method */
1268 *(p++) = 0;
1269 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1270 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, SSL_R_SERVERHELLO_TLSEXT);
1271 return -1;
1272 }
1273 p = ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH);
1274 if (p == NULL) {
1275 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1276 return -1;
1277 }
Adam Langley95c29f32014-06-20 12:00:00 -07001278
Adam Langleyfcf25832014-12-18 17:42:32 -08001279 /* do the header */
1280 l = (p - d);
David Benjaminfbdfefb2015-02-16 19:33:53 -05001281 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l)) {
1282 return -1;
1283 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001284 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1285 }
Adam Langley95c29f32014-06-20 12:00:00 -07001286
Adam Langleyfcf25832014-12-18 17:42:32 -08001287 /* SSL3_ST_SW_SRVR_HELLO_B */
1288 return ssl_do_write(s);
1289}
Adam Langley95c29f32014-06-20 12:00:00 -07001290
Adam Langleyfcf25832014-12-18 17:42:32 -08001291int ssl3_send_server_done(SSL *s) {
1292 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
David Benjaminfbdfefb2015-02-16 19:33:53 -05001293 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0)) {
1294 return -1;
1295 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001296 s->state = SSL3_ST_SW_SRVR_DONE_B;
1297 }
Adam Langley95c29f32014-06-20 12:00:00 -07001298
Adam Langleyfcf25832014-12-18 17:42:32 -08001299 /* SSL3_ST_SW_SRVR_DONE_B */
1300 return ssl_do_write(s);
1301}
Adam Langley95c29f32014-06-20 12:00:00 -07001302
Adam Langleyfcf25832014-12-18 17:42:32 -08001303int ssl3_send_server_key_exchange(SSL *s) {
1304 DH *dh = NULL, *dhp;
David Benjaminc0f763b2015-03-27 02:05:39 -04001305 EC_KEY *ecdh = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001306 uint8_t *encodedPoint = NULL;
1307 int encodedlen = 0;
David Benjamin70bd80a2014-12-27 03:06:46 -05001308 uint16_t curve_id = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001309 BN_CTX *bn_ctx = NULL;
1310 const char *psk_identity_hint = NULL;
1311 size_t psk_identity_hint_len = 0;
1312 EVP_PKEY *pkey;
1313 uint8_t *p, *d;
1314 int al, i;
David Benjamin3fa27772015-04-17 22:32:19 -04001315 uint32_t alg_k;
1316 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001317 int n;
1318 CERT *cert;
1319 BIGNUM *r[4];
1320 int nr[4], kn;
1321 BUF_MEM *buf;
1322 EVP_MD_CTX md_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07001323
Adam Langleyfcf25832014-12-18 17:42:32 -08001324 EVP_MD_CTX_init(&md_ctx);
1325 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1326 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1327 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1328 cert = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07001329
Adam Langleyfcf25832014-12-18 17:42:32 -08001330 buf = s->init_buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001331
Adam Langleyfcf25832014-12-18 17:42:32 -08001332 r[0] = r[1] = r[2] = r[3] = NULL;
1333 n = 0;
1334 if (alg_a & SSL_aPSK) {
1335 /* size for PSK identity hint */
1336 psk_identity_hint = s->psk_identity_hint;
1337 if (psk_identity_hint) {
1338 psk_identity_hint_len = strlen(psk_identity_hint);
1339 } else {
1340 psk_identity_hint_len = 0;
1341 }
1342 n += 2 + psk_identity_hint_len;
1343 }
Adam Langley95c29f32014-06-20 12:00:00 -07001344
David Benjamin7061e282015-03-19 11:10:48 -04001345 if (alg_k & SSL_kDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001346 dhp = cert->dh_tmp;
1347 if (dhp == NULL && s->cert->dh_tmp_cb != NULL) {
1348 dhp = s->cert->dh_tmp_cb(s, 0, 1024);
1349 }
1350 if (dhp == NULL) {
1351 al = SSL_AD_HANDSHAKE_FAILURE;
1352 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1353 SSL_R_MISSING_TMP_DH_KEY);
1354 goto f_err;
1355 }
Adam Langley95c29f32014-06-20 12:00:00 -07001356
Adam Langleyfcf25832014-12-18 17:42:32 -08001357 if (s->s3->tmp.dh != NULL) {
1358 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1359 ERR_R_INTERNAL_ERROR);
1360 goto err;
1361 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001362 dh = DHparams_dup(dhp);
1363 if (dh == NULL) {
1364 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1365 goto err;
1366 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001367 s->s3->tmp.dh = dh;
David Benjamin9f226a52015-04-26 22:12:32 -04001368
1369 if (!DH_generate_key(dh)) {
1370 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1371 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001372 }
Adam Langley95c29f32014-06-20 12:00:00 -07001373
Adam Langleyfcf25832014-12-18 17:42:32 -08001374 r[0] = dh->p;
1375 r[1] = dh->g;
1376 r[2] = dh->pub_key;
David Benjamin7061e282015-03-19 11:10:48 -04001377 } else if (alg_k & SSL_kECDHE) {
David Benjaminc0f763b2015-03-27 02:05:39 -04001378 /* Determine the curve to use. */
1379 int nid = NID_undef;
David Benjamindd978782015-04-24 15:20:13 -04001380 if (cert->ecdh_nid != NID_undef) {
David Benjaminc0f763b2015-03-27 02:05:39 -04001381 nid = cert->ecdh_nid;
1382 } else if (cert->ecdh_tmp_cb != NULL) {
1383 /* Note: |ecdh_tmp_cb| does NOT pass ownership of the result
1384 * to the caller. */
1385 EC_KEY *template = s->cert->ecdh_tmp_cb(s, 0, 1024);
1386 if (template != NULL && EC_KEY_get0_group(template) != NULL) {
1387 nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(template));
Adam Langleyfcf25832014-12-18 17:42:32 -08001388 }
David Benjamindd978782015-04-24 15:20:13 -04001389 } else {
1390 nid = tls1_get_shared_curve(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08001391 }
David Benjaminc0f763b2015-03-27 02:05:39 -04001392 if (nid == NID_undef) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001393 al = SSL_AD_HANDSHAKE_FAILURE;
1394 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1395 SSL_R_MISSING_TMP_ECDH_KEY);
1396 goto f_err;
1397 }
Adam Langley95c29f32014-06-20 12:00:00 -07001398
Adam Langleyfcf25832014-12-18 17:42:32 -08001399 if (s->s3->tmp.ecdh != NULL) {
1400 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1401 ERR_R_INTERNAL_ERROR);
1402 goto err;
1403 }
David Benjaminc0f763b2015-03-27 02:05:39 -04001404 ecdh = EC_KEY_new_by_curve_name(nid);
1405 if (ecdh == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001406 goto err;
1407 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001408 s->s3->tmp.ecdh = ecdh;
Adam Langley95c29f32014-06-20 12:00:00 -07001409
David Benjaminc0f763b2015-03-27 02:05:39 -04001410 if (!EC_KEY_generate_key(ecdh)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001411 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1412 goto err;
1413 }
Adam Langley95c29f32014-06-20 12:00:00 -07001414
David Benjamin70bd80a2014-12-27 03:06:46 -05001415 /* We only support ephemeral ECDH keys over named (not generic) curves. */
David Benjaminc0f763b2015-03-27 02:05:39 -04001416 const EC_GROUP *group = EC_KEY_get0_group(ecdh);
David Benjamin70bd80a2014-12-27 03:06:46 -05001417 if (!tls1_ec_nid2curve_id(&curve_id, EC_GROUP_get_curve_name(group))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001418 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1419 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1420 goto err;
1421 }
Adam Langley95c29f32014-06-20 12:00:00 -07001422
Adam Langleyfcf25832014-12-18 17:42:32 -08001423 /* Encode the public key. First check the size of encoding and allocate
1424 * memory accordingly. */
1425 encodedlen =
1426 EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
1427 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001428
Adam Langleyfcf25832014-12-18 17:42:32 -08001429 encodedPoint = (uint8_t *)OPENSSL_malloc(encodedlen * sizeof(uint8_t));
1430 bn_ctx = BN_CTX_new();
1431 if (encodedPoint == NULL || bn_ctx == NULL) {
1432 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1433 ERR_R_MALLOC_FAILURE);
1434 goto err;
1435 }
Adam Langley95c29f32014-06-20 12:00:00 -07001436
Adam Langleyfcf25832014-12-18 17:42:32 -08001437 encodedlen = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
1438 POINT_CONVERSION_UNCOMPRESSED,
1439 encodedPoint, encodedlen, bn_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07001440
Adam Langleyfcf25832014-12-18 17:42:32 -08001441 if (encodedlen == 0) {
1442 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1443 goto err;
1444 }
Adam Langley95c29f32014-06-20 12:00:00 -07001445
Adam Langleyfcf25832014-12-18 17:42:32 -08001446 BN_CTX_free(bn_ctx);
1447 bn_ctx = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001448
Adam Langleyfcf25832014-12-18 17:42:32 -08001449 /* We only support named (not generic) curves in ECDH ephemeral key
1450 * exchanges. In this situation, we need four additional bytes to encode
1451 * the entire ServerECDHParams structure. */
1452 n += 4 + encodedlen;
Adam Langley95c29f32014-06-20 12:00:00 -07001453
Adam Langleyfcf25832014-12-18 17:42:32 -08001454 /* We'll generate the serverKeyExchange message explicitly so we can set
1455 * these to NULLs */
1456 r[0] = NULL;
1457 r[1] = NULL;
1458 r[2] = NULL;
1459 r[3] = NULL;
1460 } else if (!(alg_k & SSL_kPSK)) {
1461 al = SSL_AD_HANDSHAKE_FAILURE;
1462 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1463 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1464 goto f_err;
1465 }
Adam Langley95c29f32014-06-20 12:00:00 -07001466
Adam Langleyfcf25832014-12-18 17:42:32 -08001467 for (i = 0; i < 4 && r[i] != NULL; i++) {
1468 nr[i] = BN_num_bytes(r[i]);
1469 n += 2 + nr[i];
1470 }
Adam Langley95c29f32014-06-20 12:00:00 -07001471
Adam Langleyfcf25832014-12-18 17:42:32 -08001472 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1473 pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher);
1474 if (pkey == NULL) {
1475 al = SSL_AD_DECODE_ERROR;
1476 goto f_err;
1477 }
1478 kn = EVP_PKEY_size(pkey);
1479 } else {
1480 pkey = NULL;
1481 kn = 0;
1482 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001483
Adam Langleyfcf25832014-12-18 17:42:32 -08001484 if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) {
1485 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_BUF);
1486 goto err;
1487 }
1488 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001489
Adam Langleyfcf25832014-12-18 17:42:32 -08001490 for (i = 0; i < 4 && r[i] != NULL; i++) {
1491 s2n(nr[i], p);
1492 BN_bn2bin(r[i], p);
1493 p += nr[i];
1494 }
David Benjamin00505ec2014-11-26 16:38:00 -05001495
David Benjamin7061e282015-03-19 11:10:48 -04001496 /* Note: ECDHE PSK ciphersuites use SSL_kECDHE and SSL_aPSK. When one of
Adam Langleyfcf25832014-12-18 17:42:32 -08001497 * them is used, the server key exchange record needs to have both the
1498 * psk_identity_hint and the ServerECDHParams. */
1499 if (alg_a & SSL_aPSK) {
1500 /* copy PSK identity hint (if provided) */
1501 s2n(psk_identity_hint_len, p);
1502 if (psk_identity_hint_len > 0) {
1503 memcpy(p, psk_identity_hint, psk_identity_hint_len);
1504 p += psk_identity_hint_len;
1505 }
1506 }
David Benjamin00505ec2014-11-26 16:38:00 -05001507
David Benjamin7061e282015-03-19 11:10:48 -04001508 if (alg_k & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001509 /* We only support named (not generic) curves. In this situation, the
1510 * serverKeyExchange message has:
1511 * [1 byte CurveType], [2 byte CurveName]
1512 * [1 byte length of encoded point], followed by
1513 * the actual encoded point itself. */
David Benjamin70bd80a2014-12-27 03:06:46 -05001514 *(p++) = NAMED_CURVE_TYPE;
1515 *(p++) = (uint8_t)(curve_id >> 8);
1516 *(p++) = (uint8_t)(curve_id & 0xff);
1517 *(p++) = encodedlen;
1518 memcpy(p, encodedPoint, encodedlen);
1519 p += encodedlen;
Adam Langleyfcf25832014-12-18 17:42:32 -08001520 OPENSSL_free(encodedPoint);
1521 encodedPoint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001522 }
Adam Langley95c29f32014-06-20 12:00:00 -07001523
Adam Langleyfcf25832014-12-18 17:42:32 -08001524 /* not anonymous */
1525 if (pkey != NULL) {
1526 /* n is the length of the params, they start at &(d[4]) and p points to
1527 * the space at the end. */
1528 const EVP_MD *md;
1529 size_t sig_len = EVP_PKEY_size(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001530
Adam Langleyfcf25832014-12-18 17:42:32 -08001531 /* Determine signature algorithm. */
1532 if (SSL_USE_SIGALGS(s)) {
1533 md = tls1_choose_signing_digest(s, pkey);
1534 if (!tls12_get_sigandhash(p, pkey, md)) {
1535 /* Should never happen */
1536 al = SSL_AD_INTERNAL_ERROR;
1537 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1538 ERR_R_INTERNAL_ERROR);
1539 goto f_err;
1540 }
1541 p += 2;
1542 } else if (pkey->type == EVP_PKEY_RSA) {
1543 md = EVP_md5_sha1();
1544 } else {
1545 md = EVP_sha1();
1546 }
1547
1548 if (!EVP_DigestSignInit(&md_ctx, NULL, md, NULL, pkey) ||
1549 !EVP_DigestSignUpdate(&md_ctx, s->s3->client_random,
1550 SSL3_RANDOM_SIZE) ||
1551 !EVP_DigestSignUpdate(&md_ctx, s->s3->server_random,
1552 SSL3_RANDOM_SIZE) ||
1553 !EVP_DigestSignUpdate(&md_ctx, d, n) ||
1554 !EVP_DigestSignFinal(&md_ctx, &p[2], &sig_len)) {
1555 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_EVP);
1556 goto err;
1557 }
1558
1559 s2n(sig_len, p);
1560 n += sig_len + 2;
1561 if (SSL_USE_SIGALGS(s)) {
1562 n += 2;
1563 }
1564 }
1565
David Benjaminfbdfefb2015-02-16 19:33:53 -05001566 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n)) {
1567 goto err;
1568 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001569 }
1570
1571 s->state = SSL3_ST_SW_KEY_EXCH_B;
1572 EVP_MD_CTX_cleanup(&md_ctx);
1573 return ssl_do_write(s);
1574
Adam Langley95c29f32014-06-20 12:00:00 -07001575f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001576 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001577err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001578 OPENSSL_free(encodedPoint);
Adam Langleyfcf25832014-12-18 17:42:32 -08001579 BN_CTX_free(bn_ctx);
1580 EVP_MD_CTX_cleanup(&md_ctx);
1581 return -1;
1582}
Adam Langley95c29f32014-06-20 12:00:00 -07001583
Adam Langleyfcf25832014-12-18 17:42:32 -08001584int ssl3_send_certificate_request(SSL *s) {
1585 uint8_t *p, *d;
1586 size_t i;
1587 int j, nl, off, n;
1588 STACK_OF(X509_NAME) *sk = NULL;
1589 X509_NAME *name;
1590 BUF_MEM *buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001591
Adam Langleyfcf25832014-12-18 17:42:32 -08001592 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
1593 buf = s->init_buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001594
Adam Langleyfcf25832014-12-18 17:42:32 -08001595 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001596
Adam Langleyfcf25832014-12-18 17:42:32 -08001597 /* get the list of acceptable cert types */
1598 p++;
1599 n = ssl3_get_req_cert_type(s, p);
1600 d[0] = n;
1601 p += n;
1602 n++;
Adam Langley95c29f32014-06-20 12:00:00 -07001603
Adam Langleyfcf25832014-12-18 17:42:32 -08001604 if (SSL_USE_SIGALGS(s)) {
1605 const uint8_t *psigs;
1606 nl = tls12_get_psigalgs(s, &psigs);
1607 s2n(nl, p);
1608 memcpy(p, psigs, nl);
1609 p += nl;
1610 n += nl + 2;
1611 }
Adam Langley95c29f32014-06-20 12:00:00 -07001612
Adam Langleyfcf25832014-12-18 17:42:32 -08001613 off = n;
1614 p += 2;
1615 n += 2;
Adam Langley95c29f32014-06-20 12:00:00 -07001616
Adam Langleyfcf25832014-12-18 17:42:32 -08001617 sk = SSL_get_client_CA_list(s);
1618 nl = 0;
1619 if (sk != NULL) {
1620 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1621 name = sk_X509_NAME_value(sk, i);
1622 j = i2d_X509_NAME(name, NULL);
1623 if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
1624 OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB);
1625 goto err;
1626 }
1627 p = ssl_handshake_start(s) + n;
1628 s2n(j, p);
1629 i2d_X509_NAME(name, &p);
1630 n += 2 + j;
1631 nl += 2 + j;
1632 }
1633 }
Adam Langley95c29f32014-06-20 12:00:00 -07001634
Adam Langleyfcf25832014-12-18 17:42:32 -08001635 /* else no CA names */
1636 p = ssl_handshake_start(s) + off;
1637 s2n(nl, p);
1638
David Benjaminfbdfefb2015-02-16 19:33:53 -05001639 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n)) {
1640 goto err;
1641 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001642 s->state = SSL3_ST_SW_CERT_REQ_B;
1643 }
Adam Langley95c29f32014-06-20 12:00:00 -07001644
Adam Langleyfcf25832014-12-18 17:42:32 -08001645 /* SSL3_ST_SW_CERT_REQ_B */
1646 return ssl_do_write(s);
1647
Adam Langley95c29f32014-06-20 12:00:00 -07001648err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001649 return -1;
1650}
Adam Langley95c29f32014-06-20 12:00:00 -07001651
Adam Langleyfcf25832014-12-18 17:42:32 -08001652int ssl3_get_client_key_exchange(SSL *s) {
1653 int al, ok;
1654 long n;
1655 CBS client_key_exchange;
David Benjamin3fa27772015-04-17 22:32:19 -04001656 uint32_t alg_k;
1657 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001658 uint8_t *premaster_secret = NULL;
1659 size_t premaster_secret_len = 0;
1660 RSA *rsa = NULL;
1661 uint8_t *decrypt_buf = NULL;
1662 EVP_PKEY *pkey = NULL;
1663 BIGNUM *pub = NULL;
1664 DH *dh_srvr;
Adam Langley95c29f32014-06-20 12:00:00 -07001665
Adam Langleyfcf25832014-12-18 17:42:32 -08001666 EC_KEY *srvr_ecdh = NULL;
1667 EVP_PKEY *clnt_pub_pkey = NULL;
1668 EC_POINT *clnt_ecpoint = NULL;
1669 BN_CTX *bn_ctx = NULL;
1670 unsigned int psk_len = 0;
1671 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001672
Adam Langleyfcf25832014-12-18 17:42:32 -08001673 n = s->method->ssl_get_message(s, SSL3_ST_SR_KEY_EXCH_A,
1674 SSL3_ST_SR_KEY_EXCH_B,
1675 SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, /* ??? */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001676 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001677
Adam Langleyfcf25832014-12-18 17:42:32 -08001678 if (!ok) {
1679 return n;
1680 }
Adam Langley95c29f32014-06-20 12:00:00 -07001681
Adam Langleyfcf25832014-12-18 17:42:32 -08001682 CBS_init(&client_key_exchange, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001683
Adam Langleyfcf25832014-12-18 17:42:32 -08001684 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1685 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001686
Adam Langleyfcf25832014-12-18 17:42:32 -08001687 /* If using a PSK key exchange, prepare the pre-shared key. */
1688 if (alg_a & SSL_aPSK) {
1689 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001690
Adam Langleyfcf25832014-12-18 17:42:32 -08001691 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1692 * then this is the only field in the message. */
1693 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1694 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
1695 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR);
1696 al = SSL_AD_DECODE_ERROR;
1697 goto f_err;
1698 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001699
Adam Langleyfcf25832014-12-18 17:42:32 -08001700 if (s->psk_server_callback == NULL) {
1701 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1702 SSL_R_PSK_NO_SERVER_CB);
1703 al = SSL_AD_INTERNAL_ERROR;
1704 goto f_err;
1705 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001706
Adam Langleyfcf25832014-12-18 17:42:32 -08001707 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1708 CBS_contains_zero_byte(&psk_identity)) {
1709 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1710 SSL_R_DATA_LENGTH_TOO_LONG);
1711 al = SSL_AD_ILLEGAL_PARAMETER;
1712 goto f_err;
1713 }
David Benjamin35c02302014-07-13 04:14:59 -04001714
Adam Langleyfcf25832014-12-18 17:42:32 -08001715 if (!CBS_strdup(&psk_identity, &s->session->psk_identity)) {
1716 al = SSL_AD_INTERNAL_ERROR;
1717 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1718 ERR_R_MALLOC_FAILURE);
1719 goto f_err;
1720 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001721
Adam Langleyfcf25832014-12-18 17:42:32 -08001722 /* Look up the key for the identity. */
1723 psk_len =
1724 s->psk_server_callback(s, s->session->psk_identity, psk, sizeof(psk));
1725 if (psk_len > PSK_MAX_PSK_LEN) {
1726 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1727 ERR_R_INTERNAL_ERROR);
1728 al = SSL_AD_INTERNAL_ERROR;
1729 goto f_err;
1730 } else if (psk_len == 0) {
1731 /* PSK related to the given identity not found */
1732 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1733 SSL_R_PSK_IDENTITY_NOT_FOUND);
1734 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1735 goto f_err;
1736 }
1737 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001738
Adam Langleyfcf25832014-12-18 17:42:32 -08001739 /* Depending on the key exchange method, compute |premaster_secret| and
1740 * |premaster_secret_len|. */
1741 if (alg_k & SSL_kRSA) {
1742 CBS encrypted_premaster_secret;
1743 uint8_t rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
1744 uint8_t good;
1745 size_t rsa_size, decrypt_len, premaster_index, j;
Adam Langley95c29f32014-06-20 12:00:00 -07001746
Adam Langleyfcf25832014-12-18 17:42:32 -08001747 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1748 if (pkey == NULL || pkey->type != EVP_PKEY_RSA || pkey->pkey.rsa == NULL) {
1749 al = SSL_AD_HANDSHAKE_FAILURE;
1750 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1751 SSL_R_MISSING_RSA_CERTIFICATE);
1752 goto f_err;
1753 }
1754 rsa = pkey->pkey.rsa;
Adam Langley95c29f32014-06-20 12:00:00 -07001755
Adam Langleyfcf25832014-12-18 17:42:32 -08001756 /* TLS and [incidentally] DTLS{0xFEFF} */
1757 if (s->version > SSL3_VERSION) {
1758 CBS copy = client_key_exchange;
1759 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1760 &encrypted_premaster_secret) ||
1761 CBS_len(&client_key_exchange) != 0) {
1762 if (!(s->options & SSL_OP_TLS_D5_BUG)) {
1763 al = SSL_AD_DECODE_ERROR;
1764 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1765 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1766 goto f_err;
1767 } else {
1768 encrypted_premaster_secret = copy;
1769 }
1770 }
1771 } else {
1772 encrypted_premaster_secret = client_key_exchange;
1773 }
Adam Langley95c29f32014-06-20 12:00:00 -07001774
Adam Langleyfcf25832014-12-18 17:42:32 -08001775 /* Reject overly short RSA keys because we want to be sure that the buffer
1776 * size makes it safe to iterate over the entire size of a premaster secret
1777 * (SSL_MAX_MASTER_KEY_LENGTH). The actual expected size is larger due to
1778 * RSA padding, but the bound is sufficient to be safe. */
1779 rsa_size = RSA_size(rsa);
1780 if (rsa_size < SSL_MAX_MASTER_KEY_LENGTH) {
1781 al = SSL_AD_DECRYPT_ERROR;
1782 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1783 SSL_R_DECRYPTION_FAILED);
1784 goto f_err;
1785 }
Adam Langley95c29f32014-06-20 12:00:00 -07001786
Adam Langleyfcf25832014-12-18 17:42:32 -08001787 /* We must not leak whether a decryption failure occurs because of
1788 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
1789 * section 7.4.7.1). The code follows that advice of the TLS RFC and
1790 * generates a random premaster secret for the case that the decrypt fails.
1791 * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */
1792 if (!RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret))) {
1793 goto err;
1794 }
David Benjamin35c02302014-07-13 04:14:59 -04001795
Adam Langleyfcf25832014-12-18 17:42:32 -08001796 /* Allocate a buffer large enough for an RSA decryption. */
1797 decrypt_buf = OPENSSL_malloc(rsa_size);
1798 if (decrypt_buf == NULL) {
1799 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1800 ERR_R_MALLOC_FAILURE);
1801 goto err;
1802 }
David Benjamin35c02302014-07-13 04:14:59 -04001803
Adam Langleyfcf25832014-12-18 17:42:32 -08001804 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1805 * timing-sensitive code below. */
1806 if (!RSA_decrypt(rsa, &decrypt_len, decrypt_buf, rsa_size,
1807 CBS_data(&encrypted_premaster_secret),
1808 CBS_len(&encrypted_premaster_secret), RSA_NO_PADDING)) {
1809 goto err;
1810 }
1811 if (decrypt_len != rsa_size) {
1812 /* This should never happen, but do a check so we do not read
1813 * uninitialized memory. */
1814 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1815 ERR_R_INTERNAL_ERROR);
1816 goto err;
1817 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001818
Adam Langleyfcf25832014-12-18 17:42:32 -08001819 /* Remove the PKCS#1 padding and adjust |decrypt_len| as appropriate.
1820 * |good| will be 0xff if the premaster is acceptable and zero otherwise.
1821 * */
1822 good =
1823 constant_time_eq_int_8(RSA_message_index_PKCS1_type_2(
1824 decrypt_buf, decrypt_len, &premaster_index),
1825 1);
1826 decrypt_len = decrypt_len - premaster_index;
David Benjamin0aa07672014-07-24 15:34:14 -04001827
Adam Langleyfcf25832014-12-18 17:42:32 -08001828 /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. */
1829 good &= constant_time_eq_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
Adam Langleyacff73f2014-06-20 12:00:00 -07001830
Adam Langleyfcf25832014-12-18 17:42:32 -08001831 /* Copy over the unpadded premaster. Whatever the value of
1832 * |decrypt_good_mask|, copy as if the premaster were the right length. It
1833 * is important the memory access pattern be constant. */
1834 premaster_secret =
1835 BUF_memdup(decrypt_buf + (rsa_size - SSL_MAX_MASTER_KEY_LENGTH),
1836 SSL_MAX_MASTER_KEY_LENGTH);
1837 if (premaster_secret == NULL) {
1838 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1839 ERR_R_MALLOC_FAILURE);
1840 goto err;
1841 }
1842 OPENSSL_free(decrypt_buf);
1843 decrypt_buf = NULL;
Adam Langleyacff73f2014-06-20 12:00:00 -07001844
Adam Langleyfcf25832014-12-18 17:42:32 -08001845 /* If the version in the decrypted pre-master secret is correct then
1846 * version_good will be 0xff, otherwise it'll be zero. The
1847 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
1848 * (http://eprint.iacr.org/2003/052/) exploits the version number check as
1849 * a "bad version oracle". Thus version checks are done in constant time
1850 * and are treated like any other decryption error. */
1851 good &= constant_time_eq_8(premaster_secret[0],
1852 (unsigned)(s->client_version >> 8));
1853 good &= constant_time_eq_8(premaster_secret[1],
1854 (unsigned)(s->client_version & 0xff));
Adam Langleyacff73f2014-06-20 12:00:00 -07001855
Adam Langleyfcf25832014-12-18 17:42:32 -08001856 /* Now copy rand_premaster_secret over premaster_secret using
1857 * decrypt_good_mask. */
1858 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
1859 premaster_secret[j] = constant_time_select_8(good, premaster_secret[j],
1860 rand_premaster_secret[j]);
1861 }
David Benjamin35c02302014-07-13 04:14:59 -04001862
Adam Langleyfcf25832014-12-18 17:42:32 -08001863 premaster_secret_len = sizeof(rand_premaster_secret);
David Benjamin7061e282015-03-19 11:10:48 -04001864 } else if (alg_k & SSL_kDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001865 CBS dh_Yc;
1866 int dh_len;
David Benjamin35c02302014-07-13 04:14:59 -04001867
Adam Langleyfcf25832014-12-18 17:42:32 -08001868 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &dh_Yc) ||
1869 CBS_len(&dh_Yc) == 0 || CBS_len(&client_key_exchange) != 0) {
1870 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1871 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1872 al = SSL_R_DECODE_ERROR;
1873 goto f_err;
1874 }
Adam Langley95c29f32014-06-20 12:00:00 -07001875
Adam Langleyfcf25832014-12-18 17:42:32 -08001876 if (s->s3->tmp.dh == NULL) {
1877 al = SSL_AD_HANDSHAKE_FAILURE;
1878 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1879 SSL_R_MISSING_TMP_DH_KEY);
1880 goto f_err;
1881 }
1882 dh_srvr = s->s3->tmp.dh;
Adam Langley95c29f32014-06-20 12:00:00 -07001883
Adam Langleyfcf25832014-12-18 17:42:32 -08001884 pub = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL);
1885 if (pub == NULL) {
1886 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_BN_LIB);
1887 goto err;
1888 }
Adam Langley95c29f32014-06-20 12:00:00 -07001889
Adam Langleyfcf25832014-12-18 17:42:32 -08001890 /* Allocate a buffer for the premaster secret. */
1891 premaster_secret = OPENSSL_malloc(DH_size(dh_srvr));
1892 if (premaster_secret == NULL) {
1893 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1894 ERR_R_MALLOC_FAILURE);
David Benjaminc35fb012015-02-12 00:48:45 -05001895 BN_clear_free(pub);
Adam Langleyfcf25832014-12-18 17:42:32 -08001896 goto err;
1897 }
Adam Langley95c29f32014-06-20 12:00:00 -07001898
Adam Langleyfcf25832014-12-18 17:42:32 -08001899 dh_len = DH_compute_key(premaster_secret, pub, dh_srvr);
1900 if (dh_len <= 0) {
1901 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_DH_LIB);
1902 BN_clear_free(pub);
1903 goto err;
1904 }
David Benjamin14c83e72014-07-13 04:54:57 -04001905
Adam Langleyfcf25832014-12-18 17:42:32 -08001906 DH_free(s->s3->tmp.dh);
1907 s->s3->tmp.dh = NULL;
1908 BN_clear_free(pub);
1909 pub = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001910
Adam Langleyfcf25832014-12-18 17:42:32 -08001911 premaster_secret_len = dh_len;
David Benjamin7061e282015-03-19 11:10:48 -04001912 } else if (alg_k & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001913 int field_size = 0, ecdh_len;
1914 const EC_KEY *tkey;
1915 const EC_GROUP *group;
1916 const BIGNUM *priv_key;
1917 CBS ecdh_Yc;
Adam Langley95c29f32014-06-20 12:00:00 -07001918
Adam Langleyfcf25832014-12-18 17:42:32 -08001919 /* initialize structures for server's ECDH key pair */
1920 srvr_ecdh = EC_KEY_new();
1921 if (srvr_ecdh == NULL) {
1922 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1923 ERR_R_MALLOC_FAILURE);
1924 goto err;
1925 }
Adam Langley95c29f32014-06-20 12:00:00 -07001926
Adam Langleyfcf25832014-12-18 17:42:32 -08001927 /* Use the ephermeral values we saved when generating the ServerKeyExchange
1928 * msg. */
1929 tkey = s->s3->tmp.ecdh;
Adam Langley95c29f32014-06-20 12:00:00 -07001930
Adam Langleyfcf25832014-12-18 17:42:32 -08001931 group = EC_KEY_get0_group(tkey);
1932 priv_key = EC_KEY_get0_private_key(tkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001933
Adam Langleyfcf25832014-12-18 17:42:32 -08001934 if (!EC_KEY_set_group(srvr_ecdh, group) ||
1935 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
1936 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
1937 goto err;
1938 }
Adam Langley95c29f32014-06-20 12:00:00 -07001939
Adam Langleyfcf25832014-12-18 17:42:32 -08001940 /* Let's get client's public key */
1941 clnt_ecpoint = EC_POINT_new(group);
1942 if (clnt_ecpoint == NULL) {
1943 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1944 ERR_R_MALLOC_FAILURE);
1945 goto err;
1946 }
Adam Langley95c29f32014-06-20 12:00:00 -07001947
Adam Langleyfcf25832014-12-18 17:42:32 -08001948 /* Get client's public key from encoded point in the ClientKeyExchange
1949 * message. */
1950 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ecdh_Yc) ||
1951 CBS_len(&client_key_exchange) != 0) {
1952 al = SSL_AD_DECODE_ERROR;
1953 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR);
1954 goto f_err;
1955 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001956
Adam Langleyfcf25832014-12-18 17:42:32 -08001957 bn_ctx = BN_CTX_new();
1958 if (bn_ctx == NULL) {
1959 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1960 ERR_R_MALLOC_FAILURE);
1961 goto err;
1962 }
David Benjamin35c02302014-07-13 04:14:59 -04001963
Adam Langleyfcf25832014-12-18 17:42:32 -08001964 if (!EC_POINT_oct2point(group, clnt_ecpoint, CBS_data(&ecdh_Yc),
1965 CBS_len(&ecdh_Yc), bn_ctx)) {
1966 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
1967 goto err;
1968 }
Adam Langley95c29f32014-06-20 12:00:00 -07001969
Adam Langleyfcf25832014-12-18 17:42:32 -08001970 /* Allocate a buffer for both the secret and the PSK. */
1971 field_size = EC_GROUP_get_degree(group);
1972 if (field_size <= 0) {
1973 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
1974 goto err;
1975 }
David Benjamin35c02302014-07-13 04:14:59 -04001976
Adam Langleyfcf25832014-12-18 17:42:32 -08001977 ecdh_len = (field_size + 7) / 8;
1978 premaster_secret = OPENSSL_malloc(ecdh_len);
1979 if (premaster_secret == NULL) {
1980 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1981 ERR_R_MALLOC_FAILURE);
1982 goto err;
1983 }
David Benjamin35c02302014-07-13 04:14:59 -04001984
Adam Langleyfcf25832014-12-18 17:42:32 -08001985 /* Compute the shared pre-master secret */
1986 ecdh_len = ECDH_compute_key(premaster_secret, ecdh_len, clnt_ecpoint,
1987 srvr_ecdh, NULL);
1988 if (ecdh_len <= 0) {
1989 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
1990 goto err;
1991 }
Adam Langley95c29f32014-06-20 12:00:00 -07001992
Adam Langleyfcf25832014-12-18 17:42:32 -08001993 EVP_PKEY_free(clnt_pub_pkey);
1994 clnt_pub_pkey = NULL;
1995 EC_POINT_free(clnt_ecpoint);
1996 clnt_ecpoint = NULL;
1997 EC_KEY_free(srvr_ecdh);
1998 srvr_ecdh = NULL;
1999 BN_CTX_free(bn_ctx);
2000 bn_ctx = NULL;
2001 EC_KEY_free(s->s3->tmp.ecdh);
2002 s->s3->tmp.ecdh = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002003
Adam Langleyfcf25832014-12-18 17:42:32 -08002004 premaster_secret_len = ecdh_len;
2005 } else if (alg_k & SSL_kPSK) {
2006 /* For plain PSK, other_secret is a block of 0s with the same length as the
2007 * pre-shared key. */
2008 premaster_secret_len = psk_len;
2009 premaster_secret = OPENSSL_malloc(premaster_secret_len);
2010 if (premaster_secret == NULL) {
2011 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2012 ERR_R_MALLOC_FAILURE);
2013 goto err;
2014 }
2015 memset(premaster_secret, 0, premaster_secret_len);
2016 } else {
2017 al = SSL_AD_HANDSHAKE_FAILURE;
2018 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2019 SSL_R_UNKNOWN_CIPHER_TYPE);
2020 goto f_err;
2021 }
Adam Langley95c29f32014-06-20 12:00:00 -07002022
Adam Langleyfcf25832014-12-18 17:42:32 -08002023 /* For a PSK cipher suite, the actual pre-master secret is combined with the
2024 * pre-shared key. */
2025 if (alg_a & SSL_aPSK) {
2026 CBB new_premaster, child;
2027 uint8_t *new_data;
2028 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04002029
Adam Langleyfcf25832014-12-18 17:42:32 -08002030 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len)) {
2031 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2032 ERR_R_MALLOC_FAILURE);
2033 goto err;
2034 }
2035 if (!CBB_add_u16_length_prefixed(&new_premaster, &child) ||
2036 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
2037 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
2038 !CBB_add_bytes(&child, psk, psk_len) ||
2039 !CBB_finish(&new_premaster, &new_data, &new_len)) {
2040 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2041 ERR_R_INTERNAL_ERROR);
2042 CBB_cleanup(&new_premaster);
2043 goto err;
2044 }
David Benjamin14c83e72014-07-13 04:54:57 -04002045
Adam Langleyfcf25832014-12-18 17:42:32 -08002046 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2047 OPENSSL_free(premaster_secret);
2048 premaster_secret = new_data;
2049 premaster_secret_len = new_len;
2050 }
David Benjamin14c83e72014-07-13 04:54:57 -04002051
Adam Langleyfcf25832014-12-18 17:42:32 -08002052 /* Compute the master secret */
2053 s->session->master_key_length = s->enc_method->generate_master_secret(
2054 s, s->session->master_key, premaster_secret, premaster_secret_len);
2055 if (s->session->master_key_length == 0) {
2056 goto err;
2057 }
2058 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04002059
Adam Langleyfcf25832014-12-18 17:42:32 -08002060 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2061 OPENSSL_free(premaster_secret);
2062 return 1;
2063
Adam Langley95c29f32014-06-20 12:00:00 -07002064f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002065 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07002066err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002067 if (premaster_secret) {
2068 if (premaster_secret_len) {
2069 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2070 }
2071 OPENSSL_free(premaster_secret);
2072 }
David Benjamin2755a3e2015-04-22 16:17:58 -04002073 OPENSSL_free(decrypt_buf);
Adam Langleyfcf25832014-12-18 17:42:32 -08002074 EVP_PKEY_free(clnt_pub_pkey);
2075 EC_POINT_free(clnt_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04002076 EC_KEY_free(srvr_ecdh);
Adam Langleyfcf25832014-12-18 17:42:32 -08002077 BN_CTX_free(bn_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002078
Adam Langleyfcf25832014-12-18 17:42:32 -08002079 return -1;
2080}
Adam Langley95c29f32014-06-20 12:00:00 -07002081
Adam Langleyfcf25832014-12-18 17:42:32 -08002082int ssl3_get_cert_verify(SSL *s) {
2083 int al, ok, ret = 0;
2084 long n;
2085 CBS certificate_verify, signature;
2086 X509 *peer = s->session->peer;
2087 EVP_PKEY *pkey = NULL;
2088 const EVP_MD *md = NULL;
2089 uint8_t digest[EVP_MAX_MD_SIZE];
2090 size_t digest_length;
2091 EVP_PKEY_CTX *pctx = NULL;
David Benjamin6553b372014-07-22 14:11:30 -04002092
Adam Langleyfcf25832014-12-18 17:42:32 -08002093 /* Only RSA and ECDSA client certificates are supported, so a
2094 * CertificateVerify is required if and only if there's a client certificate.
2095 * */
2096 if (peer == NULL) {
2097 if (s->s3->handshake_buffer &&
2098 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2099 return -1;
2100 }
2101 return 1;
2102 }
Adam Langley95c29f32014-06-20 12:00:00 -07002103
Adam Langleyfcf25832014-12-18 17:42:32 -08002104 n = s->method->ssl_get_message(
2105 s, SSL3_ST_SR_CERT_VRFY_A, SSL3_ST_SR_CERT_VRFY_B,
2106 SSL3_MT_CERTIFICATE_VERIFY, SSL3_RT_MAX_PLAIN_LENGTH,
David Benjamin5ca39fb2015-03-01 23:57:54 -05002107 ssl_dont_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002108
Adam Langleyfcf25832014-12-18 17:42:32 -08002109 if (!ok) {
2110 return n;
2111 }
David Benjaminef865502014-08-24 02:48:34 -04002112
Adam Langleyfcf25832014-12-18 17:42:32 -08002113 /* Filter out unsupported certificate types. */
2114 pkey = X509_get_pubkey(peer);
David Benjamin9d0847a2015-02-16 03:57:55 -05002115 if (pkey == NULL) {
2116 goto err;
2117 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002118 if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) ||
2119 (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) {
2120 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
2121 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify,
2122 SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
2123 goto f_err;
2124 }
David Benjamin6897dbe2014-07-12 20:18:28 -04002125
Adam Langleyfcf25832014-12-18 17:42:32 -08002126 CBS_init(&certificate_verify, s->init_msg, n);
David Benjamin6897dbe2014-07-12 20:18:28 -04002127
Adam Langleyfcf25832014-12-18 17:42:32 -08002128 /* Determine the digest type if needbe. */
2129 if (SSL_USE_SIGALGS(s) &&
2130 !tls12_check_peer_sigalg(&md, &al, s, &certificate_verify, pkey)) {
2131 goto f_err;
2132 }
David Benjamin854dd652014-08-26 00:32:30 -04002133
Adam Langleyfcf25832014-12-18 17:42:32 -08002134 /* Compute the digest. */
2135 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2136 goto err;
2137 }
David Benjamin854dd652014-08-26 00:32:30 -04002138
Adam Langleyfcf25832014-12-18 17:42:32 -08002139 /* The handshake buffer is no longer necessary, and we may hash the current
2140 * message.*/
2141 if (s->s3->handshake_buffer &&
2142 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2143 goto err;
2144 }
David Benjaminfbdfefb2015-02-16 19:33:53 -05002145 if (!ssl3_hash_current_message(s)) {
2146 goto err;
2147 }
Adam Langley95c29f32014-06-20 12:00:00 -07002148
Adam Langleyfcf25832014-12-18 17:42:32 -08002149 /* Parse and verify the signature. */
2150 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
2151 CBS_len(&certificate_verify) != 0) {
2152 al = SSL_AD_DECODE_ERROR;
2153 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_DECODE_ERROR);
2154 goto f_err;
2155 }
Adam Langley95c29f32014-06-20 12:00:00 -07002156
Adam Langleyfcf25832014-12-18 17:42:32 -08002157 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2158 if (pctx == NULL) {
2159 goto err;
2160 }
2161 if (!EVP_PKEY_verify_init(pctx) ||
2162 !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2163 !EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), digest,
2164 digest_length)) {
2165 al = SSL_AD_DECRYPT_ERROR;
2166 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_SIGNATURE);
2167 goto f_err;
2168 }
2169
2170 ret = 1;
2171
2172 if (0) {
2173 f_err:
2174 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2175 }
2176
David Benjamin854dd652014-08-26 00:32:30 -04002177err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002178 EVP_PKEY_CTX_free(pctx);
2179 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07002180
Adam Langleyfcf25832014-12-18 17:42:32 -08002181 return ret;
2182}
Adam Langley95c29f32014-06-20 12:00:00 -07002183
Adam Langleyfcf25832014-12-18 17:42:32 -08002184int ssl3_get_client_certificate(SSL *s) {
2185 int i, ok, al, ret = -1;
2186 X509 *x = NULL;
2187 unsigned long n;
2188 STACK_OF(X509) *sk = NULL;
2189 SHA256_CTX sha256;
2190 CBS certificate_msg, certificate_list;
2191 int is_first_certificate = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002192
Adam Langleyfcf25832014-12-18 17:42:32 -08002193 n = s->method->ssl_get_message(s, SSL3_ST_SR_CERT_A, SSL3_ST_SR_CERT_B, -1,
David Benjamin1d0a1942015-04-26 15:35:35 -04002194 (long)s->max_cert_list, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002195
Adam Langleyfcf25832014-12-18 17:42:32 -08002196 if (!ok) {
2197 return n;
2198 }
Adam Langley95c29f32014-06-20 12:00:00 -07002199
Adam Langleyfcf25832014-12-18 17:42:32 -08002200 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
2201 if ((s->verify_mode & SSL_VERIFY_PEER) &&
2202 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2203 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2204 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2205 al = SSL_AD_HANDSHAKE_FAILURE;
2206 goto f_err;
2207 }
David Benjamind5b1f842014-07-11 23:43:48 -04002208
Adam Langleyfcf25832014-12-18 17:42:32 -08002209 /* If tls asked for a client cert, the client must return a 0 list */
2210 if (s->version > SSL3_VERSION && s->s3->tmp.cert_request) {
2211 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2212 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
2213 al = SSL_AD_UNEXPECTED_MESSAGE;
2214 goto f_err;
2215 }
2216 s->s3->tmp.reuse_message = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002217
Adam Langleyfcf25832014-12-18 17:42:32 -08002218 return 1;
2219 }
Adam Langley95c29f32014-06-20 12:00:00 -07002220
Adam Langleyfcf25832014-12-18 17:42:32 -08002221 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
2222 al = SSL_AD_UNEXPECTED_MESSAGE;
2223 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2224 SSL_R_WRONG_MESSAGE_TYPE);
2225 goto f_err;
2226 }
David Benjamind5b1f842014-07-11 23:43:48 -04002227
Adam Langleyfcf25832014-12-18 17:42:32 -08002228 CBS_init(&certificate_msg, s->init_msg, n);
David Benjamind5b1f842014-07-11 23:43:48 -04002229
Adam Langleyfcf25832014-12-18 17:42:32 -08002230 sk = sk_X509_new_null();
2231 if (sk == NULL) {
2232 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2233 goto err;
2234 }
Adam Langley95c29f32014-06-20 12:00:00 -07002235
Adam Langleyfcf25832014-12-18 17:42:32 -08002236 if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) ||
2237 CBS_len(&certificate_msg) != 0) {
2238 al = SSL_AD_DECODE_ERROR;
2239 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR);
2240 goto f_err;
2241 }
Adam Langley95c29f32014-06-20 12:00:00 -07002242
Adam Langleyfcf25832014-12-18 17:42:32 -08002243 while (CBS_len(&certificate_list) > 0) {
2244 CBS certificate;
2245 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07002246
Adam Langleyfcf25832014-12-18 17:42:32 -08002247 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
2248 al = SSL_AD_DECODE_ERROR;
2249 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR);
2250 goto f_err;
2251 }
Adam Langley95c29f32014-06-20 12:00:00 -07002252
Adam Langleyfcf25832014-12-18 17:42:32 -08002253 if (is_first_certificate && s->ctx->retain_only_sha256_of_client_certs) {
2254 /* If this is the first certificate, and we don't want to keep peer
2255 * certificates in memory, then we hash it right away. */
2256 SHA256_Init(&sha256);
2257 SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate));
2258 SHA256_Final(s->session->peer_sha256, &sha256);
2259 s->session->peer_sha256_valid = 1;
2260 }
2261 is_first_certificate = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002262
Adam Langleyfcf25832014-12-18 17:42:32 -08002263 data = CBS_data(&certificate);
2264 x = d2i_X509(NULL, &data, CBS_len(&certificate));
2265 if (x == NULL) {
2266 al = SSL_AD_BAD_CERTIFICATE;
2267 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_ASN1_LIB);
2268 goto f_err;
2269 }
2270 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
2271 al = SSL_AD_DECODE_ERROR;
2272 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2273 SSL_R_CERT_LENGTH_MISMATCH);
2274 goto f_err;
2275 }
2276 if (!sk_X509_push(sk, x)) {
2277 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2278 goto err;
2279 }
2280 x = NULL;
2281 }
2282
2283 if (sk_X509_num(sk) <= 0) {
2284 /* TLS does not mind 0 certs returned */
2285 if (s->version == SSL3_VERSION) {
2286 al = SSL_AD_HANDSHAKE_FAILURE;
2287 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2288 SSL_R_NO_CERTIFICATES_RETURNED);
2289 goto f_err;
2290 }
2291 /* Fail for TLS only if we required a certificate */
2292 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
2293 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2294 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2295 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2296 al = SSL_AD_HANDSHAKE_FAILURE;
2297 goto f_err;
2298 }
2299 /* No client certificate so digest cached records */
2300 if (s->s3->handshake_buffer &&
2301 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2302 al = SSL_AD_INTERNAL_ERROR;
2303 goto f_err;
2304 }
2305 } else {
2306 i = ssl_verify_cert_chain(s, sk);
2307 if (i <= 0) {
2308 al = ssl_verify_alarm_type(s->verify_result);
2309 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2310 SSL_R_CERTIFICATE_VERIFY_FAILED);
2311 goto f_err;
2312 }
2313 }
2314
David Benjamin2755a3e2015-04-22 16:17:58 -04002315 X509_free(s->session->peer);
Adam Langleyfcf25832014-12-18 17:42:32 -08002316 s->session->peer = sk_X509_shift(sk);
2317 s->session->verify_result = s->verify_result;
2318
2319 /* With the current implementation, sess_cert will always be NULL when we
2320 * arrive here. */
2321 if (s->session->sess_cert == NULL) {
2322 s->session->sess_cert = ssl_sess_cert_new();
2323 if (s->session->sess_cert == NULL) {
2324 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2325 goto err;
2326 }
2327 }
David Benjamin2755a3e2015-04-22 16:17:58 -04002328 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
Adam Langleyfcf25832014-12-18 17:42:32 -08002329 s->session->sess_cert->cert_chain = sk;
2330 /* Inconsistency alert: cert_chain does *not* include the peer's own
2331 * certificate, while we do include it in s3_clnt.c */
2332
2333 sk = NULL;
2334
2335 ret = 1;
2336
2337 if (0) {
2338 f_err:
2339 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2340 }
2341
Adam Langley95c29f32014-06-20 12:00:00 -07002342err:
David Benjamin2755a3e2015-04-22 16:17:58 -04002343 X509_free(x);
2344 sk_X509_pop_free(sk, X509_free);
Adam Langleyfcf25832014-12-18 17:42:32 -08002345 return ret;
2346}
Adam Langley95c29f32014-06-20 12:00:00 -07002347
Adam Langleyfcf25832014-12-18 17:42:32 -08002348int ssl3_send_server_certificate(SSL *s) {
2349 CERT_PKEY *cpk;
Adam Langley95c29f32014-06-20 12:00:00 -07002350
Adam Langleyfcf25832014-12-18 17:42:32 -08002351 if (s->state == SSL3_ST_SW_CERT_A) {
2352 cpk = ssl_get_server_send_pkey(s);
2353 if (cpk == NULL) {
2354 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_certificate,
2355 ERR_R_INTERNAL_ERROR);
2356 return 0;
2357 }
Adam Langley95c29f32014-06-20 12:00:00 -07002358
David Benjamin9d0847a2015-02-16 03:57:55 -05002359 if (!ssl3_output_cert_chain(s, cpk)) {
2360 return 0;
2361 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002362 s->state = SSL3_ST_SW_CERT_B;
2363 }
Adam Langley95c29f32014-06-20 12:00:00 -07002364
Adam Langleyfcf25832014-12-18 17:42:32 -08002365 /* SSL3_ST_SW_CERT_B */
2366 return ssl_do_write(s);
2367}
Adam Langley95c29f32014-06-20 12:00:00 -07002368
Adam Langley95c29f32014-06-20 12:00:00 -07002369/* send a new session ticket (not necessarily for a new session) */
Adam Langleyfcf25832014-12-18 17:42:32 -08002370int ssl3_send_new_session_ticket(SSL *s) {
David Benjaminbcd37452015-03-09 14:48:28 -04002371 int ret = -1;
2372 uint8_t *session = NULL;
2373 size_t session_len;
2374 EVP_CIPHER_CTX ctx;
2375 HMAC_CTX hctx;
2376
2377 EVP_CIPHER_CTX_init(&ctx);
2378 HMAC_CTX_init(&hctx);
2379
Adam Langleyfcf25832014-12-18 17:42:32 -08002380 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002381 uint8_t *p, *macstart;
2382 int len;
2383 unsigned int hlen;
Adam Langleyfcf25832014-12-18 17:42:32 -08002384 SSL_CTX *tctx = s->initial_ctx;
2385 uint8_t iv[EVP_MAX_IV_LENGTH];
2386 uint8_t key_name[16];
2387 /* The maximum overhead of encrypting the session is 16 (key name) + IV +
2388 * one block of encryption overhead + HMAC. */
2389 const size_t max_ticket_overhead =
2390 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -07002391
Adam Langleyfcf25832014-12-18 17:42:32 -08002392 /* Serialize the SSL_SESSION to be encoded into the ticket. */
2393 if (!SSL_SESSION_to_bytes_for_ticket(s->session, &session, &session_len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002394 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002395 }
Adam Langley95c29f32014-06-20 12:00:00 -07002396
Adam Langleyfcf25832014-12-18 17:42:32 -08002397 /* If the session is too long, emit a dummy value rather than abort the
2398 * connection. */
2399 if (session_len > 0xFFFF - max_ticket_overhead) {
2400 static const char kTicketPlaceholder[] = "TICKET TOO LARGE";
2401 const size_t placeholder_len = strlen(kTicketPlaceholder);
David Benjamin3cac4502014-10-21 01:46:30 -04002402
Adam Langleyfcf25832014-12-18 17:42:32 -08002403 OPENSSL_free(session);
David Benjaminbcd37452015-03-09 14:48:28 -04002404 session = NULL;
David Benjamin3cac4502014-10-21 01:46:30 -04002405
Adam Langleyfcf25832014-12-18 17:42:32 -08002406 p = ssl_handshake_start(s);
2407 /* Emit ticket_lifetime_hint. */
2408 l2n(0, p);
2409 /* Emit ticket. */
2410 s2n(placeholder_len, p);
2411 memcpy(p, kTicketPlaceholder, placeholder_len);
2412 p += placeholder_len;
David Benjamin3cac4502014-10-21 01:46:30 -04002413
Adam Langleyfcf25832014-12-18 17:42:32 -08002414 len = p - ssl_handshake_start(s);
David Benjaminfbdfefb2015-02-16 19:33:53 -05002415 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002416 goto err;
David Benjaminfbdfefb2015-02-16 19:33:53 -05002417 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002418 s->state = SSL3_ST_SW_SESSION_TICKET_B;
2419 return ssl_do_write(s);
2420 }
Adam Langley95c29f32014-06-20 12:00:00 -07002421
Adam Langleyfcf25832014-12-18 17:42:32 -08002422 /* Grow buffer if need be: the length calculation is as follows:
2423 * handshake_header_length + 4 (ticket lifetime hint) + 2 (ticket length) +
2424 * max_ticket_overhead + * session_length */
2425 if (!BUF_MEM_grow(s->init_buf, SSL_HM_HEADER_LENGTH(s) + 6 +
2426 max_ticket_overhead + session_len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002427 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002428 }
2429 p = ssl_handshake_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002430 /* Initialize HMAC and cipher contexts. If callback present it does all the
2431 * work otherwise use generated values from parent ctx. */
2432 if (tctx->tlsext_ticket_key_cb) {
David Benjaminadcc3952015-04-26 13:07:57 -04002433 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, &hctx,
2434 1 /* encrypt */) < 0) {
David Benjaminbcd37452015-03-09 14:48:28 -04002435 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002436 }
2437 } else {
2438 if (!RAND_bytes(iv, 16) ||
2439 !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2440 tctx->tlsext_tick_aes_key, iv) ||
2441 !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2442 NULL)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002443 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002444 }
2445 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
2446 }
Adam Langley95c29f32014-06-20 12:00:00 -07002447
Adam Langleyfcf25832014-12-18 17:42:32 -08002448 /* Ticket lifetime hint (advisory only): We leave this unspecified for
2449 * resumed session (for simplicity), and guess that tickets for new
2450 * sessions will live as long as their sessions. */
2451 l2n(s->hit ? 0 : s->session->timeout, p);
Adam Langley95c29f32014-06-20 12:00:00 -07002452
Adam Langleyfcf25832014-12-18 17:42:32 -08002453 /* Skip ticket length for now */
2454 p += 2;
2455 /* Output key name */
2456 macstart = p;
2457 memcpy(p, key_name, 16);
2458 p += 16;
2459 /* output IV */
2460 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
2461 p += EVP_CIPHER_CTX_iv_length(&ctx);
2462 /* Encrypt session data */
David Benjaminbcd37452015-03-09 14:48:28 -04002463 if (!EVP_EncryptUpdate(&ctx, p, &len, session, session_len)) {
2464 goto err;
2465 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002466 p += len;
David Benjaminbcd37452015-03-09 14:48:28 -04002467 if (!EVP_EncryptFinal_ex(&ctx, p, &len)) {
2468 goto err;
2469 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002470 p += len;
Adam Langley95c29f32014-06-20 12:00:00 -07002471
David Benjaminbcd37452015-03-09 14:48:28 -04002472 if (!HMAC_Update(&hctx, macstart, p - macstart) ||
2473 !HMAC_Final(&hctx, p, &hlen)) {
2474 goto err;
2475 }
Adam Langley95c29f32014-06-20 12:00:00 -07002476
Adam Langleyfcf25832014-12-18 17:42:32 -08002477 p += hlen;
2478 /* Now write out lengths: p points to end of data written */
2479 /* Total length */
2480 len = p - ssl_handshake_start(s);
2481 /* Skip ticket lifetime hint */
2482 p = ssl_handshake_start(s) + 4;
2483 s2n(len - 6, p);
David Benjaminfbdfefb2015-02-16 19:33:53 -05002484 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002485 goto err;
David Benjaminfbdfefb2015-02-16 19:33:53 -05002486 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002487 s->state = SSL3_ST_SW_SESSION_TICKET_B;
Adam Langleyfcf25832014-12-18 17:42:32 -08002488 }
Adam Langley95c29f32014-06-20 12:00:00 -07002489
Adam Langleyfcf25832014-12-18 17:42:32 -08002490 /* SSL3_ST_SW_SESSION_TICKET_B */
David Benjaminbcd37452015-03-09 14:48:28 -04002491 ret = ssl_do_write(s);
2492
2493err:
David Benjamin2755a3e2015-04-22 16:17:58 -04002494 OPENSSL_free(session);
David Benjaminbcd37452015-03-09 14:48:28 -04002495 EVP_CIPHER_CTX_cleanup(&ctx);
2496 HMAC_CTX_cleanup(&hctx);
2497 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002498}
Adam Langley95c29f32014-06-20 12:00:00 -07002499
Adam Langley95c29f32014-06-20 12:00:00 -07002500/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
2501 * sets the next_proto member in s if found */
Adam Langleyfcf25832014-12-18 17:42:32 -08002502int ssl3_get_next_proto(SSL *s) {
2503 int ok;
2504 long n;
2505 CBS next_protocol, selected_protocol, padding;
Adam Langley95c29f32014-06-20 12:00:00 -07002506
Adam Langleyfcf25832014-12-18 17:42:32 -08002507 /* Clients cannot send a NextProtocol message if we didn't see the extension
2508 * in their ClientHello */
2509 if (!s->s3->next_proto_neg_seen) {
2510 OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto,
2511 SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
2512 return -1;
2513 }
Adam Langley95c29f32014-06-20 12:00:00 -07002514
Adam Langleyfcf25832014-12-18 17:42:32 -08002515 n = s->method->ssl_get_message(s, SSL3_ST_SR_NEXT_PROTO_A,
2516 SSL3_ST_SR_NEXT_PROTO_B, SSL3_MT_NEXT_PROTO,
2517 514, /* See the payload format below */
David Benjamin5ca39fb2015-03-01 23:57:54 -05002518 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002519
Adam Langleyfcf25832014-12-18 17:42:32 -08002520 if (!ok) {
2521 return n;
2522 }
Adam Langley95c29f32014-06-20 12:00:00 -07002523
Adam Langleyfcf25832014-12-18 17:42:32 -08002524 /* s->state doesn't reflect whether ChangeCipherSpec has been received in
2525 * this handshake, but s->s3->change_cipher_spec does (will be reset by
2526 * ssl3_get_finished).
2527 *
2528 * TODO(davidben): Is this check now redundant with
2529 * SSL3_FLAGS_EXPECT_CCS? */
2530 if (!s->s3->change_cipher_spec) {
2531 OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto,
2532 SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
2533 return -1;
2534 }
Adam Langley95c29f32014-06-20 12:00:00 -07002535
Adam Langleyfcf25832014-12-18 17:42:32 -08002536 CBS_init(&next_protocol, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07002537
Adam Langleyfcf25832014-12-18 17:42:32 -08002538 /* The payload looks like:
2539 * uint8 proto_len;
2540 * uint8 proto[proto_len];
2541 * uint8 padding_len;
2542 * uint8 padding[padding_len]; */
2543 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
2544 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
2545 CBS_len(&next_protocol) != 0 ||
2546 !CBS_stow(&selected_protocol, &s->next_proto_negotiated,
2547 &s->next_proto_negotiated_len)) {
2548 return 0;
2549 }
Adam Langley95c29f32014-06-20 12:00:00 -07002550
Adam Langleyfcf25832014-12-18 17:42:32 -08002551 return 1;
2552}
Adam Langley95c29f32014-06-20 12:00:00 -07002553
Adam Langley1258b6a2014-06-20 12:00:00 -07002554/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002555int ssl3_get_channel_id(SSL *s) {
2556 int ret = -1, ok;
2557 long n;
2558 EVP_MD_CTX md_ctx;
2559 uint8_t channel_id_hash[SHA256_DIGEST_LENGTH];
2560 unsigned int channel_id_hash_len;
2561 const uint8_t *p;
2562 uint16_t extension_type, expected_extension_type;
2563 EC_GROUP *p256 = NULL;
2564 EC_KEY *key = NULL;
2565 EC_POINT *point = NULL;
2566 ECDSA_SIG sig;
2567 BIGNUM x, y;
2568 CBS encrypted_extensions, extension;
Adam Langley1258b6a2014-06-20 12:00:00 -07002569
Adam Langleyfcf25832014-12-18 17:42:32 -08002570 n = s->method->ssl_get_message(
2571 s, SSL3_ST_SR_CHANNEL_ID_A, SSL3_ST_SR_CHANNEL_ID_B,
2572 SSL3_MT_ENCRYPTED_EXTENSIONS, 2 + 2 + TLSEXT_CHANNEL_ID_SIZE,
David Benjamin5ca39fb2015-03-01 23:57:54 -05002573 ssl_dont_hash_message, &ok);
Adam Langley1258b6a2014-06-20 12:00:00 -07002574
Adam Langleyfcf25832014-12-18 17:42:32 -08002575 if (!ok) {
2576 return n;
2577 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002578
Adam Langleyfcf25832014-12-18 17:42:32 -08002579 /* Before incorporating the EncryptedExtensions message to the handshake
2580 * hash, compute the hash that should have been signed. */
2581 channel_id_hash_len = sizeof(channel_id_hash);
2582 EVP_MD_CTX_init(&md_ctx);
2583 if (!EVP_DigestInit_ex(&md_ctx, EVP_sha256(), NULL) ||
2584 !tls1_channel_id_hash(&md_ctx, s) ||
2585 !EVP_DigestFinal(&md_ctx, channel_id_hash, &channel_id_hash_len)) {
2586 EVP_MD_CTX_cleanup(&md_ctx);
2587 return -1;
2588 }
2589 EVP_MD_CTX_cleanup(&md_ctx);
2590 assert(channel_id_hash_len == SHA256_DIGEST_LENGTH);
David Benjamin880b14e2014-08-25 22:35:07 -04002591
David Benjaminfbdfefb2015-02-16 19:33:53 -05002592 if (!ssl3_hash_current_message(s)) {
2593 return -1;
2594 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002595
Adam Langleyfcf25832014-12-18 17:42:32 -08002596 /* s->state doesn't reflect whether ChangeCipherSpec has been received in
2597 * this handshake, but s->s3->change_cipher_spec does (will be reset by
2598 * ssl3_get_finished).
2599 *
2600 * TODO(davidben): Is this check now redundant with SSL3_FLAGS_EXPECT_CCS? */
2601 if (!s->s3->change_cipher_spec) {
2602 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id,
2603 SSL_R_GOT_CHANNEL_ID_BEFORE_A_CCS);
2604 return -1;
2605 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002606
Adam Langleyfcf25832014-12-18 17:42:32 -08002607 CBS_init(&encrypted_extensions, s->init_msg, n);
Adam Langley1258b6a2014-06-20 12:00:00 -07002608
Adam Langleyfcf25832014-12-18 17:42:32 -08002609 /* EncryptedExtensions could include multiple extensions, but the only
2610 * extension that could be negotiated is ChannelID, so there can only be one
2611 * entry.
2612 *
2613 * The payload looks like:
2614 * uint16 extension_type
2615 * uint16 extension_len;
2616 * uint8 x[32];
2617 * uint8 y[32];
2618 * uint8 r[32];
2619 * uint8 s[32]; */
2620 expected_extension_type = TLSEXT_TYPE_channel_id;
2621 if (s->s3->tlsext_channel_id_new) {
2622 expected_extension_type = TLSEXT_TYPE_channel_id_new;
2623 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002624
Adam Langleyfcf25832014-12-18 17:42:32 -08002625 if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
2626 !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
2627 CBS_len(&encrypted_extensions) != 0 ||
2628 extension_type != expected_extension_type ||
2629 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
2630 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_INVALID_MESSAGE);
2631 return -1;
2632 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002633
Adam Langleyfcf25832014-12-18 17:42:32 -08002634 p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
2635 if (!p256) {
2636 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_NO_P256_SUPPORT);
2637 return -1;
2638 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002639
Adam Langleyfcf25832014-12-18 17:42:32 -08002640 BN_init(&x);
2641 BN_init(&y);
2642 sig.r = BN_new();
2643 sig.s = BN_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05002644 if (sig.r == NULL || sig.s == NULL) {
2645 goto err;
2646 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002647
Adam Langleyfcf25832014-12-18 17:42:32 -08002648 p = CBS_data(&extension);
2649 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
2650 BN_bin2bn(p + 32, 32, &y) == NULL ||
2651 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
2652 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
2653 goto err;
2654 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002655
Adam Langleyfcf25832014-12-18 17:42:32 -08002656 point = EC_POINT_new(p256);
2657 if (!point || !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
2658 goto err;
2659 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002660
Adam Langleyfcf25832014-12-18 17:42:32 -08002661 key = EC_KEY_new();
2662 if (!key || !EC_KEY_set_group(key, p256) ||
2663 !EC_KEY_set_public_key(key, point)) {
2664 goto err;
2665 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002666
Adam Langleyfcf25832014-12-18 17:42:32 -08002667 /* We stored the handshake hash in |tlsext_channel_id| the first time that we
2668 * were called. */
2669 if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) {
2670 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id,
2671 SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
2672 s->s3->tlsext_channel_id_valid = 0;
2673 goto err;
2674 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002675
Adam Langleyfcf25832014-12-18 17:42:32 -08002676 memcpy(s->s3->tlsext_channel_id, p, 64);
2677 ret = 1;
Adam Langley1258b6a2014-06-20 12:00:00 -07002678
2679err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002680 BN_free(&x);
2681 BN_free(&y);
2682 BN_free(sig.r);
2683 BN_free(sig.s);
David Benjamin2755a3e2015-04-22 16:17:58 -04002684 EC_KEY_free(key);
2685 EC_POINT_free(point);
2686 EC_GROUP_free(p256);
Adam Langleyfcf25832014-12-18 17:42:32 -08002687 return ret;
2688}