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>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/sha.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#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
|
|
|
#include "../crypto/dh/internal.h"
|
|
|
|
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-07 21:49:42 +01:00
|
|
|
static int ssl3_get_initial_bytes(SSL *ssl);
|
|
|
|
static int ssl3_get_v2_client_hello(SSL *ssl);
|
|
|
|
static int ssl3_get_client_hello(SSL *ssl);
|
|
|
|
static int ssl3_send_server_hello(SSL *ssl);
|
2016-06-07 22:03:46 +01:00
|
|
|
static int ssl3_send_server_certificate(SSL *ssl);
|
2016-06-07 21:49:42 +01:00
|
|
|
static int ssl3_send_certificate_status(SSL *ssl);
|
|
|
|
static int ssl3_send_server_key_exchange(SSL *ssl);
|
|
|
|
static int ssl3_send_certificate_request(SSL *ssl);
|
2016-06-07 22:13:28 +01:00
|
|
|
static int ssl3_send_server_hello_done(SSL *ssl);
|
2016-06-07 22:03:46 +01:00
|
|
|
static int ssl3_get_client_certificate(SSL *ssl);
|
2016-06-07 21:49:42 +01:00
|
|
|
static int ssl3_get_client_key_exchange(SSL *ssl);
|
|
|
|
static int ssl3_get_cert_verify(SSL *ssl);
|
|
|
|
static int ssl3_get_next_proto(SSL *ssl);
|
|
|
|
static int ssl3_get_channel_id(SSL *ssl);
|
2016-06-07 22:03:46 +01:00
|
|
|
static int ssl3_send_new_session_ticket(SSL *ssl);
|
2016-06-07 21:49:42 +01:00
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
int ssl3_accept(SSL *ssl) {
|
2014-12-19 01:42:32 +00:00
|
|
|
BUF_MEM *buf = NULL;
|
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 (;;) {
|
2015-12-19 22:05:56 +00:00
|
|
|
state = ssl->state;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
switch (ssl->state) {
|
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 (ssl->init_buf == NULL) {
|
2014-12-19 01:42:32 +00:00
|
|
|
buf = BUF_MEM_new();
|
2016-05-13 23:12:19 +01:00
|
|
|
if (!buf || !BUF_MEM_reserve(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
|
2014-12-19 01:42:32 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->init_buf = buf;
|
2014-12-19 01:42:32 +00:00
|
|
|
buf = NULL;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->init_num = 0;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-05-09 03:58:12 +01:00
|
|
|
/* Enable a write buffer. This groups handshake messages within a flight
|
|
|
|
* into a single write. */
|
2016-05-06 02:50:24 +01:00
|
|
|
if (!ssl_init_wbio_buffer(ssl)) {
|
2015-05-09 03:58:12 +01:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
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-06-07 21:49:42 +01:00
|
|
|
if (!ssl->s3->have_version && !SSL_IS_DTLS(ssl)) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_SR_INITIAL_BYTES;
|
2014-12-19 01:42:32 +00:00
|
|
|
} else {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SR_INITIAL_BYTES:
|
2016-06-07 21:49:42 +01:00
|
|
|
assert(!SSL_IS_DTLS(ssl));
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = ssl3_get_initial_bytes(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
/* ssl3_get_initial_bytes sets ssl->state to one of
|
2014-12-19 01:42:32 +00:00
|
|
|
* SSL3_ST_SR_V2_CLIENT_HELLO or SSL3_ST_SR_CLNT_HELLO_A on success. */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SR_V2_CLIENT_HELLO:
|
2016-06-07 21:49:42 +01:00
|
|
|
assert(!SSL_IS_DTLS(ssl));
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = ssl3_get_v2_client_hello(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SR_CLNT_HELLO_A:
|
|
|
|
case SSL3_ST_SR_CLNT_HELLO_B:
|
|
|
|
case SSL3_ST_SR_CLNT_HELLO_C:
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = ssl3_get_client_hello(ssl);
|
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);
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_SW_SRVR_HELLO_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SW_SRVR_HELLO_A:
|
|
|
|
case SSL3_ST_SW_SRVR_HELLO_B:
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = ssl3_send_server_hello(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->hit) {
|
2016-06-13 18:56:33 +01:00
|
|
|
ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
} else {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_SW_CERT_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SW_CERT_A:
|
|
|
|
case SSL3_ST_SW_CERT_B:
|
2016-05-12 19:54:57 +01:00
|
|
|
if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = ssl3_send_server_certificate(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = 1;
|
|
|
|
}
|
2016-06-13 18:56:33 +01:00
|
|
|
ssl->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:
|
|
|
|
case SSL3_ST_SW_CERT_STATUS_B:
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ssl->s3->tmp.certificate_status_expected) {
|
|
|
|
ret = ssl3_send_certificate_status(ssl);
|
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = 1;
|
2015-08-12 11:47:11 +01:00
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->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-07-24 17:31:31 +01:00
|
|
|
case SSL3_ST_SW_KEY_EXCH_C:
|
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)) {
|
|
|
|
ret = ssl3_send_server_key_exchange(ssl);
|
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;
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_SW_CERT_REQ_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SW_CERT_REQ_A:
|
|
|
|
case SSL3_ST_SW_CERT_REQ_B:
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->s3->tmp.cert_request) {
|
|
|
|
ret = ssl3_send_certificate_request(ssl);
|
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
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_SW_SRVR_DONE_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SW_SRVR_DONE_A:
|
|
|
|
case SSL3_ST_SW_SRVR_DONE_B:
|
2016-06-07 22:13:28 +01:00
|
|
|
ret = ssl3_send_server_hello_done(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
|
|
|
|
ssl->state = SSL3_ST_SW_FLUSH;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SR_CERT_A:
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->s3->tmp.cert_request) {
|
|
|
|
ret = ssl3_get_client_certificate(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->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:
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = ssl3_get_client_key_exchange(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_SR_CERT_VRFY_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SR_CERT_VRFY_A:
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = ssl3_get_cert_verify(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->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;
|
|
|
|
}
|
|
|
|
|
2015-12-25 20:48:39 +00:00
|
|
|
if (!tls1_change_cipher_state(ssl, 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-06-13 18:56:33 +01:00
|
|
|
ssl->state = SSL3_ST_SR_NEXT_PROTO_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SR_NEXT_PROTO_A:
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ssl->s3->next_proto_neg_seen) {
|
|
|
|
ret = ssl3_get_next_proto(ssl);
|
|
|
|
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-06-13 18:56:33 +01:00
|
|
|
ssl->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) {
|
|
|
|
ret = ssl3_get_channel_id(ssl);
|
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = 1;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_SR_FINISHED_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SR_FINISHED_A:
|
2016-05-13 23:28:17 +01:00
|
|
|
ret = ssl3_get_finished(ssl);
|
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);
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->hit) {
|
|
|
|
ssl->state = SSL_ST_OK;
|
2014-12-19 01:42:32 +00:00
|
|
|
} else {
|
2016-06-13 18:56:33 +01:00
|
|
|
ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2016-06-07 21:49:42 +01:00
|
|
|
|
2014-12-19 01:42:32 +00:00
|
|
|
/* If this is a full handshake with ChannelID then record the hashshake
|
2015-12-19 22:05:56 +00:00
|
|
|
* hashes in |ssl->session| in case we need them to verify a ChannelID
|
2014-12-19 01:42:32 +00:00
|
|
|
* signature on a resumption of this session in the future. */
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!ssl->hit && ssl->s3->tlsext_channel_id_valid) {
|
|
|
|
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:
|
|
|
|
case SSL3_ST_SW_SESSION_TICKET_B:
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ssl->tlsext_ticket_expected) {
|
|
|
|
ret = ssl3_send_new_session_ticket(ssl);
|
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = 1;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_SW_CHANGE_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SW_CHANGE_A:
|
|
|
|
case SSL3_ST_SW_CHANGE_B:
|
2016-06-07 21:49:42 +01:00
|
|
|
ret = ssl->method->send_change_cipher_spec(ssl, SSL3_ST_SW_CHANGE_A,
|
|
|
|
SSL3_ST_SW_CHANGE_B);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_SW_FINISHED_A;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-12-25 20:48:39 +00:00
|
|
|
if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
|
2014-12-19 01:42:32 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_SW_FINISHED_A:
|
|
|
|
case SSL3_ST_SW_FINISHED_B:
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A,
|
2015-12-30 00:13:58 +00:00
|
|
|
SSL3_ST_SW_FINISHED_B);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_SW_FLUSH;
|
|
|
|
if (ssl->hit) {
|
|
|
|
ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
|
2014-12-19 01:42:32 +00:00
|
|
|
} else {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->tmp.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:
|
|
|
|
if (BIO_flush(ssl->wbio) <= 0) {
|
|
|
|
ssl->rwstate = SSL_WRITING;
|
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->state = ssl->s3->tmp.next_state;
|
|
|
|
if (ssl->state != SSL_ST_OK) {
|
|
|
|
ssl->method->expect_flight(ssl);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-12-19 01:42:32 +00:00
|
|
|
case SSL_ST_OK:
|
|
|
|
/* clean a few things up */
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl3_cleanup_key_block(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-17 23:59:24 +01:00
|
|
|
BUF_MEM_free(ssl->init_buf);
|
|
|
|
ssl->init_buf = NULL;
|
|
|
|
ssl->init_num = 0;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
/* remove buffering on output */
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl_free_wbio_buffer(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
/* If we aren't retaining peer certificates then we can discard it
|
|
|
|
* now. */
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->ctx->retain_only_sha256_of_client_certs) {
|
|
|
|
X509_free(ssl->session->peer);
|
|
|
|
ssl->session->peer = NULL;
|
|
|
|
sk_X509_pop_free(ssl->session->cert_chain, X509_free);
|
|
|
|
ssl->session->cert_chain = NULL;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 21:49:42 +01:00
|
|
|
if (SSL_IS_DTLS(ssl)) {
|
|
|
|
ssl->d1->handshake_read_seq = 0;
|
|
|
|
ssl->d1->handshake_write_seq = 0;
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->initial_handshake_complete = 1;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-02 01:16:03 +01: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-06-02 01:16:03 +01:00
|
|
|
if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) {
|
|
|
|
int new_state = ssl->state;
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = state;
|
2016-06-02 01:16:03 +01:00
|
|
|
ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = new_state;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
skip = 0;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
end:
|
2015-04-22 21:17:58 +01:00
|
|
|
BUF_MEM_free(buf);
|
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-06-07 21:49:42 +01:00
|
|
|
static int ssl3_get_initial_bytes(SSL *ssl) {
|
2015-07-25 04:48:52 +01:00
|
|
|
/* Read the first 5 bytes, the size of the TLS record header. This is
|
|
|
|
* sufficient to detect a V2ClientHello and ensures that we never read beyond
|
|
|
|
* the first record. */
|
2015-12-19 22:05:56 +00:00
|
|
|
int ret = ssl_read_buffer_extend_to(ssl, SSL3_RT_HEADER_LENGTH);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
assert(ssl_read_buffer_len(ssl) == SSL3_RT_HEADER_LENGTH);
|
|
|
|
const uint8_t *p = ssl_read_buffer(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
/* Some dedicated error codes for protocol mixups should the application wish
|
|
|
|
* to interpret them differently. (These do not overlap with ClientHello or
|
|
|
|
* V2ClientHello.) */
|
|
|
|
if (strncmp("GET ", (const char *)p, 4) == 0 ||
|
|
|
|
strncmp("POST ", (const char *)p, 5) == 0 ||
|
|
|
|
strncmp("HEAD ", (const char *)p, 5) == 0 ||
|
|
|
|
strncmp("PUT ", (const char *)p, 4) == 0) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST);
|
2014-12-19 01:42:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-07-25 04:48:52 +01:00
|
|
|
if (strncmp("CONNE", (const char *)p, 5) == 0) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST);
|
2014-12-19 01:42:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:02:49 +01:00
|
|
|
/* Determine if this is a V2ClientHello. */
|
2014-12-19 01:42:32 +00:00
|
|
|
if ((p[0] & 0x80) && p[2] == SSL2_MT_CLIENT_HELLO &&
|
|
|
|
p[3] >= SSL3_VERSION_MAJOR) {
|
|
|
|
/* This is a V2ClientHello. */
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_SR_V2_CLIENT_HELLO;
|
2014-12-19 01:42:32 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-07-29 02:34:45 +01:00
|
|
|
/* Fall through to the standard logic. */
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
|
2015-07-25 04:02:49 +01:00
|
|
|
return 1;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 21:49:42 +01:00
|
|
|
static int ssl3_get_v2_client_hello(SSL *ssl) {
|
2014-12-19 01:42:32 +00:00
|
|
|
const uint8_t *p;
|
|
|
|
int ret;
|
|
|
|
CBS v2_client_hello, cipher_specs, session_id, challenge;
|
2016-05-13 23:12:19 +01:00
|
|
|
size_t msg_length, rand_len;
|
2014-12-19 01:42:32 +00:00
|
|
|
uint8_t msg_type;
|
|
|
|
uint16_t version, cipher_spec_length, session_id_length, challenge_length;
|
|
|
|
CBB client_hello, hello_body, cipher_suites;
|
|
|
|
uint8_t random[SSL3_RANDOM_SIZE];
|
|
|
|
|
2015-07-25 04:24:12 +01:00
|
|
|
/* Determine the length of the V2ClientHello. */
|
2015-12-19 22:05:56 +00:00
|
|
|
assert(ssl_read_buffer_len(ssl) >= SSL3_RT_HEADER_LENGTH);
|
|
|
|
p = ssl_read_buffer(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
msg_length = ((p[0] & 0x7f) << 8) | p[1];
|
|
|
|
if (msg_length > (1024 * 4)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
|
2014-12-19 01:42:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-07-25 04:48:52 +01:00
|
|
|
if (msg_length < SSL3_RT_HEADER_LENGTH - 2) {
|
2015-07-25 04:24:12 +01:00
|
|
|
/* Reject lengths that are too short early. We have already read
|
2015-07-25 04:48:52 +01:00
|
|
|
* |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an
|
2015-07-25 04:24:12 +01:00
|
|
|
* (invalid) V2ClientHello which would be shorter than that. */
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH);
|
2014-12-19 01:42:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-07-29 02:34:45 +01:00
|
|
|
/* Read the remainder of the V2ClientHello. */
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = ssl_read_buffer_extend_to(ssl, 2 + msg_length);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
assert(ssl_read_buffer_len(ssl) == msg_length + 2);
|
|
|
|
CBS_init(&v2_client_hello, ssl_read_buffer(ssl) + 2, msg_length);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-08-05 13:50:34 +01:00
|
|
|
/* The V2ClientHello without the length is incorporated into the handshake
|
2014-12-19 01:42:32 +00:00
|
|
|
* hash. */
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!ssl3_update_handshake_hash(ssl, CBS_data(&v2_client_hello),
|
2015-08-05 13:50:34 +01:00
|
|
|
CBS_len(&v2_client_hello))) {
|
2015-02-17 00:33:53 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2016-06-02 01:16:03 +01:00
|
|
|
|
|
|
|
ssl_do_msg_callback(ssl, 0 /* read */, SSL2_VERSION, 0,
|
|
|
|
CBS_data(&v2_client_hello), CBS_len(&v2_client_hello));
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
|
|
|
|
!CBS_get_u16(&v2_client_hello, &version) ||
|
|
|
|
!CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
|
|
|
|
!CBS_get_u16(&v2_client_hello, &session_id_length) ||
|
|
|
|
!CBS_get_u16(&v2_client_hello, &challenge_length) ||
|
|
|
|
!CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
|
|
|
|
!CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
|
|
|
|
!CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
|
|
|
|
CBS_len(&v2_client_hello) != 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
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* msg_type has already been checked. */
|
|
|
|
assert(msg_type == SSL2_MT_CLIENT_HELLO);
|
|
|
|
|
|
|
|
/* The client_random is the V2ClientHello challenge. Truncate or
|
|
|
|
* left-pad with zeros as needed. */
|
|
|
|
memset(random, 0, SSL3_RANDOM_SIZE);
|
|
|
|
rand_len = CBS_len(&challenge);
|
|
|
|
if (rand_len > SSL3_RANDOM_SIZE) {
|
|
|
|
rand_len = SSL3_RANDOM_SIZE;
|
|
|
|
}
|
|
|
|
memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
|
|
|
|
rand_len);
|
|
|
|
|
|
|
|
/* Write out an equivalent SSLv3 ClientHello. */
|
2015-06-28 06:26:10 +01:00
|
|
|
CBB_zero(&client_hello);
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!CBB_init_fixed(&client_hello, (uint8_t *)ssl->init_buf->data,
|
|
|
|
ssl->init_buf->max) ||
|
2015-06-28 06:26:10 +01:00
|
|
|
!CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
|
2014-12-19 01:42:32 +00:00
|
|
|
!CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
|
|
|
|
!CBB_add_u16(&hello_body, version) ||
|
|
|
|
!CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
|
|
|
|
/* No session id. */
|
|
|
|
!CBB_add_u8(&hello_body, 0) ||
|
|
|
|
!CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
|
|
|
|
CBB_cleanup(&client_hello);
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
2014-12-19 01:42:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the cipher suites. */
|
|
|
|
while (CBS_len(&cipher_specs) > 0) {
|
|
|
|
uint32_t cipher_spec;
|
|
|
|
if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
|
|
|
|
CBB_cleanup(&client_hello);
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2014-12-19 01:42:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip SSLv2 ciphers. */
|
|
|
|
if ((cipher_spec & 0xff0000) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
|
|
|
|
CBB_cleanup(&client_hello);
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2014-12-19 01:42:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the null compression scheme and finish. */
|
|
|
|
if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) ||
|
2016-05-13 23:12:19 +01:00
|
|
|
!CBB_finish(&client_hello, NULL, &ssl->init_buf->length)) {
|
2014-12-19 01:42:32 +00:00
|
|
|
CBB_cleanup(&client_hello);
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2014-12-19 01:42:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark the message for "re"-use by the version-specific method. */
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->tmp.reuse_message = 1;
|
|
|
|
ssl->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO;
|
2016-05-13 23:12:19 +01:00
|
|
|
ssl->s3->tmp.message_complete = 1;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-07-29 02:34:45 +01:00
|
|
|
/* Consume and discard the V2ClientHello. */
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl_read_buffer_consume(ssl, 2 + msg_length);
|
|
|
|
ssl_read_buffer_discard(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:49:42 +01:00
|
|
|
static int ssl3_get_client_hello(SSL *ssl) {
|
2015-05-26 21:37:58 +01:00
|
|
|
int ok, al = SSL_AD_INTERNAL_ERROR, ret = -1;
|
2014-12-19 01:42:32 +00:00
|
|
|
long n;
|
|
|
|
const SSL_CIPHER *c;
|
|
|
|
STACK_OF(SSL_CIPHER) *ciphers = NULL;
|
|
|
|
struct ssl_early_callback_ctx early_ctx;
|
|
|
|
CBS client_hello;
|
|
|
|
uint16_t client_version;
|
|
|
|
CBS client_random, session_id, cipher_suites, compression_methods;
|
2015-06-16 20:34:50 +01:00
|
|
|
SSL_SESSION *session = NULL;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
/* We do this so that we will respond with our native type. If we are TLSv1
|
|
|
|
* and we get SSLv3, we will respond with TLSv1, This down switching should
|
|
|
|
* be handled by a different method. If we are SSLv3, we will respond with
|
|
|
|
* SSLv3, even if prompted with TLSv1. */
|
2015-12-19 22:05:56 +00:00
|
|
|
switch (ssl->state) {
|
2014-12-19 01:42:32 +00:00
|
|
|
case SSL3_ST_SR_CLNT_HELLO_A:
|
2016-05-13 23:28:17 +01:00
|
|
|
n = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO,
|
|
|
|
ssl_hash_message, &ok);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2016-05-13 23:28:17 +01:00
|
|
|
ssl->state = SSL3_ST_SR_CLNT_HELLO_B;
|
2014-12-19 01:42:32 +00:00
|
|
|
/* fallthrough */
|
2016-05-13 23:28:17 +01:00
|
|
|
case SSL3_ST_SR_CLNT_HELLO_B:
|
2014-12-19 01:42:32 +00:00
|
|
|
case SSL3_ST_SR_CLNT_HELLO_C:
|
|
|
|
/* We have previously parsed the ClientHello message, and can't call
|
|
|
|
* ssl_get_message again without hashing the message into the Finished
|
|
|
|
* digest again. */
|
2015-12-19 22:05:56 +00:00
|
|
|
n = ssl->init_num;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
memset(&early_ctx, 0, sizeof(early_ctx));
|
2015-12-19 22:05:56 +00:00
|
|
|
early_ctx.ssl = ssl;
|
|
|
|
early_ctx.client_hello = ssl->init_msg;
|
2014-12-19 01:42:32 +00:00
|
|
|
early_ctx.client_hello_len = n;
|
|
|
|
if (!ssl_early_callback_init(&early_ctx)) {
|
|
|
|
al = SSL_AD_DECODE_ERROR;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2016-05-13 23:28:17 +01:00
|
|
|
if (ssl->state == SSL3_ST_SR_CLNT_HELLO_B &&
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->ctx->select_certificate_cb != NULL) {
|
2016-05-13 23:28:17 +01:00
|
|
|
ssl->state = SSL3_ST_SR_CLNT_HELLO_C;
|
2015-12-19 22:05:56 +00:00
|
|
|
switch (ssl->ctx->select_certificate_cb(&early_ctx)) {
|
2014-12-19 01:42:32 +00:00
|
|
|
case 0:
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
|
2015-02-09 09:32:28 +00:00
|
|
|
goto err;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
case -1:
|
|
|
|
/* Connection rejected. */
|
|
|
|
al = SSL_AD_ACCESS_DENIED;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto f_err;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* fallthrough */;
|
|
|
|
}
|
|
|
|
}
|
2016-05-13 23:28:17 +01:00
|
|
|
ssl->state = SSL3_ST_SR_CLNT_HELLO_C;
|
2014-12-19 01:42:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
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
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
CBS_init(&client_hello, ssl->init_msg, n);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (!CBS_get_u16(&client_hello, &client_version) ||
|
|
|
|
!CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) ||
|
|
|
|
!CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
|
|
|
|
CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* use version from inside client hello, not from record header (may differ:
|
|
|
|
* see RFC 2246, Appendix E, second paragraph) */
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->client_version = client_version;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
/* Load the client random. */
|
2015-12-19 22:05:56 +00:00
|
|
|
memcpy(ssl->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
if (SSL_IS_DTLS(ssl)) {
|
2014-12-19 01:42:32 +00:00
|
|
|
CBS cookie;
|
|
|
|
|
|
|
|
if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
|
|
|
|
CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add s->s3->initial_handshake_complete.
There's multiple different versions of this check, between
s->s3->have_version (only works at some points), s->new_session (really
weird and not actually right), s->renegotiate (fails on the server
because it's always 2 after ClientHello), and s->s3->tmp.finish_md_len
(super confusing). Add an explicit bit with clear meaning. We'll prune
some of the others later; notably s->renegotiate can go away when
initiating renegotiation is removed.
This also tidies up the extensions to be consistent about whether
they're allowed during renego:
- ALPN failed to condition when accepting from the server, so even
if the client didn't advertise, the server could.
- SCTs now *are* allowed during renego. I think forbidding it was a
stray copy-paste. It wasn't consistently enforced in both ClientHello
and ServerHello, so the server could still supply it. Moreover, SCTs
are part of the certificate, so we should accept it wherever we accept
certificates, otherwise that session's state becomes incomplete. This
matches OCSP stapling. (NB: Chrome will never insert a session created
on renego into the session cache and won't accept a certificate
change, so this is moot anyway.)
Change-Id: Ic9bd1ebe2a2dbe75930ed0213bf3c8ed8170e251
Reviewed-on: https://boringssl-review.googlesource.com/4730
Reviewed-by: Adam Langley <agl@google.com>
2015-05-13 03:02:08 +01:00
|
|
|
/* Note: This codepath may run twice if |ssl_get_prev_session| completes
|
|
|
|
* asynchronously.
|
|
|
|
*
|
|
|
|
* TODO(davidben): Clean up the order of events around ClientHello
|
|
|
|
* processing. */
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!ssl->s3->have_version) {
|
2014-12-19 01:42:32 +00:00
|
|
|
/* Select version to use */
|
2015-12-19 22:05:56 +00:00
|
|
|
uint16_t version = ssl3_get_mutual_version(ssl, client_version);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (version == 0) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
|
2014-12-19 01:42:32 +00:00
|
|
|
al = SSL_AD_PROTOCOL_VERSION;
|
|
|
|
goto f_err;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->version = version;
|
2015-12-30 22:39:26 +00:00
|
|
|
ssl->s3->enc_method = ssl3_get_enc_method(version);
|
|
|
|
assert(ssl->s3->enc_method != NULL);
|
2015-12-19 22:05:56 +00:00
|
|
|
/* At this point, the connection's version is known and |ssl->version| is
|
2014-12-19 01:42:32 +00:00
|
|
|
* fixed. Begin enforcing the record-layer version. */
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->have_version = 1;
|
|
|
|
} else if (SSL_IS_DTLS(ssl) ? (ssl->client_version > ssl->version)
|
|
|
|
: (ssl->client_version < ssl->version)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
|
2014-12-19 01:42:32 +00:00
|
|
|
al = SSL_AD_PROTOCOL_VERSION;
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->hit = 0;
|
2015-06-16 20:34:50 +01:00
|
|
|
int send_new_ticket = 0;
|
2015-12-19 22:05:56 +00:00
|
|
|
switch (ssl_get_prev_session(ssl, &session, &send_new_ticket, &early_ctx)) {
|
2015-06-16 20:34:50 +01:00
|
|
|
case ssl_session_success:
|
|
|
|
break;
|
|
|
|
case ssl_session_error:
|
|
|
|
goto err;
|
|
|
|
case ssl_session_retry:
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->rwstate = SSL_PENDING_SESSION;
|
2015-06-16 20:34:50 +01:00
|
|
|
goto err;
|
2015-05-26 21:37:58 +01:00
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->tlsext_ticket_expected = send_new_ticket;
|
2015-05-26 21:37:58 +01:00
|
|
|
|
2015-06-02 18:50:35 +01:00
|
|
|
/* The EMS state is needed when making the resumption decision, but
|
|
|
|
* extensions are not normally parsed until later. This detects the EMS
|
|
|
|
* extension for the resumption decision and it's checked against the result
|
|
|
|
* of the normal parse later in this function. */
|
|
|
|
const uint8_t *ems_data;
|
|
|
|
size_t ems_len;
|
|
|
|
int have_extended_master_secret =
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->version != SSL3_VERSION &&
|
2015-06-02 18:50:35 +01:00
|
|
|
SSL_early_callback_ctx_extension_get(&early_ctx,
|
|
|
|
TLSEXT_TYPE_extended_master_secret,
|
|
|
|
&ems_data, &ems_len) &&
|
|
|
|
ems_len == 0;
|
|
|
|
|
2015-06-16 20:34:50 +01:00
|
|
|
if (session != NULL) {
|
|
|
|
if (session->extended_master_secret &&
|
2015-06-02 18:50:35 +01:00
|
|
|
!have_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;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
|
2015-06-02 18:50:35 +01:00
|
|
|
goto f_err;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2015-06-02 18:50:35 +01:00
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->hit =
|
2015-06-02 18:50:35 +01:00
|
|
|
/* Only resume if the session's version matches the negotiated version:
|
|
|
|
* most clients do not accept a mismatch. */
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->version == session->ssl_version &&
|
2015-06-02 18:50:35 +01:00
|
|
|
/* If the client offers the EMS extension, but the previous session
|
|
|
|
* didn't use it, then negotiate a new session. */
|
2015-06-16 20:34:50 +01:00
|
|
|
have_extended_master_secret == session->extended_master_secret;
|
2015-06-02 18:50:35 +01:00
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->hit) {
|
2015-06-16 20:34:50 +01:00
|
|
|
/* Use the new session. */
|
2015-12-19 22:05:56 +00:00
|
|
|
SSL_SESSION_free(ssl->session);
|
|
|
|
ssl->session = session;
|
2015-06-16 20:34:50 +01:00
|
|
|
session = NULL;
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->verify_result = ssl->session->verify_result;
|
2015-10-10 19:59:09 +01:00
|
|
|
} else {
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!ssl_get_new_session(ssl, 1 /* server */)) {
|
2015-10-10 19:59:09 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear the session ID if we want the session to be single-use. */
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
|
|
|
|
ssl->session->session_id_length = 0;
|
2015-10-10 19:59:09 +01:00
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->ctx->dos_protection_cb != NULL &&
|
|
|
|
ssl->ctx->dos_protection_cb(&early_ctx) == 0) {
|
2015-02-21 00:04:00 +00:00
|
|
|
/* Connection rejected for DOS reasons. */
|
|
|
|
al = SSL_AD_ACCESS_DENIED;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
|
2015-02-21 00:04:00 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2014-12-19 01:42:32 +00:00
|
|
|
if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
|
2015-04-18 03:33:25 +01:00
|
|
|
CBS_len(&cipher_suites) == 0 ||
|
|
|
|
CBS_len(&cipher_suites) % 2 != 0 ||
|
2014-12-19 01:42:32 +00:00
|
|
|
!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
|
|
|
|
CBS_len(&compression_methods) == 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;
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
ciphers = ssl_bytes_to_cipher_list(ssl, &cipher_suites);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ciphers == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If it is a hit, check that the cipher is in the list. */
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->hit) {
|
2014-12-19 01:42:32 +00:00
|
|
|
size_t j;
|
|
|
|
int found_cipher = 0;
|
2015-12-19 22:05:56 +00:00
|
|
|
uint32_t id = ssl->session->cipher->id;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++) {
|
|
|
|
c = sk_SSL_CIPHER_value(ciphers, j);
|
|
|
|
if (c->id == id) {
|
|
|
|
found_cipher = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found_cipher) {
|
|
|
|
/* we need to have the cipher in the cipher list if we are asked to reuse
|
|
|
|
* it */
|
|
|
|
al = SSL_AD_ILLEGAL_PARAMETER;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_REQUIRED_CIPHER_MISSING);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only null compression is supported. */
|
|
|
|
if (memchr(CBS_data(&compression_methods), 0,
|
|
|
|
CBS_len(&compression_methods)) == NULL) {
|
|
|
|
al = SSL_AD_ILLEGAL_PARAMETER;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TLS extensions. */
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->version >= SSL3_VERSION &&
|
|
|
|
!ssl_parse_clienthello_tlsext(ssl, &client_hello)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There should be nothing left over in the record. */
|
|
|
|
if (CBS_len(&client_hello) != 0) {
|
|
|
|
/* wrong packet length */
|
|
|
|
al = SSL_AD_DECODE_ERROR;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
if (have_extended_master_secret != ssl->s3->tmp.extended_master_secret) {
|
2015-06-02 18:50:35 +01:00
|
|
|
al = SSL_AD_INTERNAL_ERROR;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_EMS_STATE_INCONSISTENT);
|
2015-06-02 18:50:35 +01:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2014-12-19 01:42:32 +00:00
|
|
|
/* Given ciphers and SSL_get_ciphers, we must pick a cipher */
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!ssl->hit) {
|
2014-12-19 01:42:32 +00:00
|
|
|
if (ciphers == NULL) {
|
|
|
|
al = SSL_AD_ILLEGAL_PARAMETER;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_PASSED);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Let cert callback update server certificates if required */
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->cert->cert_cb) {
|
|
|
|
int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
|
2014-12-19 01:42:32 +00:00
|
|
|
if (rv == 0) {
|
|
|
|
al = SSL_AD_INTERNAL_ERROR;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
if (rv < 0) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->rwstate = SSL_X509_LOOKUP;
|
2014-12-19 01:42:32 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
c = ssl3_choose_cipher(ssl, ciphers, ssl_get_cipher_preferences(ssl));
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
if (c == NULL) {
|
|
|
|
al = SSL_AD_HANDSHAKE_FAILURE;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
2015-12-25 20:40:14 +00:00
|
|
|
ssl->session->cipher = c;
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->tmp.new_cipher = c;
|
2015-05-26 21:59:43 +01:00
|
|
|
|
|
|
|
/* Determine whether to request a client certificate. */
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->tmp.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) {
|
|
|
|
ssl->s3->tmp.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. */
|
|
|
|
if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->tmp.cert_request = 0;
|
2015-05-26 21:59:43 +01:00
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
} else {
|
|
|
|
/* Session-id reuse */
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->tmp.new_cipher = ssl->session->cipher;
|
|
|
|
ssl->s3->tmp.cert_request = 0;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 13:50:34 +01:00
|
|
|
/* Now that the cipher is known, initialize the handshake hash. */
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!ssl3_init_handshake_hash(ssl)) {
|
2015-08-05 13:50:34 +01:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2015-05-26 21:59:43 +01:00
|
|
|
/* In TLS 1.2, client authentication requires hashing the handshake transcript
|
|
|
|
* under a different hash. Otherwise, release the handshake buffer. */
|
2015-12-30 05:08:49 +00:00
|
|
|
if (!ssl->s3->tmp.cert_request ||
|
|
|
|
ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl3_free_handshake_buffer(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* we now have the following setup;
|
|
|
|
* client_random
|
|
|
|
* cipher_list - our prefered list of ciphers
|
|
|
|
* ciphers - the clients prefered list of ciphers
|
|
|
|
* compression - basically ignored right now
|
|
|
|
* ssl version is set - sslv3
|
2015-12-19 22:05:56 +00:00
|
|
|
* ssl->session - The ssl session has been setup.
|
|
|
|
* ssl->hit - session reuse flag
|
|
|
|
* ssl->tmp.new_cipher - the new cipher to use. */
|
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-04-22 21:17:58 +01:00
|
|
|
sk_SSL_CIPHER_free(ciphers);
|
2015-06-16 20:34:50 +01:00
|
|
|
SSL_SESSION_free(session);
|
2014-12-19 01:42:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:49:42 +01:00
|
|
|
static int ssl3_send_server_hello(SSL *ssl) {
|
2015-10-10 19:59:09 +01:00
|
|
|
if (ssl->state == SSL3_ST_SW_SRVR_HELLO_B) {
|
|
|
|
return ssl_do_write(ssl);
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-10-10 19:59:09 +01:00
|
|
|
assert(ssl->state == SSL3_ST_SW_SRVR_HELLO_A);
|
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. */
|
|
|
|
if (ssl->hit && ssl->session->original_handshake_hash_len == 0) {
|
|
|
|
ssl->s3->tlsext_channel_id_valid = 0;
|
|
|
|
}
|
2015-07-22 02:06:39 +01:00
|
|
|
|
2015-10-10 19:59:09 +01:00
|
|
|
if (!ssl_fill_hello_random(ssl->s3->server_random, SSL3_RANDOM_SIZE,
|
|
|
|
1 /* server */)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-10-10 19:59:09 +01:00
|
|
|
CBB cbb, session_id;
|
|
|
|
size_t length;
|
|
|
|
CBB_zero(&cbb);
|
|
|
|
if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
|
|
|
|
ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
|
|
|
|
!CBB_add_u16(&cbb, ssl->version) ||
|
|
|
|
!CBB_add_bytes(&cbb, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
|
|
|
|
!CBB_add_u8_length_prefixed(&cbb, &session_id) ||
|
|
|
|
!CBB_add_bytes(&session_id, ssl->session->session_id,
|
|
|
|
ssl->session->session_id_length) ||
|
|
|
|
!CBB_add_u16(&cbb, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
|
|
|
|
!CBB_add_u8(&cbb, 0 /* no compression */) ||
|
|
|
|
!ssl_add_serverhello_tlsext(ssl, &cbb) ||
|
|
|
|
!CBB_finish(&cbb, NULL, &length) ||
|
|
|
|
!ssl_set_handshake_header(ssl, SSL3_MT_SERVER_HELLO, length)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
CBB_cleanup(&cbb);
|
|
|
|
return -1;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2015-10-10 19:59:09 +01:00
|
|
|
ssl->state = SSL3_ST_SW_SRVR_HELLO_B;
|
|
|
|
return ssl_do_write(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
static int ssl3_send_server_certificate(SSL *ssl) {
|
|
|
|
if (ssl->state == SSL3_ST_SW_CERT_A) {
|
|
|
|
if (!ssl3_output_cert_chain(ssl)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ssl->state = SSL3_ST_SW_CERT_B;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SSL3_ST_SW_CERT_B */
|
|
|
|
return ssl_do_write(ssl);
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:49:42 +01:00
|
|
|
static int ssl3_send_certificate_status(SSL *ssl) {
|
2015-08-12 11:47:11 +01:00
|
|
|
if (ssl->state == SSL3_ST_SW_CERT_STATUS_A) {
|
|
|
|
CBB out, ocsp_response;
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
CBB_zero(&out);
|
|
|
|
if (!CBB_init_fixed(&out, ssl_handshake_start(ssl),
|
|
|
|
ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
|
|
|
|
!CBB_add_u8(&out, TLSEXT_STATUSTYPE_ocsp) ||
|
|
|
|
!CBB_add_u24_length_prefixed(&out, &ocsp_response) ||
|
|
|
|
!CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
|
|
|
|
ssl->ctx->ocsp_response_length) ||
|
|
|
|
!CBB_finish(&out, NULL, &length) ||
|
|
|
|
!ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_STATUS, length)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
CBB_cleanup(&out);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->state = SSL3_ST_SW_CERT_STATUS_B;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SSL3_ST_SW_CERT_STATUS_B */
|
|
|
|
return ssl_do_write(ssl);
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:49:42 +01:00
|
|
|
static int ssl3_send_server_key_exchange(SSL *ssl) {
|
2015-12-18 06:01:21 +00:00
|
|
|
if (ssl->state == SSL3_ST_SW_KEY_EXCH_C) {
|
|
|
|
return ssl_do_write(ssl);
|
2015-08-28 21:17:59 +01:00
|
|
|
}
|
|
|
|
|
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. */
|
2015-12-18 06:01:21 +00:00
|
|
|
if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
|
|
|
|
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-19 05:18:25 +00:00
|
|
|
ssl->session->key_exchange_info = DH_num_bits(params);
|
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
|
|
|
|
2015-12-19 21:58:04 +00:00
|
|
|
SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.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-05-16 22:27:14 +01:00
|
|
|
!SSL_ECDH_CTX_offer(&ssl->s3->tmp.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;
|
|
|
|
if (!tls1_get_shared_group(ssl, &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-05-17 16:47:53 +01:00
|
|
|
ssl->session->key_exchange_info = 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-05-17 16:47:53 +01:00
|
|
|
if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.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) ||
|
|
|
|
!SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
} else if (alg_k & SSL_kCECPQ1) {
|
|
|
|
if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, SSL_GROUP_CECPQ1) ||
|
|
|
|
!CBB_add_u16_length_prefixed(&cbb, &child) ||
|
2016-05-16 22:27:14 +01:00
|
|
|
!SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
|
2014-12-19 01:42:32 +00: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-06-17 22:47:58 +01:00
|
|
|
size_t len;
|
|
|
|
if (!CBB_finish(&cbb, &ssl->s3->tmp.server_params, &len) ||
|
|
|
|
len > 0xffffffffu) {
|
|
|
|
OPENSSL_free(ssl->s3->tmp.server_params);
|
|
|
|
ssl->s3->tmp.server_params = NULL;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ssl->s3->tmp.server_params_len = (uint32_t)len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assemble the message. */
|
|
|
|
if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
|
|
|
|
ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
|
|
|
|
!CBB_add_bytes(&cbb, ssl->s3->tmp.server_params,
|
|
|
|
ssl->s3->tmp.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-05-12 19:54:57 +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. */
|
|
|
|
const EVP_MD *md;
|
|
|
|
if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
|
|
|
|
md = tls1_choose_signing_digest(ssl);
|
|
|
|
if (!tls12_add_sigandhash(ssl, &cbb, md)) {
|
|
|
|
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
|
|
|
} else if (ssl_private_key_type(ssl) == EVP_PKEY_RSA) {
|
|
|
|
md = EVP_md5_sha1();
|
|
|
|
} else {
|
|
|
|
md = EVP_sha1();
|
|
|
|
}
|
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;
|
|
|
|
if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
|
|
|
|
!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;
|
|
|
|
if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
|
2015-12-18 06:01:21 +00:00
|
|
|
/* Compute the digest and sign it. */
|
|
|
|
uint8_t digest[EVP_MAX_MD_SIZE];
|
2015-12-22 17:37:19 +00:00
|
|
|
unsigned digest_len = 0;
|
2015-12-18 06:01:21 +00:00
|
|
|
EVP_MD_CTX md_ctx;
|
|
|
|
EVP_MD_CTX_init(&md_ctx);
|
|
|
|
int digest_ret =
|
|
|
|
EVP_DigestInit_ex(&md_ctx, md, NULL) &&
|
|
|
|
EVP_DigestUpdate(&md_ctx, ssl->s3->client_random, SSL3_RANDOM_SIZE) &&
|
|
|
|
EVP_DigestUpdate(&md_ctx, ssl->s3->server_random, SSL3_RANDOM_SIZE) &&
|
2016-06-17 22:47:58 +01:00
|
|
|
EVP_DigestUpdate(&md_ctx, ssl->s3->tmp.server_params,
|
|
|
|
ssl->s3->tmp.server_params_len) &&
|
2015-12-18 06:01:21 +00:00
|
|
|
EVP_DigestFinal_ex(&md_ctx, digest, &digest_len);
|
|
|
|
EVP_MD_CTX_cleanup(&md_ctx);
|
2016-06-17 22:47:58 +01:00
|
|
|
if (!digest_ret) {
|
2014-12-19 01:42:32 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2015-12-18 06:01:21 +00:00
|
|
|
sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, md,
|
|
|
|
digest, digest_len);
|
2015-08-28 21:17:59 +01:00
|
|
|
} else {
|
2015-12-18 06:01:21 +00:00
|
|
|
assert(ssl->state == SSL3_ST_SW_KEY_EXCH_B);
|
|
|
|
sign_result =
|
|
|
|
ssl_private_key_sign_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;
|
|
|
|
ssl->state = SSL3_ST_SW_KEY_EXCH_B;
|
|
|
|
goto err;
|
|
|
|
}
|
2015-07-24 17:31:31 +01:00
|
|
|
}
|
|
|
|
|
2015-12-18 06:01:21 +00:00
|
|
|
size_t length;
|
|
|
|
if (!CBB_finish(&cbb, NULL, &length) ||
|
|
|
|
!ssl_set_handshake_header(ssl, SSL3_MT_SERVER_KEY_EXCHANGE, length)) {
|
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
|
|
|
|
|
|
|
OPENSSL_free(ssl->s3->tmp.server_params);
|
|
|
|
ssl->s3->tmp.server_params = NULL;
|
|
|
|
ssl->s3->tmp.server_params_len = 0;
|
|
|
|
|
2015-12-18 06:01:21 +00:00
|
|
|
ssl->state = SSL3_ST_SW_KEY_EXCH_C;
|
|
|
|
return ssl_do_write(ssl);
|
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-07 21:49:42 +01:00
|
|
|
static int ssl3_send_certificate_request(SSL *ssl) {
|
2014-12-19 01:42:32 +00:00
|
|
|
uint8_t *p, *d;
|
|
|
|
size_t i;
|
|
|
|
int j, nl, off, n;
|
|
|
|
STACK_OF(X509_NAME) *sk = NULL;
|
|
|
|
X509_NAME *name;
|
|
|
|
BUF_MEM *buf;
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->state == SSL3_ST_SW_CERT_REQ_A) {
|
|
|
|
buf = ssl->init_buf;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
d = p = ssl_handshake_start(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
/* get the list of acceptable cert types */
|
|
|
|
p++;
|
2015-12-19 22:05:56 +00:00
|
|
|
n = ssl3_get_req_cert_type(ssl, p);
|
2014-12-19 01:42:32 +00:00
|
|
|
d[0] = n;
|
|
|
|
p += n;
|
|
|
|
n++;
|
|
|
|
|
2015-12-30 05:08:49 +00:00
|
|
|
if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
|
2014-12-19 01:42:32 +00:00
|
|
|
const uint8_t *psigs;
|
2015-12-19 22:05:56 +00:00
|
|
|
nl = tls12_get_psigalgs(ssl, &psigs);
|
2014-12-19 01:42:32 +00:00
|
|
|
s2n(nl, p);
|
|
|
|
memcpy(p, psigs, nl);
|
|
|
|
p += nl;
|
|
|
|
n += nl + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
off = n;
|
|
|
|
p += 2;
|
|
|
|
n += 2;
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
sk = SSL_get_client_CA_list(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
nl = 0;
|
|
|
|
if (sk != NULL) {
|
|
|
|
for (i = 0; i < sk_X509_NAME_num(sk); i++) {
|
|
|
|
name = sk_X509_NAME_value(sk, i);
|
|
|
|
j = i2d_X509_NAME(name, NULL);
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(ssl) + n + j + 2)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
p = ssl_handshake_start(ssl) + n;
|
2014-12-19 01:42:32 +00:00
|
|
|
s2n(j, p);
|
|
|
|
i2d_X509_NAME(name, &p);
|
|
|
|
n += 2 + j;
|
|
|
|
nl += 2 + j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* else no CA names */
|
2015-12-19 22:05:56 +00:00
|
|
|
p = ssl_handshake_start(ssl) + off;
|
2014-12-19 01:42:32 +00:00
|
|
|
s2n(nl, p);
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_REQUEST, n)) {
|
2015-02-17 00:33:53 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_SW_CERT_REQ_B;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* SSL3_ST_SW_CERT_REQ_B */
|
2015-12-19 22:05:56 +00:00
|
|
|
return ssl_do_write(ssl);
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
err:
|
2014-12-19 01:42:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-06-07 22:13:28 +01:00
|
|
|
static int ssl3_send_server_hello_done(SSL *ssl) {
|
2016-06-07 22:03:46 +01:00
|
|
|
if (ssl->state == SSL3_ST_SW_SRVR_DONE_A) {
|
2016-06-07 22:13:28 +01:00
|
|
|
if (!ssl_set_handshake_header(ssl, SSL3_MT_SERVER_HELLO_DONE, 0)) {
|
2016-06-07 22:03:46 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ssl->state = SSL3_ST_SW_SRVR_DONE_B;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SSL3_ST_SW_SRVR_DONE_B */
|
|
|
|
return ssl_do_write(ssl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ssl3_get_client_certificate(SSL *ssl) {
|
|
|
|
int ok, al, ret = -1;
|
|
|
|
X509 *x = NULL;
|
|
|
|
unsigned long n;
|
|
|
|
STACK_OF(X509) *sk = NULL;
|
|
|
|
SHA256_CTX sha256;
|
|
|
|
CBS certificate_msg, certificate_list;
|
|
|
|
int is_first_certificate = 1;
|
|
|
|
|
|
|
|
assert(ssl->s3->tmp.cert_request);
|
|
|
|
n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok);
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
|
|
|
|
if (ssl->version == SSL3_VERSION &&
|
|
|
|
ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
|
|
|
|
/* In SSL 3.0, the Certificate message is omitted to signal no certificate. */
|
|
|
|
if ((ssl->verify_mode & SSL_VERIFY_PEER) &&
|
|
|
|
(ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
|
|
|
|
al = SSL_AD_HANDSHAKE_FAILURE;
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->s3->tmp.reuse_message = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
al = SSL_AD_UNEXPECTED_MESSAGE;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
CBS_init(&certificate_msg, ssl->init_msg, n);
|
|
|
|
|
|
|
|
sk = sk_X509_new_null();
|
|
|
|
if (sk == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) ||
|
|
|
|
CBS_len(&certificate_msg) != 0) {
|
|
|
|
al = SSL_AD_DECODE_ERROR;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (CBS_len(&certificate_list) > 0) {
|
|
|
|
CBS certificate;
|
|
|
|
const uint8_t *data;
|
|
|
|
|
|
|
|
if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
|
|
|
|
al = SSL_AD_DECODE_ERROR;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_first_certificate && ssl->ctx->retain_only_sha256_of_client_certs) {
|
|
|
|
/* If this is the first certificate, and we don't want to keep peer
|
|
|
|
* certificates in memory, then we hash it right away. */
|
|
|
|
SHA256_Init(&sha256);
|
|
|
|
SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate));
|
|
|
|
SHA256_Final(ssl->session->peer_sha256, &sha256);
|
|
|
|
ssl->session->peer_sha256_valid = 1;
|
|
|
|
}
|
|
|
|
is_first_certificate = 0;
|
|
|
|
|
|
|
|
/* A u24 length cannot overflow a long. */
|
|
|
|
data = CBS_data(&certificate);
|
|
|
|
x = d2i_X509(NULL, &data, (long)CBS_len(&certificate));
|
|
|
|
if (x == NULL) {
|
|
|
|
al = SSL_AD_BAD_CERTIFICATE;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
|
|
|
|
al = SSL_AD_DECODE_ERROR;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
if (!sk_X509_push(sk, x)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
x = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sk_X509_num(sk) <= 0) {
|
|
|
|
/* No client certificate so the handshake buffer may be discarded. */
|
|
|
|
ssl3_free_handshake_buffer(ssl);
|
|
|
|
|
|
|
|
/* TLS does not mind 0 certs returned */
|
|
|
|
if (ssl->version == SSL3_VERSION) {
|
|
|
|
al = SSL_AD_HANDSHAKE_FAILURE;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
|
|
|
|
goto f_err;
|
|
|
|
} else if ((ssl->verify_mode & SSL_VERIFY_PEER) &&
|
|
|
|
(ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
|
|
|
|
/* Fail for TLS only if we required a certificate */
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
|
|
|
|
al = SSL_AD_HANDSHAKE_FAILURE;
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ssl_verify_cert_chain(ssl, sk) <= 0) {
|
|
|
|
al = ssl_verify_alarm_type(ssl->verify_result);
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
X509_free(ssl->session->peer);
|
|
|
|
ssl->session->peer = sk_X509_shift(sk);
|
|
|
|
ssl->session->verify_result = ssl->verify_result;
|
|
|
|
|
|
|
|
sk_X509_pop_free(ssl->session->cert_chain, X509_free);
|
|
|
|
ssl->session->cert_chain = sk;
|
|
|
|
/* Inconsistency alert: cert_chain does *not* include the peer's own
|
|
|
|
* certificate, while we do include it in s3_clnt.c */
|
|
|
|
|
|
|
|
sk = NULL;
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
if (0) {
|
|
|
|
f_err:
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
|
|
|
X509_free(x);
|
|
|
|
sk_X509_pop_free(sk, X509_free);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:49:42 +01:00
|
|
|
static int ssl3_get_client_key_exchange(SSL *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-05-13 23:28:17 +01:00
|
|
|
if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
|
2015-08-07 22:07:52 +01:00
|
|
|
int ok;
|
2015-12-19 22:05:56 +00:00
|
|
|
const long n = ssl->method->ssl_get_message(
|
2016-05-13 23:28:17 +01:00
|
|
|
ssl, SSL3_MT_CLIENT_KEY_EXCHANGE, ssl_hash_message, &ok);
|
2015-08-07 22:07:52 +01:00
|
|
|
if (!ok) {
|
|
|
|
return n;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!CBS_strdup(&psk_identity, &ssl->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. */
|
2015-12-19 22:05:56 +00:00
|
|
|
psk_len = ssl->psk_server_callback(ssl, ssl->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-05-13 23:28:17 +01:00
|
|
|
if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!ssl_has_private_key(ssl) ||
|
|
|
|
ssl_private_key_type(ssl) != EVP_PKEY_RSA) {
|
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-05-13 23:28:17 +01:00
|
|
|
assert(ssl->state == SSL3_ST_SR_KEY_EXCH_B);
|
2015-08-07 22:07:52 +01:00
|
|
|
/* Complete async decrypt. */
|
|
|
|
decrypt_result = ssl_private_key_decrypt_complete(
|
2015-12-19 22:05:56 +00:00
|
|
|
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-05-13 23:28:17 +01:00
|
|
|
ssl->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);
|
|
|
|
size_t i;
|
|
|
|
for (i = 2; i < padding_len - 1; i++) {
|
|
|
|
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],
|
2015-12-19 22:05:56 +00:00
|
|
|
(unsigned)(ssl->client_version >> 8));
|
2015-12-02 00:31:24 +00:00
|
|
|
good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
|
2015-12-19 22:05:56 +00:00
|
|
|
(unsigned)(ssl->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|. */
|
|
|
|
for (i = 0; i < premaster_secret_len; i++) {
|
|
|
|
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-05-25 20:06:05 +01:00
|
|
|
} else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) {
|
2016-05-17 00:31:05 +01:00
|
|
|
/* Parse the ClientKeyExchange. */
|
2015-12-19 21:58:04 +00:00
|
|
|
CBS peer_key;
|
2016-05-17 00:31:05 +01:00
|
|
|
if (!SSL_ECDH_CTX_get_key(&ssl->s3->tmp.ecdh_ctx, &client_key_exchange,
|
|
|
|
&peer_key) ||
|
|
|
|
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-05-16 22:27:14 +01:00
|
|
|
if (!SSL_ECDH_CTX_finish(&ssl->s3->tmp.ecdh_ctx, &premaster_secret,
|
|
|
|
&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. */
|
2015-12-19 22:05:56 +00:00
|
|
|
SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.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;
|
|
|
|
}
|
|
|
|
memset(premaster_secret, 0, premaster_secret_len);
|
|
|
|
} 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 */
|
2015-12-29 23:56:28 +00:00
|
|
|
ssl->session->master_key_length = tls1_generate_master_secret(
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl, ssl->session->master_key, premaster_secret, premaster_secret_len);
|
|
|
|
if (ssl->session->master_key_length == 0) {
|
2014-12-19 01:42:32 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->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-06-07 21:49:42 +01:00
|
|
|
static int ssl3_get_cert_verify(SSL *ssl) {
|
2014-12-19 01:42:32 +00:00
|
|
|
int al, ok, ret = 0;
|
|
|
|
long n;
|
|
|
|
CBS certificate_verify, signature;
|
2015-12-19 22:05:56 +00:00
|
|
|
X509 *peer = ssl->session->peer;
|
2014-12-19 01:42:32 +00:00
|
|
|
EVP_PKEY *pkey = NULL;
|
|
|
|
const EVP_MD *md = NULL;
|
|
|
|
uint8_t digest[EVP_MAX_MD_SIZE];
|
|
|
|
size_t digest_length;
|
|
|
|
EVP_PKEY_CTX *pctx = NULL;
|
|
|
|
|
|
|
|
/* Only RSA and ECDSA client certificates are supported, so a
|
|
|
|
* CertificateVerify is required if and only if there's a client certificate.
|
|
|
|
* */
|
|
|
|
if (peer == NULL) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl3_free_handshake_buffer(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-05-13 23:28:17 +01:00
|
|
|
n = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE_VERIFY,
|
|
|
|
ssl_dont_hash_message, &ok);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Filter out unsupported certificate types. */
|
|
|
|
pkey = X509_get_pubkey(peer);
|
2015-02-16 08:57:55 +00:00
|
|
|
if (pkey == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) ||
|
|
|
|
(pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) {
|
|
|
|
al = SSL_AD_UNSUPPORTED_CERTIFICATE;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
|
2014-12-19 01:42:32 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
CBS_init(&certificate_verify, ssl->init_msg, n);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
/* Determine the digest type if needbe. */
|
2015-12-30 05:08:49 +00:00
|
|
|
if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
|
2015-11-02 17:02:20 +00:00
|
|
|
uint8_t hash, signature_type;
|
|
|
|
if (!CBS_get_u8(&certificate_verify, &hash) ||
|
|
|
|
!CBS_get_u8(&certificate_verify, &signature_type)) {
|
|
|
|
al = SSL_AD_DECODE_ERROR;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
goto f_err;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!tls12_check_peer_sigalg(ssl, &md, &al, hash, signature_type, pkey)) {
|
2015-11-02 17:02:20 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the digest. */
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!ssl3_cert_verify_hash(ssl, digest, &digest_length, &md, pkey->type)) {
|
2014-12-19 01:42:32 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The handshake buffer is no longer necessary, and we may hash the current
|
|
|
|
* message.*/
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl3_free_handshake_buffer(ssl);
|
|
|
|
if (!ssl3_hash_current_message(ssl)) {
|
2015-02-17 00:33:53 +00:00
|
|
|
goto 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
pctx = EVP_PKEY_CTX_new(pkey, NULL);
|
|
|
|
if (pctx == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
2016-03-02 03:57:40 +00:00
|
|
|
int sig_ok = 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_length);
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
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-08-26 05:32:30 +01:00
|
|
|
err:
|
2014-12-19 01:42:32 +00:00
|
|
|
EVP_PKEY_CTX_free(pctx);
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
static int ssl3_get_next_proto(SSL *ssl) {
|
|
|
|
int ok;
|
|
|
|
long n;
|
|
|
|
CBS next_protocol, selected_protocol, padding;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
n = ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message,
|
|
|
|
&ok);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
if (!ok) {
|
|
|
|
return n;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
CBS_init(&next_protocol, ssl->init_msg, n);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
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) ||
|
|
|
|
CBS_len(&next_protocol) != 0 ||
|
|
|
|
!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
|
|
|
|
&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. */
|
|
|
|
static int ssl3_get_channel_id(SSL *ssl) {
|
|
|
|
int ret = -1, ok;
|
|
|
|
long n;
|
|
|
|
uint8_t channel_id_hash[EVP_MAX_MD_SIZE];
|
|
|
|
size_t channel_id_hash_len;
|
|
|
|
const uint8_t *p;
|
|
|
|
uint16_t extension_type;
|
|
|
|
EC_GROUP *p256 = NULL;
|
|
|
|
EC_KEY *key = NULL;
|
|
|
|
EC_POINT *point = NULL;
|
|
|
|
ECDSA_SIG sig;
|
|
|
|
BIGNUM x, y;
|
|
|
|
CBS encrypted_extensions, extension;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
n = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID_ENCRYPTED_EXTENSIONS,
|
|
|
|
ssl_dont_hash_message, &ok);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
if (!ok) {
|
|
|
|
return n;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
/* Before incorporating the EncryptedExtensions message to the handshake
|
|
|
|
* hash, compute the hash that should have been signed. */
|
|
|
|
if (!tls1_channel_id_hash(ssl, channel_id_hash, &channel_id_hash_len)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
assert(channel_id_hash_len == SHA256_DIGEST_LENGTH);
|
2015-08-05 13:50:34 +01:00
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
if (!ssl3_hash_current_message(ssl)) {
|
|
|
|
return -1;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
CBS_init(&encrypted_extensions, ssl->init_msg, n);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
/* EncryptedExtensions could include multiple extensions, but the only
|
2016-06-13 19:09:37 +01:00
|
|
|
* extension that could be negotiated is Channel ID, so there can only be one
|
|
|
|
* entry. */
|
2016-06-07 22:03:46 +01:00
|
|
|
if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
|
|
|
|
!CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
|
|
|
|
CBS_len(&encrypted_extensions) != 0 ||
|
|
|
|
extension_type != TLSEXT_TYPE_channel_id ||
|
|
|
|
CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_MESSAGE);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
|
|
|
|
if (!p256) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
BN_init(&x);
|
|
|
|
BN_init(&y);
|
|
|
|
sig.r = BN_new();
|
|
|
|
sig.s = BN_new();
|
|
|
|
if (sig.r == NULL || sig.s == NULL) {
|
|
|
|
goto err;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
p = CBS_data(&extension);
|
|
|
|
if (BN_bin2bn(p + 0, 32, &x) == NULL ||
|
|
|
|
BN_bin2bn(p + 32, 32, &y) == NULL ||
|
|
|
|
BN_bin2bn(p + 64, 32, sig.r) == NULL ||
|
|
|
|
BN_bin2bn(p + 96, 32, sig.s) == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
point = EC_POINT_new(p256);
|
|
|
|
if (!point ||
|
|
|
|
!EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
|
|
|
|
goto err;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
key = EC_KEY_new();
|
|
|
|
if (!key || !EC_KEY_set_group(key, p256) ||
|
|
|
|
!EC_KEY_set_public_key(key, point)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We stored the handshake hash in |tlsext_channel_id| the first time that we
|
|
|
|
* were called. */
|
|
|
|
if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
|
|
|
|
ssl->s3->tlsext_channel_id_valid = 0;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(ssl->s3->tlsext_channel_id, p, 64);
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
BN_free(&x);
|
|
|
|
BN_free(&y);
|
|
|
|
BN_free(sig.r);
|
|
|
|
BN_free(sig.s);
|
|
|
|
EC_KEY_free(key);
|
|
|
|
EC_POINT_free(point);
|
|
|
|
EC_GROUP_free(p256);
|
|
|
|
return ret;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
/* send a new session ticket (not necessarily for a new session) */
|
2016-06-07 21:49:42 +01:00
|
|
|
static int ssl3_send_new_session_ticket(SSL *ssl) {
|
2015-03-09 18:48:28 +00:00
|
|
|
int ret = -1;
|
|
|
|
uint8_t *session = NULL;
|
|
|
|
size_t session_len;
|
|
|
|
EVP_CIPHER_CTX ctx;
|
|
|
|
HMAC_CTX hctx;
|
|
|
|
|
|
|
|
EVP_CIPHER_CTX_init(&ctx);
|
|
|
|
HMAC_CTX_init(&hctx);
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->state == SSL3_ST_SW_SESSION_TICKET_A) {
|
2014-12-19 01:42:32 +00:00
|
|
|
uint8_t *p, *macstart;
|
|
|
|
int len;
|
|
|
|
unsigned int hlen;
|
2015-12-19 22:05:56 +00:00
|
|
|
SSL_CTX *tctx = ssl->initial_ctx;
|
2014-12-19 01:42:32 +00:00
|
|
|
uint8_t iv[EVP_MAX_IV_LENGTH];
|
|
|
|
uint8_t key_name[16];
|
|
|
|
/* The maximum overhead of encrypting the session is 16 (key name) + IV +
|
|
|
|
* one block of encryption overhead + HMAC. */
|
|
|
|
const size_t max_ticket_overhead =
|
|
|
|
16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE;
|
|
|
|
|
|
|
|
/* Serialize the SSL_SESSION to be encoded into the ticket. */
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!SSL_SESSION_to_bytes_for_ticket(ssl->session, &session,
|
|
|
|
&session_len)) {
|
2015-03-09 18:48:28 +00:00
|
|
|
goto err;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the session is too long, emit a dummy value rather than abort the
|
|
|
|
* connection. */
|
|
|
|
if (session_len > 0xFFFF - max_ticket_overhead) {
|
|
|
|
static const char kTicketPlaceholder[] = "TICKET TOO LARGE";
|
|
|
|
const size_t placeholder_len = strlen(kTicketPlaceholder);
|
|
|
|
|
|
|
|
OPENSSL_free(session);
|
2015-03-09 18:48:28 +00:00
|
|
|
session = NULL;
|
2014-12-19 01:42:32 +00:00
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
p = ssl_handshake_start(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
/* Emit ticket_lifetime_hint. */
|
|
|
|
l2n(0, p);
|
|
|
|
/* Emit ticket. */
|
|
|
|
s2n(placeholder_len, p);
|
|
|
|
memcpy(p, kTicketPlaceholder, placeholder_len);
|
|
|
|
p += placeholder_len;
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
len = p - ssl_handshake_start(ssl);
|
2016-06-01 16:46:58 +01:00
|
|
|
if (!ssl_set_handshake_header(ssl, SSL3_MT_NEW_SESSION_TICKET, len)) {
|
2015-03-09 18:48:28 +00:00
|
|
|
goto err;
|
2015-02-17 00:33:53 +00:00
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
|
|
|
|
return ssl_do_write(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Grow buffer if need be: the length calculation is as follows:
|
|
|
|
* handshake_header_length + 4 (ticket lifetime hint) + 2 (ticket length) +
|
|
|
|
* max_ticket_overhead + * session_length */
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!BUF_MEM_grow(ssl->init_buf, SSL_HM_HEADER_LENGTH(ssl) + 6 +
|
2014-12-19 01:42:32 +00:00
|
|
|
max_ticket_overhead + session_len)) {
|
2015-03-09 18:48:28 +00:00
|
|
|
goto err;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
p = ssl_handshake_start(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
/* Initialize HMAC and cipher contexts. If callback present it does all the
|
|
|
|
* work otherwise use generated values from parent ctx. */
|
|
|
|
if (tctx->tlsext_ticket_key_cb) {
|
2015-12-19 22:05:56 +00:00
|
|
|
if (tctx->tlsext_ticket_key_cb(ssl, key_name, iv, &ctx, &hctx,
|
2015-04-26 18:07:57 +01:00
|
|
|
1 /* encrypt */) < 0) {
|
2015-03-09 18:48:28 +00:00
|
|
|
goto err;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!RAND_bytes(iv, 16) ||
|
|
|
|
!EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
|
|
|
|
tctx->tlsext_tick_aes_key, iv) ||
|
|
|
|
!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
|
|
|
|
NULL)) {
|
2015-03-09 18:48:28 +00:00
|
|
|
goto err;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
memcpy(key_name, tctx->tlsext_tick_key_name, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ticket lifetime hint (advisory only): We leave this unspecified for
|
|
|
|
* resumed session (for simplicity), and guess that tickets for new
|
|
|
|
* sessions will live as long as their sessions. */
|
2015-12-19 22:05:56 +00:00
|
|
|
l2n(ssl->hit ? 0 : ssl->session->timeout, p);
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
/* Skip ticket length for now */
|
|
|
|
p += 2;
|
|
|
|
/* Output key name */
|
|
|
|
macstart = p;
|
|
|
|
memcpy(p, key_name, 16);
|
|
|
|
p += 16;
|
|
|
|
/* output IV */
|
|
|
|
memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
|
|
|
|
p += EVP_CIPHER_CTX_iv_length(&ctx);
|
|
|
|
/* Encrypt session data */
|
2015-03-09 18:48:28 +00:00
|
|
|
if (!EVP_EncryptUpdate(&ctx, p, &len, session, session_len)) {
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
p += len;
|
2015-03-09 18:48:28 +00:00
|
|
|
if (!EVP_EncryptFinal_ex(&ctx, p, &len)) {
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
p += len;
|
|
|
|
|
2015-03-09 18:48:28 +00:00
|
|
|
if (!HMAC_Update(&hctx, macstart, p - macstart) ||
|
|
|
|
!HMAC_Final(&hctx, p, &hlen)) {
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-19 01:42:32 +00:00
|
|
|
|
|
|
|
p += hlen;
|
|
|
|
/* Now write out lengths: p points to end of data written */
|
|
|
|
/* Total length */
|
2015-12-19 22:05:56 +00:00
|
|
|
len = p - ssl_handshake_start(ssl);
|
2014-12-19 01:42:32 +00:00
|
|
|
/* Skip ticket lifetime hint */
|
2015-12-19 22:05:56 +00:00
|
|
|
p = ssl_handshake_start(ssl) + 4;
|
2014-12-19 01:42:32 +00:00
|
|
|
s2n(len - 6, p);
|
2016-06-01 16:46:58 +01:00
|
|
|
if (!ssl_set_handshake_header(ssl, SSL3_MT_NEW_SESSION_TICKET, len)) {
|
2015-03-09 18:48:28 +00:00
|
|
|
goto err;
|
2015-02-17 00:33:53 +00:00
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* SSL3_ST_SW_SESSION_TICKET_B */
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = ssl_do_write(ssl);
|
2015-03-09 18:48:28 +00:00
|
|
|
|
|
|
|
err:
|
2015-04-22 21:17:58 +01:00
|
|
|
OPENSSL_free(session);
|
2015-03-09 18:48:28 +00:00
|
|
|
EVP_CIPHER_CTX_cleanup(&ctx);
|
|
|
|
HMAC_CTX_cleanup(&hctx);
|
|
|
|
return ret;
|
2014-12-19 01:42:32 +00:00
|
|
|
}
|