2014-06-20 20:00:00 +01:00
|
|
|
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This package is an SSL implementation written
|
|
|
|
* by Eric Young (eay@cryptsoft.com).
|
|
|
|
* The implementation was written so as to conform with Netscapes SSL.
|
|
|
|
*
|
|
|
|
* This library is free for commercial and non-commercial use as long as
|
|
|
|
* the following conditions are aheared to. The following conditions
|
|
|
|
* apply to all code found in this distribution, be it the RC4, RSA,
|
|
|
|
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
|
|
|
* included with this distribution is covered by the same copyright terms
|
|
|
|
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
|
|
|
*
|
|
|
|
* Copyright remains Eric Young's, and as such any Copyright notices in
|
|
|
|
* the code are not to be removed.
|
|
|
|
* If this package is used in a product, Eric Young should be given attribution
|
|
|
|
* as the author of the parts of the library used.
|
|
|
|
* This can be in the form of a textual message at program startup or
|
|
|
|
* in documentation (online or textual) provided with the package.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* "This product includes cryptographic software written by
|
|
|
|
* Eric Young (eay@cryptsoft.com)"
|
|
|
|
* The word 'cryptographic' can be left out if the rouines from the library
|
|
|
|
* being used are not cryptographic related :-).
|
|
|
|
* 4. If you include any Windows specific code (or a derivative thereof) from
|
|
|
|
* the apps directory (application code) you must include an acknowledgement:
|
|
|
|
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* The licence and distribution terms for any publically available version or
|
|
|
|
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
|
|
|
* copied and put under another distribution licence
|
|
|
|
* [including the GNU Public Licence.]
|
|
|
|
*/
|
|
|
|
/* ====================================================================
|
|
|
|
* Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. All advertising materials mentioning features or use of this
|
|
|
|
* software must display the following acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
|
|
|
|
*
|
|
|
|
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* prior written permission. For written permission, please contact
|
|
|
|
* openssl-core@openssl.org.
|
|
|
|
*
|
|
|
|
* 5. Products derived from this software may not be called "OpenSSL"
|
|
|
|
* nor may "OpenSSL" appear in their names without prior written
|
|
|
|
* permission of the OpenSSL Project.
|
|
|
|
*
|
|
|
|
* 6. Redistributions of any form whatsoever must retain the following
|
|
|
|
* acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
|
|
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
|
|
|
*
|
|
|
|
* This product includes cryptographic software written by Eric Young
|
|
|
|
* (eay@cryptsoft.com). This product includes software written by Tim
|
|
|
|
* Hudson (tjh@cryptsoft.com).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/* ====================================================================
|
|
|
|
* Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
|
|
|
|
*
|
|
|
|
* Portions of the attached software ("Contribution") are developed by
|
|
|
|
* SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
|
|
|
|
*
|
|
|
|
* The Contribution is licensed pursuant to the OpenSSL open source
|
|
|
|
* license provided above.
|
|
|
|
*
|
|
|
|
* ECC cipher suite support in OpenSSL originally written by
|
|
|
|
* Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/* ====================================================================
|
|
|
|
* Copyright 2005 Nokia. All rights reserved.
|
|
|
|
*
|
|
|
|
* The portions of the attached software ("Contribution") is developed by
|
|
|
|
* Nokia Corporation and is licensed pursuant to the OpenSSL open source
|
|
|
|
* license.
|
|
|
|
*
|
|
|
|
* The Contribution, originally written by Mika Kousa and Pasi Eronen of
|
|
|
|
* Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
|
|
|
|
* support (see RFC 4279) to OpenSSL.
|
|
|
|
*
|
|
|
|
* No patent licenses or other rights except those expressly stated in
|
|
|
|
* the OpenSSL open source license shall be deemed granted or received
|
|
|
|
* expressly, by implication, estoppel, or otherwise.
|
|
|
|
*
|
|
|
|
* No assurances are provided by Nokia that the Contribution does not
|
|
|
|
* infringe the patent or other intellectual property rights of any third
|
|
|
|
* party or that the license provides you with all the necessary rights
|
|
|
|
* to make use of the Contribution.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
|
|
|
|
* ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
|
|
|
|
* SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
|
|
|
|
* OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
|
|
|
|
* OTHERWISE.
|
|
|
|
*/
|
|
|
|
|
2015-09-15 06:48:04 +01:00
|
|
|
#include <openssl/ssl.h>
|
|
|
|
|
Delay creating s->session until resumption is resolved.
When not offering to resume a session, the client populates s->session with a
fresh SSL_SESSION before the ServerHello is processed and, in DTLS_ANY_VERSION,
before the version is even determined. Don't create a fresh SSL_SESSION until
we know we are doing a full handshake.
This brings ssl3_send_client_hello closer to ssl23_client_hello in behavior. It
also fixes ssl_version in the client in DTLS_ANY_VERSION.
SSLv23_client_method is largely unchanged. If no session is offered, s->session
continues to be NULL until the ServerHello is received. The one difference is
that s->session isn't populated until the entire ServerHello is received,
rather than just the first half, in the case of a fragmented ServerHello. Apart
from info_callback, no external hooks get called between those points, so this
shouldn't expose new missing NULL checks.
The other client methods change significantly to match SSLv23_client_method's
behavior. For TLS, any exposed missing NULL checks are also in
SSLv23_client_method (and version-specific methods are already weird), so that
should be safe. For DTLS, I've verified that accesses in d1_*.c either handle
NULL or are after the ServerHello.
Change-Id: Idcae6bd242480e28a57dbba76ce67f1ac1ae1d1d
Reviewed-on: https://boringssl-review.googlesource.com/2404
Reviewed-by: Adam Langley <agl@google.com>
2014-11-23 09:20:17 +00:00
|
|
|
#include <assert.h>
|
2015-04-08 04:05:04 +01:00
|
|
|
#include <string.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-10-31 23:20:42 +00:00
|
|
|
#include <openssl/aead.h>
|
2015-06-12 06:40:23 +01:00
|
|
|
#include <openssl/bn.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/buf.h>
|
2014-06-25 04:27:17 +01:00
|
|
|
#include <openssl/bytestring.h>
|
2015-06-12 06:40:23 +01:00
|
|
|
#include <openssl/ec_key.h>
|
|
|
|
#include <openssl/ecdsa.h>
|
2015-04-08 04:05:04 +01:00
|
|
|
#include <openssl/err.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/md5.h>
|
2015-06-12 06:40:23 +01:00
|
|
|
#include <openssl/mem.h>
|
|
|
|
#include <openssl/rand.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
#include "../crypto/internal.h"
|
2015-04-08 03:38:30 +01:00
|
|
|
#include "internal.h"
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_send_client_hello(SSL_HANDSHAKE *hs);
|
2017-06-16 03:43:04 +01:00
|
|
|
static int dtls1_get_hello_verify_request(SSL_HANDSHAKE *hs);
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_get_server_hello(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_get_cert_status(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_send_next_proto(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_send_channel_id(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs);
|
2016-06-07 21:40:46 +01:00
|
|
|
|
2016-11-14 01:45:16 +00:00
|
|
|
int ssl3_connect(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2014-12-18 03:06:57 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
assert(ssl->handshake_func == ssl3_connect);
|
|
|
|
assert(!ssl->server);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
|
|
|
for (;;) {
|
2017-02-11 04:41:14 +00:00
|
|
|
int state = hs->state;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-12-12 22:00:50 +00:00
|
|
|
switch (hs->state) {
|
2016-09-01 06:33:33 +01:00
|
|
|
case SSL_ST_INIT:
|
2016-06-02 01:16:03 +01:00
|
|
|
ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CW_CLNT_HELLO_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CW_CLNT_HELLO_A:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_send_client_hello(hs);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-06-07 21:40:46 +01:00
|
|
|
|
2016-08-02 21:22:34 +01:00
|
|
|
if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) {
|
2017-01-11 16:34:52 +00:00
|
|
|
if (hs->early_data_offered) {
|
|
|
|
if (!tls13_init_early_key_schedule(hs) ||
|
|
|
|
!tls13_advance_key_schedule(hs, ssl->session->master_key,
|
|
|
|
ssl->session->master_key_length) ||
|
|
|
|
!tls13_derive_early_secrets(hs) ||
|
|
|
|
!tls13_set_traffic_key(ssl, evp_aead_seal,
|
|
|
|
hs->early_traffic_secret,
|
|
|
|
hs->hash_len)) {
|
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
2017-03-09 19:56:07 +00:00
|
|
|
hs->next_state = SSL3_ST_WRITE_EARLY_DATA;
|
|
|
|
} else {
|
|
|
|
hs->next_state = SSL3_ST_CR_SRVR_HELLO_A;
|
2017-01-11 16:34:52 +00:00
|
|
|
}
|
2016-06-07 21:40:46 +01:00
|
|
|
} else {
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
|
2016-06-07 21:40:46 +01:00
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CW_FLUSH;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
2016-06-07 21:40:46 +01:00
|
|
|
case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
|
2016-08-02 21:22:34 +01:00
|
|
|
assert(SSL_is_dtls(ssl));
|
2017-06-16 03:43:04 +01:00
|
|
|
ret = dtls1_get_hello_verify_request(hs);
|
2016-06-07 21:40:46 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (ssl->d1->send_cookie) {
|
|
|
|
ssl->method->received_flight(ssl);
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CW_CLNT_HELLO_A;
|
2016-06-07 21:40:46 +01:00
|
|
|
} else {
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CR_SRVR_HELLO_A;
|
2016-06-07 21:40:46 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-03-09 19:56:07 +00:00
|
|
|
case SSL3_ST_WRITE_EARLY_DATA:
|
|
|
|
/* Stash the early data session, so connection properties may be queried
|
|
|
|
* out of it. */
|
|
|
|
hs->in_early_data = 1;
|
|
|
|
hs->early_session = ssl->session;
|
|
|
|
SSL_SESSION_up_ref(ssl->session);
|
|
|
|
|
|
|
|
hs->state = SSL3_ST_CR_SRVR_HELLO_A;
|
|
|
|
hs->can_early_write = 1;
|
|
|
|
ret = 1;
|
|
|
|
goto end;
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
case SSL3_ST_CR_SRVR_HELLO_A:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_get_server_hello(hs);
|
2016-12-12 22:00:50 +00:00
|
|
|
if (hs->state == SSL_ST_TLS13) {
|
2016-07-11 18:19:03 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
if (ssl->session != NULL) {
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CR_SESSION_TICKET_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
} else {
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CR_CERT_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CR_CERT_A:
|
2017-02-10 01:01:26 +00:00
|
|
|
if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_get_server_certificate(hs);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CR_CERT_STATUS_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
case SSL3_ST_CR_CERT_STATUS_A:
|
2016-11-14 01:45:16 +00:00
|
|
|
if (hs->certificate_status_expected) {
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_get_cert_status(hs);
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_VERIFY_SERVER_CERT;
|
2016-06-07 22:03:46 +01:00
|
|
|
break;
|
|
|
|
|
2015-08-18 12:21:54 +01:00
|
|
|
case SSL3_ST_VERIFY_SERVER_CERT:
|
2017-02-10 01:01:26 +00:00
|
|
|
if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_verify_server_cert(hs);
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2015-08-18 12:21:54 +01:00
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CR_KEY_EXCH_A;
|
2015-08-18 12:21:54 +01:00
|
|
|
break;
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
case SSL3_ST_CR_KEY_EXCH_A:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_get_server_key_exchange(hs);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CR_CERT_REQ_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CR_CERT_REQ_A:
|
2017-02-10 01:01:26 +00:00
|
|
|
if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_get_certificate_request(hs);
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CR_SRVR_DONE_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CR_SRVR_DONE_A:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_get_server_hello_done(hs);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-06-07 21:40:46 +01:00
|
|
|
ssl->method->received_flight(ssl);
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CW_CERT_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CW_CERT_A:
|
2016-11-14 01:45:16 +00:00
|
|
|
if (hs->cert_request) {
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_send_client_certificate(hs);
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CW_KEY_EXCH_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CW_KEY_EXCH_A:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_send_client_key_exchange(hs);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CW_CERT_VRFY_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CW_CERT_VRFY_A:
|
|
|
|
case SSL3_ST_CW_CERT_VRFY_B:
|
2017-01-02 13:15:35 +00:00
|
|
|
if (hs->cert_request && ssl_has_certificate(ssl)) {
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_send_cert_verify(hs);
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CW_CHANGE;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
2016-06-28 16:22:02 +01:00
|
|
|
case SSL3_ST_CW_CHANGE:
|
Don't use the buffer BIO in TLS.
On the TLS side, we introduce a running buffer of ciphertext. Queuing up
pending data consists of encrypting the record into the buffer. This
effectively reimplements what the buffer BIO was doing previously, but
this resizes to fit the whole flight.
As part of this, rename all the functions to add to the pending flight
to be more uniform. This CL proposes "add_foo" to add to the pending
flight and "flush_flight" to drain it.
We add an add_alert hook for alerts but, for now, only the SSL 3.0
warning alert (sent mid-handshake) uses this mechanism. Later work will
push this down to the rest of the write path so closure alerts use it
too, as in DTLS. The intended end state is that all the ssl_buffer.c and
wpend_ret logic will only be used for application data and eventually
optionally replaced by the in-place API, while all "incidental" data
will be handled internally.
For now, the two buffers are mutually exclusive. Moving closure alerts
to "incidentals" will change this, but flushing application data early
is tricky due to wpend_ret. (If we call ssl_write_buffer_flush,
do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That
too is all left alone in this change.
To keep the diff down, write_message is retained for now and will be
removed from the state machines in a follow-up change.
BUG=72
Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d
Reviewed-on: https://boringssl-review.googlesource.com/13224
Reviewed-by: Adam Langley <agl@google.com>
2017-01-03 23:37:41 +00:00
|
|
|
if (!ssl->method->add_change_cipher_spec(ssl) ||
|
|
|
|
!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
|
2014-12-18 03:06:57 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
Don't use the buffer BIO in TLS.
On the TLS side, we introduce a running buffer of ciphertext. Queuing up
pending data consists of encrypting the record into the buffer. This
effectively reimplements what the buffer BIO was doing previously, but
this resizes to fit the whole flight.
As part of this, rename all the functions to add to the pending flight
to be more uniform. This CL proposes "add_foo" to add to the pending
flight and "flush_flight" to drain it.
We add an add_alert hook for alerts but, for now, only the SSL 3.0
warning alert (sent mid-handshake) uses this mechanism. Later work will
push this down to the rest of the write path so closure alerts use it
too, as in DTLS. The intended end state is that all the ssl_buffer.c and
wpend_ret logic will only be used for application data and eventually
optionally replaced by the in-place API, while all "incidental" data
will be handled internally.
For now, the two buffers are mutually exclusive. Moving closure alerts
to "incidentals" will change this, but flushing application data early
is tricky due to wpend_ret. (If we call ssl_write_buffer_flush,
do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That
too is all left alone in this change.
To keep the diff down, write_message is retained for now and will be
removed from the state machines in a follow-up change.
BUG=72
Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d
Reviewed-on: https://boringssl-review.googlesource.com/13224
Reviewed-by: Adam Langley <agl@google.com>
2017-01-03 23:37:41 +00:00
|
|
|
hs->state = SSL3_ST_CW_NEXT_PROTO_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CW_NEXT_PROTO_A:
|
2016-11-14 01:45:16 +00:00
|
|
|
if (hs->next_proto_neg_seen) {
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_send_next_proto(hs);
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CW_CHANNEL_ID_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CW_CHANNEL_ID_A:
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ssl->s3->tlsext_channel_id_valid) {
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_send_channel_id(hs);
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CW_FINISHED_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CW_FINISHED_A:
|
2017-01-13 01:02:05 +00:00
|
|
|
ret = ssl3_send_finished(hs);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CW_FLUSH;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
if (ssl->session != NULL) {
|
2017-02-11 04:33:27 +00:00
|
|
|
hs->next_state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
|
2014-12-18 03:06:57 +00:00
|
|
|
} else {
|
|
|
|
/* This is a non-resumption handshake. If it involves ChannelID, then
|
|
|
|
* record the handshake hashes at this point in the session so that
|
|
|
|
* any resumption of this session with ChannelID can sign those
|
|
|
|
* hashes. */
|
2017-01-12 18:17:07 +00:00
|
|
|
ret = tls1_record_handshake_hashes_for_channel_id(hs);
|
2015-07-10 22:33:46 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
|
|
|
|
ssl3_can_false_start(ssl) &&
|
2015-02-17 00:16:46 +00:00
|
|
|
/* No False Start on renegotiation (would complicate the state
|
|
|
|
* machine). */
|
2015-12-19 22:05:56 +00:00
|
|
|
!ssl->s3->initial_handshake_complete) {
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->next_state = SSL3_ST_FALSE_START;
|
2014-12-18 03:06:57 +00:00
|
|
|
} else {
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->next_state = SSL3_ST_CR_SESSION_TICKET_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
case SSL3_ST_FALSE_START:
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CR_SESSION_TICKET_A;
|
2016-11-14 01:45:16 +00:00
|
|
|
hs->in_false_start = 1;
|
2016-12-19 18:19:29 +00:00
|
|
|
hs->can_early_write = 1;
|
2016-06-07 22:03:46 +01:00
|
|
|
ret = 1;
|
|
|
|
goto end;
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
case SSL3_ST_CR_SESSION_TICKET_A:
|
2016-11-14 01:45:16 +00:00
|
|
|
if (hs->ticket_expected) {
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_get_new_session_ticket(hs);
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CR_CHANGE;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CR_CHANGE:
|
2016-06-15 22:44:37 +01:00
|
|
|
ret = ssl->method->read_change_cipher_spec(ssl);
|
2015-11-26 07:16:49 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2016-11-17 08:03:59 +00:00
|
|
|
if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
|
2014-12-18 03:06:57 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CR_FINISHED_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CR_FINISHED_A:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_get_finished(hs);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-06-07 21:40:46 +01:00
|
|
|
ssl->method->received_flight(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
if (ssl->session != NULL) {
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CW_CHANGE;
|
2014-12-18 03:06:57 +00:00
|
|
|
} else {
|
2017-02-11 04:33:27 +00:00
|
|
|
hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CW_FLUSH:
|
2017-01-01 22:41:30 +00:00
|
|
|
ret = ssl->method->flush_flight(ssl);
|
|
|
|
if (ret <= 0) {
|
2014-12-18 03:06:57 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = hs->next_state;
|
2017-02-11 04:33:27 +00:00
|
|
|
if (hs->state != SSL3_ST_FINISH_CLIENT_HANDSHAKE) {
|
2016-06-07 21:40:46 +01:00
|
|
|
ssl->method->expect_flight(ssl);
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
2016-12-19 18:19:29 +00:00
|
|
|
case SSL_ST_TLS13: {
|
|
|
|
int early_return = 0;
|
|
|
|
ret = tls13_handshake(hs, &early_return);
|
2016-07-11 18:19:03 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-12-19 18:19:29 +00:00
|
|
|
|
|
|
|
if (early_return) {
|
|
|
|
ret = 1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2017-02-11 04:33:27 +00:00
|
|
|
hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
|
2016-07-11 18:19:03 +01:00
|
|
|
break;
|
2016-12-19 18:19:29 +00:00
|
|
|
}
|
2016-07-11 18:19:03 +01:00
|
|
|
|
2017-02-11 04:33:27 +00:00
|
|
|
case SSL3_ST_FINISH_CLIENT_HANDSHAKE:
|
2016-07-27 22:51:49 +01:00
|
|
|
ssl->method->release_current_message(ssl, 1 /* free_buffer */);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
SSL_SESSION_free(ssl->s3->established_session);
|
|
|
|
if (ssl->session != NULL) {
|
2016-08-05 15:51:43 +01:00
|
|
|
SSL_SESSION_up_ref(ssl->session);
|
|
|
|
ssl->s3->established_session = ssl->session;
|
2016-06-27 21:34:59 +01:00
|
|
|
} else {
|
|
|
|
/* We make a copy of the session in order to maintain the immutability
|
|
|
|
* of the new established_session due to False Start. The caller may
|
|
|
|
* have taken a reference to the temporary session. */
|
|
|
|
ssl->s3->established_session =
|
2017-02-10 01:01:26 +00:00
|
|
|
SSL_SESSION_dup(hs->new_session, SSL_SESSION_DUP_ALL);
|
2016-06-27 21:34:59 +01:00
|
|
|
if (ssl->s3->established_session == NULL) {
|
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
ssl->s3->established_session->not_resumable = 0;
|
|
|
|
|
2017-02-10 01:01:26 +00:00
|
|
|
SSL_SESSION_free(hs->new_session);
|
|
|
|
hs->new_session = NULL;
|
2016-06-27 21:34:59 +01:00
|
|
|
}
|
|
|
|
|
2017-02-11 04:33:27 +00:00
|
|
|
hs->state = SSL_ST_OK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ST_OK: {
|
2015-12-19 22:05:56 +00:00
|
|
|
const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
|
|
|
|
ssl->s3->initial_handshake_complete = 1;
|
2015-06-18 23:36:15 +01:00
|
|
|
if (is_initial_handshake) {
|
|
|
|
/* Renegotiations do not participate in session resumption. */
|
2016-11-17 08:20:47 +00:00
|
|
|
ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
|
2015-06-18 23:36:15 +01:00
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-07 21:40:46 +01:00
|
|
|
ret = 1;
|
2016-06-02 01:16:03 +01:00
|
|
|
ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto end;
|
2017-02-11 04:33:27 +00:00
|
|
|
}
|
2016-06-27 21:34:59 +01:00
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
default:
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
|
2014-12-18 03:06:57 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2017-02-11 04:41:14 +00:00
|
|
|
if (hs->state != state) {
|
2016-06-02 01:16:03 +01:00
|
|
|
ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
end:
|
2016-06-02 01:16:03 +01:00
|
|
|
ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
|
2014-12-18 03:06:57 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-09-03 02:35:25 +01:00
|
|
|
uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index) {
|
|
|
|
/* Use the client_random for entropy. This both avoids calling |RAND_bytes| on
|
|
|
|
* a single byte repeatedly and ensures the values are deterministic. This
|
|
|
|
* allows the same ClientHello be sent twice for a HelloRetryRequest or the
|
|
|
|
* same group be advertised in both supported_groups and key_shares. */
|
|
|
|
uint16_t ret = ssl->s3->client_random[index];
|
|
|
|
/* This generates a random value of the form 0xωaωa, for all 0 ≤ ω < 16. */
|
|
|
|
ret = (ret & 0xf0) | 0x0a;
|
|
|
|
ret |= ret << 8;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-01-25 07:18:43 +00:00
|
|
|
/* ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
|
|
|
|
* disabled algorithms. */
|
|
|
|
static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a,
|
|
|
|
uint32_t *out_mask_k) {
|
|
|
|
*out_mask_a = 0;
|
|
|
|
*out_mask_k = 0;
|
|
|
|
|
|
|
|
/* PSK requires a client callback. */
|
|
|
|
if (ssl->psk_client_callback == NULL) {
|
|
|
|
*out_mask_a |= SSL_aPSK;
|
|
|
|
*out_mask_k |= SSL_kPSK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 11:55:28 +01:00
|
|
|
static int ssl_write_client_cipher_list(SSL *ssl, CBB *out,
|
|
|
|
uint16_t min_version,
|
2016-09-19 23:40:03 +01:00
|
|
|
uint16_t max_version) {
|
2017-01-25 07:18:43 +00:00
|
|
|
uint32_t mask_a, mask_k;
|
|
|
|
ssl_get_client_disabled(ssl, &mask_a, &mask_k);
|
2015-10-10 19:13:23 +01:00
|
|
|
|
|
|
|
CBB child;
|
|
|
|
if (!CBB_add_u16_length_prefixed(out, &child)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-09-03 02:35:25 +01:00
|
|
|
/* Add a fake cipher suite. See draft-davidben-tls-grease-01. */
|
|
|
|
if (ssl->ctx->grease_enabled &&
|
|
|
|
!CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-31 23:20:42 +00:00
|
|
|
/* Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
|
|
|
|
* hardware support. */
|
|
|
|
if (max_version >= TLS1_3_VERSION) {
|
|
|
|
if (!EVP_has_aes_hardware() &&
|
|
|
|
!CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
|
|
|
|
return 0;
|
2015-11-05 23:28:33 +00:00
|
|
|
}
|
2016-10-31 23:20:42 +00:00
|
|
|
if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
|
|
|
|
!CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
|
|
|
|
return 0;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2016-10-31 23:20:42 +00:00
|
|
|
if (EVP_has_aes_hardware() &&
|
|
|
|
!CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
|
2015-10-10 19:13:23 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-10-31 23:20:42 +00:00
|
|
|
if (min_version < TLS1_3_VERSION) {
|
|
|
|
STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
|
|
|
|
int any_enabled = 0;
|
|
|
|
for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
|
|
|
|
const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
|
|
|
|
/* Skip disabled ciphers */
|
2017-01-25 07:18:43 +00:00
|
|
|
if ((cipher->algorithm_mkey & mask_k) ||
|
|
|
|
(cipher->algorithm_auth & mask_a)) {
|
2016-10-31 23:20:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (SSL_CIPHER_get_min_version(cipher) > max_version ||
|
|
|
|
SSL_CIPHER_get_max_version(cipher) < min_version) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
any_enabled = 1;
|
|
|
|
if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If all ciphers were disabled, return the error to the caller. */
|
|
|
|
if (!any_enabled && max_version < TLS1_3_VERSION) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-10 19:13:23 +01:00
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-10-10 19:13:23 +01:00
|
|
|
/* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
|
|
|
|
* added. */
|
2016-12-06 18:35:25 +00:00
|
|
|
if (max_version == SSL3_VERSION &&
|
2015-10-10 19:13:23 +01:00
|
|
|
!ssl->s3->initial_handshake_complete) {
|
|
|
|
if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
|
|
|
|
return 0;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-10-10 19:13:23 +01:00
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-09-19 23:40:03 +01:00
|
|
|
if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
|
2016-07-21 11:55:28 +01:00
|
|
|
if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-10 19:13:23 +01:00
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-10-10 19:13:23 +01:00
|
|
|
return CBB_flush(out);
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-12-03 07:20:34 +00:00
|
|
|
int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2016-09-19 23:40:03 +01:00
|
|
|
uint16_t min_version, max_version;
|
|
|
|
if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
|
2016-07-18 17:40:30 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-01 17:39:36 +00:00
|
|
|
CBB cbb, body;
|
|
|
|
if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-07-18 17:40:30 +01:00
|
|
|
/* Renegotiations do not participate in session resumption. */
|
|
|
|
int has_session = ssl->session != NULL &&
|
|
|
|
!ssl->s3->initial_handshake_complete;
|
|
|
|
|
|
|
|
CBB child;
|
2016-12-06 18:35:25 +00:00
|
|
|
if (!CBB_add_u16(&body, hs->client_version) ||
|
2016-11-01 17:39:36 +00:00
|
|
|
!CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
|
|
|
|
!CBB_add_u8_length_prefixed(&body, &child) ||
|
2016-07-18 17:40:30 +01:00
|
|
|
(has_session &&
|
|
|
|
!CBB_add_bytes(&child, ssl->session->session_id,
|
|
|
|
ssl->session->session_id_length))) {
|
2016-11-01 17:39:36 +00:00
|
|
|
goto err;
|
2016-07-18 17:40:30 +01:00
|
|
|
}
|
|
|
|
|
2016-08-02 21:22:34 +01:00
|
|
|
if (SSL_is_dtls(ssl)) {
|
2016-11-01 17:39:36 +00:00
|
|
|
if (!CBB_add_u8_length_prefixed(&body, &child) ||
|
2016-07-18 17:40:30 +01:00
|
|
|
!CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
|
2016-11-01 17:39:36 +00:00
|
|
|
goto err;
|
2016-07-18 17:40:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t header_len =
|
2016-08-02 21:22:34 +01:00
|
|
|
SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
|
2016-11-01 17:39:36 +00:00
|
|
|
if (!ssl_write_client_cipher_list(ssl, &body, min_version, max_version) ||
|
|
|
|
!CBB_add_u8(&body, 1 /* one compression method */) ||
|
|
|
|
!CBB_add_u8(&body, 0 /* null compression */) ||
|
2016-12-03 07:20:34 +00:00
|
|
|
!ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
|
2016-11-01 17:39:36 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *msg = NULL;
|
|
|
|
size_t len;
|
|
|
|
if (!ssl->method->finish_message(ssl, &cbb, &msg, &len)) {
|
|
|
|
goto err;
|
2016-07-18 17:40:30 +01:00
|
|
|
}
|
|
|
|
|
2016-11-01 17:39:36 +00:00
|
|
|
/* Now that the length prefixes have been computed, fill in the placeholder
|
|
|
|
* PSK binder. */
|
2016-12-03 07:20:34 +00:00
|
|
|
if (hs->needs_psk_binder &&
|
2017-01-12 18:17:07 +00:00
|
|
|
!tls13_write_psk_binder(hs, msg, len)) {
|
2016-11-01 17:39:36 +00:00
|
|
|
OPENSSL_free(msg);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
Don't use the buffer BIO in TLS.
On the TLS side, we introduce a running buffer of ciphertext. Queuing up
pending data consists of encrypting the record into the buffer. This
effectively reimplements what the buffer BIO was doing previously, but
this resizes to fit the whole flight.
As part of this, rename all the functions to add to the pending flight
to be more uniform. This CL proposes "add_foo" to add to the pending
flight and "flush_flight" to drain it.
We add an add_alert hook for alerts but, for now, only the SSL 3.0
warning alert (sent mid-handshake) uses this mechanism. Later work will
push this down to the rest of the write path so closure alerts use it
too, as in DTLS. The intended end state is that all the ssl_buffer.c and
wpend_ret logic will only be used for application data and eventually
optionally replaced by the in-place API, while all "incidental" data
will be handled internally.
For now, the two buffers are mutually exclusive. Moving closure alerts
to "incidentals" will change this, but flushing application data early
is tricky due to wpend_ret. (If we call ssl_write_buffer_flush,
do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That
too is all left alone in this change.
To keep the diff down, write_message is retained for now and will be
removed from the state machines in a follow-up change.
BUG=72
Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d
Reviewed-on: https://boringssl-review.googlesource.com/13224
Reviewed-by: Adam Langley <agl@google.com>
2017-01-03 23:37:41 +00:00
|
|
|
return ssl->method->add_message(ssl, msg, len);
|
2016-11-01 17:39:36 +00:00
|
|
|
|
|
|
|
err:
|
|
|
|
CBB_cleanup(&cbb);
|
|
|
|
return 0;
|
2016-07-18 17:40:30 +01:00
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_send_client_hello(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2016-06-07 21:40:46 +01:00
|
|
|
/* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we
|
|
|
|
* may send multiple ClientHellos if we receive HelloVerifyRequest. */
|
2017-01-12 18:17:07 +00:00
|
|
|
if (!SSL_TRANSCRIPT_init(&hs->transcript)) {
|
2015-11-02 22:16:13 +00:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-06-21 15:33:21 +01:00
|
|
|
uint16_t min_version, max_version;
|
|
|
|
if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
|
2016-11-01 17:39:36 +00:00
|
|
|
return -1;
|
2016-06-21 15:33:21 +01:00
|
|
|
}
|
|
|
|
|
2016-12-06 18:35:25 +00:00
|
|
|
uint16_t max_wire_version = ssl->method->version_to_wire(max_version);
|
2016-12-12 22:00:50 +00:00
|
|
|
assert(hs->state == SSL3_ST_CW_CLNT_HELLO_A);
|
2015-10-10 19:13:23 +01:00
|
|
|
if (!ssl->s3->have_version) {
|
2016-12-06 18:35:25 +00:00
|
|
|
ssl->version = max_wire_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Always advertise the ClientHello version from the original maximum version,
|
|
|
|
* even on renegotiation. The static RSA key exchange uses this field, and
|
|
|
|
* some servers fail when it changes across handshakes. */
|
|
|
|
hs->client_version = max_wire_version;
|
|
|
|
if (max_version >= TLS1_3_VERSION) {
|
|
|
|
hs->client_version = ssl->method->version_to_wire(TLS1_2_VERSION);
|
2015-10-10 19:13:23 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 20:10:44 +00:00
|
|
|
/* If the configured session has expired or was created at a disabled
|
|
|
|
* version, drop it. */
|
2016-06-21 15:33:21 +01:00
|
|
|
if (ssl->session != NULL) {
|
2016-09-20 00:57:37 +01:00
|
|
|
uint16_t session_version;
|
2016-12-12 18:51:00 +00:00
|
|
|
if (ssl->session->is_server ||
|
|
|
|
!ssl->method->version_from_wire(&session_version,
|
2016-09-20 00:57:37 +01:00
|
|
|
ssl->session->ssl_version) ||
|
|
|
|
(session_version < TLS1_3_VERSION &&
|
2016-07-29 19:32:55 +01:00
|
|
|
ssl->session->session_id_length == 0) ||
|
|
|
|
ssl->session->not_resumable ||
|
|
|
|
!ssl_session_is_time_valid(ssl, ssl->session) ||
|
2016-06-21 15:33:21 +01:00
|
|
|
session_version < min_version || session_version > max_version) {
|
2016-09-01 06:10:07 +01:00
|
|
|
ssl_set_session(ssl, NULL);
|
2016-06-21 15:33:21 +01:00
|
|
|
}
|
2015-10-10 19:13:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
|
|
|
|
* renegerate the client_random. The random must be reused. */
|
2016-08-02 21:22:34 +01:00
|
|
|
if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) &&
|
2016-07-10 17:20:35 +01:00
|
|
|
!RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
|
2016-11-01 17:39:36 +00:00
|
|
|
return -1;
|
2015-10-10 19:13:23 +01:00
|
|
|
}
|
|
|
|
|
2016-12-03 07:20:34 +00:00
|
|
|
if (!ssl_write_client_hello(hs)) {
|
2016-11-01 17:39:36 +00:00
|
|
|
return -1;
|
2015-10-10 19:13:23 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 01:02:05 +00:00
|
|
|
return 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-16 03:43:04 +01:00
|
|
|
static int dtls1_get_hello_verify_request(SSL_HANDSHAKE *hs) {
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
2016-06-07 21:40:46 +01:00
|
|
|
CBS hello_verify_request, cookie;
|
|
|
|
uint16_t server_version;
|
|
|
|
|
2017-01-21 19:49:39 +00:00
|
|
|
int ret = ssl->method->ssl_get_message(ssl);
|
2016-07-08 22:32:11 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
2016-06-07 21:40:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
|
|
|
|
ssl->d1->send_cookie = 0;
|
|
|
|
ssl->s3->tmp.reuse_message = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-21 19:49:39 +00:00
|
|
|
CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num);
|
2016-06-07 21:40:46 +01:00
|
|
|
if (!CBS_get_u16(&hello_verify_request, &server_version) ||
|
|
|
|
!CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
|
2017-06-16 03:43:04 +01:00
|
|
|
CBS_len(&cookie) > sizeof(ssl->d1->cookie) ||
|
2016-06-07 21:40:46 +01:00
|
|
|
CBS_len(&hello_verify_request) != 0) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2017-06-16 03:43:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
return -1;
|
2016-06-07 21:40:46 +01:00
|
|
|
}
|
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
|
2016-06-07 21:40:46 +01:00
|
|
|
ssl->d1->cookie_len = CBS_len(&cookie);
|
|
|
|
|
|
|
|
ssl->d1->send_cookie = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_get_server_hello(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2014-12-18 03:06:57 +00:00
|
|
|
CBS server_hello, server_random, session_id;
|
2016-09-20 00:57:37 +01:00
|
|
|
uint16_t server_wire_version, cipher_suite;
|
2014-12-18 03:06:57 +00:00
|
|
|
uint8_t compression_method;
|
|
|
|
|
2017-01-21 19:49:39 +00:00
|
|
|
int ret = ssl->method->ssl_get_message(ssl);
|
2016-07-08 22:32:11 +01:00
|
|
|
if (ret <= 0) {
|
2015-01-06 17:03:19 +00:00
|
|
|
uint32_t err = ERR_peek_error();
|
|
|
|
if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
|
|
|
|
ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
|
|
|
|
/* Add a dedicated error code to the queue for a handshake_failure alert
|
|
|
|
* in response to ClientHello. This matches NSS's client behavior and
|
|
|
|
* gives a better error on a (probable) failure to negotiate initial
|
|
|
|
* parameters. Note: this error code comes after the original one.
|
|
|
|
*
|
|
|
|
* See https://crbug.com/446505. */
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
|
2015-01-06 17:03:19 +00:00
|
|
|
}
|
2016-07-08 22:32:11 +01:00
|
|
|
return ret;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 18:19:03 +01:00
|
|
|
if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO &&
|
|
|
|
ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-07-08 22:32:11 +01:00
|
|
|
CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-07-11 18:19:03 +01:00
|
|
|
if (!CBS_get_u16(&server_hello, &server_wire_version)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2017-04-08 16:05:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 00:57:37 +01:00
|
|
|
uint16_t min_version, max_version, server_version;
|
2016-09-19 23:40:03 +01:00
|
|
|
if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
|
2016-09-20 00:57:37 +01:00
|
|
|
!ssl->method->version_from_wire(&server_version, server_wire_version) ||
|
2016-07-10 17:20:35 +01:00
|
|
|
server_version < min_version || server_version > max_version) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
|
2017-04-08 16:05:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
|
|
|
|
return -1;
|
2016-07-10 17:20:35 +01:00
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
|
|
|
|
if (!ssl->s3->have_version) {
|
2016-06-21 15:33:21 +01:00
|
|
|
ssl->version = server_wire_version;
|
2015-12-19 22:05:56 +00:00
|
|
|
/* At this point, the connection's version is known and ssl->version is
|
2014-12-18 03:06:57 +00:00
|
|
|
* fixed. Begin enforcing the record-layer version. */
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->have_version = 1;
|
2016-06-21 15:33:21 +01:00
|
|
|
} else if (server_wire_version != ssl->version) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
|
2017-04-08 16:05:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
|
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 18:19:03 +01:00
|
|
|
if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL_ST_TLS13;
|
2016-11-14 01:32:04 +00:00
|
|
|
hs->do_tls13_handshake = tls13_client_handshake;
|
2016-07-11 18:19:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-11 16:34:52 +00:00
|
|
|
if (hs->early_data_offered) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
|
2017-04-08 16:05:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
|
|
|
|
return -1;
|
2017-01-11 16:34:52 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 07:43:08 +00:00
|
|
|
ssl_clear_tls13_state(hs);
|
2016-08-16 07:17:03 +01:00
|
|
|
|
2017-01-21 19:13:39 +00:00
|
|
|
if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO)) {
|
2016-07-11 18:19:03 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
|
|
|
|
!CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
|
|
|
|
CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
|
|
|
|
!CBS_get_u16(&server_hello, &cipher_suite) ||
|
|
|
|
!CBS_get_u8(&server_hello, &compression_method)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2017-04-08 16:05:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
return -1;
|
2016-07-11 18:19:03 +01:00
|
|
|
}
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
/* Copy over the server random. */
|
2017-04-08 16:05:04 +01:00
|
|
|
OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
|
|
|
|
SSL3_RANDOM_SIZE);
|
2015-12-19 22:05:56 +00:00
|
|
|
|
2016-08-12 03:01:18 +01:00
|
|
|
/* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
|
|
|
|
* 1.3 is finalized and we are not implementing a draft version. */
|
2016-07-10 17:20:35 +01:00
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
|
2016-07-29 19:32:55 +01:00
|
|
|
ssl->session->session_id_length != 0 &&
|
2015-12-19 22:05:56 +00:00
|
|
|
CBS_mem_equal(&session_id, ssl->session->session_id,
|
|
|
|
ssl->session->session_id_length)) {
|
2016-07-29 19:32:55 +01:00
|
|
|
ssl->s3->session_reused = 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
} else {
|
|
|
|
/* The session wasn't resumed. Create a fresh SSL_SESSION to
|
|
|
|
* fill out. */
|
2016-09-01 06:10:07 +01:00
|
|
|
ssl_set_session(ssl, NULL);
|
2016-11-17 08:20:47 +00:00
|
|
|
if (!ssl_get_new_session(hs, 0 /* client */)) {
|
2017-04-08 16:05:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
/* Note: session_id could be empty. */
|
2017-02-10 01:01:26 +00:00
|
|
|
hs->new_session->session_id_length = CBS_len(&session_id);
|
|
|
|
OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
|
|
|
|
CBS_len(&session_id));
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 21:49:09 +00:00
|
|
|
const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (c == NULL) {
|
|
|
|
/* unknown cipher */
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
|
2017-04-08 16:05:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2016-11-02 21:49:09 +00:00
|
|
|
|
|
|
|
/* The cipher must be allowed in the selected version and enabled. */
|
2017-01-25 07:18:43 +00:00
|
|
|
uint32_t mask_a, mask_k;
|
|
|
|
ssl_get_client_disabled(ssl, &mask_a, &mask_k);
|
|
|
|
if ((c->algorithm_mkey & mask_k) || (c->algorithm_auth & mask_a) ||
|
2016-06-15 02:14:35 +01:00
|
|
|
SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
|
2016-11-02 21:49:09 +00:00
|
|
|
SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl) ||
|
|
|
|
!sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, c)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
|
2017-04-08 16:05:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
if (ssl->session != NULL) {
|
2016-09-06 19:13:43 +01:00
|
|
|
if (ssl->session->ssl_version != ssl->version) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
|
2017-04-08 16:05:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
return -1;
|
2015-03-16 22:02:20 +00:00
|
|
|
}
|
2016-09-06 19:13:43 +01:00
|
|
|
if (ssl->session->cipher != c) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
|
2017-04-08 16:05:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
return -1;
|
2015-03-16 22:02:20 +00:00
|
|
|
}
|
2016-07-29 19:32:55 +01:00
|
|
|
if (!ssl_session_is_context_valid(ssl, ssl->session)) {
|
|
|
|
/* This is actually a client application bug. */
|
|
|
|
OPENSSL_PUT_ERROR(SSL,
|
|
|
|
SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
|
2017-04-08 16:05:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
return -1;
|
2016-07-29 19:32:55 +01:00
|
|
|
}
|
2015-12-25 20:40:14 +00:00
|
|
|
} else {
|
2017-02-10 01:01:26 +00:00
|
|
|
hs->new_session->cipher = c;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2017-02-10 01:01:26 +00:00
|
|
|
hs->new_cipher = c;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2017-01-21 19:49:39 +00:00
|
|
|
/* Now that the cipher is known, initialize the handshake hash and hash the
|
|
|
|
* ServerHello. */
|
2017-01-12 18:17:07 +00:00
|
|
|
if (!SSL_TRANSCRIPT_init_hash(&hs->transcript, ssl3_protocol_version(ssl),
|
|
|
|
c->algorithm_prf) ||
|
|
|
|
!ssl_hash_current_message(hs)) {
|
2017-04-08 16:05:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
|
|
|
return -1;
|
2015-08-05 13:50:34 +01:00
|
|
|
}
|
|
|
|
|
2016-06-30 18:27:23 +01:00
|
|
|
/* If doing a full handshake, the server may request a client certificate
|
|
|
|
* which requires hashing the handshake transcript. Otherwise, the handshake
|
|
|
|
* buffer may be released. */
|
2016-09-12 23:40:27 +01:00
|
|
|
if (ssl->session != NULL ||
|
2017-02-10 01:01:26 +00:00
|
|
|
!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
|
2017-01-12 18:17:07 +00:00
|
|
|
SSL_TRANSCRIPT_free_buffer(&hs->transcript);
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Only the NULL compression algorithm is supported. */
|
|
|
|
if (compression_method != 0) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
|
2017-04-08 16:05:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TLS extensions */
|
2016-12-03 07:20:34 +00:00
|
|
|
if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
|
2017-04-08 16:05:04 +01:00
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* There should be nothing left over in the record. */
|
|
|
|
if (CBS_len(&server_hello) != 0) {
|
|
|
|
/* wrong packet length */
|
2016-09-09 16:41:18 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2017-04-08 16:05:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
if (ssl->session != NULL &&
|
2017-02-17 21:26:01 +00:00
|
|
|
hs->extended_master_secret != ssl->session->extended_master_secret) {
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->session->extended_master_secret) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
|
2015-06-02 18:50:35 +01:00
|
|
|
} else {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
|
2015-06-02 18:50:35 +01:00
|
|
|
}
|
2017-04-08 16:05:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
|
|
|
|
return -1;
|
2015-06-02 18:50:35 +01:00
|
|
|
}
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2017-01-21 19:49:39 +00:00
|
|
|
int ret = ssl->method->ssl_get_message(ssl);
|
2016-07-14 04:03:26 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 19:49:39 +00:00
|
|
|
if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) ||
|
2017-01-12 18:17:07 +00:00
|
|
|
!ssl_hash_current_message(hs)) {
|
2017-01-21 19:13:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-07-14 04:03:26 +01:00
|
|
|
CBS cbs;
|
2016-07-08 22:32:11 +01:00
|
|
|
CBS_init(&cbs, ssl->init_msg, ssl->init_num);
|
2016-12-12 19:06:16 +00:00
|
|
|
|
2017-02-08 21:33:15 +00:00
|
|
|
uint8_t alert = SSL_AD_DECODE_ERROR;
|
2017-02-10 01:01:26 +00:00
|
|
|
sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
|
2016-12-12 19:37:43 +00:00
|
|
|
EVP_PKEY_free(hs->peer_pubkey);
|
|
|
|
hs->peer_pubkey = NULL;
|
2017-02-10 01:01:26 +00:00
|
|
|
hs->new_session->certs = ssl_parse_cert_chain(&alert, &hs->peer_pubkey, NULL,
|
|
|
|
&cbs, ssl->ctx->pool);
|
|
|
|
if (hs->new_session->certs == NULL) {
|
2016-07-14 04:03:26 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
|
2016-12-12 19:06:16 +00:00
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 01:01:26 +00:00
|
|
|
if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 ||
|
2016-12-12 19:06:16 +00:00
|
|
|
CBS_len(&cbs) != 0 ||
|
2017-02-10 01:01:26 +00:00
|
|
|
!ssl->ctx->x509_method->session_cache_objects(hs->new_session)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2016-07-14 04:03:26 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2016-12-12 19:06:16 +00:00
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 20:05:49 +00:00
|
|
|
if (!ssl_check_leaf_certificate(
|
2017-02-10 01:01:26 +00:00
|
|
|
hs, hs->peer_pubkey,
|
|
|
|
sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) {
|
2016-07-14 04:03:26 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
2016-12-12 19:06:16 +00:00
|
|
|
return -1;
|
2015-06-08 00:50:37 +01:00
|
|
|
}
|
|
|
|
|
2017-03-01 01:54:28 +00:00
|
|
|
/* Disallow the server certificate from changing during a renegotiation. See
|
|
|
|
* https://mitls.org/pages/attacks/3SHAKE. We never resume on renegotiation,
|
|
|
|
* so this check is sufficient. */
|
|
|
|
if (ssl->s3->established_session != NULL) {
|
|
|
|
if (sk_CRYPTO_BUFFER_num(ssl->s3->established_session->certs) !=
|
|
|
|
sk_CRYPTO_BUFFER_num(hs->new_session->certs)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(hs->new_session->certs); i++) {
|
|
|
|
const CRYPTO_BUFFER *old_cert =
|
|
|
|
sk_CRYPTO_BUFFER_value(ssl->s3->established_session->certs, i);
|
|
|
|
const CRYPTO_BUFFER *new_cert =
|
|
|
|
sk_CRYPTO_BUFFER_value(hs->new_session->certs, i);
|
|
|
|
if (CRYPTO_BUFFER_len(old_cert) != CRYPTO_BUFFER_len(new_cert) ||
|
|
|
|
OPENSSL_memcmp(CRYPTO_BUFFER_data(old_cert),
|
|
|
|
CRYPTO_BUFFER_data(new_cert),
|
|
|
|
CRYPTO_BUFFER_len(old_cert)) != 0) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 04:03:26 +01:00
|
|
|
return 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_get_cert_status(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2017-01-21 19:49:39 +00:00
|
|
|
int ret = ssl->method->ssl_get_message(ssl);
|
2016-07-08 22:32:11 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
|
|
|
|
/* A server may send status_request in ServerHello and then change
|
|
|
|
* its mind about sending CertificateStatus. */
|
|
|
|
ssl->s3->tmp.reuse_message = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-12 18:17:07 +00:00
|
|
|
if (!ssl_hash_current_message(hs)) {
|
2017-01-21 19:49:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-06-16 03:43:04 +01:00
|
|
|
CBS certificate_status, ocsp_response;
|
|
|
|
uint8_t status_type;
|
2016-07-08 22:32:11 +01:00
|
|
|
CBS_init(&certificate_status, ssl->init_msg, ssl->init_num);
|
2016-06-07 22:03:46 +01:00
|
|
|
if (!CBS_get_u8(&certificate_status, &status_type) ||
|
|
|
|
status_type != TLSEXT_STATUSTYPE_ocsp ||
|
|
|
|
!CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
|
|
|
|
CBS_len(&ocsp_response) == 0 ||
|
|
|
|
CBS_len(&certificate_status) != 0) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2017-06-16 03:43:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
return -1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 01:01:26 +00:00
|
|
|
if (!CBS_stow(&ocsp_response, &hs->new_session->ocsp_response,
|
|
|
|
&hs->new_session->ocsp_response_length)) {
|
2016-06-07 22:03:46 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
2017-06-16 03:43:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
|
|
|
return -1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2017-06-16 03:43:04 +01:00
|
|
|
return 1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2017-02-10 21:56:48 +00:00
|
|
|
if (!ssl->ctx->x509_method->session_verify_cert_chain(hs->new_session, ssl)) {
|
2016-08-02 01:11:13 +01:00
|
|
|
return -1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2016-07-18 18:17:16 +01:00
|
|
|
return 1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2014-12-18 03:06:57 +00:00
|
|
|
EC_KEY *ecdh = NULL;
|
|
|
|
EC_POINT *srvr_ecpoint = NULL;
|
|
|
|
|
2017-01-21 19:49:39 +00:00
|
|
|
int ret = ssl->method->ssl_get_message(ssl);
|
2016-07-08 22:32:11 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
|
2016-09-17 00:34:02 +01:00
|
|
|
/* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */
|
2017-02-10 01:01:26 +00:00
|
|
|
if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
|
2014-12-18 03:06:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->tmp.reuse_message = 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-12 18:17:07 +00:00
|
|
|
if (!ssl_hash_current_message(hs)) {
|
2017-01-21 19:49:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
/* Retain a copy of the original CBS to compute the signature over. */
|
2015-12-29 22:08:08 +00:00
|
|
|
CBS server_key_exchange;
|
2016-07-08 22:32:11 +01:00
|
|
|
CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num);
|
2015-12-29 22:08:08 +00:00
|
|
|
CBS server_key_exchange_orig = server_key_exchange;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2017-02-10 01:01:26 +00:00
|
|
|
uint32_t alg_k = hs->new_cipher->algorithm_mkey;
|
|
|
|
uint32_t alg_a = hs->new_cipher->algorithm_auth;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
|
|
|
if (alg_a & SSL_aPSK) {
|
|
|
|
CBS psk_identity_hint;
|
|
|
|
|
|
|
|
/* Each of the PSK key exchanges begins with a psk_identity_hint. */
|
|
|
|
if (!CBS_get_u16_length_prefixed(&server_key_exchange,
|
|
|
|
&psk_identity_hint)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2017-06-16 03:43:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Store PSK identity hint for later use, hint is used in
|
|
|
|
* ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
|
|
|
|
* identity hint can be as long as the maximum length of a PSK identity.
|
|
|
|
* Also do not allow NULL characters; identities are saved as C strings.
|
|
|
|
*
|
|
|
|
* TODO(davidben): Should invalid hints be ignored? It's a hint rather than
|
|
|
|
* a specific identity. */
|
|
|
|
if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
|
|
|
|
CBS_contains_zero_byte(&psk_identity_hint)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
|
2017-06-16 03:43:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
|
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 00:42:05 +01:00
|
|
|
/* Save non-empty identity hints as a C string. Empty identity hints we
|
|
|
|
* treat as missing. Plain PSK makes it possible to send either no hint
|
|
|
|
* (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
|
|
|
|
* empty hint. Having different capabilities is odd, so we interpret empty
|
|
|
|
* and missing as identical. */
|
|
|
|
if (CBS_len(&psk_identity_hint) != 0 &&
|
2016-11-14 01:32:04 +00:00
|
|
|
!CBS_strdup(&psk_identity_hint, &hs->peer_psk_identity_hint)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
2017-06-16 03:43:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 23:52:14 +01:00
|
|
|
if (alg_k & SSL_kECDHE) {
|
2015-12-19 05:18:25 +00:00
|
|
|
/* Parse the server parameters. */
|
2016-05-17 16:47:53 +01:00
|
|
|
uint8_t group_type;
|
|
|
|
uint16_t group_id;
|
2015-12-17 06:33:08 +00:00
|
|
|
CBS point;
|
2016-05-17 16:47:53 +01:00
|
|
|
if (!CBS_get_u8(&server_key_exchange, &group_type) ||
|
|
|
|
group_type != NAMED_CURVE_TYPE ||
|
2016-05-25 20:06:05 +01:00
|
|
|
!CBS_get_u16(&server_key_exchange, &group_id) ||
|
|
|
|
!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2017-06-16 03:43:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2017-02-10 01:01:26 +00:00
|
|
|
hs->new_session->group_id = group_id;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-05-17 16:47:53 +01:00
|
|
|
/* Ensure the group is consistent with preferences. */
|
|
|
|
if (!tls1_check_group_id(ssl, group_id)) {
|
2015-12-19 05:18:25 +00:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
|
2017-06-16 03:43:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-19 05:18:25 +00:00
|
|
|
|
2016-05-25 20:06:05 +01:00
|
|
|
/* Initialize ECDH and save the peer public key for later. */
|
2016-11-14 01:32:04 +00:00
|
|
|
if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
|
|
|
|
!CBS_stow(&point, &hs->peer_key, &hs->peer_key_len)) {
|
2016-05-17 00:31:05 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
} else if (!(alg_k & SSL_kPSK)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
|
2017-06-16 03:43:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
|
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* At this point, |server_key_exchange| contains the signature, if any, while
|
|
|
|
* |server_key_exchange_orig| contains the entire message. From that, derive
|
|
|
|
* a CBS containing just the parameter. */
|
2015-12-29 22:08:08 +00:00
|
|
|
CBS parameter;
|
2014-12-18 03:06:57 +00:00
|
|
|
CBS_init(¶meter, CBS_data(&server_key_exchange_orig),
|
|
|
|
CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
|
|
|
|
|
2015-06-07 16:00:53 +01:00
|
|
|
/* ServerKeyExchange should be signed by the server's public key. */
|
2017-02-10 01:01:26 +00:00
|
|
|
if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
|
2016-06-29 18:16:27 +01:00
|
|
|
uint16_t signature_algorithm = 0;
|
2015-12-30 05:08:49 +00:00
|
|
|
if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
|
2016-06-23 18:33:05 +01:00
|
|
|
if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
|
2015-11-02 17:02:20 +00:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2017-06-16 03:43:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2017-06-16 03:43:04 +01:00
|
|
|
uint8_t alert = SSL_AD_DECODE_ERROR;
|
|
|
|
if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
|
|
|
|
goto err;
|
2015-11-02 17:02:20 +00:00
|
|
|
}
|
2017-02-10 01:01:26 +00:00
|
|
|
hs->new_session->peer_signature_algorithm = signature_algorithm;
|
2017-04-20 22:49:36 +01:00
|
|
|
} else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
|
|
|
|
hs->peer_pubkey)) {
|
2016-07-14 05:11:26 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
|
2017-06-16 03:43:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
|
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The last field in |server_key_exchange| is the signature. */
|
2015-06-07 16:00:53 +01:00
|
|
|
CBS signature;
|
2014-12-18 03:06:57 +00:00
|
|
|
if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
|
|
|
|
CBS_len(&server_key_exchange) != 0) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2017-06-16 03:43:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-06-30 18:27:23 +01:00
|
|
|
CBB transcript;
|
|
|
|
uint8_t *transcript_data;
|
|
|
|
size_t transcript_len;
|
|
|
|
if (!CBB_init(&transcript, 2*SSL3_RANDOM_SIZE + CBS_len(¶meter)) ||
|
|
|
|
!CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
|
|
|
|
!CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
|
|
|
|
!CBB_add_bytes(&transcript, CBS_data(¶meter), CBS_len(¶meter)) ||
|
|
|
|
!CBB_finish(&transcript, &transcript_data, &transcript_len)) {
|
|
|
|
CBB_cleanup(&transcript);
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2017-06-16 03:43:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
|
|
|
goto err;
|
2016-06-29 18:16:27 +01:00
|
|
|
}
|
2016-06-30 18:27:23 +01:00
|
|
|
|
|
|
|
int sig_ok = ssl_public_key_verify(
|
|
|
|
ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
|
2016-12-12 19:37:43 +00:00
|
|
|
hs->peer_pubkey, transcript_data, transcript_len);
|
2016-06-30 18:27:23 +01:00
|
|
|
OPENSSL_free(transcript_data);
|
|
|
|
|
2016-03-02 03:57:40 +00:00
|
|
|
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
|
|
|
|
sig_ok = 1;
|
|
|
|
ERR_clear_error();
|
|
|
|
#endif
|
|
|
|
if (!sig_ok) {
|
2014-12-18 03:06:57 +00:00
|
|
|
/* bad signature */
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
|
2017-06-16 03:43:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
|
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-06-07 16:00:53 +01:00
|
|
|
/* PSK ciphers are the only supported certificate-less ciphers. */
|
|
|
|
assert(alg_a == SSL_aPSK);
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
if (CBS_len(&server_key_exchange) > 0) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
|
2017-06-16 03:43:04 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
err:
|
2014-12-18 03:06:57 +00:00
|
|
|
EC_POINT_free(srvr_ecpoint);
|
2015-04-22 21:17:58 +01:00
|
|
|
EC_KEY_free(ecdh);
|
2014-12-18 03:06:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2017-01-21 19:49:39 +00:00
|
|
|
int msg_ret = ssl->method->ssl_get_message(ssl);
|
2016-07-08 22:32:11 +01:00
|
|
|
if (msg_ret <= 0) {
|
|
|
|
return msg_ret;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:13:28 +01:00
|
|
|
if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->tmp.reuse_message = 1;
|
2015-08-05 13:50:34 +01:00
|
|
|
/* If we get here we don't need the handshake buffer as we won't be doing
|
|
|
|
* client auth. */
|
2017-01-12 18:17:07 +00:00
|
|
|
SSL_TRANSCRIPT_free_buffer(&hs->transcript);
|
2014-12-18 03:06:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-21 19:49:39 +00:00
|
|
|
if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_REQUEST) ||
|
2017-01-12 18:17:07 +00:00
|
|
|
!ssl_hash_current_message(hs)) {
|
2016-07-14 03:40:36 +01:00
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-03-12 00:38:17 +00:00
|
|
|
CBS cbs;
|
2016-07-08 22:32:11 +01:00
|
|
|
CBS_init(&cbs, ssl->init_msg, ssl->init_num);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-07-14 03:40:36 +01:00
|
|
|
/* Get the certificate types. */
|
2016-03-12 00:38:17 +00:00
|
|
|
CBS certificate_types;
|
2014-12-18 03:06:57 +00:00
|
|
|
if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2016-07-14 03:40:36 +01:00
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
if (!CBS_stow(&certificate_types, &hs->certificate_types,
|
|
|
|
&hs->num_certificate_types)) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
2016-07-14 03:40:36 +01:00
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 05:08:49 +00:00
|
|
|
if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
|
2014-12-18 03:06:57 +00:00
|
|
|
CBS supported_signature_algorithms;
|
2015-09-04 17:41:04 +01:00
|
|
|
if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
|
2016-11-17 08:20:47 +00:00
|
|
|
!tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2016-07-14 03:40:36 +01:00
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 21:33:15 +00:00
|
|
|
uint8_t alert = SSL_AD_DECODE_ERROR;
|
2017-02-02 18:57:17 +00:00
|
|
|
STACK_OF(CRYPTO_BUFFER) *ca_names =
|
|
|
|
ssl_parse_client_CA_list(ssl, &alert, &cbs);
|
|
|
|
if (ca_names == NULL) {
|
2016-07-14 03:40:36 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
|
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 16:41:18 +01:00
|
|
|
if (CBS_len(&cbs) != 0) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2017-02-02 18:57:17 +00:00
|
|
|
sk_CRYPTO_BUFFER_pop_free(ca_names, CRYPTO_BUFFER_free);
|
2016-09-09 16:41:18 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
hs->cert_request = 1;
|
2017-02-02 18:57:17 +00:00
|
|
|
sk_CRYPTO_BUFFER_pop_free(hs->ca_names, CRYPTO_BUFFER_free);
|
|
|
|
hs->ca_names = ca_names;
|
|
|
|
ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
|
2016-07-14 03:40:36 +01:00
|
|
|
return 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2017-01-21 19:49:39 +00:00
|
|
|
int ret = ssl->method->ssl_get_message(ssl);
|
2016-07-08 22:32:11 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 19:49:39 +00:00
|
|
|
if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO_DONE) ||
|
2017-01-12 18:17:07 +00:00
|
|
|
!ssl_hash_current_message(hs)) {
|
2017-01-21 19:13:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-07-08 22:32:11 +01:00
|
|
|
/* ServerHelloDone is empty. */
|
|
|
|
if (ssl->init_num > 0) {
|
2016-06-07 22:03:46 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2016-09-09 16:41:18 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2016-06-07 22:03:46 +01:00
|
|
|
return -1;
|
2015-10-23 22:41:12 +01:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2015-06-16 00:53:32 +01:00
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2016-12-10 20:46:58 +00:00
|
|
|
/* Call cert_cb to update the certificate. */
|
|
|
|
if (ssl->cert->cert_cb) {
|
|
|
|
int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
|
|
|
|
if (ret < 0) {
|
|
|
|
ssl->rwstate = SSL_X509_LOOKUP;
|
2016-06-07 22:03:46 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2016-12-10 20:46:58 +00:00
|
|
|
if (ret == 0) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
|
|
|
return -1;
|
2016-07-14 03:03:11 +01:00
|
|
|
}
|
2016-12-10 20:46:58 +00:00
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-12-10 20:46:58 +00:00
|
|
|
if (!ssl_has_certificate(ssl)) {
|
|
|
|
/* Without a client certificate, the handshake buffer may be released. */
|
2017-01-12 18:17:07 +00:00
|
|
|
SSL_TRANSCRIPT_free_buffer(&hs->transcript);
|
2016-12-10 20:46:58 +00:00
|
|
|
|
Don't use the buffer BIO in TLS.
On the TLS side, we introduce a running buffer of ciphertext. Queuing up
pending data consists of encrypting the record into the buffer. This
effectively reimplements what the buffer BIO was doing previously, but
this resizes to fit the whole flight.
As part of this, rename all the functions to add to the pending flight
to be more uniform. This CL proposes "add_foo" to add to the pending
flight and "flush_flight" to drain it.
We add an add_alert hook for alerts but, for now, only the SSL 3.0
warning alert (sent mid-handshake) uses this mechanism. Later work will
push this down to the rest of the write path so closure alerts use it
too, as in DTLS. The intended end state is that all the ssl_buffer.c and
wpend_ret logic will only be used for application data and eventually
optionally replaced by the in-place API, while all "incidental" data
will be handled internally.
For now, the two buffers are mutually exclusive. Moving closure alerts
to "incidentals" will change this, but flushing application data early
is tricky due to wpend_ret. (If we call ssl_write_buffer_flush,
do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That
too is all left alone in this change.
To keep the diff down, write_message is retained for now and will be
removed from the state machines in a follow-up change.
BUG=72
Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d
Reviewed-on: https://boringssl-review.googlesource.com/13224
Reviewed-by: Adam Langley <agl@google.com>
2017-01-03 23:37:41 +00:00
|
|
|
/* In SSL 3.0, the Certificate message is replaced with a warning alert. */
|
2016-12-10 20:46:58 +00:00
|
|
|
if (ssl->version == SSL3_VERSION) {
|
Don't use the buffer BIO in TLS.
On the TLS side, we introduce a running buffer of ciphertext. Queuing up
pending data consists of encrypting the record into the buffer. This
effectively reimplements what the buffer BIO was doing previously, but
this resizes to fit the whole flight.
As part of this, rename all the functions to add to the pending flight
to be more uniform. This CL proposes "add_foo" to add to the pending
flight and "flush_flight" to drain it.
We add an add_alert hook for alerts but, for now, only the SSL 3.0
warning alert (sent mid-handshake) uses this mechanism. Later work will
push this down to the rest of the write path so closure alerts use it
too, as in DTLS. The intended end state is that all the ssl_buffer.c and
wpend_ret logic will only be used for application data and eventually
optionally replaced by the in-place API, while all "incidental" data
will be handled internally.
For now, the two buffers are mutually exclusive. Moving closure alerts
to "incidentals" will change this, but flushing application data early
is tricky due to wpend_ret. (If we call ssl_write_buffer_flush,
do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That
too is all left alone in this change.
To keep the diff down, write_message is retained for now and will be
removed from the state machines in a follow-up change.
BUG=72
Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d
Reviewed-on: https://boringssl-review.googlesource.com/13224
Reviewed-by: Adam Langley <agl@google.com>
2017-01-03 23:37:41 +00:00
|
|
|
if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
|
|
|
|
SSL_AD_NO_CERTIFICATE)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-12-10 20:46:58 +00:00
|
|
|
return 1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 21:51:53 +01:00
|
|
|
if (!ssl_on_certificate_selected(hs) ||
|
2016-12-20 23:55:16 +00:00
|
|
|
!ssl3_output_cert_chain(ssl)) {
|
2016-12-10 20:46:58 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-01-13 01:02:05 +00:00
|
|
|
return 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2015-10-09 04:10:15 +01:00
|
|
|
OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned),
|
|
|
|
SIZE_T_IS_SMALLER_THAN_UNSIGNED);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2014-12-18 03:06:57 +00:00
|
|
|
uint8_t *pms = NULL;
|
|
|
|
size_t pms_len = 0;
|
2016-06-17 23:48:29 +01:00
|
|
|
CBB cbb, body;
|
|
|
|
if (!ssl->method->init_message(ssl, &cbb, &body,
|
|
|
|
SSL3_MT_CLIENT_KEY_EXCHANGE)) {
|
2015-12-17 07:01:25 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2017-02-10 01:01:26 +00:00
|
|
|
uint32_t alg_k = hs->new_cipher->algorithm_mkey;
|
|
|
|
uint32_t alg_a = hs->new_cipher->algorithm_auth;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-17 07:01:25 +00:00
|
|
|
/* If using a PSK key exchange, prepare the pre-shared key. */
|
|
|
|
unsigned psk_len = 0;
|
|
|
|
uint8_t psk[PSK_MAX_PSK_LEN];
|
|
|
|
if (alg_a & SSL_aPSK) {
|
|
|
|
if (ssl->psk_client_callback == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-17 07:01:25 +00:00
|
|
|
char identity[PSK_MAX_IDENTITY_LEN + 1];
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(identity, 0, sizeof(identity));
|
2016-11-14 01:32:04 +00:00
|
|
|
psk_len =
|
|
|
|
ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint, identity,
|
|
|
|
sizeof(identity), psk, sizeof(psk));
|
2015-12-17 07:01:25 +00:00
|
|
|
if (psk_len == 0) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
|
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-17 07:01:25 +00:00
|
|
|
assert(psk_len <= PSK_MAX_PSK_LEN);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2017-02-10 01:01:26 +00:00
|
|
|
OPENSSL_free(hs->new_session->psk_identity);
|
|
|
|
hs->new_session->psk_identity = BUF_strdup(identity);
|
|
|
|
if (hs->new_session->psk_identity == NULL) {
|
2015-12-17 07:01:25 +00:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-17 07:01:25 +00:00
|
|
|
/* Write out psk_identity. */
|
|
|
|
CBB child;
|
2016-06-17 23:48:29 +01:00
|
|
|
if (!CBB_add_u16_length_prefixed(&body, &child) ||
|
2015-12-17 07:01:25 +00:00
|
|
|
!CBB_add_bytes(&child, (const uint8_t *)identity,
|
|
|
|
OPENSSL_strnlen(identity, sizeof(identity))) ||
|
2016-06-17 23:48:29 +01:00
|
|
|
!CBB_flush(&body)) {
|
2015-12-17 07:01:25 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-17 07:01:25 +00:00
|
|
|
/* Depending on the key exchange method, compute |pms| and |pms_len|. */
|
|
|
|
if (alg_k & SSL_kRSA) {
|
|
|
|
pms_len = SSL_MAX_MASTER_KEY_LENGTH;
|
|
|
|
pms = OPENSSL_malloc(pms_len);
|
|
|
|
if (pms == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-11-20 22:47:25 +00:00
|
|
|
|
2016-12-12 19:37:43 +00:00
|
|
|
RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey);
|
2015-12-17 07:01:25 +00:00
|
|
|
if (rsa == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-12-06 18:35:25 +00:00
|
|
|
pms[0] = hs->client_version >> 8;
|
|
|
|
pms[1] = hs->client_version & 0xff;
|
2015-12-17 07:01:25 +00:00
|
|
|
if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-17 23:48:29 +01:00
|
|
|
CBB child, *enc_pms = &body;
|
2015-12-17 07:01:25 +00:00
|
|
|
size_t enc_pms_len;
|
|
|
|
/* In TLS, there is a length prefix. */
|
|
|
|
if (ssl->version > SSL3_VERSION) {
|
2016-06-17 23:48:29 +01:00
|
|
|
if (!CBB_add_u16_length_prefixed(&body, &child)) {
|
2014-12-18 03:06:57 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2015-12-17 07:01:25 +00:00
|
|
|
enc_pms = &child;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-17 07:01:25 +00:00
|
|
|
uint8_t *ptr;
|
|
|
|
if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
|
|
|
|
!RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
|
|
|
|
RSA_PKCS1_PADDING) ||
|
|
|
|
!CBB_did_write(enc_pms, enc_pms_len) ||
|
2016-06-17 23:48:29 +01:00
|
|
|
!CBB_flush(&body)) {
|
2015-12-17 07:01:25 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2017-04-10 23:52:14 +01:00
|
|
|
} else if (alg_k & SSL_kECDHE) {
|
2016-05-17 00:31:05 +01:00
|
|
|
/* Generate a keypair and serialize the public half. */
|
2015-12-17 07:01:25 +00:00
|
|
|
CBB child;
|
2017-04-10 23:52:14 +01:00
|
|
|
if (!CBB_add_u8_length_prefixed(&body, &child)) {
|
2015-12-17 07:01:25 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-12-19 05:18:25 +00:00
|
|
|
/* Compute the premaster. */
|
2017-02-08 21:33:15 +00:00
|
|
|
uint8_t alert = SSL_AD_DECODE_ERROR;
|
2016-11-14 01:32:04 +00:00
|
|
|
if (!SSL_ECDH_CTX_accept(&hs->ecdh_ctx, &child, &pms, &pms_len, &alert,
|
|
|
|
hs->peer_key, hs->peer_key_len)) {
|
2015-12-19 05:18:25 +00:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
|
2015-12-17 07:01:25 +00:00
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2016-06-17 23:48:29 +01:00
|
|
|
if (!CBB_flush(&body)) {
|
2016-05-16 22:27:14 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-19 05:18:25 +00:00
|
|
|
/* The key exchange state may now be discarded. */
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
|
|
|
|
OPENSSL_free(hs->peer_key);
|
|
|
|
hs->peer_key = NULL;
|
|
|
|
hs->peer_key_len = 0;
|
2015-12-17 07:01:25 +00:00
|
|
|
} else if (alg_k & SSL_kPSK) {
|
|
|
|
/* For plain PSK, other_secret is a block of 0s with the same length as
|
|
|
|
* the pre-shared key. */
|
|
|
|
pms_len = psk_len;
|
|
|
|
pms = OPENSSL_malloc(pms_len);
|
|
|
|
if (pms == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
2015-02-17 00:33:53 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(pms, 0, pms_len);
|
2015-12-17 07:01:25 +00:00
|
|
|
} else {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-17 07:01:25 +00:00
|
|
|
/* For a PSK cipher suite, other_secret is combined with the pre-shared
|
|
|
|
* key. */
|
|
|
|
if (alg_a & SSL_aPSK) {
|
|
|
|
CBB pms_cbb, child;
|
|
|
|
uint8_t *new_pms;
|
|
|
|
size_t new_pms_len;
|
|
|
|
|
|
|
|
CBB_zero(&pms_cbb);
|
|
|
|
if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) ||
|
|
|
|
!CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
|
|
|
|
!CBB_add_bytes(&child, pms, pms_len) ||
|
|
|
|
!CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
|
|
|
|
!CBB_add_bytes(&child, psk, psk_len) ||
|
|
|
|
!CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) {
|
|
|
|
CBB_cleanup(&pms_cbb);
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
OPENSSL_cleanse(pms, pms_len);
|
|
|
|
OPENSSL_free(pms);
|
2015-12-17 07:01:25 +00:00
|
|
|
pms = new_pms;
|
|
|
|
pms_len = new_pms_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The message must be added to the finished hash before calculating the
|
|
|
|
* master secret. */
|
Don't use the buffer BIO in TLS.
On the TLS side, we introduce a running buffer of ciphertext. Queuing up
pending data consists of encrypting the record into the buffer. This
effectively reimplements what the buffer BIO was doing previously, but
this resizes to fit the whole flight.
As part of this, rename all the functions to add to the pending flight
to be more uniform. This CL proposes "add_foo" to add to the pending
flight and "flush_flight" to drain it.
We add an add_alert hook for alerts but, for now, only the SSL 3.0
warning alert (sent mid-handshake) uses this mechanism. Later work will
push this down to the rest of the write path so closure alerts use it
too, as in DTLS. The intended end state is that all the ssl_buffer.c and
wpend_ret logic will only be used for application data and eventually
optionally replaced by the in-place API, while all "incidental" data
will be handled internally.
For now, the two buffers are mutually exclusive. Moving closure alerts
to "incidentals" will change this, but flushing application data early
is tricky due to wpend_ret. (If we call ssl_write_buffer_flush,
do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That
too is all left alone in this change.
To keep the diff down, write_message is retained for now and will be
removed from the state machines in a follow-up change.
BUG=72
Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d
Reviewed-on: https://boringssl-review.googlesource.com/13224
Reviewed-by: Adam Langley <agl@google.com>
2017-01-03 23:37:41 +00:00
|
|
|
if (!ssl_add_message_cbb(ssl, &cbb)) {
|
2015-12-17 07:01:25 +00:00
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-17 07:01:25 +00:00
|
|
|
|
2017-02-10 01:01:26 +00:00
|
|
|
hs->new_session->master_key_length = tls1_generate_master_secret(
|
|
|
|
hs, hs->new_session->master_key, pms, pms_len);
|
|
|
|
if (hs->new_session->master_key_length == 0) {
|
2015-12-17 07:01:25 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2017-02-17 21:26:01 +00:00
|
|
|
hs->new_session->extended_master_secret = hs->extended_master_secret;
|
2015-12-17 07:01:25 +00:00
|
|
|
OPENSSL_cleanse(pms, pms_len);
|
|
|
|
OPENSSL_free(pms);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2017-01-13 01:02:05 +00:00
|
|
|
return 1;
|
2014-10-11 00:23:43 +01:00
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
err:
|
2016-03-02 05:23:57 +00:00
|
|
|
CBB_cleanup(&cbb);
|
2015-12-17 07:01:25 +00:00
|
|
|
if (pms != NULL) {
|
2014-12-18 03:06:57 +00:00
|
|
|
OPENSSL_cleanse(pms, pms_len);
|
|
|
|
OPENSSL_free(pms);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2016-06-17 22:33:30 +01:00
|
|
|
assert(ssl_has_private_key(ssl));
|
|
|
|
|
2016-06-17 23:48:29 +01:00
|
|
|
CBB cbb, body, child;
|
|
|
|
if (!ssl->method->init_message(ssl, &cbb, &body,
|
|
|
|
SSL3_MT_CERTIFICATE_VERIFY)) {
|
2015-12-18 06:57:43 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-07-08 23:52:59 +01:00
|
|
|
uint16_t signature_algorithm;
|
2016-11-17 08:20:47 +00:00
|
|
|
if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
|
2016-07-08 23:52:59 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2016-06-17 22:33:30 +01:00
|
|
|
if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
|
2016-06-29 18:16:27 +01:00
|
|
|
/* Write out the digest type in TLS 1.2. */
|
|
|
|
if (!CBB_add_u16(&body, signature_algorithm)) {
|
2016-06-17 22:33:30 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-17 22:33:30 +01:00
|
|
|
/* Set aside space for the signature. */
|
2017-03-30 21:51:53 +01:00
|
|
|
const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey);
|
2016-06-17 22:33:30 +01:00
|
|
|
uint8_t *ptr;
|
2016-06-17 23:48:29 +01:00
|
|
|
if (!CBB_add_u16_length_prefixed(&body, &child) ||
|
2016-06-17 22:33:30 +01:00
|
|
|
!CBB_reserve(&child, &ptr, max_sig_len)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-06-30 18:27:23 +01:00
|
|
|
size_t sig_len = max_sig_len;
|
2015-12-18 06:57:43 +00:00
|
|
|
enum ssl_private_key_result_t sign_result;
|
2016-12-12 22:00:50 +00:00
|
|
|
if (hs->state == SSL3_ST_CW_CERT_VRFY_A) {
|
2016-06-30 18:27:23 +01:00
|
|
|
/* The SSL3 construction for CertificateVerify does not decompose into a
|
|
|
|
* single final digest and signature, and must be special-cased. */
|
|
|
|
if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
|
|
|
|
if (ssl->cert->key_method != NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t digest[EVP_MAX_MD_SIZE];
|
|
|
|
size_t digest_len;
|
2017-02-10 01:01:26 +00:00
|
|
|
if (!SSL_TRANSCRIPT_ssl3_cert_verify_hash(&hs->transcript, digest,
|
|
|
|
&digest_len, hs->new_session,
|
|
|
|
signature_algorithm)) {
|
2016-06-30 18:27:23 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
sign_result = ssl_private_key_success;
|
|
|
|
|
|
|
|
EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
|
|
|
|
if (pctx == NULL ||
|
|
|
|
!EVP_PKEY_sign_init(pctx) ||
|
|
|
|
!EVP_PKEY_sign(pctx, ptr, &sig_len, digest, digest_len)) {
|
|
|
|
EVP_PKEY_CTX_free(pctx);
|
|
|
|
sign_result = ssl_private_key_failure;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
EVP_PKEY_CTX_free(pctx);
|
|
|
|
} else {
|
|
|
|
sign_result = ssl_private_key_sign(
|
|
|
|
ssl, ptr, &sig_len, max_sig_len, signature_algorithm,
|
2017-01-12 18:17:07 +00:00
|
|
|
(const uint8_t *)hs->transcript.buffer->data,
|
|
|
|
hs->transcript.buffer->length);
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-18 06:57:43 +00:00
|
|
|
|
|
|
|
/* The handshake buffer is no longer necessary. */
|
2017-01-12 18:17:07 +00:00
|
|
|
SSL_TRANSCRIPT_free_buffer(&hs->transcript);
|
2015-12-18 06:57:43 +00:00
|
|
|
} else {
|
2016-12-12 22:00:50 +00:00
|
|
|
assert(hs->state == SSL3_ST_CW_CERT_VRFY_B);
|
2016-07-14 19:52:41 +01:00
|
|
|
sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
|
2015-12-18 06:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (sign_result) {
|
|
|
|
case ssl_private_key_success:
|
|
|
|
break;
|
|
|
|
case ssl_private_key_failure:
|
|
|
|
goto err;
|
|
|
|
case ssl_private_key_retry:
|
|
|
|
ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_CW_CERT_VRFY_B;
|
2015-12-18 06:57:43 +00:00
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 06:57:43 +00:00
|
|
|
if (!CBB_did_write(&child, sig_len) ||
|
Don't use the buffer BIO in TLS.
On the TLS side, we introduce a running buffer of ciphertext. Queuing up
pending data consists of encrypting the record into the buffer. This
effectively reimplements what the buffer BIO was doing previously, but
this resizes to fit the whole flight.
As part of this, rename all the functions to add to the pending flight
to be more uniform. This CL proposes "add_foo" to add to the pending
flight and "flush_flight" to drain it.
We add an add_alert hook for alerts but, for now, only the SSL 3.0
warning alert (sent mid-handshake) uses this mechanism. Later work will
push this down to the rest of the write path so closure alerts use it
too, as in DTLS. The intended end state is that all the ssl_buffer.c and
wpend_ret logic will only be used for application data and eventually
optionally replaced by the in-place API, while all "incidental" data
will be handled internally.
For now, the two buffers are mutually exclusive. Moving closure alerts
to "incidentals" will change this, but flushing application data early
is tricky due to wpend_ret. (If we call ssl_write_buffer_flush,
do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That
too is all left alone in this change.
To keep the diff down, write_message is retained for now and will be
removed from the state machines in a follow-up change.
BUG=72
Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d
Reviewed-on: https://boringssl-review.googlesource.com/13224
Reviewed-by: Adam Langley <agl@google.com>
2017-01-03 23:37:41 +00:00
|
|
|
!ssl_add_message_cbb(ssl, &cbb)) {
|
2015-12-18 06:57:43 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-01-13 01:02:05 +00:00
|
|
|
return 1;
|
2015-12-18 06:57:43 +00:00
|
|
|
|
|
|
|
err:
|
|
|
|
CBB_cleanup(&cbb);
|
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_send_next_proto(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2015-10-10 19:59:09 +01:00
|
|
|
static const uint8_t kZero[32] = {0};
|
2015-12-25 20:56:49 +00:00
|
|
|
size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
|
2015-10-10 19:59:09 +01:00
|
|
|
|
2016-06-17 23:48:29 +01:00
|
|
|
CBB cbb, body, child;
|
|
|
|
if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) ||
|
|
|
|
!CBB_add_u8_length_prefixed(&body, &child) ||
|
2015-12-25 20:56:49 +00:00
|
|
|
!CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
|
|
|
|
ssl->s3->next_proto_negotiated_len) ||
|
2016-06-17 23:48:29 +01:00
|
|
|
!CBB_add_u8_length_prefixed(&body, &child) ||
|
2015-10-10 19:59:09 +01:00
|
|
|
!CBB_add_bytes(&child, kZero, padding_len) ||
|
Don't use the buffer BIO in TLS.
On the TLS side, we introduce a running buffer of ciphertext. Queuing up
pending data consists of encrypting the record into the buffer. This
effectively reimplements what the buffer BIO was doing previously, but
this resizes to fit the whole flight.
As part of this, rename all the functions to add to the pending flight
to be more uniform. This CL proposes "add_foo" to add to the pending
flight and "flush_flight" to drain it.
We add an add_alert hook for alerts but, for now, only the SSL 3.0
warning alert (sent mid-handshake) uses this mechanism. Later work will
push this down to the rest of the write path so closure alerts use it
too, as in DTLS. The intended end state is that all the ssl_buffer.c and
wpend_ret logic will only be used for application data and eventually
optionally replaced by the in-place API, while all "incidental" data
will be handled internally.
For now, the two buffers are mutually exclusive. Moving closure alerts
to "incidentals" will change this, but flushing application data early
is tricky due to wpend_ret. (If we call ssl_write_buffer_flush,
do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That
too is all left alone in this change.
To keep the diff down, write_message is retained for now and will be
removed from the state machines in a follow-up change.
BUG=72
Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d
Reviewed-on: https://boringssl-review.googlesource.com/13224
Reviewed-by: Adam Langley <agl@google.com>
2017-01-03 23:37:41 +00:00
|
|
|
!ssl_add_message_cbb(ssl, &cbb)) {
|
2015-10-10 19:59:09 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
CBB_cleanup(&cbb);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-01-13 01:02:05 +00:00
|
|
|
return 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_send_channel_id(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2016-09-24 00:25:11 +01:00
|
|
|
if (!ssl_do_channel_id_callback(ssl)) {
|
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2015-10-10 21:30:22 +01:00
|
|
|
if (ssl->tlsext_channel_id_private == NULL) {
|
|
|
|
ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
|
2014-12-18 03:06:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-09-24 00:25:11 +01:00
|
|
|
CBB cbb, body;
|
2016-07-06 21:50:14 +01:00
|
|
|
if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
|
2017-01-12 18:17:07 +00:00
|
|
|
!tls1_write_channel_id(hs, &body) ||
|
Don't use the buffer BIO in TLS.
On the TLS side, we introduce a running buffer of ciphertext. Queuing up
pending data consists of encrypting the record into the buffer. This
effectively reimplements what the buffer BIO was doing previously, but
this resizes to fit the whole flight.
As part of this, rename all the functions to add to the pending flight
to be more uniform. This CL proposes "add_foo" to add to the pending
flight and "flush_flight" to drain it.
We add an add_alert hook for alerts but, for now, only the SSL 3.0
warning alert (sent mid-handshake) uses this mechanism. Later work will
push this down to the rest of the write path so closure alerts use it
too, as in DTLS. The intended end state is that all the ssl_buffer.c and
wpend_ret logic will only be used for application data and eventually
optionally replaced by the in-place API, while all "incidental" data
will be handled internally.
For now, the two buffers are mutually exclusive. Moving closure alerts
to "incidentals" will change this, but flushing application data early
is tricky due to wpend_ret. (If we call ssl_write_buffer_flush,
do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That
too is all left alone in this change.
To keep the diff down, write_message is retained for now and will be
removed from the state machines in a follow-up change.
BUG=72
Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d
Reviewed-on: https://boringssl-review.googlesource.com/13224
Reviewed-by: Adam Langley <agl@google.com>
2017-01-03 23:37:41 +00:00
|
|
|
!ssl_add_message_cbb(ssl, &cbb)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2015-10-10 21:30:22 +01:00
|
|
|
CBB_cleanup(&cbb);
|
2016-09-24 00:25:11 +01:00
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 01:02:05 +00:00
|
|
|
return 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2017-01-21 19:49:39 +00:00
|
|
|
int ret = ssl->method->ssl_get_message(ssl);
|
2016-07-08 22:32:11 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2016-03-10 13:43:27 +00:00
|
|
|
|
2017-01-21 19:49:39 +00:00
|
|
|
if (!ssl_check_message_type(ssl, SSL3_MT_NEW_SESSION_TICKET) ||
|
2017-01-12 18:17:07 +00:00
|
|
|
!ssl_hash_current_message(hs)) {
|
2017-01-21 19:13:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
CBS new_session_ticket, ticket;
|
2016-08-09 18:18:40 +01:00
|
|
|
uint32_t tlsext_tick_lifetime_hint;
|
2016-07-08 22:32:11 +01:00
|
|
|
CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num);
|
2016-08-09 18:18:40 +01:00
|
|
|
if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
|
2016-06-07 22:03:46 +01:00
|
|
|
!CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
|
|
|
|
CBS_len(&new_session_ticket) != 0) {
|
2016-06-27 21:34:59 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2016-06-07 22:03:46 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2016-06-27 21:34:59 +01:00
|
|
|
return -1;
|
2016-03-10 13:43:27 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
if (CBS_len(&ticket) == 0) {
|
|
|
|
/* RFC 5077 allows a server to change its mind and send no ticket after
|
2016-11-12 02:20:55 +00:00
|
|
|
* negotiating the extension. The value of |ticket_expected| is checked in
|
|
|
|
* |ssl_update_cache| so is cleared here to avoid an unnecessary update. */
|
2016-11-14 01:32:04 +00:00
|
|
|
hs->ticket_expected = 0;
|
2016-06-07 22:03:46 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2015-08-18 12:21:54 +01:00
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
int session_renewed = ssl->session != NULL;
|
2017-02-10 01:01:26 +00:00
|
|
|
SSL_SESSION *session = hs->new_session;
|
2016-06-27 21:34:59 +01:00
|
|
|
if (session_renewed) {
|
2016-06-07 22:03:46 +01:00
|
|
|
/* The server is sending a new ticket for an existing session. Sessions are
|
|
|
|
* immutable once established, so duplicate all but the ticket of the
|
|
|
|
* existing session. */
|
2016-07-29 19:32:55 +01:00
|
|
|
session = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
|
2016-06-27 21:34:59 +01:00
|
|
|
if (session == NULL) {
|
2016-06-07 22:03:46 +01:00
|
|
|
/* This should never happen. */
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-08-18 12:21:54 +01:00
|
|
|
}
|
|
|
|
|
2016-11-03 20:59:25 +00:00
|
|
|
/* |tlsext_tick_lifetime_hint| is measured from when the ticket was issued. */
|
2017-01-28 19:00:32 +00:00
|
|
|
ssl_session_rebase_time(ssl, session);
|
2016-11-03 20:59:25 +00:00
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
|
2016-06-07 22:03:46 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2016-08-09 18:18:40 +01:00
|
|
|
session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
|
2016-06-07 22:03:46 +01:00
|
|
|
|
|
|
|
/* Generate a session ID for this session based on the session ticket. We use
|
|
|
|
* the session ID mechanism for detecting ticket resumption. This also fits in
|
|
|
|
* with assumptions elsewhere in OpenSSL.*/
|
2016-06-27 21:34:59 +01:00
|
|
|
if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
|
|
|
|
session->session_id, &session->session_id_length,
|
|
|
|
EVP_sha256(), NULL)) {
|
2016-06-07 22:03:46 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
if (session_renewed) {
|
|
|
|
session->not_resumable = 0;
|
|
|
|
SSL_SESSION_free(ssl->session);
|
|
|
|
ssl->session = session;
|
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
err:
|
2016-06-27 21:34:59 +01:00
|
|
|
if (session_renewed) {
|
|
|
|
SSL_SESSION_free(session);
|
|
|
|
}
|
2016-06-07 22:03:46 +01:00
|
|
|
return -1;
|
2015-08-18 12:21:54 +01:00
|
|
|
}
|