blob: 2045590e4e683295fa31db9baf222ed5d69c63b5 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 *
113 * Portions of the attached software ("Contribution") are developed by
114 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115 *
116 * The Contribution is licensed pursuant to the OpenSSL open source
117 * license provided above.
118 *
119 * ECC cipher suite support in OpenSSL originally written by
120 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121 *
122 */
123/* ====================================================================
124 * Copyright 2005 Nokia. All rights reserved.
125 *
126 * The portions of the attached software ("Contribution") is developed by
127 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128 * license.
129 *
130 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132 * support (see RFC 4279) to OpenSSL.
133 *
134 * No patent licenses or other rights except those expressly stated in
135 * the OpenSSL open source license shall be deemed granted or received
136 * expressly, by implication, estoppel, or otherwise.
137 *
138 * No assurances are provided by Nokia that the Contribution does not
139 * infringe the patent or other intellectual property rights of any third
140 * party or that the license provides you with all the necessary rights
141 * to make use of the Contribution.
142 *
143 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147 * OTHERWISE. */
148
David Benjamin880b14e2014-08-25 22:35:07 -0400149#include <assert.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700150#include <stdio.h>
David Benjamin22f9bcc2014-07-13 12:29:21 -0400151#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700152
153#include <openssl/bn.h>
154#include <openssl/buf.h>
David Benjamindc72ff72014-06-25 12:36:10 -0400155#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700156#include <openssl/cipher.h>
157#include <openssl/dh.h>
Adam Langley1258b6a2014-06-20 12:00:00 -0700158#include <openssl/ec.h>
159#include <openssl/ecdsa.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700160#include <openssl/evp.h>
161#include <openssl/hmac.h>
162#include <openssl/md5.h>
163#include <openssl/mem.h>
164#include <openssl/obj.h>
165#include <openssl/rand.h>
Adam Langley1258b6a2014-06-20 12:00:00 -0700166#include <openssl/sha.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700167#include <openssl/x509.h>
168
169#include "ssl_locl.h"
Adam Langleyaf6e45b2014-11-03 19:34:49 -0800170#include "../crypto/internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700171#include "../crypto/dh/internal.h"
172
Adam Langleyfcf25832014-12-18 17:42:32 -0800173
David Benjamin4b755cb2014-12-12 03:58:07 -0500174/* INITIAL_SNIFF_BUFFER_SIZE is the number of bytes read in the initial sniff
175 * buffer. */
176#define INITIAL_SNIFF_BUFFER_SIZE 8
177
Adam Langleyfcf25832014-12-18 17:42:32 -0800178int ssl3_accept(SSL *s) {
179 BUF_MEM *buf = NULL;
180 unsigned long alg_a;
181 void (*cb)(const SSL *ssl, int type, int val) = NULL;
182 int ret = -1;
183 int new_state, state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700184
Adam Langleyfcf25832014-12-18 17:42:32 -0800185 assert(s->handshake_func == ssl3_accept);
186 assert(s->server);
187 assert(!SSL_IS_DTLS(s));
David Benjaminbeb47022014-11-30 02:58:52 -0500188
Adam Langleyfcf25832014-12-18 17:42:32 -0800189 ERR_clear_error();
190 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700191
Adam Langleyfcf25832014-12-18 17:42:32 -0800192 if (s->info_callback != NULL) {
193 cb = s->info_callback;
194 } else if (s->ctx->info_callback != NULL) {
195 cb = s->ctx->info_callback;
196 }
Adam Langley95c29f32014-06-20 12:00:00 -0700197
Adam Langleyfcf25832014-12-18 17:42:32 -0800198 s->in_handshake++;
Adam Langley95c29f32014-06-20 12:00:00 -0700199
Adam Langleyfcf25832014-12-18 17:42:32 -0800200 if (s->cert == NULL) {
201 OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_NO_CERTIFICATE_SET);
202 return -1;
203 }
Adam Langley95c29f32014-06-20 12:00:00 -0700204
Adam Langleyfcf25832014-12-18 17:42:32 -0800205 for (;;) {
206 state = s->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700207
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 switch (s->state) {
209 case SSL_ST_RENEGOTIATE:
210 /* This state is the renegotiate entry point. It sends a HelloRequest
211 * and nothing else. */
212 s->renegotiate = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700213
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 if (cb != NULL) {
215 cb(s, SSL_CB_HANDSHAKE_START, 1);
216 }
Adam Langley95c29f32014-06-20 12:00:00 -0700217
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 if (s->init_buf == NULL) {
219 buf = BUF_MEM_new();
220 if (!buf || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
221 ret = -1;
222 goto end;
223 }
224 s->init_buf = buf;
225 buf = NULL;
226 }
227 s->init_num = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500228
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 if (!ssl3_setup_buffers(s)) {
230 ret = -1;
231 goto end;
232 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500233
Adam Langleyfcf25832014-12-18 17:42:32 -0800234 if (!s->s3->send_connection_binding &&
235 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
236 /* Server attempting to renegotiate with client that doesn't support
237 * secure renegotiation. */
238 OPENSSL_PUT_ERROR(SSL, ssl3_accept,
239 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
240 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
241 ret = -1;
242 goto end;
243 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 s->ctx->stats.sess_accept_renegotiate++;
246 s->state = SSL3_ST_SW_HELLO_REQ_A;
247 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 case SSL3_ST_SW_HELLO_REQ_A:
250 case SSL3_ST_SW_HELLO_REQ_B:
251 s->shutdown = 0;
252 ret = ssl3_send_hello_request(s);
253 if (ret <= 0) {
254 goto end;
255 }
256 s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C;
257 s->state = SSL3_ST_SW_FLUSH;
258 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700259
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 if (!ssl3_init_finished_mac(s)) {
261 OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR);
262 ret = -1;
263 goto end;
264 }
265 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700266
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 case SSL3_ST_SW_HELLO_REQ_C:
268 s->state = SSL_ST_OK;
269 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700270
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 case SSL_ST_ACCEPT:
272 case SSL_ST_BEFORE | SSL_ST_ACCEPT:
273 /* This state is the entry point for the handshake itself (initial and
274 * renegotiation). */
275 if (cb != NULL) {
276 cb(s, SSL_CB_HANDSHAKE_START, 1);
277 }
Adam Langley95c29f32014-06-20 12:00:00 -0700278
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 if (s->init_buf == NULL) {
280 buf = BUF_MEM_new();
281 if (!buf || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
282 ret = -1;
283 goto end;
284 }
285 s->init_buf = buf;
286 buf = NULL;
287 }
288 s->init_num = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500289
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 if (!ssl3_init_finished_mac(s)) {
291 OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR);
292 ret = -1;
293 goto end;
294 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500295
Adam Langleyfcf25832014-12-18 17:42:32 -0800296 if (!s->s3->have_version) {
297 /* This is the initial handshake. The record layer has not been
298 * initialized yet. Sniff for a V2ClientHello before reading a
299 * ClientHello normally. */
300 assert(s->s3->rbuf.buf == NULL);
301 assert(s->s3->wbuf.buf == NULL);
302 s->state = SSL3_ST_SR_INITIAL_BYTES;
303 } else {
304 /* Enable a write buffer. This groups handshake messages within a
305 * flight into a single write. */
306 if (!ssl3_setup_buffers(s) || !ssl_init_wbio_buffer(s, 1)) {
307 ret = -1;
308 goto end;
309 }
310 s->state = SSL3_ST_SR_CLNT_HELLO_A;
311 }
312 s->ctx->stats.sess_accept++;
313 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500314
Adam Langleyfcf25832014-12-18 17:42:32 -0800315 case SSL3_ST_SR_INITIAL_BYTES:
316 ret = ssl3_get_initial_bytes(s);
317 if (ret <= 0) {
318 goto end;
319 }
320 /* ssl3_get_initial_bytes sets s->state to one of
321 * SSL3_ST_SR_V2_CLIENT_HELLO or SSL3_ST_SR_CLNT_HELLO_A on success. */
322 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500323
Adam Langleyfcf25832014-12-18 17:42:32 -0800324 case SSL3_ST_SR_V2_CLIENT_HELLO:
325 ret = ssl3_get_v2_client_hello(s);
326 if (ret <= 0) {
327 goto end;
328 }
329 s->state = SSL3_ST_SR_CLNT_HELLO_A;
330 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500331
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 case SSL3_ST_SR_CLNT_HELLO_A:
333 case SSL3_ST_SR_CLNT_HELLO_B:
334 case SSL3_ST_SR_CLNT_HELLO_C:
335 case SSL3_ST_SR_CLNT_HELLO_D:
336 s->shutdown = 0;
337 ret = ssl3_get_client_hello(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800338 if (ret <= 0) {
339 goto end;
340 }
341 s->renegotiate = 2;
342 s->state = SSL3_ST_SW_SRVR_HELLO_A;
343 s->init_num = 0;
344 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500345
Adam Langleyfcf25832014-12-18 17:42:32 -0800346 case SSL3_ST_SW_SRVR_HELLO_A:
347 case SSL3_ST_SW_SRVR_HELLO_B:
348 ret = ssl3_send_server_hello(s);
349 if (ret <= 0) {
350 goto end;
351 }
352 if (s->hit) {
353 if (s->tlsext_ticket_expected) {
354 s->state = SSL3_ST_SW_SESSION_TICKET_A;
355 } else {
356 s->state = SSL3_ST_SW_CHANGE_A;
357 }
358 } else {
359 s->state = SSL3_ST_SW_CERT_A;
360 }
361 s->init_num = 0;
362 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700363
Adam Langleyfcf25832014-12-18 17:42:32 -0800364 case SSL3_ST_SW_CERT_A:
365 case SSL3_ST_SW_CERT_B:
366 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
367 ret = ssl3_send_server_certificate(s);
368 if (ret <= 0) {
369 goto end;
370 }
371 if (s->s3->tmp.certificate_status_expected) {
372 s->state = SSL3_ST_SW_CERT_STATUS_A;
373 } else {
374 s->state = SSL3_ST_SW_KEY_EXCH_A;
375 }
376 } else {
377 skip = 1;
378 s->state = SSL3_ST_SW_KEY_EXCH_A;
379 }
380 s->init_num = 0;
381 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 case SSL3_ST_SW_KEY_EXCH_A:
384 case SSL3_ST_SW_KEY_EXCH_B:
385 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700386
Adam Langleyfcf25832014-12-18 17:42:32 -0800387 /* Send a ServerKeyExchange message if:
388 * - The key exchange is ephemeral or anonymous
389 * Diffie-Hellman.
390 * - There is a PSK identity hint.
391 *
392 * TODO(davidben): This logic is currently duplicated in d1_srvr.c. Fix
393 * this. In the meantime, keep them in sync. */
394 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) ||
395 ((alg_a & SSL_aPSK) && s->psk_identity_hint)) {
396 ret = ssl3_send_server_key_exchange(s);
David Benjamin6eb000d2015-02-11 01:17:41 -0500397 if (ret <= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800398 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500399 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800400 } else {
401 skip = 1;
402 }
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langleyfcf25832014-12-18 17:42:32 -0800404 s->state = SSL3_ST_SW_CERT_REQ_A;
405 s->init_num = 0;
406 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700407
Adam Langleyfcf25832014-12-18 17:42:32 -0800408 case SSL3_ST_SW_CERT_REQ_A:
409 case SSL3_ST_SW_CERT_REQ_B:
410 if (/* don't request cert unless asked for it: */
411 !(s->verify_mode & SSL_VERIFY_PEER) ||
412 /* Don't request a certificate if an obc was presented */
413 ((s->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
414 s->s3->tlsext_channel_id_valid) ||
415 /* if SSL_VERIFY_CLIENT_ONCE is set,
416 * don't request cert during re-negotiation: */
417 ((s->session->peer != NULL) &&
418 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
419 /* never request cert in anonymous ciphersuites
420 * (see section "Certificate request" in SSL 3 drafts
421 * and in RFC 2246): */
422 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
423 /* ... except when the application insists on verification
424 * (against the specs, but s3_clnt.c accepts this for SSL 3) */
425 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
426 /* With normal PSK Certificates and
427 * Certificate Requests are omitted */
428 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
429 /* no cert request */
430 skip = 1;
431 s->s3->tmp.cert_request = 0;
432 s->state = SSL3_ST_SW_SRVR_DONE_A;
433 if (s->s3->handshake_buffer &&
434 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
435 return -1;
436 }
437 } else {
438 s->s3->tmp.cert_request = 1;
439 ret = ssl3_send_certificate_request(s);
440 if (ret <= 0) {
441 goto end;
442 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800443 s->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800444 s->init_num = 0;
445 }
446 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700447
Adam Langleyfcf25832014-12-18 17:42:32 -0800448 case SSL3_ST_SW_SRVR_DONE_A:
449 case SSL3_ST_SW_SRVR_DONE_B:
450 ret = ssl3_send_server_done(s);
451 if (ret <= 0) {
452 goto end;
453 }
454 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
455 s->state = SSL3_ST_SW_FLUSH;
456 s->init_num = 0;
457 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700458
Adam Langleyfcf25832014-12-18 17:42:32 -0800459 case SSL3_ST_SW_FLUSH:
460 /* This code originally checked to see if any data was pending using
461 * BIO_CTRL_INFO and then flushed. This caused problems as documented
462 * in PR#1939. The proposed fix doesn't completely resolve this issue
463 * as buggy implementations of BIO_CTRL_PENDING still exist. So instead
464 * we just flush unconditionally. */
465 s->rwstate = SSL_WRITING;
466 if (BIO_flush(s->wbio) <= 0) {
467 ret = -1;
468 goto end;
469 }
470 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700471
Adam Langleyfcf25832014-12-18 17:42:32 -0800472 s->state = s->s3->tmp.next_state;
473 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700474
Adam Langleyfcf25832014-12-18 17:42:32 -0800475 case SSL3_ST_SR_CERT_A:
476 case SSL3_ST_SR_CERT_B:
477 if (s->s3->tmp.cert_request) {
478 ret = ssl3_get_client_certificate(s);
479 if (ret <= 0) {
480 goto end;
481 }
482 }
483 s->init_num = 0;
484 s->state = SSL3_ST_SR_KEY_EXCH_A;
485 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700486
Adam Langleyfcf25832014-12-18 17:42:32 -0800487 case SSL3_ST_SR_KEY_EXCH_A:
488 case SSL3_ST_SR_KEY_EXCH_B:
489 ret = ssl3_get_client_key_exchange(s);
490 if (ret <= 0) {
491 goto end;
492 }
493 s->state = SSL3_ST_SR_CERT_VRFY_A;
494 s->init_num = 0;
495 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700496
Adam Langleyfcf25832014-12-18 17:42:32 -0800497 case SSL3_ST_SR_CERT_VRFY_A:
498 case SSL3_ST_SR_CERT_VRFY_B:
499 ret = ssl3_get_cert_verify(s);
500 if (ret <= 0) {
501 goto end;
502 }
Adam Langley95c29f32014-06-20 12:00:00 -0700503
Adam Langleyfcf25832014-12-18 17:42:32 -0800504 s->state = SSL3_ST_SR_CHANGE;
505 s->init_num = 0;
506 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700507
Adam Langleyfcf25832014-12-18 17:42:32 -0800508 case SSL3_ST_SR_CHANGE: {
509 char next_proto_neg = 0;
510 char channel_id = 0;
511 next_proto_neg = s->s3->next_proto_neg_seen;
512 channel_id = s->s3->tlsext_channel_id_valid;
Adam Langley95c29f32014-06-20 12:00:00 -0700513
Adam Langleyfcf25832014-12-18 17:42:32 -0800514 /* At this point, the next message must be entirely behind a
515 * ChangeCipherSpec. */
516 if (!ssl3_expect_change_cipher_spec(s)) {
517 ret = -1;
518 goto end;
519 }
520 if (next_proto_neg) {
521 s->state = SSL3_ST_SR_NEXT_PROTO_A;
522 } else if (channel_id) {
523 s->state = SSL3_ST_SR_CHANNEL_ID_A;
524 } else {
525 s->state = SSL3_ST_SR_FINISHED_A;
526 }
527 break;
528 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700529
Adam Langleyfcf25832014-12-18 17:42:32 -0800530 case SSL3_ST_SR_NEXT_PROTO_A:
531 case SSL3_ST_SR_NEXT_PROTO_B:
532 ret = ssl3_get_next_proto(s);
533 if (ret <= 0) {
534 goto end;
535 }
536 s->init_num = 0;
537 if (s->s3->tlsext_channel_id_valid) {
538 s->state = SSL3_ST_SR_CHANNEL_ID_A;
539 } else {
540 s->state = SSL3_ST_SR_FINISHED_A;
541 }
542 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700543
Adam Langleyfcf25832014-12-18 17:42:32 -0800544 case SSL3_ST_SR_CHANNEL_ID_A:
545 case SSL3_ST_SR_CHANNEL_ID_B:
546 ret = ssl3_get_channel_id(s);
547 if (ret <= 0) {
548 goto end;
549 }
550 s->init_num = 0;
551 s->state = SSL3_ST_SR_FINISHED_A;
552 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700553
Adam Langleyfcf25832014-12-18 17:42:32 -0800554 case SSL3_ST_SR_FINISHED_A:
555 case SSL3_ST_SR_FINISHED_B:
556 ret =
557 ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, SSL3_ST_SR_FINISHED_B);
558 if (ret <= 0) {
559 goto end;
560 }
Adam Langley95c29f32014-06-20 12:00:00 -0700561
Adam Langleyfcf25832014-12-18 17:42:32 -0800562 if (s->hit) {
563 s->state = SSL_ST_OK;
564 } else if (s->tlsext_ticket_expected) {
565 s->state = SSL3_ST_SW_SESSION_TICKET_A;
566 } else {
567 s->state = SSL3_ST_SW_CHANGE_A;
568 }
569 /* If this is a full handshake with ChannelID then record the hashshake
570 * hashes in |s->session| in case we need them to verify a ChannelID
571 * signature on a resumption of this session in the future. */
572 if (!s->hit && s->s3->tlsext_channel_id_new) {
573 ret = tls1_record_handshake_hashes_for_channel_id(s);
574 if (ret <= 0) {
575 goto end;
576 }
577 }
578 s->init_num = 0;
579 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700580
Adam Langleyfcf25832014-12-18 17:42:32 -0800581 case SSL3_ST_SW_SESSION_TICKET_A:
582 case SSL3_ST_SW_SESSION_TICKET_B:
583 ret = ssl3_send_new_session_ticket(s);
584 if (ret <= 0) {
585 goto end;
586 }
587 s->state = SSL3_ST_SW_CHANGE_A;
588 s->init_num = 0;
589 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700590
Adam Langleyfcf25832014-12-18 17:42:32 -0800591 case SSL3_ST_SW_CHANGE_A:
592 case SSL3_ST_SW_CHANGE_B:
593 s->session->cipher = s->s3->tmp.new_cipher;
594 if (!s->enc_method->setup_key_block(s)) {
595 ret = -1;
596 goto end;
597 }
Adam Langley95c29f32014-06-20 12:00:00 -0700598
Adam Langleyfcf25832014-12-18 17:42:32 -0800599 ret = ssl3_send_change_cipher_spec(s, SSL3_ST_SW_CHANGE_A,
600 SSL3_ST_SW_CHANGE_B);
601 if (ret <= 0) {
602 goto end;
603 }
604 s->state = SSL3_ST_SW_FINISHED_A;
605 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700606
Adam Langleyfcf25832014-12-18 17:42:32 -0800607 if (!s->enc_method->change_cipher_state(
608 s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
609 ret = -1;
610 goto end;
611 }
612 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700613
Adam Langleyfcf25832014-12-18 17:42:32 -0800614 case SSL3_ST_SW_FINISHED_A:
615 case SSL3_ST_SW_FINISHED_B:
616 ret =
617 ssl3_send_finished(s, SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B,
618 s->enc_method->server_finished_label,
619 s->enc_method->server_finished_label_len);
620 if (ret <= 0) {
621 goto end;
622 }
623 s->state = SSL3_ST_SW_FLUSH;
624 if (s->hit) {
625 s->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
626 } else {
627 s->s3->tmp.next_state = SSL_ST_OK;
628 }
629 s->init_num = 0;
630 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700631
Adam Langleyfcf25832014-12-18 17:42:32 -0800632 case SSL_ST_OK:
633 /* clean a few things up */
634 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 BUF_MEM_free(s->init_buf);
637 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700638
Adam Langleyfcf25832014-12-18 17:42:32 -0800639 /* remove buffering on output */
640 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700641
Adam Langleyfcf25832014-12-18 17:42:32 -0800642 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700643
Adam Langleyfcf25832014-12-18 17:42:32 -0800644 /* If we aren't retaining peer certificates then we can discard it
645 * now. */
646 if (s->session->peer && s->ctx->retain_only_sha256_of_client_certs) {
647 X509_free(s->session->peer);
648 s->session->peer = NULL;
649 }
Adam Langley95c29f32014-06-20 12:00:00 -0700650
Adam Langleyfcf25832014-12-18 17:42:32 -0800651 if (s->renegotiate == 2) {
652 /* skipped if we just sent a HelloRequest */
653 s->renegotiate = 0;
654 s->new_session = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700655
Adam Langleyfcf25832014-12-18 17:42:32 -0800656 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
Adam Langley95c29f32014-06-20 12:00:00 -0700657
Adam Langleyfcf25832014-12-18 17:42:32 -0800658 s->ctx->stats.sess_accept_good++;
Adam Langley95c29f32014-06-20 12:00:00 -0700659
Adam Langleyfcf25832014-12-18 17:42:32 -0800660 if (cb != NULL) {
661 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
662 }
663 }
Adam Langley75872532014-06-20 12:00:00 -0700664
Adam Langleyfcf25832014-12-18 17:42:32 -0800665 ret = 1;
666 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700667
Adam Langleyfcf25832014-12-18 17:42:32 -0800668 default:
669 OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNKNOWN_STATE);
670 ret = -1;
671 goto end;
672 }
Adam Langley95c29f32014-06-20 12:00:00 -0700673
Adam Langleyfcf25832014-12-18 17:42:32 -0800674 if (!s->s3->tmp.reuse_message && !skip && cb != NULL && s->state != state) {
675 new_state = s->state;
676 s->state = state;
677 cb(s, SSL_CB_ACCEPT_LOOP, 1);
678 s->state = new_state;
679 }
680 skip = 0;
681 }
682
Adam Langley95c29f32014-06-20 12:00:00 -0700683end:
Adam Langleyfcf25832014-12-18 17:42:32 -0800684 s->in_handshake--;
685 if (buf != NULL) {
686 BUF_MEM_free(buf);
687 }
688 if (cb != NULL) {
689 cb(s, SSL_CB_ACCEPT_EXIT, ret);
690 }
691 return ret;
692}
Adam Langley95c29f32014-06-20 12:00:00 -0700693
Adam Langleyfcf25832014-12-18 17:42:32 -0800694static int ssl3_read_sniff_buffer(SSL *s, size_t n) {
695 if (s->s3->sniff_buffer == NULL) {
696 s->s3->sniff_buffer = BUF_MEM_new();
697 }
698 if (s->s3->sniff_buffer == NULL || !BUF_MEM_grow(s->s3->sniff_buffer, n)) {
699 return -1;
700 }
Adam Langley95c29f32014-06-20 12:00:00 -0700701
Adam Langleyfcf25832014-12-18 17:42:32 -0800702 while (s->s3->sniff_buffer_len < n) {
703 int ret;
David Benjamin4b755cb2014-12-12 03:58:07 -0500704
Adam Langleyfcf25832014-12-18 17:42:32 -0800705 s->rwstate = SSL_READING;
706 ret = BIO_read(s->rbio, s->s3->sniff_buffer->data + s->s3->sniff_buffer_len,
707 n - s->s3->sniff_buffer_len);
708 if (ret <= 0) {
709 return ret;
710 }
711 s->rwstate = SSL_NOTHING;
712 s->s3->sniff_buffer_len += ret;
713 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500714
Adam Langleyfcf25832014-12-18 17:42:32 -0800715 return 1;
716}
David Benjamin4b755cb2014-12-12 03:58:07 -0500717
Adam Langleyfcf25832014-12-18 17:42:32 -0800718int ssl3_get_initial_bytes(SSL *s) {
719 int ret;
720 const uint8_t *p;
David Benjamin4b755cb2014-12-12 03:58:07 -0500721
Adam Langleyfcf25832014-12-18 17:42:32 -0800722 /* Read the first 8 bytes. To recognize a ClientHello or V2ClientHello only
723 * needs the first 6 bytes, but 8 is needed to recognize CONNECT below. */
724 ret = ssl3_read_sniff_buffer(s, INITIAL_SNIFF_BUFFER_SIZE);
725 if (ret <= 0) {
726 return ret;
727 }
728 assert(s->s3->sniff_buffer_len >= INITIAL_SNIFF_BUFFER_SIZE);
729 p = (const uint8_t *)s->s3->sniff_buffer->data;
David Benjamin4b755cb2014-12-12 03:58:07 -0500730
Adam Langleyfcf25832014-12-18 17:42:32 -0800731 /* Some dedicated error codes for protocol mixups should the application wish
732 * to interpret them differently. (These do not overlap with ClientHello or
733 * V2ClientHello.) */
734 if (strncmp("GET ", (const char *)p, 4) == 0 ||
735 strncmp("POST ", (const char *)p, 5) == 0 ||
736 strncmp("HEAD ", (const char *)p, 5) == 0 ||
737 strncmp("PUT ", (const char *)p, 4) == 0) {
738 OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_HTTP_REQUEST);
739 return -1;
740 }
741 if (strncmp("CONNECT ", (const char *)p, 8) == 0) {
742 OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_HTTPS_PROXY_REQUEST);
743 return -1;
744 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500745
Adam Langleyfcf25832014-12-18 17:42:32 -0800746 /* Determine if this is a ClientHello or V2ClientHello. */
747 if ((p[0] & 0x80) && p[2] == SSL2_MT_CLIENT_HELLO &&
748 p[3] >= SSL3_VERSION_MAJOR) {
749 /* This is a V2ClientHello. */
750 s->state = SSL3_ST_SR_V2_CLIENT_HELLO;
751 return 1;
752 }
753 if (p[0] == SSL3_RT_HANDSHAKE && p[1] >= SSL3_VERSION_MAJOR &&
754 p[5] == SSL3_MT_CLIENT_HELLO) {
755 /* This is a ClientHello. Initialize the record layer with the already
756 * consumed data and continue the handshake. */
757 if (!ssl3_setup_buffers(s) || !ssl_init_wbio_buffer(s, 1)) {
758 return -1;
759 }
760 assert(s->rstate == SSL_ST_READ_HEADER);
761 memcpy(s->s3->rbuf.buf, p, s->s3->sniff_buffer_len);
762 s->s3->rbuf.offset = 0;
763 s->s3->rbuf.left = s->s3->sniff_buffer_len;
764 s->packet_length = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500765
Adam Langleyfcf25832014-12-18 17:42:32 -0800766 BUF_MEM_free(s->s3->sniff_buffer);
767 s->s3->sniff_buffer = NULL;
768 s->s3->sniff_buffer_len = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500769
Adam Langleyfcf25832014-12-18 17:42:32 -0800770 s->state = SSL3_ST_SR_CLNT_HELLO_A;
771 return 1;
772 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500773
Adam Langleyfcf25832014-12-18 17:42:32 -0800774 OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_UNKNOWN_PROTOCOL);
775 return -1;
776}
David Benjamin4b755cb2014-12-12 03:58:07 -0500777
Adam Langleyfcf25832014-12-18 17:42:32 -0800778int ssl3_get_v2_client_hello(SSL *s) {
779 const uint8_t *p;
780 int ret;
781 CBS v2_client_hello, cipher_specs, session_id, challenge;
782 size_t msg_length, rand_len, len;
783 uint8_t msg_type;
784 uint16_t version, cipher_spec_length, session_id_length, challenge_length;
785 CBB client_hello, hello_body, cipher_suites;
786 uint8_t random[SSL3_RANDOM_SIZE];
David Benjamin4b755cb2014-12-12 03:58:07 -0500787
Adam Langleyfcf25832014-12-18 17:42:32 -0800788 /* Read the remainder of the V2ClientHello. We have previously read 8 bytes
789 * in ssl3_get_initial_bytes. */
790 assert(s->s3->sniff_buffer_len >= INITIAL_SNIFF_BUFFER_SIZE);
791 p = (const uint8_t *)s->s3->sniff_buffer->data;
792 msg_length = ((p[0] & 0x7f) << 8) | p[1];
793 if (msg_length > (1024 * 4)) {
794 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_RECORD_TOO_LARGE);
795 return -1;
796 }
797 if (msg_length < INITIAL_SNIFF_BUFFER_SIZE - 2) {
798 /* Reject lengths that are too short early. We have already read 8 bytes,
799 * so we should not attempt to process an (invalid) V2ClientHello which
800 * would be shorter than that. */
801 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello,
802 SSL_R_RECORD_LENGTH_MISMATCH);
803 return -1;
804 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500805
Adam Langleyfcf25832014-12-18 17:42:32 -0800806 ret = ssl3_read_sniff_buffer(s, msg_length + 2);
807 if (ret <= 0) {
808 return ret;
809 }
810 assert(s->s3->sniff_buffer_len == msg_length + 2);
811 CBS_init(&v2_client_hello, (const uint8_t *)s->s3->sniff_buffer->data + 2,
812 msg_length);
David Benjamin4b755cb2014-12-12 03:58:07 -0500813
Adam Langleyfcf25832014-12-18 17:42:32 -0800814 /* The V2ClientHello without the length is incorporated into the Finished
815 * hash. */
816 ssl3_finish_mac(s, CBS_data(&v2_client_hello), CBS_len(&v2_client_hello));
817 if (s->msg_callback) {
818 s->msg_callback(0, SSL2_VERSION, 0, CBS_data(&v2_client_hello),
819 CBS_len(&v2_client_hello), s, s->msg_callback_arg);
820 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500821
Adam Langleyfcf25832014-12-18 17:42:32 -0800822 if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
823 !CBS_get_u16(&v2_client_hello, &version) ||
824 !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
825 !CBS_get_u16(&v2_client_hello, &session_id_length) ||
826 !CBS_get_u16(&v2_client_hello, &challenge_length) ||
827 !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
828 !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
829 !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
830 CBS_len(&v2_client_hello) != 0) {
831 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_DECODE_ERROR);
832 return -1;
833 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500834
Adam Langleyfcf25832014-12-18 17:42:32 -0800835 /* msg_type has already been checked. */
836 assert(msg_type == SSL2_MT_CLIENT_HELLO);
David Benjamin4b755cb2014-12-12 03:58:07 -0500837
Adam Langleyfcf25832014-12-18 17:42:32 -0800838 /* The client_random is the V2ClientHello challenge. Truncate or
839 * left-pad with zeros as needed. */
840 memset(random, 0, SSL3_RANDOM_SIZE);
841 rand_len = CBS_len(&challenge);
842 if (rand_len > SSL3_RANDOM_SIZE) {
843 rand_len = SSL3_RANDOM_SIZE;
844 }
845 memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
846 rand_len);
David Benjamin4b755cb2014-12-12 03:58:07 -0500847
Adam Langleyfcf25832014-12-18 17:42:32 -0800848 /* Write out an equivalent SSLv3 ClientHello. */
849 if (!CBB_init_fixed(&client_hello, (uint8_t *)s->init_buf->data,
850 s->init_buf->max)) {
851 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_MALLOC_FAILURE);
852 return -1;
853 }
854 if (!CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
855 !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
856 !CBB_add_u16(&hello_body, version) ||
857 !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
858 /* No session id. */
859 !CBB_add_u8(&hello_body, 0) ||
860 !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
861 CBB_cleanup(&client_hello);
862 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
863 return -1;
864 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500865
Adam Langleyfcf25832014-12-18 17:42:32 -0800866 /* Copy the cipher suites. */
867 while (CBS_len(&cipher_specs) > 0) {
868 uint32_t cipher_spec;
869 if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
870 CBB_cleanup(&client_hello);
871 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_DECODE_ERROR);
872 return -1;
873 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500874
Adam Langleyfcf25832014-12-18 17:42:32 -0800875 /* Skip SSLv2 ciphers. */
876 if ((cipher_spec & 0xff0000) != 0) {
877 continue;
878 }
879 if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
880 CBB_cleanup(&client_hello);
881 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
882 return -1;
883 }
884 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500885
Adam Langleyfcf25832014-12-18 17:42:32 -0800886 /* Add the null compression scheme and finish. */
887 if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) ||
888 !CBB_finish(&client_hello, NULL, &len)) {
889 CBB_cleanup(&client_hello);
890 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
891 return -1;
892 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500893
Adam Langleyfcf25832014-12-18 17:42:32 -0800894 /* Mark the message for "re"-use by the version-specific method. */
895 s->s3->tmp.reuse_message = 1;
896 s->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO;
897 /* The handshake message header is 4 bytes. */
898 s->s3->tmp.message_size = len - 4;
David Benjamin4b755cb2014-12-12 03:58:07 -0500899
Adam Langleyfcf25832014-12-18 17:42:32 -0800900 /* Initialize the record layer. */
901 if (!ssl3_setup_buffers(s) || !ssl_init_wbio_buffer(s, 1)) {
902 return -1;
903 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500904
Adam Langleyfcf25832014-12-18 17:42:32 -0800905 /* Drop the sniff buffer. */
906 BUF_MEM_free(s->s3->sniff_buffer);
907 s->s3->sniff_buffer = NULL;
908 s->s3->sniff_buffer_len = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500909
Adam Langleyfcf25832014-12-18 17:42:32 -0800910 return 1;
911}
David Benjamin4b755cb2014-12-12 03:58:07 -0500912
Adam Langleyfcf25832014-12-18 17:42:32 -0800913int ssl3_send_hello_request(SSL *s) {
914 if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
915 ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0);
916 s->state = SSL3_ST_SW_HELLO_REQ_B;
917 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500918
Adam Langleyfcf25832014-12-18 17:42:32 -0800919 /* SSL3_ST_SW_HELLO_REQ_B */
920 return ssl_do_write(s);
921}
Adam Langley95c29f32014-06-20 12:00:00 -0700922
Adam Langleyfcf25832014-12-18 17:42:32 -0800923int ssl3_get_client_hello(SSL *s) {
924 int i, ok, al = SSL_AD_INTERNAL_ERROR, ret = -1;
925 long n;
926 const SSL_CIPHER *c;
927 STACK_OF(SSL_CIPHER) *ciphers = NULL;
928 struct ssl_early_callback_ctx early_ctx;
929 CBS client_hello;
930 uint16_t client_version;
931 CBS client_random, session_id, cipher_suites, compression_methods;
Adam Langley95c29f32014-06-20 12:00:00 -0700932
Adam Langleyfcf25832014-12-18 17:42:32 -0800933 /* We do this so that we will respond with our native type. If we are TLSv1
934 * and we get SSLv3, we will respond with TLSv1, This down switching should
935 * be handled by a different method. If we are SSLv3, we will respond with
936 * SSLv3, even if prompted with TLSv1. */
937 switch (s->state) {
938 case SSL3_ST_SR_CLNT_HELLO_A:
939 case SSL3_ST_SR_CLNT_HELLO_B:
940 n = s->method->ssl_get_message(
941 s, SSL3_ST_SR_CLNT_HELLO_A, SSL3_ST_SR_CLNT_HELLO_B,
942 SSL3_MT_CLIENT_HELLO, SSL3_RT_MAX_PLAIN_LENGTH,
943 SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700944
Adam Langleyfcf25832014-12-18 17:42:32 -0800945 if (!ok) {
946 return n;
947 }
Adam Langley95c29f32014-06-20 12:00:00 -0700948
Adam Langleyfcf25832014-12-18 17:42:32 -0800949 /* If we require cookies and this ClientHello doesn't contain one, just
950 * return since we do not want to allocate any memory yet. So check
951 * cookie length... */
952 if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) {
953 uint8_t cookie_length;
Adam Langley95c29f32014-06-20 12:00:00 -0700954
Adam Langleyfcf25832014-12-18 17:42:32 -0800955 CBS_init(&client_hello, s->init_msg, n);
956 if (!CBS_skip(&client_hello, 2 + SSL3_RANDOM_SIZE) ||
957 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
958 !CBS_get_u8(&client_hello, &cookie_length)) {
959 al = SSL_AD_DECODE_ERROR;
960 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
961 goto f_err;
962 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700963
Adam Langleyfcf25832014-12-18 17:42:32 -0800964 if (cookie_length == 0) {
965 return 1;
966 }
967 }
968 s->state = SSL3_ST_SR_CLNT_HELLO_C;
969 /* fallthrough */
970 case SSL3_ST_SR_CLNT_HELLO_C:
971 case SSL3_ST_SR_CLNT_HELLO_D:
972 /* We have previously parsed the ClientHello message, and can't call
973 * ssl_get_message again without hashing the message into the Finished
974 * digest again. */
975 n = s->init_num;
Adam Langleydc9b1412014-06-20 12:00:00 -0700976
Adam Langleyfcf25832014-12-18 17:42:32 -0800977 memset(&early_ctx, 0, sizeof(early_ctx));
978 early_ctx.ssl = s;
979 early_ctx.client_hello = s->init_msg;
980 early_ctx.client_hello_len = n;
981 if (!ssl_early_callback_init(&early_ctx)) {
982 al = SSL_AD_DECODE_ERROR;
983 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
984 SSL_R_CLIENTHELLO_PARSE_FAILED);
985 goto f_err;
986 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400987
Adam Langleyfcf25832014-12-18 17:42:32 -0800988 if (s->state == SSL3_ST_SR_CLNT_HELLO_C &&
989 s->ctx->select_certificate_cb != NULL) {
990 s->state = SSL3_ST_SR_CLNT_HELLO_D;
991 switch (s->ctx->select_certificate_cb(&early_ctx)) {
992 case 0:
David Benjamin2fff5bf2015-02-09 04:32:28 -0500993 s->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
994 goto err;
Adam Langleydc9b1412014-06-20 12:00:00 -0700995
Adam Langleyfcf25832014-12-18 17:42:32 -0800996 case -1:
997 /* Connection rejected. */
998 al = SSL_AD_ACCESS_DENIED;
999 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
1000 SSL_R_CONNECTION_REJECTED);
1001 goto f_err;
Adam Langleydc9b1412014-06-20 12:00:00 -07001002
Adam Langleyfcf25832014-12-18 17:42:32 -08001003 default:
1004 /* fallthrough */;
1005 }
1006 }
1007 s->state = SSL3_ST_SR_CLNT_HELLO_D;
1008 break;
Adam Langleyb2ce0582014-06-20 12:00:00 -07001009
Adam Langleyfcf25832014-12-18 17:42:32 -08001010 default:
1011 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNKNOWN_STATE);
1012 return -1;
1013 }
Adam Langley95c29f32014-06-20 12:00:00 -07001014
Adam Langleyfcf25832014-12-18 17:42:32 -08001015 CBS_init(&client_hello, s->init_msg, n);
1016 if (!CBS_get_u16(&client_hello, &client_version) ||
1017 !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) ||
1018 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
1019 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1020 al = SSL_AD_DECODE_ERROR;
1021 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
1022 goto f_err;
1023 }
Adam Langley95c29f32014-06-20 12:00:00 -07001024
Adam Langleyfcf25832014-12-18 17:42:32 -08001025 /* use version from inside client hello, not from record header (may differ:
1026 * see RFC 2246, Appendix E, second paragraph) */
1027 s->client_version = client_version;
Adam Langley95c29f32014-06-20 12:00:00 -07001028
Adam Langleyfcf25832014-12-18 17:42:32 -08001029 /* Load the client random. */
1030 memcpy(s->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -07001031
Adam Langleyfcf25832014-12-18 17:42:32 -08001032 if (SSL_IS_DTLS(s)) {
1033 CBS cookie;
Adam Langley95c29f32014-06-20 12:00:00 -07001034
Adam Langleyfcf25832014-12-18 17:42:32 -08001035 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
1036 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
1037 al = SSL_AD_DECODE_ERROR;
1038 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
1039 goto f_err;
1040 }
Adam Langley95c29f32014-06-20 12:00:00 -07001041
Adam Langleyfcf25832014-12-18 17:42:32 -08001042 /* Verify the cookie if appropriate option is set. */
1043 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && CBS_len(&cookie) > 0) {
1044 if (s->ctx->app_verify_cookie_cb != NULL) {
1045 if (s->ctx->app_verify_cookie_cb(s, CBS_data(&cookie),
1046 CBS_len(&cookie)) == 0) {
1047 al = SSL_AD_HANDSHAKE_FAILURE;
1048 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH);
1049 goto f_err;
1050 }
1051 /* else cookie verification succeeded */
1052 } else if (!CBS_mem_equal(&cookie, s->d1->cookie, s->d1->cookie_len)) {
1053 /* default verification */
1054 al = SSL_AD_HANDSHAKE_FAILURE;
1055 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH);
1056 goto f_err;
1057 }
1058 /* Set to -2 so if successful we return 2 and don't send
1059 * HelloVerifyRequest. */
1060 ret = -2;
1061 }
1062 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001063
Adam Langleyfcf25832014-12-18 17:42:32 -08001064 if (!s->s3->have_version) {
1065 /* Select version to use */
1066 uint16_t version = ssl3_get_mutual_version(s, client_version);
1067 if (version == 0) {
1068 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL);
1069 s->version = s->client_version;
1070 al = SSL_AD_PROTOCOL_VERSION;
1071 goto f_err;
1072 }
1073 s->version = version;
1074 s->enc_method = ssl3_get_enc_method(version);
1075 assert(s->enc_method != NULL);
1076 /* At this point, the connection's version is known and |s->version| is
1077 * fixed. Begin enforcing the record-layer version. */
1078 s->s3->have_version = 1;
1079 } else if (SSL_IS_DTLS(s) ? (s->client_version > s->version)
1080 : (s->client_version < s->version)) {
1081 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER);
1082 al = SSL_AD_PROTOCOL_VERSION;
1083 goto f_err;
1084 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001085
Adam Langleyfcf25832014-12-18 17:42:32 -08001086 s->hit = 0;
1087 /* Versions before 0.9.7 always allow clients to resume sessions in
1088 * renegotiation. 0.9.7 and later allow this by default, but optionally
1089 * ignore resumption requests with flag
1090 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather than
1091 * a change to default behavior so that applications relying on this for
1092 * security won't even compile against older library versions).
1093 *
1094 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1095 * request renegotiation but not a new session (s->new_session remains
1096 * unset): for servers, this essentially just means that the
1097 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be ignored. */
1098 if (s->new_session &&
1099 (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)) {
1100 if (!ssl_get_new_session(s, 1)) {
1101 goto err;
1102 }
1103 } else {
1104 i = ssl_get_prev_session(s, &early_ctx);
1105 if (i == PENDING_SESSION) {
David Benjamin2fff5bf2015-02-09 04:32:28 -05001106 s->rwstate = SSL_PENDING_SESSION;
Adam Langleyfcf25832014-12-18 17:42:32 -08001107 goto err;
1108 } else if (i == -1) {
1109 goto err;
1110 }
Adam Langley95c29f32014-06-20 12:00:00 -07001111
Adam Langleyfcf25832014-12-18 17:42:32 -08001112 /* Only resume if the session's version matches the negotiated version:
1113 * most clients do not accept a mismatch. */
1114 if (i == 1 && s->version == s->session->ssl_version) {
1115 s->hit = 1;
1116 } else {
1117 /* No session was found or it was unacceptable. */
1118 if (!ssl_get_new_session(s, 1)) {
1119 goto err;
1120 }
1121 }
1122 }
David Benjamin22f9bcc2014-07-13 12:29:21 -04001123
Adam Langleyfcf25832014-12-18 17:42:32 -08001124 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
1125 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
1126 CBS_len(&compression_methods) == 0) {
1127 al = SSL_AD_DECODE_ERROR;
1128 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
1129 goto f_err;
1130 }
David Benjamin22f9bcc2014-07-13 12:29:21 -04001131
Adam Langleyfcf25832014-12-18 17:42:32 -08001132 /* TODO(davidben): Per spec, cipher_suites can never be empty (specified at
1133 * the ClientHello structure level). This logic allows it to be empty if
1134 * resuming a session. Can we always require non-empty? If a client sends
1135 * empty cipher_suites because it's resuming a session, it could always fail
1136 * to resume a session, so it's unlikely to actually work. */
1137 if (CBS_len(&cipher_suites) == 0 && CBS_len(&session_id) != 0) {
1138 /* We need a cipher if we are not resuming a session. */
1139 al = SSL_AD_ILLEGAL_PARAMETER;
1140 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_SPECIFIED);
1141 goto f_err;
1142 }
Adam Langley95c29f32014-06-20 12:00:00 -07001143
Adam Langleyfcf25832014-12-18 17:42:32 -08001144 ciphers = ssl_bytes_to_cipher_list(s, &cipher_suites);
1145 if (ciphers == NULL) {
1146 goto err;
1147 }
Adam Langley95c29f32014-06-20 12:00:00 -07001148
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 /* If it is a hit, check that the cipher is in the list. */
1150 if (s->hit && CBS_len(&cipher_suites) > 0) {
1151 size_t j;
1152 int found_cipher = 0;
1153 unsigned long id = s->session->cipher->id;
Adam Langley95c29f32014-06-20 12:00:00 -07001154
Adam Langleyfcf25832014-12-18 17:42:32 -08001155 for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++) {
1156 c = sk_SSL_CIPHER_value(ciphers, j);
1157 if (c->id == id) {
1158 found_cipher = 1;
1159 break;
1160 }
1161 }
Adam Langley95c29f32014-06-20 12:00:00 -07001162
Adam Langleyfcf25832014-12-18 17:42:32 -08001163 if (!found_cipher) {
1164 /* we need to have the cipher in the cipher list if we are asked to reuse
1165 * it */
1166 al = SSL_AD_ILLEGAL_PARAMETER;
1167 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
1168 SSL_R_REQUIRED_CIPHER_MISSING);
1169 goto f_err;
1170 }
1171 }
Adam Langley95c29f32014-06-20 12:00:00 -07001172
Adam Langleyfcf25832014-12-18 17:42:32 -08001173 /* Only null compression is supported. */
1174 if (memchr(CBS_data(&compression_methods), 0,
1175 CBS_len(&compression_methods)) == NULL) {
1176 al = SSL_AD_ILLEGAL_PARAMETER;
1177 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
1178 SSL_R_NO_COMPRESSION_SPECIFIED);
1179 goto f_err;
1180 }
David Benjamindc72ff72014-06-25 12:36:10 -04001181
Adam Langleyfcf25832014-12-18 17:42:32 -08001182 /* TLS extensions. */
1183 if (s->version >= SSL3_VERSION &&
1184 !ssl_parse_clienthello_tlsext(s, &client_hello)) {
1185 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_PARSE_TLSEXT);
1186 goto err;
1187 }
Adam Langley95c29f32014-06-20 12:00:00 -07001188
Adam Langleyfcf25832014-12-18 17:42:32 -08001189 /* There should be nothing left over in the record. */
1190 if (CBS_len(&client_hello) != 0) {
1191 /* wrong packet length */
1192 al = SSL_AD_DECODE_ERROR;
1193 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_BAD_PACKET_LENGTH);
1194 goto f_err;
1195 }
Adam Langley95c29f32014-06-20 12:00:00 -07001196
Adam Langleyfcf25832014-12-18 17:42:32 -08001197 /* Given ciphers and SSL_get_ciphers, we must pick a cipher */
1198 if (!s->hit) {
1199 if (ciphers == NULL) {
1200 al = SSL_AD_ILLEGAL_PARAMETER;
1201 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_PASSED);
1202 goto f_err;
1203 }
Adam Langley95c29f32014-06-20 12:00:00 -07001204
Adam Langleyfcf25832014-12-18 17:42:32 -08001205 /* Let cert callback update server certificates if required */
1206 if (s->cert->cert_cb) {
1207 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1208 if (rv == 0) {
1209 al = SSL_AD_INTERNAL_ERROR;
1210 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CERT_CB_ERROR);
1211 goto f_err;
1212 }
1213 if (rv < 0) {
1214 s->rwstate = SSL_X509_LOOKUP;
1215 goto err;
1216 }
1217 s->rwstate = SSL_NOTHING;
1218 }
1219 c = ssl3_choose_cipher(s, ciphers, ssl_get_cipher_preferences(s));
1220
1221 if (c == NULL) {
1222 al = SSL_AD_HANDSHAKE_FAILURE;
1223 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER);
1224 goto f_err;
1225 }
1226 s->s3->tmp.new_cipher = c;
1227 } else {
1228 /* Session-id reuse */
1229 s->s3->tmp.new_cipher = s->session->cipher;
1230 }
1231
1232 if ((!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) &&
1233 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1234 goto f_err;
1235 }
1236
1237 /* we now have the following setup;
1238 * client_random
1239 * cipher_list - our prefered list of ciphers
1240 * ciphers - the clients prefered list of ciphers
1241 * compression - basically ignored right now
1242 * ssl version is set - sslv3
1243 * s->session - The ssl session has been setup.
1244 * s->hit - session reuse flag
1245 * s->tmp.new_cipher - the new cipher to use. */
1246
1247 if (ret < 0) {
1248 ret = -ret;
1249 }
1250
1251 if (0) {
1252 f_err:
1253 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1254 }
1255
Adam Langley95c29f32014-06-20 12:00:00 -07001256err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001257 if (ciphers != NULL) {
1258 sk_SSL_CIPHER_free(ciphers);
1259 }
1260 return ret;
1261}
Adam Langley95c29f32014-06-20 12:00:00 -07001262
Adam Langleyfcf25832014-12-18 17:42:32 -08001263int ssl3_send_server_hello(SSL *s) {
1264 uint8_t *buf;
1265 uint8_t *p, *d;
1266 int sl;
1267 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001268
Adam Langleyfcf25832014-12-18 17:42:32 -08001269 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1270 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1271 * known attack while we fix ChannelID itself. */
1272 if (s->s3->tlsext_channel_id_valid &&
1273 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kEECDH) == 0) {
1274 s->s3->tlsext_channel_id_valid = 0;
1275 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001276
Adam Langleyfcf25832014-12-18 17:42:32 -08001277 /* If this is a resumption and the original handshake didn't support
1278 * ChannelID then we didn't record the original handshake hashes in the
1279 * session and so cannot resume with ChannelIDs. */
1280 if (s->hit && s->s3->tlsext_channel_id_new &&
1281 s->session->original_handshake_hash_len == 0) {
1282 s->s3->tlsext_channel_id_valid = 0;
1283 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001284
Adam Langleyfcf25832014-12-18 17:42:32 -08001285 buf = (uint8_t *)s->init_buf->data;
1286 /* Do the message type and length last */
1287 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001288
Adam Langleyfcf25832014-12-18 17:42:32 -08001289 *(p++) = s->version >> 8;
1290 *(p++) = s->version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -07001291
Adam Langleyfcf25832014-12-18 17:42:32 -08001292 /* Random stuff */
1293 if (!ssl_fill_hello_random(s, 1, s->s3->server_random, SSL3_RANDOM_SIZE)) {
1294 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1295 return -1;
1296 }
1297 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1298 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -07001299
Adam Langleyfcf25832014-12-18 17:42:32 -08001300 /* There are several cases for the session ID to send
1301 * back in the server hello:
1302 * - For session reuse from the session cache, we send back the old session
1303 * ID.
1304 * - If stateless session reuse (using a session ticket) is successful, we
1305 * send back the client's "session ID" (which doesn't actually identify
1306 * the session).
1307 * - If it is a new session, we send back the new session ID.
1308 * - However, if we want the new session to be single-use, we send back a
1309 * 0-length session ID.
1310 * s->hit is non-zero in either case of session reuse, so the following
1311 * won't overwrite an ID that we're supposed to send back. */
1312 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) && !s->hit) {
1313 s->session->session_id_length = 0;
1314 }
Adam Langley95c29f32014-06-20 12:00:00 -07001315
Adam Langleyfcf25832014-12-18 17:42:32 -08001316 sl = s->session->session_id_length;
1317 if (sl > (int)sizeof(s->session->session_id)) {
1318 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1319 return -1;
1320 }
1321 *(p++) = sl;
1322 memcpy(p, s->session->session_id, sl);
1323 p += sl;
Adam Langley95c29f32014-06-20 12:00:00 -07001324
Adam Langleyfcf25832014-12-18 17:42:32 -08001325 /* put the cipher */
1326 s2n(ssl3_get_cipher_value(s->s3->tmp.new_cipher), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001327
Adam Langleyfcf25832014-12-18 17:42:32 -08001328 /* put the compression method */
1329 *(p++) = 0;
1330 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1331 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, SSL_R_SERVERHELLO_TLSEXT);
1332 return -1;
1333 }
1334 p = ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH);
1335 if (p == NULL) {
1336 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1337 return -1;
1338 }
Adam Langley95c29f32014-06-20 12:00:00 -07001339
Adam Langleyfcf25832014-12-18 17:42:32 -08001340 /* do the header */
1341 l = (p - d);
1342 ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l);
1343 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1344 }
Adam Langley95c29f32014-06-20 12:00:00 -07001345
Adam Langleyfcf25832014-12-18 17:42:32 -08001346 /* SSL3_ST_SW_SRVR_HELLO_B */
1347 return ssl_do_write(s);
1348}
Adam Langley95c29f32014-06-20 12:00:00 -07001349
Adam Langleyfcf25832014-12-18 17:42:32 -08001350int ssl3_send_server_done(SSL *s) {
1351 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1352 ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0);
1353 s->state = SSL3_ST_SW_SRVR_DONE_B;
1354 }
Adam Langley95c29f32014-06-20 12:00:00 -07001355
Adam Langleyfcf25832014-12-18 17:42:32 -08001356 /* SSL3_ST_SW_SRVR_DONE_B */
1357 return ssl_do_write(s);
1358}
Adam Langley95c29f32014-06-20 12:00:00 -07001359
Adam Langleyfcf25832014-12-18 17:42:32 -08001360int ssl3_send_server_key_exchange(SSL *s) {
1361 DH *dh = NULL, *dhp;
1362 EC_KEY *ecdh = NULL, *ecdhp;
1363 uint8_t *encodedPoint = NULL;
1364 int encodedlen = 0;
David Benjamin70bd80a2014-12-27 03:06:46 -05001365 uint16_t curve_id = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001366 BN_CTX *bn_ctx = NULL;
1367 const char *psk_identity_hint = NULL;
1368 size_t psk_identity_hint_len = 0;
1369 EVP_PKEY *pkey;
1370 uint8_t *p, *d;
1371 int al, i;
1372 unsigned long alg_k;
1373 unsigned long alg_a;
1374 int n;
1375 CERT *cert;
1376 BIGNUM *r[4];
1377 int nr[4], kn;
1378 BUF_MEM *buf;
1379 EVP_MD_CTX md_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07001380
Adam Langleyfcf25832014-12-18 17:42:32 -08001381 EVP_MD_CTX_init(&md_ctx);
1382 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1383 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1384 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1385 cert = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07001386
Adam Langleyfcf25832014-12-18 17:42:32 -08001387 buf = s->init_buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001388
Adam Langleyfcf25832014-12-18 17:42:32 -08001389 r[0] = r[1] = r[2] = r[3] = NULL;
1390 n = 0;
1391 if (alg_a & SSL_aPSK) {
1392 /* size for PSK identity hint */
1393 psk_identity_hint = s->psk_identity_hint;
1394 if (psk_identity_hint) {
1395 psk_identity_hint_len = strlen(psk_identity_hint);
1396 } else {
1397 psk_identity_hint_len = 0;
1398 }
1399 n += 2 + psk_identity_hint_len;
1400 }
Adam Langley95c29f32014-06-20 12:00:00 -07001401
Adam Langleyfcf25832014-12-18 17:42:32 -08001402 if (alg_k & SSL_kEDH) {
1403 dhp = cert->dh_tmp;
1404 if (dhp == NULL && s->cert->dh_tmp_cb != NULL) {
1405 dhp = s->cert->dh_tmp_cb(s, 0, 1024);
1406 }
1407 if (dhp == NULL) {
1408 al = SSL_AD_HANDSHAKE_FAILURE;
1409 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1410 SSL_R_MISSING_TMP_DH_KEY);
1411 goto f_err;
1412 }
Adam Langley95c29f32014-06-20 12:00:00 -07001413
Adam Langleyfcf25832014-12-18 17:42:32 -08001414 if (s->s3->tmp.dh != NULL) {
1415 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1416 ERR_R_INTERNAL_ERROR);
1417 goto err;
1418 }
Adam Langley95c29f32014-06-20 12:00:00 -07001419
Adam Langleyfcf25832014-12-18 17:42:32 -08001420 dh = DHparams_dup(dhp);
1421 if (dh == NULL) {
1422 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1423 goto err;
1424 }
Adam Langley95c29f32014-06-20 12:00:00 -07001425
Adam Langleyfcf25832014-12-18 17:42:32 -08001426 s->s3->tmp.dh = dh;
1427 if (dhp->pub_key == NULL || dhp->priv_key == NULL ||
1428 (s->options & SSL_OP_SINGLE_DH_USE)) {
1429 if (!DH_generate_key(dh)) {
1430 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1431 goto err;
1432 }
1433 } else {
1434 dh->pub_key = BN_dup(dhp->pub_key);
1435 dh->priv_key = BN_dup(dhp->priv_key);
1436 if (dh->pub_key == NULL || dh->priv_key == NULL) {
1437 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1438 goto err;
1439 }
1440 }
Adam Langley95c29f32014-06-20 12:00:00 -07001441
Adam Langleyfcf25832014-12-18 17:42:32 -08001442 r[0] = dh->p;
1443 r[1] = dh->g;
1444 r[2] = dh->pub_key;
1445 } else if (alg_k & SSL_kEECDH) {
1446 const EC_GROUP *group;
Adam Langley95c29f32014-06-20 12:00:00 -07001447
Adam Langleyfcf25832014-12-18 17:42:32 -08001448 ecdhp = cert->ecdh_tmp;
1449 if (s->cert->ecdh_tmp_auto) {
1450 /* Get NID of appropriate shared curve */
1451 int nid = tls1_get_shared_curve(s);
1452 if (nid != NID_undef) {
1453 ecdhp = EC_KEY_new_by_curve_name(nid);
1454 }
1455 } else if (ecdhp == NULL && s->cert->ecdh_tmp_cb) {
1456 ecdhp = s->cert->ecdh_tmp_cb(s, 0, 1024);
1457 }
1458 if (ecdhp == NULL) {
1459 al = SSL_AD_HANDSHAKE_FAILURE;
1460 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1461 SSL_R_MISSING_TMP_ECDH_KEY);
1462 goto f_err;
1463 }
Adam Langley95c29f32014-06-20 12:00:00 -07001464
Adam Langleyfcf25832014-12-18 17:42:32 -08001465 if (s->s3->tmp.ecdh != NULL) {
1466 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1467 ERR_R_INTERNAL_ERROR);
1468 goto err;
1469 }
Adam Langley95c29f32014-06-20 12:00:00 -07001470
Adam Langleyfcf25832014-12-18 17:42:32 -08001471 /* Duplicate the ECDH structure. */
1472 if (ecdhp == NULL) {
1473 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1474 goto err;
1475 }
Adam Langley95c29f32014-06-20 12:00:00 -07001476
Adam Langleyfcf25832014-12-18 17:42:32 -08001477 if (s->cert->ecdh_tmp_auto) {
1478 ecdh = ecdhp;
1479 } else {
1480 ecdh = EC_KEY_dup(ecdhp);
1481 if (ecdh == NULL) {
1482 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1483 goto err;
1484 }
1485 }
Adam Langley95c29f32014-06-20 12:00:00 -07001486
Adam Langleyfcf25832014-12-18 17:42:32 -08001487 s->s3->tmp.ecdh = ecdh;
1488 if (EC_KEY_get0_public_key(ecdh) == NULL ||
1489 EC_KEY_get0_private_key(ecdh) == NULL ||
1490 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1491 if (!EC_KEY_generate_key(ecdh)) {
1492 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1493 goto err;
1494 }
1495 }
Adam Langley95c29f32014-06-20 12:00:00 -07001496
Adam Langleyfcf25832014-12-18 17:42:32 -08001497 group = EC_KEY_get0_group(ecdh);
1498 if (group == NULL ||
1499 EC_KEY_get0_public_key(ecdh) == NULL ||
1500 EC_KEY_get0_private_key(ecdh) == NULL) {
1501 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1502 goto err;
1503 }
Adam Langley95c29f32014-06-20 12:00:00 -07001504
David Benjamin70bd80a2014-12-27 03:06:46 -05001505 /* We only support ephemeral ECDH keys over named (not generic) curves. */
1506 if (!tls1_ec_nid2curve_id(&curve_id, EC_GROUP_get_curve_name(group))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001507 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1508 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1509 goto err;
1510 }
Adam Langley95c29f32014-06-20 12:00:00 -07001511
Adam Langleyfcf25832014-12-18 17:42:32 -08001512 /* Encode the public key. First check the size of encoding and allocate
1513 * memory accordingly. */
1514 encodedlen =
1515 EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
1516 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001517
Adam Langleyfcf25832014-12-18 17:42:32 -08001518 encodedPoint = (uint8_t *)OPENSSL_malloc(encodedlen * sizeof(uint8_t));
1519 bn_ctx = BN_CTX_new();
1520 if (encodedPoint == NULL || bn_ctx == NULL) {
1521 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1522 ERR_R_MALLOC_FAILURE);
1523 goto err;
1524 }
Adam Langley95c29f32014-06-20 12:00:00 -07001525
Adam Langleyfcf25832014-12-18 17:42:32 -08001526 encodedlen = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
1527 POINT_CONVERSION_UNCOMPRESSED,
1528 encodedPoint, encodedlen, bn_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07001529
Adam Langleyfcf25832014-12-18 17:42:32 -08001530 if (encodedlen == 0) {
1531 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1532 goto err;
1533 }
Adam Langley95c29f32014-06-20 12:00:00 -07001534
Adam Langleyfcf25832014-12-18 17:42:32 -08001535 BN_CTX_free(bn_ctx);
1536 bn_ctx = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001537
Adam Langleyfcf25832014-12-18 17:42:32 -08001538 /* We only support named (not generic) curves in ECDH ephemeral key
1539 * exchanges. In this situation, we need four additional bytes to encode
1540 * the entire ServerECDHParams structure. */
1541 n += 4 + encodedlen;
Adam Langley95c29f32014-06-20 12:00:00 -07001542
Adam Langleyfcf25832014-12-18 17:42:32 -08001543 /* We'll generate the serverKeyExchange message explicitly so we can set
1544 * these to NULLs */
1545 r[0] = NULL;
1546 r[1] = NULL;
1547 r[2] = NULL;
1548 r[3] = NULL;
1549 } else if (!(alg_k & SSL_kPSK)) {
1550 al = SSL_AD_HANDSHAKE_FAILURE;
1551 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1552 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1553 goto f_err;
1554 }
Adam Langley95c29f32014-06-20 12:00:00 -07001555
Adam Langleyfcf25832014-12-18 17:42:32 -08001556 for (i = 0; i < 4 && r[i] != NULL; i++) {
1557 nr[i] = BN_num_bytes(r[i]);
1558 n += 2 + nr[i];
1559 }
Adam Langley95c29f32014-06-20 12:00:00 -07001560
Adam Langleyfcf25832014-12-18 17:42:32 -08001561 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1562 pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher);
1563 if (pkey == NULL) {
1564 al = SSL_AD_DECODE_ERROR;
1565 goto f_err;
1566 }
1567 kn = EVP_PKEY_size(pkey);
1568 } else {
1569 pkey = NULL;
1570 kn = 0;
1571 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001572
Adam Langleyfcf25832014-12-18 17:42:32 -08001573 if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) {
1574 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_BUF);
1575 goto err;
1576 }
1577 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001578
Adam Langleyfcf25832014-12-18 17:42:32 -08001579 for (i = 0; i < 4 && r[i] != NULL; i++) {
1580 s2n(nr[i], p);
1581 BN_bn2bin(r[i], p);
1582 p += nr[i];
1583 }
David Benjamin00505ec2014-11-26 16:38:00 -05001584
Adam Langleyfcf25832014-12-18 17:42:32 -08001585 /* Note: ECDHE PSK ciphersuites use SSL_kEECDH and SSL_aPSK. When one of
1586 * them is used, the server key exchange record needs to have both the
1587 * psk_identity_hint and the ServerECDHParams. */
1588 if (alg_a & SSL_aPSK) {
1589 /* copy PSK identity hint (if provided) */
1590 s2n(psk_identity_hint_len, p);
1591 if (psk_identity_hint_len > 0) {
1592 memcpy(p, psk_identity_hint, psk_identity_hint_len);
1593 p += psk_identity_hint_len;
1594 }
1595 }
David Benjamin00505ec2014-11-26 16:38:00 -05001596
Adam Langleyfcf25832014-12-18 17:42:32 -08001597 if (alg_k & SSL_kEECDH) {
1598 /* We only support named (not generic) curves. In this situation, the
1599 * serverKeyExchange message has:
1600 * [1 byte CurveType], [2 byte CurveName]
1601 * [1 byte length of encoded point], followed by
1602 * the actual encoded point itself. */
David Benjamin70bd80a2014-12-27 03:06:46 -05001603 *(p++) = NAMED_CURVE_TYPE;
1604 *(p++) = (uint8_t)(curve_id >> 8);
1605 *(p++) = (uint8_t)(curve_id & 0xff);
1606 *(p++) = encodedlen;
1607 memcpy(p, encodedPoint, encodedlen);
1608 p += encodedlen;
Adam Langleyfcf25832014-12-18 17:42:32 -08001609 OPENSSL_free(encodedPoint);
1610 encodedPoint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001611 }
Adam Langley95c29f32014-06-20 12:00:00 -07001612
Adam Langleyfcf25832014-12-18 17:42:32 -08001613 /* not anonymous */
1614 if (pkey != NULL) {
1615 /* n is the length of the params, they start at &(d[4]) and p points to
1616 * the space at the end. */
1617 const EVP_MD *md;
1618 size_t sig_len = EVP_PKEY_size(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001619
Adam Langleyfcf25832014-12-18 17:42:32 -08001620 /* Determine signature algorithm. */
1621 if (SSL_USE_SIGALGS(s)) {
1622 md = tls1_choose_signing_digest(s, pkey);
1623 if (!tls12_get_sigandhash(p, pkey, md)) {
1624 /* Should never happen */
1625 al = SSL_AD_INTERNAL_ERROR;
1626 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1627 ERR_R_INTERNAL_ERROR);
1628 goto f_err;
1629 }
1630 p += 2;
1631 } else if (pkey->type == EVP_PKEY_RSA) {
1632 md = EVP_md5_sha1();
1633 } else {
1634 md = EVP_sha1();
1635 }
1636
1637 if (!EVP_DigestSignInit(&md_ctx, NULL, md, NULL, pkey) ||
1638 !EVP_DigestSignUpdate(&md_ctx, s->s3->client_random,
1639 SSL3_RANDOM_SIZE) ||
1640 !EVP_DigestSignUpdate(&md_ctx, s->s3->server_random,
1641 SSL3_RANDOM_SIZE) ||
1642 !EVP_DigestSignUpdate(&md_ctx, d, n) ||
1643 !EVP_DigestSignFinal(&md_ctx, &p[2], &sig_len)) {
1644 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_EVP);
1645 goto err;
1646 }
1647
1648 s2n(sig_len, p);
1649 n += sig_len + 2;
1650 if (SSL_USE_SIGALGS(s)) {
1651 n += 2;
1652 }
1653 }
1654
1655 ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n);
1656 }
1657
1658 s->state = SSL3_ST_SW_KEY_EXCH_B;
1659 EVP_MD_CTX_cleanup(&md_ctx);
1660 return ssl_do_write(s);
1661
Adam Langley95c29f32014-06-20 12:00:00 -07001662f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001663 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001664err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001665 if (encodedPoint != NULL) {
1666 OPENSSL_free(encodedPoint);
1667 }
1668 BN_CTX_free(bn_ctx);
1669 EVP_MD_CTX_cleanup(&md_ctx);
1670 return -1;
1671}
Adam Langley95c29f32014-06-20 12:00:00 -07001672
Adam Langleyfcf25832014-12-18 17:42:32 -08001673int ssl3_send_certificate_request(SSL *s) {
1674 uint8_t *p, *d;
1675 size_t i;
1676 int j, nl, off, n;
1677 STACK_OF(X509_NAME) *sk = NULL;
1678 X509_NAME *name;
1679 BUF_MEM *buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001680
Adam Langleyfcf25832014-12-18 17:42:32 -08001681 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
1682 buf = s->init_buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001683
Adam Langleyfcf25832014-12-18 17:42:32 -08001684 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001685
Adam Langleyfcf25832014-12-18 17:42:32 -08001686 /* get the list of acceptable cert types */
1687 p++;
1688 n = ssl3_get_req_cert_type(s, p);
1689 d[0] = n;
1690 p += n;
1691 n++;
Adam Langley95c29f32014-06-20 12:00:00 -07001692
Adam Langleyfcf25832014-12-18 17:42:32 -08001693 if (SSL_USE_SIGALGS(s)) {
1694 const uint8_t *psigs;
1695 nl = tls12_get_psigalgs(s, &psigs);
1696 s2n(nl, p);
1697 memcpy(p, psigs, nl);
1698 p += nl;
1699 n += nl + 2;
1700 }
Adam Langley95c29f32014-06-20 12:00:00 -07001701
Adam Langleyfcf25832014-12-18 17:42:32 -08001702 off = n;
1703 p += 2;
1704 n += 2;
Adam Langley95c29f32014-06-20 12:00:00 -07001705
Adam Langleyfcf25832014-12-18 17:42:32 -08001706 sk = SSL_get_client_CA_list(s);
1707 nl = 0;
1708 if (sk != NULL) {
1709 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1710 name = sk_X509_NAME_value(sk, i);
1711 j = i2d_X509_NAME(name, NULL);
1712 if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
1713 OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB);
1714 goto err;
1715 }
1716 p = ssl_handshake_start(s) + n;
1717 s2n(j, p);
1718 i2d_X509_NAME(name, &p);
1719 n += 2 + j;
1720 nl += 2 + j;
1721 }
1722 }
Adam Langley95c29f32014-06-20 12:00:00 -07001723
Adam Langleyfcf25832014-12-18 17:42:32 -08001724 /* else no CA names */
1725 p = ssl_handshake_start(s) + off;
1726 s2n(nl, p);
1727
1728 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n);
Adam Langleyfcf25832014-12-18 17:42:32 -08001729 s->state = SSL3_ST_SW_CERT_REQ_B;
1730 }
Adam Langley95c29f32014-06-20 12:00:00 -07001731
Adam Langleyfcf25832014-12-18 17:42:32 -08001732 /* SSL3_ST_SW_CERT_REQ_B */
1733 return ssl_do_write(s);
1734
Adam Langley95c29f32014-06-20 12:00:00 -07001735err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001736 return -1;
1737}
Adam Langley95c29f32014-06-20 12:00:00 -07001738
Adam Langleyfcf25832014-12-18 17:42:32 -08001739int ssl3_get_client_key_exchange(SSL *s) {
1740 int al, ok;
1741 long n;
1742 CBS client_key_exchange;
1743 unsigned long alg_k;
1744 unsigned long alg_a;
1745 uint8_t *premaster_secret = NULL;
1746 size_t premaster_secret_len = 0;
1747 RSA *rsa = NULL;
1748 uint8_t *decrypt_buf = NULL;
1749 EVP_PKEY *pkey = NULL;
1750 BIGNUM *pub = NULL;
1751 DH *dh_srvr;
Adam Langley95c29f32014-06-20 12:00:00 -07001752
Adam Langleyfcf25832014-12-18 17:42:32 -08001753 EC_KEY *srvr_ecdh = NULL;
1754 EVP_PKEY *clnt_pub_pkey = NULL;
1755 EC_POINT *clnt_ecpoint = NULL;
1756 BN_CTX *bn_ctx = NULL;
1757 unsigned int psk_len = 0;
1758 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001759
Adam Langleyfcf25832014-12-18 17:42:32 -08001760 n = s->method->ssl_get_message(s, SSL3_ST_SR_KEY_EXCH_A,
1761 SSL3_ST_SR_KEY_EXCH_B,
1762 SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, /* ??? */
1763 SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001764
Adam Langleyfcf25832014-12-18 17:42:32 -08001765 if (!ok) {
1766 return n;
1767 }
Adam Langley95c29f32014-06-20 12:00:00 -07001768
Adam Langleyfcf25832014-12-18 17:42:32 -08001769 CBS_init(&client_key_exchange, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001770
Adam Langleyfcf25832014-12-18 17:42:32 -08001771 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1772 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001773
Adam Langleyfcf25832014-12-18 17:42:32 -08001774 /* If using a PSK key exchange, prepare the pre-shared key. */
1775 if (alg_a & SSL_aPSK) {
1776 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001777
Adam Langleyfcf25832014-12-18 17:42:32 -08001778 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1779 * then this is the only field in the message. */
1780 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1781 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
1782 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR);
1783 al = SSL_AD_DECODE_ERROR;
1784 goto f_err;
1785 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001786
Adam Langleyfcf25832014-12-18 17:42:32 -08001787 if (s->psk_server_callback == NULL) {
1788 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1789 SSL_R_PSK_NO_SERVER_CB);
1790 al = SSL_AD_INTERNAL_ERROR;
1791 goto f_err;
1792 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001793
Adam Langleyfcf25832014-12-18 17:42:32 -08001794 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1795 CBS_contains_zero_byte(&psk_identity)) {
1796 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1797 SSL_R_DATA_LENGTH_TOO_LONG);
1798 al = SSL_AD_ILLEGAL_PARAMETER;
1799 goto f_err;
1800 }
David Benjamin35c02302014-07-13 04:14:59 -04001801
Adam Langleyfcf25832014-12-18 17:42:32 -08001802 if (!CBS_strdup(&psk_identity, &s->session->psk_identity)) {
1803 al = SSL_AD_INTERNAL_ERROR;
1804 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1805 ERR_R_MALLOC_FAILURE);
1806 goto f_err;
1807 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001808
Adam Langleyfcf25832014-12-18 17:42:32 -08001809 /* Look up the key for the identity. */
1810 psk_len =
1811 s->psk_server_callback(s, s->session->psk_identity, psk, sizeof(psk));
1812 if (psk_len > PSK_MAX_PSK_LEN) {
1813 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1814 ERR_R_INTERNAL_ERROR);
1815 al = SSL_AD_INTERNAL_ERROR;
1816 goto f_err;
1817 } else if (psk_len == 0) {
1818 /* PSK related to the given identity not found */
1819 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1820 SSL_R_PSK_IDENTITY_NOT_FOUND);
1821 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1822 goto f_err;
1823 }
1824 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001825
Adam Langleyfcf25832014-12-18 17:42:32 -08001826 /* Depending on the key exchange method, compute |premaster_secret| and
1827 * |premaster_secret_len|. */
1828 if (alg_k & SSL_kRSA) {
1829 CBS encrypted_premaster_secret;
1830 uint8_t rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
1831 uint8_t good;
1832 size_t rsa_size, decrypt_len, premaster_index, j;
Adam Langley95c29f32014-06-20 12:00:00 -07001833
Adam Langleyfcf25832014-12-18 17:42:32 -08001834 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1835 if (pkey == NULL || pkey->type != EVP_PKEY_RSA || pkey->pkey.rsa == NULL) {
1836 al = SSL_AD_HANDSHAKE_FAILURE;
1837 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1838 SSL_R_MISSING_RSA_CERTIFICATE);
1839 goto f_err;
1840 }
1841 rsa = pkey->pkey.rsa;
Adam Langley95c29f32014-06-20 12:00:00 -07001842
Adam Langleyfcf25832014-12-18 17:42:32 -08001843 /* TLS and [incidentally] DTLS{0xFEFF} */
1844 if (s->version > SSL3_VERSION) {
1845 CBS copy = client_key_exchange;
1846 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1847 &encrypted_premaster_secret) ||
1848 CBS_len(&client_key_exchange) != 0) {
1849 if (!(s->options & SSL_OP_TLS_D5_BUG)) {
1850 al = SSL_AD_DECODE_ERROR;
1851 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1852 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1853 goto f_err;
1854 } else {
1855 encrypted_premaster_secret = copy;
1856 }
1857 }
1858 } else {
1859 encrypted_premaster_secret = client_key_exchange;
1860 }
Adam Langley95c29f32014-06-20 12:00:00 -07001861
Adam Langleyfcf25832014-12-18 17:42:32 -08001862 /* Reject overly short RSA keys because we want to be sure that the buffer
1863 * size makes it safe to iterate over the entire size of a premaster secret
1864 * (SSL_MAX_MASTER_KEY_LENGTH). The actual expected size is larger due to
1865 * RSA padding, but the bound is sufficient to be safe. */
1866 rsa_size = RSA_size(rsa);
1867 if (rsa_size < SSL_MAX_MASTER_KEY_LENGTH) {
1868 al = SSL_AD_DECRYPT_ERROR;
1869 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1870 SSL_R_DECRYPTION_FAILED);
1871 goto f_err;
1872 }
Adam Langley95c29f32014-06-20 12:00:00 -07001873
Adam Langleyfcf25832014-12-18 17:42:32 -08001874 /* We must not leak whether a decryption failure occurs because of
1875 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
1876 * section 7.4.7.1). The code follows that advice of the TLS RFC and
1877 * generates a random premaster secret for the case that the decrypt fails.
1878 * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */
1879 if (!RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret))) {
1880 goto err;
1881 }
David Benjamin35c02302014-07-13 04:14:59 -04001882
Adam Langleyfcf25832014-12-18 17:42:32 -08001883 /* Allocate a buffer large enough for an RSA decryption. */
1884 decrypt_buf = OPENSSL_malloc(rsa_size);
1885 if (decrypt_buf == NULL) {
1886 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1887 ERR_R_MALLOC_FAILURE);
1888 goto err;
1889 }
David Benjamin35c02302014-07-13 04:14:59 -04001890
Adam Langleyfcf25832014-12-18 17:42:32 -08001891 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1892 * timing-sensitive code below. */
1893 if (!RSA_decrypt(rsa, &decrypt_len, decrypt_buf, rsa_size,
1894 CBS_data(&encrypted_premaster_secret),
1895 CBS_len(&encrypted_premaster_secret), RSA_NO_PADDING)) {
1896 goto err;
1897 }
1898 if (decrypt_len != rsa_size) {
1899 /* This should never happen, but do a check so we do not read
1900 * uninitialized memory. */
1901 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1902 ERR_R_INTERNAL_ERROR);
1903 goto err;
1904 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001905
Adam Langleyfcf25832014-12-18 17:42:32 -08001906 /* Remove the PKCS#1 padding and adjust |decrypt_len| as appropriate.
1907 * |good| will be 0xff if the premaster is acceptable and zero otherwise.
1908 * */
1909 good =
1910 constant_time_eq_int_8(RSA_message_index_PKCS1_type_2(
1911 decrypt_buf, decrypt_len, &premaster_index),
1912 1);
1913 decrypt_len = decrypt_len - premaster_index;
David Benjamin0aa07672014-07-24 15:34:14 -04001914
Adam Langleyfcf25832014-12-18 17:42:32 -08001915 /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. */
1916 good &= constant_time_eq_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
Adam Langleyacff73f2014-06-20 12:00:00 -07001917
Adam Langleyfcf25832014-12-18 17:42:32 -08001918 /* Copy over the unpadded premaster. Whatever the value of
1919 * |decrypt_good_mask|, copy as if the premaster were the right length. It
1920 * is important the memory access pattern be constant. */
1921 premaster_secret =
1922 BUF_memdup(decrypt_buf + (rsa_size - SSL_MAX_MASTER_KEY_LENGTH),
1923 SSL_MAX_MASTER_KEY_LENGTH);
1924 if (premaster_secret == NULL) {
1925 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1926 ERR_R_MALLOC_FAILURE);
1927 goto err;
1928 }
1929 OPENSSL_free(decrypt_buf);
1930 decrypt_buf = NULL;
Adam Langleyacff73f2014-06-20 12:00:00 -07001931
Adam Langleyfcf25832014-12-18 17:42:32 -08001932 /* If the version in the decrypted pre-master secret is correct then
1933 * version_good will be 0xff, otherwise it'll be zero. The
1934 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
1935 * (http://eprint.iacr.org/2003/052/) exploits the version number check as
1936 * a "bad version oracle". Thus version checks are done in constant time
1937 * and are treated like any other decryption error. */
1938 good &= constant_time_eq_8(premaster_secret[0],
1939 (unsigned)(s->client_version >> 8));
1940 good &= constant_time_eq_8(premaster_secret[1],
1941 (unsigned)(s->client_version & 0xff));
Adam Langleyacff73f2014-06-20 12:00:00 -07001942
Adam Langleyfcf25832014-12-18 17:42:32 -08001943 /* Now copy rand_premaster_secret over premaster_secret using
1944 * decrypt_good_mask. */
1945 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
1946 premaster_secret[j] = constant_time_select_8(good, premaster_secret[j],
1947 rand_premaster_secret[j]);
1948 }
David Benjamin35c02302014-07-13 04:14:59 -04001949
Adam Langleyfcf25832014-12-18 17:42:32 -08001950 premaster_secret_len = sizeof(rand_premaster_secret);
1951 } else if (alg_k & SSL_kEDH) {
1952 CBS dh_Yc;
1953 int dh_len;
David Benjamin35c02302014-07-13 04:14:59 -04001954
Adam Langleyfcf25832014-12-18 17:42:32 -08001955 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &dh_Yc) ||
1956 CBS_len(&dh_Yc) == 0 || CBS_len(&client_key_exchange) != 0) {
1957 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1958 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1959 al = SSL_R_DECODE_ERROR;
1960 goto f_err;
1961 }
Adam Langley95c29f32014-06-20 12:00:00 -07001962
Adam Langleyfcf25832014-12-18 17:42:32 -08001963 if (s->s3->tmp.dh == NULL) {
1964 al = SSL_AD_HANDSHAKE_FAILURE;
1965 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1966 SSL_R_MISSING_TMP_DH_KEY);
1967 goto f_err;
1968 }
1969 dh_srvr = s->s3->tmp.dh;
Adam Langley95c29f32014-06-20 12:00:00 -07001970
Adam Langleyfcf25832014-12-18 17:42:32 -08001971 pub = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL);
1972 if (pub == NULL) {
1973 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_BN_LIB);
1974 goto err;
1975 }
Adam Langley95c29f32014-06-20 12:00:00 -07001976
Adam Langleyfcf25832014-12-18 17:42:32 -08001977 /* Allocate a buffer for the premaster secret. */
1978 premaster_secret = OPENSSL_malloc(DH_size(dh_srvr));
1979 if (premaster_secret == NULL) {
1980 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1981 ERR_R_MALLOC_FAILURE);
1982 goto err;
1983 }
Adam Langley95c29f32014-06-20 12:00:00 -07001984
Adam Langleyfcf25832014-12-18 17:42:32 -08001985 dh_len = DH_compute_key(premaster_secret, pub, dh_srvr);
1986 if (dh_len <= 0) {
1987 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_DH_LIB);
1988 BN_clear_free(pub);
1989 goto err;
1990 }
David Benjamin14c83e72014-07-13 04:54:57 -04001991
Adam Langleyfcf25832014-12-18 17:42:32 -08001992 DH_free(s->s3->tmp.dh);
1993 s->s3->tmp.dh = NULL;
1994 BN_clear_free(pub);
1995 pub = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001996
Adam Langleyfcf25832014-12-18 17:42:32 -08001997 premaster_secret_len = dh_len;
1998 } else if (alg_k & SSL_kEECDH) {
1999 int field_size = 0, ecdh_len;
2000 const EC_KEY *tkey;
2001 const EC_GROUP *group;
2002 const BIGNUM *priv_key;
2003 CBS ecdh_Yc;
Adam Langley95c29f32014-06-20 12:00:00 -07002004
Adam Langleyfcf25832014-12-18 17:42:32 -08002005 /* initialize structures for server's ECDH key pair */
2006 srvr_ecdh = EC_KEY_new();
2007 if (srvr_ecdh == NULL) {
2008 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2009 ERR_R_MALLOC_FAILURE);
2010 goto err;
2011 }
Adam Langley95c29f32014-06-20 12:00:00 -07002012
Adam Langleyfcf25832014-12-18 17:42:32 -08002013 /* Use the ephermeral values we saved when generating the ServerKeyExchange
2014 * msg. */
2015 tkey = s->s3->tmp.ecdh;
Adam Langley95c29f32014-06-20 12:00:00 -07002016
Adam Langleyfcf25832014-12-18 17:42:32 -08002017 group = EC_KEY_get0_group(tkey);
2018 priv_key = EC_KEY_get0_private_key(tkey);
Adam Langley95c29f32014-06-20 12:00:00 -07002019
Adam Langleyfcf25832014-12-18 17:42:32 -08002020 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2021 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
2022 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
2023 goto err;
2024 }
Adam Langley95c29f32014-06-20 12:00:00 -07002025
Adam Langleyfcf25832014-12-18 17:42:32 -08002026 /* Let's get client's public key */
2027 clnt_ecpoint = EC_POINT_new(group);
2028 if (clnt_ecpoint == NULL) {
2029 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2030 ERR_R_MALLOC_FAILURE);
2031 goto err;
2032 }
Adam Langley95c29f32014-06-20 12:00:00 -07002033
Adam Langleyfcf25832014-12-18 17:42:32 -08002034 /* Get client's public key from encoded point in the ClientKeyExchange
2035 * message. */
2036 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ecdh_Yc) ||
2037 CBS_len(&client_key_exchange) != 0) {
2038 al = SSL_AD_DECODE_ERROR;
2039 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR);
2040 goto f_err;
2041 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04002042
Adam Langleyfcf25832014-12-18 17:42:32 -08002043 bn_ctx = BN_CTX_new();
2044 if (bn_ctx == NULL) {
2045 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2046 ERR_R_MALLOC_FAILURE);
2047 goto err;
2048 }
David Benjamin35c02302014-07-13 04:14:59 -04002049
Adam Langleyfcf25832014-12-18 17:42:32 -08002050 if (!EC_POINT_oct2point(group, clnt_ecpoint, CBS_data(&ecdh_Yc),
2051 CBS_len(&ecdh_Yc), bn_ctx)) {
2052 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
2053 goto err;
2054 }
Adam Langley95c29f32014-06-20 12:00:00 -07002055
Adam Langleyfcf25832014-12-18 17:42:32 -08002056 /* Allocate a buffer for both the secret and the PSK. */
2057 field_size = EC_GROUP_get_degree(group);
2058 if (field_size <= 0) {
2059 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
2060 goto err;
2061 }
David Benjamin35c02302014-07-13 04:14:59 -04002062
Adam Langleyfcf25832014-12-18 17:42:32 -08002063 ecdh_len = (field_size + 7) / 8;
2064 premaster_secret = OPENSSL_malloc(ecdh_len);
2065 if (premaster_secret == NULL) {
2066 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2067 ERR_R_MALLOC_FAILURE);
2068 goto err;
2069 }
David Benjamin35c02302014-07-13 04:14:59 -04002070
Adam Langleyfcf25832014-12-18 17:42:32 -08002071 /* Compute the shared pre-master secret */
2072 ecdh_len = ECDH_compute_key(premaster_secret, ecdh_len, clnt_ecpoint,
2073 srvr_ecdh, NULL);
2074 if (ecdh_len <= 0) {
2075 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
2076 goto err;
2077 }
Adam Langley95c29f32014-06-20 12:00:00 -07002078
Adam Langleyfcf25832014-12-18 17:42:32 -08002079 EVP_PKEY_free(clnt_pub_pkey);
2080 clnt_pub_pkey = NULL;
2081 EC_POINT_free(clnt_ecpoint);
2082 clnt_ecpoint = NULL;
2083 EC_KEY_free(srvr_ecdh);
2084 srvr_ecdh = NULL;
2085 BN_CTX_free(bn_ctx);
2086 bn_ctx = NULL;
2087 EC_KEY_free(s->s3->tmp.ecdh);
2088 s->s3->tmp.ecdh = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002089
Adam Langleyfcf25832014-12-18 17:42:32 -08002090 premaster_secret_len = ecdh_len;
2091 } else if (alg_k & SSL_kPSK) {
2092 /* For plain PSK, other_secret is a block of 0s with the same length as the
2093 * pre-shared key. */
2094 premaster_secret_len = psk_len;
2095 premaster_secret = OPENSSL_malloc(premaster_secret_len);
2096 if (premaster_secret == NULL) {
2097 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2098 ERR_R_MALLOC_FAILURE);
2099 goto err;
2100 }
2101 memset(premaster_secret, 0, premaster_secret_len);
2102 } else {
2103 al = SSL_AD_HANDSHAKE_FAILURE;
2104 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2105 SSL_R_UNKNOWN_CIPHER_TYPE);
2106 goto f_err;
2107 }
Adam Langley95c29f32014-06-20 12:00:00 -07002108
Adam Langleyfcf25832014-12-18 17:42:32 -08002109 /* For a PSK cipher suite, the actual pre-master secret is combined with the
2110 * pre-shared key. */
2111 if (alg_a & SSL_aPSK) {
2112 CBB new_premaster, child;
2113 uint8_t *new_data;
2114 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04002115
Adam Langleyfcf25832014-12-18 17:42:32 -08002116 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len)) {
2117 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2118 ERR_R_MALLOC_FAILURE);
2119 goto err;
2120 }
2121 if (!CBB_add_u16_length_prefixed(&new_premaster, &child) ||
2122 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
2123 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
2124 !CBB_add_bytes(&child, psk, psk_len) ||
2125 !CBB_finish(&new_premaster, &new_data, &new_len)) {
2126 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2127 ERR_R_INTERNAL_ERROR);
2128 CBB_cleanup(&new_premaster);
2129 goto err;
2130 }
David Benjamin14c83e72014-07-13 04:54:57 -04002131
Adam Langleyfcf25832014-12-18 17:42:32 -08002132 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2133 OPENSSL_free(premaster_secret);
2134 premaster_secret = new_data;
2135 premaster_secret_len = new_len;
2136 }
David Benjamin14c83e72014-07-13 04:54:57 -04002137
Adam Langleyfcf25832014-12-18 17:42:32 -08002138 /* Compute the master secret */
2139 s->session->master_key_length = s->enc_method->generate_master_secret(
2140 s, s->session->master_key, premaster_secret, premaster_secret_len);
2141 if (s->session->master_key_length == 0) {
2142 goto err;
2143 }
2144 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04002145
Adam Langleyfcf25832014-12-18 17:42:32 -08002146 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2147 OPENSSL_free(premaster_secret);
2148 return 1;
2149
Adam Langley95c29f32014-06-20 12:00:00 -07002150f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002151 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07002152err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002153 if (premaster_secret) {
2154 if (premaster_secret_len) {
2155 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2156 }
2157 OPENSSL_free(premaster_secret);
2158 }
2159 if (decrypt_buf) {
2160 OPENSSL_free(decrypt_buf);
2161 }
2162 EVP_PKEY_free(clnt_pub_pkey);
2163 EC_POINT_free(clnt_ecpoint);
2164 if (srvr_ecdh != NULL) {
2165 EC_KEY_free(srvr_ecdh);
2166 }
2167 BN_CTX_free(bn_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002168
Adam Langleyfcf25832014-12-18 17:42:32 -08002169 return -1;
2170}
Adam Langley95c29f32014-06-20 12:00:00 -07002171
Adam Langleyfcf25832014-12-18 17:42:32 -08002172int ssl3_get_cert_verify(SSL *s) {
2173 int al, ok, ret = 0;
2174 long n;
2175 CBS certificate_verify, signature;
2176 X509 *peer = s->session->peer;
2177 EVP_PKEY *pkey = NULL;
2178 const EVP_MD *md = NULL;
2179 uint8_t digest[EVP_MAX_MD_SIZE];
2180 size_t digest_length;
2181 EVP_PKEY_CTX *pctx = NULL;
David Benjamin6553b372014-07-22 14:11:30 -04002182
Adam Langleyfcf25832014-12-18 17:42:32 -08002183 /* Only RSA and ECDSA client certificates are supported, so a
2184 * CertificateVerify is required if and only if there's a client certificate.
2185 * */
2186 if (peer == NULL) {
2187 if (s->s3->handshake_buffer &&
2188 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2189 return -1;
2190 }
2191 return 1;
2192 }
Adam Langley95c29f32014-06-20 12:00:00 -07002193
Adam Langleyfcf25832014-12-18 17:42:32 -08002194 n = s->method->ssl_get_message(
2195 s, SSL3_ST_SR_CERT_VRFY_A, SSL3_ST_SR_CERT_VRFY_B,
2196 SSL3_MT_CERTIFICATE_VERIFY, SSL3_RT_MAX_PLAIN_LENGTH,
2197 SSL_GET_MESSAGE_DONT_HASH_MESSAGE, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002198
Adam Langleyfcf25832014-12-18 17:42:32 -08002199 if (!ok) {
2200 return n;
2201 }
David Benjaminef865502014-08-24 02:48:34 -04002202
Adam Langleyfcf25832014-12-18 17:42:32 -08002203 /* Filter out unsupported certificate types. */
2204 pkey = X509_get_pubkey(peer);
2205 if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) ||
2206 (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) {
2207 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
2208 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify,
2209 SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
2210 goto f_err;
2211 }
David Benjamin6897dbe2014-07-12 20:18:28 -04002212
Adam Langleyfcf25832014-12-18 17:42:32 -08002213 CBS_init(&certificate_verify, s->init_msg, n);
David Benjamin6897dbe2014-07-12 20:18:28 -04002214
Adam Langleyfcf25832014-12-18 17:42:32 -08002215 /* Determine the digest type if needbe. */
2216 if (SSL_USE_SIGALGS(s) &&
2217 !tls12_check_peer_sigalg(&md, &al, s, &certificate_verify, pkey)) {
2218 goto f_err;
2219 }
David Benjamin854dd652014-08-26 00:32:30 -04002220
Adam Langleyfcf25832014-12-18 17:42:32 -08002221 /* Compute the digest. */
2222 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2223 goto err;
2224 }
David Benjamin854dd652014-08-26 00:32:30 -04002225
Adam Langleyfcf25832014-12-18 17:42:32 -08002226 /* The handshake buffer is no longer necessary, and we may hash the current
2227 * message.*/
2228 if (s->s3->handshake_buffer &&
2229 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2230 goto err;
2231 }
2232 ssl3_hash_current_message(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002233
Adam Langleyfcf25832014-12-18 17:42:32 -08002234 /* Parse and verify the signature. */
2235 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
2236 CBS_len(&certificate_verify) != 0) {
2237 al = SSL_AD_DECODE_ERROR;
2238 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_DECODE_ERROR);
2239 goto f_err;
2240 }
Adam Langley95c29f32014-06-20 12:00:00 -07002241
Adam Langleyfcf25832014-12-18 17:42:32 -08002242 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2243 if (pctx == NULL) {
2244 goto err;
2245 }
2246 if (!EVP_PKEY_verify_init(pctx) ||
2247 !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2248 !EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), digest,
2249 digest_length)) {
2250 al = SSL_AD_DECRYPT_ERROR;
2251 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_SIGNATURE);
2252 goto f_err;
2253 }
2254
2255 ret = 1;
2256
2257 if (0) {
2258 f_err:
2259 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2260 }
2261
David Benjamin854dd652014-08-26 00:32:30 -04002262err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002263 EVP_PKEY_CTX_free(pctx);
2264 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07002265
Adam Langleyfcf25832014-12-18 17:42:32 -08002266 return ret;
2267}
Adam Langley95c29f32014-06-20 12:00:00 -07002268
Adam Langleyfcf25832014-12-18 17:42:32 -08002269int ssl3_get_client_certificate(SSL *s) {
2270 int i, ok, al, ret = -1;
2271 X509 *x = NULL;
2272 unsigned long n;
2273 STACK_OF(X509) *sk = NULL;
2274 SHA256_CTX sha256;
2275 CBS certificate_msg, certificate_list;
2276 int is_first_certificate = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002277
Adam Langleyfcf25832014-12-18 17:42:32 -08002278 n = s->method->ssl_get_message(s, SSL3_ST_SR_CERT_A, SSL3_ST_SR_CERT_B, -1,
2279 s->max_cert_list, SSL_GET_MESSAGE_HASH_MESSAGE,
2280 &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002281
Adam Langleyfcf25832014-12-18 17:42:32 -08002282 if (!ok) {
2283 return n;
2284 }
Adam Langley95c29f32014-06-20 12:00:00 -07002285
Adam Langleyfcf25832014-12-18 17:42:32 -08002286 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
2287 if ((s->verify_mode & SSL_VERIFY_PEER) &&
2288 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2289 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2290 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2291 al = SSL_AD_HANDSHAKE_FAILURE;
2292 goto f_err;
2293 }
David Benjamind5b1f842014-07-11 23:43:48 -04002294
Adam Langleyfcf25832014-12-18 17:42:32 -08002295 /* If tls asked for a client cert, the client must return a 0 list */
2296 if (s->version > SSL3_VERSION && s->s3->tmp.cert_request) {
2297 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2298 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
2299 al = SSL_AD_UNEXPECTED_MESSAGE;
2300 goto f_err;
2301 }
2302 s->s3->tmp.reuse_message = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002303
Adam Langleyfcf25832014-12-18 17:42:32 -08002304 return 1;
2305 }
Adam Langley95c29f32014-06-20 12:00:00 -07002306
Adam Langleyfcf25832014-12-18 17:42:32 -08002307 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
2308 al = SSL_AD_UNEXPECTED_MESSAGE;
2309 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2310 SSL_R_WRONG_MESSAGE_TYPE);
2311 goto f_err;
2312 }
David Benjamind5b1f842014-07-11 23:43:48 -04002313
Adam Langleyfcf25832014-12-18 17:42:32 -08002314 CBS_init(&certificate_msg, s->init_msg, n);
David Benjamind5b1f842014-07-11 23:43:48 -04002315
Adam Langleyfcf25832014-12-18 17:42:32 -08002316 sk = sk_X509_new_null();
2317 if (sk == NULL) {
2318 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2319 goto err;
2320 }
Adam Langley95c29f32014-06-20 12:00:00 -07002321
Adam Langleyfcf25832014-12-18 17:42:32 -08002322 if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) ||
2323 CBS_len(&certificate_msg) != 0) {
2324 al = SSL_AD_DECODE_ERROR;
2325 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR);
2326 goto f_err;
2327 }
Adam Langley95c29f32014-06-20 12:00:00 -07002328
Adam Langleyfcf25832014-12-18 17:42:32 -08002329 while (CBS_len(&certificate_list) > 0) {
2330 CBS certificate;
2331 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07002332
Adam Langleyfcf25832014-12-18 17:42:32 -08002333 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
2334 al = SSL_AD_DECODE_ERROR;
2335 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR);
2336 goto f_err;
2337 }
Adam Langley95c29f32014-06-20 12:00:00 -07002338
Adam Langleyfcf25832014-12-18 17:42:32 -08002339 if (is_first_certificate && s->ctx->retain_only_sha256_of_client_certs) {
2340 /* If this is the first certificate, and we don't want to keep peer
2341 * certificates in memory, then we hash it right away. */
2342 SHA256_Init(&sha256);
2343 SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate));
2344 SHA256_Final(s->session->peer_sha256, &sha256);
2345 s->session->peer_sha256_valid = 1;
2346 }
2347 is_first_certificate = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002348
Adam Langleyfcf25832014-12-18 17:42:32 -08002349 data = CBS_data(&certificate);
2350 x = d2i_X509(NULL, &data, CBS_len(&certificate));
2351 if (x == NULL) {
2352 al = SSL_AD_BAD_CERTIFICATE;
2353 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_ASN1_LIB);
2354 goto f_err;
2355 }
2356 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
2357 al = SSL_AD_DECODE_ERROR;
2358 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2359 SSL_R_CERT_LENGTH_MISMATCH);
2360 goto f_err;
2361 }
2362 if (!sk_X509_push(sk, x)) {
2363 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2364 goto err;
2365 }
2366 x = NULL;
2367 }
2368
2369 if (sk_X509_num(sk) <= 0) {
2370 /* TLS does not mind 0 certs returned */
2371 if (s->version == SSL3_VERSION) {
2372 al = SSL_AD_HANDSHAKE_FAILURE;
2373 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2374 SSL_R_NO_CERTIFICATES_RETURNED);
2375 goto f_err;
2376 }
2377 /* Fail for TLS only if we required a certificate */
2378 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
2379 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2380 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2381 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2382 al = SSL_AD_HANDSHAKE_FAILURE;
2383 goto f_err;
2384 }
2385 /* No client certificate so digest cached records */
2386 if (s->s3->handshake_buffer &&
2387 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2388 al = SSL_AD_INTERNAL_ERROR;
2389 goto f_err;
2390 }
2391 } else {
2392 i = ssl_verify_cert_chain(s, sk);
2393 if (i <= 0) {
2394 al = ssl_verify_alarm_type(s->verify_result);
2395 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2396 SSL_R_CERTIFICATE_VERIFY_FAILED);
2397 goto f_err;
2398 }
2399 }
2400
2401 if (s->session->peer != NULL) {
2402 /* This should not be needed */
2403 X509_free(s->session->peer);
2404 }
2405
2406 s->session->peer = sk_X509_shift(sk);
2407 s->session->verify_result = s->verify_result;
2408
2409 /* With the current implementation, sess_cert will always be NULL when we
2410 * arrive here. */
2411 if (s->session->sess_cert == NULL) {
2412 s->session->sess_cert = ssl_sess_cert_new();
2413 if (s->session->sess_cert == NULL) {
2414 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2415 goto err;
2416 }
2417 }
2418 if (s->session->sess_cert->cert_chain != NULL) {
2419 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
2420 }
2421 s->session->sess_cert->cert_chain = sk;
2422 /* Inconsistency alert: cert_chain does *not* include the peer's own
2423 * certificate, while we do include it in s3_clnt.c */
2424
2425 sk = NULL;
2426
2427 ret = 1;
2428
2429 if (0) {
2430 f_err:
2431 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2432 }
2433
Adam Langley95c29f32014-06-20 12:00:00 -07002434err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002435 if (x != NULL) {
2436 X509_free(x);
2437 }
2438 if (sk != NULL) {
2439 sk_X509_pop_free(sk, X509_free);
2440 }
2441 return ret;
2442}
Adam Langley95c29f32014-06-20 12:00:00 -07002443
Adam Langleyfcf25832014-12-18 17:42:32 -08002444int ssl3_send_server_certificate(SSL *s) {
2445 CERT_PKEY *cpk;
Adam Langley95c29f32014-06-20 12:00:00 -07002446
Adam Langleyfcf25832014-12-18 17:42:32 -08002447 if (s->state == SSL3_ST_SW_CERT_A) {
2448 cpk = ssl_get_server_send_pkey(s);
2449 if (cpk == NULL) {
2450 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_certificate,
2451 ERR_R_INTERNAL_ERROR);
2452 return 0;
2453 }
Adam Langley95c29f32014-06-20 12:00:00 -07002454
Adam Langleyfcf25832014-12-18 17:42:32 -08002455 ssl3_output_cert_chain(s, cpk);
2456 s->state = SSL3_ST_SW_CERT_B;
2457 }
Adam Langley95c29f32014-06-20 12:00:00 -07002458
Adam Langleyfcf25832014-12-18 17:42:32 -08002459 /* SSL3_ST_SW_CERT_B */
2460 return ssl_do_write(s);
2461}
Adam Langley95c29f32014-06-20 12:00:00 -07002462
Adam Langley95c29f32014-06-20 12:00:00 -07002463/* send a new session ticket (not necessarily for a new session) */
Adam Langleyfcf25832014-12-18 17:42:32 -08002464int ssl3_send_new_session_ticket(SSL *s) {
2465 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
2466 uint8_t *session;
2467 size_t session_len;
2468 uint8_t *p, *macstart;
2469 int len;
2470 unsigned int hlen;
2471 EVP_CIPHER_CTX ctx;
2472 HMAC_CTX hctx;
2473 SSL_CTX *tctx = s->initial_ctx;
2474 uint8_t iv[EVP_MAX_IV_LENGTH];
2475 uint8_t key_name[16];
2476 /* The maximum overhead of encrypting the session is 16 (key name) + IV +
2477 * one block of encryption overhead + HMAC. */
2478 const size_t max_ticket_overhead =
2479 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -07002480
Adam Langleyfcf25832014-12-18 17:42:32 -08002481 /* Serialize the SSL_SESSION to be encoded into the ticket. */
2482 if (!SSL_SESSION_to_bytes_for_ticket(s->session, &session, &session_len)) {
2483 return -1;
2484 }
Adam Langley95c29f32014-06-20 12:00:00 -07002485
Adam Langleyfcf25832014-12-18 17:42:32 -08002486 /* If the session is too long, emit a dummy value rather than abort the
2487 * connection. */
2488 if (session_len > 0xFFFF - max_ticket_overhead) {
2489 static const char kTicketPlaceholder[] = "TICKET TOO LARGE";
2490 const size_t placeholder_len = strlen(kTicketPlaceholder);
David Benjamin3cac4502014-10-21 01:46:30 -04002491
Adam Langleyfcf25832014-12-18 17:42:32 -08002492 OPENSSL_free(session);
David Benjamin3cac4502014-10-21 01:46:30 -04002493
Adam Langleyfcf25832014-12-18 17:42:32 -08002494 p = ssl_handshake_start(s);
2495 /* Emit ticket_lifetime_hint. */
2496 l2n(0, p);
2497 /* Emit ticket. */
2498 s2n(placeholder_len, p);
2499 memcpy(p, kTicketPlaceholder, placeholder_len);
2500 p += placeholder_len;
David Benjamin3cac4502014-10-21 01:46:30 -04002501
Adam Langleyfcf25832014-12-18 17:42:32 -08002502 len = p - ssl_handshake_start(s);
2503 ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len);
2504 s->state = SSL3_ST_SW_SESSION_TICKET_B;
2505 return ssl_do_write(s);
2506 }
Adam Langley95c29f32014-06-20 12:00:00 -07002507
Adam Langleyfcf25832014-12-18 17:42:32 -08002508 /* Grow buffer if need be: the length calculation is as follows:
2509 * handshake_header_length + 4 (ticket lifetime hint) + 2 (ticket length) +
2510 * max_ticket_overhead + * session_length */
2511 if (!BUF_MEM_grow(s->init_buf, SSL_HM_HEADER_LENGTH(s) + 6 +
2512 max_ticket_overhead + session_len)) {
2513 OPENSSL_free(session);
2514 return -1;
2515 }
2516 p = ssl_handshake_start(s);
2517 EVP_CIPHER_CTX_init(&ctx);
2518 HMAC_CTX_init(&hctx);
2519 /* Initialize HMAC and cipher contexts. If callback present it does all the
2520 * work otherwise use generated values from parent ctx. */
2521 if (tctx->tlsext_ticket_key_cb) {
2522 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, &hctx, 1) < 0) {
2523 OPENSSL_free(session);
2524 EVP_CIPHER_CTX_cleanup(&ctx);
2525 HMAC_CTX_cleanup(&hctx);
2526 return -1;
2527 }
2528 } else {
2529 if (!RAND_bytes(iv, 16) ||
2530 !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2531 tctx->tlsext_tick_aes_key, iv) ||
2532 !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2533 NULL)) {
2534 OPENSSL_free(session);
2535 EVP_CIPHER_CTX_cleanup(&ctx);
2536 HMAC_CTX_cleanup(&hctx);
2537 return -1;
2538 }
2539 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
2540 }
Adam Langley95c29f32014-06-20 12:00:00 -07002541
Adam Langleyfcf25832014-12-18 17:42:32 -08002542 /* Ticket lifetime hint (advisory only): We leave this unspecified for
2543 * resumed session (for simplicity), and guess that tickets for new
2544 * sessions will live as long as their sessions. */
2545 l2n(s->hit ? 0 : s->session->timeout, p);
Adam Langley95c29f32014-06-20 12:00:00 -07002546
Adam Langleyfcf25832014-12-18 17:42:32 -08002547 /* Skip ticket length for now */
2548 p += 2;
2549 /* Output key name */
2550 macstart = p;
2551 memcpy(p, key_name, 16);
2552 p += 16;
2553 /* output IV */
2554 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
2555 p += EVP_CIPHER_CTX_iv_length(&ctx);
2556 /* Encrypt session data */
2557 EVP_EncryptUpdate(&ctx, p, &len, session, session_len);
2558 p += len;
2559 EVP_EncryptFinal_ex(&ctx, p, &len);
2560 p += len;
2561 EVP_CIPHER_CTX_cleanup(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002562
Adam Langleyfcf25832014-12-18 17:42:32 -08002563 HMAC_Update(&hctx, macstart, p - macstart);
2564 HMAC_Final(&hctx, p, &hlen);
2565 HMAC_CTX_cleanup(&hctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002566
Adam Langleyfcf25832014-12-18 17:42:32 -08002567 p += hlen;
2568 /* Now write out lengths: p points to end of data written */
2569 /* Total length */
2570 len = p - ssl_handshake_start(s);
2571 /* Skip ticket lifetime hint */
2572 p = ssl_handshake_start(s) + 4;
2573 s2n(len - 6, p);
2574 ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len);
2575 s->state = SSL3_ST_SW_SESSION_TICKET_B;
2576 OPENSSL_free(session);
2577 }
Adam Langley95c29f32014-06-20 12:00:00 -07002578
Adam Langleyfcf25832014-12-18 17:42:32 -08002579 /* SSL3_ST_SW_SESSION_TICKET_B */
2580 return ssl_do_write(s);
2581}
Adam Langley95c29f32014-06-20 12:00:00 -07002582
Adam Langley95c29f32014-06-20 12:00:00 -07002583/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
2584 * sets the next_proto member in s if found */
Adam Langleyfcf25832014-12-18 17:42:32 -08002585int ssl3_get_next_proto(SSL *s) {
2586 int ok;
2587 long n;
2588 CBS next_protocol, selected_protocol, padding;
Adam Langley95c29f32014-06-20 12:00:00 -07002589
Adam Langleyfcf25832014-12-18 17:42:32 -08002590 /* Clients cannot send a NextProtocol message if we didn't see the extension
2591 * in their ClientHello */
2592 if (!s->s3->next_proto_neg_seen) {
2593 OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto,
2594 SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
2595 return -1;
2596 }
Adam Langley95c29f32014-06-20 12:00:00 -07002597
Adam Langleyfcf25832014-12-18 17:42:32 -08002598 n = s->method->ssl_get_message(s, SSL3_ST_SR_NEXT_PROTO_A,
2599 SSL3_ST_SR_NEXT_PROTO_B, SSL3_MT_NEXT_PROTO,
2600 514, /* See the payload format below */
2601 SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002602
Adam Langleyfcf25832014-12-18 17:42:32 -08002603 if (!ok) {
2604 return n;
2605 }
Adam Langley95c29f32014-06-20 12:00:00 -07002606
Adam Langleyfcf25832014-12-18 17:42:32 -08002607 /* s->state doesn't reflect whether ChangeCipherSpec has been received in
2608 * this handshake, but s->s3->change_cipher_spec does (will be reset by
2609 * ssl3_get_finished).
2610 *
2611 * TODO(davidben): Is this check now redundant with
2612 * SSL3_FLAGS_EXPECT_CCS? */
2613 if (!s->s3->change_cipher_spec) {
2614 OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto,
2615 SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
2616 return -1;
2617 }
Adam Langley95c29f32014-06-20 12:00:00 -07002618
Adam Langleyfcf25832014-12-18 17:42:32 -08002619 CBS_init(&next_protocol, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07002620
Adam Langleyfcf25832014-12-18 17:42:32 -08002621 /* The payload looks like:
2622 * uint8 proto_len;
2623 * uint8 proto[proto_len];
2624 * uint8 padding_len;
2625 * uint8 padding[padding_len]; */
2626 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
2627 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
2628 CBS_len(&next_protocol) != 0 ||
2629 !CBS_stow(&selected_protocol, &s->next_proto_negotiated,
2630 &s->next_proto_negotiated_len)) {
2631 return 0;
2632 }
Adam Langley95c29f32014-06-20 12:00:00 -07002633
Adam Langleyfcf25832014-12-18 17:42:32 -08002634 return 1;
2635}
Adam Langley95c29f32014-06-20 12:00:00 -07002636
Adam Langley1258b6a2014-06-20 12:00:00 -07002637/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002638int ssl3_get_channel_id(SSL *s) {
2639 int ret = -1, ok;
2640 long n;
2641 EVP_MD_CTX md_ctx;
2642 uint8_t channel_id_hash[SHA256_DIGEST_LENGTH];
2643 unsigned int channel_id_hash_len;
2644 const uint8_t *p;
2645 uint16_t extension_type, expected_extension_type;
2646 EC_GROUP *p256 = NULL;
2647 EC_KEY *key = NULL;
2648 EC_POINT *point = NULL;
2649 ECDSA_SIG sig;
2650 BIGNUM x, y;
2651 CBS encrypted_extensions, extension;
Adam Langley1258b6a2014-06-20 12:00:00 -07002652
Adam Langleyfcf25832014-12-18 17:42:32 -08002653 n = s->method->ssl_get_message(
2654 s, SSL3_ST_SR_CHANNEL_ID_A, SSL3_ST_SR_CHANNEL_ID_B,
2655 SSL3_MT_ENCRYPTED_EXTENSIONS, 2 + 2 + TLSEXT_CHANNEL_ID_SIZE,
2656 SSL_GET_MESSAGE_DONT_HASH_MESSAGE, &ok);
Adam Langley1258b6a2014-06-20 12:00:00 -07002657
Adam Langleyfcf25832014-12-18 17:42:32 -08002658 if (!ok) {
2659 return n;
2660 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002661
Adam Langleyfcf25832014-12-18 17:42:32 -08002662 /* Before incorporating the EncryptedExtensions message to the handshake
2663 * hash, compute the hash that should have been signed. */
2664 channel_id_hash_len = sizeof(channel_id_hash);
2665 EVP_MD_CTX_init(&md_ctx);
2666 if (!EVP_DigestInit_ex(&md_ctx, EVP_sha256(), NULL) ||
2667 !tls1_channel_id_hash(&md_ctx, s) ||
2668 !EVP_DigestFinal(&md_ctx, channel_id_hash, &channel_id_hash_len)) {
2669 EVP_MD_CTX_cleanup(&md_ctx);
2670 return -1;
2671 }
2672 EVP_MD_CTX_cleanup(&md_ctx);
2673 assert(channel_id_hash_len == SHA256_DIGEST_LENGTH);
David Benjamin880b14e2014-08-25 22:35:07 -04002674
Adam Langleyfcf25832014-12-18 17:42:32 -08002675 ssl3_hash_current_message(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07002676
Adam Langleyfcf25832014-12-18 17:42:32 -08002677 /* s->state doesn't reflect whether ChangeCipherSpec has been received in
2678 * this handshake, but s->s3->change_cipher_spec does (will be reset by
2679 * ssl3_get_finished).
2680 *
2681 * TODO(davidben): Is this check now redundant with SSL3_FLAGS_EXPECT_CCS? */
2682 if (!s->s3->change_cipher_spec) {
2683 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id,
2684 SSL_R_GOT_CHANNEL_ID_BEFORE_A_CCS);
2685 return -1;
2686 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002687
Adam Langleyfcf25832014-12-18 17:42:32 -08002688 CBS_init(&encrypted_extensions, s->init_msg, n);
Adam Langley1258b6a2014-06-20 12:00:00 -07002689
Adam Langleyfcf25832014-12-18 17:42:32 -08002690 /* EncryptedExtensions could include multiple extensions, but the only
2691 * extension that could be negotiated is ChannelID, so there can only be one
2692 * entry.
2693 *
2694 * The payload looks like:
2695 * uint16 extension_type
2696 * uint16 extension_len;
2697 * uint8 x[32];
2698 * uint8 y[32];
2699 * uint8 r[32];
2700 * uint8 s[32]; */
2701 expected_extension_type = TLSEXT_TYPE_channel_id;
2702 if (s->s3->tlsext_channel_id_new) {
2703 expected_extension_type = TLSEXT_TYPE_channel_id_new;
2704 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002705
Adam Langleyfcf25832014-12-18 17:42:32 -08002706 if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
2707 !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
2708 CBS_len(&encrypted_extensions) != 0 ||
2709 extension_type != expected_extension_type ||
2710 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
2711 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_INVALID_MESSAGE);
2712 return -1;
2713 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002714
Adam Langleyfcf25832014-12-18 17:42:32 -08002715 p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
2716 if (!p256) {
2717 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_NO_P256_SUPPORT);
2718 return -1;
2719 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002720
Adam Langleyfcf25832014-12-18 17:42:32 -08002721 BN_init(&x);
2722 BN_init(&y);
2723 sig.r = BN_new();
2724 sig.s = BN_new();
Adam Langley1258b6a2014-06-20 12:00:00 -07002725
Adam Langleyfcf25832014-12-18 17:42:32 -08002726 p = CBS_data(&extension);
2727 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
2728 BN_bin2bn(p + 32, 32, &y) == NULL ||
2729 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
2730 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
2731 goto err;
2732 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002733
Adam Langleyfcf25832014-12-18 17:42:32 -08002734 point = EC_POINT_new(p256);
2735 if (!point || !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
2736 goto err;
2737 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002738
Adam Langleyfcf25832014-12-18 17:42:32 -08002739 key = EC_KEY_new();
2740 if (!key || !EC_KEY_set_group(key, p256) ||
2741 !EC_KEY_set_public_key(key, point)) {
2742 goto err;
2743 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002744
Adam Langleyfcf25832014-12-18 17:42:32 -08002745 /* We stored the handshake hash in |tlsext_channel_id| the first time that we
2746 * were called. */
2747 if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) {
2748 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id,
2749 SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
2750 s->s3->tlsext_channel_id_valid = 0;
2751 goto err;
2752 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002753
Adam Langleyfcf25832014-12-18 17:42:32 -08002754 memcpy(s->s3->tlsext_channel_id, p, 64);
2755 ret = 1;
Adam Langley1258b6a2014-06-20 12:00:00 -07002756
2757err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002758 BN_free(&x);
2759 BN_free(&y);
2760 BN_free(sig.r);
2761 BN_free(sig.s);
2762 if (key) {
2763 EC_KEY_free(key);
2764 }
2765 if (point) {
2766 EC_POINT_free(point);
2767 }
2768 if (p256) {
2769 EC_GROUP_free(p256);
2770 }
2771 return ret;
2772}