2014-06-20 20:00:00 +01:00
|
|
|
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This package is an SSL implementation written
|
|
|
|
* by Eric Young (eay@cryptsoft.com).
|
|
|
|
* The implementation was written so as to conform with Netscapes SSL.
|
|
|
|
*
|
|
|
|
* This library is free for commercial and non-commercial use as long as
|
|
|
|
* the following conditions are aheared to. The following conditions
|
|
|
|
* apply to all code found in this distribution, be it the RC4, RSA,
|
|
|
|
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
|
|
|
* included with this distribution is covered by the same copyright terms
|
|
|
|
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
|
|
|
*
|
|
|
|
* Copyright remains Eric Young's, and as such any Copyright notices in
|
|
|
|
* the code are not to be removed.
|
|
|
|
* If this package is used in a product, Eric Young should be given attribution
|
|
|
|
* as the author of the parts of the library used.
|
|
|
|
* This can be in the form of a textual message at program startup or
|
|
|
|
* in documentation (online or textual) provided with the package.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* "This product includes cryptographic software written by
|
|
|
|
* Eric Young (eay@cryptsoft.com)"
|
|
|
|
* The word 'cryptographic' can be left out if the rouines from the library
|
|
|
|
* being used are not cryptographic related :-).
|
|
|
|
* 4. If you include any Windows specific code (or a derivative thereof) from
|
|
|
|
* the apps directory (application code) you must include an acknowledgement:
|
|
|
|
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* The licence and distribution terms for any publically available version or
|
|
|
|
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
|
|
|
* copied and put under another distribution licence
|
|
|
|
* [including the GNU Public Licence.]
|
|
|
|
*/
|
|
|
|
/* ====================================================================
|
|
|
|
* Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. All advertising materials mentioning features or use of this
|
|
|
|
* software must display the following acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
|
|
|
|
*
|
|
|
|
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* prior written permission. For written permission, please contact
|
|
|
|
* openssl-core@openssl.org.
|
|
|
|
*
|
|
|
|
* 5. Products derived from this software may not be called "OpenSSL"
|
|
|
|
* nor may "OpenSSL" appear in their names without prior written
|
|
|
|
* permission of the OpenSSL Project.
|
|
|
|
*
|
|
|
|
* 6. Redistributions of any form whatsoever must retain the following
|
|
|
|
* acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
|
|
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
|
|
|
*
|
|
|
|
* This product includes cryptographic software written by Eric Young
|
|
|
|
* (eay@cryptsoft.com). This product includes software written by Tim
|
|
|
|
* Hudson (tjh@cryptsoft.com).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/* ====================================================================
|
|
|
|
* Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
|
|
|
|
*
|
|
|
|
* Portions of the attached software ("Contribution") are developed by
|
|
|
|
* SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
|
|
|
|
*
|
|
|
|
* The Contribution is licensed pursuant to the OpenSSL open source
|
|
|
|
* license provided above.
|
|
|
|
*
|
|
|
|
* ECC cipher suite support in OpenSSL originally written by
|
|
|
|
* Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/* ====================================================================
|
|
|
|
* Copyright 2005 Nokia. All rights reserved.
|
|
|
|
*
|
|
|
|
* The portions of the attached software ("Contribution") is developed by
|
|
|
|
* Nokia Corporation and is licensed pursuant to the OpenSSL open source
|
|
|
|
* license.
|
|
|
|
*
|
|
|
|
* The Contribution, originally written by Mika Kousa and Pasi Eronen of
|
|
|
|
* Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
|
|
|
|
* support (see RFC 4279) to OpenSSL.
|
|
|
|
*
|
|
|
|
* No patent licenses or other rights except those expressly stated in
|
|
|
|
* the OpenSSL open source license shall be deemed granted or received
|
|
|
|
* expressly, by implication, estoppel, or otherwise.
|
|
|
|
*
|
|
|
|
* No assurances are provided by Nokia that the Contribution does not
|
|
|
|
* infringe the patent or other intellectual property rights of any third
|
|
|
|
* party or that the license provides you with all the necessary rights
|
|
|
|
* to make use of the Contribution.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
|
|
|
|
* ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
|
|
|
|
* SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
|
|
|
|
* OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
|
|
|
|
* OTHERWISE.
|
|
|
|
*/
|
|
|
|
|
2015-09-15 06:48:04 +01:00
|
|
|
#include <openssl/ssl.h>
|
|
|
|
|
Delay creating s->session until resumption is resolved.
When not offering to resume a session, the client populates s->session with a
fresh SSL_SESSION before the ServerHello is processed and, in DTLS_ANY_VERSION,
before the version is even determined. Don't create a fresh SSL_SESSION until
we know we are doing a full handshake.
This brings ssl3_send_client_hello closer to ssl23_client_hello in behavior. It
also fixes ssl_version in the client in DTLS_ANY_VERSION.
SSLv23_client_method is largely unchanged. If no session is offered, s->session
continues to be NULL until the ServerHello is received. The one difference is
that s->session isn't populated until the entire ServerHello is received,
rather than just the first half, in the case of a fragmented ServerHello. Apart
from info_callback, no external hooks get called between those points, so this
shouldn't expose new missing NULL checks.
The other client methods change significantly to match SSLv23_client_method's
behavior. For TLS, any exposed missing NULL checks are also in
SSLv23_client_method (and version-specific methods are already weird), so that
should be safe. For DTLS, I've verified that accesses in d1_*.c either handle
NULL or are after the ServerHello.
Change-Id: Idcae6bd242480e28a57dbba76ce67f1ac1ae1d1d
Reviewed-on: https://boringssl-review.googlesource.com/2404
Reviewed-by: Adam Langley <agl@google.com>
2014-11-23 09:20:17 +00:00
|
|
|
#include <assert.h>
|
2015-04-08 04:05:04 +01:00
|
|
|
#include <string.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-06-12 06:40:23 +01:00
|
|
|
#include <openssl/bn.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/buf.h>
|
2014-06-25 04:27:17 +01:00
|
|
|
#include <openssl/bytestring.h>
|
2015-06-12 06:40:23 +01:00
|
|
|
#include <openssl/dh.h>
|
|
|
|
#include <openssl/ec_key.h>
|
|
|
|
#include <openssl/ecdsa.h>
|
2015-04-08 04:05:04 +01:00
|
|
|
#include <openssl/err.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/md5.h>
|
2015-06-12 06:40:23 +01:00
|
|
|
#include <openssl/mem.h>
|
|
|
|
#include <openssl/rand.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/x509.h>
|
2015-06-08 00:50:37 +01:00
|
|
|
#include <openssl/x509v3.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-04-08 03:38:30 +01:00
|
|
|
#include "internal.h"
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-07 21:40:46 +01:00
|
|
|
static int ssl3_send_client_hello(SSL *ssl);
|
|
|
|
static int dtls1_get_hello_verify(SSL *ssl);
|
|
|
|
static int ssl3_get_server_hello(SSL *ssl);
|
2016-06-07 22:03:46 +01:00
|
|
|
static int ssl3_get_server_certificate(SSL *ssl);
|
2016-06-07 21:40:46 +01:00
|
|
|
static int ssl3_get_cert_status(SSL *ssl);
|
2016-06-07 22:03:46 +01:00
|
|
|
static int ssl3_verify_server_cert(SSL *ssl);
|
|
|
|
static int ssl3_get_server_key_exchange(SSL *ssl);
|
|
|
|
static int ssl3_get_certificate_request(SSL *ssl);
|
2016-06-07 22:13:28 +01:00
|
|
|
static int ssl3_get_server_hello_done(SSL *ssl);
|
2016-06-07 21:40:46 +01:00
|
|
|
static int ssl3_send_client_certificate(SSL *ssl);
|
|
|
|
static int ssl3_send_client_key_exchange(SSL *ssl);
|
2016-06-07 22:03:46 +01:00
|
|
|
static int ssl3_send_cert_verify(SSL *ssl);
|
2016-06-07 21:40:46 +01:00
|
|
|
static int ssl3_send_next_proto(SSL *ssl);
|
|
|
|
static int ssl3_send_channel_id(SSL *ssl);
|
2016-06-07 22:03:46 +01:00
|
|
|
static int ssl3_get_new_session_ticket(SSL *ssl);
|
2016-06-07 21:40:46 +01:00
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
int ssl3_connect(SSL *ssl) {
|
2014-12-18 03:06:57 +00:00
|
|
|
int ret = -1;
|
2016-06-02 01:16:03 +01:00
|
|
|
int state, skip = 0;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
assert(ssl->handshake_func == ssl3_connect);
|
|
|
|
assert(!ssl->server);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
|
|
|
for (;;) {
|
2015-12-19 22:05:56 +00:00
|
|
|
state = ssl->state;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
switch (ssl->state) {
|
2016-09-01 06:33:33 +01:00
|
|
|
case SSL_ST_INIT:
|
|
|
|
ssl->state = SSL_ST_CONNECT;
|
|
|
|
skip = 1;
|
|
|
|
break;
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
case SSL_ST_CONNECT:
|
2016-06-02 01:16:03 +01:00
|
|
|
ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-07-11 18:19:03 +01:00
|
|
|
ssl->s3->hs = ssl_handshake_new(tls13_client_handshake);
|
2016-07-27 19:01:59 +01:00
|
|
|
if (ssl->s3->hs == NULL) {
|
2016-07-08 17:16:50 +01:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-05-06 02:50:24 +01:00
|
|
|
if (!ssl_init_wbio_buffer(ssl)) {
|
2014-12-18 03:06:57 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CW_CLNT_HELLO_A:
|
|
|
|
case SSL3_ST_CW_CLNT_HELLO_B:
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = ssl3_send_client_hello(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-06-07 21:40:46 +01:00
|
|
|
|
2016-08-02 21:22:34 +01:00
|
|
|
if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) {
|
2016-06-07 21:40:46 +01:00
|
|
|
ssl->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
|
|
|
|
} else {
|
|
|
|
ssl->s3->tmp.next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
|
|
|
|
}
|
2016-05-06 02:50:24 +01:00
|
|
|
ssl->state = SSL3_ST_CW_FLUSH;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
2016-06-07 21:40:46 +01:00
|
|
|
case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
|
2016-08-02 21:22:34 +01:00
|
|
|
assert(SSL_is_dtls(ssl));
|
2016-06-07 21:40:46 +01:00
|
|
|
ret = dtls1_get_hello_verify(ssl);
|
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (ssl->d1->send_cookie) {
|
|
|
|
ssl->method->received_flight(ssl);
|
|
|
|
ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
|
|
|
|
} else {
|
|
|
|
ssl->state = SSL3_ST_CR_SRVR_HELLO_A;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
case SSL3_ST_CR_SRVR_HELLO_A:
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = ssl3_get_server_hello(ssl);
|
2016-07-11 18:19:03 +01:00
|
|
|
if (ssl->state == SSL_ST_TLS13) {
|
|
|
|
break;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
if (ssl->session != NULL) {
|
2016-06-13 18:56:33 +01:00
|
|
|
ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
} else {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_CR_CERT_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CR_CERT_A:
|
2016-09-06 19:29:47 +01:00
|
|
|
if (ssl->s3->hs->use_cert_auth) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = ssl3_get_server_certificate(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = 1;
|
|
|
|
}
|
2016-06-13 18:56:33 +01:00
|
|
|
ssl->state = SSL3_ST_CR_CERT_STATUS_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
case SSL3_ST_CR_CERT_STATUS_A:
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ssl->s3->tmp.certificate_status_expected) {
|
|
|
|
ret = ssl3_get_cert_status(ssl);
|
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = 1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
|
|
|
|
break;
|
|
|
|
|
2015-08-18 12:21:54 +01:00
|
|
|
case SSL3_ST_VERIFY_SERVER_CERT:
|
2016-09-06 19:29:47 +01:00
|
|
|
if (ssl->s3->hs->use_cert_auth) {
|
2016-06-13 18:56:33 +01:00
|
|
|
ret = ssl3_verify_server_cert(ssl);
|
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = 1;
|
2015-08-18 12:21:54 +01:00
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_CR_KEY_EXCH_A;
|
2015-08-18 12:21:54 +01:00
|
|
|
break;
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
case SSL3_ST_CR_KEY_EXCH_A:
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = ssl3_get_server_key_exchange(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-06-13 18:56:33 +01:00
|
|
|
ssl->state = SSL3_ST_CR_CERT_REQ_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CR_CERT_REQ_A:
|
2016-09-06 19:29:47 +01:00
|
|
|
if (ssl->s3->hs->use_cert_auth) {
|
2016-06-13 18:56:33 +01:00
|
|
|
ret = ssl3_get_certificate_request(ssl);
|
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_CR_SRVR_DONE_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CR_SRVR_DONE_A:
|
2016-06-07 22:13:28 +01:00
|
|
|
ret = ssl3_get_server_hello_done(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-06-07 21:40:46 +01:00
|
|
|
ssl->method->received_flight(ssl);
|
2016-06-13 18:56:33 +01:00
|
|
|
ssl->state = SSL3_ST_CW_CERT_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CW_CERT_A:
|
|
|
|
case SSL3_ST_CW_CERT_B:
|
|
|
|
case SSL3_ST_CW_CERT_C:
|
2016-06-13 19:05:19 +01:00
|
|
|
if (ssl->s3->tmp.cert_request) {
|
2016-06-13 18:56:33 +01:00
|
|
|
ret = ssl3_send_client_certificate(ssl);
|
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_CW_KEY_EXCH_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CW_KEY_EXCH_A:
|
|
|
|
case SSL3_ST_CW_KEY_EXCH_B:
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = ssl3_send_client_key_exchange(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-06-13 18:56:33 +01:00
|
|
|
ssl->state = SSL3_ST_CW_CERT_VRFY_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CW_CERT_VRFY_A:
|
|
|
|
case SSL3_ST_CW_CERT_VRFY_B:
|
2015-05-29 22:11:21 +01:00
|
|
|
case SSL3_ST_CW_CERT_VRFY_C:
|
2016-06-13 19:05:19 +01:00
|
|
|
if (ssl->s3->tmp.cert_request) {
|
2016-06-13 18:56:33 +01:00
|
|
|
ret = ssl3_send_cert_verify(ssl);
|
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2016-06-28 16:22:02 +01:00
|
|
|
ssl->state = SSL3_ST_CW_CHANGE;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
2016-06-28 16:22:02 +01:00
|
|
|
case SSL3_ST_CW_CHANGE:
|
|
|
|
ret = ssl->method->send_change_cipher_spec(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2016-06-13 18:56:33 +01:00
|
|
|
ssl->state = SSL3_ST_CW_NEXT_PROTO_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-25 20:48:39 +00:00
|
|
|
if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
|
2014-12-18 03:06:57 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CW_NEXT_PROTO_A:
|
|
|
|
case SSL3_ST_CW_NEXT_PROTO_B:
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ssl->s3->next_proto_neg_seen) {
|
|
|
|
ret = ssl3_send_next_proto(ssl);
|
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
} else {
|
2016-06-13 18:56:33 +01:00
|
|
|
skip = 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2016-06-13 18:56:33 +01:00
|
|
|
ssl->state = SSL3_ST_CW_CHANNEL_ID_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CW_CHANNEL_ID_A:
|
|
|
|
case SSL3_ST_CW_CHANNEL_ID_B:
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ssl->s3->tlsext_channel_id_valid) {
|
|
|
|
ret = ssl3_send_channel_id(ssl);
|
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_CW_FINISHED_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CW_FINISHED_A:
|
|
|
|
case SSL3_ST_CW_FINISHED_B:
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = ssl3_send_finished(ssl, SSL3_ST_CW_FINISHED_A,
|
2015-12-30 00:13:58 +00:00
|
|
|
SSL3_ST_CW_FINISHED_B);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_CW_FLUSH;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
if (ssl->session != NULL) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->tmp.next_state = SSL_ST_OK;
|
2014-12-18 03:06:57 +00:00
|
|
|
} else {
|
|
|
|
/* This is a non-resumption handshake. If it involves ChannelID, then
|
|
|
|
* record the handshake hashes at this point in the session so that
|
|
|
|
* any resumption of this session with ChannelID can sign those
|
|
|
|
* hashes. */
|
2015-12-19 22:05:56 +00:00
|
|
|
ret = tls1_record_handshake_hashes_for_channel_id(ssl);
|
2015-07-10 22:33:46 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
|
|
|
|
ssl3_can_false_start(ssl) &&
|
2015-02-17 00:16:46 +00:00
|
|
|
/* No False Start on renegotiation (would complicate the state
|
|
|
|
* machine). */
|
2015-12-19 22:05:56 +00:00
|
|
|
!ssl->s3->initial_handshake_complete) {
|
|
|
|
ssl->s3->tmp.next_state = SSL3_ST_FALSE_START;
|
2014-12-18 03:06:57 +00:00
|
|
|
} else {
|
2016-06-13 18:56:33 +01:00
|
|
|
ssl->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
case SSL3_ST_FALSE_START:
|
2016-06-13 18:56:33 +01:00
|
|
|
ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
|
2016-06-07 22:03:46 +01:00
|
|
|
ssl->s3->tmp.in_false_start = 1;
|
|
|
|
|
|
|
|
ssl_free_wbio_buffer(ssl);
|
|
|
|
ret = 1;
|
|
|
|
goto end;
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
case SSL3_ST_CR_SESSION_TICKET_A:
|
2016-06-13 18:56:33 +01:00
|
|
|
if (ssl->tlsext_ticket_expected) {
|
|
|
|
ret = ssl3_get_new_session_ticket(ssl);
|
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_CR_CHANGE;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CR_CHANGE:
|
2016-06-15 22:44:37 +01:00
|
|
|
ret = ssl->method->read_change_cipher_spec(ssl);
|
2015-11-26 07:16:49 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2015-12-25 20:48:39 +00:00
|
|
|
if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
|
2014-12-18 03:06:57 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL3_ST_CR_FINISHED_A;
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CR_FINISHED_A:
|
2016-05-13 23:28:17 +01:00
|
|
|
ret = ssl3_get_finished(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-06-07 21:40:46 +01:00
|
|
|
ssl->method->received_flight(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
if (ssl->session != NULL) {
|
2016-06-28 16:22:02 +01:00
|
|
|
ssl->state = SSL3_ST_CW_CHANGE;
|
2014-12-18 03:06:57 +00:00
|
|
|
} else {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = SSL_ST_OK;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL3_ST_CW_FLUSH:
|
2015-12-19 22:05:56 +00:00
|
|
|
if (BIO_flush(ssl->wbio) <= 0) {
|
2016-03-12 03:56:19 +00:00
|
|
|
ssl->rwstate = SSL_WRITING;
|
2014-12-18 03:06:57 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->state = ssl->s3->tmp.next_state;
|
2016-06-07 21:40:46 +01:00
|
|
|
if (ssl->state != SSL_ST_OK) {
|
|
|
|
ssl->method->expect_flight(ssl);
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
break;
|
|
|
|
|
2016-07-11 18:19:03 +01:00
|
|
|
case SSL_ST_TLS13:
|
|
|
|
ret = tls13_handshake(ssl);
|
|
|
|
if (ret <= 0) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
ssl->state = SSL_ST_OK;
|
|
|
|
break;
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
case SSL_ST_OK:
|
2016-07-27 22:51:49 +01:00
|
|
|
/* Clean a few things up. */
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl3_cleanup_key_block(ssl);
|
2016-07-27 22:51:49 +01:00
|
|
|
ssl->method->release_current_message(ssl, 1 /* free_buffer */);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
SSL_SESSION_free(ssl->s3->established_session);
|
|
|
|
if (ssl->session != NULL) {
|
2016-08-05 15:51:43 +01:00
|
|
|
SSL_SESSION_up_ref(ssl->session);
|
|
|
|
ssl->s3->established_session = ssl->session;
|
2016-06-27 21:34:59 +01:00
|
|
|
} else {
|
|
|
|
/* We make a copy of the session in order to maintain the immutability
|
|
|
|
* of the new established_session due to False Start. The caller may
|
|
|
|
* have taken a reference to the temporary session. */
|
|
|
|
ssl->s3->established_session =
|
2016-07-29 19:32:55 +01:00
|
|
|
SSL_SESSION_dup(ssl->s3->new_session, SSL_SESSION_DUP_ALL);
|
2016-06-27 21:34:59 +01:00
|
|
|
if (ssl->s3->established_session == NULL) {
|
|
|
|
/* Do not stay in SSL_ST_OK, to avoid confusing |SSL_in_init|
|
|
|
|
* callers. */
|
|
|
|
ssl->state = SSL_ST_ERROR;
|
|
|
|
skip = 1;
|
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
ssl->s3->established_session->not_resumable = 0;
|
|
|
|
|
|
|
|
SSL_SESSION_free(ssl->s3->new_session);
|
|
|
|
ssl->s3->new_session = NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
/* Remove write buffering now. */
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl_free_wbio_buffer(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-07-11 18:19:03 +01:00
|
|
|
ssl_handshake_free(ssl->s3->hs);
|
|
|
|
ssl->s3->hs = NULL;
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
|
2015-06-18 23:36:15 +01:00
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->tmp.in_false_start = 0;
|
|
|
|
ssl->s3->initial_handshake_complete = 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-06-18 23:36:15 +01:00
|
|
|
if (is_initial_handshake) {
|
|
|
|
/* Renegotiations do not participate in session resumption. */
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl_update_cache(ssl, SSL_SESS_CACHE_CLIENT);
|
2015-06-18 23:36:15 +01:00
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-07 21:40:46 +01:00
|
|
|
ret = 1;
|
2016-06-02 01:16:03 +01:00
|
|
|
ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto end;
|
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
case SSL_ST_ERROR:
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
|
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
default:
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
|
2014-12-18 03:06:57 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2016-06-02 01:16:03 +01:00
|
|
|
if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) {
|
|
|
|
int new_state = ssl->state;
|
|
|
|
ssl->state = state;
|
|
|
|
ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
|
|
|
|
ssl->state = new_state;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
skip = 0;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
end:
|
2016-06-02 01:16:03 +01:00
|
|
|
ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
|
2014-12-18 03:06:57 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-07-21 11:55:28 +01:00
|
|
|
static int ssl_write_client_cipher_list(SSL *ssl, CBB *out,
|
|
|
|
uint16_t min_version,
|
|
|
|
uint16_t max_version,
|
|
|
|
uint16_t real_max_version) {
|
2015-10-10 19:13:23 +01:00
|
|
|
/* Prepare disabled cipher masks. */
|
|
|
|
ssl_set_client_disabled(ssl);
|
|
|
|
|
|
|
|
CBB child;
|
|
|
|
if (!CBB_add_u16_length_prefixed(out, &child)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-10-10 19:13:23 +01:00
|
|
|
STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
|
|
|
|
|
|
|
|
int any_enabled = 0;
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
|
|
|
|
const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
|
|
|
|
/* Skip disabled ciphers */
|
2015-11-05 23:28:33 +00:00
|
|
|
if ((cipher->algorithm_mkey & ssl->cert->mask_k) ||
|
|
|
|
(cipher->algorithm_auth & ssl->cert->mask_a)) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-25 04:55:58 +01:00
|
|
|
if (SSL_CIPHER_get_min_version(cipher) > max_version ||
|
|
|
|
SSL_CIPHER_get_max_version(cipher) < min_version) {
|
2015-10-10 19:13:23 +01:00
|
|
|
continue;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-10-10 19:13:23 +01:00
|
|
|
any_enabled = 1;
|
|
|
|
if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-07-29 19:32:55 +01:00
|
|
|
/* Add PSK ciphers for TLS 1.3 resumption. */
|
|
|
|
if (ssl->session != NULL &&
|
|
|
|
ssl->method->version_from_wire(ssl->session->ssl_version) >=
|
|
|
|
TLS1_3_VERSION) {
|
|
|
|
uint16_t resumption_cipher;
|
|
|
|
if (ssl_cipher_get_ecdhe_psk_cipher(cipher, &resumption_cipher) &&
|
|
|
|
!CBB_add_u16(&child, resumption_cipher)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2015-10-10 19:13:23 +01:00
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-10-10 19:13:23 +01:00
|
|
|
/* If all ciphers were disabled, return the error to the caller. */
|
|
|
|
if (!any_enabled) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-10-10 19:13:23 +01:00
|
|
|
/* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
|
|
|
|
* added. */
|
|
|
|
if (ssl->client_version == SSL3_VERSION &&
|
|
|
|
!ssl->s3->initial_handshake_complete) {
|
|
|
|
if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
|
|
|
|
return 0;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-10-10 19:13:23 +01:00
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-07-21 11:55:28 +01:00
|
|
|
if ((ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) ||
|
|
|
|
real_max_version > max_version) {
|
|
|
|
if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-10 19:13:23 +01:00
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-10-10 19:13:23 +01:00
|
|
|
return CBB_flush(out);
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-07-18 17:40:30 +01:00
|
|
|
int ssl_add_client_hello_body(SSL *ssl, CBB *body) {
|
2016-07-21 11:55:28 +01:00
|
|
|
uint16_t min_version, max_version, real_max_version;
|
|
|
|
if (!ssl_get_full_version_range(ssl, &min_version, &max_version,
|
|
|
|
&real_max_version)) {
|
2016-07-18 17:40:30 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Renegotiations do not participate in session resumption. */
|
|
|
|
int has_session = ssl->session != NULL &&
|
|
|
|
!ssl->s3->initial_handshake_complete;
|
|
|
|
|
|
|
|
CBB child;
|
|
|
|
if (!CBB_add_u16(body, ssl->client_version) ||
|
|
|
|
!CBB_add_bytes(body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
|
|
|
|
!CBB_add_u8_length_prefixed(body, &child) ||
|
|
|
|
(has_session &&
|
|
|
|
!CBB_add_bytes(&child, ssl->session->session_id,
|
|
|
|
ssl->session->session_id_length))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-02 21:22:34 +01:00
|
|
|
if (SSL_is_dtls(ssl)) {
|
2016-07-18 17:40:30 +01:00
|
|
|
if (!CBB_add_u8_length_prefixed(body, &child) ||
|
|
|
|
!CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t header_len =
|
2016-08-02 21:22:34 +01:00
|
|
|
SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
|
2016-07-21 11:55:28 +01:00
|
|
|
if (!ssl_write_client_cipher_list(ssl, body, min_version, max_version,
|
|
|
|
real_max_version) ||
|
2016-07-18 17:40:30 +01:00
|
|
|
!CBB_add_u8(body, 1 /* one compression method */) ||
|
|
|
|
!CBB_add_u8(body, 0 /* null compression */) ||
|
|
|
|
!ssl_add_clienthello_tlsext(ssl, body, header_len + CBB_len(body))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:40:46 +01:00
|
|
|
static int ssl3_send_client_hello(SSL *ssl) {
|
2015-10-10 19:13:23 +01:00
|
|
|
if (ssl->state == SSL3_ST_CW_CLNT_HELLO_B) {
|
2016-06-17 23:48:29 +01:00
|
|
|
return ssl->method->write_message(ssl);
|
2015-10-10 19:13:23 +01:00
|
|
|
}
|
|
|
|
|
2016-06-07 21:40:46 +01:00
|
|
|
/* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we
|
|
|
|
* may send multiple ClientHellos if we receive HelloVerifyRequest. */
|
|
|
|
if (!ssl3_init_handshake_buffer(ssl)) {
|
2015-11-02 22:16:13 +00:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-10 19:13:23 +01:00
|
|
|
CBB cbb;
|
|
|
|
CBB_zero(&cbb);
|
|
|
|
|
2016-06-21 15:33:21 +01:00
|
|
|
uint16_t min_version, max_version;
|
|
|
|
if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-10-10 19:13:23 +01:00
|
|
|
assert(ssl->state == SSL3_ST_CW_CLNT_HELLO_A);
|
|
|
|
if (!ssl->s3->have_version) {
|
2016-06-21 15:33:21 +01:00
|
|
|
ssl->version = ssl->method->version_to_wire(max_version);
|
2015-10-10 19:13:23 +01:00
|
|
|
/* Only set |ssl->client_version| on the initial handshake. Renegotiations,
|
|
|
|
* although locked to a version, reuse the value. When using the plain RSA
|
|
|
|
* key exchange, the ClientHello version is checked in the premaster secret.
|
|
|
|
* Some servers fail when this value changes. */
|
2016-06-21 15:33:21 +01:00
|
|
|
ssl->client_version = ssl->version;
|
2015-10-10 19:13:23 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 20:10:44 +00:00
|
|
|
/* If the configured session has expired or was created at a disabled
|
|
|
|
* version, drop it. */
|
2016-06-21 15:33:21 +01:00
|
|
|
if (ssl->session != NULL) {
|
|
|
|
uint16_t session_version =
|
|
|
|
ssl->method->version_from_wire(ssl->session->ssl_version);
|
2016-07-29 19:32:55 +01:00
|
|
|
if ((session_version < TLS1_3_VERSION &&
|
|
|
|
ssl->session->session_id_length == 0) ||
|
|
|
|
ssl->session->not_resumable ||
|
|
|
|
!ssl_session_is_time_valid(ssl, ssl->session) ||
|
2016-06-21 15:33:21 +01:00
|
|
|
session_version < min_version || session_version > max_version) {
|
2016-09-01 06:10:07 +01:00
|
|
|
ssl_set_session(ssl, NULL);
|
2016-06-21 15:33:21 +01:00
|
|
|
}
|
2015-10-10 19:13:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
|
|
|
|
* renegerate the client_random. The random must be reused. */
|
2016-08-02 21:22:34 +01:00
|
|
|
if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) &&
|
2016-07-10 17:20:35 +01:00
|
|
|
!RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
|
2015-10-10 19:13:23 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-07-18 17:40:30 +01:00
|
|
|
CBB body;
|
2016-06-17 23:48:29 +01:00
|
|
|
if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO) ||
|
2016-07-18 17:40:30 +01:00
|
|
|
!ssl_add_client_hello_body(ssl, &body) ||
|
2016-06-17 23:48:29 +01:00
|
|
|
!ssl->method->finish_message(ssl, &cbb)) {
|
2015-10-10 19:13:23 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->state = SSL3_ST_CW_CLNT_HELLO_B;
|
2016-06-17 23:48:29 +01:00
|
|
|
return ssl->method->write_message(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
err:
|
2015-10-10 19:13:23 +01:00
|
|
|
CBB_cleanup(&cbb);
|
2014-12-18 03:06:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:40:46 +01:00
|
|
|
static int dtls1_get_hello_verify(SSL *ssl) {
|
2016-07-08 22:32:11 +01:00
|
|
|
int al;
|
2016-06-07 21:40:46 +01:00
|
|
|
CBS hello_verify_request, cookie;
|
|
|
|
uint16_t server_version;
|
|
|
|
|
2016-07-08 22:32:11 +01:00
|
|
|
int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
|
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
2016-06-07 21:40:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
|
|
|
|
ssl->d1->send_cookie = 0;
|
|
|
|
ssl->s3->tmp.reuse_message = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-07-08 22:32:11 +01:00
|
|
|
CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num);
|
2016-06-07 21:40:46 +01:00
|
|
|
|
|
|
|
if (!CBS_get_u16(&hello_verify_request, &server_version) ||
|
|
|
|
!CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
|
|
|
|
CBS_len(&hello_verify_request) != 0) {
|
|
|
|
al = SSL_AD_DECODE_ERROR;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) {
|
|
|
|
al = SSL_AD_ILLEGAL_PARAMETER;
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
|
|
|
|
ssl->d1->cookie_len = CBS_len(&cookie);
|
|
|
|
|
|
|
|
ssl->d1->send_cookie = 1;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
f_err:
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ssl3_get_server_hello(SSL *ssl) {
|
2015-05-03 20:21:28 +01:00
|
|
|
STACK_OF(SSL_CIPHER) *sk;
|
2014-12-18 03:06:57 +00:00
|
|
|
const SSL_CIPHER *c;
|
2015-12-19 22:05:56 +00:00
|
|
|
CERT *ct = ssl->cert;
|
2016-07-08 22:32:11 +01:00
|
|
|
int al = SSL_AD_INTERNAL_ERROR;
|
2014-12-18 03:06:57 +00:00
|
|
|
CBS server_hello, server_random, session_id;
|
2016-06-21 15:33:21 +01:00
|
|
|
uint16_t server_wire_version, server_version, cipher_suite;
|
2014-12-18 03:06:57 +00:00
|
|
|
uint8_t compression_method;
|
|
|
|
|
2016-07-11 18:19:03 +01:00
|
|
|
int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
|
2016-07-08 22:32:11 +01:00
|
|
|
if (ret <= 0) {
|
2015-01-06 17:03:19 +00:00
|
|
|
uint32_t err = ERR_peek_error();
|
|
|
|
if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
|
|
|
|
ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
|
|
|
|
/* Add a dedicated error code to the queue for a handshake_failure alert
|
|
|
|
* in response to ClientHello. This matches NSS's client behavior and
|
|
|
|
* gives a better error on a (probable) failure to negotiate initial
|
|
|
|
* parameters. Note: this error code comes after the original one.
|
|
|
|
*
|
|
|
|
* See https://crbug.com/446505. */
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
|
2015-01-06 17:03:19 +00:00
|
|
|
}
|
2016-07-08 22:32:11 +01:00
|
|
|
return ret;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 18:19:03 +01:00
|
|
|
if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO &&
|
|
|
|
ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-07-08 22:32:11 +01:00
|
|
|
CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-07-11 18:19:03 +01:00
|
|
|
if (!CBS_get_u16(&server_hello, &server_wire_version)) {
|
2014-12-18 03:06:57 +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-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2016-06-21 15:33:21 +01:00
|
|
|
server_version = ssl->method->version_from_wire(server_wire_version);
|
|
|
|
|
2016-07-21 11:55:28 +01:00
|
|
|
uint16_t min_version, max_version, real_max_version;
|
|
|
|
if (!ssl_get_full_version_range(ssl, &min_version, &max_version,
|
|
|
|
&real_max_version) ||
|
2016-07-10 17:20:35 +01:00
|
|
|
server_version < min_version || server_version > max_version) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
|
|
|
|
al = SSL_AD_PROTOCOL_VERSION;
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
|
|
|
|
if (!ssl->s3->have_version) {
|
2016-06-21 15:33:21 +01:00
|
|
|
ssl->version = server_wire_version;
|
2015-12-30 22:39:26 +00:00
|
|
|
ssl->s3->enc_method = ssl3_get_enc_method(server_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-18 03:06:57 +00:00
|
|
|
* fixed. Begin enforcing the record-layer version. */
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->have_version = 1;
|
2016-06-21 15:33:21 +01:00
|
|
|
} else if (server_wire_version != ssl->version) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
|
2014-12-18 03:06:57 +00:00
|
|
|
al = SSL_AD_PROTOCOL_VERSION;
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2016-07-11 18:19:03 +01:00
|
|
|
if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
|
|
|
|
ssl->state = SSL_ST_TLS13;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
|
|
|
|
!CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
|
|
|
|
CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
|
|
|
|
!CBS_get_u16(&server_hello, &cipher_suite) ||
|
|
|
|
!CBS_get_u8(&server_hello, &compression_method)) {
|
|
|
|
al = SSL_AD_DECODE_ERROR;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
/* Copy over the server random. */
|
2015-12-19 22:05:56 +00:00
|
|
|
memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
|
|
|
|
|
2016-08-12 03:01:18 +01:00
|
|
|
/* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
|
|
|
|
* 1.3 is finalized and we are not implementing a draft version. */
|
2016-07-10 17:20:35 +01:00
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
|
2016-07-29 19:32:55 +01:00
|
|
|
ssl->session->session_id_length != 0 &&
|
2015-12-19 22:05:56 +00:00
|
|
|
CBS_mem_equal(&session_id, ssl->session->session_id,
|
|
|
|
ssl->session->session_id_length)) {
|
2016-07-29 19:32:55 +01:00
|
|
|
ssl->s3->session_reused = 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
} else {
|
|
|
|
/* The session wasn't resumed. Create a fresh SSL_SESSION to
|
|
|
|
* fill out. */
|
2016-09-01 06:10:07 +01:00
|
|
|
ssl_set_session(ssl, NULL);
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!ssl_get_new_session(ssl, 0 /* client */)) {
|
2014-12-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
/* Note: session_id could be empty. */
|
2016-06-27 21:34:59 +01:00
|
|
|
ssl->s3->new_session->session_id_length = CBS_len(&session_id);
|
|
|
|
memcpy(ssl->s3->new_session->session_id, CBS_data(&session_id),
|
2015-12-19 22:05:56 +00:00
|
|
|
CBS_len(&session_id));
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2015-05-30 22:03:14 +01:00
|
|
|
c = SSL_get_cipher_by_value(cipher_suite);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (c == NULL) {
|
|
|
|
/* unknown cipher */
|
|
|
|
al = SSL_AD_ILLEGAL_PARAMETER;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
/* If the cipher is disabled then we didn't sent it in the ClientHello, so if
|
|
|
|
* the server selected it, it's an error. */
|
2015-12-19 22:05:56 +00:00
|
|
|
if ((c->algorithm_mkey & ct->mask_k) || (c->algorithm_auth & ct->mask_a) ||
|
2016-06-15 02:14:35 +01:00
|
|
|
SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
|
|
|
|
SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) {
|
2014-12-18 03:06:57 +00:00
|
|
|
al = SSL_AD_ILLEGAL_PARAMETER;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
sk = ssl_get_ciphers_by_id(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
|
|
|
|
/* we did not say we would use this cipher */
|
|
|
|
al = SSL_AD_ILLEGAL_PARAMETER;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
if (ssl->session != NULL) {
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->session->cipher != c) {
|
2015-03-16 22:02:20 +00:00
|
|
|
al = SSL_AD_ILLEGAL_PARAMETER;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
|
2015-03-16 22:02:20 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->session->ssl_version != ssl->version) {
|
2015-03-16 22:02:20 +00:00
|
|
|
al = SSL_AD_ILLEGAL_PARAMETER;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
|
2015-03-16 22:02:20 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
2016-07-29 19:32:55 +01:00
|
|
|
if (!ssl_session_is_context_valid(ssl, ssl->session)) {
|
|
|
|
/* This is actually a client application bug. */
|
|
|
|
al = SSL_AD_ILLEGAL_PARAMETER;
|
|
|
|
OPENSSL_PUT_ERROR(SSL,
|
|
|
|
SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
|
|
|
|
goto f_err;
|
|
|
|
}
|
2015-12-25 20:40:14 +00:00
|
|
|
} else {
|
2016-06-27 21:34:59 +01:00
|
|
|
ssl->s3->new_session->cipher = c;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->tmp.new_cipher = c;
|
2016-09-06 19:29:47 +01:00
|
|
|
if (ssl_cipher_uses_certificate_auth(c)) {
|
|
|
|
ssl->s3->hs->use_cert_auth = 1;
|
|
|
|
}
|
2014-12-18 03:06:57 +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;
|
|
|
|
}
|
|
|
|
|
2016-06-30 18:27:23 +01:00
|
|
|
/* If doing a full handshake, the server may request a client certificate
|
|
|
|
* which requires hashing the handshake transcript. Otherwise, the handshake
|
|
|
|
* buffer may be released. */
|
2016-09-06 19:29:47 +01:00
|
|
|
if (ssl->session != NULL || !ssl->s3->hs->use_cert_auth) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl3_free_handshake_buffer(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Only the NULL compression algorithm is supported. */
|
|
|
|
if (compression_method != 0) {
|
|
|
|
al = SSL_AD_ILLEGAL_PARAMETER;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TLS extensions */
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!ssl_parse_serverhello_tlsext(ssl, &server_hello)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There should be nothing left over in the record. */
|
|
|
|
if (CBS_len(&server_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-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
if (ssl->session != NULL &&
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->tmp.extended_master_secret !=
|
|
|
|
ssl->session->extended_master_secret) {
|
2015-06-02 18:50:35 +01:00
|
|
|
al = SSL_AD_HANDSHAKE_FAILURE;
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->session->extended_master_secret) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
|
2015-06-02 18:50:35 +01:00
|
|
|
} else {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
|
2015-06-02 18:50:35 +01:00
|
|
|
}
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
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:
|
2014-12-18 03:06:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:40:46 +01:00
|
|
|
static int ssl3_get_server_certificate(SSL *ssl) {
|
2016-07-14 04:03:26 +01:00
|
|
|
int ret =
|
2016-07-08 22:32:11 +01:00
|
|
|
ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE, ssl_hash_message);
|
2016-07-14 04:03:26 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 04:03:26 +01:00
|
|
|
CBS cbs;
|
2016-07-08 22:32:11 +01:00
|
|
|
CBS_init(&cbs, ssl->init_msg, ssl->init_num);
|
2016-07-14 04:03:26 +01:00
|
|
|
uint8_t alert;
|
|
|
|
STACK_OF(X509) *chain = ssl_parse_cert_chain(ssl, &alert, NULL, &cbs);
|
|
|
|
if (chain == NULL) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-07-14 04:03:26 +01:00
|
|
|
if (sk_X509_num(chain) == 0 || CBS_len(&cbs) != 0) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2016-07-14 04:03:26 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 04:03:26 +01:00
|
|
|
X509 *leaf = sk_X509_value(chain, 0);
|
2016-07-15 12:07:40 +01:00
|
|
|
if (!ssl_check_leaf_certificate(ssl, leaf)) {
|
2016-07-14 04:03:26 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
goto err;
|
2015-06-08 00:50:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: Unlike the server half, the client's copy of |cert_chain| includes
|
|
|
|
* the leaf. */
|
2016-06-27 21:34:59 +01:00
|
|
|
sk_X509_pop_free(ssl->s3->new_session->cert_chain, X509_free);
|
|
|
|
ssl->s3->new_session->cert_chain = chain;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
X509_free(ssl->s3->new_session->peer);
|
2016-08-11 17:14:47 +01:00
|
|
|
X509_up_ref(leaf);
|
|
|
|
ssl->s3->new_session->peer = leaf;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-07-14 04:03:26 +01:00
|
|
|
return 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
err:
|
2016-07-14 04:03:26 +01:00
|
|
|
sk_X509_pop_free(chain, X509_free);
|
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
static int ssl3_get_cert_status(SSL *ssl) {
|
2016-07-08 22:32:11 +01:00
|
|
|
int al;
|
2016-06-07 22:03:46 +01:00
|
|
|
CBS certificate_status, ocsp_response;
|
|
|
|
uint8_t status_type;
|
|
|
|
|
2016-07-08 22:32:11 +01:00
|
|
|
int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
|
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
|
|
|
|
/* A server may send status_request in ServerHello and then change
|
|
|
|
* its mind about sending CertificateStatus. */
|
|
|
|
ssl->s3->tmp.reuse_message = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-07-08 22:32:11 +01:00
|
|
|
CBS_init(&certificate_status, ssl->init_msg, ssl->init_num);
|
2016-06-07 22:03:46 +01:00
|
|
|
if (!CBS_get_u8(&certificate_status, &status_type) ||
|
|
|
|
status_type != TLSEXT_STATUSTYPE_ocsp ||
|
|
|
|
!CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
|
|
|
|
CBS_len(&ocsp_response) == 0 ||
|
|
|
|
CBS_len(&certificate_status) != 0) {
|
|
|
|
al = SSL_AD_DECODE_ERROR;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
|
|
|
|
&ssl->s3->new_session->ocsp_response_length)) {
|
2016-06-07 22:03:46 +01:00
|
|
|
al = SSL_AD_INTERNAL_ERROR;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
f_err:
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ssl3_verify_server_cert(SSL *ssl) {
|
2016-08-09 02:38:32 +01:00
|
|
|
if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
|
|
|
|
ssl->s3->new_session->cert_chain)) {
|
2016-08-02 01:11:13 +01:00
|
|
|
return -1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2016-07-18 18:17:16 +01:00
|
|
|
return 1;
|
2016-06-07 22:03:46 +01:00
|
|
|
}
|
|
|
|
|
2016-06-07 21:40:46 +01:00
|
|
|
static int ssl3_get_server_key_exchange(SSL *ssl) {
|
2016-07-08 22:32:11 +01:00
|
|
|
int al;
|
2014-12-18 03:06:57 +00:00
|
|
|
EVP_PKEY *pkey = NULL;
|
|
|
|
DH *dh = NULL;
|
|
|
|
EC_KEY *ecdh = NULL;
|
|
|
|
EC_POINT *srvr_ecpoint = NULL;
|
|
|
|
|
2016-07-08 22:32:11 +01:00
|
|
|
int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
|
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
|
|
|
|
if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
|
2014-12-18 03:06:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-08-22 21:04:17 +01:00
|
|
|
/* In plain PSK ciphersuite, ServerKeyExchange may be omitted to send no
|
|
|
|
* identity hint. */
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
|
2014-12-18 03:06:57 +00:00
|
|
|
/* TODO(davidben): This should be reset in one place with the rest of the
|
|
|
|
* handshake state. */
|
2015-12-19 22:05:56 +00:00
|
|
|
OPENSSL_free(ssl->s3->tmp.peer_psk_identity_hint);
|
|
|
|
ssl->s3->tmp.peer_psk_identity_hint = NULL;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->tmp.reuse_message = 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Retain a copy of the original CBS to compute the signature over. */
|
2015-12-29 22:08:08 +00:00
|
|
|
CBS server_key_exchange;
|
2016-07-08 22:32:11 +01:00
|
|
|
CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num);
|
2015-12-29 22:08:08 +00:00
|
|
|
CBS server_key_exchange_orig = server_key_exchange;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-29 22:08:08 +00:00
|
|
|
uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
|
|
|
|
uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
|
|
|
if (alg_a & SSL_aPSK) {
|
|
|
|
CBS psk_identity_hint;
|
|
|
|
|
|
|
|
/* Each of the PSK key exchanges begins with a psk_identity_hint. */
|
|
|
|
if (!CBS_get_u16_length_prefixed(&server_key_exchange,
|
|
|
|
&psk_identity_hint)) {
|
|
|
|
al = SSL_AD_DECODE_ERROR;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store PSK identity hint for later use, hint is used in
|
|
|
|
* ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
|
|
|
|
* identity hint can be as long as the maximum length of a PSK identity.
|
|
|
|
* Also do not allow NULL characters; identities are saved as C strings.
|
|
|
|
*
|
|
|
|
* TODO(davidben): Should invalid hints be ignored? It's a hint rather than
|
|
|
|
* a specific identity. */
|
|
|
|
if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
|
|
|
|
CBS_contains_zero_byte(&psk_identity_hint)) {
|
|
|
|
al = SSL_AD_HANDSHAKE_FAILURE;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the identity hint as a C string. */
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!CBS_strdup(&psk_identity_hint, &ssl->s3->tmp.peer_psk_identity_hint)) {
|
2014-12-18 03:06:57 +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-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 15:10:48 +00:00
|
|
|
if (alg_k & SSL_kDHE) {
|
2014-12-18 03:06:57 +00:00
|
|
|
CBS dh_p, dh_g, dh_Ys;
|
|
|
|
if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
|
|
|
|
CBS_len(&dh_p) == 0 ||
|
|
|
|
!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
|
|
|
|
CBS_len(&dh_g) == 0 ||
|
|
|
|
!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
|
|
|
|
CBS_len(&dh_Ys) == 0) {
|
|
|
|
al = SSL_AD_DECODE_ERROR;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
dh = DH_new();
|
|
|
|
if (dh == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-12-17 06:33:08 +00:00
|
|
|
dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL);
|
|
|
|
dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL);
|
2015-12-19 21:58:04 +00:00
|
|
|
if (dh->p == NULL || dh->g == NULL) {
|
2014-12-18 03:06:57 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
ssl->s3->new_session->key_exchange_info = DH_num_bits(dh);
|
|
|
|
if (ssl->s3->new_session->key_exchange_info < 1024) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto err;
|
2016-06-27 21:34:59 +01:00
|
|
|
} else if (ssl->s3->new_session->key_exchange_info > 4096) {
|
2015-11-11 21:23:05 +00:00
|
|
|
/* Overly large DHE groups are prohibitively expensive, so enforce a limit
|
|
|
|
* to prevent a server from causing us to perform too expensive of a
|
|
|
|
* computation. */
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-12-19 21:58:04 +00:00
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh);
|
2014-12-18 03:06:57 +00:00
|
|
|
dh = NULL;
|
2015-12-19 21:58:04 +00:00
|
|
|
|
|
|
|
/* Save the peer public key for later. */
|
|
|
|
size_t peer_key_len;
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!CBS_stow(&dh_Ys, &ssl->s3->tmp.peer_key, &peer_key_len)) {
|
2015-12-19 21:58:04 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2016-05-25 20:06:05 +01:00
|
|
|
/* |dh_Ys| was initialized with CBS_get_u16_length_prefixed, so peer_key_len
|
|
|
|
* fits in a uint16_t. */
|
2015-12-19 22:05:56 +00:00
|
|
|
assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
|
|
|
|
ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
|
2015-03-19 15:10:48 +00:00
|
|
|
} else if (alg_k & SSL_kECDHE) {
|
2015-12-19 05:18:25 +00:00
|
|
|
/* Parse the server parameters. */
|
2016-05-17 16:47:53 +01:00
|
|
|
uint8_t group_type;
|
|
|
|
uint16_t group_id;
|
2015-12-17 06:33:08 +00:00
|
|
|
CBS point;
|
2016-05-17 16:47:53 +01:00
|
|
|
if (!CBS_get_u8(&server_key_exchange, &group_type) ||
|
|
|
|
group_type != NAMED_CURVE_TYPE ||
|
2016-05-25 20:06:05 +01:00
|
|
|
!CBS_get_u16(&server_key_exchange, &group_id) ||
|
|
|
|
!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
|
2014-12-18 03:06:57 +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-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
2016-06-27 21:34:59 +01:00
|
|
|
ssl->s3->new_session->key_exchange_info = group_id;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-05-17 16:47:53 +01:00
|
|
|
/* Ensure the group is consistent with preferences. */
|
|
|
|
if (!tls1_check_group_id(ssl, group_id)) {
|
2015-12-19 05:18:25 +00:00
|
|
|
al = SSL_AD_ILLEGAL_PARAMETER;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
2015-12-19 05:18:25 +00:00
|
|
|
|
2016-05-25 20:06:05 +01:00
|
|
|
/* Initialize ECDH and save the peer public key for later. */
|
|
|
|
size_t peer_key_len;
|
|
|
|
if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, group_id) ||
|
|
|
|
!CBS_stow(&point, &ssl->s3->tmp.peer_key, &peer_key_len)) {
|
2016-05-17 00:31:05 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2016-05-25 20:06:05 +01:00
|
|
|
/* |point| was initialized with CBS_get_u8_length_prefixed, so peer_key_len
|
|
|
|
* fits in a uint16_t. */
|
|
|
|
assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
|
|
|
|
ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
|
|
|
|
} else if (alg_k & SSL_kCECPQ1) {
|
2016-06-30 18:46:24 +01:00
|
|
|
SSL_ECDH_CTX_init_for_cecpq1(&ssl->s3->tmp.ecdh_ctx);
|
2016-05-25 20:06:05 +01:00
|
|
|
CBS key;
|
|
|
|
if (!CBS_get_u16_length_prefixed(&server_key_exchange, &key)) {
|
2016-05-17 00:31:05 +01:00
|
|
|
al = SSL_AD_DECODE_ERROR;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2015-12-19 05:18:25 +00:00
|
|
|
size_t peer_key_len;
|
2016-05-25 20:06:05 +01:00
|
|
|
if (!CBS_stow(&key, &ssl->s3->tmp.peer_key, &peer_key_len)) {
|
2015-12-17 06:22:15 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2016-05-25 20:06:05 +01:00
|
|
|
/* |key| was initialized with CBS_get_u16_length_prefixed, so peer_key_len
|
|
|
|
* fits in a uint16_t. */
|
2015-12-19 22:05:56 +00:00
|
|
|
assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
|
|
|
|
ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
|
2014-12-18 03:06:57 +00:00
|
|
|
} else if (!(alg_k & SSL_kPSK)) {
|
|
|
|
al = SSL_AD_UNEXPECTED_MESSAGE;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* At this point, |server_key_exchange| contains the signature, if any, while
|
|
|
|
* |server_key_exchange_orig| contains the entire message. From that, derive
|
|
|
|
* a CBS containing just the parameter. */
|
2015-12-29 22:08:08 +00:00
|
|
|
CBS parameter;
|
2014-12-18 03:06:57 +00:00
|
|
|
CBS_init(¶meter, CBS_data(&server_key_exchange_orig),
|
|
|
|
CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
|
|
|
|
|
2015-06-07 16:00:53 +01:00
|
|
|
/* ServerKeyExchange should be signed by the server's public key. */
|
2016-09-06 19:29:47 +01:00
|
|
|
if (ssl->s3->hs->use_cert_auth) {
|
2016-06-27 21:34:59 +01:00
|
|
|
pkey = X509_get_pubkey(ssl->s3->new_session->peer);
|
2015-06-07 16:00:53 +01:00
|
|
|
if (pkey == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-29 18:16:27 +01:00
|
|
|
uint16_t signature_algorithm = 0;
|
2015-12-30 05:08:49 +00:00
|
|
|
if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
|
2016-06-23 18:33:05 +01:00
|
|
|
if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
|
2015-11-02 17:02:20 +00:00
|
|
|
al = SSL_AD_DECODE_ERROR;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
2016-07-09 00:15:32 +01:00
|
|
|
if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
|
2015-11-02 17:02:20 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
2016-06-23 18:33:05 +01:00
|
|
|
ssl->s3->tmp.peer_signature_algorithm = signature_algorithm;
|
2014-12-18 03:06:57 +00:00
|
|
|
} else if (pkey->type == EVP_PKEY_RSA) {
|
2016-06-29 18:16:27 +01:00
|
|
|
signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
|
|
|
|
} else if (pkey->type == EVP_PKEY_EC) {
|
|
|
|
signature_algorithm = SSL_SIGN_ECDSA_SHA1;
|
2016-07-14 05:11:26 +01:00
|
|
|
} else {
|
|
|
|
al = SSL_AD_UNSUPPORTED_CERTIFICATE;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
|
|
|
|
goto f_err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The last field in |server_key_exchange| is the signature. */
|
2015-06-07 16:00:53 +01:00
|
|
|
CBS signature;
|
2014-12-18 03:06:57 +00:00
|
|
|
if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
|
|
|
|
CBS_len(&server_key_exchange) != 0) {
|
|
|
|
al = SSL_AD_DECODE_ERROR;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
|
2016-06-30 18:27:23 +01:00
|
|
|
CBB transcript;
|
|
|
|
uint8_t *transcript_data;
|
|
|
|
size_t transcript_len;
|
|
|
|
if (!CBB_init(&transcript, 2*SSL3_RANDOM_SIZE + CBS_len(¶meter)) ||
|
|
|
|
!CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
|
|
|
|
!CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
|
|
|
|
!CBB_add_bytes(&transcript, CBS_data(¶meter), CBS_len(¶meter)) ||
|
|
|
|
!CBB_finish(&transcript, &transcript_data, &transcript_len)) {
|
|
|
|
CBB_cleanup(&transcript);
|
|
|
|
al = SSL_AD_INTERNAL_ERROR;
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2016-06-29 18:16:27 +01:00
|
|
|
goto f_err;
|
|
|
|
}
|
2016-06-30 18:27:23 +01:00
|
|
|
|
|
|
|
int sig_ok = ssl_public_key_verify(
|
|
|
|
ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
|
|
|
|
pkey, transcript_data, transcript_len);
|
|
|
|
OPENSSL_free(transcript_data);
|
|
|
|
|
2016-03-02 03:57:40 +00:00
|
|
|
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
|
|
|
|
sig_ok = 1;
|
|
|
|
ERR_clear_error();
|
|
|
|
#endif
|
|
|
|
if (!sig_ok) {
|
2014-12-18 03:06:57 +00:00
|
|
|
/* bad signature */
|
|
|
|
al = SSL_AD_DECRYPT_ERROR;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
} else {
|
2015-06-07 16:00:53 +01:00
|
|
|
/* PSK ciphers are the only supported certificate-less ciphers. */
|
|
|
|
assert(alg_a == SSL_aPSK);
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
if (CBS_len(&server_key_exchange) > 0) {
|
|
|
|
al = SSL_AD_DECODE_ERROR;
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto f_err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
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:
|
2014-12-18 03:06:57 +00:00
|
|
|
EVP_PKEY_free(pkey);
|
2015-04-22 21:17:58 +01:00
|
|
|
DH_free(dh);
|
2014-12-18 03:06:57 +00:00
|
|
|
EC_POINT_free(srvr_ecpoint);
|
2015-04-22 21:17:58 +01:00
|
|
|
EC_KEY_free(ecdh);
|
2014-12-18 03:06:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:40:46 +01:00
|
|
|
static int ssl3_get_certificate_request(SSL *ssl) {
|
2016-07-08 22:32:11 +01:00
|
|
|
int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
|
|
|
|
if (msg_ret <= 0) {
|
|
|
|
return msg_ret;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-06-13 19:05:19 +01:00
|
|
|
ssl->s3->tmp.cert_request = 0;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-07 22:13:28 +01:00
|
|
|
if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl->s3->tmp.reuse_message = 1;
|
2015-08-05 13:50:34 +01:00
|
|
|
/* If we get here we don't need the handshake buffer as we won't be doing
|
|
|
|
* client auth. */
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl3_free_handshake_buffer(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
|
2016-03-10 21:11:51 +00:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
|
2016-07-14 03:40:36 +01:00
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-03-12 00:38:17 +00:00
|
|
|
CBS cbs;
|
2016-07-08 22:32:11 +01:00
|
|
|
CBS_init(&cbs, ssl->init_msg, ssl->init_num);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-07-14 03:40:36 +01:00
|
|
|
/* Get the certificate types. */
|
2016-03-12 00:38:17 +00:00
|
|
|
CBS certificate_types;
|
2014-12-18 03:06:57 +00:00
|
|
|
if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
|
2015-12-19 22:05:56 +00:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2016-07-14 03:40:36 +01:00
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 22:05:56 +00:00
|
|
|
if (!CBS_stow(&certificate_types, &ssl->s3->tmp.certificate_types,
|
|
|
|
&ssl->s3->tmp.num_certificate_types)) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
2016-07-14 03:40:36 +01:00
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 05:08:49 +00:00
|
|
|
if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
|
2014-12-18 03:06:57 +00:00
|
|
|
CBS supported_signature_algorithms;
|
2015-09-04 17:41:04 +01:00
|
|
|
if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
|
2015-12-19 22:05:56 +00:00
|
|
|
!tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2016-07-14 03:40:36 +01:00
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:40:36 +01:00
|
|
|
uint8_t alert;
|
|
|
|
STACK_OF(X509_NAME) *ca_sk = ssl_parse_client_CA_list(ssl, &alert, &cbs);
|
|
|
|
if (ca_sk == NULL) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
|
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-06-13 19:05:19 +01:00
|
|
|
ssl->s3->tmp.cert_request = 1;
|
2015-12-19 22:05:56 +00:00
|
|
|
sk_X509_NAME_pop_free(ssl->s3->tmp.ca_names, X509_NAME_free);
|
|
|
|
ssl->s3->tmp.ca_names = ca_sk;
|
2016-07-14 03:40:36 +01:00
|
|
|
return 1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:13:28 +01:00
|
|
|
static int ssl3_get_server_hello_done(SSL *ssl) {
|
2016-07-08 22:32:11 +01:00
|
|
|
int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_SERVER_HELLO_DONE,
|
|
|
|
ssl_hash_message);
|
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-07-08 22:32:11 +01:00
|
|
|
/* ServerHelloDone is empty. */
|
|
|
|
if (ssl->init_num > 0) {
|
2016-06-07 22:03:46 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
|
|
|
|
return -1;
|
2015-10-23 22:41:12 +01:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2015-06-16 00:53:32 +01:00
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
static int ssl3_send_client_certificate(SSL *ssl) {
|
|
|
|
if (ssl->state == SSL3_ST_CW_CERT_A) {
|
|
|
|
/* Call cert_cb to update the certificate. */
|
|
|
|
if (ssl->cert->cert_cb) {
|
|
|
|
int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
|
|
|
|
if (ret < 0) {
|
|
|
|
ssl->rwstate = SSL_X509_LOOKUP;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ret == 0) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-07-20 09:11:04 +01:00
|
|
|
ssl->state = SSL3_ST_CW_CERT_B;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
if (ssl->state == SSL3_ST_CW_CERT_B) {
|
|
|
|
/* Call client_cert_cb to update the certificate. */
|
2016-07-20 09:11:04 +01:00
|
|
|
int should_retry;
|
|
|
|
if (!ssl_do_client_cert_cb(ssl, &should_retry)) {
|
|
|
|
if (should_retry) {
|
|
|
|
ssl->rwstate = SSL_X509_LOOKUP;
|
|
|
|
}
|
2016-06-07 22:03:46 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-07-14 03:03:11 +01:00
|
|
|
if (!ssl_has_certificate(ssl)) {
|
2016-06-13 19:05:19 +01:00
|
|
|
ssl->s3->tmp.cert_request = 0;
|
2016-06-07 22:03:46 +01:00
|
|
|
/* Without a client certificate, the handshake buffer may be released. */
|
|
|
|
ssl3_free_handshake_buffer(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
if (ssl->version == SSL3_VERSION) {
|
|
|
|
/* In SSL 3.0, send no certificate by skipping both messages. */
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-07-14 03:03:11 +01:00
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-07-14 03:03:11 +01:00
|
|
|
if (!ssl3_output_cert_chain(ssl)) {
|
2016-06-07 22:03:46 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2016-07-20 09:11:04 +01:00
|
|
|
ssl->state = SSL3_ST_CW_CERT_C;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 09:11:04 +01:00
|
|
|
assert(ssl->state == SSL3_ST_CW_CERT_C);
|
2016-06-17 23:48:29 +01:00
|
|
|
return ssl->method->write_message(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2015-10-09 04:10:15 +01:00
|
|
|
OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned),
|
|
|
|
SIZE_T_IS_SMALLER_THAN_UNSIGNED);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-07 21:40:46 +01:00
|
|
|
static int ssl3_send_client_key_exchange(SSL *ssl) {
|
2015-12-17 07:01:25 +00:00
|
|
|
if (ssl->state == SSL3_ST_CW_KEY_EXCH_B) {
|
2016-06-17 23:48:29 +01:00
|
|
|
return ssl->method->write_message(ssl);
|
2015-12-17 07:01:25 +00:00
|
|
|
}
|
|
|
|
assert(ssl->state == SSL3_ST_CW_KEY_EXCH_A);
|
|
|
|
|
2014-12-18 03:06:57 +00:00
|
|
|
uint8_t *pms = NULL;
|
|
|
|
size_t pms_len = 0;
|
2016-06-17 23:48:29 +01:00
|
|
|
CBB cbb, body;
|
|
|
|
if (!ssl->method->init_message(ssl, &cbb, &body,
|
|
|
|
SSL3_MT_CLIENT_KEY_EXCHANGE)) {
|
2015-12-17 07:01:25 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-17 07:01:25 +00:00
|
|
|
uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
|
|
|
|
uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-17 07:01:25 +00:00
|
|
|
/* If using a PSK key exchange, prepare the pre-shared key. */
|
|
|
|
unsigned psk_len = 0;
|
|
|
|
uint8_t psk[PSK_MAX_PSK_LEN];
|
|
|
|
if (alg_a & SSL_aPSK) {
|
|
|
|
if (ssl->psk_client_callback == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-17 07:01:25 +00:00
|
|
|
char identity[PSK_MAX_IDENTITY_LEN + 1];
|
|
|
|
memset(identity, 0, sizeof(identity));
|
|
|
|
psk_len = ssl->psk_client_callback(
|
|
|
|
ssl, ssl->s3->tmp.peer_psk_identity_hint, identity, sizeof(identity),
|
|
|
|
psk, sizeof(psk));
|
|
|
|
if (psk_len == 0) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
|
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-17 07:01:25 +00:00
|
|
|
assert(psk_len <= PSK_MAX_PSK_LEN);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
OPENSSL_free(ssl->s3->new_session->psk_identity);
|
|
|
|
ssl->s3->new_session->psk_identity = BUF_strdup(identity);
|
|
|
|
if (ssl->s3->new_session->psk_identity == NULL) {
|
2015-12-17 07:01:25 +00:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-17 07:01:25 +00:00
|
|
|
/* Write out psk_identity. */
|
|
|
|
CBB child;
|
2016-06-17 23:48:29 +01:00
|
|
|
if (!CBB_add_u16_length_prefixed(&body, &child) ||
|
2015-12-17 07:01:25 +00:00
|
|
|
!CBB_add_bytes(&child, (const uint8_t *)identity,
|
|
|
|
OPENSSL_strnlen(identity, sizeof(identity))) ||
|
2016-06-17 23:48:29 +01:00
|
|
|
!CBB_flush(&body)) {
|
2015-12-17 07:01:25 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-17 07:01:25 +00:00
|
|
|
/* Depending on the key exchange method, compute |pms| and |pms_len|. */
|
|
|
|
if (alg_k & SSL_kRSA) {
|
|
|
|
pms_len = SSL_MAX_MASTER_KEY_LENGTH;
|
|
|
|
pms = OPENSSL_malloc(pms_len);
|
|
|
|
if (pms == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-11-20 22:47:25 +00:00
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
EVP_PKEY *pkey = X509_get_pubkey(ssl->s3->new_session->peer);
|
2015-12-17 07:01:25 +00:00
|
|
|
if (pkey == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-17 07:01:25 +00:00
|
|
|
RSA *rsa = EVP_PKEY_get0_RSA(pkey);
|
|
|
|
if (rsa == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2014-12-18 03:06:57 +00:00
|
|
|
EVP_PKEY_free(pkey);
|
2015-12-17 07:01:25 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-17 07:01:25 +00:00
|
|
|
EVP_PKEY_free(pkey);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-17 07:01:25 +00:00
|
|
|
pms[0] = ssl->client_version >> 8;
|
|
|
|
pms[1] = ssl->client_version & 0xff;
|
|
|
|
if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-17 23:48:29 +01:00
|
|
|
CBB child, *enc_pms = &body;
|
2015-12-17 07:01:25 +00:00
|
|
|
size_t enc_pms_len;
|
|
|
|
/* In TLS, there is a length prefix. */
|
|
|
|
if (ssl->version > SSL3_VERSION) {
|
2016-06-17 23:48:29 +01:00
|
|
|
if (!CBB_add_u16_length_prefixed(&body, &child)) {
|
2014-12-18 03:06:57 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2015-12-17 07:01:25 +00:00
|
|
|
enc_pms = &child;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-17 07:01:25 +00:00
|
|
|
uint8_t *ptr;
|
|
|
|
if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
|
|
|
|
!RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
|
|
|
|
RSA_PKCS1_PADDING) ||
|
|
|
|
/* Log the premaster secret, if logging is enabled. */
|
|
|
|
!ssl_log_rsa_client_key_exchange(ssl, ptr, enc_pms_len, pms, pms_len) ||
|
|
|
|
!CBB_did_write(enc_pms, enc_pms_len) ||
|
2016-06-17 23:48:29 +01:00
|
|
|
!CBB_flush(&body)) {
|
2015-12-17 07:01:25 +00:00
|
|
|
goto err;
|
|
|
|
}
|
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
|
|
|
/* Generate a keypair and serialize the public half. */
|
2015-12-17 07:01:25 +00:00
|
|
|
CBB child;
|
2016-06-17 23:48:29 +01:00
|
|
|
if (!SSL_ECDH_CTX_add_key(&ssl->s3->tmp.ecdh_ctx, &body, &child)) {
|
2015-12-17 07:01:25 +00:00
|
|
|
goto 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_accept(&ssl->s3->tmp.ecdh_ctx, &child, &pms, &pms_len,
|
|
|
|
&alert, ssl->s3->tmp.peer_key,
|
|
|
|
ssl->s3->tmp.peer_key_len)) {
|
2015-12-19 05:18:25 +00:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
|
2015-12-17 07:01:25 +00:00
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2016-06-17 23:48:29 +01:00
|
|
|
if (!CBB_flush(&body)) {
|
2016-05-16 22:27:14 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-19 05:18:25 +00:00
|
|
|
/* The key exchange state may now be discarded. */
|
|
|
|
SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx);
|
|
|
|
OPENSSL_free(ssl->s3->tmp.peer_key);
|
|
|
|
ssl->s3->tmp.peer_key = NULL;
|
2015-12-17 07:01:25 +00:00
|
|
|
} else if (alg_k & SSL_kPSK) {
|
|
|
|
/* For plain PSK, other_secret is a block of 0s with the same length as
|
|
|
|
* the pre-shared key. */
|
|
|
|
pms_len = psk_len;
|
|
|
|
pms = OPENSSL_malloc(pms_len);
|
|
|
|
if (pms == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
2015-02-17 00:33:53 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2015-12-17 07:01:25 +00:00
|
|
|
memset(pms, 0, pms_len);
|
|
|
|
} else {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2015-12-17 07:01:25 +00:00
|
|
|
/* For a PSK cipher suite, other_secret is combined with the pre-shared
|
|
|
|
* key. */
|
|
|
|
if (alg_a & SSL_aPSK) {
|
|
|
|
CBB pms_cbb, child;
|
|
|
|
uint8_t *new_pms;
|
|
|
|
size_t new_pms_len;
|
|
|
|
|
|
|
|
CBB_zero(&pms_cbb);
|
|
|
|
if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) ||
|
|
|
|
!CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
|
|
|
|
!CBB_add_bytes(&child, pms, pms_len) ||
|
|
|
|
!CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
|
|
|
|
!CBB_add_bytes(&child, psk, psk_len) ||
|
|
|
|
!CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) {
|
|
|
|
CBB_cleanup(&pms_cbb);
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
OPENSSL_cleanse(pms, pms_len);
|
|
|
|
OPENSSL_free(pms);
|
2015-12-17 07:01:25 +00:00
|
|
|
pms = new_pms;
|
|
|
|
pms_len = new_pms_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The message must be added to the finished hash before calculating the
|
|
|
|
* master secret. */
|
2016-06-17 23:48:29 +01:00
|
|
|
if (!ssl->method->finish_message(ssl, &cbb)) {
|
2015-12-17 07:01:25 +00:00
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-17 07:01:25 +00:00
|
|
|
ssl->state = SSL3_ST_CW_KEY_EXCH_B;
|
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
ssl->s3->new_session->master_key_length =
|
|
|
|
tls1_generate_master_secret(ssl, ssl->s3->new_session->master_key, pms,
|
|
|
|
pms_len);
|
|
|
|
if (ssl->s3->new_session->master_key_length == 0) {
|
2015-12-17 07:01:25 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2016-06-27 21:34:59 +01:00
|
|
|
ssl->s3->new_session->extended_master_secret =
|
|
|
|
ssl->s3->tmp.extended_master_secret;
|
2015-12-17 07:01:25 +00:00
|
|
|
OPENSSL_cleanse(pms, pms_len);
|
|
|
|
OPENSSL_free(pms);
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-17 23:48:29 +01:00
|
|
|
return ssl->method->write_message(ssl);
|
2014-10-11 00:23:43 +01:00
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
err:
|
2016-03-02 05:23:57 +00:00
|
|
|
CBB_cleanup(&cbb);
|
2015-12-17 07:01:25 +00:00
|
|
|
if (pms != NULL) {
|
2014-12-18 03:06:57 +00:00
|
|
|
OPENSSL_cleanse(pms, pms_len);
|
|
|
|
OPENSSL_free(pms);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:40:46 +01:00
|
|
|
static int ssl3_send_cert_verify(SSL *ssl) {
|
2015-12-18 06:57:43 +00:00
|
|
|
if (ssl->state == SSL3_ST_CW_CERT_VRFY_C) {
|
2016-06-17 23:48:29 +01:00
|
|
|
return ssl->method->write_message(ssl);
|
2015-12-18 06:57:43 +00:00
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-17 22:33:30 +01:00
|
|
|
assert(ssl_has_private_key(ssl));
|
|
|
|
|
2016-06-17 23:48:29 +01:00
|
|
|
CBB cbb, body, child;
|
|
|
|
if (!ssl->method->init_message(ssl, &cbb, &body,
|
|
|
|
SSL3_MT_CERTIFICATE_VERIFY)) {
|
2015-12-18 06:57:43 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-07-08 23:52:59 +01:00
|
|
|
uint16_t signature_algorithm;
|
|
|
|
if (!tls1_choose_signature_algorithm(ssl, &signature_algorithm)) {
|
|
|
|
goto err;
|
|
|
|
}
|
2016-06-17 22:33:30 +01:00
|
|
|
if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
|
2016-06-29 18:16:27 +01:00
|
|
|
/* Write out the digest type in TLS 1.2. */
|
|
|
|
if (!CBB_add_u16(&body, signature_algorithm)) {
|
2016-06-17 22:33:30 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2014-12-18 03:06:57 +00:00
|
|
|
|
2016-06-17 22:33:30 +01:00
|
|
|
/* Set aside space for the signature. */
|
2015-12-18 06:57:43 +00:00
|
|
|
const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
|
2016-06-17 22:33:30 +01:00
|
|
|
uint8_t *ptr;
|
2016-06-17 23:48:29 +01:00
|
|
|
if (!CBB_add_u16_length_prefixed(&body, &child) ||
|
2016-06-17 22:33:30 +01:00
|
|
|
!CBB_reserve(&child, &ptr, max_sig_len)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-06-30 18:27:23 +01:00
|
|
|
size_t sig_len = max_sig_len;
|
2015-12-18 06:57:43 +00:00
|
|
|
enum ssl_private_key_result_t sign_result;
|
|
|
|
if (ssl->state == SSL3_ST_CW_CERT_VRFY_A) {
|
2016-06-30 18:27:23 +01:00
|
|
|
/* The SSL3 construction for CertificateVerify does not decompose into a
|
|
|
|
* single final digest and signature, and must be special-cased. */
|
|
|
|
if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
|
|
|
|
if (ssl->cert->key_method != NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-07-08 22:44:56 +01:00
|
|
|
const EVP_MD *md;
|
2016-06-30 18:27:23 +01:00
|
|
|
uint8_t digest[EVP_MAX_MD_SIZE];
|
|
|
|
size_t digest_len;
|
2016-07-08 22:44:56 +01:00
|
|
|
if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
|
2016-06-30 18:27:23 +01:00
|
|
|
signature_algorithm)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
sign_result = ssl_private_key_success;
|
|
|
|
|
|
|
|
EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
|
|
|
|
if (pctx == NULL ||
|
|
|
|
!EVP_PKEY_sign_init(pctx) ||
|
2016-07-08 22:44:56 +01:00
|
|
|
!EVP_PKEY_CTX_set_signature_md(pctx, md) ||
|
2016-06-30 18:27:23 +01:00
|
|
|
!EVP_PKEY_sign(pctx, ptr, &sig_len, digest, digest_len)) {
|
|
|
|
EVP_PKEY_CTX_free(pctx);
|
|
|
|
sign_result = ssl_private_key_failure;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
EVP_PKEY_CTX_free(pctx);
|
|
|
|
} else {
|
|
|
|
sign_result = ssl_private_key_sign(
|
|
|
|
ssl, ptr, &sig_len, max_sig_len, signature_algorithm,
|
|
|
|
(const uint8_t *)ssl->s3->handshake_buffer->data,
|
|
|
|
ssl->s3->handshake_buffer->length);
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-12-18 06:57:43 +00:00
|
|
|
|
|
|
|
/* The handshake buffer is no longer necessary. */
|
|
|
|
ssl3_free_handshake_buffer(ssl);
|
|
|
|
} else {
|
|
|
|
assert(ssl->state == SSL3_ST_CW_CERT_VRFY_B);
|
2016-07-14 19:52:41 +01:00
|
|
|
sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
|
2015-12-18 06:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (sign_result) {
|
|
|
|
case ssl_private_key_success:
|
|
|
|
break;
|
|
|
|
case ssl_private_key_failure:
|
|
|
|
goto err;
|
|
|
|
case ssl_private_key_retry:
|
|
|
|
ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
|
|
|
|
ssl->state = SSL3_ST_CW_CERT_VRFY_B;
|
|
|
|
goto err;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 06:57:43 +00:00
|
|
|
if (!CBB_did_write(&child, sig_len) ||
|
2016-06-17 23:48:29 +01:00
|
|
|
!ssl->method->finish_message(ssl, &cbb)) {
|
2015-12-18 06:57:43 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->state = SSL3_ST_CW_CERT_VRFY_C;
|
2016-06-17 23:48:29 +01:00
|
|
|
return ssl->method->write_message(ssl);
|
2015-12-18 06:57:43 +00:00
|
|
|
|
|
|
|
err:
|
|
|
|
CBB_cleanup(&cbb);
|
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-06-07 21:40:46 +01:00
|
|
|
static int ssl3_send_next_proto(SSL *ssl) {
|
2015-10-10 19:59:09 +01:00
|
|
|
if (ssl->state == SSL3_ST_CW_NEXT_PROTO_B) {
|
2016-06-17 23:48:29 +01:00
|
|
|
return ssl->method->write_message(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2015-10-10 19:59:09 +01:00
|
|
|
assert(ssl->state == SSL3_ST_CW_NEXT_PROTO_A);
|
|
|
|
|
|
|
|
static const uint8_t kZero[32] = {0};
|
2015-12-25 20:56:49 +00:00
|
|
|
size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
|
2015-10-10 19:59:09 +01:00
|
|
|
|
2016-06-17 23:48:29 +01:00
|
|
|
CBB cbb, body, child;
|
|
|
|
if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) ||
|
|
|
|
!CBB_add_u8_length_prefixed(&body, &child) ||
|
2015-12-25 20:56:49 +00:00
|
|
|
!CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
|
|
|
|
ssl->s3->next_proto_negotiated_len) ||
|
2016-06-17 23:48:29 +01:00
|
|
|
!CBB_add_u8_length_prefixed(&body, &child) ||
|
2015-10-10 19:59:09 +01:00
|
|
|
!CBB_add_bytes(&child, kZero, padding_len) ||
|
2016-06-17 23:48:29 +01:00
|
|
|
!ssl->method->finish_message(ssl, &cbb)) {
|
2015-10-10 19:59:09 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
CBB_cleanup(&cbb);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->state = SSL3_ST_CW_NEXT_PROTO_B;
|
2016-06-17 23:48:29 +01:00
|
|
|
return ssl->method->write_message(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 21:40:46 +01:00
|
|
|
static int ssl3_send_channel_id(SSL *ssl) {
|
2015-10-10 21:30:22 +01:00
|
|
|
if (ssl->state == SSL3_ST_CW_CHANNEL_ID_B) {
|
2016-06-17 23:48:29 +01:00
|
|
|
return ssl->method->write_message(ssl);
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2015-10-10 21:30:22 +01:00
|
|
|
assert(ssl->state == SSL3_ST_CW_CHANNEL_ID_A);
|
|
|
|
|
|
|
|
if (ssl->tlsext_channel_id_private == NULL &&
|
|
|
|
ssl->ctx->channel_id_cb != NULL) {
|
2014-12-18 03:06:57 +00:00
|
|
|
EVP_PKEY *key = NULL;
|
2015-10-10 21:30:22 +01:00
|
|
|
ssl->ctx->channel_id_cb(ssl, &key);
|
|
|
|
if (key != NULL &&
|
|
|
|
!SSL_set1_tls_channel_id(ssl, key)) {
|
|
|
|
EVP_PKEY_free(key);
|
|
|
|
return -1;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2015-10-10 21:30:22 +01:00
|
|
|
EVP_PKEY_free(key);
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
|
|
|
|
2015-10-10 21:30:22 +01:00
|
|
|
if (ssl->tlsext_channel_id_private == NULL) {
|
|
|
|
ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
|
2014-12-18 03:06:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-11-20 22:47:25 +00:00
|
|
|
EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
|
|
|
|
if (ec_key == NULL) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2015-06-12 06:40:23 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-10 21:30:22 +01:00
|
|
|
int ret = -1;
|
|
|
|
BIGNUM *x = BN_new();
|
|
|
|
BIGNUM *y = BN_new();
|
|
|
|
ECDSA_SIG *sig = NULL;
|
|
|
|
if (x == NULL || y == NULL ||
|
|
|
|
!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
|
|
|
|
EC_KEY_get0_public_key(ec_key),
|
|
|
|
x, y, NULL)) {
|
2014-12-18 03:06:57 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-06-12 06:40:23 +01:00
|
|
|
uint8_t digest[EVP_MAX_MD_SIZE];
|
2015-08-06 16:10:51 +01:00
|
|
|
size_t digest_len;
|
2015-10-10 21:30:22 +01:00
|
|
|
if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
|
2014-12-18 03:06:57 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-06-12 06:40:23 +01:00
|
|
|
sig = ECDSA_do_sign(digest, digest_len, ec_key);
|
2014-12-18 03:06:57 +00:00
|
|
|
if (sig == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-06-17 23:48:29 +01:00
|
|
|
CBB cbb, body, child;
|
2016-07-06 21:50:14 +01:00
|
|
|
if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
|
2016-06-17 23:48:29 +01:00
|
|
|
!CBB_add_u16(&body, TLSEXT_TYPE_channel_id) ||
|
|
|
|
!CBB_add_u16_length_prefixed(&body, &child) ||
|
|
|
|
!BN_bn2cbb_padded(&child, 32, x) || !BN_bn2cbb_padded(&child, 32, y) ||
|
2015-12-17 07:01:25 +00:00
|
|
|
!BN_bn2cbb_padded(&child, 32, sig->r) ||
|
|
|
|
!BN_bn2cbb_padded(&child, 32, sig->s) ||
|
2016-06-17 23:48:29 +01:00
|
|
|
!ssl->method->finish_message(ssl, &cbb)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2015-10-10 21:30:22 +01:00
|
|
|
CBB_cleanup(&cbb);
|
2014-12-18 03:06:57 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-10-10 21:30:22 +01:00
|
|
|
ssl->state = SSL3_ST_CW_CHANNEL_ID_B;
|
2016-06-17 23:48:29 +01:00
|
|
|
ret = ssl->method->write_message(ssl);
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
err:
|
2015-10-10 21:30:22 +01:00
|
|
|
BN_free(x);
|
|
|
|
BN_free(y);
|
2015-04-22 21:17:58 +01:00
|
|
|
ECDSA_SIG_free(sig);
|
2014-12-18 03:06:57 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
static int ssl3_get_new_session_ticket(SSL *ssl) {
|
2016-07-08 22:32:11 +01:00
|
|
|
int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_NEW_SESSION_TICKET,
|
|
|
|
ssl_hash_message);
|
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
2014-12-18 03:06:57 +00:00
|
|
|
}
|
2016-03-10 13:43:27 +00:00
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
CBS new_session_ticket, ticket;
|
2016-08-09 18:18:40 +01:00
|
|
|
uint32_t tlsext_tick_lifetime_hint;
|
2016-07-08 22:32:11 +01:00
|
|
|
CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num);
|
2016-08-09 18:18:40 +01:00
|
|
|
if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
|
2016-06-07 22:03:46 +01:00
|
|
|
!CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
|
|
|
|
CBS_len(&new_session_ticket) != 0) {
|
2016-06-27 21:34:59 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2016-06-07 22:03:46 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2016-06-27 21:34:59 +01:00
|
|
|
return -1;
|
2016-03-10 13:43:27 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
if (CBS_len(&ticket) == 0) {
|
|
|
|
/* RFC 5077 allows a server to change its mind and send no ticket after
|
|
|
|
* negotiating the extension. The value of |tlsext_ticket_expected| is
|
|
|
|
* checked in |ssl_update_cache| so is cleared here to avoid an unnecessary
|
|
|
|
* update. */
|
|
|
|
ssl->tlsext_ticket_expected = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
2015-08-18 12:21:54 +01:00
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
int session_renewed = ssl->session != NULL;
|
|
|
|
SSL_SESSION *session = ssl->s3->new_session;
|
|
|
|
if (session_renewed) {
|
2016-06-07 22:03:46 +01:00
|
|
|
/* The server is sending a new ticket for an existing session. Sessions are
|
|
|
|
* immutable once established, so duplicate all but the ticket of the
|
|
|
|
* existing session. */
|
2016-07-29 19:32:55 +01:00
|
|
|
session = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
|
2016-06-27 21:34:59 +01:00
|
|
|
if (session == NULL) {
|
2016-06-07 22:03:46 +01:00
|
|
|
/* This should never happen. */
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-08-18 12:21:54 +01:00
|
|
|
}
|
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
|
2016-06-07 22:03:46 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2016-08-09 18:18:40 +01:00
|
|
|
session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
|
2016-06-07 22:03:46 +01:00
|
|
|
|
|
|
|
/* Generate a session ID for this session based on the session ticket. We use
|
|
|
|
* the session ID mechanism for detecting ticket resumption. This also fits in
|
|
|
|
* with assumptions elsewhere in OpenSSL.*/
|
2016-06-27 21:34:59 +01:00
|
|
|
if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
|
|
|
|
session->session_id, &session->session_id_length,
|
|
|
|
EVP_sha256(), NULL)) {
|
2016-06-07 22:03:46 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-06-27 21:34:59 +01:00
|
|
|
if (session_renewed) {
|
|
|
|
session->not_resumable = 0;
|
|
|
|
SSL_SESSION_free(ssl->session);
|
|
|
|
ssl->session = session;
|
|
|
|
}
|
|
|
|
|
2016-06-07 22:03:46 +01:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
err:
|
2016-06-27 21:34:59 +01:00
|
|
|
if (session_renewed) {
|
|
|
|
SSL_SESSION_free(session);
|
|
|
|
}
|
2016-06-07 22:03:46 +01:00
|
|
|
return -1;
|
2015-08-18 12:21:54 +01:00
|
|
|
}
|