blob: 3f89558bfd37ec794d77b0565eb0f1161654c04b [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 s->state = SSL3_ST_SR_CLNT_HELLO_C;
950 /* fallthrough */
951 case SSL3_ST_SR_CLNT_HELLO_C:
952 case SSL3_ST_SR_CLNT_HELLO_D:
953 /* We have previously parsed the ClientHello message, and can't call
954 * ssl_get_message again without hashing the message into the Finished
955 * digest again. */
956 n = s->init_num;
Adam Langleydc9b1412014-06-20 12:00:00 -0700957
Adam Langleyfcf25832014-12-18 17:42:32 -0800958 memset(&early_ctx, 0, sizeof(early_ctx));
959 early_ctx.ssl = s;
960 early_ctx.client_hello = s->init_msg;
961 early_ctx.client_hello_len = n;
962 if (!ssl_early_callback_init(&early_ctx)) {
963 al = SSL_AD_DECODE_ERROR;
964 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
965 SSL_R_CLIENTHELLO_PARSE_FAILED);
966 goto f_err;
967 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400968
Adam Langleyfcf25832014-12-18 17:42:32 -0800969 if (s->state == SSL3_ST_SR_CLNT_HELLO_C &&
970 s->ctx->select_certificate_cb != NULL) {
971 s->state = SSL3_ST_SR_CLNT_HELLO_D;
972 switch (s->ctx->select_certificate_cb(&early_ctx)) {
973 case 0:
David Benjamin2fff5bf2015-02-09 04:32:28 -0500974 s->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
975 goto err;
Adam Langleydc9b1412014-06-20 12:00:00 -0700976
Adam Langleyfcf25832014-12-18 17:42:32 -0800977 case -1:
978 /* Connection rejected. */
979 al = SSL_AD_ACCESS_DENIED;
980 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
981 SSL_R_CONNECTION_REJECTED);
982 goto f_err;
Adam Langleydc9b1412014-06-20 12:00:00 -0700983
Adam Langleyfcf25832014-12-18 17:42:32 -0800984 default:
985 /* fallthrough */;
986 }
987 }
988 s->state = SSL3_ST_SR_CLNT_HELLO_D;
989 break;
Adam Langleyb2ce0582014-06-20 12:00:00 -0700990
Adam Langleyfcf25832014-12-18 17:42:32 -0800991 default:
992 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNKNOWN_STATE);
993 return -1;
994 }
Adam Langley95c29f32014-06-20 12:00:00 -0700995
Adam Langleyfcf25832014-12-18 17:42:32 -0800996 CBS_init(&client_hello, s->init_msg, n);
997 if (!CBS_get_u16(&client_hello, &client_version) ||
998 !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) ||
999 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
1000 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1001 al = SSL_AD_DECODE_ERROR;
1002 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
1003 goto f_err;
1004 }
Adam Langley95c29f32014-06-20 12:00:00 -07001005
Adam Langleyfcf25832014-12-18 17:42:32 -08001006 /* use version from inside client hello, not from record header (may differ:
1007 * see RFC 2246, Appendix E, second paragraph) */
1008 s->client_version = client_version;
Adam Langley95c29f32014-06-20 12:00:00 -07001009
Adam Langleyfcf25832014-12-18 17:42:32 -08001010 /* Load the client random. */
1011 memcpy(s->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -07001012
Adam Langleyfcf25832014-12-18 17:42:32 -08001013 if (SSL_IS_DTLS(s)) {
1014 CBS cookie;
Adam Langley95c29f32014-06-20 12:00:00 -07001015
Adam Langleyfcf25832014-12-18 17:42:32 -08001016 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
1017 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
1018 al = SSL_AD_DECODE_ERROR;
1019 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
1020 goto f_err;
1021 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001022 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001023
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 if (!s->s3->have_version) {
1025 /* Select version to use */
1026 uint16_t version = ssl3_get_mutual_version(s, client_version);
1027 if (version == 0) {
1028 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL);
1029 s->version = s->client_version;
1030 al = SSL_AD_PROTOCOL_VERSION;
1031 goto f_err;
1032 }
1033 s->version = version;
1034 s->enc_method = ssl3_get_enc_method(version);
1035 assert(s->enc_method != NULL);
1036 /* At this point, the connection's version is known and |s->version| is
1037 * fixed. Begin enforcing the record-layer version. */
1038 s->s3->have_version = 1;
1039 } else if (SSL_IS_DTLS(s) ? (s->client_version > s->version)
1040 : (s->client_version < s->version)) {
1041 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER);
1042 al = SSL_AD_PROTOCOL_VERSION;
1043 goto f_err;
1044 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001045
Adam Langleyfcf25832014-12-18 17:42:32 -08001046 s->hit = 0;
1047 /* Versions before 0.9.7 always allow clients to resume sessions in
1048 * renegotiation. 0.9.7 and later allow this by default, but optionally
1049 * ignore resumption requests with flag
1050 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather than
1051 * a change to default behavior so that applications relying on this for
1052 * security won't even compile against older library versions).
1053 *
1054 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1055 * request renegotiation but not a new session (s->new_session remains
1056 * unset): for servers, this essentially just means that the
1057 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be ignored. */
1058 if (s->new_session &&
1059 (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)) {
1060 if (!ssl_get_new_session(s, 1)) {
1061 goto err;
1062 }
1063 } else {
1064 i = ssl_get_prev_session(s, &early_ctx);
1065 if (i == PENDING_SESSION) {
David Benjamin2fff5bf2015-02-09 04:32:28 -05001066 s->rwstate = SSL_PENDING_SESSION;
Adam Langleyfcf25832014-12-18 17:42:32 -08001067 goto err;
1068 } else if (i == -1) {
1069 goto err;
1070 }
Adam Langley95c29f32014-06-20 12:00:00 -07001071
Adam Langleyfcf25832014-12-18 17:42:32 -08001072 /* Only resume if the session's version matches the negotiated version:
1073 * most clients do not accept a mismatch. */
1074 if (i == 1 && s->version == s->session->ssl_version) {
1075 s->hit = 1;
1076 } else {
1077 /* No session was found or it was unacceptable. */
1078 if (!ssl_get_new_session(s, 1)) {
1079 goto err;
1080 }
1081 }
1082 }
David Benjamin22f9bcc2014-07-13 12:29:21 -04001083
Adam Langleyfcf25832014-12-18 17:42:32 -08001084 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
1085 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
1086 CBS_len(&compression_methods) == 0) {
1087 al = SSL_AD_DECODE_ERROR;
1088 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
1089 goto f_err;
1090 }
David Benjamin22f9bcc2014-07-13 12:29:21 -04001091
Adam Langleyfcf25832014-12-18 17:42:32 -08001092 /* TODO(davidben): Per spec, cipher_suites can never be empty (specified at
1093 * the ClientHello structure level). This logic allows it to be empty if
1094 * resuming a session. Can we always require non-empty? If a client sends
1095 * empty cipher_suites because it's resuming a session, it could always fail
1096 * to resume a session, so it's unlikely to actually work. */
1097 if (CBS_len(&cipher_suites) == 0 && CBS_len(&session_id) != 0) {
1098 /* We need a cipher if we are not resuming a session. */
1099 al = SSL_AD_ILLEGAL_PARAMETER;
1100 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_SPECIFIED);
1101 goto f_err;
1102 }
Adam Langley95c29f32014-06-20 12:00:00 -07001103
Adam Langleyfcf25832014-12-18 17:42:32 -08001104 ciphers = ssl_bytes_to_cipher_list(s, &cipher_suites);
1105 if (ciphers == NULL) {
1106 goto err;
1107 }
Adam Langley95c29f32014-06-20 12:00:00 -07001108
Adam Langleyfcf25832014-12-18 17:42:32 -08001109 /* If it is a hit, check that the cipher is in the list. */
1110 if (s->hit && CBS_len(&cipher_suites) > 0) {
1111 size_t j;
1112 int found_cipher = 0;
1113 unsigned long id = s->session->cipher->id;
Adam Langley95c29f32014-06-20 12:00:00 -07001114
Adam Langleyfcf25832014-12-18 17:42:32 -08001115 for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++) {
1116 c = sk_SSL_CIPHER_value(ciphers, j);
1117 if (c->id == id) {
1118 found_cipher = 1;
1119 break;
1120 }
1121 }
Adam Langley95c29f32014-06-20 12:00:00 -07001122
Adam Langleyfcf25832014-12-18 17:42:32 -08001123 if (!found_cipher) {
1124 /* we need to have the cipher in the cipher list if we are asked to reuse
1125 * it */
1126 al = SSL_AD_ILLEGAL_PARAMETER;
1127 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
1128 SSL_R_REQUIRED_CIPHER_MISSING);
1129 goto f_err;
1130 }
1131 }
Adam Langley95c29f32014-06-20 12:00:00 -07001132
Adam Langleyfcf25832014-12-18 17:42:32 -08001133 /* Only null compression is supported. */
1134 if (memchr(CBS_data(&compression_methods), 0,
1135 CBS_len(&compression_methods)) == NULL) {
1136 al = SSL_AD_ILLEGAL_PARAMETER;
1137 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
1138 SSL_R_NO_COMPRESSION_SPECIFIED);
1139 goto f_err;
1140 }
David Benjamindc72ff72014-06-25 12:36:10 -04001141
Adam Langleyfcf25832014-12-18 17:42:32 -08001142 /* TLS extensions. */
1143 if (s->version >= SSL3_VERSION &&
1144 !ssl_parse_clienthello_tlsext(s, &client_hello)) {
1145 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_PARSE_TLSEXT);
1146 goto err;
1147 }
Adam Langley95c29f32014-06-20 12:00:00 -07001148
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 /* There should be nothing left over in the record. */
1150 if (CBS_len(&client_hello) != 0) {
1151 /* wrong packet length */
1152 al = SSL_AD_DECODE_ERROR;
1153 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_BAD_PACKET_LENGTH);
1154 goto f_err;
1155 }
Adam Langley95c29f32014-06-20 12:00:00 -07001156
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 /* Given ciphers and SSL_get_ciphers, we must pick a cipher */
1158 if (!s->hit) {
1159 if (ciphers == NULL) {
1160 al = SSL_AD_ILLEGAL_PARAMETER;
1161 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_PASSED);
1162 goto f_err;
1163 }
Adam Langley95c29f32014-06-20 12:00:00 -07001164
Adam Langleyfcf25832014-12-18 17:42:32 -08001165 /* Let cert callback update server certificates if required */
1166 if (s->cert->cert_cb) {
1167 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1168 if (rv == 0) {
1169 al = SSL_AD_INTERNAL_ERROR;
1170 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CERT_CB_ERROR);
1171 goto f_err;
1172 }
1173 if (rv < 0) {
1174 s->rwstate = SSL_X509_LOOKUP;
1175 goto err;
1176 }
1177 s->rwstate = SSL_NOTHING;
1178 }
1179 c = ssl3_choose_cipher(s, ciphers, ssl_get_cipher_preferences(s));
1180
1181 if (c == NULL) {
1182 al = SSL_AD_HANDSHAKE_FAILURE;
1183 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER);
1184 goto f_err;
1185 }
1186 s->s3->tmp.new_cipher = c;
1187 } else {
1188 /* Session-id reuse */
1189 s->s3->tmp.new_cipher = s->session->cipher;
1190 }
1191
1192 if ((!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) &&
1193 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1194 goto f_err;
1195 }
1196
1197 /* we now have the following setup;
1198 * client_random
1199 * cipher_list - our prefered list of ciphers
1200 * ciphers - the clients prefered list of ciphers
1201 * compression - basically ignored right now
1202 * ssl version is set - sslv3
1203 * s->session - The ssl session has been setup.
1204 * s->hit - session reuse flag
1205 * s->tmp.new_cipher - the new cipher to use. */
1206
1207 if (ret < 0) {
1208 ret = -ret;
1209 }
1210
1211 if (0) {
1212 f_err:
1213 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1214 }
1215
Adam Langley95c29f32014-06-20 12:00:00 -07001216err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001217 if (ciphers != NULL) {
1218 sk_SSL_CIPHER_free(ciphers);
1219 }
1220 return ret;
1221}
Adam Langley95c29f32014-06-20 12:00:00 -07001222
Adam Langleyfcf25832014-12-18 17:42:32 -08001223int ssl3_send_server_hello(SSL *s) {
1224 uint8_t *buf;
1225 uint8_t *p, *d;
1226 int sl;
1227 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001228
Adam Langleyfcf25832014-12-18 17:42:32 -08001229 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1230 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1231 * known attack while we fix ChannelID itself. */
1232 if (s->s3->tlsext_channel_id_valid &&
1233 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kEECDH) == 0) {
1234 s->s3->tlsext_channel_id_valid = 0;
1235 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001236
Adam Langleyfcf25832014-12-18 17:42:32 -08001237 /* If this is a resumption and the original handshake didn't support
1238 * ChannelID then we didn't record the original handshake hashes in the
1239 * session and so cannot resume with ChannelIDs. */
1240 if (s->hit && s->s3->tlsext_channel_id_new &&
1241 s->session->original_handshake_hash_len == 0) {
1242 s->s3->tlsext_channel_id_valid = 0;
1243 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001244
Adam Langleyfcf25832014-12-18 17:42:32 -08001245 buf = (uint8_t *)s->init_buf->data;
1246 /* Do the message type and length last */
1247 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001248
Adam Langleyfcf25832014-12-18 17:42:32 -08001249 *(p++) = s->version >> 8;
1250 *(p++) = s->version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -07001251
Adam Langleyfcf25832014-12-18 17:42:32 -08001252 /* Random stuff */
1253 if (!ssl_fill_hello_random(s, 1, s->s3->server_random, SSL3_RANDOM_SIZE)) {
1254 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1255 return -1;
1256 }
1257 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1258 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -07001259
Adam Langleyfcf25832014-12-18 17:42:32 -08001260 /* There are several cases for the session ID to send
1261 * back in the server hello:
1262 * - For session reuse from the session cache, we send back the old session
1263 * ID.
1264 * - If stateless session reuse (using a session ticket) is successful, we
1265 * send back the client's "session ID" (which doesn't actually identify
1266 * the session).
1267 * - If it is a new session, we send back the new session ID.
1268 * - However, if we want the new session to be single-use, we send back a
1269 * 0-length session ID.
1270 * s->hit is non-zero in either case of session reuse, so the following
1271 * won't overwrite an ID that we're supposed to send back. */
1272 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) && !s->hit) {
1273 s->session->session_id_length = 0;
1274 }
Adam Langley95c29f32014-06-20 12:00:00 -07001275
Adam Langleyfcf25832014-12-18 17:42:32 -08001276 sl = s->session->session_id_length;
1277 if (sl > (int)sizeof(s->session->session_id)) {
1278 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1279 return -1;
1280 }
1281 *(p++) = sl;
1282 memcpy(p, s->session->session_id, sl);
1283 p += sl;
Adam Langley95c29f32014-06-20 12:00:00 -07001284
Adam Langleyfcf25832014-12-18 17:42:32 -08001285 /* put the cipher */
1286 s2n(ssl3_get_cipher_value(s->s3->tmp.new_cipher), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001287
Adam Langleyfcf25832014-12-18 17:42:32 -08001288 /* put the compression method */
1289 *(p++) = 0;
1290 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1291 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, SSL_R_SERVERHELLO_TLSEXT);
1292 return -1;
1293 }
1294 p = ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH);
1295 if (p == NULL) {
1296 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1297 return -1;
1298 }
Adam Langley95c29f32014-06-20 12:00:00 -07001299
Adam Langleyfcf25832014-12-18 17:42:32 -08001300 /* do the header */
1301 l = (p - d);
1302 ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l);
1303 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1304 }
Adam Langley95c29f32014-06-20 12:00:00 -07001305
Adam Langleyfcf25832014-12-18 17:42:32 -08001306 /* SSL3_ST_SW_SRVR_HELLO_B */
1307 return ssl_do_write(s);
1308}
Adam Langley95c29f32014-06-20 12:00:00 -07001309
Adam Langleyfcf25832014-12-18 17:42:32 -08001310int ssl3_send_server_done(SSL *s) {
1311 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1312 ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0);
1313 s->state = SSL3_ST_SW_SRVR_DONE_B;
1314 }
Adam Langley95c29f32014-06-20 12:00:00 -07001315
Adam Langleyfcf25832014-12-18 17:42:32 -08001316 /* SSL3_ST_SW_SRVR_DONE_B */
1317 return ssl_do_write(s);
1318}
Adam Langley95c29f32014-06-20 12:00:00 -07001319
Adam Langleyfcf25832014-12-18 17:42:32 -08001320int ssl3_send_server_key_exchange(SSL *s) {
1321 DH *dh = NULL, *dhp;
1322 EC_KEY *ecdh = NULL, *ecdhp;
1323 uint8_t *encodedPoint = NULL;
1324 int encodedlen = 0;
David Benjamin70bd80a2014-12-27 03:06:46 -05001325 uint16_t curve_id = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001326 BN_CTX *bn_ctx = NULL;
1327 const char *psk_identity_hint = NULL;
1328 size_t psk_identity_hint_len = 0;
1329 EVP_PKEY *pkey;
1330 uint8_t *p, *d;
1331 int al, i;
1332 unsigned long alg_k;
1333 unsigned long alg_a;
1334 int n;
1335 CERT *cert;
1336 BIGNUM *r[4];
1337 int nr[4], kn;
1338 BUF_MEM *buf;
1339 EVP_MD_CTX md_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07001340
Adam Langleyfcf25832014-12-18 17:42:32 -08001341 EVP_MD_CTX_init(&md_ctx);
1342 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1343 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1344 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1345 cert = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07001346
Adam Langleyfcf25832014-12-18 17:42:32 -08001347 buf = s->init_buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001348
Adam Langleyfcf25832014-12-18 17:42:32 -08001349 r[0] = r[1] = r[2] = r[3] = NULL;
1350 n = 0;
1351 if (alg_a & SSL_aPSK) {
1352 /* size for PSK identity hint */
1353 psk_identity_hint = s->psk_identity_hint;
1354 if (psk_identity_hint) {
1355 psk_identity_hint_len = strlen(psk_identity_hint);
1356 } else {
1357 psk_identity_hint_len = 0;
1358 }
1359 n += 2 + psk_identity_hint_len;
1360 }
Adam Langley95c29f32014-06-20 12:00:00 -07001361
Adam Langleyfcf25832014-12-18 17:42:32 -08001362 if (alg_k & SSL_kEDH) {
1363 dhp = cert->dh_tmp;
1364 if (dhp == NULL && s->cert->dh_tmp_cb != NULL) {
1365 dhp = s->cert->dh_tmp_cb(s, 0, 1024);
1366 }
1367 if (dhp == NULL) {
1368 al = SSL_AD_HANDSHAKE_FAILURE;
1369 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1370 SSL_R_MISSING_TMP_DH_KEY);
1371 goto f_err;
1372 }
Adam Langley95c29f32014-06-20 12:00:00 -07001373
Adam Langleyfcf25832014-12-18 17:42:32 -08001374 if (s->s3->tmp.dh != NULL) {
1375 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1376 ERR_R_INTERNAL_ERROR);
1377 goto err;
1378 }
Adam Langley95c29f32014-06-20 12:00:00 -07001379
Adam Langleyfcf25832014-12-18 17:42:32 -08001380 dh = DHparams_dup(dhp);
1381 if (dh == NULL) {
1382 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1383 goto err;
1384 }
Adam Langley95c29f32014-06-20 12:00:00 -07001385
Adam Langleyfcf25832014-12-18 17:42:32 -08001386 s->s3->tmp.dh = dh;
1387 if (dhp->pub_key == NULL || dhp->priv_key == NULL ||
1388 (s->options & SSL_OP_SINGLE_DH_USE)) {
1389 if (!DH_generate_key(dh)) {
1390 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1391 goto err;
1392 }
1393 } else {
1394 dh->pub_key = BN_dup(dhp->pub_key);
1395 dh->priv_key = BN_dup(dhp->priv_key);
1396 if (dh->pub_key == NULL || dh->priv_key == NULL) {
1397 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1398 goto err;
1399 }
1400 }
Adam Langley95c29f32014-06-20 12:00:00 -07001401
Adam Langleyfcf25832014-12-18 17:42:32 -08001402 r[0] = dh->p;
1403 r[1] = dh->g;
1404 r[2] = dh->pub_key;
1405 } else if (alg_k & SSL_kEECDH) {
1406 const EC_GROUP *group;
Adam Langley95c29f32014-06-20 12:00:00 -07001407
Adam Langleyfcf25832014-12-18 17:42:32 -08001408 ecdhp = cert->ecdh_tmp;
1409 if (s->cert->ecdh_tmp_auto) {
1410 /* Get NID of appropriate shared curve */
1411 int nid = tls1_get_shared_curve(s);
1412 if (nid != NID_undef) {
1413 ecdhp = EC_KEY_new_by_curve_name(nid);
1414 }
1415 } else if (ecdhp == NULL && s->cert->ecdh_tmp_cb) {
1416 ecdhp = s->cert->ecdh_tmp_cb(s, 0, 1024);
1417 }
1418 if (ecdhp == NULL) {
1419 al = SSL_AD_HANDSHAKE_FAILURE;
1420 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1421 SSL_R_MISSING_TMP_ECDH_KEY);
1422 goto f_err;
1423 }
Adam Langley95c29f32014-06-20 12:00:00 -07001424
Adam Langleyfcf25832014-12-18 17:42:32 -08001425 if (s->s3->tmp.ecdh != NULL) {
1426 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1427 ERR_R_INTERNAL_ERROR);
1428 goto err;
1429 }
Adam Langley95c29f32014-06-20 12:00:00 -07001430
Adam Langleyfcf25832014-12-18 17:42:32 -08001431 /* Duplicate the ECDH structure. */
1432 if (ecdhp == NULL) {
1433 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1434 goto err;
1435 }
Adam Langley95c29f32014-06-20 12:00:00 -07001436
Adam Langleyfcf25832014-12-18 17:42:32 -08001437 if (s->cert->ecdh_tmp_auto) {
1438 ecdh = ecdhp;
1439 } else {
1440 ecdh = EC_KEY_dup(ecdhp);
1441 if (ecdh == NULL) {
1442 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1443 goto err;
1444 }
1445 }
Adam Langley95c29f32014-06-20 12:00:00 -07001446
Adam Langleyfcf25832014-12-18 17:42:32 -08001447 s->s3->tmp.ecdh = ecdh;
1448 if (EC_KEY_get0_public_key(ecdh) == NULL ||
1449 EC_KEY_get0_private_key(ecdh) == NULL ||
1450 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1451 if (!EC_KEY_generate_key(ecdh)) {
1452 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1453 goto err;
1454 }
1455 }
Adam Langley95c29f32014-06-20 12:00:00 -07001456
Adam Langleyfcf25832014-12-18 17:42:32 -08001457 group = EC_KEY_get0_group(ecdh);
1458 if (group == NULL ||
1459 EC_KEY_get0_public_key(ecdh) == NULL ||
1460 EC_KEY_get0_private_key(ecdh) == NULL) {
1461 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1462 goto err;
1463 }
Adam Langley95c29f32014-06-20 12:00:00 -07001464
David Benjamin70bd80a2014-12-27 03:06:46 -05001465 /* We only support ephemeral ECDH keys over named (not generic) curves. */
1466 if (!tls1_ec_nid2curve_id(&curve_id, EC_GROUP_get_curve_name(group))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001467 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1468 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1469 goto err;
1470 }
Adam Langley95c29f32014-06-20 12:00:00 -07001471
Adam Langleyfcf25832014-12-18 17:42:32 -08001472 /* Encode the public key. First check the size of encoding and allocate
1473 * memory accordingly. */
1474 encodedlen =
1475 EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
1476 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001477
Adam Langleyfcf25832014-12-18 17:42:32 -08001478 encodedPoint = (uint8_t *)OPENSSL_malloc(encodedlen * sizeof(uint8_t));
1479 bn_ctx = BN_CTX_new();
1480 if (encodedPoint == NULL || bn_ctx == NULL) {
1481 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1482 ERR_R_MALLOC_FAILURE);
1483 goto err;
1484 }
Adam Langley95c29f32014-06-20 12:00:00 -07001485
Adam Langleyfcf25832014-12-18 17:42:32 -08001486 encodedlen = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
1487 POINT_CONVERSION_UNCOMPRESSED,
1488 encodedPoint, encodedlen, bn_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07001489
Adam Langleyfcf25832014-12-18 17:42:32 -08001490 if (encodedlen == 0) {
1491 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1492 goto err;
1493 }
Adam Langley95c29f32014-06-20 12:00:00 -07001494
Adam Langleyfcf25832014-12-18 17:42:32 -08001495 BN_CTX_free(bn_ctx);
1496 bn_ctx = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001497
Adam Langleyfcf25832014-12-18 17:42:32 -08001498 /* We only support named (not generic) curves in ECDH ephemeral key
1499 * exchanges. In this situation, we need four additional bytes to encode
1500 * the entire ServerECDHParams structure. */
1501 n += 4 + encodedlen;
Adam Langley95c29f32014-06-20 12:00:00 -07001502
Adam Langleyfcf25832014-12-18 17:42:32 -08001503 /* We'll generate the serverKeyExchange message explicitly so we can set
1504 * these to NULLs */
1505 r[0] = NULL;
1506 r[1] = NULL;
1507 r[2] = NULL;
1508 r[3] = NULL;
1509 } else if (!(alg_k & SSL_kPSK)) {
1510 al = SSL_AD_HANDSHAKE_FAILURE;
1511 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1512 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1513 goto f_err;
1514 }
Adam Langley95c29f32014-06-20 12:00:00 -07001515
Adam Langleyfcf25832014-12-18 17:42:32 -08001516 for (i = 0; i < 4 && r[i] != NULL; i++) {
1517 nr[i] = BN_num_bytes(r[i]);
1518 n += 2 + nr[i];
1519 }
Adam Langley95c29f32014-06-20 12:00:00 -07001520
Adam Langleyfcf25832014-12-18 17:42:32 -08001521 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1522 pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher);
1523 if (pkey == NULL) {
1524 al = SSL_AD_DECODE_ERROR;
1525 goto f_err;
1526 }
1527 kn = EVP_PKEY_size(pkey);
1528 } else {
1529 pkey = NULL;
1530 kn = 0;
1531 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001532
Adam Langleyfcf25832014-12-18 17:42:32 -08001533 if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) {
1534 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_BUF);
1535 goto err;
1536 }
1537 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001538
Adam Langleyfcf25832014-12-18 17:42:32 -08001539 for (i = 0; i < 4 && r[i] != NULL; i++) {
1540 s2n(nr[i], p);
1541 BN_bn2bin(r[i], p);
1542 p += nr[i];
1543 }
David Benjamin00505ec2014-11-26 16:38:00 -05001544
Adam Langleyfcf25832014-12-18 17:42:32 -08001545 /* Note: ECDHE PSK ciphersuites use SSL_kEECDH and SSL_aPSK. When one of
1546 * them is used, the server key exchange record needs to have both the
1547 * psk_identity_hint and the ServerECDHParams. */
1548 if (alg_a & SSL_aPSK) {
1549 /* copy PSK identity hint (if provided) */
1550 s2n(psk_identity_hint_len, p);
1551 if (psk_identity_hint_len > 0) {
1552 memcpy(p, psk_identity_hint, psk_identity_hint_len);
1553 p += psk_identity_hint_len;
1554 }
1555 }
David Benjamin00505ec2014-11-26 16:38:00 -05001556
Adam Langleyfcf25832014-12-18 17:42:32 -08001557 if (alg_k & SSL_kEECDH) {
1558 /* We only support named (not generic) curves. In this situation, the
1559 * serverKeyExchange message has:
1560 * [1 byte CurveType], [2 byte CurveName]
1561 * [1 byte length of encoded point], followed by
1562 * the actual encoded point itself. */
David Benjamin70bd80a2014-12-27 03:06:46 -05001563 *(p++) = NAMED_CURVE_TYPE;
1564 *(p++) = (uint8_t)(curve_id >> 8);
1565 *(p++) = (uint8_t)(curve_id & 0xff);
1566 *(p++) = encodedlen;
1567 memcpy(p, encodedPoint, encodedlen);
1568 p += encodedlen;
Adam Langleyfcf25832014-12-18 17:42:32 -08001569 OPENSSL_free(encodedPoint);
1570 encodedPoint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001571 }
Adam Langley95c29f32014-06-20 12:00:00 -07001572
Adam Langleyfcf25832014-12-18 17:42:32 -08001573 /* not anonymous */
1574 if (pkey != NULL) {
1575 /* n is the length of the params, they start at &(d[4]) and p points to
1576 * the space at the end. */
1577 const EVP_MD *md;
1578 size_t sig_len = EVP_PKEY_size(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001579
Adam Langleyfcf25832014-12-18 17:42:32 -08001580 /* Determine signature algorithm. */
1581 if (SSL_USE_SIGALGS(s)) {
1582 md = tls1_choose_signing_digest(s, pkey);
1583 if (!tls12_get_sigandhash(p, pkey, md)) {
1584 /* Should never happen */
1585 al = SSL_AD_INTERNAL_ERROR;
1586 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1587 ERR_R_INTERNAL_ERROR);
1588 goto f_err;
1589 }
1590 p += 2;
1591 } else if (pkey->type == EVP_PKEY_RSA) {
1592 md = EVP_md5_sha1();
1593 } else {
1594 md = EVP_sha1();
1595 }
1596
1597 if (!EVP_DigestSignInit(&md_ctx, NULL, md, NULL, pkey) ||
1598 !EVP_DigestSignUpdate(&md_ctx, s->s3->client_random,
1599 SSL3_RANDOM_SIZE) ||
1600 !EVP_DigestSignUpdate(&md_ctx, s->s3->server_random,
1601 SSL3_RANDOM_SIZE) ||
1602 !EVP_DigestSignUpdate(&md_ctx, d, n) ||
1603 !EVP_DigestSignFinal(&md_ctx, &p[2], &sig_len)) {
1604 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_EVP);
1605 goto err;
1606 }
1607
1608 s2n(sig_len, p);
1609 n += sig_len + 2;
1610 if (SSL_USE_SIGALGS(s)) {
1611 n += 2;
1612 }
1613 }
1614
1615 ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n);
1616 }
1617
1618 s->state = SSL3_ST_SW_KEY_EXCH_B;
1619 EVP_MD_CTX_cleanup(&md_ctx);
1620 return ssl_do_write(s);
1621
Adam Langley95c29f32014-06-20 12:00:00 -07001622f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001623 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001624err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001625 if (encodedPoint != NULL) {
1626 OPENSSL_free(encodedPoint);
1627 }
1628 BN_CTX_free(bn_ctx);
1629 EVP_MD_CTX_cleanup(&md_ctx);
1630 return -1;
1631}
Adam Langley95c29f32014-06-20 12:00:00 -07001632
Adam Langleyfcf25832014-12-18 17:42:32 -08001633int ssl3_send_certificate_request(SSL *s) {
1634 uint8_t *p, *d;
1635 size_t i;
1636 int j, nl, off, n;
1637 STACK_OF(X509_NAME) *sk = NULL;
1638 X509_NAME *name;
1639 BUF_MEM *buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001640
Adam Langleyfcf25832014-12-18 17:42:32 -08001641 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
1642 buf = s->init_buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001643
Adam Langleyfcf25832014-12-18 17:42:32 -08001644 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001645
Adam Langleyfcf25832014-12-18 17:42:32 -08001646 /* get the list of acceptable cert types */
1647 p++;
1648 n = ssl3_get_req_cert_type(s, p);
1649 d[0] = n;
1650 p += n;
1651 n++;
Adam Langley95c29f32014-06-20 12:00:00 -07001652
Adam Langleyfcf25832014-12-18 17:42:32 -08001653 if (SSL_USE_SIGALGS(s)) {
1654 const uint8_t *psigs;
1655 nl = tls12_get_psigalgs(s, &psigs);
1656 s2n(nl, p);
1657 memcpy(p, psigs, nl);
1658 p += nl;
1659 n += nl + 2;
1660 }
Adam Langley95c29f32014-06-20 12:00:00 -07001661
Adam Langleyfcf25832014-12-18 17:42:32 -08001662 off = n;
1663 p += 2;
1664 n += 2;
Adam Langley95c29f32014-06-20 12:00:00 -07001665
Adam Langleyfcf25832014-12-18 17:42:32 -08001666 sk = SSL_get_client_CA_list(s);
1667 nl = 0;
1668 if (sk != NULL) {
1669 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1670 name = sk_X509_NAME_value(sk, i);
1671 j = i2d_X509_NAME(name, NULL);
1672 if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
1673 OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB);
1674 goto err;
1675 }
1676 p = ssl_handshake_start(s) + n;
1677 s2n(j, p);
1678 i2d_X509_NAME(name, &p);
1679 n += 2 + j;
1680 nl += 2 + j;
1681 }
1682 }
Adam Langley95c29f32014-06-20 12:00:00 -07001683
Adam Langleyfcf25832014-12-18 17:42:32 -08001684 /* else no CA names */
1685 p = ssl_handshake_start(s) + off;
1686 s2n(nl, p);
1687
1688 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n);
Adam Langleyfcf25832014-12-18 17:42:32 -08001689 s->state = SSL3_ST_SW_CERT_REQ_B;
1690 }
Adam Langley95c29f32014-06-20 12:00:00 -07001691
Adam Langleyfcf25832014-12-18 17:42:32 -08001692 /* SSL3_ST_SW_CERT_REQ_B */
1693 return ssl_do_write(s);
1694
Adam Langley95c29f32014-06-20 12:00:00 -07001695err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001696 return -1;
1697}
Adam Langley95c29f32014-06-20 12:00:00 -07001698
Adam Langleyfcf25832014-12-18 17:42:32 -08001699int ssl3_get_client_key_exchange(SSL *s) {
1700 int al, ok;
1701 long n;
1702 CBS client_key_exchange;
1703 unsigned long alg_k;
1704 unsigned long alg_a;
1705 uint8_t *premaster_secret = NULL;
1706 size_t premaster_secret_len = 0;
1707 RSA *rsa = NULL;
1708 uint8_t *decrypt_buf = NULL;
1709 EVP_PKEY *pkey = NULL;
1710 BIGNUM *pub = NULL;
1711 DH *dh_srvr;
Adam Langley95c29f32014-06-20 12:00:00 -07001712
Adam Langleyfcf25832014-12-18 17:42:32 -08001713 EC_KEY *srvr_ecdh = NULL;
1714 EVP_PKEY *clnt_pub_pkey = NULL;
1715 EC_POINT *clnt_ecpoint = NULL;
1716 BN_CTX *bn_ctx = NULL;
1717 unsigned int psk_len = 0;
1718 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001719
Adam Langleyfcf25832014-12-18 17:42:32 -08001720 n = s->method->ssl_get_message(s, SSL3_ST_SR_KEY_EXCH_A,
1721 SSL3_ST_SR_KEY_EXCH_B,
1722 SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, /* ??? */
1723 SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001724
Adam Langleyfcf25832014-12-18 17:42:32 -08001725 if (!ok) {
1726 return n;
1727 }
Adam Langley95c29f32014-06-20 12:00:00 -07001728
Adam Langleyfcf25832014-12-18 17:42:32 -08001729 CBS_init(&client_key_exchange, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001730
Adam Langleyfcf25832014-12-18 17:42:32 -08001731 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1732 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001733
Adam Langleyfcf25832014-12-18 17:42:32 -08001734 /* If using a PSK key exchange, prepare the pre-shared key. */
1735 if (alg_a & SSL_aPSK) {
1736 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001737
Adam Langleyfcf25832014-12-18 17:42:32 -08001738 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1739 * then this is the only field in the message. */
1740 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1741 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
1742 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR);
1743 al = SSL_AD_DECODE_ERROR;
1744 goto f_err;
1745 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001746
Adam Langleyfcf25832014-12-18 17:42:32 -08001747 if (s->psk_server_callback == NULL) {
1748 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1749 SSL_R_PSK_NO_SERVER_CB);
1750 al = SSL_AD_INTERNAL_ERROR;
1751 goto f_err;
1752 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001753
Adam Langleyfcf25832014-12-18 17:42:32 -08001754 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1755 CBS_contains_zero_byte(&psk_identity)) {
1756 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1757 SSL_R_DATA_LENGTH_TOO_LONG);
1758 al = SSL_AD_ILLEGAL_PARAMETER;
1759 goto f_err;
1760 }
David Benjamin35c02302014-07-13 04:14:59 -04001761
Adam Langleyfcf25832014-12-18 17:42:32 -08001762 if (!CBS_strdup(&psk_identity, &s->session->psk_identity)) {
1763 al = SSL_AD_INTERNAL_ERROR;
1764 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1765 ERR_R_MALLOC_FAILURE);
1766 goto f_err;
1767 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001768
Adam Langleyfcf25832014-12-18 17:42:32 -08001769 /* Look up the key for the identity. */
1770 psk_len =
1771 s->psk_server_callback(s, s->session->psk_identity, psk, sizeof(psk));
1772 if (psk_len > PSK_MAX_PSK_LEN) {
1773 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1774 ERR_R_INTERNAL_ERROR);
1775 al = SSL_AD_INTERNAL_ERROR;
1776 goto f_err;
1777 } else if (psk_len == 0) {
1778 /* PSK related to the given identity not found */
1779 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1780 SSL_R_PSK_IDENTITY_NOT_FOUND);
1781 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1782 goto f_err;
1783 }
1784 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001785
Adam Langleyfcf25832014-12-18 17:42:32 -08001786 /* Depending on the key exchange method, compute |premaster_secret| and
1787 * |premaster_secret_len|. */
1788 if (alg_k & SSL_kRSA) {
1789 CBS encrypted_premaster_secret;
1790 uint8_t rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
1791 uint8_t good;
1792 size_t rsa_size, decrypt_len, premaster_index, j;
Adam Langley95c29f32014-06-20 12:00:00 -07001793
Adam Langleyfcf25832014-12-18 17:42:32 -08001794 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1795 if (pkey == NULL || pkey->type != EVP_PKEY_RSA || pkey->pkey.rsa == NULL) {
1796 al = SSL_AD_HANDSHAKE_FAILURE;
1797 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1798 SSL_R_MISSING_RSA_CERTIFICATE);
1799 goto f_err;
1800 }
1801 rsa = pkey->pkey.rsa;
Adam Langley95c29f32014-06-20 12:00:00 -07001802
Adam Langleyfcf25832014-12-18 17:42:32 -08001803 /* TLS and [incidentally] DTLS{0xFEFF} */
1804 if (s->version > SSL3_VERSION) {
1805 CBS copy = client_key_exchange;
1806 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1807 &encrypted_premaster_secret) ||
1808 CBS_len(&client_key_exchange) != 0) {
1809 if (!(s->options & SSL_OP_TLS_D5_BUG)) {
1810 al = SSL_AD_DECODE_ERROR;
1811 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1812 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1813 goto f_err;
1814 } else {
1815 encrypted_premaster_secret = copy;
1816 }
1817 }
1818 } else {
1819 encrypted_premaster_secret = client_key_exchange;
1820 }
Adam Langley95c29f32014-06-20 12:00:00 -07001821
Adam Langleyfcf25832014-12-18 17:42:32 -08001822 /* Reject overly short RSA keys because we want to be sure that the buffer
1823 * size makes it safe to iterate over the entire size of a premaster secret
1824 * (SSL_MAX_MASTER_KEY_LENGTH). The actual expected size is larger due to
1825 * RSA padding, but the bound is sufficient to be safe. */
1826 rsa_size = RSA_size(rsa);
1827 if (rsa_size < SSL_MAX_MASTER_KEY_LENGTH) {
1828 al = SSL_AD_DECRYPT_ERROR;
1829 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1830 SSL_R_DECRYPTION_FAILED);
1831 goto f_err;
1832 }
Adam Langley95c29f32014-06-20 12:00:00 -07001833
Adam Langleyfcf25832014-12-18 17:42:32 -08001834 /* We must not leak whether a decryption failure occurs because of
1835 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
1836 * section 7.4.7.1). The code follows that advice of the TLS RFC and
1837 * generates a random premaster secret for the case that the decrypt fails.
1838 * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */
1839 if (!RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret))) {
1840 goto err;
1841 }
David Benjamin35c02302014-07-13 04:14:59 -04001842
Adam Langleyfcf25832014-12-18 17:42:32 -08001843 /* Allocate a buffer large enough for an RSA decryption. */
1844 decrypt_buf = OPENSSL_malloc(rsa_size);
1845 if (decrypt_buf == NULL) {
1846 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1847 ERR_R_MALLOC_FAILURE);
1848 goto err;
1849 }
David Benjamin35c02302014-07-13 04:14:59 -04001850
Adam Langleyfcf25832014-12-18 17:42:32 -08001851 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1852 * timing-sensitive code below. */
1853 if (!RSA_decrypt(rsa, &decrypt_len, decrypt_buf, rsa_size,
1854 CBS_data(&encrypted_premaster_secret),
1855 CBS_len(&encrypted_premaster_secret), RSA_NO_PADDING)) {
1856 goto err;
1857 }
1858 if (decrypt_len != rsa_size) {
1859 /* This should never happen, but do a check so we do not read
1860 * uninitialized memory. */
1861 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1862 ERR_R_INTERNAL_ERROR);
1863 goto err;
1864 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001865
Adam Langleyfcf25832014-12-18 17:42:32 -08001866 /* Remove the PKCS#1 padding and adjust |decrypt_len| as appropriate.
1867 * |good| will be 0xff if the premaster is acceptable and zero otherwise.
1868 * */
1869 good =
1870 constant_time_eq_int_8(RSA_message_index_PKCS1_type_2(
1871 decrypt_buf, decrypt_len, &premaster_index),
1872 1);
1873 decrypt_len = decrypt_len - premaster_index;
David Benjamin0aa07672014-07-24 15:34:14 -04001874
Adam Langleyfcf25832014-12-18 17:42:32 -08001875 /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. */
1876 good &= constant_time_eq_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
Adam Langleyacff73f2014-06-20 12:00:00 -07001877
Adam Langleyfcf25832014-12-18 17:42:32 -08001878 /* Copy over the unpadded premaster. Whatever the value of
1879 * |decrypt_good_mask|, copy as if the premaster were the right length. It
1880 * is important the memory access pattern be constant. */
1881 premaster_secret =
1882 BUF_memdup(decrypt_buf + (rsa_size - SSL_MAX_MASTER_KEY_LENGTH),
1883 SSL_MAX_MASTER_KEY_LENGTH);
1884 if (premaster_secret == NULL) {
1885 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1886 ERR_R_MALLOC_FAILURE);
1887 goto err;
1888 }
1889 OPENSSL_free(decrypt_buf);
1890 decrypt_buf = NULL;
Adam Langleyacff73f2014-06-20 12:00:00 -07001891
Adam Langleyfcf25832014-12-18 17:42:32 -08001892 /* If the version in the decrypted pre-master secret is correct then
1893 * version_good will be 0xff, otherwise it'll be zero. The
1894 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
1895 * (http://eprint.iacr.org/2003/052/) exploits the version number check as
1896 * a "bad version oracle". Thus version checks are done in constant time
1897 * and are treated like any other decryption error. */
1898 good &= constant_time_eq_8(premaster_secret[0],
1899 (unsigned)(s->client_version >> 8));
1900 good &= constant_time_eq_8(premaster_secret[1],
1901 (unsigned)(s->client_version & 0xff));
Adam Langleyacff73f2014-06-20 12:00:00 -07001902
Adam Langleyfcf25832014-12-18 17:42:32 -08001903 /* Now copy rand_premaster_secret over premaster_secret using
1904 * decrypt_good_mask. */
1905 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
1906 premaster_secret[j] = constant_time_select_8(good, premaster_secret[j],
1907 rand_premaster_secret[j]);
1908 }
David Benjamin35c02302014-07-13 04:14:59 -04001909
Adam Langleyfcf25832014-12-18 17:42:32 -08001910 premaster_secret_len = sizeof(rand_premaster_secret);
1911 } else if (alg_k & SSL_kEDH) {
1912 CBS dh_Yc;
1913 int dh_len;
David Benjamin35c02302014-07-13 04:14:59 -04001914
Adam Langleyfcf25832014-12-18 17:42:32 -08001915 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &dh_Yc) ||
1916 CBS_len(&dh_Yc) == 0 || CBS_len(&client_key_exchange) != 0) {
1917 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1918 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1919 al = SSL_R_DECODE_ERROR;
1920 goto f_err;
1921 }
Adam Langley95c29f32014-06-20 12:00:00 -07001922
Adam Langleyfcf25832014-12-18 17:42:32 -08001923 if (s->s3->tmp.dh == NULL) {
1924 al = SSL_AD_HANDSHAKE_FAILURE;
1925 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1926 SSL_R_MISSING_TMP_DH_KEY);
1927 goto f_err;
1928 }
1929 dh_srvr = s->s3->tmp.dh;
Adam Langley95c29f32014-06-20 12:00:00 -07001930
Adam Langleyfcf25832014-12-18 17:42:32 -08001931 pub = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL);
1932 if (pub == NULL) {
1933 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_BN_LIB);
1934 goto err;
1935 }
Adam Langley95c29f32014-06-20 12:00:00 -07001936
Adam Langleyfcf25832014-12-18 17:42:32 -08001937 /* Allocate a buffer for the premaster secret. */
1938 premaster_secret = OPENSSL_malloc(DH_size(dh_srvr));
1939 if (premaster_secret == NULL) {
1940 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1941 ERR_R_MALLOC_FAILURE);
David Benjaminc35fb012015-02-12 00:48:45 -05001942 BN_clear_free(pub);
Adam Langleyfcf25832014-12-18 17:42:32 -08001943 goto err;
1944 }
Adam Langley95c29f32014-06-20 12:00:00 -07001945
Adam Langleyfcf25832014-12-18 17:42:32 -08001946 dh_len = DH_compute_key(premaster_secret, pub, dh_srvr);
1947 if (dh_len <= 0) {
1948 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_DH_LIB);
1949 BN_clear_free(pub);
1950 goto err;
1951 }
David Benjamin14c83e72014-07-13 04:54:57 -04001952
Adam Langleyfcf25832014-12-18 17:42:32 -08001953 DH_free(s->s3->tmp.dh);
1954 s->s3->tmp.dh = NULL;
1955 BN_clear_free(pub);
1956 pub = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001957
Adam Langleyfcf25832014-12-18 17:42:32 -08001958 premaster_secret_len = dh_len;
1959 } else if (alg_k & SSL_kEECDH) {
1960 int field_size = 0, ecdh_len;
1961 const EC_KEY *tkey;
1962 const EC_GROUP *group;
1963 const BIGNUM *priv_key;
1964 CBS ecdh_Yc;
Adam Langley95c29f32014-06-20 12:00:00 -07001965
Adam Langleyfcf25832014-12-18 17:42:32 -08001966 /* initialize structures for server's ECDH key pair */
1967 srvr_ecdh = EC_KEY_new();
1968 if (srvr_ecdh == NULL) {
1969 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1970 ERR_R_MALLOC_FAILURE);
1971 goto err;
1972 }
Adam Langley95c29f32014-06-20 12:00:00 -07001973
Adam Langleyfcf25832014-12-18 17:42:32 -08001974 /* Use the ephermeral values we saved when generating the ServerKeyExchange
1975 * msg. */
1976 tkey = s->s3->tmp.ecdh;
Adam Langley95c29f32014-06-20 12:00:00 -07001977
Adam Langleyfcf25832014-12-18 17:42:32 -08001978 group = EC_KEY_get0_group(tkey);
1979 priv_key = EC_KEY_get0_private_key(tkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001980
Adam Langleyfcf25832014-12-18 17:42:32 -08001981 if (!EC_KEY_set_group(srvr_ecdh, group) ||
1982 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
1983 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
1984 goto err;
1985 }
Adam Langley95c29f32014-06-20 12:00:00 -07001986
Adam Langleyfcf25832014-12-18 17:42:32 -08001987 /* Let's get client's public key */
1988 clnt_ecpoint = EC_POINT_new(group);
1989 if (clnt_ecpoint == NULL) {
1990 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1991 ERR_R_MALLOC_FAILURE);
1992 goto err;
1993 }
Adam Langley95c29f32014-06-20 12:00:00 -07001994
Adam Langleyfcf25832014-12-18 17:42:32 -08001995 /* Get client's public key from encoded point in the ClientKeyExchange
1996 * message. */
1997 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ecdh_Yc) ||
1998 CBS_len(&client_key_exchange) != 0) {
1999 al = SSL_AD_DECODE_ERROR;
2000 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR);
2001 goto f_err;
2002 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04002003
Adam Langleyfcf25832014-12-18 17:42:32 -08002004 bn_ctx = BN_CTX_new();
2005 if (bn_ctx == NULL) {
2006 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2007 ERR_R_MALLOC_FAILURE);
2008 goto err;
2009 }
David Benjamin35c02302014-07-13 04:14:59 -04002010
Adam Langleyfcf25832014-12-18 17:42:32 -08002011 if (!EC_POINT_oct2point(group, clnt_ecpoint, CBS_data(&ecdh_Yc),
2012 CBS_len(&ecdh_Yc), bn_ctx)) {
2013 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
2014 goto err;
2015 }
Adam Langley95c29f32014-06-20 12:00:00 -07002016
Adam Langleyfcf25832014-12-18 17:42:32 -08002017 /* Allocate a buffer for both the secret and the PSK. */
2018 field_size = EC_GROUP_get_degree(group);
2019 if (field_size <= 0) {
2020 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
2021 goto err;
2022 }
David Benjamin35c02302014-07-13 04:14:59 -04002023
Adam Langleyfcf25832014-12-18 17:42:32 -08002024 ecdh_len = (field_size + 7) / 8;
2025 premaster_secret = OPENSSL_malloc(ecdh_len);
2026 if (premaster_secret == NULL) {
2027 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2028 ERR_R_MALLOC_FAILURE);
2029 goto err;
2030 }
David Benjamin35c02302014-07-13 04:14:59 -04002031
Adam Langleyfcf25832014-12-18 17:42:32 -08002032 /* Compute the shared pre-master secret */
2033 ecdh_len = ECDH_compute_key(premaster_secret, ecdh_len, clnt_ecpoint,
2034 srvr_ecdh, NULL);
2035 if (ecdh_len <= 0) {
2036 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
2037 goto err;
2038 }
Adam Langley95c29f32014-06-20 12:00:00 -07002039
Adam Langleyfcf25832014-12-18 17:42:32 -08002040 EVP_PKEY_free(clnt_pub_pkey);
2041 clnt_pub_pkey = NULL;
2042 EC_POINT_free(clnt_ecpoint);
2043 clnt_ecpoint = NULL;
2044 EC_KEY_free(srvr_ecdh);
2045 srvr_ecdh = NULL;
2046 BN_CTX_free(bn_ctx);
2047 bn_ctx = NULL;
2048 EC_KEY_free(s->s3->tmp.ecdh);
2049 s->s3->tmp.ecdh = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002050
Adam Langleyfcf25832014-12-18 17:42:32 -08002051 premaster_secret_len = ecdh_len;
2052 } else if (alg_k & SSL_kPSK) {
2053 /* For plain PSK, other_secret is a block of 0s with the same length as the
2054 * pre-shared key. */
2055 premaster_secret_len = psk_len;
2056 premaster_secret = OPENSSL_malloc(premaster_secret_len);
2057 if (premaster_secret == NULL) {
2058 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2059 ERR_R_MALLOC_FAILURE);
2060 goto err;
2061 }
2062 memset(premaster_secret, 0, premaster_secret_len);
2063 } else {
2064 al = SSL_AD_HANDSHAKE_FAILURE;
2065 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2066 SSL_R_UNKNOWN_CIPHER_TYPE);
2067 goto f_err;
2068 }
Adam Langley95c29f32014-06-20 12:00:00 -07002069
Adam Langleyfcf25832014-12-18 17:42:32 -08002070 /* For a PSK cipher suite, the actual pre-master secret is combined with the
2071 * pre-shared key. */
2072 if (alg_a & SSL_aPSK) {
2073 CBB new_premaster, child;
2074 uint8_t *new_data;
2075 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04002076
Adam Langleyfcf25832014-12-18 17:42:32 -08002077 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len)) {
2078 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2079 ERR_R_MALLOC_FAILURE);
2080 goto err;
2081 }
2082 if (!CBB_add_u16_length_prefixed(&new_premaster, &child) ||
2083 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
2084 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
2085 !CBB_add_bytes(&child, psk, psk_len) ||
2086 !CBB_finish(&new_premaster, &new_data, &new_len)) {
2087 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
2088 ERR_R_INTERNAL_ERROR);
2089 CBB_cleanup(&new_premaster);
2090 goto err;
2091 }
David Benjamin14c83e72014-07-13 04:54:57 -04002092
Adam Langleyfcf25832014-12-18 17:42:32 -08002093 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2094 OPENSSL_free(premaster_secret);
2095 premaster_secret = new_data;
2096 premaster_secret_len = new_len;
2097 }
David Benjamin14c83e72014-07-13 04:54:57 -04002098
Adam Langleyfcf25832014-12-18 17:42:32 -08002099 /* Compute the master secret */
2100 s->session->master_key_length = s->enc_method->generate_master_secret(
2101 s, s->session->master_key, premaster_secret, premaster_secret_len);
2102 if (s->session->master_key_length == 0) {
2103 goto err;
2104 }
2105 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04002106
Adam Langleyfcf25832014-12-18 17:42:32 -08002107 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2108 OPENSSL_free(premaster_secret);
2109 return 1;
2110
Adam Langley95c29f32014-06-20 12:00:00 -07002111f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002112 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07002113err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002114 if (premaster_secret) {
2115 if (premaster_secret_len) {
2116 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2117 }
2118 OPENSSL_free(premaster_secret);
2119 }
2120 if (decrypt_buf) {
2121 OPENSSL_free(decrypt_buf);
2122 }
2123 EVP_PKEY_free(clnt_pub_pkey);
2124 EC_POINT_free(clnt_ecpoint);
2125 if (srvr_ecdh != NULL) {
2126 EC_KEY_free(srvr_ecdh);
2127 }
2128 BN_CTX_free(bn_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002129
Adam Langleyfcf25832014-12-18 17:42:32 -08002130 return -1;
2131}
Adam Langley95c29f32014-06-20 12:00:00 -07002132
Adam Langleyfcf25832014-12-18 17:42:32 -08002133int ssl3_get_cert_verify(SSL *s) {
2134 int al, ok, ret = 0;
2135 long n;
2136 CBS certificate_verify, signature;
2137 X509 *peer = s->session->peer;
2138 EVP_PKEY *pkey = NULL;
2139 const EVP_MD *md = NULL;
2140 uint8_t digest[EVP_MAX_MD_SIZE];
2141 size_t digest_length;
2142 EVP_PKEY_CTX *pctx = NULL;
David Benjamin6553b372014-07-22 14:11:30 -04002143
Adam Langleyfcf25832014-12-18 17:42:32 -08002144 /* Only RSA and ECDSA client certificates are supported, so a
2145 * CertificateVerify is required if and only if there's a client certificate.
2146 * */
2147 if (peer == NULL) {
2148 if (s->s3->handshake_buffer &&
2149 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2150 return -1;
2151 }
2152 return 1;
2153 }
Adam Langley95c29f32014-06-20 12:00:00 -07002154
Adam Langleyfcf25832014-12-18 17:42:32 -08002155 n = s->method->ssl_get_message(
2156 s, SSL3_ST_SR_CERT_VRFY_A, SSL3_ST_SR_CERT_VRFY_B,
2157 SSL3_MT_CERTIFICATE_VERIFY, SSL3_RT_MAX_PLAIN_LENGTH,
2158 SSL_GET_MESSAGE_DONT_HASH_MESSAGE, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002159
Adam Langleyfcf25832014-12-18 17:42:32 -08002160 if (!ok) {
2161 return n;
2162 }
David Benjaminef865502014-08-24 02:48:34 -04002163
Adam Langleyfcf25832014-12-18 17:42:32 -08002164 /* Filter out unsupported certificate types. */
2165 pkey = X509_get_pubkey(peer);
2166 if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) ||
2167 (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) {
2168 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
2169 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify,
2170 SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
2171 goto f_err;
2172 }
David Benjamin6897dbe2014-07-12 20:18:28 -04002173
Adam Langleyfcf25832014-12-18 17:42:32 -08002174 CBS_init(&certificate_verify, s->init_msg, n);
David Benjamin6897dbe2014-07-12 20:18:28 -04002175
Adam Langleyfcf25832014-12-18 17:42:32 -08002176 /* Determine the digest type if needbe. */
2177 if (SSL_USE_SIGALGS(s) &&
2178 !tls12_check_peer_sigalg(&md, &al, s, &certificate_verify, pkey)) {
2179 goto f_err;
2180 }
David Benjamin854dd652014-08-26 00:32:30 -04002181
Adam Langleyfcf25832014-12-18 17:42:32 -08002182 /* Compute the digest. */
2183 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2184 goto err;
2185 }
David Benjamin854dd652014-08-26 00:32:30 -04002186
Adam Langleyfcf25832014-12-18 17:42:32 -08002187 /* The handshake buffer is no longer necessary, and we may hash the current
2188 * message.*/
2189 if (s->s3->handshake_buffer &&
2190 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2191 goto err;
2192 }
2193 ssl3_hash_current_message(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002194
Adam Langleyfcf25832014-12-18 17:42:32 -08002195 /* Parse and verify the signature. */
2196 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
2197 CBS_len(&certificate_verify) != 0) {
2198 al = SSL_AD_DECODE_ERROR;
2199 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_DECODE_ERROR);
2200 goto f_err;
2201 }
Adam Langley95c29f32014-06-20 12:00:00 -07002202
Adam Langleyfcf25832014-12-18 17:42:32 -08002203 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2204 if (pctx == NULL) {
2205 goto err;
2206 }
2207 if (!EVP_PKEY_verify_init(pctx) ||
2208 !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2209 !EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), digest,
2210 digest_length)) {
2211 al = SSL_AD_DECRYPT_ERROR;
2212 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_SIGNATURE);
2213 goto f_err;
2214 }
2215
2216 ret = 1;
2217
2218 if (0) {
2219 f_err:
2220 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2221 }
2222
David Benjamin854dd652014-08-26 00:32:30 -04002223err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002224 EVP_PKEY_CTX_free(pctx);
2225 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07002226
Adam Langleyfcf25832014-12-18 17:42:32 -08002227 return ret;
2228}
Adam Langley95c29f32014-06-20 12:00:00 -07002229
Adam Langleyfcf25832014-12-18 17:42:32 -08002230int ssl3_get_client_certificate(SSL *s) {
2231 int i, ok, al, ret = -1;
2232 X509 *x = NULL;
2233 unsigned long n;
2234 STACK_OF(X509) *sk = NULL;
2235 SHA256_CTX sha256;
2236 CBS certificate_msg, certificate_list;
2237 int is_first_certificate = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002238
Adam Langleyfcf25832014-12-18 17:42:32 -08002239 n = s->method->ssl_get_message(s, SSL3_ST_SR_CERT_A, SSL3_ST_SR_CERT_B, -1,
2240 s->max_cert_list, SSL_GET_MESSAGE_HASH_MESSAGE,
2241 &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002242
Adam Langleyfcf25832014-12-18 17:42:32 -08002243 if (!ok) {
2244 return n;
2245 }
Adam Langley95c29f32014-06-20 12:00:00 -07002246
Adam Langleyfcf25832014-12-18 17:42:32 -08002247 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
2248 if ((s->verify_mode & SSL_VERIFY_PEER) &&
2249 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2250 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2251 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2252 al = SSL_AD_HANDSHAKE_FAILURE;
2253 goto f_err;
2254 }
David Benjamind5b1f842014-07-11 23:43:48 -04002255
Adam Langleyfcf25832014-12-18 17:42:32 -08002256 /* If tls asked for a client cert, the client must return a 0 list */
2257 if (s->version > SSL3_VERSION && s->s3->tmp.cert_request) {
2258 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2259 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
2260 al = SSL_AD_UNEXPECTED_MESSAGE;
2261 goto f_err;
2262 }
2263 s->s3->tmp.reuse_message = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002264
Adam Langleyfcf25832014-12-18 17:42:32 -08002265 return 1;
2266 }
Adam Langley95c29f32014-06-20 12:00:00 -07002267
Adam Langleyfcf25832014-12-18 17:42:32 -08002268 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
2269 al = SSL_AD_UNEXPECTED_MESSAGE;
2270 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2271 SSL_R_WRONG_MESSAGE_TYPE);
2272 goto f_err;
2273 }
David Benjamind5b1f842014-07-11 23:43:48 -04002274
Adam Langleyfcf25832014-12-18 17:42:32 -08002275 CBS_init(&certificate_msg, s->init_msg, n);
David Benjamind5b1f842014-07-11 23:43:48 -04002276
Adam Langleyfcf25832014-12-18 17:42:32 -08002277 sk = sk_X509_new_null();
2278 if (sk == NULL) {
2279 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2280 goto err;
2281 }
Adam Langley95c29f32014-06-20 12:00:00 -07002282
Adam Langleyfcf25832014-12-18 17:42:32 -08002283 if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) ||
2284 CBS_len(&certificate_msg) != 0) {
2285 al = SSL_AD_DECODE_ERROR;
2286 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR);
2287 goto f_err;
2288 }
Adam Langley95c29f32014-06-20 12:00:00 -07002289
Adam Langleyfcf25832014-12-18 17:42:32 -08002290 while (CBS_len(&certificate_list) > 0) {
2291 CBS certificate;
2292 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07002293
Adam Langleyfcf25832014-12-18 17:42:32 -08002294 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
2295 al = SSL_AD_DECODE_ERROR;
2296 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR);
2297 goto f_err;
2298 }
Adam Langley95c29f32014-06-20 12:00:00 -07002299
Adam Langleyfcf25832014-12-18 17:42:32 -08002300 if (is_first_certificate && s->ctx->retain_only_sha256_of_client_certs) {
2301 /* If this is the first certificate, and we don't want to keep peer
2302 * certificates in memory, then we hash it right away. */
2303 SHA256_Init(&sha256);
2304 SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate));
2305 SHA256_Final(s->session->peer_sha256, &sha256);
2306 s->session->peer_sha256_valid = 1;
2307 }
2308 is_first_certificate = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002309
Adam Langleyfcf25832014-12-18 17:42:32 -08002310 data = CBS_data(&certificate);
2311 x = d2i_X509(NULL, &data, CBS_len(&certificate));
2312 if (x == NULL) {
2313 al = SSL_AD_BAD_CERTIFICATE;
2314 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_ASN1_LIB);
2315 goto f_err;
2316 }
2317 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
2318 al = SSL_AD_DECODE_ERROR;
2319 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2320 SSL_R_CERT_LENGTH_MISMATCH);
2321 goto f_err;
2322 }
2323 if (!sk_X509_push(sk, x)) {
2324 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2325 goto err;
2326 }
2327 x = NULL;
2328 }
2329
2330 if (sk_X509_num(sk) <= 0) {
2331 /* TLS does not mind 0 certs returned */
2332 if (s->version == SSL3_VERSION) {
2333 al = SSL_AD_HANDSHAKE_FAILURE;
2334 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2335 SSL_R_NO_CERTIFICATES_RETURNED);
2336 goto f_err;
2337 }
2338 /* Fail for TLS only if we required a certificate */
2339 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
2340 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2341 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2342 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2343 al = SSL_AD_HANDSHAKE_FAILURE;
2344 goto f_err;
2345 }
2346 /* No client certificate so digest cached records */
2347 if (s->s3->handshake_buffer &&
2348 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2349 al = SSL_AD_INTERNAL_ERROR;
2350 goto f_err;
2351 }
2352 } else {
2353 i = ssl_verify_cert_chain(s, sk);
2354 if (i <= 0) {
2355 al = ssl_verify_alarm_type(s->verify_result);
2356 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2357 SSL_R_CERTIFICATE_VERIFY_FAILED);
2358 goto f_err;
2359 }
2360 }
2361
2362 if (s->session->peer != NULL) {
2363 /* This should not be needed */
2364 X509_free(s->session->peer);
2365 }
2366
2367 s->session->peer = sk_X509_shift(sk);
2368 s->session->verify_result = s->verify_result;
2369
2370 /* With the current implementation, sess_cert will always be NULL when we
2371 * arrive here. */
2372 if (s->session->sess_cert == NULL) {
2373 s->session->sess_cert = ssl_sess_cert_new();
2374 if (s->session->sess_cert == NULL) {
2375 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2376 goto err;
2377 }
2378 }
2379 if (s->session->sess_cert->cert_chain != NULL) {
2380 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
2381 }
2382 s->session->sess_cert->cert_chain = sk;
2383 /* Inconsistency alert: cert_chain does *not* include the peer's own
2384 * certificate, while we do include it in s3_clnt.c */
2385
2386 sk = NULL;
2387
2388 ret = 1;
2389
2390 if (0) {
2391 f_err:
2392 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2393 }
2394
Adam Langley95c29f32014-06-20 12:00:00 -07002395err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002396 if (x != NULL) {
2397 X509_free(x);
2398 }
2399 if (sk != NULL) {
2400 sk_X509_pop_free(sk, X509_free);
2401 }
2402 return ret;
2403}
Adam Langley95c29f32014-06-20 12:00:00 -07002404
Adam Langleyfcf25832014-12-18 17:42:32 -08002405int ssl3_send_server_certificate(SSL *s) {
2406 CERT_PKEY *cpk;
Adam Langley95c29f32014-06-20 12:00:00 -07002407
Adam Langleyfcf25832014-12-18 17:42:32 -08002408 if (s->state == SSL3_ST_SW_CERT_A) {
2409 cpk = ssl_get_server_send_pkey(s);
2410 if (cpk == NULL) {
2411 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_certificate,
2412 ERR_R_INTERNAL_ERROR);
2413 return 0;
2414 }
Adam Langley95c29f32014-06-20 12:00:00 -07002415
Adam Langleyfcf25832014-12-18 17:42:32 -08002416 ssl3_output_cert_chain(s, cpk);
2417 s->state = SSL3_ST_SW_CERT_B;
2418 }
Adam Langley95c29f32014-06-20 12:00:00 -07002419
Adam Langleyfcf25832014-12-18 17:42:32 -08002420 /* SSL3_ST_SW_CERT_B */
2421 return ssl_do_write(s);
2422}
Adam Langley95c29f32014-06-20 12:00:00 -07002423
Adam Langley95c29f32014-06-20 12:00:00 -07002424/* send a new session ticket (not necessarily for a new session) */
Adam Langleyfcf25832014-12-18 17:42:32 -08002425int ssl3_send_new_session_ticket(SSL *s) {
2426 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
2427 uint8_t *session;
2428 size_t session_len;
2429 uint8_t *p, *macstart;
2430 int len;
2431 unsigned int hlen;
2432 EVP_CIPHER_CTX ctx;
2433 HMAC_CTX hctx;
2434 SSL_CTX *tctx = s->initial_ctx;
2435 uint8_t iv[EVP_MAX_IV_LENGTH];
2436 uint8_t key_name[16];
2437 /* The maximum overhead of encrypting the session is 16 (key name) + IV +
2438 * one block of encryption overhead + HMAC. */
2439 const size_t max_ticket_overhead =
2440 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -07002441
Adam Langleyfcf25832014-12-18 17:42:32 -08002442 /* Serialize the SSL_SESSION to be encoded into the ticket. */
2443 if (!SSL_SESSION_to_bytes_for_ticket(s->session, &session, &session_len)) {
2444 return -1;
2445 }
Adam Langley95c29f32014-06-20 12:00:00 -07002446
Adam Langleyfcf25832014-12-18 17:42:32 -08002447 /* If the session is too long, emit a dummy value rather than abort the
2448 * connection. */
2449 if (session_len > 0xFFFF - max_ticket_overhead) {
2450 static const char kTicketPlaceholder[] = "TICKET TOO LARGE";
2451 const size_t placeholder_len = strlen(kTicketPlaceholder);
David Benjamin3cac4502014-10-21 01:46:30 -04002452
Adam Langleyfcf25832014-12-18 17:42:32 -08002453 OPENSSL_free(session);
David Benjamin3cac4502014-10-21 01:46:30 -04002454
Adam Langleyfcf25832014-12-18 17:42:32 -08002455 p = ssl_handshake_start(s);
2456 /* Emit ticket_lifetime_hint. */
2457 l2n(0, p);
2458 /* Emit ticket. */
2459 s2n(placeholder_len, p);
2460 memcpy(p, kTicketPlaceholder, placeholder_len);
2461 p += placeholder_len;
David Benjamin3cac4502014-10-21 01:46:30 -04002462
Adam Langleyfcf25832014-12-18 17:42:32 -08002463 len = p - ssl_handshake_start(s);
2464 ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len);
2465 s->state = SSL3_ST_SW_SESSION_TICKET_B;
2466 return ssl_do_write(s);
2467 }
Adam Langley95c29f32014-06-20 12:00:00 -07002468
Adam Langleyfcf25832014-12-18 17:42:32 -08002469 /* Grow buffer if need be: the length calculation is as follows:
2470 * handshake_header_length + 4 (ticket lifetime hint) + 2 (ticket length) +
2471 * max_ticket_overhead + * session_length */
2472 if (!BUF_MEM_grow(s->init_buf, SSL_HM_HEADER_LENGTH(s) + 6 +
2473 max_ticket_overhead + session_len)) {
2474 OPENSSL_free(session);
2475 return -1;
2476 }
2477 p = ssl_handshake_start(s);
2478 EVP_CIPHER_CTX_init(&ctx);
2479 HMAC_CTX_init(&hctx);
2480 /* Initialize HMAC and cipher contexts. If callback present it does all the
2481 * work otherwise use generated values from parent ctx. */
2482 if (tctx->tlsext_ticket_key_cb) {
2483 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, &hctx, 1) < 0) {
2484 OPENSSL_free(session);
2485 EVP_CIPHER_CTX_cleanup(&ctx);
2486 HMAC_CTX_cleanup(&hctx);
2487 return -1;
2488 }
2489 } else {
2490 if (!RAND_bytes(iv, 16) ||
2491 !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2492 tctx->tlsext_tick_aes_key, iv) ||
2493 !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2494 NULL)) {
2495 OPENSSL_free(session);
2496 EVP_CIPHER_CTX_cleanup(&ctx);
2497 HMAC_CTX_cleanup(&hctx);
2498 return -1;
2499 }
2500 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
2501 }
Adam Langley95c29f32014-06-20 12:00:00 -07002502
Adam Langleyfcf25832014-12-18 17:42:32 -08002503 /* Ticket lifetime hint (advisory only): We leave this unspecified for
2504 * resumed session (for simplicity), and guess that tickets for new
2505 * sessions will live as long as their sessions. */
2506 l2n(s->hit ? 0 : s->session->timeout, p);
Adam Langley95c29f32014-06-20 12:00:00 -07002507
Adam Langleyfcf25832014-12-18 17:42:32 -08002508 /* Skip ticket length for now */
2509 p += 2;
2510 /* Output key name */
2511 macstart = p;
2512 memcpy(p, key_name, 16);
2513 p += 16;
2514 /* output IV */
2515 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
2516 p += EVP_CIPHER_CTX_iv_length(&ctx);
2517 /* Encrypt session data */
2518 EVP_EncryptUpdate(&ctx, p, &len, session, session_len);
2519 p += len;
2520 EVP_EncryptFinal_ex(&ctx, p, &len);
2521 p += len;
2522 EVP_CIPHER_CTX_cleanup(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002523
Adam Langleyfcf25832014-12-18 17:42:32 -08002524 HMAC_Update(&hctx, macstart, p - macstart);
2525 HMAC_Final(&hctx, p, &hlen);
2526 HMAC_CTX_cleanup(&hctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002527
Adam Langleyfcf25832014-12-18 17:42:32 -08002528 p += hlen;
2529 /* Now write out lengths: p points to end of data written */
2530 /* Total length */
2531 len = p - ssl_handshake_start(s);
2532 /* Skip ticket lifetime hint */
2533 p = ssl_handshake_start(s) + 4;
2534 s2n(len - 6, p);
2535 ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len);
2536 s->state = SSL3_ST_SW_SESSION_TICKET_B;
2537 OPENSSL_free(session);
2538 }
Adam Langley95c29f32014-06-20 12:00:00 -07002539
Adam Langleyfcf25832014-12-18 17:42:32 -08002540 /* SSL3_ST_SW_SESSION_TICKET_B */
2541 return ssl_do_write(s);
2542}
Adam Langley95c29f32014-06-20 12:00:00 -07002543
Adam Langley95c29f32014-06-20 12:00:00 -07002544/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
2545 * sets the next_proto member in s if found */
Adam Langleyfcf25832014-12-18 17:42:32 -08002546int ssl3_get_next_proto(SSL *s) {
2547 int ok;
2548 long n;
2549 CBS next_protocol, selected_protocol, padding;
Adam Langley95c29f32014-06-20 12:00:00 -07002550
Adam Langleyfcf25832014-12-18 17:42:32 -08002551 /* Clients cannot send a NextProtocol message if we didn't see the extension
2552 * in their ClientHello */
2553 if (!s->s3->next_proto_neg_seen) {
2554 OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto,
2555 SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
2556 return -1;
2557 }
Adam Langley95c29f32014-06-20 12:00:00 -07002558
Adam Langleyfcf25832014-12-18 17:42:32 -08002559 n = s->method->ssl_get_message(s, SSL3_ST_SR_NEXT_PROTO_A,
2560 SSL3_ST_SR_NEXT_PROTO_B, SSL3_MT_NEXT_PROTO,
2561 514, /* See the payload format below */
2562 SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002563
Adam Langleyfcf25832014-12-18 17:42:32 -08002564 if (!ok) {
2565 return n;
2566 }
Adam Langley95c29f32014-06-20 12:00:00 -07002567
Adam Langleyfcf25832014-12-18 17:42:32 -08002568 /* s->state doesn't reflect whether ChangeCipherSpec has been received in
2569 * this handshake, but s->s3->change_cipher_spec does (will be reset by
2570 * ssl3_get_finished).
2571 *
2572 * TODO(davidben): Is this check now redundant with
2573 * SSL3_FLAGS_EXPECT_CCS? */
2574 if (!s->s3->change_cipher_spec) {
2575 OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto,
2576 SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
2577 return -1;
2578 }
Adam Langley95c29f32014-06-20 12:00:00 -07002579
Adam Langleyfcf25832014-12-18 17:42:32 -08002580 CBS_init(&next_protocol, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07002581
Adam Langleyfcf25832014-12-18 17:42:32 -08002582 /* The payload looks like:
2583 * uint8 proto_len;
2584 * uint8 proto[proto_len];
2585 * uint8 padding_len;
2586 * uint8 padding[padding_len]; */
2587 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
2588 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
2589 CBS_len(&next_protocol) != 0 ||
2590 !CBS_stow(&selected_protocol, &s->next_proto_negotiated,
2591 &s->next_proto_negotiated_len)) {
2592 return 0;
2593 }
Adam Langley95c29f32014-06-20 12:00:00 -07002594
Adam Langleyfcf25832014-12-18 17:42:32 -08002595 return 1;
2596}
Adam Langley95c29f32014-06-20 12:00:00 -07002597
Adam Langley1258b6a2014-06-20 12:00:00 -07002598/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002599int ssl3_get_channel_id(SSL *s) {
2600 int ret = -1, ok;
2601 long n;
2602 EVP_MD_CTX md_ctx;
2603 uint8_t channel_id_hash[SHA256_DIGEST_LENGTH];
2604 unsigned int channel_id_hash_len;
2605 const uint8_t *p;
2606 uint16_t extension_type, expected_extension_type;
2607 EC_GROUP *p256 = NULL;
2608 EC_KEY *key = NULL;
2609 EC_POINT *point = NULL;
2610 ECDSA_SIG sig;
2611 BIGNUM x, y;
2612 CBS encrypted_extensions, extension;
Adam Langley1258b6a2014-06-20 12:00:00 -07002613
Adam Langleyfcf25832014-12-18 17:42:32 -08002614 n = s->method->ssl_get_message(
2615 s, SSL3_ST_SR_CHANNEL_ID_A, SSL3_ST_SR_CHANNEL_ID_B,
2616 SSL3_MT_ENCRYPTED_EXTENSIONS, 2 + 2 + TLSEXT_CHANNEL_ID_SIZE,
2617 SSL_GET_MESSAGE_DONT_HASH_MESSAGE, &ok);
Adam Langley1258b6a2014-06-20 12:00:00 -07002618
Adam Langleyfcf25832014-12-18 17:42:32 -08002619 if (!ok) {
2620 return n;
2621 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002622
Adam Langleyfcf25832014-12-18 17:42:32 -08002623 /* Before incorporating the EncryptedExtensions message to the handshake
2624 * hash, compute the hash that should have been signed. */
2625 channel_id_hash_len = sizeof(channel_id_hash);
2626 EVP_MD_CTX_init(&md_ctx);
2627 if (!EVP_DigestInit_ex(&md_ctx, EVP_sha256(), NULL) ||
2628 !tls1_channel_id_hash(&md_ctx, s) ||
2629 !EVP_DigestFinal(&md_ctx, channel_id_hash, &channel_id_hash_len)) {
2630 EVP_MD_CTX_cleanup(&md_ctx);
2631 return -1;
2632 }
2633 EVP_MD_CTX_cleanup(&md_ctx);
2634 assert(channel_id_hash_len == SHA256_DIGEST_LENGTH);
David Benjamin880b14e2014-08-25 22:35:07 -04002635
Adam Langleyfcf25832014-12-18 17:42:32 -08002636 ssl3_hash_current_message(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07002637
Adam Langleyfcf25832014-12-18 17:42:32 -08002638 /* s->state doesn't reflect whether ChangeCipherSpec has been received in
2639 * this handshake, but s->s3->change_cipher_spec does (will be reset by
2640 * ssl3_get_finished).
2641 *
2642 * TODO(davidben): Is this check now redundant with SSL3_FLAGS_EXPECT_CCS? */
2643 if (!s->s3->change_cipher_spec) {
2644 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id,
2645 SSL_R_GOT_CHANNEL_ID_BEFORE_A_CCS);
2646 return -1;
2647 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002648
Adam Langleyfcf25832014-12-18 17:42:32 -08002649 CBS_init(&encrypted_extensions, s->init_msg, n);
Adam Langley1258b6a2014-06-20 12:00:00 -07002650
Adam Langleyfcf25832014-12-18 17:42:32 -08002651 /* EncryptedExtensions could include multiple extensions, but the only
2652 * extension that could be negotiated is ChannelID, so there can only be one
2653 * entry.
2654 *
2655 * The payload looks like:
2656 * uint16 extension_type
2657 * uint16 extension_len;
2658 * uint8 x[32];
2659 * uint8 y[32];
2660 * uint8 r[32];
2661 * uint8 s[32]; */
2662 expected_extension_type = TLSEXT_TYPE_channel_id;
2663 if (s->s3->tlsext_channel_id_new) {
2664 expected_extension_type = TLSEXT_TYPE_channel_id_new;
2665 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002666
Adam Langleyfcf25832014-12-18 17:42:32 -08002667 if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
2668 !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
2669 CBS_len(&encrypted_extensions) != 0 ||
2670 extension_type != expected_extension_type ||
2671 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
2672 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_INVALID_MESSAGE);
2673 return -1;
2674 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002675
Adam Langleyfcf25832014-12-18 17:42:32 -08002676 p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
2677 if (!p256) {
2678 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_NO_P256_SUPPORT);
2679 return -1;
2680 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002681
Adam Langleyfcf25832014-12-18 17:42:32 -08002682 BN_init(&x);
2683 BN_init(&y);
2684 sig.r = BN_new();
2685 sig.s = BN_new();
Adam Langley1258b6a2014-06-20 12:00:00 -07002686
Adam Langleyfcf25832014-12-18 17:42:32 -08002687 p = CBS_data(&extension);
2688 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
2689 BN_bin2bn(p + 32, 32, &y) == NULL ||
2690 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
2691 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
2692 goto err;
2693 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002694
Adam Langleyfcf25832014-12-18 17:42:32 -08002695 point = EC_POINT_new(p256);
2696 if (!point || !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
2697 goto err;
2698 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002699
Adam Langleyfcf25832014-12-18 17:42:32 -08002700 key = EC_KEY_new();
2701 if (!key || !EC_KEY_set_group(key, p256) ||
2702 !EC_KEY_set_public_key(key, point)) {
2703 goto err;
2704 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002705
Adam Langleyfcf25832014-12-18 17:42:32 -08002706 /* We stored the handshake hash in |tlsext_channel_id| the first time that we
2707 * were called. */
2708 if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) {
2709 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id,
2710 SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
2711 s->s3->tlsext_channel_id_valid = 0;
2712 goto err;
2713 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002714
Adam Langleyfcf25832014-12-18 17:42:32 -08002715 memcpy(s->s3->tlsext_channel_id, p, 64);
2716 ret = 1;
Adam Langley1258b6a2014-06-20 12:00:00 -07002717
2718err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002719 BN_free(&x);
2720 BN_free(&y);
2721 BN_free(sig.r);
2722 BN_free(sig.s);
2723 if (key) {
2724 EC_KEY_free(key);
2725 }
2726 if (point) {
2727 EC_POINT_free(point);
2728 }
2729 if (p256) {
2730 EC_GROUP_free(p256);
2731 }
2732 return ret;
2733}