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.
|
2017-08-14 20:08:34 +01:00
|
|
|
*
|
2014-06-20 20:00:00 +01:00
|
|
|
* 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).
|
2017-08-14 20:08:34 +01:00
|
|
|
*
|
2014-06-20 20:00:00 +01:00
|
|
|
* 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.
|
2017-08-14 20:08:34 +01:00
|
|
|
*
|
2014-06-20 20:00:00 +01:00
|
|
|
* 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 :-).
|
2017-08-14 20:08:34 +01:00
|
|
|
* 4. If you include any Windows specific code (or a derivative thereof) from
|
2014-06-20 20:00:00 +01:00
|
|
|
* the apps directory (application code) you must include an acknowledgement:
|
|
|
|
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
2017-08-14 20:08:34 +01:00
|
|
|
*
|
2014-06-20 20:00:00 +01:00
|
|
|
* 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.
|
2017-08-14 20:08:34 +01:00
|
|
|
*
|
2014-06-20 20:00:00 +01:00
|
|
|
* 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
|
2017-08-14 20:08:34 +01:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2014-06-20 20:00:00 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2017-08-14 20:08:34 +01:00
|
|
|
* Portions of the attached software ("Contribution") are developed by
|
2014-06-20 20:00:00 +01:00
|
|
|
* 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>
|
2017-08-14 20:08:34 +01:00
|
|
|
#include <limits.h>
|
2015-04-08 04:05:04 +01:00
|
|
|
#include <string.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-07-20 19:49:15 +01:00
|
|
|
#include <utility>
|
|
|
|
|
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
|
|
|
|
Move libssl's internals into the bssl namespace.
This is horrible, but everything else I tried was worse. The goal with
this CL is to take the extern "C" out of ssl/internal.h and move most
symbols to namespace bssl, so we can start using C++ helpers and
destructors without worry.
Complications:
- Public API functions must be extern "C" and match their declaration in
ssl.h, which is unnamespaced. C++ really does not want you to
interleave namespaced and unnamespaced things. One can actually write
a namespaced extern "C" function, but this means, from C++'s
perspective, the function is namespaced. Trying to namespace the
public header would worked but ended up too deep a rabbithole.
- Our STACK_OF macros do not work right in namespaces.
- The typedefs for our exposed but opaque types are visible in the
header files and copied into consuming projects as forward
declarations. We ultimately want to give SSL a destructor, but
clobbering an unnamespaced ssl_st::~ssl_st seems bad manners.
- MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL.
This CL opts for:
- ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This
informs the public headers to create forward declarations which are
compatible with our namespaces.
- For now, C++-defined type FOO ends up at bssl::FOO with a typedef
outside. Later I imagine we'll rename many of them.
- Internal functions get namespace bssl, so we stop worrying about
stomping the tls1_prf symbol. Exported C functions are stuck as they
are. Rather than try anything weird, bite the bullet and reorder files
which have a mix of public and private functions. I expect that over
time, the public functions will become fairly small as we move logic
to more idiomatic C++.
Files without any public C functions can just be written normally.
- To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle
in advance of them being made idiomatic C++.
Bug: 132
Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581
Reviewed-on: https://boringssl-review.googlesource.com/18124
Reviewed-by: David Benjamin <davidben@google.com>
2017-07-18 21:34:25 +01:00
|
|
|
namespace bssl {
|
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
enum ssl_client_hs_state_t {
|
|
|
|
state_start_connect = 0,
|
|
|
|
state_enter_early_data,
|
|
|
|
state_read_hello_verify_request,
|
|
|
|
state_read_server_hello,
|
|
|
|
state_tls13,
|
|
|
|
state_read_server_certificate,
|
|
|
|
state_read_certificate_status,
|
|
|
|
state_verify_server_certificate,
|
|
|
|
state_read_server_key_exchange,
|
|
|
|
state_read_certificate_request,
|
|
|
|
state_read_server_hello_done,
|
|
|
|
state_send_client_certificate,
|
|
|
|
state_send_client_key_exchange,
|
|
|
|
state_send_client_certificate_verify,
|
|
|
|
state_send_client_finished,
|
|
|
|
state_finish_flight,
|
|
|
|
state_read_session_ticket,
|
|
|
|
state_process_change_cipher_spec,
|
|
|
|
state_read_server_finished,
|
|
|
|
state_finish_client_handshake,
|
|
|
|
state_done,
|
|
|
|
};
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
|
|
|
|
// disabled algorithms.
|
2017-01-25 07:18:43 +00:00
|
|
|
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;
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// PSK requires a client callback.
|
2017-01-25 07:18:43 +00:00
|
|
|
if (ssl->psk_client_callback == NULL) {
|
|
|
|
*out_mask_a |= SSL_aPSK;
|
|
|
|
*out_mask_k |= SSL_kPSK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 19:49:43 +01:00
|
|
|
static int ssl_write_client_cipher_list(SSL_HANDSHAKE *hs, CBB *out) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
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
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Add a fake cipher suite. See draft-davidben-tls-grease-01.
|
2016-09-03 02:35:25 +01:00
|
|
|
if (ssl->ctx->grease_enabled &&
|
|
|
|
!CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
|
|
|
|
// hardware support.
|
2017-06-20 19:49:43 +01:00
|
|
|
if (hs->max_version >= TLS1_3_VERSION) {
|
2016-10-31 23:20:42 +00:00
|
|
|
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
|
|
|
|
2017-06-20 19:49:43 +01:00
|
|
|
if (hs->min_version < TLS1_3_VERSION) {
|
2016-10-31 23:20:42 +00:00
|
|
|
int any_enabled = 0;
|
2017-07-26 03:36:00 +01:00
|
|
|
for (const SSL_CIPHER *cipher : SSL_get_ciphers(ssl)) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// 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;
|
|
|
|
}
|
2017-06-20 19:49:43 +01:00
|
|
|
if (SSL_CIPHER_get_min_version(cipher) > hs->max_version ||
|
|
|
|
SSL_CIPHER_get_max_version(cipher) < hs->min_version) {
|
2016-10-31 23:20:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
any_enabled = 1;
|
|
|
|
if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// If all ciphers were disabled, return the error to the caller.
|
2017-06-20 19:49:43 +01:00
|
|
|
if (!any_enabled && hs->max_version < TLS1_3_VERSION) {
|
2016-10-31 23:20:42 +00:00
|
|
|
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
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
|
|
|
|
// added.
|
2017-06-20 19:49:43 +01:00
|
|
|
if (hs->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;
|
Move libssl's internals into the bssl namespace.
This is horrible, but everything else I tried was worse. The goal with
this CL is to take the extern "C" out of ssl/internal.h and move most
symbols to namespace bssl, so we can start using C++ helpers and
destructors without worry.
Complications:
- Public API functions must be extern "C" and match their declaration in
ssl.h, which is unnamespaced. C++ really does not want you to
interleave namespaced and unnamespaced things. One can actually write
a namespaced extern "C" function, but this means, from C++'s
perspective, the function is namespaced. Trying to namespace the
public header would worked but ended up too deep a rabbithole.
- Our STACK_OF macros do not work right in namespaces.
- The typedefs for our exposed but opaque types are visible in the
header files and copied into consuming projects as forward
declarations. We ultimately want to give SSL a destructor, but
clobbering an unnamespaced ssl_st::~ssl_st seems bad manners.
- MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL.
This CL opts for:
- ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This
informs the public headers to create forward declarations which are
compatible with our namespaces.
- For now, C++-defined type FOO ends up at bssl::FOO with a typedef
outside. Later I imagine we'll rename many of them.
- Internal functions get namespace bssl, so we stop worrying about
stomping the tls1_prf symbol. Exported C functions are stuck as they
are. Rather than try anything weird, bite the bullet and reorder files
which have a mix of public and private functions. I expect that over
time, the public functions will become fairly small as we move logic
to more idiomatic C++.
Files without any public C functions can just be written normally.
- To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle
in advance of them being made idiomatic C++.
Bug: 132
Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581
Reviewed-on: https://boringssl-review.googlesource.com/18124
Reviewed-by: David Benjamin <davidben@google.com>
2017-07-18 21:34:25 +01:00
|
|
|
ScopedCBB cbb;
|
2017-07-12 21:25:57 +01:00
|
|
|
CBB body;
|
|
|
|
if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO)) {
|
|
|
|
return 0;
|
2016-11-01 17:39:36 +00:00
|
|
|
}
|
|
|
|
|
2016-07-18 17:40:30 +01:00
|
|
|
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) ||
|
2017-12-01 17:24:01 +00:00
|
|
|
!CBB_add_u8_length_prefixed(&body, &child)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not send a session ID on renegotiation.
|
|
|
|
if (!ssl->s3->initial_handshake_complete &&
|
2017-11-12 01:34:06 +00:00
|
|
|
!CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) {
|
2017-07-12 21:25:57 +01:00
|
|
|
return 0;
|
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)) {
|
2017-07-12 21:25:57 +01:00
|
|
|
return 0;
|
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;
|
2017-06-20 19:49:43 +01:00
|
|
|
if (!ssl_write_client_cipher_list(hs, &body) ||
|
2016-11-01 17:39:36 +00:00
|
|
|
!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))) {
|
2017-07-12 21:25:57 +01:00
|
|
|
return 0;
|
2016-11-01 17:39:36 +00:00
|
|
|
}
|
|
|
|
|
2017-09-21 16:20:53 +01:00
|
|
|
Array<uint8_t> msg;
|
|
|
|
if (!ssl->method->finish_message(ssl, cbb.get(), &msg)) {
|
2017-07-12 21:25:57 +01:00
|
|
|
return 0;
|
2016-07-18 17:40:30 +01:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01: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-09-21 16:20:53 +01:00
|
|
|
!tls13_write_psk_binder(hs, msg.data(), msg.size())) {
|
2017-07-12 21:25:57 +01:00
|
|
|
return 0;
|
2016-11-01 17:39:36 +00:00
|
|
|
}
|
|
|
|
|
2017-09-21 16:20:53 +01:00
|
|
|
return ssl->method->add_message(ssl, std::move(msg));
|
2016-07-18 17:40:30 +01:00
|
|
|
}
|
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
static int parse_server_version(SSL_HANDSHAKE *hs, uint16_t *out,
|
|
|
|
const SSLMessage &msg) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
|
|
|
if (msg.type != SSL3_MT_SERVER_HELLO &&
|
|
|
|
msg.type != SSL3_MT_HELLO_RETRY_REQUEST) {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
|
2017-08-14 20:08:34 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CBS server_hello = msg.body;
|
|
|
|
if (!CBS_get_u16(&server_hello, out)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// The server version may also be in the supported_versions extension if
|
|
|
|
// applicable.
|
2017-08-14 20:08:34 +01:00
|
|
|
if (msg.type != SSL3_MT_SERVER_HELLO || *out != TLS1_2_VERSION) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t sid_length;
|
|
|
|
if (!CBS_skip(&server_hello, SSL3_RANDOM_SIZE) ||
|
|
|
|
!CBS_get_u8(&server_hello, &sid_length) ||
|
|
|
|
!CBS_skip(&server_hello, sid_length + 2 /* cipher_suite */ +
|
|
|
|
1 /* compression_method */)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// The extensions block may not be present.
|
2017-08-14 20:08:34 +01:00
|
|
|
if (CBS_len(&server_hello) == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CBS extensions;
|
|
|
|
if (!CBS_get_u16_length_prefixed(&server_hello, &extensions) ||
|
|
|
|
CBS_len(&server_hello) != 0) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-31 20:13:12 +01:00
|
|
|
bool have_supported_versions;
|
2017-08-14 20:08:34 +01:00
|
|
|
CBS supported_versions;
|
|
|
|
const SSL_EXTENSION_TYPE ext_types[] = {
|
|
|
|
{TLSEXT_TYPE_supported_versions, &have_supported_versions,
|
|
|
|
&supported_versions},
|
|
|
|
};
|
|
|
|
|
|
|
|
uint8_t alert = SSL_AD_DECODE_ERROR;
|
|
|
|
if (!ssl_parse_extensions(&extensions, &alert, ext_types,
|
|
|
|
OPENSSL_ARRAY_SIZE(ext_types),
|
|
|
|
1 /* ignore unknown */)) {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
|
2017-08-14 20:08:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_supported_versions &&
|
|
|
|
(!CBS_get_u16(&supported_versions, out) ||
|
|
|
|
CBS_len(&supported_versions) != 0)) {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum ssl_hs_wait_t do_start_connect(SSL_HANDSHAKE *hs) {
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
2017-08-14 20:08:34 +01:00
|
|
|
|
2017-08-29 18:25:56 +01:00
|
|
|
ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
|
2017-09-06 21:46:50 +01:00
|
|
|
// |session_reused| must be reset in case this is a renegotiation.
|
|
|
|
ssl->s3->session_reused = false;
|
2015-11-02 22:16:13 +00:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Freeze the version range.
|
2017-06-20 19:49:43 +01:00
|
|
|
if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2016-06-21 15:33:21 +01:00
|
|
|
}
|
|
|
|
|
2017-09-11 18:53:08 +01:00
|
|
|
// SSL 3.0 ClientHellos should use SSL 3.0 not TLS 1.0, for the record-layer
|
|
|
|
// version.
|
|
|
|
if (hs->max_version == SSL3_VERSION) {
|
|
|
|
ssl->s3->aead_write_ctx->SetVersionIfNullCipher(SSL3_VERSION);
|
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// 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.
|
2017-06-20 15:55:02 +01:00
|
|
|
if (SSL_is_dtls(hs->ssl)) {
|
|
|
|
hs->client_version =
|
|
|
|
hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION;
|
|
|
|
} else {
|
|
|
|
hs->client_version =
|
|
|
|
hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version;
|
2015-10-10 19:13:23 +01:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01: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-12-12 18:51:00 +00:00
|
|
|
if (ssl->session->is_server ||
|
2017-06-20 15:55:02 +01:00
|
|
|
!ssl_supports_version(hs, ssl->session->ssl_version) ||
|
|
|
|
(ssl->session->session_id_length == 0 &&
|
|
|
|
ssl->session->tlsext_ticklen == 0) ||
|
2016-07-29 19:32:55 +01:00
|
|
|
ssl->session->not_resumable ||
|
2017-06-20 15:55:02 +01:00
|
|
|
!ssl_session_is_time_valid(ssl, ssl->session)) {
|
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
|
|
|
}
|
|
|
|
|
2017-08-29 18:25:56 +01:00
|
|
|
if (!RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2015-10-10 19:13:23 +01:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Initialize a random session ID for the experimental TLS 1.3 variant
|
|
|
|
// requiring a session id.
|
2017-11-12 01:34:06 +00:00
|
|
|
if (ssl->session != nullptr &&
|
|
|
|
!ssl->s3->initial_handshake_complete &&
|
|
|
|
ssl->session->session_id_length > 0) {
|
|
|
|
hs->session_id_len = ssl->session->session_id_length;
|
|
|
|
OPENSSL_memcpy(hs->session_id, ssl->session->session_id,
|
|
|
|
hs->session_id_len);
|
|
|
|
} else if (ssl_is_resumption_variant(ssl->tls13_variant)) {
|
2017-06-13 17:45:25 +01:00
|
|
|
hs->session_id_len = sizeof(hs->session_id);
|
|
|
|
if (!RAND_bytes(hs->session_id, hs->session_id_len)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2017-06-13 17:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-03 07:20:34 +00:00
|
|
|
if (!ssl_write_client_hello(hs)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2015-10-10 19:13:23 +01:00
|
|
|
}
|
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
hs->state = state_enter_early_data;
|
|
|
|
return ssl_hs_flush;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum ssl_hs_wait_t do_enter_early_data(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
|
|
|
|
2017-08-29 18:25:56 +01:00
|
|
|
if (SSL_is_dtls(ssl)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
hs->state = state_read_hello_verify_request;
|
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hs->early_data_offered) {
|
|
|
|
hs->state = state_read_server_hello;
|
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
2017-11-07 22:09:52 +00:00
|
|
|
ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->session->ssl_version);
|
|
|
|
if (ssl_is_draft22(ssl->session->ssl_version) &&
|
|
|
|
!ssl->method->add_change_cipher_spec(ssl)) {
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2017-10-11 17:29:36 +01:00
|
|
|
if (!tls13_init_early_key_schedule(hs, ssl->session->master_key,
|
|
|
|
ssl->session->master_key_length) ||
|
2017-08-14 20:08:34 +01:00
|
|
|
!tls13_derive_early_secrets(hs) ||
|
|
|
|
!tls13_set_traffic_key(ssl, evp_aead_seal, hs->early_traffic_secret,
|
|
|
|
hs->hash_len)) {
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Stash the early data session, so connection properties may be queried out
|
|
|
|
// of it.
|
2017-08-31 19:49:09 +01:00
|
|
|
hs->in_early_data = true;
|
2017-08-14 20:08:34 +01:00
|
|
|
SSL_SESSION_up_ref(ssl->session);
|
|
|
|
hs->early_session.reset(ssl->session);
|
2017-08-31 19:49:09 +01:00
|
|
|
hs->can_early_write = true;
|
2017-08-14 20:08:34 +01:00
|
|
|
|
|
|
|
hs->state = state_read_server_hello;
|
|
|
|
return ssl_hs_early_return;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
static enum ssl_hs_wait_t do_read_hello_verify_request(SSL_HANDSHAKE *hs) {
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
2017-08-14 20:08:34 +01:00
|
|
|
|
|
|
|
assert(SSL_is_dtls(ssl));
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
SSLMessage msg;
|
2017-08-14 20:08:34 +01:00
|
|
|
if (!ssl->method->get_message(ssl, &msg)) {
|
|
|
|
return ssl_hs_read_message;
|
2016-06-07 21:40:46 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
if (msg.type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
|
2017-08-14 20:08:34 +01:00
|
|
|
hs->state = state_read_server_hello;
|
|
|
|
return ssl_hs_ok;
|
2016-06-07 21:40:46 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
CBS hello_verify_request = msg.body, cookie;
|
|
|
|
uint16_t server_version;
|
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-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
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);
|
|
|
|
|
2017-08-01 22:35:55 +01:00
|
|
|
ssl->method->next_message(ssl);
|
2017-08-29 18:25:56 +01:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// DTLS resets the handshake buffer after HelloVerifyRequest.
|
2017-08-29 18:25:56 +01:00
|
|
|
if (!hs->transcript.Init()) {
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ssl_write_client_hello(hs)) {
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
hs->state = state_read_server_hello;
|
|
|
|
return ssl_hs_flush;
|
2016-06-07 21:40:46 +01:00
|
|
|
}
|
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) {
|
2017-07-14 23:40:34 +01:00
|
|
|
SSL *const ssl = hs->ssl;
|
2017-08-01 21:32:25 +01:00
|
|
|
SSLMessage msg;
|
2017-08-14 20:08:34 +01:00
|
|
|
if (!ssl->method->get_message(ssl, &msg)) {
|
|
|
|
return ssl_hs_read_server_hello;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 23:40:34 +01:00
|
|
|
uint16_t server_version;
|
2017-08-01 21:32:25 +01:00
|
|
|
if (!parse_server_version(hs, &server_version, msg)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-20 15:55:02 +01:00
|
|
|
if (!ssl_supports_version(hs, server_version)) {
|
2016-07-10 17:20:35 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
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) {
|
2017-06-20 15:55:02 +01:00
|
|
|
ssl->version = server_version;
|
2017-08-29 21:33:21 +01:00
|
|
|
// At this point, the connection's version is known and ssl->version is
|
|
|
|
// fixed. Begin enforcing the record-layer version.
|
2017-08-31 20:06:42 +01:00
|
|
|
ssl->s3->have_version = true;
|
2017-09-11 18:53:08 +01:00
|
|
|
ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
|
2017-06-20 15:55:02 +01:00
|
|
|
} else if (server_version != ssl->version) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 23:31:15 +01:00
|
|
|
if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
|
2017-08-14 20:08:34 +01:00
|
|
|
hs->state = state_tls13;
|
|
|
|
return ssl_hs_ok;
|
2016-07-11 18:19:03 +01:00
|
|
|
}
|
|
|
|
|
2017-09-21 08:05:35 +01:00
|
|
|
// Clear some TLS 1.3 state that no longer needs to be retained.
|
|
|
|
hs->key_share.reset();
|
|
|
|
hs->key_share_bytes.Reset();
|
|
|
|
|
|
|
|
// A TLS 1.2 server would not know to skip the early data we offered. Report
|
|
|
|
// an error code sooner. The caller may use this error code to implement the
|
|
|
|
// fallback described in draft-ietf-tls-tls13-18 appendix C.3.
|
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-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2017-01-11 16:34:52 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2016-07-11 18:19:03 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
CBS server_hello = msg.body, server_random, session_id;
|
2017-07-14 23:40:34 +01:00
|
|
|
uint16_t cipher_suite;
|
|
|
|
uint8_t compression_method;
|
|
|
|
if (!CBS_skip(&server_hello, 2 /* version */) ||
|
|
|
|
!CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
|
2016-07-11 18:19:03 +01:00
|
|
|
!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-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2016-07-11 18:19:03 +01:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01: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
|
|
|
|
2017-08-29 21:33:21 +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)) {
|
2017-08-31 20:06:42 +01:00
|
|
|
ssl->s3->session_reused = true;
|
2014-12-18 03:06:57 +00:00
|
|
|
} else {
|
2017-08-29 21:33:21 +01:00
|
|
|
// 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-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2017-08-29 21:33:21 +01: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
|
|
|
}
|
|
|
|
|
2017-08-09 20:02:34 +01:00
|
|
|
const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
|
|
|
|
if (cipher == NULL) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// unknown cipher
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2016-11-02 21:49:09 +00:00
|
|
|
|
2017-08-29 21:33:21 +01: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);
|
2017-08-09 20:02:34 +01:00
|
|
|
if ((cipher->algorithm_mkey & mask_k) || (cipher->algorithm_auth & mask_a) ||
|
2017-10-06 23:31:15 +01:00
|
|
|
SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) ||
|
|
|
|
SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl) ||
|
2017-08-09 20:02:34 +01:00
|
|
|
!sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, cipher)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
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-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2015-03-16 22:02:20 +00:00
|
|
|
}
|
2017-08-09 20:02:34 +01:00
|
|
|
if (ssl->session->cipher != cipher) {
|
2016-09-06 19:13:43 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
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)) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// This is actually a client application bug.
|
2016-07-29 19:32:55 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL,
|
|
|
|
SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2016-07-29 19:32:55 +01:00
|
|
|
}
|
2015-12-25 20:40:14 +00:00
|
|
|
} else {
|
2017-08-09 20:02:34 +01:00
|
|
|
hs->new_session->cipher = cipher;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2017-08-09 20:02:34 +01:00
|
|
|
hs->new_cipher = cipher;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Now that the cipher is known, initialize the handshake hash and hash the
|
|
|
|
// ServerHello.
|
2017-10-06 23:31:15 +01:00
|
|
|
if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
|
2017-08-01 21:32:25 +01:00
|
|
|
!ssl_hash_message(hs, msg)) {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2015-08-05 13:50:34 +01:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +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-07-19 21:38:21 +01:00
|
|
|
hs->transcript.FreeBuffer();
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Only the NULL compression algorithm is supported.
|
2014-12-18 03:06:57 +00:00
|
|
|
if (compression_method != 0) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01: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-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// There should be nothing left over in the record.
|
2014-12-18 03:06:57 +00:00
|
|
|
if (CBS_len(&server_hello) != 0) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// wrong packet length
|
2016-09-09 16:41:18 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
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-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2015-06-02 18:50:35 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 22:35:55 +01:00
|
|
|
ssl->method->next_message(ssl);
|
2017-08-14 20:08:34 +01:00
|
|
|
|
|
|
|
if (ssl->session != NULL) {
|
|
|
|
hs->state = state_read_session_ticket;
|
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
hs->state = state_read_server_certificate;
|
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
|
|
|
|
enum ssl_hs_wait_t wait = tls13_client_handshake(hs);
|
|
|
|
if (wait == ssl_hs_ok) {
|
|
|
|
hs->state = state_finish_client_handshake;
|
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
return wait;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) {
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
2017-08-14 20:08:34 +01:00
|
|
|
|
|
|
|
if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
|
|
|
|
hs->state = state_read_certificate_status;
|
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
SSLMessage msg;
|
2017-08-14 20:08:34 +01:00
|
|
|
if (!ssl->method->get_message(ssl, &msg)) {
|
|
|
|
return ssl_hs_read_message;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
|
|
|
|
!ssl_hash_message(hs, msg)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2017-01-21 19:13:39 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
CBS body = msg.body;
|
2017-02-08 21:33:15 +00:00
|
|
|
uint8_t alert = SSL_AD_DECODE_ERROR;
|
2017-07-21 01:19:36 +01:00
|
|
|
UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
|
2017-08-01 21:32:25 +01:00
|
|
|
if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey, NULL, &body,
|
2017-07-21 01:19:36 +01:00
|
|
|
ssl->ctx->pool)) {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2017-07-21 01:19:36 +01:00
|
|
|
sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
|
|
|
|
hs->new_session->certs = chain.release();
|
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 ||
|
2017-08-01 21:32:25 +01:00
|
|
|
CBS_len(&body) != 0 ||
|
2017-07-20 19:49:15 +01:00
|
|
|
!ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 20:05:49 +00:00
|
|
|
if (!ssl_check_leaf_certificate(
|
2017-07-20 19:49:15 +01:00
|
|
|
hs, hs->peer_pubkey.get(),
|
2017-02-10 01:01:26 +00:00
|
|
|
sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2015-06-08 00:50:37 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 22:35:55 +01:00
|
|
|
ssl->method->next_message(ssl);
|
2017-08-14 20:08:34 +01:00
|
|
|
|
|
|
|
hs->state = state_read_certificate_status;
|
|
|
|
return ssl_hs_ok;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
static enum ssl_hs_wait_t do_read_certificate_status(SSL_HANDSHAKE *hs) {
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
2017-08-14 20:08:34 +01:00
|
|
|
|
|
|
|
if (!hs->certificate_status_expected) {
|
|
|
|
hs->state = state_verify_server_certificate;
|
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
SSLMessage msg;
|
2017-08-14 20:08:34 +01:00
|
|
|
if (!ssl->method->get_message(ssl, &msg)) {
|
|
|
|
return ssl_hs_read_message;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
if (msg.type != SSL3_MT_CERTIFICATE_STATUS) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// A server may send status_request in ServerHello and then change its mind
|
|
|
|
// about sending CertificateStatus.
|
2017-08-14 20:08:34 +01:00
|
|
|
hs->state = state_verify_server_certificate;
|
|
|
|
return ssl_hs_ok;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
if (!ssl_hash_message(hs, msg)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2017-01-21 19:49:39 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
CBS certificate_status = msg.body, ocsp_response;
|
2017-06-16 03:43:04 +01:00
|
|
|
uint8_t status_type;
|
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-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2017-08-22 23:07:51 +01:00
|
|
|
CRYPTO_BUFFER_free(hs->new_session->ocsp_response);
|
|
|
|
hs->new_session->ocsp_response =
|
|
|
|
CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool);
|
|
|
|
if (hs->new_session->ocsp_response == nullptr) {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 22:35:55 +01:00
|
|
|
ssl->method->next_message(ssl);
|
2017-08-14 20:08:34 +01:00
|
|
|
|
|
|
|
hs->state = state_verify_server_certificate;
|
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum ssl_hs_wait_t do_verify_server_certificate(SSL_HANDSHAKE *hs) {
|
|
|
|
if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
|
|
|
|
hs->state = state_read_server_key_exchange;
|
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ssl_verify_peer_cert(hs)) {
|
|
|
|
case ssl_verify_ok:
|
|
|
|
break;
|
|
|
|
case ssl_verify_invalid:
|
|
|
|
return ssl_hs_error;
|
|
|
|
case ssl_verify_retry:
|
|
|
|
hs->state = state_verify_server_certificate;
|
|
|
|
return ssl_hs_certificate_verify;
|
|
|
|
}
|
|
|
|
|
|
|
|
hs->state = state_read_server_key_exchange;
|
|
|
|
return ssl_hs_ok;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
static enum ssl_hs_wait_t do_read_server_key_exchange(SSL_HANDSHAKE *hs) {
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
2017-08-01 21:32:25 +01:00
|
|
|
SSLMessage msg;
|
2017-08-14 20:08:34 +01:00
|
|
|
if (!ssl->method->get_message(ssl, &msg)) {
|
|
|
|
return ssl_hs_read_message;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
if (msg.type != SSL3_MT_SERVER_KEY_EXCHANGE) {
|
2017-08-29 21:33:21 +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);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
hs->state = state_read_certificate_request;
|
|
|
|
return ssl_hs_ok;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
if (!ssl_hash_message(hs, msg)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2017-01-21 19:49:39 +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;
|
2017-08-01 21:32:25 +01:00
|
|
|
CBS server_key_exchange = msg.body;
|
2014-12-18 03:06:57 +00:00
|
|
|
if (alg_a & SSL_aPSK) {
|
|
|
|
CBS psk_identity_hint;
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Each of the PSK key exchanges begins with a psk_identity_hint.
|
2014-12-18 03:06:57 +00:00
|
|
|
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-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 23:31:15 +01:00
|
|
|
// Store the PSK identity hint for the ClientKeyExchange. 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.
|
2017-08-29 21:33:21 +01:00
|
|
|
//
|
|
|
|
// TODO(davidben): Should invalid hints be ignored? It's a hint rather than
|
|
|
|
// a specific identity.
|
2014-12-18 03:06:57 +00:00
|
|
|
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-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +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.
|
2017-07-20 19:49:15 +01:00
|
|
|
char *raw = nullptr;
|
2016-09-17 00:42:05 +01:00
|
|
|
if (CBS_len(&psk_identity_hint) != 0 &&
|
2017-07-20 19:49:15 +01:00
|
|
|
!CBS_strdup(&psk_identity_hint, &raw)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2017-07-20 19:49:15 +01:00
|
|
|
hs->peer_psk_identity_hint.reset(raw);
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 23:52:14 +01:00
|
|
|
if (alg_k & SSL_kECDHE) {
|
2017-08-29 21:33:21 +01: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-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
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
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Ensure the group is consistent with preferences.
|
2016-05-17 16:47:53 +01:00
|
|
|
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-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-19 05:18:25 +00:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Initialize ECDH and save the peer public key for later.
|
2017-07-20 04:28:43 +01:00
|
|
|
hs->key_share = SSLKeyShare::Create(group_id);
|
|
|
|
if (!hs->key_share ||
|
2017-09-21 07:43:05 +01:00
|
|
|
!hs->peer_key.CopyFrom(point)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2016-05-17 00:31:05 +01:00
|
|
|
}
|
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-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// At this point, |server_key_exchange| contains the signature, if any, while
|
|
|
|
// |msg.body| contains the entire message. From that, derive a CBS containing
|
|
|
|
// just the parameter.
|
2015-12-29 22:08:08 +00:00
|
|
|
CBS parameter;
|
2017-08-01 21:32:25 +01:00
|
|
|
CBS_init(¶meter, CBS_data(&msg.body),
|
|
|
|
CBS_len(&msg.body) - CBS_len(&server_key_exchange));
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2017-08-29 21:33:21 +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;
|
2017-10-06 23:31:15 +01:00
|
|
|
if (ssl_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-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
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)) {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
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,
|
2017-07-20 19:49:15 +01:00
|
|
|
hs->peer_pubkey.get())) {
|
2016-07-14 05:11:26 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01: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-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 04:57:40 +01:00
|
|
|
ScopedCBB transcript;
|
2017-10-11 22:19:19 +01:00
|
|
|
Array<uint8_t> transcript_data;
|
2017-07-20 04:57:40 +01:00
|
|
|
if (!CBB_init(transcript.get(),
|
|
|
|
2 * SSL3_RANDOM_SIZE + CBS_len(¶meter)) ||
|
|
|
|
!CBB_add_bytes(transcript.get(), ssl->s3->client_random,
|
|
|
|
SSL3_RANDOM_SIZE) ||
|
|
|
|
!CBB_add_bytes(transcript.get(), ssl->s3->server_random,
|
|
|
|
SSL3_RANDOM_SIZE) ||
|
|
|
|
!CBB_add_bytes(transcript.get(), CBS_data(¶meter),
|
|
|
|
CBS_len(¶meter)) ||
|
2017-10-11 22:19:19 +01:00
|
|
|
!CBBFinishArray(transcript.get(), &transcript_data)) {
|
2016-06-30 18:27:23 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2016-06-29 18:16:27 +01:00
|
|
|
}
|
2016-06-30 18:27:23 +01:00
|
|
|
|
2017-10-11 22:19:19 +01:00
|
|
|
bool sig_ok = ssl_public_key_verify(ssl, signature, signature_algorithm,
|
|
|
|
hs->peer_pubkey.get(), transcript_data);
|
2016-03-02 03:57:40 +00:00
|
|
|
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
|
2017-10-11 22:19:19 +01:00
|
|
|
sig_ok = true;
|
2016-03-02 03:57:40 +00:00
|
|
|
ERR_clear_error();
|
|
|
|
#endif
|
|
|
|
if (!sig_ok) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// bad signature
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-08-29 21:33:21 +01:00
|
|
|
// PSK ciphers are the only supported certificate-less ciphers.
|
2015-06-07 16:00:53 +01:00
|
|
|
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-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-01 22:35:55 +01:00
|
|
|
|
|
|
|
ssl->method->next_message(ssl);
|
2017-08-14 20:08:34 +01:00
|
|
|
hs->state = state_read_certificate_request;
|
|
|
|
return ssl_hs_ok;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) {
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
2017-08-14 20:08:34 +01:00
|
|
|
|
|
|
|
if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
|
|
|
|
hs->state = state_read_server_hello_done;
|
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
SSLMessage msg;
|
2017-08-14 20:08:34 +01:00
|
|
|
if (!ssl->method->get_message(ssl, &msg)) {
|
|
|
|
return ssl_hs_read_message;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
if (msg.type == SSL3_MT_SERVER_HELLO_DONE) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// If we get here we don't need the handshake buffer as we won't be doing
|
|
|
|
// client auth.
|
2017-07-19 21:38:21 +01:00
|
|
|
hs->transcript.FreeBuffer();
|
2017-08-14 20:08:34 +01:00
|
|
|
hs->state = state_read_server_hello_done;
|
|
|
|
return ssl_hs_ok;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_REQUEST) ||
|
|
|
|
!ssl_hash_message(hs, msg)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Get the certificate types.
|
2017-08-01 21:32:25 +01:00
|
|
|
CBS body = msg.body, certificate_types;
|
|
|
|
if (!CBS_get_u8_length_prefixed(&body, &certificate_types)) {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_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);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-09-21 07:43:05 +01:00
|
|
|
if (!hs->certificate_types.CopyFrom(certificate_types)) {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 23:31:15 +01:00
|
|
|
if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
|
2014-12-18 03:06:57 +00:00
|
|
|
CBS supported_signature_algorithms;
|
2017-08-01 21:32:25 +01:00
|
|
|
if (!CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) ||
|
2016-11-17 08:20:47 +00:00
|
|
|
!tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_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);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 21:33:15 +00:00
|
|
|
uint8_t alert = SSL_AD_DECODE_ERROR;
|
2017-07-20 19:49:15 +01:00
|
|
|
UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names =
|
2017-08-01 21:32:25 +01:00
|
|
|
ssl_parse_client_CA_list(ssl, &alert, &body);
|
2017-07-20 19:49:15 +01:00
|
|
|
if (!ca_names) {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
if (CBS_len(&body) != 0) {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_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);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2016-09-09 16:41:18 +01:00
|
|
|
}
|
|
|
|
|
2017-08-31 19:49:09 +01:00
|
|
|
hs->cert_request = true;
|
2017-07-20 19:49:15 +01:00
|
|
|
hs->ca_names = std::move(ca_names);
|
2017-02-02 18:57:17 +00:00
|
|
|
ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
|
2017-08-14 20:08:34 +01:00
|
|
|
|
2017-08-01 22:35:55 +01:00
|
|
|
ssl->method->next_message(ssl);
|
2017-08-14 20:08:34 +01:00
|
|
|
hs->state = state_read_server_hello_done;
|
|
|
|
return ssl_hs_ok;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
static enum ssl_hs_wait_t do_read_server_hello_done(SSL_HANDSHAKE *hs) {
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
2017-08-01 21:32:25 +01:00
|
|
|
SSLMessage msg;
|
2017-08-14 20:08:34 +01:00
|
|
|
if (!ssl->method->get_message(ssl, &msg)) {
|
|
|
|
return ssl_hs_read_message;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO_DONE) ||
|
|
|
|
!ssl_hash_message(hs, msg)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2017-01-21 19:13:39 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// ServerHelloDone is empty.
|
2017-08-01 21:32:25 +01:00
|
|
|
if (CBS_len(&msg.body) != 0) {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_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);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2015-10-23 22:41:12 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 22:35:55 +01:00
|
|
|
ssl->method->next_message(ssl);
|
2017-08-14 20:08:34 +01:00
|
|
|
hs->state = state_send_client_certificate;
|
|
|
|
return ssl_hs_ok;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
2015-06-16 00:53:32 +01:00
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
2017-08-14 20:08:34 +01:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// The peer didn't request a certificate.
|
2017-08-14 20:08:34 +01:00
|
|
|
if (!hs->cert_request) {
|
|
|
|
hs->state = state_send_client_key_exchange;
|
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Call cert_cb to update the certificate.
|
2017-08-14 20:08:34 +01:00
|
|
|
if (ssl->cert->cert_cb != NULL) {
|
|
|
|
int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
|
|
|
|
if (rv == 0) {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
if (rv < 0) {
|
|
|
|
hs->state = state_send_client_certificate;
|
|
|
|
return ssl_hs_x509_lookup;
|
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)) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// Without a client certificate, the handshake buffer may be released.
|
2017-07-19 21:38:21 +01:00
|
|
|
hs->transcript.FreeBuffer();
|
2016-12-10 20:46:58 +00:00
|
|
|
|
2017-08-29 21:33:21 +01: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)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
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
|
|
|
}
|
2017-08-14 20:08:34 +01:00
|
|
|
hs->state = state_send_client_key_exchange;
|
|
|
|
return ssl_hs_ok;
|
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) ||
|
2017-10-06 23:43:53 +01:00
|
|
|
!ssl_output_cert_chain(ssl)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2016-12-10 20:46:58 +00:00
|
|
|
}
|
2017-08-14 20:08:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
hs->state = state_send_client_key_exchange;
|
|
|
|
return ssl_hs_ok;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-07-15 00:36:07 +01:00
|
|
|
static_assert(sizeof(size_t) >= sizeof(unsigned),
|
|
|
|
"size_t is smaller than unsigned");
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
static enum ssl_hs_wait_t do_send_client_key_exchange(SSL_HANDSHAKE *hs) {
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
Move libssl's internals into the bssl namespace.
This is horrible, but everything else I tried was worse. The goal with
this CL is to take the extern "C" out of ssl/internal.h and move most
symbols to namespace bssl, so we can start using C++ helpers and
destructors without worry.
Complications:
- Public API functions must be extern "C" and match their declaration in
ssl.h, which is unnamespaced. C++ really does not want you to
interleave namespaced and unnamespaced things. One can actually write
a namespaced extern "C" function, but this means, from C++'s
perspective, the function is namespaced. Trying to namespace the
public header would worked but ended up too deep a rabbithole.
- Our STACK_OF macros do not work right in namespaces.
- The typedefs for our exposed but opaque types are visible in the
header files and copied into consuming projects as forward
declarations. We ultimately want to give SSL a destructor, but
clobbering an unnamespaced ssl_st::~ssl_st seems bad manners.
- MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL.
This CL opts for:
- ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This
informs the public headers to create forward declarations which are
compatible with our namespaces.
- For now, C++-defined type FOO ends up at bssl::FOO with a typedef
outside. Later I imagine we'll rename many of them.
- Internal functions get namespace bssl, so we stop worrying about
stomping the tls1_prf symbol. Exported C functions are stuck as they
are. Rather than try anything weird, bite the bullet and reorder files
which have a mix of public and private functions. I expect that over
time, the public functions will become fairly small as we move logic
to more idiomatic C++.
Files without any public C functions can just be written normally.
- To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle
in advance of them being made idiomatic C++.
Bug: 132
Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581
Reviewed-on: https://boringssl-review.googlesource.com/18124
Reviewed-by: David Benjamin <davidben@google.com>
2017-07-18 21:34:25 +01:00
|
|
|
ScopedCBB cbb;
|
2017-07-12 21:25:57 +01:00
|
|
|
CBB body;
|
|
|
|
if (!ssl->method->init_message(ssl, cbb.get(), &body,
|
2016-06-17 23:48:29 +01:00
|
|
|
SSL3_MT_CLIENT_KEY_EXCHANGE)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2015-12-17 07:01:25 +00:00
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2017-07-22 17:45:38 +01:00
|
|
|
Array<uint8_t> pms;
|
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
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// If using a PSK key exchange, prepare the pre-shared key.
|
2015-12-17 07:01:25 +00:00
|
|
|
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);
|
2017-07-22 17:45:38 +01:00
|
|
|
return ssl_hs_error;
|
2015-12-17 07:01:25 +00:00
|
|
|
}
|
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 =
|
2017-07-20 19:49:15 +01:00
|
|
|
ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint.get(),
|
|
|
|
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);
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
|
2017-07-22 17:45:38 +01:00
|
|
|
return ssl_hs_error;
|
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);
|
2017-07-22 17:45:38 +01:00
|
|
|
return ssl_hs_error;
|
2015-12-17 07:01:25 +00:00
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Write out psk_identity.
|
2015-12-17 07:01:25 +00:00
|
|
|
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)) {
|
2017-07-22 17:45:38 +01:00
|
|
|
return ssl_hs_error;
|
2015-12-17 07:01:25 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2017-07-22 17:45:38 +01:00
|
|
|
// Depending on the key exchange method, compute |pms|.
|
2015-12-17 07:01:25 +00:00
|
|
|
if (alg_k & SSL_kRSA) {
|
2017-07-22 17:45:38 +01:00
|
|
|
if (!pms.Init(SSL_MAX_MASTER_KEY_LENGTH)) {
|
|
|
|
return ssl_hs_error;
|
2015-12-17 07:01:25 +00:00
|
|
|
}
|
2015-11-20 22:47:25 +00:00
|
|
|
|
2017-07-20 19:49:15 +01:00
|
|
|
RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey.get());
|
2015-12-17 07:01:25 +00:00
|
|
|
if (rsa == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2017-07-22 17:45:38 +01:00
|
|
|
return ssl_hs_error;
|
2015-12-17 07:01:25 +00:00
|
|
|
}
|
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)) {
|
2017-07-22 17:45:38 +01:00
|
|
|
return ssl_hs_error;
|
2015-12-17 07:01:25 +00:00
|
|
|
}
|
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;
|
2017-08-29 21:33:21 +01:00
|
|
|
// In TLS, there is a length prefix.
|
2015-12-17 07:01:25 +00:00
|
|
|
if (ssl->version > SSL3_VERSION) {
|
2016-06-17 23:48:29 +01:00
|
|
|
if (!CBB_add_u16_length_prefixed(&body, &child)) {
|
2017-07-22 17:45:38 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
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)) ||
|
2017-07-22 17:45:38 +01:00
|
|
|
!RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms.data(),
|
|
|
|
pms.size(), RSA_PKCS1_PADDING) ||
|
2015-12-17 07:01:25 +00:00
|
|
|
!CBB_did_write(enc_pms, enc_pms_len) ||
|
2016-06-17 23:48:29 +01:00
|
|
|
!CBB_flush(&body)) {
|
2017-07-22 17:45:38 +01:00
|
|
|
return ssl_hs_error;
|
2015-12-17 07:01:25 +00:00
|
|
|
}
|
2017-04-10 23:52:14 +01:00
|
|
|
} else if (alg_k & SSL_kECDHE) {
|
2017-08-29 21:33:21 +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)) {
|
2017-07-22 17:45:38 +01:00
|
|
|
return ssl_hs_error;
|
2015-12-17 07:01:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Compute the premaster.
|
2017-02-08 21:33:15 +00:00
|
|
|
uint8_t alert = SSL_AD_DECODE_ERROR;
|
2017-07-22 17:45:38 +01:00
|
|
|
if (!hs->key_share->Accept(&child, &pms, &alert, hs->peer_key)) {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
|
2017-07-22 17:45:38 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2016-06-17 23:48:29 +01:00
|
|
|
if (!CBB_flush(&body)) {
|
2017-07-22 17:45:38 +01:00
|
|
|
return ssl_hs_error;
|
2016-05-16 22:27:14 +01:00
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// The key exchange state may now be discarded.
|
2017-07-20 04:28:43 +01:00
|
|
|
hs->key_share.reset();
|
2017-07-22 17:45:38 +01:00
|
|
|
hs->peer_key.Reset();
|
2015-12-17 07:01:25 +00:00
|
|
|
} else if (alg_k & SSL_kPSK) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// For plain PSK, other_secret is a block of 0s with the same length as
|
|
|
|
// the pre-shared key.
|
2017-07-22 17:45:38 +01:00
|
|
|
if (!pms.Init(psk_len)) {
|
|
|
|
return ssl_hs_error;
|
2015-02-17 00:33:53 +00:00
|
|
|
}
|
2017-07-22 17:45:38 +01:00
|
|
|
OPENSSL_memset(pms.data(), 0, pms.size());
|
2015-12-17 07:01:25 +00:00
|
|
|
} else {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
|
2015-12-17 07:01:25 +00:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2017-07-22 17:45:38 +01:00
|
|
|
return ssl_hs_error;
|
2015-12-17 07:01:25 +00:00
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// For a PSK cipher suite, other_secret is combined with the pre-shared
|
|
|
|
// key.
|
2015-12-17 07:01:25 +00:00
|
|
|
if (alg_a & SSL_aPSK) {
|
2017-07-20 04:57:40 +01:00
|
|
|
ScopedCBB pms_cbb;
|
|
|
|
CBB child;
|
2017-07-22 17:45:38 +01:00
|
|
|
if (!CBB_init(pms_cbb.get(), 2 + psk_len + 2 + pms.size()) ||
|
2017-07-20 04:57:40 +01:00
|
|
|
!CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
|
2017-07-22 17:45:38 +01:00
|
|
|
!CBB_add_bytes(&child, pms.data(), pms.size()) ||
|
2017-07-20 04:57:40 +01:00
|
|
|
!CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
|
2015-12-17 07:01:25 +00:00
|
|
|
!CBB_add_bytes(&child, psk, psk_len) ||
|
2017-10-11 22:19:19 +01:00
|
|
|
!CBBFinishArray(pms_cbb.get(), &pms)) {
|
2015-12-17 07:01:25 +00:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
2017-07-22 17:45:38 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-17 07:01:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// The message must be added to the finished hash before calculating the
|
|
|
|
// master secret.
|
2017-07-12 21:25:57 +01:00
|
|
|
if (!ssl_add_message_cbb(ssl, cbb.get())) {
|
2017-07-22 17:45:38 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-17 07:01:25 +00:00
|
|
|
|
2017-10-27 05:56:55 +01:00
|
|
|
hs->new_session->master_key_length =
|
|
|
|
tls1_generate_master_secret(hs, hs->new_session->master_key, pms);
|
2017-02-10 01:01:26 +00:00
|
|
|
if (hs->new_session->master_key_length == 0) {
|
2017-07-22 17:45:38 +01:00
|
|
|
return ssl_hs_error;
|
2015-12-17 07:01:25 +00:00
|
|
|
}
|
2017-02-17 21:26:01 +00:00
|
|
|
hs->new_session->extended_master_secret = hs->extended_master_secret;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
hs->state = state_send_client_certificate_verify;
|
|
|
|
return ssl_hs_ok;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
2016-06-17 22:33:30 +01:00
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
if (!hs->cert_request || !ssl_has_certificate(ssl)) {
|
2017-08-31 19:52:31 +01:00
|
|
|
hs->state = state_send_client_finished;
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(ssl_has_private_key(ssl));
|
Move libssl's internals into the bssl namespace.
This is horrible, but everything else I tried was worse. The goal with
this CL is to take the extern "C" out of ssl/internal.h and move most
symbols to namespace bssl, so we can start using C++ helpers and
destructors without worry.
Complications:
- Public API functions must be extern "C" and match their declaration in
ssl.h, which is unnamespaced. C++ really does not want you to
interleave namespaced and unnamespaced things. One can actually write
a namespaced extern "C" function, but this means, from C++'s
perspective, the function is namespaced. Trying to namespace the
public header would worked but ended up too deep a rabbithole.
- Our STACK_OF macros do not work right in namespaces.
- The typedefs for our exposed but opaque types are visible in the
header files and copied into consuming projects as forward
declarations. We ultimately want to give SSL a destructor, but
clobbering an unnamespaced ssl_st::~ssl_st seems bad manners.
- MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL.
This CL opts for:
- ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This
informs the public headers to create forward declarations which are
compatible with our namespaces.
- For now, C++-defined type FOO ends up at bssl::FOO with a typedef
outside. Later I imagine we'll rename many of them.
- Internal functions get namespace bssl, so we stop worrying about
stomping the tls1_prf symbol. Exported C functions are stuck as they
are. Rather than try anything weird, bite the bullet and reorder files
which have a mix of public and private functions. I expect that over
time, the public functions will become fairly small as we move logic
to more idiomatic C++.
Files without any public C functions can just be written normally.
- To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle
in advance of them being made idiomatic C++.
Bug: 132
Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581
Reviewed-on: https://boringssl-review.googlesource.com/18124
Reviewed-by: David Benjamin <davidben@google.com>
2017-07-18 21:34:25 +01:00
|
|
|
ScopedCBB cbb;
|
2017-07-12 21:25:57 +01:00
|
|
|
CBB body, child;
|
|
|
|
if (!ssl->method->init_message(ssl, cbb.get(), &body,
|
2016-06-17 23:48:29 +01:00
|
|
|
SSL3_MT_CERTIFICATE_VERIFY)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2015-12-18 06:57:43 +00:00
|
|
|
}
|
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)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2016-07-08 23:52:59 +01:00
|
|
|
}
|
2017-10-06 23:31:15 +01:00
|
|
|
if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// Write out the digest type in TLS 1.2.
|
2016-06-29 18:16:27 +01:00
|
|
|
if (!CBB_add_u16(&body, signature_algorithm)) {
|
2016-06-17 22:33:30 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2016-06-17 22:33:30 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Set aside space for the signature.
|
2017-07-20 19:49:15 +01:00
|
|
|
const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
|
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)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2016-06-17 22:33:30 +01:00
|
|
|
}
|
|
|
|
|
2016-06-30 18:27:23 +01:00
|
|
|
size_t sig_len = max_sig_len;
|
2017-08-29 21:33:21 +01:00
|
|
|
// The SSL3 construction for CertificateVerify does not decompose into a
|
|
|
|
// single final digest and signature, and must be special-cased.
|
2017-10-06 23:31:15 +01:00
|
|
|
if (ssl_protocol_version(ssl) == SSL3_VERSION) {
|
2017-06-17 18:20:59 +01:00
|
|
|
if (ssl->cert->key_method != NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-18 06:57:43 +00:00
|
|
|
|
2017-06-17 18:20:59 +01:00
|
|
|
uint8_t digest[EVP_MAX_MD_SIZE];
|
|
|
|
size_t digest_len;
|
2017-07-19 21:38:21 +01:00
|
|
|
if (!hs->transcript.GetSSL3CertVerifyHash(
|
|
|
|
digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2017-06-17 18:20:59 +01:00
|
|
|
}
|
|
|
|
|
2017-07-20 04:57:40 +01:00
|
|
|
UniquePtr<EVP_PKEY_CTX> pctx(EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL));
|
|
|
|
if (!pctx ||
|
|
|
|
!EVP_PKEY_sign_init(pctx.get()) ||
|
|
|
|
!EVP_PKEY_sign(pctx.get(), ptr, &sig_len, digest, digest_len)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2017-06-17 18:20:59 +01:00
|
|
|
}
|
|
|
|
} else {
|
2017-10-11 22:19:19 +01:00
|
|
|
switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
|
|
|
|
signature_algorithm,
|
|
|
|
hs->transcript.buffer())) {
|
2017-06-17 18:20:59 +01:00
|
|
|
case ssl_private_key_success:
|
|
|
|
break;
|
|
|
|
case ssl_private_key_failure:
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2017-06-17 18:20:59 +01:00
|
|
|
case ssl_private_key_retry:
|
2017-08-14 20:08:34 +01:00
|
|
|
hs->state = state_send_client_certificate_verify;
|
|
|
|
return ssl_hs_private_key_operation;
|
2017-06-17 18:20:59 +01:00
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 06:57:43 +00:00
|
|
|
if (!CBB_did_write(&child, sig_len) ||
|
2017-07-12 21:25:57 +01:00
|
|
|
!ssl_add_message_cbb(ssl, cbb.get())) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2015-12-18 06:57:43 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// The handshake buffer is no longer necessary.
|
2017-07-19 21:38:21 +01:00
|
|
|
hs->transcript.FreeBuffer();
|
2017-08-14 20:08:34 +01:00
|
|
|
|
2017-08-31 19:52:31 +01:00
|
|
|
hs->state = state_send_client_finished;
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_ok;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-08-31 19:52:31 +01:00
|
|
|
static enum ssl_hs_wait_t do_send_client_finished(SSL_HANDSHAKE *hs) {
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
2017-08-31 19:52:31 +01:00
|
|
|
// Resolve Channel ID first, before any non-idempotent operations.
|
|
|
|
if (ssl->s3->tlsext_channel_id_valid) {
|
|
|
|
if (!ssl_do_channel_id_callback(ssl)) {
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl->tlsext_channel_id_private == NULL) {
|
|
|
|
hs->state = state_send_client_finished;
|
|
|
|
return ssl_hs_channel_id_lookup;
|
|
|
|
}
|
|
|
|
}
|
2015-10-10 19:59:09 +01:00
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
if (!ssl->method->add_change_cipher_spec(ssl) ||
|
2017-09-22 00:07:15 +01:00
|
|
|
!tls1_change_cipher_state(hs, evp_aead_seal)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2015-10-10 19:59:09 +01:00
|
|
|
}
|
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
if (hs->next_proto_neg_seen) {
|
|
|
|
static const uint8_t kZero[32] = {0};
|
2017-10-13 22:18:35 +01:00
|
|
|
size_t padding_len =
|
|
|
|
32 - ((ssl->s3->next_proto_negotiated.size() + 2) % 32);
|
2017-08-14 20:08:34 +01:00
|
|
|
|
|
|
|
ScopedCBB cbb;
|
|
|
|
CBB body, child;
|
|
|
|
if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_NEXT_PROTO) ||
|
|
|
|
!CBB_add_u8_length_prefixed(&body, &child) ||
|
2017-10-13 22:18:35 +01:00
|
|
|
!CBB_add_bytes(&child, ssl->s3->next_proto_negotiated.data(),
|
|
|
|
ssl->s3->next_proto_negotiated.size()) ||
|
2017-08-14 20:08:34 +01:00
|
|
|
!CBB_add_u8_length_prefixed(&body, &child) ||
|
|
|
|
!CBB_add_bytes(&child, kZero, padding_len) ||
|
|
|
|
!ssl_add_message_cbb(ssl, cbb.get())) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 19:52:31 +01:00
|
|
|
if (ssl->s3->tlsext_channel_id_valid) {
|
|
|
|
ScopedCBB cbb;
|
|
|
|
CBB body;
|
|
|
|
if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
|
|
|
|
!tls1_write_channel_id(hs, &body) ||
|
|
|
|
!ssl_add_message_cbb(ssl, cbb.get())) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 23:43:53 +01:00
|
|
|
if (!ssl_send_finished(hs)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
hs->state = state_finish_flight;
|
|
|
|
return ssl_hs_flush;
|
|
|
|
}
|
|
|
|
|
2017-10-06 23:43:53 +01:00
|
|
|
static bool can_false_start(const SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
|
|
|
|
2017-12-01 18:05:12 +00:00
|
|
|
// False Start only for TLS 1.2 with an ECDHE+AEAD cipher.
|
|
|
|
if (SSL_is_dtls(ssl) ||
|
|
|
|
SSL_version(ssl) != TLS1_2_VERSION ||
|
|
|
|
hs->new_cipher->algorithm_mkey != SSL_kECDHE ||
|
|
|
|
hs->new_cipher->algorithm_mac != SSL_AEAD) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Additionally require ALPN or NPN by default.
|
|
|
|
//
|
|
|
|
// TODO(davidben): Can this constraint be relaxed globally now that cipher
|
|
|
|
// suite requirements have been relaxed?
|
|
|
|
if (!ssl->ctx->false_start_allowed_without_alpn &&
|
|
|
|
ssl->s3->alpn_selected.empty() &&
|
|
|
|
ssl->s3->next_proto_negotiated.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2017-10-06 23:43:53 +01:00
|
|
|
}
|
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
static enum ssl_hs_wait_t do_finish_flight(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
|
|
|
if (ssl->session != NULL) {
|
|
|
|
hs->state = state_finish_client_handshake;
|
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// This is a full 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-08-14 20:08:34 +01:00
|
|
|
if (!tls1_record_handshake_hashes_for_channel_id(hs)) {
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
hs->state = state_read_session_ticket;
|
|
|
|
|
|
|
|
if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
|
2017-10-06 23:43:53 +01:00
|
|
|
can_false_start(hs) &&
|
2017-08-29 21:33:21 +01:00
|
|
|
// No False Start on renegotiation (would complicate the state machine).
|
2017-08-14 20:08:34 +01:00
|
|
|
!ssl->s3->initial_handshake_complete) {
|
2017-08-31 19:49:09 +01:00
|
|
|
hs->in_false_start = true;
|
|
|
|
hs->can_early_write = true;
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_early_return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ssl_hs_ok;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
static enum ssl_hs_wait_t do_read_session_ticket(SSL_HANDSHAKE *hs) {
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
2017-08-14 20:08:34 +01:00
|
|
|
|
|
|
|
if (!hs->ticket_expected) {
|
|
|
|
hs->state = state_process_change_cipher_spec;
|
|
|
|
return ssl_hs_read_change_cipher_spec;
|
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
SSLMessage msg;
|
2017-08-14 20:08:34 +01:00
|
|
|
if (!ssl->method->get_message(ssl, &msg)) {
|
|
|
|
return ssl_hs_read_message;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2016-03-10 13:43:27 +00:00
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEW_SESSION_TICKET) ||
|
|
|
|
!ssl_hash_message(hs, msg)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2017-01-21 19:13:39 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 21:32:25 +01:00
|
|
|
CBS new_session_ticket = msg.body, ticket;
|
2016-08-09 18:18:40 +01:00
|
|
|
uint32_t tlsext_tick_lifetime_hint;
|
|
|
|
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) {
|
2017-10-06 23:31:15 +01:00
|
|
|
ssl_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);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2016-03-10 13:43:27 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
if (CBS_len(&ticket) == 0) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// RFC 5077 allows a server to change its mind and send no ticket after
|
|
|
|
// negotiating the extension. The value of |ticket_expected| is checked in
|
|
|
|
// |ssl_update_cache| so is cleared here to avoid an unnecessary update.
|
2017-08-31 19:49:09 +01:00
|
|
|
hs->ticket_expected = false;
|
2017-08-01 22:35:55 +01:00
|
|
|
ssl->method->next_message(ssl);
|
2017-08-14 20:08:34 +01:00
|
|
|
hs->state = state_process_change_cipher_spec;
|
|
|
|
return ssl_hs_read_change_cipher_spec;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
2015-08-18 12:21:54 +01:00
|
|
|
|
2017-07-20 19:49:15 +01:00
|
|
|
SSL_SESSION *session = hs->new_session.get();
|
2017-07-20 04:57:40 +01:00
|
|
|
UniquePtr<SSL_SESSION> renewed_session;
|
|
|
|
if (ssl->session != NULL) {
|
2017-08-29 21:33:21 +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.
|
2017-07-20 19:49:15 +01:00
|
|
|
renewed_session =
|
|
|
|
SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
|
2017-07-20 04:57:40 +01:00
|
|
|
if (!renewed_session) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// This should never happen.
|
2016-06-07 22:03:46 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
2017-07-20 04:57:40 +01:00
|
|
|
session = renewed_session.get();
|
2015-08-18 12:21:54 +01:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01: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);
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
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
|
|
|
|
2017-08-29 21:33:21 +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)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2017-07-20 04:57:40 +01:00
|
|
|
if (renewed_session) {
|
2016-06-27 21:34:59 +01:00
|
|
|
session->not_resumable = 0;
|
|
|
|
SSL_SESSION_free(ssl->session);
|
2017-07-20 04:57:40 +01:00
|
|
|
ssl->session = renewed_session.release();
|
2016-06-27 21:34:59 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 22:35:55 +01:00
|
|
|
ssl->method->next_message(ssl);
|
2017-08-14 20:08:34 +01:00
|
|
|
hs->state = state_process_change_cipher_spec;
|
|
|
|
return ssl_hs_read_change_cipher_spec;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
|
2017-09-22 00:07:15 +01:00
|
|
|
if (!tls1_change_cipher_state(hs, evp_aead_open)) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
hs->state = state_read_server_finished;
|
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
|
|
|
enum ssl_hs_wait_t wait = ssl_get_finished(hs);
|
|
|
|
if (wait != ssl_hs_ok) {
|
|
|
|
return wait;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl->session != NULL) {
|
2017-08-31 19:52:31 +01:00
|
|
|
hs->state = state_send_client_finished;
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
hs->state = state_finish_client_handshake;
|
|
|
|
return ssl_hs_ok;
|
2015-08-18 12:21:54 +01:00
|
|
|
}
|
Move libssl's internals into the bssl namespace.
This is horrible, but everything else I tried was worse. The goal with
this CL is to take the extern "C" out of ssl/internal.h and move most
symbols to namespace bssl, so we can start using C++ helpers and
destructors without worry.
Complications:
- Public API functions must be extern "C" and match their declaration in
ssl.h, which is unnamespaced. C++ really does not want you to
interleave namespaced and unnamespaced things. One can actually write
a namespaced extern "C" function, but this means, from C++'s
perspective, the function is namespaced. Trying to namespace the
public header would worked but ended up too deep a rabbithole.
- Our STACK_OF macros do not work right in namespaces.
- The typedefs for our exposed but opaque types are visible in the
header files and copied into consuming projects as forward
declarations. We ultimately want to give SSL a destructor, but
clobbering an unnamespaced ssl_st::~ssl_st seems bad manners.
- MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL.
This CL opts for:
- ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This
informs the public headers to create forward declarations which are
compatible with our namespaces.
- For now, C++-defined type FOO ends up at bssl::FOO with a typedef
outside. Later I imagine we'll rename many of them.
- Internal functions get namespace bssl, so we stop worrying about
stomping the tls1_prf symbol. Exported C functions are stuck as they
are. Rather than try anything weird, bite the bullet and reorder files
which have a mix of public and private functions. I expect that over
time, the public functions will become fairly small as we move logic
to more idiomatic C++.
Files without any public C functions can just be written normally.
- To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle
in advance of them being made idiomatic C++.
Bug: 132
Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581
Reviewed-on: https://boringssl-review.googlesource.com/18124
Reviewed-by: David Benjamin <davidben@google.com>
2017-07-18 21:34:25 +01:00
|
|
|
|
2017-08-14 20:08:34 +01:00
|
|
|
static enum ssl_hs_wait_t do_finish_client_handshake(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
|
|
|
|
|
|
|
ssl->method->on_handshake_complete(ssl);
|
|
|
|
|
|
|
|
if (ssl->session != NULL) {
|
|
|
|
SSL_SESSION_up_ref(ssl->session);
|
2017-10-13 22:18:35 +01:00
|
|
|
ssl->s3->established_session.reset(ssl->session);
|
2017-08-14 20:08:34 +01:00
|
|
|
} else {
|
2017-08-29 21:33:21 +01:00
|
|
|
// 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.
|
2017-08-14 20:08:34 +01:00
|
|
|
ssl->s3->established_session =
|
2017-10-13 22:18:35 +01:00
|
|
|
SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_DUP_ALL);
|
|
|
|
if (!ssl->s3->established_session) {
|
2017-08-14 20:08:34 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
2017-08-29 21:33:21 +01:00
|
|
|
// Renegotiations do not participate in session resumption.
|
2017-08-14 20:08:34 +01:00
|
|
|
if (!ssl->s3->initial_handshake_complete) {
|
|
|
|
ssl->s3->established_session->not_resumable = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
hs->new_session.reset();
|
|
|
|
}
|
|
|
|
|
2017-08-31 19:49:09 +01:00
|
|
|
hs->handshake_finalized = true;
|
2017-08-31 20:06:42 +01:00
|
|
|
ssl->s3->initial_handshake_complete = true;
|
2017-08-14 20:08:34 +01:00
|
|
|
ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
|
|
|
|
|
|
|
|
hs->state = state_done;
|
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs) {
|
|
|
|
while (hs->state != state_done) {
|
|
|
|
enum ssl_hs_wait_t ret = ssl_hs_error;
|
|
|
|
enum ssl_client_hs_state_t state =
|
|
|
|
static_cast<enum ssl_client_hs_state_t>(hs->state);
|
|
|
|
switch (state) {
|
|
|
|
case state_start_connect:
|
|
|
|
ret = do_start_connect(hs);
|
|
|
|
break;
|
|
|
|
case state_enter_early_data:
|
|
|
|
ret = do_enter_early_data(hs);
|
|
|
|
break;
|
|
|
|
case state_read_hello_verify_request:
|
|
|
|
ret = do_read_hello_verify_request(hs);
|
|
|
|
break;
|
|
|
|
case state_read_server_hello:
|
|
|
|
ret = do_read_server_hello(hs);
|
|
|
|
break;
|
|
|
|
case state_tls13:
|
|
|
|
ret = do_tls13(hs);
|
|
|
|
break;
|
|
|
|
case state_read_server_certificate:
|
|
|
|
ret = do_read_server_certificate(hs);
|
|
|
|
break;
|
|
|
|
case state_read_certificate_status:
|
|
|
|
ret = do_read_certificate_status(hs);
|
|
|
|
break;
|
|
|
|
case state_verify_server_certificate:
|
|
|
|
ret = do_verify_server_certificate(hs);
|
|
|
|
break;
|
|
|
|
case state_read_server_key_exchange:
|
|
|
|
ret = do_read_server_key_exchange(hs);
|
|
|
|
break;
|
|
|
|
case state_read_certificate_request:
|
|
|
|
ret = do_read_certificate_request(hs);
|
|
|
|
break;
|
|
|
|
case state_read_server_hello_done:
|
|
|
|
ret = do_read_server_hello_done(hs);
|
|
|
|
break;
|
|
|
|
case state_send_client_certificate:
|
|
|
|
ret = do_send_client_certificate(hs);
|
|
|
|
break;
|
|
|
|
case state_send_client_key_exchange:
|
|
|
|
ret = do_send_client_key_exchange(hs);
|
|
|
|
break;
|
|
|
|
case state_send_client_certificate_verify:
|
|
|
|
ret = do_send_client_certificate_verify(hs);
|
|
|
|
break;
|
|
|
|
case state_send_client_finished:
|
|
|
|
ret = do_send_client_finished(hs);
|
|
|
|
break;
|
|
|
|
case state_finish_flight:
|
|
|
|
ret = do_finish_flight(hs);
|
|
|
|
break;
|
|
|
|
case state_read_session_ticket:
|
|
|
|
ret = do_read_session_ticket(hs);
|
|
|
|
break;
|
|
|
|
case state_process_change_cipher_spec:
|
|
|
|
ret = do_process_change_cipher_spec(hs);
|
|
|
|
break;
|
|
|
|
case state_read_server_finished:
|
|
|
|
ret = do_read_server_finished(hs);
|
|
|
|
break;
|
|
|
|
case state_finish_client_handshake:
|
|
|
|
ret = do_finish_client_handshake(hs);
|
|
|
|
break;
|
|
|
|
case state_done:
|
|
|
|
ret = ssl_hs_ok;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hs->state != state) {
|
|
|
|
ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != ssl_hs_ok) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
|
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs) {
|
|
|
|
enum ssl_client_hs_state_t state =
|
|
|
|
static_cast<enum ssl_client_hs_state_t>(hs->state);
|
|
|
|
switch (state) {
|
|
|
|
case state_start_connect:
|
|
|
|
return "TLS client start_connect";
|
|
|
|
case state_enter_early_data:
|
|
|
|
return "TLS client enter_early_data";
|
|
|
|
case state_read_hello_verify_request:
|
|
|
|
return "TLS client read_hello_verify_request";
|
|
|
|
case state_read_server_hello:
|
|
|
|
return "TLS client read_server_hello";
|
|
|
|
case state_tls13:
|
|
|
|
return tls13_client_handshake_state(hs);
|
|
|
|
case state_read_server_certificate:
|
|
|
|
return "TLS client read_server_certificate";
|
|
|
|
case state_read_certificate_status:
|
|
|
|
return "TLS client read_certificate_status";
|
|
|
|
case state_verify_server_certificate:
|
|
|
|
return "TLS client verify_server_certificate";
|
|
|
|
case state_read_server_key_exchange:
|
|
|
|
return "TLS client read_server_key_exchange";
|
|
|
|
case state_read_certificate_request:
|
|
|
|
return "TLS client read_certificate_request";
|
|
|
|
case state_read_server_hello_done:
|
|
|
|
return "TLS client read_server_hello_done";
|
|
|
|
case state_send_client_certificate:
|
|
|
|
return "TLS client send_client_certificate";
|
|
|
|
case state_send_client_key_exchange:
|
|
|
|
return "TLS client send_client_key_exchange";
|
|
|
|
case state_send_client_certificate_verify:
|
|
|
|
return "TLS client send_client_certificate_verify";
|
|
|
|
case state_send_client_finished:
|
|
|
|
return "TLS client send_client_finished";
|
|
|
|
case state_finish_flight:
|
|
|
|
return "TLS client finish_flight";
|
|
|
|
case state_read_session_ticket:
|
|
|
|
return "TLS client read_session_ticket";
|
|
|
|
case state_process_change_cipher_spec:
|
|
|
|
return "TLS client process_change_cipher_spec";
|
|
|
|
case state_read_server_finished:
|
|
|
|
return "TLS client read_server_finished";
|
|
|
|
case state_finish_client_handshake:
|
|
|
|
return "TLS client finish_client_handshake";
|
|
|
|
case state_done:
|
|
|
|
return "TLS client done";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "TLS client unknown";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|