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