2014-06-20 20:00:00 +01:00
|
|
|
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This package is an SSL implementation written
|
|
|
|
* by Eric Young (eay@cryptsoft.com).
|
|
|
|
* The implementation was written so as to conform with Netscapes SSL.
|
|
|
|
*
|
|
|
|
* This library is free for commercial and non-commercial use as long as
|
|
|
|
* the following conditions are aheared to. The following conditions
|
|
|
|
* apply to all code found in this distribution, be it the RC4, RSA,
|
|
|
|
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
|
|
|
* included with this distribution is covered by the same copyright terms
|
|
|
|
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
|
|
|
*
|
|
|
|
* Copyright remains Eric Young's, and as such any Copyright notices in
|
|
|
|
* the code are not to be removed.
|
|
|
|
* If this package is used in a product, Eric Young should be given attribution
|
|
|
|
* as the author of the parts of the library used.
|
|
|
|
* This can be in the form of a textual message at program startup or
|
|
|
|
* in documentation (online or textual) provided with the package.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* "This product includes cryptographic software written by
|
|
|
|
* Eric Young (eay@cryptsoft.com)"
|
|
|
|
* The word 'cryptographic' can be left out if the rouines from the library
|
|
|
|
* being used are not cryptographic related :-).
|
|
|
|
* 4. If you include any Windows specific code (or a derivative thereof) from
|
|
|
|
* the apps directory (application code) you must include an acknowledgement:
|
|
|
|
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* The licence and distribution terms for any publically available version or
|
|
|
|
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
|
|
|
* copied and put under another distribution licence
|
|
|
|
* [including the GNU Public Licence.]
|
|
|
|
*/
|
|
|
|
/* ====================================================================
|
|
|
|
* Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. All advertising materials mentioning features or use of this
|
|
|
|
* software must display the following acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
|
|
|
|
*
|
|
|
|
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* prior written permission. For written permission, please contact
|
|
|
|
* openssl-core@openssl.org.
|
|
|
|
*
|
|
|
|
* 5. Products derived from this software may not be called "OpenSSL"
|
|
|
|
* nor may "OpenSSL" appear in their names without prior written
|
|
|
|
* permission of the OpenSSL Project.
|
|
|
|
*
|
|
|
|
* 6. Redistributions of any form whatsoever must retain the following
|
|
|
|
* acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
|
|
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
|
|
|
*
|
|
|
|
* This product includes cryptographic software written by Eric Young
|
|
|
|
* (eay@cryptsoft.com). This product includes software written by Tim
|
|
|
|
* Hudson (tjh@cryptsoft.com).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/* ====================================================================
|
|
|
|
* Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
|
|
|
|
*
|
|
|
|
* Portions of the attached software ("Contribution") are developed by
|
|
|
|
* SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
|
|
|
|
*
|
|
|
|
* The Contribution is licensed pursuant to the OpenSSL open source
|
|
|
|
* license provided above.
|
|
|
|
*
|
|
|
|
* ECC cipher suite support in OpenSSL originally written by
|
|
|
|
* Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/* ====================================================================
|
|
|
|
* Copyright 2005 Nokia. All rights reserved.
|
|
|
|
*
|
|
|
|
* The portions of the attached software ("Contribution") is developed by
|
|
|
|
* Nokia Corporation and is licensed pursuant to the OpenSSL open source
|
|
|
|
* license.
|
|
|
|
*
|
|
|
|
* The Contribution, originally written by Mika Kousa and Pasi Eronen of
|
|
|
|
* Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
|
|
|
|
* support (see RFC 4279) to OpenSSL.
|
|
|
|
*
|
|
|
|
* No patent licenses or other rights except those expressly stated in
|
|
|
|
* the OpenSSL open source license shall be deemed granted or received
|
|
|
|
* expressly, by implication, estoppel, or otherwise.
|
|
|
|
*
|
|
|
|
* No assurances are provided by Nokia that the Contribution does not
|
|
|
|
* infringe the patent or other intellectual property rights of any third
|
|
|
|
* party or that the license provides you with all the necessary rights
|
|
|
|
* to make use of the Contribution.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
|
|
|
|
* ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
|
|
|
|
* SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
|
|
|
|
* OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
|
|
|
|
* OTHERWISE. */
|
|
|
|
|
2015-09-15 06:48:04 +01:00
|
|
|
#include <openssl/ssl.h>
|
|
|
|
|
2014-08-26 03:35:07 +01:00
|
|
|
#include <assert.h>
|
2014-07-13 17:29:21 +01:00
|
|
|
#include <string.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/buf.h>
|
2014-06-25 17:36:10 +01:00
|
|
|
#include <openssl/bytestring.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/cipher.h>
|
|
|
|
#include <openssl/dh.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/ec.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/hmac.h>
|
|
|
|
#include <openssl/md5.h>
|
|
|
|
#include <openssl/mem.h>
|
2016-03-25 22:07:11 +00:00
|
|
|
#include <openssl/nid.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/x509.h>
|
|
|
|
|
2015-04-08 03:38:30 +01:00
|
|
|
#include "internal.h"
|
2014-11-04 03:34:49 +00:00
|
|
|
#include "../crypto/internal.h"
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2017-02-03 04:51:22 +00:00
|
|
|
static int ssl3_process_client_hello(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_select_certificate(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_select_parameters(SSL_HANDSHAKE *hs);
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_send_server_hello(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_get_next_proto(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_get_channel_id(SSL_HANDSHAKE *hs);
|
|
|
|
static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs);
|
2016-06-07 21:49:42 +01:00
|
|
|
|
2017-01-25 20:47:49 +00:00
|
|
|
static struct CRYPTO_STATIC_MUTEX g_v2clienthello_lock =
|
|
|
|
CRYPTO_STATIC_MUTEX_INIT;
|
|
|
|
static uint64_t g_v2clienthello_count = 0;
|
|
|
|
|
|
|
|
uint64_t SSL_get_v2clienthello_count(void) {
|
|
|
|
CRYPTO_STATIC_MUTEX_lock_read(&g_v2clienthello_lock);
|
|
|
|
uint64_t ret = g_v2clienthello_count;
|
|
|
|
CRYPTO_STATIC_MUTEX_unlock_read(&g_v2clienthello_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:45:16 +00:00
|
|
|
int ssl3_accept(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2015-04-18 03:32:19 +01:00
|
|
|
uint32_t alg_a;
|
2014-12-19 01:42:32 +00:00
|
|
|
int ret = -1;
|
2016-06-02 01:16:03 +01:00
|
|
|
int state, skip = 0;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
assert(ssl->handshake_func == ssl3_accept);
|
|
|
|
assert(ssl->server);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
for (;;) {
|
2016-12-12 22:00:50 +00:00
|
|
|
state = hs->state;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-12-12 22:00:50 +00:00
|
|
|
switch (hs->state) {
|
2016-09-01 06:33:33 +01:00
|
|
|
case SSL_ST_INIT:
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL_ST_ACCEPT;
|
2016-09-01 06:33:33 +01:00
|
|
|
skip = 1;
|
|
|
|
break;
|
|
|
|
|
2014-12-19 01:42:32 +00:00
|
|
|
case SSL_ST_ACCEPT:
|
2016-06-02 01:16:03 +01:00
|
|
|
ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!ssl3_init_handshake_buffer(ssl)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2014-12-19 01:42:32 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SR_CLNT_HELLO_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SR_CLNT_HELLO_A:
|
2017-02-03 04:51:22 +00:00
|
|
|
ret = ssl->method->ssl_get_message(ssl);
|
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
hs->state = SSL3_ST_SR_CLNT_HELLO_B;
|
|
|
|
break;
|
|
|
|
|
2014-12-19 01:42:32 +00:00
|
|
|
case SSL3_ST_SR_CLNT_HELLO_B:
|
2017-02-03 04:51:22 +00:00
|
|
|
ret = ssl3_process_client_hello(hs);
|
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
hs->state = SSL3_ST_SR_CLNT_HELLO_C;
|
|
|
|
break;
|
|
|
|
|
2014-12-19 01:42:32 +00:00
|
|
|
case SSL3_ST_SR_CLNT_HELLO_C:
|
2017-02-03 04:51:22 +00:00
|
|
|
ret = ssl3_select_certificate(hs);
|
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (hs->state != SSL_ST_TLS13) {
|
|
|
|
hs->state = SSL3_ST_SR_CLNT_HELLO_D;
|
2016-07-11 18:19:03 +01:00
|
|
|
}
|
2017-02-03 04:51:22 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SR_CLNT_HELLO_D:
|
|
|
|
ret = ssl3_select_parameters(hs);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-06-07 21:49:42 +01:00
|
|
|
ssl->method->received_flight(ssl);
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SW_SRVR_HELLO_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SW_SRVR_HELLO_A:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_send_server_hello(hs);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-06-27 21:34:59 +01:00
|
|
|
if (ssl->session != NULL) {
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SW_SESSION_TICKET_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
} else {
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SW_CERT_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SW_CERT_A:
|
2016-09-12 23:40:27 +01:00
|
|
|
if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_send_server_certificate(hs);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = 1;
|
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SW_CERT_STATUS_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
2015-08-12 11:47:11 +01:00
|
|
|
case SSL3_ST_SW_CERT_STATUS_A:
|
2016-11-14 01:45:16 +00:00
|
|
|
if (hs->certificate_status_expected) {
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_send_certificate_status(hs);
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = 1;
|
2015-08-12 11:47:11 +01:00
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SW_KEY_EXCH_A;
|
2015-08-12 11:47:11 +01:00
|
|
|
break;
|
|
|
|
|
2014-12-19 01:42:32 +00:00
|
|
|
case SSL3_ST_SW_KEY_EXCH_A:
|
|
|
|
case SSL3_ST_SW_KEY_EXCH_B:
|
2015-12-19 22:05:56 +00:00
|
|
|
alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-07 21:49:42 +01:00
|
|
|
/* PSK ciphers send ServerKeyExchange if there is an identity hint. */
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
|
|
|
|
((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_send_server_key_exchange(hs);
|
2015-02-11 06:17:41 +00:00
|
|
|
if (ret <= 0) {
|
2014-12-19 01:42:32 +00:00
|
|
|
goto end;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
} else {
|
|
|
|
skip = 1;
|
|
|
|
}
|
|
|
|
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SW_CERT_REQ_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SW_CERT_REQ_A:
|
2016-11-14 01:45:16 +00:00
|
|
|
if (hs->cert_request) {
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_send_certificate_request(hs);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2015-05-26 21:59:43 +01:00
|
|
|
} else {
|
|
|
|
skip = 1;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SW_SRVR_DONE_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SW_SRVR_DONE_A:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_send_server_hello_done(hs);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->next_state = SSL3_ST_SR_CERT_A;
|
|
|
|
hs->state = SSL3_ST_SW_FLUSH;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SR_CERT_A:
|
2016-11-14 01:45:16 +00:00
|
|
|
if (hs->cert_request) {
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_get_client_certificate(hs);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SR_KEY_EXCH_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SR_KEY_EXCH_A:
|
|
|
|
case SSL3_ST_SR_KEY_EXCH_B:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_get_client_key_exchange(hs);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SR_CERT_VRFY_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SR_CERT_VRFY_A:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_get_cert_verify(hs);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SR_CHANGE;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
2015-11-26 07:16:49 +00:00
|
|
|
case SSL3_ST_SR_CHANGE:
|
2016-06-15 22:44:37 +01:00
|
|
|
ret = ssl->method->read_change_cipher_spec(ssl);
|
2015-11-26 07:16:49 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2016-11-17 08:03:59 +00:00
|
|
|
if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
|
2014-12-19 01:42:32 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
2015-11-26 07:16:49 +00:00
|
|
|
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SR_NEXT_PROTO_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SR_NEXT_PROTO_A:
|
2016-11-14 01:45:16 +00:00
|
|
|
if (hs->next_proto_neg_seen) {
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_get_next_proto(hs);
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
} else {
|
2016-06-13 18:56:33 +01:00
|
|
|
skip = 1;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SR_CHANNEL_ID_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SR_CHANNEL_ID_A:
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ssl->s3->tlsext_channel_id_valid) {
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_get_channel_id(hs);
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = 1;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SR_FINISHED_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SR_FINISHED_A:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_get_finished(hs);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:49:42 +01:00
|
|
|
ssl->method->received_flight(ssl);
|
2016-06-27 21:34:59 +01:00
|
|
|
if (ssl->session != NULL) {
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL_ST_OK;
|
2014-12-19 01:42:32 +00:00
|
|
|
} else {
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SW_SESSION_TICKET_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2016-06-07 21:49:42 +01:00
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
/* If this is a full handshake with ChannelID then record the handshake
|
|
|
|
* hashes in |ssl->s3->new_session| in case we need them to verify a
|
|
|
|
* ChannelID signature on a resumption of this session in the future. */
|
|
|
|
if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = tls1_record_handshake_hashes_for_channel_id(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SW_SESSION_TICKET_A:
|
2016-11-14 01:45:16 +00:00
|
|
|
if (hs->ticket_expected) {
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = ssl3_send_new_session_ticket(hs);
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = 1;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SW_CHANGE;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
2016-06-28 16:22:02 +01:00
|
|
|
case SSL3_ST_SW_CHANGE:
|
Don't use the buffer BIO in TLS.
On the TLS side, we introduce a running buffer of ciphertext. Queuing up
pending data consists of encrypting the record into the buffer. This
effectively reimplements what the buffer BIO was doing previously, but
this resizes to fit the whole flight.
As part of this, rename all the functions to add to the pending flight
to be more uniform. This CL proposes "add_foo" to add to the pending
flight and "flush_flight" to drain it.
We add an add_alert hook for alerts but, for now, only the SSL 3.0
warning alert (sent mid-handshake) uses this mechanism. Later work will
push this down to the rest of the write path so closure alerts use it
too, as in DTLS. The intended end state is that all the ssl_buffer.c and
wpend_ret logic will only be used for application data and eventually
optionally replaced by the in-place API, while all "incidental" data
will be handled internally.
For now, the two buffers are mutually exclusive. Moving closure alerts
to "incidentals" will change this, but flushing application data early
is tricky due to wpend_ret. (If we call ssl_write_buffer_flush,
do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That
too is all left alone in this change.
To keep the diff down, write_message is retained for now and will be
removed from the state machines in a follow-up change.
BUG=72
Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d
Reviewed-on: https://boringssl-review.googlesource.com/13224
Reviewed-by: Adam Langley <agl@google.com>
2017-01-03 23:37:41 +00:00
|
|
|
if (!ssl->method->add_change_cipher_spec(ssl) ||
|
|
|
|
!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
|
2014-12-19 01:42:32 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
Don't use the buffer BIO in TLS.
On the TLS side, we introduce a running buffer of ciphertext. Queuing up
pending data consists of encrypting the record into the buffer. This
effectively reimplements what the buffer BIO was doing previously, but
this resizes to fit the whole flight.
As part of this, rename all the functions to add to the pending flight
to be more uniform. This CL proposes "add_foo" to add to the pending
flight and "flush_flight" to drain it.
We add an add_alert hook for alerts but, for now, only the SSL 3.0
warning alert (sent mid-handshake) uses this mechanism. Later work will
push this down to the rest of the write path so closure alerts use it
too, as in DTLS. The intended end state is that all the ssl_buffer.c and
wpend_ret logic will only be used for application data and eventually
optionally replaced by the in-place API, while all "incidental" data
will be handled internally.
For now, the two buffers are mutually exclusive. Moving closure alerts
to "incidentals" will change this, but flushing application data early
is tricky due to wpend_ret. (If we call ssl_write_buffer_flush,
do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That
too is all left alone in this change.
To keep the diff down, write_message is retained for now and will be
removed from the state machines in a follow-up change.
BUG=72
Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d
Reviewed-on: https://boringssl-review.googlesource.com/13224
Reviewed-by: Adam Langley <agl@google.com>
2017-01-03 23:37:41 +00:00
|
|
|
|
|
|
|
hs->state = SSL3_ST_SW_FINISHED_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SW_FINISHED_A:
|
2017-01-13 01:02:05 +00:00
|
|
|
ret = ssl3_send_finished(hs);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SW_FLUSH;
|
2016-06-27 21:34:59 +01:00
|
|
|
if (ssl->session != NULL) {
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->next_state = SSL3_ST_SR_CHANGE;
|
2014-12-19 01:42:32 +00:00
|
|
|
} else {
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->next_state = SSL_ST_OK;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
case SSL3_ST_SW_FLUSH:
|
2017-01-01 22:41:30 +00:00
|
|
|
ret = ssl->method->flush_flight(ssl);
|
|
|
|
if (ret <= 0) {
|
2016-06-07 22:03:46 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = hs->next_state;
|
|
|
|
if (hs->state != SSL_ST_OK) {
|
2016-06-07 22:03:46 +01:00
|
|
|
ssl->method->expect_flight(ssl);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-07-11 18:19:03 +01:00
|
|
|
case SSL_ST_TLS13:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = tls13_handshake(hs);
|
2016-07-11 18:19:03 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL_ST_OK;
|
2016-07-11 18:19:03 +01:00
|
|
|
break;
|
|
|
|
|
2014-12-19 01:42:32 +00:00
|
|
|
case SSL_ST_OK:
|
2016-07-27 22:51:49 +01:00
|
|
|
ssl->method->release_current_message(ssl, 1 /* free_buffer */);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
/* If we aren't retaining peer certificates then we can discard it
|
|
|
|
* now. */
|
|
|
|
if (ssl->s3->new_session != NULL &&
|
2016-11-17 01:53:09 +00:00
|
|
|
ssl->retain_only_sha256_of_client_certs) {
|
2016-11-07 20:02:35 +00:00
|
|
|
X509_free(ssl->s3->new_session->x509_peer);
|
|
|
|
ssl->s3->new_session->x509_peer = NULL;
|
|
|
|
sk_X509_pop_free(ssl->s3->new_session->x509_chain, X509_free);
|
|
|
|
ssl->s3->new_session->x509_chain = NULL;
|
2016-06-27 21:34:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SSL_SESSION_free(ssl->s3->established_session);
|
|
|
|
if (ssl->session != NULL) {
|
2016-08-05 15:51:43 +01:00
|
|
|
SSL_SESSION_up_ref(ssl->session);
|
|
|
|
ssl->s3->established_session = ssl->session;
|
2016-06-27 21:34:59 +01:00
|
|
|
} else {
|
|
|
|
ssl->s3->established_session = ssl->s3->new_session;
|
|
|
|
ssl->s3->established_session->not_resumable = 0;
|
|
|
|
ssl->s3->new_session = NULL;
|
|
|
|
}
|
|
|
|
|
2017-01-25 20:47:49 +00:00
|
|
|
if (hs->v2_clienthello) {
|
|
|
|
CRYPTO_STATIC_MUTEX_lock_write(&g_v2clienthello_lock);
|
|
|
|
g_v2clienthello_count++;
|
|
|
|
CRYPTO_STATIC_MUTEX_unlock_write(&g_v2clienthello_lock);
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->initial_handshake_complete = 1;
|
2016-11-17 08:20:47 +00:00
|
|
|
ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-11-12 02:20:55 +00:00
|
|
|
ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
|
2014-12-19 01:42:32 +00:00
|
|
|
ret = 1;
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
default:
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
|
2014-12-19 01:42:32 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2016-12-12 22:00:50 +00:00
|
|
|
if (!ssl->s3->tmp.reuse_message && !skip && hs->state != state) {
|
|
|
|
int new_state = hs->state;
|
|
|
|
hs->state = state;
|
2016-06-02 01:16:03 +01:00
|
|
|
ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = new_state;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
skip = 0;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
end:
|
2016-06-02 01:16:03 +01:00
|
|
|
ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
|
2014-12-19 01:42:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-12-04 04:15:13 +00:00
|
|
|
int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
|
|
|
|
uint16_t id) {
|
2016-08-10 00:36:38 +01:00
|
|
|
CBS cipher_suites;
|
|
|
|
CBS_init(&cipher_suites, client_hello->cipher_suites,
|
|
|
|
client_hello->cipher_suites_len);
|
|
|
|
|
|
|
|
while (CBS_len(&cipher_suites) > 0) {
|
|
|
|
uint16_t got_id;
|
|
|
|
if (!CBS_get_u16(&cipher_suites, &got_id)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (got_id == id) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-06 18:35:25 +00:00
|
|
|
static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
|
2016-12-04 04:15:13 +00:00
|
|
|
const SSL_CLIENT_HELLO *client_hello) {
|
2016-12-06 18:35:25 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
2017-02-03 04:28:14 +00:00
|
|
|
assert(!ssl->s3->have_version);
|
2016-08-10 01:00:32 +01:00
|
|
|
uint16_t min_version, max_version;
|
|
|
|
if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
|
|
|
|
*out_alert = SSL_AD_PROTOCOL_VERSION;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-20 00:57:37 +01:00
|
|
|
uint16_t version = 0;
|
2016-09-15 21:27:05 +01:00
|
|
|
/* Check supported_versions extension if it is present. */
|
|
|
|
CBS supported_versions;
|
2016-12-04 04:15:13 +00:00
|
|
|
if (ssl_client_hello_get_extension(client_hello, &supported_versions,
|
|
|
|
TLSEXT_TYPE_supported_versions)) {
|
2016-09-15 21:27:05 +01:00
|
|
|
CBS versions;
|
|
|
|
if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
|
|
|
|
CBS_len(&supported_versions) != 0 ||
|
|
|
|
CBS_len(&versions) == 0) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
*out_alert = SSL_AD_DECODE_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-28 21:34:06 +01:00
|
|
|
/* Choose the newest commonly-supported version advertised by the client.
|
|
|
|
* The client orders the versions according to its preferences, but we're
|
|
|
|
* not required to honor the client's preferences. */
|
2016-09-15 21:27:05 +01:00
|
|
|
int found_version = 0;
|
|
|
|
while (CBS_len(&versions) != 0) {
|
|
|
|
uint16_t ext_version;
|
|
|
|
if (!CBS_get_u16(&versions, &ext_version)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
*out_alert = SSL_AD_DECODE_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!ssl->method->version_from_wire(&ext_version, ext_version)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (min_version <= ext_version &&
|
2016-10-28 21:34:06 +01:00
|
|
|
ext_version <= max_version &&
|
|
|
|
(!found_version || version < ext_version)) {
|
2016-09-15 21:27:05 +01:00
|
|
|
version = ext_version;
|
|
|
|
found_version = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found_version) {
|
|
|
|
goto unsupported_protocol;
|
2016-09-20 00:57:37 +01:00
|
|
|
}
|
|
|
|
} else {
|
2016-09-15 21:27:05 +01:00
|
|
|
/* Process ClientHello.version instead. Note that versions beyond (D)TLS 1.2
|
|
|
|
* do not use this mechanism. */
|
|
|
|
if (SSL_is_dtls(ssl)) {
|
|
|
|
if (client_hello->version <= DTLS1_2_VERSION) {
|
|
|
|
version = TLS1_2_VERSION;
|
|
|
|
} else if (client_hello->version <= DTLS1_VERSION) {
|
|
|
|
version = TLS1_1_VERSION;
|
|
|
|
} else {
|
|
|
|
goto unsupported_protocol;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (client_hello->version >= TLS1_2_VERSION) {
|
|
|
|
version = TLS1_2_VERSION;
|
|
|
|
} else if (client_hello->version >= TLS1_1_VERSION) {
|
|
|
|
version = TLS1_1_VERSION;
|
|
|
|
} else if (client_hello->version >= TLS1_VERSION) {
|
|
|
|
version = TLS1_VERSION;
|
|
|
|
} else if (client_hello->version >= SSL3_VERSION) {
|
|
|
|
version = SSL3_VERSION;
|
|
|
|
} else {
|
|
|
|
goto unsupported_protocol;
|
|
|
|
}
|
2016-09-20 00:57:37 +01:00
|
|
|
}
|
2016-08-10 01:00:32 +01:00
|
|
|
|
2016-09-15 21:27:05 +01:00
|
|
|
/* Apply our minimum and maximum version. */
|
|
|
|
if (version > max_version) {
|
|
|
|
version = max_version;
|
|
|
|
}
|
2016-08-10 01:00:32 +01:00
|
|
|
|
2016-09-15 21:27:05 +01:00
|
|
|
if (version < min_version) {
|
|
|
|
goto unsupported_protocol;
|
|
|
|
}
|
2016-08-10 01:00:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle FALLBACK_SCSV. */
|
|
|
|
if (ssl_client_cipher_list_contains_cipher(client_hello,
|
|
|
|
SSL3_CK_FALLBACK_SCSV & 0xffff) &&
|
|
|
|
version < max_version) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
|
|
|
|
*out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-06 18:35:25 +00:00
|
|
|
hs->client_version = client_hello->version;
|
2016-08-10 01:00:32 +01:00
|
|
|
ssl->version = ssl->method->version_to_wire(version);
|
|
|
|
ssl->s3->enc_method = ssl3_get_enc_method(version);
|
|
|
|
assert(ssl->s3->enc_method != NULL);
|
|
|
|
|
|
|
|
/* At this point, the connection's version is known and |ssl->version| is
|
|
|
|
* fixed. Begin enforcing the record-layer version. */
|
|
|
|
ssl->s3->have_version = 1;
|
|
|
|
|
|
|
|
return 1;
|
2016-09-15 21:27:05 +01:00
|
|
|
|
|
|
|
unsupported_protocol:
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
|
|
|
|
*out_alert = SSL_AD_PROTOCOL_VERSION;
|
|
|
|
return 0;
|
2016-08-10 01:00:32 +01:00
|
|
|
}
|
|
|
|
|
2016-12-04 04:19:55 +00:00
|
|
|
static STACK_OF(SSL_CIPHER) *
|
|
|
|
ssl_parse_client_cipher_list(const SSL_CLIENT_HELLO *client_hello) {
|
|
|
|
CBS cipher_suites;
|
|
|
|
CBS_init(&cipher_suites, client_hello->cipher_suites,
|
|
|
|
client_hello->cipher_suites_len);
|
|
|
|
|
|
|
|
STACK_OF(SSL_CIPHER) *sk = sk_SSL_CIPHER_new_null();
|
|
|
|
if (sk == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (CBS_len(&cipher_suites) > 0) {
|
|
|
|
uint16_t cipher_suite;
|
|
|
|
|
|
|
|
if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
|
|
|
|
if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sk;
|
|
|
|
|
|
|
|
err:
|
|
|
|
sk_SSL_CIPHER_free(sk);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ssl_get_compatible_server_ciphers determines the key exchange and
|
|
|
|
* authentication cipher suite masks compatible with the server configuration
|
|
|
|
* and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
|
|
|
|
* exchange mask and |*out_mask_a| to the authentication mask. */
|
|
|
|
static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
|
|
|
|
uint32_t *out_mask_k,
|
|
|
|
uint32_t *out_mask_a) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
|
|
|
if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
|
|
|
|
*out_mask_k = SSL_kGENERIC;
|
|
|
|
*out_mask_a = SSL_aGENERIC;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t mask_k = 0;
|
|
|
|
uint32_t mask_a = 0;
|
|
|
|
|
2017-01-24 21:59:42 +00:00
|
|
|
if (ssl_has_certificate(ssl)) {
|
2016-12-04 04:19:55 +00:00
|
|
|
int type = ssl_private_key_type(ssl);
|
|
|
|
if (type == NID_rsaEncryption) {
|
|
|
|
mask_k |= SSL_kRSA;
|
|
|
|
mask_a |= SSL_aRSA;
|
|
|
|
} else if (ssl_is_ecdsa_key_type(type)) {
|
|
|
|
mask_a |= SSL_aECDSA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl->cert->dh_tmp != NULL || ssl->cert->dh_tmp_cb != NULL) {
|
|
|
|
mask_k |= SSL_kDHE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for a shared group to consider ECDHE ciphers. */
|
|
|
|
uint16_t unused;
|
|
|
|
if (tls1_get_shared_group(hs, &unused)) {
|
|
|
|
mask_k |= SSL_kECDHE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PSK requires a server callback. */
|
|
|
|
if (ssl->psk_server_callback != NULL) {
|
|
|
|
mask_k |= SSL_kPSK;
|
|
|
|
mask_a |= SSL_aPSK;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_mask_k = mask_k;
|
|
|
|
*out_mask_a = mask_a;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const SSL_CIPHER *ssl3_choose_cipher(
|
|
|
|
SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
|
|
|
|
const struct ssl_cipher_preference_list_st *server_pref) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
|
|
|
const SSL_CIPHER *c, *ret = NULL;
|
|
|
|
STACK_OF(SSL_CIPHER) *srvr = server_pref->ciphers, *prio, *allow;
|
|
|
|
int ok;
|
|
|
|
size_t cipher_index;
|
|
|
|
uint32_t alg_k, alg_a, mask_k, mask_a;
|
|
|
|
/* in_group_flags will either be NULL, or will point to an array of bytes
|
|
|
|
* which indicate equal-preference groups in the |prio| stack. See the
|
|
|
|
* comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
|
|
|
|
* struct. */
|
|
|
|
const uint8_t *in_group_flags;
|
|
|
|
/* group_min contains the minimal index so far found in a group, or -1 if no
|
|
|
|
* such value exists yet. */
|
|
|
|
int group_min = -1;
|
|
|
|
|
|
|
|
STACK_OF(SSL_CIPHER) *clnt = ssl_parse_client_cipher_list(client_hello);
|
|
|
|
if (clnt == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
|
|
|
|
prio = srvr;
|
|
|
|
in_group_flags = server_pref->in_group_flags;
|
|
|
|
allow = clnt;
|
|
|
|
} else {
|
|
|
|
prio = clnt;
|
|
|
|
in_group_flags = NULL;
|
|
|
|
allow = srvr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
|
|
|
|
c = sk_SSL_CIPHER_value(prio, i);
|
|
|
|
|
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
/* Check the TLS version. */
|
|
|
|
if (SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
|
|
|
|
SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) {
|
|
|
|
ok = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
alg_k = c->algorithm_mkey;
|
|
|
|
alg_a = c->algorithm_auth;
|
|
|
|
|
|
|
|
ok = ok && (alg_k & mask_k) && (alg_a & mask_a);
|
|
|
|
|
|
|
|
if (ok && sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
|
|
|
|
if (in_group_flags != NULL && in_group_flags[i] == 1) {
|
|
|
|
/* This element of |prio| is in a group. Update the minimum index found
|
|
|
|
* so far and continue looking. */
|
|
|
|
if (group_min == -1 || (size_t)group_min > cipher_index) {
|
|
|
|
group_min = cipher_index;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (group_min != -1 && (size_t)group_min < cipher_index) {
|
|
|
|
cipher_index = group_min;
|
|
|
|
}
|
|
|
|
ret = sk_SSL_CIPHER_value(allow, cipher_index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
|
|
|
|
/* We are about to leave a group, but we found a match in it, so that's
|
|
|
|
* our answer. */
|
|
|
|
ret = sk_SSL_CIPHER_value(allow, group_min);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_SSL_CIPHER_free(clnt);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-03 04:51:22 +00:00
|
|
|
static int ssl3_process_client_hello(SSL_HANDSHAKE *hs) {
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
2017-02-03 04:51:22 +00:00
|
|
|
if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_HELLO)) {
|
|
|
|
return -1;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 04:15:13 +00:00
|
|
|
SSL_CLIENT_HELLO client_hello;
|
|
|
|
if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
|
|
|
|
ssl->init_num)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2017-02-03 04:51:22 +00:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
return -1;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 04:51:22 +00:00
|
|
|
/* Run the early callback. */
|
|
|
|
if (ssl->ctx->select_certificate_cb != NULL) {
|
|
|
|
switch (ssl->ctx->select_certificate_cb(&client_hello)) {
|
|
|
|
case 0:
|
|
|
|
ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
/* Connection rejected. */
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
|
|
|
|
return -1;
|
2016-06-21 15:33:21 +01:00
|
|
|
|
2017-02-03 04:51:22 +00:00
|
|
|
default:
|
|
|
|
/* fallthrough */;
|
2016-08-10 01:00:32 +01:00
|
|
|
}
|
2017-02-03 04:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t alert;
|
|
|
|
if (!negotiate_version(hs, &alert, &client_hello)) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the client random. */
|
|
|
|
if (client_hello.random_len != SSL3_RANDOM_SIZE) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
|
|
|
|
client_hello.random_len);
|
|
|
|
|
|
|
|
/* Only null compression is supported. TLS 1.3 further requires the peer
|
|
|
|
* advertise no other compression. */
|
|
|
|
if (OPENSSL_memchr(client_hello.compression_methods, 0,
|
|
|
|
client_hello.compression_methods_len) == NULL ||
|
|
|
|
(ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
|
|
|
|
client_hello.compression_methods_len != 1)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2017-02-03 04:51:22 +00:00
|
|
|
/* TLS extensions. */
|
|
|
|
if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ssl3_select_certificate(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
|
|
|
/* Call |cert_cb| to update server certificates if required. */
|
|
|
|
if (ssl->cert->cert_cb != NULL) {
|
|
|
|
int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
|
|
|
|
if (rv == 0) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
2016-08-10 01:00:32 +01:00
|
|
|
return -1;
|
2016-06-21 15:33:21 +01:00
|
|
|
}
|
2017-02-03 04:51:22 +00:00
|
|
|
if (rv < 0) {
|
|
|
|
ssl->rwstate = SSL_X509_LOOKUP;
|
|
|
|
return -1;
|
2016-11-16 08:08:23 +00:00
|
|
|
}
|
2017-02-03 04:51:22 +00:00
|
|
|
}
|
2016-11-16 08:08:23 +00:00
|
|
|
|
2017-02-03 04:51:22 +00:00
|
|
|
if (!ssl_auto_chain_if_needed(ssl)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-11-16 08:08:23 +00:00
|
|
|
|
2017-02-03 04:51:22 +00:00
|
|
|
if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
|
|
|
|
/* Jump to the TLS 1.3 state machine. */
|
|
|
|
hs->state = SSL_ST_TLS13;
|
|
|
|
hs->do_tls13_handshake = tls13_server_handshake;
|
|
|
|
return 1;
|
2016-11-16 08:08:23 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 04:51:22 +00:00
|
|
|
SSL_CLIENT_HELLO client_hello;
|
|
|
|
if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
|
|
|
|
ssl->init_num)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-08-10 01:00:32 +01:00
|
|
|
|
2017-02-03 04:51:22 +00:00
|
|
|
/* Negotiate the cipher suite. This must be done after |cert_cb| so the
|
|
|
|
* certificate is finalized. */
|
|
|
|
ssl->s3->tmp.new_cipher =
|
|
|
|
ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
|
|
|
|
if (ssl->s3->tmp.new_cipher == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-12-20 23:55:16 +00:00
|
|
|
|
2017-02-03 04:51:22 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-02-03 04:33:21 +00:00
|
|
|
|
2017-02-03 04:51:22 +00:00
|
|
|
static int ssl3_select_parameters(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
|
|
|
uint8_t al = SSL_AD_INTERNAL_ERROR;
|
|
|
|
int ret = -1;
|
|
|
|
SSL_SESSION *session = NULL;
|
Negotiate ciphers before resumption.
This changes our resumption strategy. Before, we would negotiate ciphers
only on fresh handshakes. On resumption, we would blindly use whatever
was in the session.
Instead, evaluate cipher suite preferences on every handshake.
Resumption requires that the saved cipher suite match the one that would
have been negotiated anyway. If client or server preferences changed
sufficiently, we decline the session.
This is much easier to reason about (we always pick the best cipher
suite), simpler, and avoids getting stuck under old preferences if
tickets are continuously renewed. Notably, although TLS 1.2 ticket
renewal does not work in practice, TLS 1.3 will renew tickets like
there's no tomorrow.
It also means we don't need dedicated code to avoid resuming a cipher
which has since been disabled. (That dedicated code was a little odd
anyway since the mask_k, etc., checks didn't occur. When cert_cb was
skipped on resumption, one could resume without ever configuring a
certificate! So we couldn't know whether to mask off RSA or ECDSA cipher
suites.)
Add tests which assert on this new arrangement.
BUG=116
Change-Id: Id40d851ccd87e06c46c6ec272527fd8ece8abfc6
Reviewed-on: https://boringssl-review.googlesource.com/11847
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
2016-11-16 10:05:33 +00:00
|
|
|
|
2017-02-03 04:51:22 +00:00
|
|
|
SSL_CLIENT_HELLO client_hello;
|
|
|
|
if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
|
|
|
|
ssl->init_num)) {
|
|
|
|
return -1;
|
2016-11-16 10:07:53 +00:00
|
|
|
}
|
2016-08-10 00:36:38 +01:00
|
|
|
|
2016-11-16 10:07:53 +00:00
|
|
|
/* Determine whether we are doing session resumption. */
|
|
|
|
int tickets_supported = 0, renew_ticket = 0;
|
|
|
|
switch (ssl_get_prev_session(ssl, &session, &tickets_supported, &renew_ticket,
|
|
|
|
&client_hello)) {
|
|
|
|
case ssl_session_success:
|
|
|
|
break;
|
|
|
|
case ssl_session_error:
|
|
|
|
goto err;
|
|
|
|
case ssl_session_retry:
|
|
|
|
ssl->rwstate = SSL_PENDING_SESSION;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session != NULL) {
|
|
|
|
if (session->extended_master_secret &&
|
|
|
|
!ssl->s3->tmp.extended_master_secret) {
|
|
|
|
/* A ClientHello without EMS that attempts to resume a session with EMS
|
|
|
|
* is fatal to the connection. */
|
|
|
|
al = SSL_AD_HANDSHAKE_FAILURE;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
|
|
|
|
goto f_err;
|
2016-08-10 00:36:38 +01:00
|
|
|
}
|
|
|
|
|
2016-11-16 10:07:53 +00:00
|
|
|
if (!ssl_session_is_resumable(ssl, session) ||
|
|
|
|
/* If the client offers the EMS extension, but the previous session
|
|
|
|
* didn't use it, then negotiate a new session. */
|
|
|
|
ssl->s3->tmp.extended_master_secret !=
|
|
|
|
session->extended_master_secret) {
|
|
|
|
SSL_SESSION_free(session);
|
2016-08-10 01:00:32 +01:00
|
|
|
session = NULL;
|
|
|
|
}
|
2016-11-16 10:07:53 +00:00
|
|
|
}
|
2016-07-11 18:19:03 +01:00
|
|
|
|
2016-11-16 10:07:53 +00:00
|
|
|
if (session != NULL) {
|
|
|
|
/* Use the old session. */
|
2016-11-14 01:32:04 +00:00
|
|
|
hs->ticket_expected = renew_ticket;
|
2016-11-16 10:07:53 +00:00
|
|
|
ssl->session = session;
|
|
|
|
session = NULL;
|
|
|
|
ssl->s3->session_reused = 1;
|
|
|
|
} else {
|
2016-11-14 01:32:04 +00:00
|
|
|
hs->ticket_expected = tickets_supported;
|
2016-11-16 10:07:53 +00:00
|
|
|
ssl_set_session(ssl, NULL);
|
2016-11-17 08:20:47 +00:00
|
|
|
if (!ssl_get_new_session(hs, 1 /* server */)) {
|
2016-11-16 10:07:53 +00:00
|
|
|
goto err;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2015-06-02 18:50:35 +01:00
|
|
|
|
2016-11-16 10:07:53 +00:00
|
|
|
/* Clear the session ID if we want the session to be single-use. */
|
|
|
|
if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
|
|
|
|
ssl->s3->new_session->session_id_length = 0;
|
|
|
|
}
|
2015-02-21 00:04:00 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 10:07:53 +00:00
|
|
|
if (ssl->ctx->dos_protection_cb != NULL &&
|
|
|
|
ssl->ctx->dos_protection_cb(&client_hello) == 0) {
|
|
|
|
/* Connection rejected for DOS reasons. */
|
|
|
|
al = SSL_AD_INTERNAL_ERROR;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
|
|
|
|
goto f_err;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
Negotiate ciphers before resumption.
This changes our resumption strategy. Before, we would negotiate ciphers
only on fresh handshakes. On resumption, we would blindly use whatever
was in the session.
Instead, evaluate cipher suite preferences on every handshake.
Resumption requires that the saved cipher suite match the one that would
have been negotiated anyway. If client or server preferences changed
sufficiently, we decline the session.
This is much easier to reason about (we always pick the best cipher
suite), simpler, and avoids getting stuck under old preferences if
tickets are continuously renewed. Notably, although TLS 1.2 ticket
renewal does not work in practice, TLS 1.3 will renew tickets like
there's no tomorrow.
It also means we don't need dedicated code to avoid resuming a cipher
which has since been disabled. (That dedicated code was a little odd
anyway since the mask_k, etc., checks didn't occur. When cert_cb was
skipped on resumption, one could resume without ever configuring a
certificate! So we couldn't know whether to mask off RSA or ECDSA cipher
suites.)
Add tests which assert on this new arrangement.
BUG=116
Change-Id: Id40d851ccd87e06c46c6ec272527fd8ece8abfc6
Reviewed-on: https://boringssl-review.googlesource.com/11847
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
2016-11-16 10:05:33 +00:00
|
|
|
if (ssl->session == NULL) {
|
|
|
|
ssl->s3->new_session->cipher = ssl->s3->tmp.new_cipher;
|
2015-05-26 21:59:43 +01:00
|
|
|
|
2016-11-16 08:08:23 +00:00
|
|
|
/* On new sessions, stash the SNI value in the session. */
|
2016-11-14 01:32:04 +00:00
|
|
|
if (hs->hostname != NULL) {
|
2017-02-06 17:06:01 +00:00
|
|
|
OPENSSL_free(ssl->s3->new_session->tlsext_hostname);
|
2016-11-14 01:32:04 +00:00
|
|
|
ssl->s3->new_session->tlsext_hostname = BUF_strdup(hs->hostname);
|
2016-11-16 08:08:23 +00:00
|
|
|
if (ssl->s3->new_session->tlsext_hostname == NULL) {
|
|
|
|
al = SSL_AD_INTERNAL_ERROR;
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-26 21:59:43 +01:00
|
|
|
/* Determine whether to request a client certificate. */
|
2016-11-14 01:32:04 +00:00
|
|
|
hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
|
2015-05-26 21:59:43 +01:00
|
|
|
/* Only request a certificate if Channel ID isn't negotiated. */
|
2015-12-19 22:05:56 +00:00
|
|
|
if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
|
|
|
|
ssl->s3->tlsext_channel_id_valid) {
|
2016-11-14 01:32:04 +00:00
|
|
|
hs->cert_request = 0;
|
2015-05-26 21:59:43 +01:00
|
|
|
}
|
2016-05-12 19:54:57 +01:00
|
|
|
/* CertificateRequest may only be sent in certificate-based ciphers. */
|
2016-09-12 23:40:27 +01:00
|
|
|
if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
|
2016-11-14 01:32:04 +00:00
|
|
|
hs->cert_request = 0;
|
2015-05-26 21:59:43 +01:00
|
|
|
}
|
2016-08-02 00:16:46 +01:00
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
if (!hs->cert_request) {
|
2016-08-02 00:16:46 +01:00
|
|
|
/* OpenSSL returns X509_V_OK when no certificates are requested. This is
|
2016-08-18 21:40:28 +01:00
|
|
|
* classed by them as a bug, but it's assumed by at least NGINX. */
|
2016-08-02 00:16:46 +01:00
|
|
|
ssl->s3->new_session->verify_result = X509_V_OK;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
Negotiate ciphers before resumption.
This changes our resumption strategy. Before, we would negotiate ciphers
only on fresh handshakes. On resumption, we would blindly use whatever
was in the session.
Instead, evaluate cipher suite preferences on every handshake.
Resumption requires that the saved cipher suite match the one that would
have been negotiated anyway. If client or server preferences changed
sufficiently, we decline the session.
This is much easier to reason about (we always pick the best cipher
suite), simpler, and avoids getting stuck under old preferences if
tickets are continuously renewed. Notably, although TLS 1.2 ticket
renewal does not work in practice, TLS 1.3 will renew tickets like
there's no tomorrow.
It also means we don't need dedicated code to avoid resuming a cipher
which has since been disabled. (That dedicated code was a little odd
anyway since the mask_k, etc., checks didn't occur. When cert_cb was
skipped on resumption, one could resume without ever configuring a
certificate! So we couldn't know whether to mask off RSA or ECDSA cipher
suites.)
Add tests which assert on this new arrangement.
BUG=116
Change-Id: Id40d851ccd87e06c46c6ec272527fd8ece8abfc6
Reviewed-on: https://boringssl-review.googlesource.com/11847
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
2016-11-16 10:05:33 +00:00
|
|
|
/* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
|
|
|
|
* deferred. Complete it now. */
|
2016-11-17 08:20:47 +00:00
|
|
|
if (!ssl_negotiate_alpn(hs, &al, &client_hello)) {
|
2016-10-31 22:01:13 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2017-01-21 19:49:39 +00:00
|
|
|
/* Now that all parameters are known, initialize the handshake hash and hash
|
|
|
|
* the ClientHello. */
|
|
|
|
if (!ssl3_init_handshake_hash(ssl) ||
|
|
|
|
!ssl_hash_current_message(ssl)) {
|
2015-08-05 13:50:34 +01:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2016-06-30 18:27:23 +01:00
|
|
|
/* Release the handshake buffer if client authentication isn't required. */
|
2016-11-14 01:32:04 +00:00
|
|
|
if (!hs->cert_request) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl3_free_handshake_buffer(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2015-12-01 19:14:40 +00:00
|
|
|
ret = 1;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
if (0) {
|
|
|
|
f_err:
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
err:
|
2015-06-16 20:34:50 +01:00
|
|
|
SSL_SESSION_free(session);
|
2014-12-19 01:42:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-10-10 19:59:09 +01:00
|
|
|
/* We only accept ChannelIDs on connections with ECDHE in order to avoid a
|
|
|
|
* known attack while we fix ChannelID itself. */
|
|
|
|
if (ssl->s3->tlsext_channel_id_valid &&
|
|
|
|
(ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
|
|
|
|
ssl->s3->tlsext_channel_id_valid = 0;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-10-10 19:59:09 +01:00
|
|
|
/* If this is a resumption and the original handshake didn't support
|
|
|
|
* ChannelID then we didn't record the original handshake hashes in the
|
|
|
|
* session and so cannot resume with ChannelIDs. */
|
2016-06-27 21:34:59 +01:00
|
|
|
if (ssl->session != NULL &&
|
|
|
|
ssl->session->original_handshake_hash_len == 0) {
|
2015-10-10 19:59:09 +01:00
|
|
|
ssl->s3->tlsext_channel_id_valid = 0;
|
|
|
|
}
|
2015-07-22 02:06:39 +01:00
|
|
|
|
2016-08-03 18:13:17 +01:00
|
|
|
struct timeval now;
|
|
|
|
ssl_get_current_time(ssl, &now);
|
|
|
|
ssl->s3->server_random[0] = now.tv_sec >> 24;
|
|
|
|
ssl->s3->server_random[1] = now.tv_sec >> 16;
|
|
|
|
ssl->s3->server_random[2] = now.tv_sec >> 8;
|
|
|
|
ssl->s3->server_random[3] = now.tv_sec;
|
2016-07-10 17:20:35 +01:00
|
|
|
if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
|
2015-10-10 19:59:09 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-08-12 03:01:18 +01:00
|
|
|
/* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
|
|
|
|
* 1.3 is finalized and we are not implementing a draft version. */
|
2016-07-10 17:20:35 +01:00
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
const SSL_SESSION *session = ssl->s3->new_session;
|
|
|
|
if (ssl->session != NULL) {
|
|
|
|
session = ssl->session;
|
|
|
|
}
|
|
|
|
|
2016-06-17 23:48:29 +01:00
|
|
|
CBB cbb, body, session_id;
|
|
|
|
if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
|
|
|
|
!CBB_add_u16(&body, ssl->version) ||
|
|
|
|
!CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
|
|
|
|
!CBB_add_u8_length_prefixed(&body, &session_id) ||
|
2016-06-27 21:34:59 +01:00
|
|
|
!CBB_add_bytes(&session_id, session->session_id,
|
|
|
|
session->session_id_length) ||
|
2016-06-17 23:48:29 +01:00
|
|
|
!CBB_add_u16(&body, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
|
|
|
|
!CBB_add_u8(&body, 0 /* no compression */) ||
|
2016-12-03 07:20:34 +00:00
|
|
|
!ssl_add_serverhello_tlsext(hs, &body) ||
|
Don't use the buffer BIO in TLS.
On the TLS side, we introduce a running buffer of ciphertext. Queuing up
pending data consists of encrypting the record into the buffer. This
effectively reimplements what the buffer BIO was doing previously, but
this resizes to fit the whole flight.
As part of this, rename all the functions to add to the pending flight
to be more uniform. This CL proposes "add_foo" to add to the pending
flight and "flush_flight" to drain it.
We add an add_alert hook for alerts but, for now, only the SSL 3.0
warning alert (sent mid-handshake) uses this mechanism. Later work will
push this down to the rest of the write path so closure alerts use it
too, as in DTLS. The intended end state is that all the ssl_buffer.c and
wpend_ret logic will only be used for application data and eventually
optionally replaced by the in-place API, while all "incidental" data
will be handled internally.
For now, the two buffers are mutually exclusive. Moving closure alerts
to "incidentals" will change this, but flushing application data early
is tricky due to wpend_ret. (If we call ssl_write_buffer_flush,
do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That
too is all left alone in this change.
To keep the diff down, write_message is retained for now and will be
removed from the state machines in a follow-up change.
BUG=72
Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d
Reviewed-on: https://boringssl-review.googlesource.com/13224
Reviewed-by: Adam Langley <agl@google.com>
2017-01-03 23:37:41 +00:00
|
|
|
!ssl_add_message_cbb(ssl, &cbb)) {
|
2015-10-10 19:59:09 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
CBB_cleanup(&cbb);
|
|
|
|
return -1;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 01:02:05 +00:00
|
|
|
return 1;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2016-07-14 03:03:11 +01:00
|
|
|
if (!ssl_has_certificate(ssl)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
|
2017-01-13 01:02:05 +00:00
|
|
|
return -1;
|
2016-07-14 03:03:11 +01:00
|
|
|
}
|
|
|
|
|
2016-06-17 23:48:29 +01:00
|
|
|
if (!ssl3_output_cert_chain(ssl)) {
|
2017-01-13 01:02:05 +00:00
|
|
|
return -1;
|
2016-06-17 23:48:29 +01:00
|
|
|
}
|
2017-01-13 01:02:05 +00:00
|
|
|
return 1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2016-06-17 23:48:29 +01:00
|
|
|
CBB cbb, body, ocsp_response;
|
|
|
|
if (!ssl->method->init_message(ssl, &cbb, &body,
|
|
|
|
SSL3_MT_CERTIFICATE_STATUS) ||
|
|
|
|
!CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
|
|
|
|
!CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
|
2016-12-07 12:55:32 +00:00
|
|
|
!CBB_add_bytes(&ocsp_response, CRYPTO_BUFFER_data(ssl->ocsp_response),
|
|
|
|
CRYPTO_BUFFER_len(ssl->ocsp_response)) ||
|
Don't use the buffer BIO in TLS.
On the TLS side, we introduce a running buffer of ciphertext. Queuing up
pending data consists of encrypting the record into the buffer. This
effectively reimplements what the buffer BIO was doing previously, but
this resizes to fit the whole flight.
As part of this, rename all the functions to add to the pending flight
to be more uniform. This CL proposes "add_foo" to add to the pending
flight and "flush_flight" to drain it.
We add an add_alert hook for alerts but, for now, only the SSL 3.0
warning alert (sent mid-handshake) uses this mechanism. Later work will
push this down to the rest of the write path so closure alerts use it
too, as in DTLS. The intended end state is that all the ssl_buffer.c and
wpend_ret logic will only be used for application data and eventually
optionally replaced by the in-place API, while all "incidental" data
will be handled internally.
For now, the two buffers are mutually exclusive. Moving closure alerts
to "incidentals" will change this, but flushing application data early
is tricky due to wpend_ret. (If we call ssl_write_buffer_flush,
do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That
too is all left alone in this change.
To keep the diff down, write_message is retained for now and will be
removed from the state machines in a follow-up change.
BUG=72
Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d
Reviewed-on: https://boringssl-review.googlesource.com/13224
Reviewed-by: Adam Langley <agl@google.com>
2017-01-03 23:37:41 +00:00
|
|
|
!ssl_add_message_cbb(ssl, &cbb)) {
|
2016-06-17 23:48:29 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
CBB_cleanup(&cbb);
|
|
|
|
return -1;
|
2015-08-12 11:47:11 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 01:02:05 +00:00
|
|
|
return 1;
|
2015-08-12 11:47:11 +01:00
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2015-12-18 06:01:21 +00:00
|
|
|
CBB cbb, child;
|
2016-06-17 22:47:58 +01:00
|
|
|
CBB_zero(&cbb);
|
2015-07-24 17:31:31 +01:00
|
|
|
|
2016-06-17 22:47:58 +01:00
|
|
|
/* Put together the parameters. */
|
2016-12-12 22:00:50 +00:00
|
|
|
if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
|
2015-12-18 06:01:21 +00:00
|
|
|
uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
|
|
|
|
uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-17 22:47:58 +01:00
|
|
|
/* Pre-allocate enough room to comfortably fit an ECDHE public key. */
|
|
|
|
if (!CBB_init(&cbb, 128)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-12-18 06:01:21 +00:00
|
|
|
/* PSK ciphers begin with an identity hint. */
|
2014-12-19 01:42:32 +00:00
|
|
|
if (alg_a & SSL_aPSK) {
|
2015-12-18 06:01:21 +00:00
|
|
|
size_t len =
|
|
|
|
(ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
|
|
|
|
if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
|
|
|
|
!CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
|
|
|
|
len)) {
|
|
|
|
goto err;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 15:10:48 +00:00
|
|
|
if (alg_k & SSL_kDHE) {
|
2015-12-18 06:01:21 +00:00
|
|
|
/* Determine the group to use. */
|
|
|
|
DH *params = ssl->cert->dh_tmp;
|
|
|
|
if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
|
|
|
|
params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2015-12-18 06:01:21 +00:00
|
|
|
if (params == NULL) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
|
2015-12-18 06:01:21 +00:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2015-12-18 06:01:21 +00:00
|
|
|
|
2015-12-19 21:58:04 +00:00
|
|
|
/* Set up DH, generate a key, and emit the public half. */
|
2015-12-18 06:01:21 +00:00
|
|
|
DH *dh = DHparams_dup(params);
|
2015-12-19 21:58:04 +00:00
|
|
|
if (dh == NULL) {
|
2014-12-19 01:42:32 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2015-12-18 06:01:21 +00:00
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
|
2015-12-18 06:01:21 +00:00
|
|
|
if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
|
2015-12-19 21:58:04 +00:00
|
|
|
!BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
|
2015-12-18 06:01:21 +00:00
|
|
|
!CBB_add_u16_length_prefixed(&cbb, &child) ||
|
2015-12-19 21:58:04 +00:00
|
|
|
!BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
|
2016-05-25 20:06:05 +01:00
|
|
|
!CBB_add_u16_length_prefixed(&cbb, &child) ||
|
2016-11-14 01:32:04 +00:00
|
|
|
!SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
|
2015-04-27 03:12:32 +01:00
|
|
|
goto err;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2015-03-19 15:10:48 +00:00
|
|
|
} else if (alg_k & SSL_kECDHE) {
|
2016-05-17 16:47:53 +01:00
|
|
|
/* Determine the group to use. */
|
|
|
|
uint16_t group_id;
|
2016-11-17 08:20:47 +00:00
|
|
|
if (!tls1_get_shared_group(hs, &group_id)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
|
2015-12-18 06:01:21 +00:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2016-12-11 07:48:12 +00:00
|
|
|
ssl->s3->new_session->group_id = group_id;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-12-19 05:18:25 +00:00
|
|
|
/* Set up ECDH, generate a key, and emit the public half. */
|
2016-11-14 01:32:04 +00:00
|
|
|
if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
|
2015-12-18 06:01:21 +00:00
|
|
|
!CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
|
2016-05-17 16:47:53 +01:00
|
|
|
!CBB_add_u16(&cbb, group_id) ||
|
2016-05-25 20:06:05 +01:00
|
|
|
!CBB_add_u8_length_prefixed(&cbb, &child) ||
|
2016-11-14 01:32:04 +00:00
|
|
|
!SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
|
2016-05-25 20:06:05 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2015-12-18 06:01:21 +00:00
|
|
|
} else {
|
|
|
|
assert(alg_k & SSL_kPSK);
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
if (!CBB_finish(&cbb, &hs->server_params, &hs->server_params_len)) {
|
2016-06-17 22:47:58 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assemble the message. */
|
2016-06-17 23:48:29 +01:00
|
|
|
CBB body;
|
|
|
|
if (!ssl->method->init_message(ssl, &cbb, &body,
|
|
|
|
SSL3_MT_SERVER_KEY_EXCHANGE) ||
|
2016-11-14 01:32:04 +00:00
|
|
|
!CBB_add_bytes(&body, hs->server_params, hs->server_params_len)) {
|
2015-12-18 06:01:21 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-12-18 06:01:21 +00:00
|
|
|
/* Add a signature. */
|
2016-09-12 23:40:27 +01:00
|
|
|
if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
|
2015-12-18 06:01:21 +00:00
|
|
|
if (!ssl_has_private_key(ssl)) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-06-17 22:47:58 +01:00
|
|
|
/* Determine the signature algorithm. */
|
2016-07-08 23:52:59 +01:00
|
|
|
uint16_t signature_algorithm;
|
2016-11-17 08:20:47 +00:00
|
|
|
if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
|
2016-07-08 23:52:59 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2016-06-17 22:47:58 +01:00
|
|
|
if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
|
2016-06-29 18:16:27 +01:00
|
|
|
if (!CBB_add_u16(&body, signature_algorithm)) {
|
2016-06-17 22:47:58 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
2015-11-27 00:39:08 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2016-06-17 22:47:58 +01:00
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-17 22:47:58 +01:00
|
|
|
/* Add space for the signature. */
|
|
|
|
const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
|
|
|
|
uint8_t *ptr;
|
2016-06-17 23:48:29 +01:00
|
|
|
if (!CBB_add_u16_length_prefixed(&body, &child) ||
|
2016-06-17 22:47:58 +01:00
|
|
|
!CBB_reserve(&child, &ptr, max_sig_len)) {
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-17 22:47:58 +01:00
|
|
|
size_t sig_len;
|
|
|
|
enum ssl_private_key_result_t sign_result;
|
2016-12-12 22:00:50 +00:00
|
|
|
if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
|
2016-06-30 18:27:23 +01:00
|
|
|
CBB transcript;
|
|
|
|
uint8_t *transcript_data;
|
|
|
|
size_t transcript_len;
|
|
|
|
if (!CBB_init(&transcript,
|
2016-11-14 01:32:04 +00:00
|
|
|
2 * SSL3_RANDOM_SIZE + hs->server_params_len) ||
|
|
|
|
!CBB_add_bytes(&transcript, ssl->s3->client_random,
|
|
|
|
SSL3_RANDOM_SIZE) ||
|
|
|
|
!CBB_add_bytes(&transcript, ssl->s3->server_random,
|
|
|
|
SSL3_RANDOM_SIZE) ||
|
|
|
|
!CBB_add_bytes(&transcript, hs->server_params,
|
|
|
|
hs->server_params_len) ||
|
2016-06-30 18:27:23 +01:00
|
|
|
!CBB_finish(&transcript, &transcript_data, &transcript_len)) {
|
|
|
|
CBB_cleanup(&transcript);
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
2016-06-29 18:16:27 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
|
2016-06-30 18:27:23 +01:00
|
|
|
signature_algorithm, transcript_data,
|
|
|
|
transcript_len);
|
|
|
|
OPENSSL_free(transcript_data);
|
2015-08-28 21:17:59 +01:00
|
|
|
} else {
|
2016-12-12 22:00:50 +00:00
|
|
|
assert(hs->state == SSL3_ST_SW_KEY_EXCH_B);
|
2016-07-14 19:52:41 +01:00
|
|
|
sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2015-08-28 21:17:59 +01:00
|
|
|
|
2015-12-18 06:01:21 +00:00
|
|
|
switch (sign_result) {
|
|
|
|
case ssl_private_key_success:
|
|
|
|
if (!CBB_did_write(&child, sig_len)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ssl_private_key_failure:
|
|
|
|
goto err;
|
|
|
|
case ssl_private_key_retry:
|
|
|
|
ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SW_KEY_EXCH_B;
|
2015-12-18 06:01:21 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2015-07-24 17:31:31 +01:00
|
|
|
}
|
|
|
|
|
Don't use the buffer BIO in TLS.
On the TLS side, we introduce a running buffer of ciphertext. Queuing up
pending data consists of encrypting the record into the buffer. This
effectively reimplements what the buffer BIO was doing previously, but
this resizes to fit the whole flight.
As part of this, rename all the functions to add to the pending flight
to be more uniform. This CL proposes "add_foo" to add to the pending
flight and "flush_flight" to drain it.
We add an add_alert hook for alerts but, for now, only the SSL 3.0
warning alert (sent mid-handshake) uses this mechanism. Later work will
push this down to the rest of the write path so closure alerts use it
too, as in DTLS. The intended end state is that all the ssl_buffer.c and
wpend_ret logic will only be used for application data and eventually
optionally replaced by the in-place API, while all "incidental" data
will be handled internally.
For now, the two buffers are mutually exclusive. Moving closure alerts
to "incidentals" will change this, but flushing application data early
is tricky due to wpend_ret. (If we call ssl_write_buffer_flush,
do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That
too is all left alone in this change.
To keep the diff down, write_message is retained for now and will be
removed from the state machines in a follow-up change.
BUG=72
Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d
Reviewed-on: https://boringssl-review.googlesource.com/13224
Reviewed-by: Adam Langley <agl@google.com>
2017-01-03 23:37:41 +00:00
|
|
|
if (!ssl_add_message_cbb(ssl, &cbb)) {
|
2015-08-28 21:17:59 +01:00
|
|
|
goto err;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2016-06-17 22:47:58 +01:00
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
OPENSSL_free(hs->server_params);
|
|
|
|
hs->server_params = NULL;
|
|
|
|
hs->server_params_len = 0;
|
2016-06-17 22:47:58 +01:00
|
|
|
|
2017-01-13 01:02:05 +00:00
|
|
|
return 1;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
err:
|
2015-12-18 06:01:21 +00:00
|
|
|
CBB_cleanup(&cbb);
|
2014-12-19 01:42:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-06-17 23:48:29 +01:00
|
|
|
static int add_cert_types(SSL *ssl, CBB *cbb) {
|
|
|
|
/* Get configured signature algorithms. */
|
|
|
|
int have_rsa_sign = 0;
|
|
|
|
int have_ecdsa_sign = 0;
|
2016-06-23 18:33:05 +01:00
|
|
|
const uint16_t *sig_algs;
|
2016-10-17 22:59:54 +01:00
|
|
|
size_t num_sig_algs = tls12_get_verify_sigalgs(ssl, &sig_algs);
|
|
|
|
for (size_t i = 0; i < num_sig_algs; i++) {
|
2016-06-23 18:33:05 +01:00
|
|
|
switch (sig_algs[i]) {
|
|
|
|
case SSL_SIGN_RSA_PKCS1_SHA512:
|
|
|
|
case SSL_SIGN_RSA_PKCS1_SHA384:
|
|
|
|
case SSL_SIGN_RSA_PKCS1_SHA256:
|
|
|
|
case SSL_SIGN_RSA_PKCS1_SHA1:
|
2016-06-17 23:48:29 +01:00
|
|
|
have_rsa_sign = 1;
|
|
|
|
break;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-23 18:33:05 +01:00
|
|
|
case SSL_SIGN_ECDSA_SECP521R1_SHA512:
|
|
|
|
case SSL_SIGN_ECDSA_SECP384R1_SHA384:
|
|
|
|
case SSL_SIGN_ECDSA_SECP256R1_SHA256:
|
|
|
|
case SSL_SIGN_ECDSA_SHA1:
|
2016-06-17 23:48:29 +01:00
|
|
|
have_ecdsa_sign = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-17 23:48:29 +01:00
|
|
|
if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-17 23:48:29 +01:00
|
|
|
/* ECDSA certs can be used with RSA cipher suites as well so we don't need to
|
|
|
|
* check for SSL_kECDH or SSL_kECDHE. */
|
|
|
|
if (ssl->version >= TLS1_VERSION && have_ecdsa_sign &&
|
|
|
|
!CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-17 23:48:29 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2016-07-14 03:03:11 +01:00
|
|
|
CBB cbb, body, cert_types, sigalgs_cbb;
|
2016-06-17 23:48:29 +01:00
|
|
|
if (!ssl->method->init_message(ssl, &cbb, &body,
|
|
|
|
SSL3_MT_CERTIFICATE_REQUEST) ||
|
|
|
|
!CBB_add_u8_length_prefixed(&body, &cert_types) ||
|
|
|
|
!add_cert_types(ssl, &cert_types)) {
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-17 23:48:29 +01:00
|
|
|
if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
|
2016-06-23 18:33:05 +01:00
|
|
|
const uint16_t *sigalgs;
|
2016-10-17 22:59:54 +01:00
|
|
|
size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
|
2016-06-23 18:33:05 +01:00
|
|
|
if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) {
|
2015-02-17 00:33:53 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2016-06-23 18:33:05 +01:00
|
|
|
|
2016-08-17 20:29:46 +01:00
|
|
|
for (size_t i = 0; i < num_sigalgs; i++) {
|
2016-06-23 18:33:05 +01:00
|
|
|
if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 03:03:11 +01:00
|
|
|
if (!ssl_add_client_CA_list(ssl, &body) ||
|
Don't use the buffer BIO in TLS.
On the TLS side, we introduce a running buffer of ciphertext. Queuing up
pending data consists of encrypting the record into the buffer. This
effectively reimplements what the buffer BIO was doing previously, but
this resizes to fit the whole flight.
As part of this, rename all the functions to add to the pending flight
to be more uniform. This CL proposes "add_foo" to add to the pending
flight and "flush_flight" to drain it.
We add an add_alert hook for alerts but, for now, only the SSL 3.0
warning alert (sent mid-handshake) uses this mechanism. Later work will
push this down to the rest of the write path so closure alerts use it
too, as in DTLS. The intended end state is that all the ssl_buffer.c and
wpend_ret logic will only be used for application data and eventually
optionally replaced by the in-place API, while all "incidental" data
will be handled internally.
For now, the two buffers are mutually exclusive. Moving closure alerts
to "incidentals" will change this, but flushing application data early
is tricky due to wpend_ret. (If we call ssl_write_buffer_flush,
do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That
too is all left alone in this change.
To keep the diff down, write_message is retained for now and will be
removed from the state machines in a follow-up change.
BUG=72
Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d
Reviewed-on: https://boringssl-review.googlesource.com/13224
Reviewed-by: Adam Langley <agl@google.com>
2017-01-03 23:37:41 +00:00
|
|
|
!ssl_add_message_cbb(ssl, &cbb)) {
|
2016-06-17 23:48:29 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-01-13 01:02:05 +00:00
|
|
|
return 1;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
err:
|
2016-06-17 23:48:29 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
CBB_cleanup(&cbb);
|
2014-12-19 01:42:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2016-06-17 23:48:29 +01:00
|
|
|
CBB cbb, body;
|
|
|
|
if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
|
Don't use the buffer BIO in TLS.
On the TLS side, we introduce a running buffer of ciphertext. Queuing up
pending data consists of encrypting the record into the buffer. This
effectively reimplements what the buffer BIO was doing previously, but
this resizes to fit the whole flight.
As part of this, rename all the functions to add to the pending flight
to be more uniform. This CL proposes "add_foo" to add to the pending
flight and "flush_flight" to drain it.
We add an add_alert hook for alerts but, for now, only the SSL 3.0
warning alert (sent mid-handshake) uses this mechanism. Later work will
push this down to the rest of the write path so closure alerts use it
too, as in DTLS. The intended end state is that all the ssl_buffer.c and
wpend_ret logic will only be used for application data and eventually
optionally replaced by the in-place API, while all "incidental" data
will be handled internally.
For now, the two buffers are mutually exclusive. Moving closure alerts
to "incidentals" will change this, but flushing application data early
is tricky due to wpend_ret. (If we call ssl_write_buffer_flush,
do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That
too is all left alone in this change.
To keep the diff down, write_message is retained for now and will be
removed from the state machines in a follow-up change.
BUG=72
Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d
Reviewed-on: https://boringssl-review.googlesource.com/13224
Reviewed-by: Adam Langley <agl@google.com>
2017-01-03 23:37:41 +00:00
|
|
|
!ssl_add_message_cbb(ssl, &cbb)) {
|
2016-06-17 23:48:29 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
CBB_cleanup(&cbb);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-01-13 01:02:05 +00:00
|
|
|
return 1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
|
|
|
assert(hs->cert_request);
|
2016-06-07 22:03:46 +01:00
|
|
|
|
2017-01-21 19:49:39 +00:00
|
|
|
int msg_ret = ssl->method->ssl_get_message(ssl);
|
2016-07-08 22:32:11 +01:00
|
|
|
if (msg_ret <= 0) {
|
|
|
|
return msg_ret;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
|
|
|
|
if (ssl->version == SSL3_VERSION &&
|
|
|
|
ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
|
2016-08-02 00:16:46 +01:00
|
|
|
/* In SSL 3.0, the Certificate message is omitted to signal no
|
|
|
|
* certificate. */
|
2016-08-02 00:57:43 +01:00
|
|
|
if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
|
2016-06-07 22:03:46 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
|
2016-07-14 04:03:26 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
|
|
|
|
return -1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2016-08-02 00:16:46 +01:00
|
|
|
/* OpenSSL returns X509_V_OK when no certificates are received. This is
|
2016-08-18 21:40:28 +01:00
|
|
|
* classed by them as a bug, but it's assumed by at least NGINX. */
|
2016-08-02 00:16:46 +01:00
|
|
|
ssl->s3->new_session->verify_result = X509_V_OK;
|
2016-06-07 22:03:46 +01:00
|
|
|
ssl->s3->tmp.reuse_message = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
|
2016-07-14 04:03:26 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
|
|
|
|
return -1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2017-01-21 19:49:39 +00:00
|
|
|
if (!ssl_hash_current_message(ssl)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-07-14 04:03:26 +01:00
|
|
|
CBS certificate_msg;
|
2016-07-08 22:32:11 +01:00
|
|
|
CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
|
2016-12-12 19:06:16 +00:00
|
|
|
|
|
|
|
sk_CRYPTO_BUFFER_pop_free(ssl->s3->new_session->certs, CRYPTO_BUFFER_free);
|
2016-12-12 19:37:43 +00:00
|
|
|
EVP_PKEY_free(hs->peer_pubkey);
|
|
|
|
hs->peer_pubkey = NULL;
|
2016-07-14 04:03:26 +01:00
|
|
|
uint8_t alert;
|
2016-12-12 19:06:16 +00:00
|
|
|
ssl->s3->new_session->certs =
|
2016-12-12 19:37:43 +00:00
|
|
|
ssl_parse_cert_chain(&alert, &hs->peer_pubkey,
|
|
|
|
ssl->retain_only_sha256_of_client_certs
|
|
|
|
? ssl->s3->new_session->peer_sha256
|
|
|
|
: NULL,
|
2016-12-12 19:16:44 +00:00
|
|
|
&certificate_msg, ssl->ctx->pool);
|
2016-12-12 19:06:16 +00:00
|
|
|
if (ssl->s3->new_session->certs == NULL) {
|
2016-07-14 04:03:26 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
|
2016-12-12 19:06:16 +00:00
|
|
|
return -1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2016-12-12 19:06:16 +00:00
|
|
|
if (CBS_len(&certificate_msg) != 0 ||
|
|
|
|
!ssl_session_x509_cache_objects(ssl->s3->new_session)) {
|
2016-06-07 22:03:46 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2016-07-14 04:03:26 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2016-12-12 19:06:16 +00:00
|
|
|
return -1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2016-12-12 19:06:16 +00:00
|
|
|
if (sk_CRYPTO_BUFFER_num(ssl->s3->new_session->certs) == 0) {
|
2016-06-07 22:03:46 +01:00
|
|
|
/* No client certificate so the handshake buffer may be discarded. */
|
|
|
|
ssl3_free_handshake_buffer(ssl);
|
|
|
|
|
2016-08-02 01:11:13 +01:00
|
|
|
/* In SSL 3.0, sending no certificate is signaled by omitting the
|
|
|
|
* Certificate message. */
|
2016-06-07 22:03:46 +01:00
|
|
|
if (ssl->version == SSL3_VERSION) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
|
2016-07-14 04:03:26 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
|
2016-12-12 19:06:16 +00:00
|
|
|
return -1;
|
2016-08-02 01:11:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
|
2016-06-07 22:03:46 +01:00
|
|
|
/* Fail for TLS only if we required a certificate */
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
|
2016-07-14 04:03:26 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
|
2016-12-12 19:06:16 +00:00
|
|
|
return -1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
2016-08-02 00:16:46 +01:00
|
|
|
|
|
|
|
/* OpenSSL returns X509_V_OK when no certificates are received. This is
|
2016-08-18 21:40:28 +01:00
|
|
|
* classed by them as a bug, but it's assumed by at least NGINX. */
|
2016-08-09 02:38:32 +01:00
|
|
|
ssl->s3->new_session->verify_result = X509_V_OK;
|
2016-12-12 19:06:16 +00:00
|
|
|
return 1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2016-12-12 19:06:16 +00:00
|
|
|
/* The hash will have been filled in. */
|
|
|
|
if (ssl->retain_only_sha256_of_client_certs) {
|
|
|
|
ssl->s3->new_session->peer_sha256_valid = 1;
|
2016-12-12 18:51:00 +00:00
|
|
|
}
|
2016-06-07 22:03:46 +01:00
|
|
|
|
2016-12-12 19:06:16 +00:00
|
|
|
if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
|
|
|
|
ssl->s3->new_session->x509_chain)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-07-14 04:03:26 +01:00
|
|
|
return 1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2015-08-07 22:07:52 +01:00
|
|
|
int al;
|
2014-12-19 01:42:32 +00:00
|
|
|
CBS client_key_exchange;
|
2015-04-18 03:32:19 +01:00
|
|
|
uint32_t alg_k;
|
|
|
|
uint32_t alg_a;
|
2014-12-19 01:42:32 +00:00
|
|
|
uint8_t *premaster_secret = NULL;
|
|
|
|
size_t premaster_secret_len = 0;
|
|
|
|
uint8_t *decrypt_buf = NULL;
|
|
|
|
|
2015-12-19 05:18:25 +00:00
|
|
|
unsigned psk_len = 0;
|
2014-12-19 01:42:32 +00:00
|
|
|
uint8_t psk[PSK_MAX_PSK_LEN];
|
|
|
|
|
2016-12-12 22:00:50 +00:00
|
|
|
if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
|
2017-01-21 19:49:39 +00:00
|
|
|
int ret = ssl->method->ssl_get_message(ssl);
|
2016-07-08 22:32:11 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
2015-08-07 22:07:52 +01:00
|
|
|
}
|
2017-01-21 19:13:39 +00:00
|
|
|
|
2017-01-21 19:49:39 +00:00
|
|
|
if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE) ||
|
|
|
|
!ssl_hash_current_message(ssl)) {
|
2017-01-21 19:13:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
|
|
|
|
alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
|
|
|
|
alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
/* If using a PSK key exchange, prepare the pre-shared key. */
|
|
|
|
if (alg_a & SSL_aPSK) {
|
|
|
|
CBS psk_identity;
|
|
|
|
|
|
|
|
/* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
|
|
|
|
* then this is the only field in the message. */
|
|
|
|
if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
|
|
|
|
((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2014-12-19 01:42:32 +00:00
|
|
|
al = SSL_AD_DECODE_ERROR;
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->psk_server_callback == NULL) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
|
2014-12-19 01:42:32 +00:00
|
|
|
al = SSL_AD_INTERNAL_ERROR;
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
|
|
|
|
CBS_contains_zero_byte(&psk_identity)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
|
2014-12-19 01:42:32 +00:00
|
|
|
al = SSL_AD_ILLEGAL_PARAMETER;
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
if (!CBS_strdup(&psk_identity, &ssl->s3->new_session->psk_identity)) {
|
2014-12-19 01:42:32 +00:00
|
|
|
al = SSL_AD_INTERNAL_ERROR;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look up the key for the identity. */
|
2016-06-27 21:34:59 +01:00
|
|
|
psk_len = ssl->psk_server_callback(ssl, ssl->s3->new_session->psk_identity,
|
|
|
|
psk, sizeof(psk));
|
2014-12-19 01:42:32 +00:00
|
|
|
if (psk_len > PSK_MAX_PSK_LEN) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2014-12-19 01:42:32 +00:00
|
|
|
al = SSL_AD_INTERNAL_ERROR;
|
|
|
|
goto f_err;
|
|
|
|
} else if (psk_len == 0) {
|
|
|
|
/* PSK related to the given identity not found */
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
|
2014-12-19 01:42:32 +00:00
|
|
|
al = SSL_AD_UNKNOWN_PSK_IDENTITY;
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Depending on the key exchange method, compute |premaster_secret| and
|
|
|
|
* |premaster_secret_len|. */
|
|
|
|
if (alg_k & SSL_kRSA) {
|
2015-08-07 22:07:52 +01:00
|
|
|
/* Allocate a buffer large enough for an RSA decryption. */
|
2015-12-19 22:05:56 +00:00
|
|
|
const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
|
2015-08-07 22:07:52 +01:00
|
|
|
decrypt_buf = OPENSSL_malloc(rsa_size);
|
|
|
|
if (decrypt_buf == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2015-10-17 03:45:17 +01:00
|
|
|
|
2015-08-07 22:07:52 +01:00
|
|
|
enum ssl_private_key_result_t decrypt_result;
|
2015-12-02 00:31:24 +00:00
|
|
|
size_t decrypt_len;
|
2016-12-12 22:00:50 +00:00
|
|
|
if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!ssl_has_private_key(ssl) ||
|
2016-07-14 18:47:55 +01:00
|
|
|
ssl_private_key_type(ssl) != NID_rsaEncryption) {
|
2015-08-07 22:07:52 +01:00
|
|
|
al = SSL_AD_HANDSHAKE_FAILURE;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
|
|
|
|
goto f_err;
|
|
|
|
}
|
2015-12-02 00:31:24 +00:00
|
|
|
CBS encrypted_premaster_secret;
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->version > SSL3_VERSION) {
|
2015-08-07 22:07:52 +01:00
|
|
|
if (!CBS_get_u16_length_prefixed(&client_key_exchange,
|
|
|
|
&encrypted_premaster_secret) ||
|
|
|
|
CBS_len(&client_key_exchange) != 0) {
|
2015-11-19 01:35:31 +00:00
|
|
|
al = SSL_AD_DECODE_ERROR;
|
|
|
|
OPENSSL_PUT_ERROR(SSL,
|
|
|
|
SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
|
|
|
|
goto f_err;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2015-08-07 22:07:52 +01:00
|
|
|
} else {
|
|
|
|
encrypted_premaster_secret = client_key_exchange;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 22:07:52 +01:00
|
|
|
/* Decrypt with no padding. PKCS#1 padding will be removed as part of the
|
|
|
|
* timing-sensitive code below. */
|
|
|
|
decrypt_result = ssl_private_key_decrypt(
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl, decrypt_buf, &decrypt_len, rsa_size,
|
2015-08-07 22:07:52 +01:00
|
|
|
CBS_data(&encrypted_premaster_secret),
|
|
|
|
CBS_len(&encrypted_premaster_secret));
|
|
|
|
} else {
|
2016-12-12 22:00:50 +00:00
|
|
|
assert(hs->state == SSL3_ST_SR_KEY_EXCH_B);
|
2015-08-07 22:07:52 +01:00
|
|
|
/* Complete async decrypt. */
|
2016-07-14 19:52:41 +01:00
|
|
|
decrypt_result =
|
|
|
|
ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size);
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 22:07:52 +01:00
|
|
|
switch (decrypt_result) {
|
|
|
|
case ssl_private_key_success:
|
|
|
|
break;
|
|
|
|
case ssl_private_key_failure:
|
|
|
|
goto err;
|
|
|
|
case ssl_private_key_retry:
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
|
2016-12-12 22:00:50 +00:00
|
|
|
hs->state = SSL3_ST_SR_KEY_EXCH_B;
|
2015-08-07 22:07:52 +01:00
|
|
|
goto err;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-04-14 22:18:02 +01:00
|
|
|
if (decrypt_len != rsa_size) {
|
|
|
|
al = SSL_AD_DECRYPT_ERROR;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
|
|
|
|
goto f_err;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-12-02 00:31:24 +00:00
|
|
|
/* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
|
|
|
|
* section 7.4.7.1. */
|
|
|
|
premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
|
|
|
|
premaster_secret = OPENSSL_malloc(premaster_secret_len);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (premaster_secret == NULL) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2015-12-02 00:31:24 +00:00
|
|
|
if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-12-02 00:31:24 +00:00
|
|
|
/* The smallest padded premaster is 11 bytes of overhead. Small keys are
|
|
|
|
* publicly invalid. */
|
|
|
|
if (decrypt_len < 11 + premaster_secret_len) {
|
|
|
|
al = SSL_AD_DECRYPT_ERROR;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
|
|
|
|
goto f_err;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-12-02 00:31:24 +00:00
|
|
|
/* Check the padding. See RFC 3447, section 7.2.2. */
|
|
|
|
size_t padding_len = decrypt_len - premaster_secret_len;
|
|
|
|
uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
|
|
|
|
constant_time_eq_int_8(decrypt_buf[1], 2);
|
2016-09-05 17:47:25 +01:00
|
|
|
for (size_t i = 2; i < padding_len - 1; i++) {
|
2015-12-02 00:31:24 +00:00
|
|
|
good &= ~constant_time_is_zero_8(decrypt_buf[i]);
|
2015-08-07 22:07:52 +01:00
|
|
|
}
|
2015-12-02 00:31:24 +00:00
|
|
|
good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
|
2015-08-07 22:07:52 +01:00
|
|
|
|
2015-12-02 00:31:24 +00:00
|
|
|
/* The premaster secret must begin with |client_version|. This too must be
|
|
|
|
* checked in constant time (http://eprint.iacr.org/2003/052/). */
|
|
|
|
good &= constant_time_eq_8(decrypt_buf[padding_len],
|
2016-12-06 18:35:25 +00:00
|
|
|
(unsigned)(hs->client_version >> 8));
|
2015-12-02 00:31:24 +00:00
|
|
|
good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
|
2016-12-06 18:35:25 +00:00
|
|
|
(unsigned)(hs->client_version & 0xff));
|
2015-12-02 00:31:24 +00:00
|
|
|
|
|
|
|
/* Select, in constant time, either the decrypted premaster or the random
|
|
|
|
* premaster based on |good|. */
|
2016-09-05 17:47:25 +01:00
|
|
|
for (size_t i = 0; i < premaster_secret_len; i++) {
|
2015-12-02 00:31:24 +00:00
|
|
|
premaster_secret[i] = constant_time_select_8(
|
|
|
|
good, decrypt_buf[padding_len + i], premaster_secret[i]);
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 00:31:24 +00:00
|
|
|
OPENSSL_free(decrypt_buf);
|
|
|
|
decrypt_buf = NULL;
|
2016-12-09 00:14:36 +00:00
|
|
|
} else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
|
2016-05-17 00:31:05 +01:00
|
|
|
/* Parse the ClientKeyExchange. */
|
2015-12-19 21:58:04 +00:00
|
|
|
CBS peer_key;
|
2016-11-14 01:32:04 +00:00
|
|
|
if (!SSL_ECDH_CTX_get_key(&hs->ecdh_ctx, &client_key_exchange, &peer_key) ||
|
2016-05-17 00:31:05 +01:00
|
|
|
CBS_len(&client_key_exchange) != 0) {
|
2014-12-19 01:42:32 +00:00
|
|
|
al = SSL_AD_DECODE_ERROR;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2015-12-19 05:18:25 +00:00
|
|
|
/* Compute the premaster. */
|
|
|
|
uint8_t alert;
|
2016-11-14 01:32:04 +00:00
|
|
|
if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, &premaster_secret,
|
2016-05-16 22:27:14 +01:00
|
|
|
&premaster_secret_len, &alert, CBS_data(&peer_key),
|
|
|
|
CBS_len(&peer_key))) {
|
2015-12-19 05:18:25 +00:00
|
|
|
al = alert;
|
|
|
|
goto f_err;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 05:18:25 +00:00
|
|
|
/* The key exchange state may now be discarded. */
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
|
2014-12-19 01:42:32 +00:00
|
|
|
} else if (alg_k & SSL_kPSK) {
|
|
|
|
/* For plain PSK, other_secret is a block of 0s with the same length as the
|
|
|
|
* pre-shared key. */
|
|
|
|
premaster_secret_len = psk_len;
|
|
|
|
premaster_secret = OPENSSL_malloc(premaster_secret_len);
|
|
|
|
if (premaster_secret == NULL) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
|
2014-12-19 01:42:32 +00:00
|
|
|
} else {
|
|
|
|
al = SSL_AD_HANDSHAKE_FAILURE;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For a PSK cipher suite, the actual pre-master secret is combined with the
|
|
|
|
* pre-shared key. */
|
|
|
|
if (alg_a & SSL_aPSK) {
|
|
|
|
CBB new_premaster, child;
|
|
|
|
uint8_t *new_data;
|
|
|
|
size_t new_len;
|
|
|
|
|
2015-06-28 06:26:10 +01:00
|
|
|
CBB_zero(&new_premaster);
|
|
|
|
if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
|
|
|
|
!CBB_add_u16_length_prefixed(&new_premaster, &child) ||
|
2014-12-19 01:42:32 +00:00
|
|
|
!CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
|
|
|
|
!CBB_add_u16_length_prefixed(&new_premaster, &child) ||
|
|
|
|
!CBB_add_bytes(&child, psk, psk_len) ||
|
|
|
|
!CBB_finish(&new_premaster, &new_data, &new_len)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
2014-12-19 01:42:32 +00:00
|
|
|
CBB_cleanup(&new_premaster);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
OPENSSL_cleanse(premaster_secret, premaster_secret_len);
|
|
|
|
OPENSSL_free(premaster_secret);
|
|
|
|
premaster_secret = new_data;
|
|
|
|
premaster_secret_len = new_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the master secret */
|
2016-06-27 21:34:59 +01:00
|
|
|
ssl->s3->new_session->master_key_length = tls1_generate_master_secret(
|
|
|
|
ssl, ssl->s3->new_session->master_key, premaster_secret,
|
|
|
|
premaster_secret_len);
|
|
|
|
if (ssl->s3->new_session->master_key_length == 0) {
|
2014-12-19 01:42:32 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2016-06-27 21:34:59 +01:00
|
|
|
ssl->s3->new_session->extended_master_secret =
|
|
|
|
ssl->s3->tmp.extended_master_secret;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
OPENSSL_cleanse(premaster_secret, premaster_secret_len);
|
|
|
|
OPENSSL_free(premaster_secret);
|
|
|
|
return 1;
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
f_err:
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
|
2014-06-20 20:00:00 +01:00
|
|
|
err:
|
2015-12-19 05:18:25 +00:00
|
|
|
if (premaster_secret != NULL) {
|
|
|
|
OPENSSL_cleanse(premaster_secret, premaster_secret_len);
|
2014-12-19 01:42:32 +00:00
|
|
|
OPENSSL_free(premaster_secret);
|
|
|
|
}
|
2015-04-22 21:17:58 +01:00
|
|
|
OPENSSL_free(decrypt_buf);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2016-12-12 19:37:43 +00:00
|
|
|
int al;
|
2014-12-19 01:42:32 +00:00
|
|
|
CBS certificate_verify, signature;
|
|
|
|
|
|
|
|
/* Only RSA and ECDSA client certificates are supported, so a
|
|
|
|
* CertificateVerify is required if and only if there's a client certificate.
|
|
|
|
* */
|
2016-12-12 19:37:43 +00:00
|
|
|
if (hs->peer_pubkey == NULL) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl3_free_handshake_buffer(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-21 19:49:39 +00:00
|
|
|
int msg_ret = ssl->method->ssl_get_message(ssl);
|
2016-07-08 22:32:11 +01:00
|
|
|
if (msg_ret <= 0) {
|
|
|
|
return msg_ret;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 19:13:39 +00:00
|
|
|
if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-07-08 22:32:11 +01:00
|
|
|
CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
/* Determine the digest type if needbe. */
|
2016-06-29 18:16:27 +01:00
|
|
|
uint16_t signature_algorithm = 0;
|
2015-12-30 05:08:49 +00:00
|
|
|
if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
|
2016-06-23 18:33:05 +01:00
|
|
|
if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
|
2015-11-02 17:02:20 +00:00
|
|
|
al = SSL_AD_DECODE_ERROR;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
goto f_err;
|
|
|
|
}
|
2016-07-09 00:15:32 +01:00
|
|
|
if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
|
2015-11-02 17:02:20 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
2016-12-14 01:05:36 +00:00
|
|
|
ssl->s3->new_session->peer_signature_algorithm = signature_algorithm;
|
2016-12-12 19:37:43 +00:00
|
|
|
} else if (hs->peer_pubkey->type == EVP_PKEY_RSA) {
|
2016-06-29 18:16:27 +01:00
|
|
|
signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
|
2016-12-12 19:37:43 +00:00
|
|
|
} else if (hs->peer_pubkey->type == EVP_PKEY_EC) {
|
2016-06-29 18:16:27 +01:00
|
|
|
signature_algorithm = SSL_SIGN_ECDSA_SHA1;
|
2016-07-14 05:11:26 +01:00
|
|
|
} else {
|
|
|
|
al = SSL_AD_UNSUPPORTED_CERTIFICATE;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
|
|
|
|
goto f_err;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse and verify the signature. */
|
|
|
|
if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
|
|
|
|
CBS_len(&certificate_verify) != 0) {
|
|
|
|
al = SSL_AD_DECODE_ERROR;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2016-06-30 18:27:23 +01:00
|
|
|
int sig_ok;
|
|
|
|
/* The SSL3 construction for CertificateVerify does not decompose into a
|
|
|
|
* single final digest and signature, and must be special-cased. */
|
|
|
|
if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
|
2016-07-08 22:44:56 +01:00
|
|
|
const EVP_MD *md;
|
2016-06-30 18:27:23 +01:00
|
|
|
uint8_t digest[EVP_MAX_MD_SIZE];
|
|
|
|
size_t digest_len;
|
2016-07-08 22:44:56 +01:00
|
|
|
if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
|
2016-06-30 18:27:23 +01:00
|
|
|
signature_algorithm)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-12-12 19:37:43 +00:00
|
|
|
EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(hs->peer_pubkey, NULL);
|
2016-06-30 18:27:23 +01:00
|
|
|
sig_ok = pctx != NULL &&
|
|
|
|
EVP_PKEY_verify_init(pctx) &&
|
|
|
|
EVP_PKEY_CTX_set_signature_md(pctx, md) &&
|
|
|
|
EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
|
|
|
|
digest, digest_len);
|
|
|
|
EVP_PKEY_CTX_free(pctx);
|
|
|
|
} else {
|
|
|
|
sig_ok = ssl_public_key_verify(
|
|
|
|
ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
|
2016-12-12 19:37:43 +00:00
|
|
|
hs->peer_pubkey, (const uint8_t *)ssl->s3->handshake_buffer->data,
|
2016-06-30 18:27:23 +01:00
|
|
|
ssl->s3->handshake_buffer->length);
|
2016-06-29 18:16:27 +01:00
|
|
|
}
|
|
|
|
|
2016-03-02 03:57:40 +00:00
|
|
|
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
|
|
|
|
sig_ok = 1;
|
|
|
|
ERR_clear_error();
|
|
|
|
#endif
|
|
|
|
if (!sig_ok) {
|
2014-12-19 01:42:32 +00:00
|
|
|
al = SSL_AD_DECRYPT_ERROR;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2016-06-30 18:27:23 +01:00
|
|
|
/* The handshake buffer is no longer necessary, and we may hash the current
|
|
|
|
* message.*/
|
|
|
|
ssl3_free_handshake_buffer(ssl);
|
2016-11-14 08:12:11 +00:00
|
|
|
if (!ssl_hash_current_message(ssl)) {
|
2016-06-30 18:27:23 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-12-12 19:37:43 +00:00
|
|
|
return 1;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-12-12 19:37:43 +00:00
|
|
|
f_err:
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
|
2014-08-26 05:32:30 +01:00
|
|
|
err:
|
2016-12-12 19:37:43 +00:00
|
|
|
return 0;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
|
|
|
|
* sets the next_proto member in s if found */
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2017-01-21 19:49:39 +00:00
|
|
|
int ret = ssl->method->ssl_get_message(ssl);
|
2016-07-08 22:32:11 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 19:49:39 +00:00
|
|
|
if (!ssl_check_message_type(ssl, SSL3_MT_NEXT_PROTO) ||
|
|
|
|
!ssl_hash_current_message(ssl)) {
|
2017-01-21 19:13:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-07-08 22:32:11 +01:00
|
|
|
CBS next_protocol, selected_protocol, padding;
|
|
|
|
CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
|
2016-06-07 22:03:46 +01:00
|
|
|
if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
|
|
|
|
!CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
|
2016-09-09 16:41:18 +01:00
|
|
|
CBS_len(&next_protocol) != 0) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
|
2016-06-07 22:03:46 +01:00
|
|
|
&ssl->s3->next_proto_negotiated_len)) {
|
|
|
|
return 0;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2017-01-21 19:49:39 +00:00
|
|
|
int msg_ret = ssl->method->ssl_get_message(ssl);
|
2016-07-08 22:32:11 +01:00
|
|
|
if (msg_ret <= 0) {
|
|
|
|
return msg_ret;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 19:13:39 +00:00
|
|
|
if (!ssl_check_message_type(ssl, SSL3_MT_CHANNEL_ID) ||
|
|
|
|
!tls1_verify_channel_id(ssl) ||
|
2016-11-14 08:12:11 +00:00
|
|
|
!ssl_hash_current_message(ssl)) {
|
2016-06-07 22:03:46 +01:00
|
|
|
return -1;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2016-09-24 00:25:11 +01:00
|
|
|
return 1;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2016-11-03 20:59:25 +00:00
|
|
|
const SSL_SESSION *session;
|
|
|
|
SSL_SESSION *session_copy = NULL;
|
|
|
|
if (ssl->session == NULL) {
|
|
|
|
/* Fix the timeout to measure from the ticket issuance time. */
|
2017-01-28 19:00:32 +00:00
|
|
|
ssl_session_rebase_time(ssl, ssl->s3->new_session);
|
2016-11-03 20:59:25 +00:00
|
|
|
session = ssl->s3->new_session;
|
|
|
|
} else {
|
|
|
|
/* We are renewing an existing session. Duplicate the session to adjust the
|
|
|
|
* timeout. */
|
|
|
|
session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
|
|
|
|
if (session_copy == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-01-28 19:00:32 +00:00
|
|
|
ssl_session_rebase_time(ssl, session_copy);
|
2016-11-03 20:59:25 +00:00
|
|
|
session = session_copy;
|
|
|
|
}
|
|
|
|
|
2016-06-17 23:48:29 +01:00
|
|
|
CBB cbb, body, ticket;
|
2016-11-03 20:59:25 +00:00
|
|
|
int ok =
|
|
|
|
ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) &&
|
|
|
|
CBB_add_u32(&body, session->timeout) &&
|
|
|
|
CBB_add_u16_length_prefixed(&body, &ticket) &&
|
|
|
|
ssl_encrypt_ticket(ssl, &ticket, session) &&
|
Don't use the buffer BIO in TLS.
On the TLS side, we introduce a running buffer of ciphertext. Queuing up
pending data consists of encrypting the record into the buffer. This
effectively reimplements what the buffer BIO was doing previously, but
this resizes to fit the whole flight.
As part of this, rename all the functions to add to the pending flight
to be more uniform. This CL proposes "add_foo" to add to the pending
flight and "flush_flight" to drain it.
We add an add_alert hook for alerts but, for now, only the SSL 3.0
warning alert (sent mid-handshake) uses this mechanism. Later work will
push this down to the rest of the write path so closure alerts use it
too, as in DTLS. The intended end state is that all the ssl_buffer.c and
wpend_ret logic will only be used for application data and eventually
optionally replaced by the in-place API, while all "incidental" data
will be handled internally.
For now, the two buffers are mutually exclusive. Moving closure alerts
to "incidentals" will change this, but flushing application data early
is tricky due to wpend_ret. (If we call ssl_write_buffer_flush,
do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That
too is all left alone in this change.
To keep the diff down, write_message is retained for now and will be
removed from the state machines in a follow-up change.
BUG=72
Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d
Reviewed-on: https://boringssl-review.googlesource.com/13224
Reviewed-by: Adam Langley <agl@google.com>
2017-01-03 23:37:41 +00:00
|
|
|
ssl_add_message_cbb(ssl, &cbb);
|
2016-11-03 20:59:25 +00:00
|
|
|
|
|
|
|
SSL_SESSION_free(session_copy);
|
|
|
|
CBB_cleanup(&cbb);
|
|
|
|
|
|
|
|
if (!ok) {
|
2016-11-03 22:06:27 +00:00
|
|
|
return -1;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 01:02:05 +00:00
|
|
|
return 1;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|