1*f4fe6251Sjsing /* $OpenBSD: ssl_srvr.c,v 1.165 2024/07/22 14:47:15 jsing Exp $ */ 23395f70eSjsing /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 33395f70eSjsing * All rights reserved. 43395f70eSjsing * 53395f70eSjsing * This package is an SSL implementation written 63395f70eSjsing * by Eric Young (eay@cryptsoft.com). 73395f70eSjsing * The implementation was written so as to conform with Netscapes SSL. 83395f70eSjsing * 93395f70eSjsing * This library is free for commercial and non-commercial use as long as 103395f70eSjsing * the following conditions are aheared to. The following conditions 113395f70eSjsing * apply to all code found in this distribution, be it the RC4, RSA, 123395f70eSjsing * lhash, DES, etc., code; not just the SSL code. The SSL documentation 133395f70eSjsing * included with this distribution is covered by the same copyright terms 143395f70eSjsing * except that the holder is Tim Hudson (tjh@cryptsoft.com). 153395f70eSjsing * 163395f70eSjsing * Copyright remains Eric Young's, and as such any Copyright notices in 173395f70eSjsing * the code are not to be removed. 183395f70eSjsing * If this package is used in a product, Eric Young should be given attribution 193395f70eSjsing * as the author of the parts of the library used. 203395f70eSjsing * This can be in the form of a textual message at program startup or 213395f70eSjsing * in documentation (online or textual) provided with the package. 223395f70eSjsing * 233395f70eSjsing * Redistribution and use in source and binary forms, with or without 243395f70eSjsing * modification, are permitted provided that the following conditions 253395f70eSjsing * are met: 263395f70eSjsing * 1. Redistributions of source code must retain the copyright 273395f70eSjsing * notice, this list of conditions and the following disclaimer. 283395f70eSjsing * 2. Redistributions in binary form must reproduce the above copyright 293395f70eSjsing * notice, this list of conditions and the following disclaimer in the 303395f70eSjsing * documentation and/or other materials provided with the distribution. 313395f70eSjsing * 3. All advertising materials mentioning features or use of this software 323395f70eSjsing * must display the following acknowledgement: 333395f70eSjsing * "This product includes cryptographic software written by 343395f70eSjsing * Eric Young (eay@cryptsoft.com)" 353395f70eSjsing * The word 'cryptographic' can be left out if the rouines from the library 363395f70eSjsing * being used are not cryptographic related :-). 373395f70eSjsing * 4. If you include any Windows specific code (or a derivative thereof) from 383395f70eSjsing * the apps directory (application code) you must include an acknowledgement: 393395f70eSjsing * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 403395f70eSjsing * 413395f70eSjsing * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 423395f70eSjsing * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 433395f70eSjsing * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 443395f70eSjsing * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 453395f70eSjsing * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 463395f70eSjsing * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 473395f70eSjsing * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 483395f70eSjsing * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 493395f70eSjsing * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 503395f70eSjsing * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 513395f70eSjsing * SUCH DAMAGE. 523395f70eSjsing * 533395f70eSjsing * The licence and distribution terms for any publically available version or 543395f70eSjsing * derivative of this code cannot be changed. i.e. this code cannot simply be 553395f70eSjsing * copied and put under another distribution licence 563395f70eSjsing * [including the GNU Public Licence.] 573395f70eSjsing */ 583395f70eSjsing /* ==================================================================== 593395f70eSjsing * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. 603395f70eSjsing * 613395f70eSjsing * Redistribution and use in source and binary forms, with or without 623395f70eSjsing * modification, are permitted provided that the following conditions 633395f70eSjsing * are met: 643395f70eSjsing * 653395f70eSjsing * 1. Redistributions of source code must retain the above copyright 663395f70eSjsing * notice, this list of conditions and the following disclaimer. 673395f70eSjsing * 683395f70eSjsing * 2. Redistributions in binary form must reproduce the above copyright 693395f70eSjsing * notice, this list of conditions and the following disclaimer in 703395f70eSjsing * the documentation and/or other materials provided with the 713395f70eSjsing * distribution. 723395f70eSjsing * 733395f70eSjsing * 3. All advertising materials mentioning features or use of this 743395f70eSjsing * software must display the following acknowledgment: 753395f70eSjsing * "This product includes software developed by the OpenSSL Project 763395f70eSjsing * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 773395f70eSjsing * 783395f70eSjsing * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 793395f70eSjsing * endorse or promote products derived from this software without 803395f70eSjsing * prior written permission. For written permission, please contact 813395f70eSjsing * openssl-core@openssl.org. 823395f70eSjsing * 833395f70eSjsing * 5. Products derived from this software may not be called "OpenSSL" 843395f70eSjsing * nor may "OpenSSL" appear in their names without prior written 853395f70eSjsing * permission of the OpenSSL Project. 863395f70eSjsing * 873395f70eSjsing * 6. Redistributions of any form whatsoever must retain the following 883395f70eSjsing * acknowledgment: 893395f70eSjsing * "This product includes software developed by the OpenSSL Project 903395f70eSjsing * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 913395f70eSjsing * 923395f70eSjsing * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 933395f70eSjsing * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 943395f70eSjsing * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 953395f70eSjsing * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 963395f70eSjsing * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 973395f70eSjsing * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 983395f70eSjsing * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 993395f70eSjsing * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1003395f70eSjsing * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 1013395f70eSjsing * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1023395f70eSjsing * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 1033395f70eSjsing * OF THE POSSIBILITY OF SUCH DAMAGE. 1043395f70eSjsing * ==================================================================== 1053395f70eSjsing * 1063395f70eSjsing * This product includes cryptographic software written by Eric Young 1073395f70eSjsing * (eay@cryptsoft.com). This product includes software written by Tim 1083395f70eSjsing * Hudson (tjh@cryptsoft.com). 1093395f70eSjsing * 1103395f70eSjsing */ 1113395f70eSjsing /* ==================================================================== 1123395f70eSjsing * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 1133395f70eSjsing * 1143395f70eSjsing * Portions of the attached software ("Contribution") are developed by 1153395f70eSjsing * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. 1163395f70eSjsing * 1173395f70eSjsing * The Contribution is licensed pursuant to the OpenSSL open source 1183395f70eSjsing * license provided above. 1193395f70eSjsing * 1203395f70eSjsing * ECC cipher suite support in OpenSSL originally written by 1213395f70eSjsing * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. 1223395f70eSjsing * 1233395f70eSjsing */ 1243395f70eSjsing /* ==================================================================== 1253395f70eSjsing * Copyright 2005 Nokia. All rights reserved. 1263395f70eSjsing * 1273395f70eSjsing * The portions of the attached software ("Contribution") is developed by 1283395f70eSjsing * Nokia Corporation and is licensed pursuant to the OpenSSL open source 1293395f70eSjsing * license. 1303395f70eSjsing * 1313395f70eSjsing * The Contribution, originally written by Mika Kousa and Pasi Eronen of 1323395f70eSjsing * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites 1333395f70eSjsing * support (see RFC 4279) to OpenSSL. 1343395f70eSjsing * 1353395f70eSjsing * No patent licenses or other rights except those expressly stated in 1363395f70eSjsing * the OpenSSL open source license shall be deemed granted or received 1373395f70eSjsing * expressly, by implication, estoppel, or otherwise. 1383395f70eSjsing * 1393395f70eSjsing * No assurances are provided by Nokia that the Contribution does not 1403395f70eSjsing * infringe the patent or other intellectual property rights of any third 1413395f70eSjsing * party or that the license provides you with all the necessary rights 1423395f70eSjsing * to make use of the Contribution. 1433395f70eSjsing * 1443395f70eSjsing * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN 1453395f70eSjsing * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA 1463395f70eSjsing * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY 1473395f70eSjsing * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR 1483395f70eSjsing * OTHERWISE. 1493395f70eSjsing */ 1503395f70eSjsing 151eb1ff9efSjsing #include <limits.h> 1523395f70eSjsing #include <stdio.h> 1533395f70eSjsing 1543395f70eSjsing #include <openssl/bn.h> 1553395f70eSjsing #include <openssl/buffer.h> 1563395f70eSjsing #include <openssl/curve25519.h> 1573395f70eSjsing #include <openssl/evp.h> 1583395f70eSjsing #include <openssl/dh.h> 1593395f70eSjsing #include <openssl/hmac.h> 1603395f70eSjsing #include <openssl/md5.h> 1613395f70eSjsing #include <openssl/objects.h> 162f4dd87b5Sjsing #include <openssl/opensslconf.h> 1633395f70eSjsing #include <openssl/x509.h> 1643395f70eSjsing 1653395f70eSjsing #include "bytestring.h" 166b9e57b4bSjsing #include "crypto_internal.h" 167c9675a23Stb #include "dtls_local.h" 168c9675a23Stb #include "ssl_local.h" 16996b1ac03Sbeck #include "ssl_sigalgs.h" 170b626ebe3Sjsing #include "ssl_tlsext.h" 1713395f70eSjsing 172eb1ff9efSjsing static int ssl3_get_client_hello(SSL *s); 173eb1ff9efSjsing static int ssl3_send_dtls_hello_verify_request(SSL *s); 174eb1ff9efSjsing static int ssl3_send_server_hello(SSL *s); 175eb1ff9efSjsing static int ssl3_send_hello_request(SSL *s); 176eb1ff9efSjsing static int ssl3_send_server_certificate(SSL *s); 177eb1ff9efSjsing static int ssl3_send_server_key_exchange(SSL *s); 178eb1ff9efSjsing static int ssl3_send_certificate_request(SSL *s); 179eb1ff9efSjsing static int ssl3_send_server_done(SSL *s); 180eb1ff9efSjsing static int ssl3_get_client_certificate(SSL *s); 181eb1ff9efSjsing static int ssl3_get_client_key_exchange(SSL *s); 182eb1ff9efSjsing static int ssl3_get_cert_verify(SSL *s); 183eb1ff9efSjsing static int ssl3_send_newsession_ticket(SSL *s); 184eb1ff9efSjsing static int ssl3_send_cert_status(SSL *s); 185eb1ff9efSjsing static int ssl3_send_server_change_cipher_spec(SSL *s); 186eb1ff9efSjsing static int ssl3_send_server_finished(SSL *s); 187eb1ff9efSjsing static int ssl3_get_client_finished(SSL *s); 188eb1ff9efSjsing 1893395f70eSjsing int 1903395f70eSjsing ssl3_accept(SSL *s) 1913395f70eSjsing { 19278478f59Sjsing unsigned long alg_k; 1933395f70eSjsing int new_state, state, skip = 0; 194e79bb433Sinoguchi int listen = 0; 195545b2b63Sjsing int ret = -1; 1963395f70eSjsing 1973395f70eSjsing ERR_clear_error(); 1983395f70eSjsing errno = 0; 1993395f70eSjsing 2009e659261Sjsing if (SSL_is_dtls(s)) 201f19d9718Sjsing listen = s->d1->listen; 202b04c6658Sjsing 2033395f70eSjsing /* init things to blank */ 2046f7f653bSjsing s->in_handshake++; 2053395f70eSjsing if (!SSL_in_init(s) || SSL_in_before(s)) 2063395f70eSjsing SSL_clear(s); 2073395f70eSjsing 2089e659261Sjsing if (SSL_is_dtls(s)) 209f19d9718Sjsing s->d1->listen = listen; 210b04c6658Sjsing 2113395f70eSjsing for (;;) { 21202876cc3Sjsing state = s->s3->hs.state; 2133395f70eSjsing 21402876cc3Sjsing switch (s->s3->hs.state) { 2153395f70eSjsing case SSL_ST_RENEGOTIATE: 2166f7f653bSjsing s->renegotiate = 1; 21702876cc3Sjsing /* s->s3->hs.state=SSL_ST_ACCEPT; */ 2183395f70eSjsing 2193395f70eSjsing case SSL_ST_BEFORE: 2203395f70eSjsing case SSL_ST_ACCEPT: 2213395f70eSjsing case SSL_ST_BEFORE|SSL_ST_ACCEPT: 2223395f70eSjsing case SSL_ST_OK|SSL_ST_ACCEPT: 2233395f70eSjsing s->server = 1; 224545b2b63Sjsing 225545b2b63Sjsing ssl_info_callback(s, SSL_CB_HANDSHAKE_START, 1); 2263395f70eSjsing 227b81375ccSjsing if (!ssl_legacy_stack_version(s, s->version)) { 228b04c6658Sjsing SSLerror(s, ERR_R_INTERNAL_ERROR); 229b04c6658Sjsing ret = -1; 230b04c6658Sjsing goto end; 231b04c6658Sjsing } 232b81375ccSjsing 233c5e6469dSjsing if (!ssl_supported_tls_version_range(s, 23402876cc3Sjsing &s->s3->hs.our_min_tls_version, 23502876cc3Sjsing &s->s3->hs.our_max_tls_version)) { 236c5e6469dSjsing SSLerror(s, SSL_R_NO_PROTOCOLS_AVAILABLE); 237c5e6469dSjsing ret = -1; 238c5e6469dSjsing goto end; 239c5e6469dSjsing } 240c5e6469dSjsing 24189b4969bStb if (!ssl_security_version(s, 24289b4969bStb s->s3->hs.our_min_tls_version)) { 24389b4969bStb SSLerror(s, SSL_R_VERSION_TOO_LOW); 24489b4969bStb ret = -1; 24589b4969bStb goto end; 24689b4969bStb } 24789b4969bStb 2483395f70eSjsing if (!ssl3_setup_init_buffer(s)) { 2493395f70eSjsing ret = -1; 2503395f70eSjsing goto end; 2513395f70eSjsing } 2523395f70eSjsing if (!ssl3_setup_buffers(s)) { 2533395f70eSjsing ret = -1; 2543395f70eSjsing goto end; 2553395f70eSjsing } 2563395f70eSjsing 2576f7f653bSjsing s->init_num = 0; 2583395f70eSjsing 25902876cc3Sjsing if (s->s3->hs.state != SSL_ST_RENEGOTIATE) { 2603395f70eSjsing /* 2613395f70eSjsing * Ok, we now need to push on a buffering BIO 2623395f70eSjsing * so that the output is sent in a way that 2633395f70eSjsing * TCP likes :-) 2643395f70eSjsing */ 2653395f70eSjsing if (!ssl_init_wbio_buffer(s, 1)) { 2663395f70eSjsing ret = -1; 2673395f70eSjsing goto end; 2683395f70eSjsing } 269cef855dcSjsing 270cef855dcSjsing if (!tls1_transcript_init(s)) { 2713395f70eSjsing ret = -1; 2723395f70eSjsing goto end; 2733395f70eSjsing } 2743395f70eSjsing 27502876cc3Sjsing s->s3->hs.state = SSL3_ST_SR_CLNT_HELLO_A; 2766f7f653bSjsing s->ctx->stats.sess_accept++; 27702876cc3Sjsing } else if (!SSL_is_dtls(s) && !s->s3->send_connection_binding) { 2783395f70eSjsing /* 2793395f70eSjsing * Server attempting to renegotiate with 2803395f70eSjsing * client that doesn't support secure 2813395f70eSjsing * renegotiation. 2823395f70eSjsing */ 283c9d7abb7Sbeck SSLerror(s, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); 2843395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, 2853395f70eSjsing SSL_AD_HANDSHAKE_FAILURE); 2863395f70eSjsing ret = -1; 2873395f70eSjsing goto end; 2883395f70eSjsing } else { 2893395f70eSjsing /* 29002876cc3Sjsing * s->s3->hs.state == SSL_ST_RENEGOTIATE, 29178478f59Sjsing * we will just send a HelloRequest. 2923395f70eSjsing */ 2936f7f653bSjsing s->ctx->stats.sess_accept_renegotiate++; 29402876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_HELLO_REQ_A; 2953395f70eSjsing } 2963395f70eSjsing break; 2973395f70eSjsing 2983395f70eSjsing case SSL3_ST_SW_HELLO_REQ_A: 2993395f70eSjsing case SSL3_ST_SW_HELLO_REQ_B: 3006f7f653bSjsing s->shutdown = 0; 3019e659261Sjsing if (SSL_is_dtls(s)) { 302b04c6658Sjsing dtls1_clear_record_buffer(s); 303b04c6658Sjsing dtls1_start_timer(s); 304b04c6658Sjsing } 3053395f70eSjsing ret = ssl3_send_hello_request(s); 3063395f70eSjsing if (ret <= 0) 3073395f70eSjsing goto end; 3089e659261Sjsing if (SSL_is_dtls(s)) 30902876cc3Sjsing s->s3->hs.tls12.next_state = SSL3_ST_SR_CLNT_HELLO_A; 310b04c6658Sjsing else 31102876cc3Sjsing s->s3->hs.tls12.next_state = SSL3_ST_SW_HELLO_REQ_C; 31202876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_FLUSH; 3136f7f653bSjsing s->init_num = 0; 3143395f70eSjsing 3155c4127adSjsing if (SSL_is_dtls(s)) { 316cef855dcSjsing if (!tls1_transcript_init(s)) { 3173395f70eSjsing ret = -1; 3183395f70eSjsing goto end; 3193395f70eSjsing } 3205c4127adSjsing } 3213395f70eSjsing break; 3223395f70eSjsing 3233395f70eSjsing case SSL3_ST_SW_HELLO_REQ_C: 32402876cc3Sjsing s->s3->hs.state = SSL_ST_OK; 3253395f70eSjsing break; 3263395f70eSjsing 3273395f70eSjsing case SSL3_ST_SR_CLNT_HELLO_A: 3283395f70eSjsing case SSL3_ST_SR_CLNT_HELLO_B: 3293395f70eSjsing case SSL3_ST_SR_CLNT_HELLO_C: 3306f7f653bSjsing s->shutdown = 0; 3319e659261Sjsing if (SSL_is_dtls(s)) { 332b04c6658Sjsing ret = ssl3_get_client_hello(s); 333b04c6658Sjsing if (ret <= 0) 334b04c6658Sjsing goto end; 335b04c6658Sjsing dtls1_stop_timer(s); 336b04c6658Sjsing 337b04c6658Sjsing if (ret == 1 && 338b04c6658Sjsing (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) 33902876cc3Sjsing s->s3->hs.state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A; 340b04c6658Sjsing else 34102876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_SRVR_HELLO_A; 342b04c6658Sjsing 3436f7f653bSjsing s->init_num = 0; 344b04c6658Sjsing 345b04c6658Sjsing /* 346b04c6658Sjsing * Reflect ClientHello sequence to remain 347b04c6658Sjsing * stateless while listening. 348b04c6658Sjsing */ 349b04c6658Sjsing if (listen) { 350f2284ad0Sjsing tls12_record_layer_reflect_seq_num( 3516f7f653bSjsing s->rl); 352b04c6658Sjsing } 353b04c6658Sjsing 354b04c6658Sjsing /* If we're just listening, stop here */ 35502876cc3Sjsing if (listen && s->s3->hs.state == SSL3_ST_SW_SRVR_HELLO_A) { 356b04c6658Sjsing ret = 2; 357f19d9718Sjsing s->d1->listen = 0; 358b04c6658Sjsing /* 359b04c6658Sjsing * Set expected sequence numbers to 360b04c6658Sjsing * continue the handshake. 361b04c6658Sjsing */ 362f19d9718Sjsing s->d1->handshake_read_seq = 2; 363f19d9718Sjsing s->d1->handshake_write_seq = 1; 364f19d9718Sjsing s->d1->next_handshake_write_seq = 1; 365b04c6658Sjsing goto end; 366b04c6658Sjsing } 367b04c6658Sjsing } else { 3686f7f653bSjsing if (s->rwstate != SSL_X509_LOOKUP) { 3693395f70eSjsing ret = ssl3_get_client_hello(s); 3703395f70eSjsing if (ret <= 0) 3713395f70eSjsing goto end; 3723395f70eSjsing } 3733395f70eSjsing 3746f7f653bSjsing s->renegotiate = 2; 37502876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_SRVR_HELLO_A; 3766f7f653bSjsing s->init_num = 0; 377b04c6658Sjsing } 378b04c6658Sjsing break; 379b04c6658Sjsing 380b04c6658Sjsing case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A: 381b04c6658Sjsing case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B: 382f6d60a7aSjsing ret = ssl3_send_dtls_hello_verify_request(s); 383b04c6658Sjsing if (ret <= 0) 384b04c6658Sjsing goto end; 38502876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_FLUSH; 38602876cc3Sjsing s->s3->hs.tls12.next_state = SSL3_ST_SR_CLNT_HELLO_A; 387b04c6658Sjsing 388b04c6658Sjsing /* HelloVerifyRequest resets Finished MAC. */ 389372de723Sjsing tls1_transcript_reset(s); 3903395f70eSjsing break; 3913395f70eSjsing 3923395f70eSjsing case SSL3_ST_SW_SRVR_HELLO_A: 3933395f70eSjsing case SSL3_ST_SW_SRVR_HELLO_B: 3949e659261Sjsing if (SSL_is_dtls(s)) { 3956f7f653bSjsing s->renegotiate = 2; 396b04c6658Sjsing dtls1_start_timer(s); 397b04c6658Sjsing } 3983395f70eSjsing ret = ssl3_send_server_hello(s); 3993395f70eSjsing if (ret <= 0) 4003395f70eSjsing goto end; 4016f7f653bSjsing if (s->hit) { 4026f7f653bSjsing if (s->tlsext_ticket_expected) 40302876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_SESSION_TICKET_A; 4043395f70eSjsing else 40502876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_CHANGE_A; 40678478f59Sjsing } else { 40702876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_CERT_A; 40878478f59Sjsing } 4096f7f653bSjsing s->init_num = 0; 4103395f70eSjsing break; 4113395f70eSjsing 4123395f70eSjsing case SSL3_ST_SW_CERT_A: 4133395f70eSjsing case SSL3_ST_SW_CERT_B: 4143395f70eSjsing /* Check if it is anon DH or anon ECDH. */ 41502876cc3Sjsing if (!(s->s3->hs.cipher->algorithm_auth & 4163395f70eSjsing SSL_aNULL)) { 4179e659261Sjsing if (SSL_is_dtls(s)) 418b04c6658Sjsing dtls1_start_timer(s); 4193395f70eSjsing ret = ssl3_send_server_certificate(s); 4203395f70eSjsing if (ret <= 0) 4213395f70eSjsing goto end; 4226f7f653bSjsing if (s->tlsext_status_expected) 42302876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_CERT_STATUS_A; 4243395f70eSjsing else 42502876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_KEY_EXCH_A; 4263395f70eSjsing } else { 4273395f70eSjsing skip = 1; 42802876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_KEY_EXCH_A; 4293395f70eSjsing } 4306f7f653bSjsing s->init_num = 0; 4313395f70eSjsing break; 4323395f70eSjsing 4333395f70eSjsing case SSL3_ST_SW_KEY_EXCH_A: 4343395f70eSjsing case SSL3_ST_SW_KEY_EXCH_B: 43502876cc3Sjsing alg_k = s->s3->hs.cipher->algorithm_mkey; 4363395f70eSjsing 4373395f70eSjsing /* 4383395f70eSjsing * Only send if using a DH key exchange. 4393395f70eSjsing * 4403395f70eSjsing * For ECC ciphersuites, we send a ServerKeyExchange 4413395f70eSjsing * message only if the cipher suite is ECDHE. In other 4423395f70eSjsing * cases, the server certificate contains the server's 4433395f70eSjsing * public key for key exchange. 4443395f70eSjsing */ 4453395f70eSjsing if (alg_k & (SSL_kDHE|SSL_kECDHE)) { 4469e659261Sjsing if (SSL_is_dtls(s)) 447b04c6658Sjsing dtls1_start_timer(s); 4483395f70eSjsing ret = ssl3_send_server_key_exchange(s); 4493395f70eSjsing if (ret <= 0) 4503395f70eSjsing goto end; 4513395f70eSjsing } else 4523395f70eSjsing skip = 1; 4533395f70eSjsing 45402876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_CERT_REQ_A; 4556f7f653bSjsing s->init_num = 0; 4563395f70eSjsing break; 4573395f70eSjsing 4583395f70eSjsing case SSL3_ST_SW_CERT_REQ_A: 4593395f70eSjsing case SSL3_ST_SW_CERT_REQ_B: 4603395f70eSjsing /* 4613395f70eSjsing * Determine whether or not we need to request a 4623395f70eSjsing * certificate. 4633395f70eSjsing * 4643395f70eSjsing * Do not request a certificate if: 4653395f70eSjsing * 4663395f70eSjsing * - We did not ask for it (SSL_VERIFY_PEER is unset). 4673395f70eSjsing * 4683395f70eSjsing * - SSL_VERIFY_CLIENT_ONCE is set and we are 4693395f70eSjsing * renegotiating. 4703395f70eSjsing * 4713395f70eSjsing * - We are using an anonymous ciphersuites 4723395f70eSjsing * (see section "Certificate request" in SSL 3 drafts 4733395f70eSjsing * and in RFC 2246) ... except when the application 4743395f70eSjsing * insists on verification (against the specs, but 4753395f70eSjsing * s3_clnt.c accepts this for SSL 3). 4763395f70eSjsing */ 4773395f70eSjsing if (!(s->verify_mode & SSL_VERIFY_PEER) || 478666c9986Sjsing ((s->session->peer_cert != NULL) && 4793395f70eSjsing (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || 48002876cc3Sjsing ((s->s3->hs.cipher->algorithm_auth & 4813395f70eSjsing SSL_aNULL) && !(s->verify_mode & 4823395f70eSjsing SSL_VERIFY_FAIL_IF_NO_PEER_CERT))) { 48378478f59Sjsing /* No cert request. */ 4843395f70eSjsing skip = 1; 48502876cc3Sjsing s->s3->hs.tls12.cert_request = 0; 48602876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_SRVR_DONE_A; 487cef855dcSjsing 4889e659261Sjsing if (!SSL_is_dtls(s)) 489cef855dcSjsing tls1_transcript_free(s); 4903395f70eSjsing } else { 49102876cc3Sjsing s->s3->hs.tls12.cert_request = 1; 4929e659261Sjsing if (SSL_is_dtls(s)) 493b04c6658Sjsing dtls1_start_timer(s); 4943395f70eSjsing ret = ssl3_send_certificate_request(s); 4953395f70eSjsing if (ret <= 0) 4963395f70eSjsing goto end; 49702876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_SRVR_DONE_A; 4986f7f653bSjsing s->init_num = 0; 4993395f70eSjsing } 5003395f70eSjsing break; 5013395f70eSjsing 5023395f70eSjsing case SSL3_ST_SW_SRVR_DONE_A: 5033395f70eSjsing case SSL3_ST_SW_SRVR_DONE_B: 5049e659261Sjsing if (SSL_is_dtls(s)) 505b04c6658Sjsing dtls1_start_timer(s); 5063395f70eSjsing ret = ssl3_send_server_done(s); 5073395f70eSjsing if (ret <= 0) 5083395f70eSjsing goto end; 50902876cc3Sjsing s->s3->hs.tls12.next_state = SSL3_ST_SR_CERT_A; 51002876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_FLUSH; 5116f7f653bSjsing s->init_num = 0; 5123395f70eSjsing break; 5133395f70eSjsing 5143395f70eSjsing case SSL3_ST_SW_FLUSH: 5153395f70eSjsing /* 5163395f70eSjsing * This code originally checked to see if 5173395f70eSjsing * any data was pending using BIO_CTRL_INFO 5183395f70eSjsing * and then flushed. This caused problems 5193395f70eSjsing * as documented in PR#1939. The proposed 5203395f70eSjsing * fix doesn't completely resolve this issue 5213395f70eSjsing * as buggy implementations of BIO_CTRL_PENDING 5223395f70eSjsing * still exist. So instead we just flush 5233395f70eSjsing * unconditionally. 5243395f70eSjsing */ 5256f7f653bSjsing s->rwstate = SSL_WRITING; 5263395f70eSjsing if (BIO_flush(s->wbio) <= 0) { 5279e659261Sjsing if (SSL_is_dtls(s)) { 528b04c6658Sjsing /* If the write error was fatal, stop trying. */ 529b04c6658Sjsing if (!BIO_should_retry(s->wbio)) { 5306f7f653bSjsing s->rwstate = SSL_NOTHING; 53102876cc3Sjsing s->s3->hs.state = s->s3->hs.tls12.next_state; 532b04c6658Sjsing } 533b04c6658Sjsing } 5343395f70eSjsing ret = -1; 5353395f70eSjsing goto end; 5363395f70eSjsing } 5376f7f653bSjsing s->rwstate = SSL_NOTHING; 53802876cc3Sjsing s->s3->hs.state = s->s3->hs.tls12.next_state; 5393395f70eSjsing break; 5403395f70eSjsing 5413395f70eSjsing case SSL3_ST_SR_CERT_A: 5423395f70eSjsing case SSL3_ST_SR_CERT_B: 543a5c907f4Sjsing if (s->s3->hs.tls12.cert_request != 0) { 5443395f70eSjsing ret = ssl3_get_client_certificate(s); 5453395f70eSjsing if (ret <= 0) 5463395f70eSjsing goto end; 5473395f70eSjsing } 5486f7f653bSjsing s->init_num = 0; 54902876cc3Sjsing s->s3->hs.state = SSL3_ST_SR_KEY_EXCH_A; 5503395f70eSjsing break; 5513395f70eSjsing 5523395f70eSjsing case SSL3_ST_SR_KEY_EXCH_A: 5533395f70eSjsing case SSL3_ST_SR_KEY_EXCH_B: 5543395f70eSjsing ret = ssl3_get_client_key_exchange(s); 5553395f70eSjsing if (ret <= 0) 5563395f70eSjsing goto end; 557b04c6658Sjsing 5589e659261Sjsing if (SSL_is_dtls(s)) { 55902876cc3Sjsing s->s3->hs.state = SSL3_ST_SR_CERT_VRFY_A; 5606f7f653bSjsing s->init_num = 0; 561b04c6658Sjsing } 562b04c6658Sjsing 56302876cc3Sjsing alg_k = s->s3->hs.cipher->algorithm_mkey; 564689a9b7eSbeck if (SSL_USE_SIGALGS(s)) { 56502876cc3Sjsing s->s3->hs.state = SSL3_ST_SR_CERT_VRFY_A; 5666f7f653bSjsing s->init_num = 0; 567666c9986Sjsing if (!s->session->peer_cert) 5683395f70eSjsing break; 5693395f70eSjsing /* 570cef855dcSjsing * Freeze the transcript for use during client 571cef855dcSjsing * certificate verification. 5723395f70eSjsing */ 573cef855dcSjsing tls1_transcript_freeze(s); 5743395f70eSjsing } else { 57502876cc3Sjsing s->s3->hs.state = SSL3_ST_SR_CERT_VRFY_A; 5766f7f653bSjsing s->init_num = 0; 5773395f70eSjsing 578cef855dcSjsing tls1_transcript_free(s); 579cef855dcSjsing 5803395f70eSjsing /* 5813395f70eSjsing * We need to get hashes here so if there is 582cc372b7aSjsing * a client cert, it can be verified. 5833395f70eSjsing */ 584df40b63bSjsing if (!tls1_transcript_hash_value(s, 58502876cc3Sjsing s->s3->hs.tls12.cert_verify, 58602876cc3Sjsing sizeof(s->s3->hs.tls12.cert_verify), 587cc372b7aSjsing NULL)) { 5883395f70eSjsing ret = -1; 5893395f70eSjsing goto end; 5903395f70eSjsing } 5913395f70eSjsing } 5923395f70eSjsing break; 5933395f70eSjsing 5943395f70eSjsing case SSL3_ST_SR_CERT_VRFY_A: 5953395f70eSjsing case SSL3_ST_SR_CERT_VRFY_B: 5969e659261Sjsing if (SSL_is_dtls(s)) 597f19d9718Sjsing s->d1->change_cipher_spec_ok = 1; 598b04c6658Sjsing else 5993395f70eSjsing s->s3->flags |= SSL3_FLAGS_CCS_OK; 6003395f70eSjsing 6013395f70eSjsing /* we should decide if we expected this one */ 6023395f70eSjsing ret = ssl3_get_cert_verify(s); 6033395f70eSjsing if (ret <= 0) 6043395f70eSjsing goto end; 60502876cc3Sjsing s->s3->hs.state = SSL3_ST_SR_FINISHED_A; 6066f7f653bSjsing s->init_num = 0; 6073395f70eSjsing break; 6083395f70eSjsing 6093395f70eSjsing case SSL3_ST_SR_FINISHED_A: 6103395f70eSjsing case SSL3_ST_SR_FINISHED_B: 6119e659261Sjsing if (SSL_is_dtls(s)) 612f19d9718Sjsing s->d1->change_cipher_spec_ok = 1; 613b04c6658Sjsing else 6143395f70eSjsing s->s3->flags |= SSL3_FLAGS_CCS_OK; 615eb1ff9efSjsing ret = ssl3_get_client_finished(s); 6163395f70eSjsing if (ret <= 0) 6173395f70eSjsing goto end; 6189e659261Sjsing if (SSL_is_dtls(s)) 619b04c6658Sjsing dtls1_stop_timer(s); 6206f7f653bSjsing if (s->hit) 62102876cc3Sjsing s->s3->hs.state = SSL_ST_OK; 6226f7f653bSjsing else if (s->tlsext_ticket_expected) 62302876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_SESSION_TICKET_A; 6243395f70eSjsing else 62502876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_CHANGE_A; 6266f7f653bSjsing s->init_num = 0; 6273395f70eSjsing break; 6283395f70eSjsing 6293395f70eSjsing case SSL3_ST_SW_SESSION_TICKET_A: 6303395f70eSjsing case SSL3_ST_SW_SESSION_TICKET_B: 6313395f70eSjsing ret = ssl3_send_newsession_ticket(s); 6323395f70eSjsing if (ret <= 0) 6333395f70eSjsing goto end; 63402876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_CHANGE_A; 6356f7f653bSjsing s->init_num = 0; 6363395f70eSjsing break; 6373395f70eSjsing 6383395f70eSjsing case SSL3_ST_SW_CERT_STATUS_A: 6393395f70eSjsing case SSL3_ST_SW_CERT_STATUS_B: 6403395f70eSjsing ret = ssl3_send_cert_status(s); 6413395f70eSjsing if (ret <= 0) 6423395f70eSjsing goto end; 64302876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_KEY_EXCH_A; 6446f7f653bSjsing s->init_num = 0; 6453395f70eSjsing break; 6463395f70eSjsing 6473395f70eSjsing case SSL3_ST_SW_CHANGE_A: 6483395f70eSjsing case SSL3_ST_SW_CHANGE_B: 649eb1ff9efSjsing ret = ssl3_send_server_change_cipher_spec(s); 6503395f70eSjsing if (ret <= 0) 6513395f70eSjsing goto end; 65202876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_FINISHED_A; 6536f7f653bSjsing s->init_num = 0; 654*f4fe6251Sjsing s->session->cipher_value = s->s3->hs.cipher->value; 655b3d9ef4bSjsing 65656985c13Sjsing if (!tls1_setup_key_block(s)) { 65756985c13Sjsing ret = -1; 65856985c13Sjsing goto end; 65956985c13Sjsing } 660b3d9ef4bSjsing if (!tls1_change_write_cipher_state(s)) { 6613395f70eSjsing ret = -1; 6623395f70eSjsing goto end; 6633395f70eSjsing } 6643395f70eSjsing break; 6653395f70eSjsing 6663395f70eSjsing case SSL3_ST_SW_FINISHED_A: 6673395f70eSjsing case SSL3_ST_SW_FINISHED_B: 668eb1ff9efSjsing ret = ssl3_send_server_finished(s); 6693395f70eSjsing if (ret <= 0) 6703395f70eSjsing goto end; 67102876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_FLUSH; 6726f7f653bSjsing if (s->hit) { 67302876cc3Sjsing s->s3->hs.tls12.next_state = SSL3_ST_SR_FINISHED_A; 674e88a9643Sbluhm tls1_transcript_free(s); 675e88a9643Sbluhm } else 67602876cc3Sjsing s->s3->hs.tls12.next_state = SSL_ST_OK; 6776f7f653bSjsing s->init_num = 0; 6783395f70eSjsing break; 6793395f70eSjsing 6803395f70eSjsing case SSL_ST_OK: 6813395f70eSjsing /* clean a few things up */ 6823395f70eSjsing tls1_cleanup_key_block(s); 6833395f70eSjsing 68402876cc3Sjsing if (s->s3->handshake_transcript != NULL) { 685d250fe7cSjsing SSLerror(s, ERR_R_INTERNAL_ERROR); 686d250fe7cSjsing ret = -1; 687d250fe7cSjsing goto end; 688d250fe7cSjsing } 689d250fe7cSjsing 6909e659261Sjsing if (!SSL_is_dtls(s)) 691beb7767dSjsing ssl3_release_init_buffer(s); 6923395f70eSjsing 6933395f70eSjsing /* remove buffering on output */ 6943395f70eSjsing ssl_free_wbio_buffer(s); 6953395f70eSjsing 6966f7f653bSjsing s->init_num = 0; 6973395f70eSjsing 69878478f59Sjsing /* Skipped if we just sent a HelloRequest. */ 6996f7f653bSjsing if (s->renegotiate == 2) { 7006f7f653bSjsing s->renegotiate = 0; 7016f7f653bSjsing s->new_session = 0; 7023395f70eSjsing 7033395f70eSjsing ssl_update_cache(s, SSL_SESS_CACHE_SERVER); 7043395f70eSjsing 7056f7f653bSjsing s->ctx->stats.sess_accept_good++; 7063395f70eSjsing /* s->server=1; */ 7076f7f653bSjsing s->handshake_func = ssl3_accept; 7083395f70eSjsing 709545b2b63Sjsing ssl_info_callback(s, SSL_CB_HANDSHAKE_DONE, 1); 7103395f70eSjsing } 7113395f70eSjsing 7123395f70eSjsing ret = 1; 713b04c6658Sjsing 7149e659261Sjsing if (SSL_is_dtls(s)) { 715b04c6658Sjsing /* Done handshaking, next message is client hello. */ 716f19d9718Sjsing s->d1->handshake_read_seq = 0; 717b04c6658Sjsing /* Next message is server hello. */ 718f19d9718Sjsing s->d1->handshake_write_seq = 0; 719f19d9718Sjsing s->d1->next_handshake_write_seq = 0; 720b04c6658Sjsing } 7213395f70eSjsing goto end; 7223395f70eSjsing /* break; */ 7233395f70eSjsing 7243395f70eSjsing default: 725c9d7abb7Sbeck SSLerror(s, SSL_R_UNKNOWN_STATE); 7263395f70eSjsing ret = -1; 7273395f70eSjsing goto end; 7283395f70eSjsing /* break; */ 7293395f70eSjsing } 7303395f70eSjsing 73102876cc3Sjsing if (!s->s3->hs.tls12.reuse_message && !skip) { 73202876cc3Sjsing if (s->s3->hs.state != state) { 73302876cc3Sjsing new_state = s->s3->hs.state; 73402876cc3Sjsing s->s3->hs.state = state; 735545b2b63Sjsing ssl_info_callback(s, SSL_CB_ACCEPT_LOOP, 1); 73602876cc3Sjsing s->s3->hs.state = new_state; 7373395f70eSjsing } 7383395f70eSjsing } 7393395f70eSjsing skip = 0; 7403395f70eSjsing } 7413395f70eSjsing end: 7423395f70eSjsing /* BIO_flush(s->wbio); */ 7436f7f653bSjsing s->in_handshake--; 744545b2b63Sjsing ssl_info_callback(s, SSL_CB_ACCEPT_EXIT, ret); 74578478f59Sjsing 7463395f70eSjsing return (ret); 7473395f70eSjsing } 7483395f70eSjsing 749eb1ff9efSjsing static int 7503395f70eSjsing ssl3_send_hello_request(SSL *s) 7513395f70eSjsing { 752a9e43a46Sjsing CBB cbb, hello; 753a9e43a46Sjsing 754a9e43a46Sjsing memset(&cbb, 0, sizeof(cbb)); 755a9e43a46Sjsing 75602876cc3Sjsing if (s->s3->hs.state == SSL3_ST_SW_HELLO_REQ_A) { 757312b4b14Sjsing if (!ssl3_handshake_msg_start(s, &cbb, &hello, 758a9e43a46Sjsing SSL3_MT_HELLO_REQUEST)) 759a9e43a46Sjsing goto err; 760312b4b14Sjsing if (!ssl3_handshake_msg_finish(s, &cbb)) 761a9e43a46Sjsing goto err; 7623395f70eSjsing 76302876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_HELLO_REQ_B; 7643395f70eSjsing } 7653395f70eSjsing 7663395f70eSjsing /* SSL3_ST_SW_HELLO_REQ_B */ 7673395f70eSjsing return (ssl3_handshake_write(s)); 768a9e43a46Sjsing 769a9e43a46Sjsing err: 770a9e43a46Sjsing CBB_cleanup(&cbb); 771a9e43a46Sjsing 772a9e43a46Sjsing return (-1); 7733395f70eSjsing } 7743395f70eSjsing 775eb1ff9efSjsing static int 7763395f70eSjsing ssl3_get_client_hello(SSL *s) 7773395f70eSjsing { 7786f7155e1Sjsing CBS cbs, client_random, session_id, cookie, cipher_suites; 7796f7155e1Sjsing CBS compression_methods; 7806f7155e1Sjsing uint16_t client_version; 7816f7155e1Sjsing uint8_t comp_method; 7826f7155e1Sjsing int comp_null; 783f2c3e964Sjsing int i, j, al, ret, cookie_valid = 0; 7843395f70eSjsing SSL_CIPHER *c; 7853395f70eSjsing STACK_OF(SSL_CIPHER) *ciphers = NULL; 7863395f70eSjsing const SSL_METHOD *method; 7871927d779Sjsing uint16_t shared_version; 7883395f70eSjsing 7893395f70eSjsing /* 7903395f70eSjsing * We do this so that we will respond with our native type. 7913395f70eSjsing * If we are TLSv1 and we get SSLv3, we will respond with TLSv1, 7923395f70eSjsing * This down switching should be handled by a different method. 7933395f70eSjsing * If we are SSLv3, we will respond with SSLv3, even if prompted with 7943395f70eSjsing * TLSv1. 7953395f70eSjsing */ 79602876cc3Sjsing if (s->s3->hs.state == SSL3_ST_SR_CLNT_HELLO_A) 79702876cc3Sjsing s->s3->hs.state = SSL3_ST_SR_CLNT_HELLO_B; 7983395f70eSjsing 7996f7f653bSjsing s->first_packet = 1; 800f2c3e964Sjsing if ((ret = ssl3_get_message(s, SSL3_ST_SR_CLNT_HELLO_B, 8013395f70eSjsing SSL3_ST_SR_CLNT_HELLO_C, SSL3_MT_CLIENT_HELLO, 802f2c3e964Sjsing SSL3_RT_MAX_PLAIN_LENGTH)) <= 0) 803f2c3e964Sjsing return ret; 8046f7f653bSjsing s->first_packet = 0; 8053395f70eSjsing 806f2c3e964Sjsing ret = -1; 807f2c3e964Sjsing 8086f7f653bSjsing if (s->init_num < 0) 8096f7155e1Sjsing goto err; 8103395f70eSjsing 8116f7f653bSjsing CBS_init(&cbs, s->init_msg, s->init_num); 8126f7155e1Sjsing 8135a15326fSjsing /* Parse client hello up until the extensions (if any). */ 8145a15326fSjsing if (!CBS_get_u16(&cbs, &client_version)) 815e0d5994dStb goto decode_err; 8165a15326fSjsing if (!CBS_get_bytes(&cbs, &client_random, SSL3_RANDOM_SIZE)) 817e0d5994dStb goto decode_err; 8185a15326fSjsing if (!CBS_get_u8_length_prefixed(&cbs, &session_id)) 819e0d5994dStb goto decode_err; 820380a8befStb if (CBS_len(&session_id) > SSL3_SESSION_ID_SIZE) { 821380a8befStb al = SSL_AD_ILLEGAL_PARAMETER; 822380a8befStb SSLerror(s, SSL_R_SSL3_SESSION_ID_TOO_LONG); 823ba06b73eStb goto fatal_err; 824380a8befStb } 8259e659261Sjsing if (SSL_is_dtls(s)) { 8265a15326fSjsing if (!CBS_get_u8_length_prefixed(&cbs, &cookie)) 827e0d5994dStb goto decode_err; 8285a15326fSjsing } 8295a15326fSjsing if (!CBS_get_u16_length_prefixed(&cbs, &cipher_suites)) 830e0d5994dStb goto decode_err; 8315a15326fSjsing if (!CBS_get_u8_length_prefixed(&cbs, &compression_methods)) 832e0d5994dStb goto decode_err; 8335a15326fSjsing 8343395f70eSjsing /* 8353395f70eSjsing * Use version from inside client hello, not from record header. 8363395f70eSjsing * (may differ: see RFC 2246, Appendix E, second paragraph) 8373395f70eSjsing */ 838c5e6469dSjsing if (!ssl_max_shared_version(s, client_version, &shared_version)) { 83901f29c58Sjsing if ((client_version >> 8) == SSL3_VERSION_MAJOR && 8406f7f653bSjsing !tls12_record_layer_write_protected(s->rl)) { 8413395f70eSjsing /* 8423395f70eSjsing * Similar to ssl3_get_record, send alert using remote 8433395f70eSjsing * version number. 8443395f70eSjsing */ 84501f29c58Sjsing s->version = client_version; 8463395f70eSjsing } 8471365e68cSjsing SSLerror(s, SSL_R_WRONG_VERSION_NUMBER); 8483395f70eSjsing al = SSL_AD_PROTOCOL_VERSION; 849ba06b73eStb goto fatal_err; 8503395f70eSjsing } 85102876cc3Sjsing s->s3->hs.peer_legacy_version = client_version; 8523395f70eSjsing s->version = shared_version; 8533395f70eSjsing 85402876cc3Sjsing s->s3->hs.negotiated_tls_version = ssl_tls_version(shared_version); 85502876cc3Sjsing if (s->s3->hs.negotiated_tls_version == 0) { 856c5e6469dSjsing SSLerror(s, ERR_R_INTERNAL_ERROR); 857c5e6469dSjsing goto err; 858c5e6469dSjsing } 859c5e6469dSjsing 8609fef1c44Sjsing if ((method = ssl_get_method(shared_version)) == NULL) { 861c9d7abb7Sbeck SSLerror(s, ERR_R_INTERNAL_ERROR); 8623395f70eSjsing goto err; 8633395f70eSjsing } 8643395f70eSjsing s->method = method; 8653395f70eSjsing 8663395f70eSjsing /* 8675a15326fSjsing * If we require cookies (DTLS) and this ClientHello does not contain 8685a15326fSjsing * one, just return since we do not want to allocate any memory yet. 8695a15326fSjsing * So check cookie length... 8703395f70eSjsing */ 8719e659261Sjsing if (SSL_is_dtls(s)) { 8723395f70eSjsing if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) { 8736f7155e1Sjsing if (CBS_len(&cookie) == 0) 8743395f70eSjsing return (1); 8753395f70eSjsing } 8766f7155e1Sjsing } 8773395f70eSjsing 8786f7155e1Sjsing if (!CBS_write_bytes(&client_random, s->s3->client_random, 8796f7155e1Sjsing sizeof(s->s3->client_random), NULL)) 8806f7155e1Sjsing goto err; 8813395f70eSjsing 8826f7f653bSjsing s->hit = 0; 8836f7155e1Sjsing 8843395f70eSjsing /* 8853395f70eSjsing * Versions before 0.9.7 always allow clients to resume sessions in 8863395f70eSjsing * renegotiation. 0.9.7 and later allow this by default, but optionally 8873395f70eSjsing * ignore resumption requests with flag 8883395f70eSjsing * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag 8893395f70eSjsing * rather than a change to default behavior so that applications 8903395f70eSjsing * relying on this for security won't even compile against older 8913395f70eSjsing * library versions). 8923395f70eSjsing * 8933395f70eSjsing * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() 8946f7f653bSjsing * to request renegotiation but not a new session (s->new_session 8953395f70eSjsing * remains unset): for servers, this essentially just means that the 8963395f70eSjsing * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be 8973395f70eSjsing * ignored. 8983395f70eSjsing */ 8996f7f653bSjsing if ((s->new_session && (s->options & 9003395f70eSjsing SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) { 9013395f70eSjsing if (!ssl_get_new_session(s, 1)) 9023395f70eSjsing goto err; 9033395f70eSjsing } else { 904307759eeSjsing CBS ext_block; 905307759eeSjsing 906307759eeSjsing CBS_dup(&cbs, &ext_block); 907307759eeSjsing 9089a3f7535Stb i = ssl_get_prev_session(s, &session_id, &ext_block, &al); 9093395f70eSjsing if (i == 1) { /* previous session */ 9106f7f653bSjsing s->hit = 1; 9113395f70eSjsing } else if (i == -1) 912ba06b73eStb goto fatal_err; 9133395f70eSjsing else { 9143395f70eSjsing /* i == 0 */ 9153395f70eSjsing if (!ssl_get_new_session(s, 1)) 9163395f70eSjsing goto err; 9173395f70eSjsing } 9183395f70eSjsing } 9193395f70eSjsing 9209e659261Sjsing if (SSL_is_dtls(s)) { 9213395f70eSjsing /* 9226f7155e1Sjsing * The ClientHello may contain a cookie even if the HelloVerify 9236f7155e1Sjsing * message has not been sent - make sure that it does not cause 9246f7155e1Sjsing * an overflow. 9253395f70eSjsing */ 926f19d9718Sjsing if (CBS_len(&cookie) > sizeof(s->d1->rcvd_cookie)) { 9273395f70eSjsing al = SSL_AD_DECODE_ERROR; 928c9d7abb7Sbeck SSLerror(s, SSL_R_COOKIE_MISMATCH); 929ba06b73eStb goto fatal_err; 9303395f70eSjsing } 9313395f70eSjsing 9326f7155e1Sjsing /* Verify the cookie if appropriate option is set. */ 9333395f70eSjsing if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && 9346f7155e1Sjsing CBS_len(&cookie) > 0) { 9356f7155e1Sjsing size_t cookie_len; 9366f7155e1Sjsing 9376f7155e1Sjsing /* XXX - rcvd_cookie seems to only be used here... */ 938f19d9718Sjsing if (!CBS_write_bytes(&cookie, s->d1->rcvd_cookie, 939f19d9718Sjsing sizeof(s->d1->rcvd_cookie), &cookie_len)) 9406f7155e1Sjsing goto err; 9413395f70eSjsing 9426f7f653bSjsing if (s->ctx->app_verify_cookie_cb != NULL) { 9436f7f653bSjsing if (s->ctx->app_verify_cookie_cb(s, 944f19d9718Sjsing s->d1->rcvd_cookie, cookie_len) == 0) { 9453395f70eSjsing al = SSL_AD_HANDSHAKE_FAILURE; 946c9d7abb7Sbeck SSLerror(s, SSL_R_COOKIE_MISMATCH); 947ba06b73eStb goto fatal_err; 9483395f70eSjsing } 9493395f70eSjsing /* else cookie verification succeeded */ 9506f7155e1Sjsing /* XXX - can d1->cookie_len > sizeof(rcvd_cookie) ? */ 951f19d9718Sjsing } else if (timingsafe_memcmp(s->d1->rcvd_cookie, 952f19d9718Sjsing s->d1->cookie, s->d1->cookie_len) != 0) { 9533395f70eSjsing /* default verification */ 9543395f70eSjsing al = SSL_AD_HANDSHAKE_FAILURE; 955c9d7abb7Sbeck SSLerror(s, SSL_R_COOKIE_MISMATCH); 956ba06b73eStb goto fatal_err; 9573395f70eSjsing } 9586f1892dcSjsing cookie_valid = 1; 9593395f70eSjsing } 9603395f70eSjsing } 9613395f70eSjsing 9626f7155e1Sjsing /* XXX - This logic seems wrong... */ 9636f7155e1Sjsing if (CBS_len(&cipher_suites) == 0 && CBS_len(&session_id) != 0) { 9643395f70eSjsing /* we need a cipher if we are not resuming a session */ 9653395f70eSjsing al = SSL_AD_ILLEGAL_PARAMETER; 966c9d7abb7Sbeck SSLerror(s, SSL_R_NO_CIPHERS_SPECIFIED); 967ba06b73eStb goto fatal_err; 9683395f70eSjsing } 9696f7155e1Sjsing 9706f7155e1Sjsing if (CBS_len(&cipher_suites) > 0) { 9716f7155e1Sjsing if ((ciphers = ssl_bytes_to_cipher_list(s, 972eb97b740Sjsing &cipher_suites)) == NULL) 9733395f70eSjsing goto err; 9743395f70eSjsing } 9753395f70eSjsing 9763395f70eSjsing /* If it is a hit, check that the cipher is in the list */ 977eb97b740Sjsing /* XXX - CBS_len(&cipher_suites) will always be zero here... */ 9786f7f653bSjsing if (s->hit && CBS_len(&cipher_suites) > 0) { 9793395f70eSjsing j = 0; 9803395f70eSjsing 9813395f70eSjsing for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { 9823395f70eSjsing c = sk_SSL_CIPHER_value(ciphers, i); 983*f4fe6251Sjsing if (c->value == s->session->cipher_value) { 9843395f70eSjsing j = 1; 9853395f70eSjsing break; 9863395f70eSjsing } 9873395f70eSjsing } 9883395f70eSjsing if (j == 0) { 9893395f70eSjsing /* 9903395f70eSjsing * We need to have the cipher in the cipher 9913395f70eSjsing * list if we are asked to reuse it 9923395f70eSjsing */ 9933395f70eSjsing al = SSL_AD_ILLEGAL_PARAMETER; 994c9d7abb7Sbeck SSLerror(s, SSL_R_REQUIRED_CIPHER_MISSING); 995ba06b73eStb goto fatal_err; 9963395f70eSjsing } 9973395f70eSjsing } 9983395f70eSjsing 9996f7155e1Sjsing comp_null = 0; 10006f7155e1Sjsing while (CBS_len(&compression_methods) > 0) { 10016f7155e1Sjsing if (!CBS_get_u8(&compression_methods, &comp_method)) 1002e0d5994dStb goto decode_err; 10036f7155e1Sjsing if (comp_method == 0) 10046f7155e1Sjsing comp_null = 1; 10056f7155e1Sjsing } 10066f7155e1Sjsing if (comp_null == 0) { 10073395f70eSjsing al = SSL_AD_DECODE_ERROR; 1008c9d7abb7Sbeck SSLerror(s, SSL_R_NO_COMPRESSION_SPECIFIED); 1009ba06b73eStb goto fatal_err; 10103395f70eSjsing } 10113395f70eSjsing 10129b8a142fStb if (!tlsext_server_parse(s, SSL_TLSEXT_MSG_CH, &cbs, &al)) { 1013c9d7abb7Sbeck SSLerror(s, SSL_R_PARSE_TLSEXT); 1014ba06b73eStb goto fatal_err; 10153395f70eSjsing } 1016fd8e9d0dSjsing 1017b73cd773Sjsing if (CBS_len(&cbs) != 0) 1018b73cd773Sjsing goto decode_err; 1019b73cd773Sjsing 10206f7f653bSjsing if (!s->s3->renegotiate_seen && s->renegotiate) { 1021fd8e9d0dSjsing al = SSL_AD_HANDSHAKE_FAILURE; 1022fd8e9d0dSjsing SSLerror(s, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); 1023ba06b73eStb goto fatal_err; 1024fd8e9d0dSjsing } 1025fd8e9d0dSjsing 10263395f70eSjsing if (ssl_check_clienthello_tlsext_early(s) <= 0) { 1027c9d7abb7Sbeck SSLerror(s, SSL_R_CLIENTHELLO_TLSEXT); 10283395f70eSjsing goto err; 10293395f70eSjsing } 10303395f70eSjsing 10313395f70eSjsing /* 10323395f70eSjsing * Check if we want to use external pre-shared secret for this 10333395f70eSjsing * handshake for not reused session only. We need to generate 10343395f70eSjsing * server_random before calling tls_session_secret_cb in order to allow 10353395f70eSjsing * SessionTicket processing to use it in key derivation. 10363395f70eSjsing */ 10373395f70eSjsing arc4random_buf(s->s3->server_random, SSL3_RANDOM_SIZE); 10383395f70eSjsing 103902876cc3Sjsing if (s->s3->hs.our_max_tls_version >= TLS1_2_VERSION && 104002876cc3Sjsing s->s3->hs.negotiated_tls_version < s->s3->hs.our_max_tls_version) { 1041bec9724eSbeck /* 1042bec9724eSbeck * RFC 8446 section 4.1.3. If we are downgrading from TLS 1.3 1043bec9724eSbeck * we must set the last 8 bytes of the server random to magical 104434393826Stb * values to indicate we meant to downgrade. For TLS 1.2 it is 104534393826Stb * recommended that we do the same. 1046bec9724eSbeck */ 1047bec9724eSbeck size_t index = SSL3_RANDOM_SIZE - sizeof(tls13_downgrade_12); 1048bec9724eSbeck uint8_t *magic = &s->s3->server_random[index]; 104902876cc3Sjsing if (s->s3->hs.negotiated_tls_version == TLS1_2_VERSION) { 1050bec9724eSbeck /* Indicate we chose to downgrade to 1.2. */ 1051bec9724eSbeck memcpy(magic, tls13_downgrade_12, 1052bec9724eSbeck sizeof(tls13_downgrade_12)); 1053bec9724eSbeck } else { 1054bec9724eSbeck /* Indicate we chose to downgrade to 1.1 or lower */ 1055bec9724eSbeck memcpy(magic, tls13_downgrade_11, 1056bec9724eSbeck sizeof(tls13_downgrade_11)); 1057bec9724eSbeck } 1058bec9724eSbeck } 1059bec9724eSbeck 10606f7f653bSjsing if (!s->hit && s->tls_session_secret_cb != NULL) { 10613395f70eSjsing SSL_CIPHER *pref_cipher = NULL; 1062573b2ffaStb int master_key_length = sizeof(s->session->master_key); 10633395f70eSjsing 10646f7f653bSjsing if (!s->tls_session_secret_cb(s, 1065573b2ffaStb s->session->master_key, &master_key_length, ciphers, 10666f7f653bSjsing &pref_cipher, s->tls_session_secret_cb_arg)) { 1067573b2ffaStb SSLerror(s, ERR_R_INTERNAL_ERROR); 1068573b2ffaStb goto err; 1069573b2ffaStb } 1070573b2ffaStb if (master_key_length <= 0) { 1071573b2ffaStb SSLerror(s, ERR_R_INTERNAL_ERROR); 1072573b2ffaStb goto err; 1073573b2ffaStb } 1074573b2ffaStb s->session->master_key_length = master_key_length; 1075573b2ffaStb 10766f7f653bSjsing s->hit = 1; 10773395f70eSjsing s->session->verify_result = X509_V_OK; 10783395f70eSjsing 10791a5be6e3Sjsing sk_SSL_CIPHER_free(s->s3->hs.client_ciphers); 10801a5be6e3Sjsing s->s3->hs.client_ciphers = ciphers; 10813395f70eSjsing ciphers = NULL; 10823395f70eSjsing 108359ec10beSjsing /* 108459ec10beSjsing * XXX - this allows the callback to use any client cipher and 108559ec10beSjsing * completely ignore the server cipher list. We should ensure 108659ec10beSjsing * that the pref_cipher is in both the client list and the 108759ec10beSjsing * server list. 108859ec10beSjsing */ 108959ec10beSjsing 1090573b2ffaStb /* Check if some cipher was preferred by the callback. */ 1091573b2ffaStb if (pref_cipher == NULL) 10921a5be6e3Sjsing pref_cipher = ssl3_choose_cipher(s, s->s3->hs.client_ciphers, 10933395f70eSjsing SSL_get_ciphers(s)); 10943395f70eSjsing if (pref_cipher == NULL) { 10953395f70eSjsing al = SSL_AD_HANDSHAKE_FAILURE; 1096c9d7abb7Sbeck SSLerror(s, SSL_R_NO_SHARED_CIPHER); 1097ba06b73eStb goto fatal_err; 10983395f70eSjsing } 1099387303bbSjsing s->s3->hs.cipher = pref_cipher; 11003395f70eSjsing 110159ec10beSjsing /* XXX - why? */ 11023395f70eSjsing sk_SSL_CIPHER_free(s->cipher_list); 11031a5be6e3Sjsing s->cipher_list = sk_SSL_CIPHER_dup(s->s3->hs.client_ciphers); 11043395f70eSjsing } 11053395f70eSjsing 11063395f70eSjsing /* 11073395f70eSjsing * Given s->session->ciphers and SSL_get_ciphers, we must 11083395f70eSjsing * pick a cipher 11093395f70eSjsing */ 11103395f70eSjsing 11116f7f653bSjsing if (!s->hit) { 11123395f70eSjsing if (ciphers == NULL) { 11133395f70eSjsing al = SSL_AD_ILLEGAL_PARAMETER; 1114c9d7abb7Sbeck SSLerror(s, SSL_R_NO_CIPHERS_PASSED); 1115ba06b73eStb goto fatal_err; 11163395f70eSjsing } 11171a5be6e3Sjsing sk_SSL_CIPHER_free(s->s3->hs.client_ciphers); 11181a5be6e3Sjsing s->s3->hs.client_ciphers = ciphers; 11193395f70eSjsing ciphers = NULL; 11203395f70eSjsing 11211a5be6e3Sjsing if ((c = ssl3_choose_cipher(s, s->s3->hs.client_ciphers, 1122bab2d47bStb SSL_get_ciphers(s))) == NULL) { 11233395f70eSjsing al = SSL_AD_HANDSHAKE_FAILURE; 1124c9d7abb7Sbeck SSLerror(s, SSL_R_NO_SHARED_CIPHER); 1125ba06b73eStb goto fatal_err; 11263395f70eSjsing } 112702876cc3Sjsing s->s3->hs.cipher = c; 1128*f4fe6251Sjsing s->session->cipher_value = s->s3->hs.cipher->value; 11293395f70eSjsing } else { 1130*f4fe6251Sjsing s->s3->hs.cipher = ssl3_get_cipher_by_value(s->session->cipher_value); 1131387303bbSjsing if (s->s3->hs.cipher == NULL) 1132387303bbSjsing goto fatal_err; 11333395f70eSjsing } 11343395f70eSjsing 1135df40b63bSjsing if (!tls1_transcript_hash_init(s)) 1136366dc2a2Sjsing goto err; 1137366dc2a2Sjsing 1138689a9b7eSbeck if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) 1139cef855dcSjsing tls1_transcript_free(s); 11403395f70eSjsing 11413395f70eSjsing /* 11423395f70eSjsing * We now have the following setup. 11433395f70eSjsing * client_random 114440ca958bSjmc * cipher_list - our preferred list of ciphers 114540ca958bSjmc * ciphers - the clients preferred list of ciphers 11463395f70eSjsing * compression - basically ignored right now 11473395f70eSjsing * ssl version is set - sslv3 11483395f70eSjsing * s->session - The ssl session has been setup. 11496f7f653bSjsing * s->hit - session reuse flag 1150661440b7Sjsing * s->hs.cipher - the new cipher to use. 11513395f70eSjsing */ 11523395f70eSjsing 11533395f70eSjsing /* Handles TLS extensions that we couldn't check earlier */ 11543395f70eSjsing if (ssl_check_clienthello_tlsext_late(s) <= 0) { 1155c9d7abb7Sbeck SSLerror(s, SSL_R_CLIENTHELLO_TLSEXT); 11563395f70eSjsing goto err; 11573395f70eSjsing } 11583395f70eSjsing 11596f1892dcSjsing ret = cookie_valid ? 2 : 1; 11606f1892dcSjsing 11613395f70eSjsing if (0) { 1162e0d5994dStb decode_err: 11633395f70eSjsing al = SSL_AD_DECODE_ERROR; 1164c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1165ba06b73eStb fatal_err: 11663395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, al); 11673395f70eSjsing } 11683395f70eSjsing err: 11693395f70eSjsing sk_SSL_CIPHER_free(ciphers); 11703395f70eSjsing 11713395f70eSjsing return (ret); 11723395f70eSjsing } 11733395f70eSjsing 1174eb1ff9efSjsing static int 1175f6d60a7aSjsing ssl3_send_dtls_hello_verify_request(SSL *s) 1176f6d60a7aSjsing { 1177f6d60a7aSjsing CBB cbb, verify, cookie; 1178f6d60a7aSjsing 1179f6d60a7aSjsing memset(&cbb, 0, sizeof(cbb)); 1180f6d60a7aSjsing 118102876cc3Sjsing if (s->s3->hs.state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A) { 11826f7f653bSjsing if (s->ctx->app_gen_cookie_cb == NULL || 11836f7f653bSjsing s->ctx->app_gen_cookie_cb(s, s->d1->cookie, 1184f19d9718Sjsing &(s->d1->cookie_len)) == 0) { 1185f6d60a7aSjsing SSLerror(s, ERR_R_INTERNAL_ERROR); 1186f6d60a7aSjsing return 0; 1187f6d60a7aSjsing } 1188f6d60a7aSjsing 11894ddbf855Sjsing /* 11904ddbf855Sjsing * Per RFC 6347 section 4.2.1, the HelloVerifyRequest should 11914ddbf855Sjsing * always contain DTLSv1.0 regardless of the version that is 11924ddbf855Sjsing * going to be negotiated. 11934ddbf855Sjsing */ 1194f6d60a7aSjsing if (!ssl3_handshake_msg_start(s, &cbb, &verify, 1195f6d60a7aSjsing DTLS1_MT_HELLO_VERIFY_REQUEST)) 1196f6d60a7aSjsing goto err; 11974ddbf855Sjsing if (!CBB_add_u16(&verify, DTLS1_VERSION)) 1198f6d60a7aSjsing goto err; 1199f6d60a7aSjsing if (!CBB_add_u8_length_prefixed(&verify, &cookie)) 1200f6d60a7aSjsing goto err; 1201f19d9718Sjsing if (!CBB_add_bytes(&cookie, s->d1->cookie, s->d1->cookie_len)) 1202f6d60a7aSjsing goto err; 1203f6d60a7aSjsing if (!ssl3_handshake_msg_finish(s, &cbb)) 1204f6d60a7aSjsing goto err; 1205f6d60a7aSjsing 120602876cc3Sjsing s->s3->hs.state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B; 1207f6d60a7aSjsing } 1208f6d60a7aSjsing 120902876cc3Sjsing /* s->s3->hs.state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */ 1210f6d60a7aSjsing return (ssl3_handshake_write(s)); 1211f6d60a7aSjsing 1212f6d60a7aSjsing err: 1213f6d60a7aSjsing CBB_cleanup(&cbb); 1214f6d60a7aSjsing 1215f6d60a7aSjsing return (-1); 1216f6d60a7aSjsing } 1217f6d60a7aSjsing 1218eb1ff9efSjsing static int 12193395f70eSjsing ssl3_send_server_hello(SSL *s) 12203395f70eSjsing { 1221b626ebe3Sjsing CBB cbb, server_hello, session_id; 1222b626ebe3Sjsing size_t sl; 12233395f70eSjsing 12243395f70eSjsing memset(&cbb, 0, sizeof(cbb)); 12253395f70eSjsing 122602876cc3Sjsing if (s->s3->hs.state == SSL3_ST_SW_SRVR_HELLO_A) { 1227312b4b14Sjsing if (!ssl3_handshake_msg_start(s, &cbb, &server_hello, 1228b626ebe3Sjsing SSL3_MT_SERVER_HELLO)) 12293395f70eSjsing goto err; 12303395f70eSjsing 1231b626ebe3Sjsing if (!CBB_add_u16(&server_hello, s->version)) 12323395f70eSjsing goto err; 1233b626ebe3Sjsing if (!CBB_add_bytes(&server_hello, s->s3->server_random, 12343395f70eSjsing sizeof(s->s3->server_random))) 12353395f70eSjsing goto err; 12363395f70eSjsing 12373395f70eSjsing /* 12383395f70eSjsing * There are several cases for the session ID to send 12393395f70eSjsing * back in the server hello: 12403395f70eSjsing * 12413395f70eSjsing * - For session reuse from the session cache, 12423395f70eSjsing * we send back the old session ID. 12433395f70eSjsing * - If stateless session reuse (using a session ticket) 12443395f70eSjsing * is successful, we send back the client's "session ID" 12453395f70eSjsing * (which doesn't actually identify the session). 12463395f70eSjsing * - If it is a new session, we send back the new 12473395f70eSjsing * session ID. 12483395f70eSjsing * - However, if we want the new session to be single-use, 12493395f70eSjsing * we send back a 0-length session ID. 12503395f70eSjsing * 12516f7f653bSjsing * s->hit is non-zero in either case of session reuse, 12523395f70eSjsing * so the following won't overwrite an ID that we're supposed 12533395f70eSjsing * to send back. 12543395f70eSjsing */ 12556f7f653bSjsing if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) 12566f7f653bSjsing && !s->hit) 12573395f70eSjsing s->session->session_id_length = 0; 12583395f70eSjsing 12593395f70eSjsing sl = s->session->session_id_length; 1260b626ebe3Sjsing if (sl > sizeof(s->session->session_id)) { 1261c9d7abb7Sbeck SSLerror(s, ERR_R_INTERNAL_ERROR); 12623395f70eSjsing goto err; 12633395f70eSjsing } 1264b626ebe3Sjsing if (!CBB_add_u8_length_prefixed(&server_hello, &session_id)) 12653395f70eSjsing goto err; 12663395f70eSjsing if (!CBB_add_bytes(&session_id, s->session->session_id, sl)) 12673395f70eSjsing goto err; 12683395f70eSjsing 12693395f70eSjsing /* Cipher suite. */ 1270*f4fe6251Sjsing if (!CBB_add_u16(&server_hello, s->s3->hs.cipher->value)) 12713395f70eSjsing goto err; 12723395f70eSjsing 1273b626ebe3Sjsing /* Compression method (null). */ 1274b626ebe3Sjsing if (!CBB_add_u8(&server_hello, 0)) 12753395f70eSjsing goto err; 12763395f70eSjsing 1277b626ebe3Sjsing /* TLS extensions */ 12789b8a142fStb if (!tlsext_server_build(s, SSL_TLSEXT_MSG_SH, &server_hello)) { 1279c9d7abb7Sbeck SSLerror(s, ERR_R_INTERNAL_ERROR); 12803395f70eSjsing goto err; 12813395f70eSjsing } 12823395f70eSjsing 1283312b4b14Sjsing if (!ssl3_handshake_msg_finish(s, &cbb)) 1284b626ebe3Sjsing goto err; 12853395f70eSjsing } 12863395f70eSjsing 12873395f70eSjsing /* SSL3_ST_SW_SRVR_HELLO_B */ 12883395f70eSjsing return (ssl3_handshake_write(s)); 12893395f70eSjsing 12903395f70eSjsing err: 12913395f70eSjsing CBB_cleanup(&cbb); 12923395f70eSjsing 12933395f70eSjsing return (-1); 12943395f70eSjsing } 12953395f70eSjsing 1296eb1ff9efSjsing static int 12973395f70eSjsing ssl3_send_server_done(SSL *s) 12983395f70eSjsing { 1299a9e43a46Sjsing CBB cbb, done; 1300a9e43a46Sjsing 1301a9e43a46Sjsing memset(&cbb, 0, sizeof(cbb)); 1302a9e43a46Sjsing 130302876cc3Sjsing if (s->s3->hs.state == SSL3_ST_SW_SRVR_DONE_A) { 1304312b4b14Sjsing if (!ssl3_handshake_msg_start(s, &cbb, &done, 1305a9e43a46Sjsing SSL3_MT_SERVER_DONE)) 1306a9e43a46Sjsing goto err; 1307312b4b14Sjsing if (!ssl3_handshake_msg_finish(s, &cbb)) 1308a9e43a46Sjsing goto err; 13093395f70eSjsing 131002876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_SRVR_DONE_B; 13113395f70eSjsing } 13123395f70eSjsing 13133395f70eSjsing /* SSL3_ST_SW_SRVR_DONE_B */ 13143395f70eSjsing return (ssl3_handshake_write(s)); 1315a9e43a46Sjsing 1316a9e43a46Sjsing err: 1317a9e43a46Sjsing CBB_cleanup(&cbb); 1318a9e43a46Sjsing 1319a9e43a46Sjsing return (-1); 13203395f70eSjsing } 13213395f70eSjsing 13222ddd511dSjsing static int 13233395f70eSjsing ssl3_send_server_kex_dhe(SSL *s, CBB *cbb) 13243395f70eSjsing { 1325aebe2672Sjsing int nid = NID_dhKeyAgreement; 13263395f70eSjsing 132702876cc3Sjsing tls_key_share_free(s->s3->hs.key_share); 132802876cc3Sjsing if ((s->s3->hs.key_share = tls_key_share_new_nid(nid)) == NULL) 132940050a94Sjsing goto err; 133040050a94Sjsing 1331c0b85757Sjsing if (s->cert->dhe_params_auto != 0) { 133240050a94Sjsing size_t key_bits; 133340050a94Sjsing 133440050a94Sjsing if ((key_bits = ssl_dhe_params_auto_key_bits(s)) == 0) { 1335c9d7abb7Sbeck SSLerror(s, ERR_R_INTERNAL_ERROR); 1336aebe2672Sjsing ssl3_send_alert(s, SSL3_AL_FATAL, 1337aebe2672Sjsing SSL_AD_INTERNAL_ERROR); 133840050a94Sjsing goto err; 1339aebe2672Sjsing } 134002876cc3Sjsing tls_key_share_set_key_bits(s->s3->hs.key_share, 1341aebe2672Sjsing key_bits); 134240050a94Sjsing } else { 1343c0b85757Sjsing DH *dh_params = s->cert->dhe_params; 134440050a94Sjsing 1345c0b85757Sjsing if (dh_params == NULL && s->cert->dhe_params_cb != NULL) 1346c0b85757Sjsing dh_params = s->cert->dhe_params_cb(s, 0, 134702876cc3Sjsing SSL_C_PKEYLENGTH(s->s3->hs.cipher)); 13483395f70eSjsing 134940050a94Sjsing if (dh_params == NULL) { 1350c9d7abb7Sbeck SSLerror(s, SSL_R_MISSING_TMP_DH_KEY); 1351aebe2672Sjsing ssl3_send_alert(s, SSL3_AL_FATAL, 1352aebe2672Sjsing SSL_AD_HANDSHAKE_FAILURE); 13533395f70eSjsing goto err; 13543395f70eSjsing } 13553395f70eSjsing 135602876cc3Sjsing if (!tls_key_share_set_dh_params(s->s3->hs.key_share, dh_params)) 1357203d15d2Sjsing goto err; 13583395f70eSjsing } 13593395f70eSjsing 136002876cc3Sjsing if (!tls_key_share_generate(s->s3->hs.key_share)) 13613395f70eSjsing goto err; 13623395f70eSjsing 136302876cc3Sjsing if (!tls_key_share_params(s->s3->hs.key_share, cbb)) 13643395f70eSjsing goto err; 136502876cc3Sjsing if (!tls_key_share_public(s->s3->hs.key_share, cbb)) 13663395f70eSjsing goto err; 13673395f70eSjsing 13682c1036d8Stb if (!tls_key_share_peer_security(s, s->s3->hs.key_share)) { 13692c1036d8Stb SSLerror(s, SSL_R_DH_KEY_TOO_SMALL); 13702c1036d8Stb ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 13712c1036d8Stb return 0; 13722c1036d8Stb } 13732c1036d8Stb 13749ccbb9b1Sjsing return 1; 13753395f70eSjsing 13763395f70eSjsing err: 13779ccbb9b1Sjsing return 0; 13783395f70eSjsing } 13793395f70eSjsing 13803395f70eSjsing static int 13813395f70eSjsing ssl3_send_server_kex_ecdhe(SSL *s, CBB *cbb) 13823395f70eSjsing { 1383aebe2672Sjsing CBB public; 13843395f70eSjsing int nid; 13853395f70eSjsing 1386c5270c5dStb if (!tls1_get_supported_group(s, &nid)) { 1387aebe2672Sjsing SSLerror(s, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); 1388aebe2672Sjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1389aebe2672Sjsing goto err; 1390aebe2672Sjsing } 13913395f70eSjsing 139202876cc3Sjsing tls_key_share_free(s->s3->hs.key_share); 139302876cc3Sjsing if ((s->s3->hs.key_share = tls_key_share_new_nid(nid)) == NULL) 1394aebe2672Sjsing goto err; 13953395f70eSjsing 139602876cc3Sjsing if (!tls_key_share_generate(s->s3->hs.key_share)) 1397aebe2672Sjsing goto err; 1398aebe2672Sjsing 1399aebe2672Sjsing /* 1400aebe2672Sjsing * ECC key exchange - see RFC 8422, section 5.4. 1401aebe2672Sjsing */ 1402aebe2672Sjsing if (!CBB_add_u8(cbb, NAMED_CURVE_TYPE)) 1403aebe2672Sjsing goto err; 140402876cc3Sjsing if (!CBB_add_u16(cbb, tls_key_share_group(s->s3->hs.key_share))) 1405aebe2672Sjsing goto err; 1406aebe2672Sjsing if (!CBB_add_u8_length_prefixed(cbb, &public)) 1407aebe2672Sjsing goto err; 140802876cc3Sjsing if (!tls_key_share_public(s->s3->hs.key_share, &public)) 1409aebe2672Sjsing goto err; 1410aebe2672Sjsing if (!CBB_flush(cbb)) 1411aebe2672Sjsing goto err; 1412aebe2672Sjsing 1413aebe2672Sjsing return 1; 1414aebe2672Sjsing 1415aebe2672Sjsing err: 1416aebe2672Sjsing return 0; 14173395f70eSjsing } 14183395f70eSjsing 1419eb1ff9efSjsing static int 14203395f70eSjsing ssl3_send_server_key_exchange(SSL *s) 14213395f70eSjsing { 1422bf68ec7fStb CBB cbb, cbb_signature, cbb_signed_params, server_kex; 1423bf68ec7fStb CBS params; 1424fbe97c86Sbeck const struct ssl_sigalg *sigalg = NULL; 1425bf68ec7fStb unsigned char *signed_params = NULL; 1426bf68ec7fStb size_t signed_params_len; 142729dfe984Sjsing unsigned char *signature = NULL; 1428bfb305e8Sbeck size_t signature_len = 0; 14293395f70eSjsing const EVP_MD *md = NULL; 14303395f70eSjsing unsigned long type; 1431e0f8f021Stb EVP_MD_CTX *md_ctx = NULL; 1432bfb305e8Sbeck EVP_PKEY_CTX *pctx; 143329dfe984Sjsing EVP_PKEY *pkey; 1434a7da1590Sjsing int al; 14353395f70eSjsing 14363395f70eSjsing memset(&cbb, 0, sizeof(cbb)); 1437bf68ec7fStb memset(&cbb_signed_params, 0, sizeof(cbb_signed_params)); 14383395f70eSjsing 1439e0f8f021Stb if ((md_ctx = EVP_MD_CTX_new()) == NULL) 1440e0f8f021Stb goto err; 144129dfe984Sjsing 144202876cc3Sjsing if (s->s3->hs.state == SSL3_ST_SW_KEY_EXCH_A) { 14433395f70eSjsing 1444312b4b14Sjsing if (!ssl3_handshake_msg_start(s, &cbb, &server_kex, 144529dfe984Sjsing SSL3_MT_SERVER_KEY_EXCHANGE)) 14463395f70eSjsing goto err; 14473395f70eSjsing 1448bf68ec7fStb if (!CBB_init(&cbb_signed_params, 0)) 144929dfe984Sjsing goto err; 145029dfe984Sjsing 1451bf68ec7fStb if (!CBB_add_bytes(&cbb_signed_params, s->s3->client_random, 1452bf68ec7fStb SSL3_RANDOM_SIZE)) { 1453bf68ec7fStb SSLerror(s, ERR_R_INTERNAL_ERROR); 1454bf68ec7fStb goto err; 1455bf68ec7fStb } 1456bf68ec7fStb if (!CBB_add_bytes(&cbb_signed_params, s->s3->server_random, 1457bf68ec7fStb SSL3_RANDOM_SIZE)) { 1458bf68ec7fStb SSLerror(s, ERR_R_INTERNAL_ERROR); 1459bf68ec7fStb goto err; 1460bf68ec7fStb } 1461bf68ec7fStb 146202876cc3Sjsing type = s->s3->hs.cipher->algorithm_mkey; 14633395f70eSjsing if (type & SSL_kDHE) { 1464bf68ec7fStb if (!ssl3_send_server_kex_dhe(s, &cbb_signed_params)) 14653395f70eSjsing goto err; 14663395f70eSjsing } else if (type & SSL_kECDHE) { 1467bf68ec7fStb if (!ssl3_send_server_kex_ecdhe(s, &cbb_signed_params)) 14683395f70eSjsing goto err; 14693395f70eSjsing } else { 14703395f70eSjsing al = SSL_AD_HANDSHAKE_FAILURE; 1471c9d7abb7Sbeck SSLerror(s, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); 1472ba06b73eStb goto fatal_err; 14733395f70eSjsing } 14743395f70eSjsing 1475bf68ec7fStb if (!CBB_finish(&cbb_signed_params, &signed_params, 1476bf68ec7fStb &signed_params_len)) 147729dfe984Sjsing goto err; 147829dfe984Sjsing 1479bf68ec7fStb CBS_init(¶ms, signed_params, signed_params_len); 1480bf68ec7fStb if (!CBS_skip(¶ms, 2 * SSL3_RANDOM_SIZE)) 1481bf68ec7fStb goto err; 1482bf68ec7fStb 1483bf68ec7fStb if (!CBB_add_bytes(&server_kex, CBS_data(¶ms), 1484bf68ec7fStb CBS_len(¶ms))) 14853395f70eSjsing goto err; 14863395f70eSjsing 1487a7da1590Sjsing /* Add signature unless anonymous. */ 148802876cc3Sjsing if (!(s->s3->hs.cipher->algorithm_auth & SSL_aNULL)) { 148902876cc3Sjsing if ((pkey = ssl_get_sign_pkey(s, s->s3->hs.cipher, 1490fbe97c86Sbeck &md, &sigalg)) == NULL) { 14913395f70eSjsing al = SSL_AD_DECODE_ERROR; 1492ba06b73eStb goto fatal_err; 14933395f70eSjsing } 149402876cc3Sjsing s->s3->hs.our_sigalg = sigalg; 14953395f70eSjsing 14963395f70eSjsing /* Send signature algorithm. */ 14973395f70eSjsing if (SSL_USE_SIGALGS(s)) { 1498fbe97c86Sbeck if (!CBB_add_u16(&server_kex, sigalg->value)) { 14993395f70eSjsing al = SSL_AD_INTERNAL_ERROR; 1500c9d7abb7Sbeck SSLerror(s, ERR_R_INTERNAL_ERROR); 1501ba06b73eStb goto fatal_err; 15023395f70eSjsing } 15033395f70eSjsing } 150429dfe984Sjsing 1505e0f8f021Stb if (!EVP_DigestSignInit(md_ctx, &pctx, md, NULL, pkey)) { 1506bfb305e8Sbeck SSLerror(s, ERR_R_EVP_LIB); 150729dfe984Sjsing goto err; 1508bfb305e8Sbeck } 1509844b8990Sbeck if ((sigalg->flags & SIGALG_FLAG_RSA_PSS) && 1510844b8990Sbeck (!EVP_PKEY_CTX_set_rsa_padding(pctx, 1511844b8990Sbeck RSA_PKCS1_PSS_PADDING) || 1512844b8990Sbeck !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1))) { 1513844b8990Sbeck SSLerror(s, ERR_R_EVP_LIB); 1514844b8990Sbeck goto err; 1515844b8990Sbeck } 1516bf68ec7fStb if (!EVP_DigestSign(md_ctx, NULL, &signature_len, 1517bf68ec7fStb signed_params, signed_params_len)) { 1518bfb305e8Sbeck SSLerror(s, ERR_R_EVP_LIB); 1519c65d0191Sjsing goto err; 1520bfb305e8Sbeck } 1521bfb305e8Sbeck if ((signature = calloc(1, signature_len)) == NULL) { 1522bfb305e8Sbeck SSLerror(s, ERR_R_MALLOC_FAILURE); 1523bfb305e8Sbeck goto err; 1524bfb305e8Sbeck } 1525bf68ec7fStb if (!EVP_DigestSign(md_ctx, signature, &signature_len, 1526bf68ec7fStb signed_params, signed_params_len)) { 1527c9d7abb7Sbeck SSLerror(s, ERR_R_EVP_LIB); 15283395f70eSjsing goto err; 15293395f70eSjsing } 153029dfe984Sjsing 153129dfe984Sjsing if (!CBB_add_u16_length_prefixed(&server_kex, 153229dfe984Sjsing &cbb_signature)) 153329dfe984Sjsing goto err; 153429dfe984Sjsing if (!CBB_add_bytes(&cbb_signature, signature, 153529dfe984Sjsing signature_len)) 153629dfe984Sjsing goto err; 15373395f70eSjsing } 15383395f70eSjsing 1539312b4b14Sjsing if (!ssl3_handshake_msg_finish(s, &cbb)) 154029dfe984Sjsing goto err; 15413395f70eSjsing 154202876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_KEY_EXCH_B; 154329dfe984Sjsing } 15443395f70eSjsing 1545e0f8f021Stb EVP_MD_CTX_free(md_ctx); 154629dfe984Sjsing free(signature); 1547bf68ec7fStb free(signed_params); 15483395f70eSjsing 15493395f70eSjsing return (ssl3_handshake_write(s)); 15503395f70eSjsing 1551ba06b73eStb fatal_err: 15523395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, al); 15533395f70eSjsing err: 1554bf68ec7fStb CBB_cleanup(&cbb_signed_params); 15553395f70eSjsing CBB_cleanup(&cbb); 1556e0f8f021Stb EVP_MD_CTX_free(md_ctx); 155729dfe984Sjsing free(signature); 1558bf68ec7fStb free(signed_params); 15593395f70eSjsing 15603395f70eSjsing return (-1); 15613395f70eSjsing } 15623395f70eSjsing 1563eb1ff9efSjsing static int 15643395f70eSjsing ssl3_send_certificate_request(SSL *s) 15653395f70eSjsing { 156627f1767cSjsing CBB cbb, cert_request, cert_types, sigalgs, cert_auth, dn; 15673395f70eSjsing STACK_OF(X509_NAME) *sk = NULL; 15683395f70eSjsing X509_NAME *name; 156927f1767cSjsing int i; 157027f1767cSjsing 157127f1767cSjsing /* 157227f1767cSjsing * Certificate Request - RFC 5246 section 7.4.4. 157327f1767cSjsing */ 157427f1767cSjsing 157527f1767cSjsing memset(&cbb, 0, sizeof(cbb)); 15763395f70eSjsing 157702876cc3Sjsing if (s->s3->hs.state == SSL3_ST_SW_CERT_REQ_A) { 1578312b4b14Sjsing if (!ssl3_handshake_msg_start(s, &cbb, &cert_request, 157927f1767cSjsing SSL3_MT_CERTIFICATE_REQUEST)) 158027f1767cSjsing goto err; 15813395f70eSjsing 158227f1767cSjsing if (!CBB_add_u8_length_prefixed(&cert_request, &cert_types)) 158327f1767cSjsing goto err; 158427f1767cSjsing if (!ssl3_get_req_cert_types(s, &cert_types)) 158527f1767cSjsing goto err; 15863395f70eSjsing 15873395f70eSjsing if (SSL_USE_SIGALGS(s)) { 1588c4b0c3ceSjsing if (!CBB_add_u16_length_prefixed(&cert_request, 1589c4b0c3ceSjsing &sigalgs)) 159027f1767cSjsing goto err; 1591678f3880Stb if (!ssl_sigalgs_build(s->s3->hs.negotiated_tls_version, 1592678f3880Stb &sigalgs, SSL_get_security_level(s))) 159327f1767cSjsing goto err; 15943395f70eSjsing } 15953395f70eSjsing 159627f1767cSjsing if (!CBB_add_u16_length_prefixed(&cert_request, &cert_auth)) 159727f1767cSjsing goto err; 15983395f70eSjsing 15993395f70eSjsing sk = SSL_get_client_CA_list(s); 16003395f70eSjsing for (i = 0; i < sk_X509_NAME_num(sk); i++) { 160127f1767cSjsing unsigned char *name_data; 160227f1767cSjsing size_t name_len; 160327f1767cSjsing 16043395f70eSjsing name = sk_X509_NAME_value(sk, i); 160527f1767cSjsing name_len = i2d_X509_NAME(name, NULL); 160627f1767cSjsing 160727f1767cSjsing if (!CBB_add_u16_length_prefixed(&cert_auth, &dn)) 160827f1767cSjsing goto err; 160927f1767cSjsing if (!CBB_add_space(&dn, &name_data, name_len)) 161027f1767cSjsing goto err; 161127f1767cSjsing if (i2d_X509_NAME(name, &name_data) != name_len) 16123395f70eSjsing goto err; 16133395f70eSjsing } 16143395f70eSjsing 1615312b4b14Sjsing if (!ssl3_handshake_msg_finish(s, &cbb)) 161627f1767cSjsing goto err; 16173395f70eSjsing 161802876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_CERT_REQ_B; 16193395f70eSjsing } 16203395f70eSjsing 16213395f70eSjsing /* SSL3_ST_SW_CERT_REQ_B */ 16223395f70eSjsing return (ssl3_handshake_write(s)); 162327f1767cSjsing 16243395f70eSjsing err: 162527f1767cSjsing CBB_cleanup(&cbb); 162627f1767cSjsing 16273395f70eSjsing return (-1); 16283395f70eSjsing } 16293395f70eSjsing 16303395f70eSjsing static int 1631b8f4d8feSjsing ssl3_get_client_kex_rsa(SSL *s, CBS *cbs) 16323395f70eSjsing { 1633b9e57b4bSjsing uint8_t fakepms[SSL_MAX_MASTER_KEY_LENGTH]; 1634b9e57b4bSjsing uint8_t *pms = NULL; 163511a84ef2Sjsing size_t pms_len = 0; 1636b9e57b4bSjsing size_t pad_len; 16373395f70eSjsing EVP_PKEY *pkey = NULL; 163811a84ef2Sjsing RSA *rsa = NULL; 1639b8f4d8feSjsing CBS enc_pms; 164011a84ef2Sjsing int decrypt_len; 1641b9e57b4bSjsing uint8_t mask; 1642b9e57b4bSjsing size_t i; 1643b9e57b4bSjsing int valid = 1; 1644b9e57b4bSjsing int ret = 0; 16453395f70eSjsing 1646b9e57b4bSjsing /* 1647b9e57b4bSjsing * Handle key exchange in the form of an RSA-Encrypted Premaster Secret 1648b9e57b4bSjsing * Message. See RFC 5246, section 7.4.7.1. 1649b9e57b4bSjsing */ 1650c5e6469dSjsing 1651b9e57b4bSjsing arc4random_buf(fakepms, sizeof(fakepms)); 1652b9e57b4bSjsing 1653b9e57b4bSjsing fakepms[0] = s->s3->hs.peer_legacy_version >> 8; 1654b9e57b4bSjsing fakepms[1] = s->s3->hs.peer_legacy_version & 0xff; 16553395f70eSjsing 165616933562Sjsing pkey = s->cert->pkeys[SSL_PKEY_RSA].privatekey; 16575c273ab3Stb if (pkey == NULL || (rsa = EVP_PKEY_get0_RSA(pkey)) == NULL) { 1658c9d7abb7Sbeck SSLerror(s, SSL_R_MISSING_RSA_CERTIFICATE); 1659b9e57b4bSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1660b9e57b4bSjsing goto err; 16613395f70eSjsing } 16623395f70eSjsing 1663b9e57b4bSjsing /* 1664b9e57b4bSjsing * The minimum size of an encrypted premaster secret is 11 bytes of 1665b9e57b4bSjsing * padding (00 02 <8 or more non-zero bytes> 00) (RFC 8017, section 1666b9e57b4bSjsing * 9.2) and 48 bytes of premaster secret (RFC 5246, section 7.4.7.1). 1667b9e57b4bSjsing * This means an RSA key size of at least 472 bits. 1668b9e57b4bSjsing */ 166911a84ef2Sjsing pms_len = RSA_size(rsa); 1670b9e57b4bSjsing if (pms_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) { 1671b9e57b4bSjsing SSLerror(s, SSL_R_DECRYPTION_FAILED); 1672b9e57b4bSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); 16733395f70eSjsing goto err; 167411a84ef2Sjsing } 1675b9e57b4bSjsing pad_len = pms_len - SSL_MAX_MASTER_KEY_LENGTH; 1676b9e57b4bSjsing 1677b9e57b4bSjsing if (!CBS_get_u16_length_prefixed(cbs, &enc_pms)) { 1678b9e57b4bSjsing SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1679b9e57b4bSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1680b9e57b4bSjsing goto err; 1681b9e57b4bSjsing } 1682b9e57b4bSjsing if (CBS_len(&enc_pms) != pms_len || CBS_len(cbs) != 0) { 1683b9e57b4bSjsing SSLerror(s, SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); 1684b9e57b4bSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1685b9e57b4bSjsing goto err; 1686b9e57b4bSjsing } 1687b9e57b4bSjsing 1688b9e57b4bSjsing if ((pms = calloc(1, pms_len)) == NULL) 1689b9e57b4bSjsing goto err; 16903395f70eSjsing 169111a84ef2Sjsing decrypt_len = RSA_private_decrypt(CBS_len(&enc_pms), CBS_data(&enc_pms), 1692b9e57b4bSjsing pms, rsa, RSA_NO_PADDING); 16933395f70eSjsing 1694b9e57b4bSjsing if (decrypt_len != pms_len) { 1695b9e57b4bSjsing SSLerror(s, SSL_R_DECRYPTION_FAILED); 1696b9e57b4bSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); 1697b9e57b4bSjsing goto err; 16983395f70eSjsing } 16993395f70eSjsing 17003395f70eSjsing /* 1701b9e57b4bSjsing * All processing from here on needs to avoid leaking any information 1702b9e57b4bSjsing * about the decrypted content, in order to prevent oracle attacks and 1703b9e57b4bSjsing * minimise timing attacks. 17043395f70eSjsing */ 17053395f70eSjsing 1706b9e57b4bSjsing /* Check padding - 00 02 <8 or more non-zero bytes> 00 */ 1707b9e57b4bSjsing valid &= crypto_ct_eq_u8(pms[0], 0x00); 1708b9e57b4bSjsing valid &= crypto_ct_eq_u8(pms[1], 0x02); 1709b9e57b4bSjsing for (i = 2; i < pad_len - 1; i++) 1710b9e57b4bSjsing valid &= crypto_ct_ne_u8(pms[i], 0x00); 1711b9e57b4bSjsing valid &= crypto_ct_eq_u8(pms[pad_len - 1], 0x00); 17123395f70eSjsing 1713b9e57b4bSjsing /* Ensure client version in premaster secret matches ClientHello version. */ 1714b9e57b4bSjsing valid &= crypto_ct_eq_u8(pms[pad_len + 0], s->s3->hs.peer_legacy_version >> 8); 1715b9e57b4bSjsing valid &= crypto_ct_eq_u8(pms[pad_len + 1], s->s3->hs.peer_legacy_version & 0xff); 1716b9e57b4bSjsing 1717b9e57b4bSjsing /* Use the premaster secret if padding is correct, if not use the fake. */ 1718b9e57b4bSjsing mask = crypto_ct_eq_mask_u8(valid, 1); 1719b9e57b4bSjsing for (i = 0; i < SSL_MAX_MASTER_KEY_LENGTH; i++) 1720b9e57b4bSjsing pms[i] = (pms[pad_len + i] & mask) | (fakepms[i] & ~mask); 1721b9e57b4bSjsing 1722b9e57b4bSjsing if (!tls12_derive_master_secret(s, pms, SSL_MAX_MASTER_KEY_LENGTH)) 1723155a0ed0Sjsing goto err; 17243395f70eSjsing 1725b9e57b4bSjsing ret = 1; 17263395f70eSjsing 17273395f70eSjsing err: 172811a84ef2Sjsing freezero(pms, pms_len); 172911a84ef2Sjsing 1730b9e57b4bSjsing return ret; 17313395f70eSjsing } 17323395f70eSjsing 17333395f70eSjsing static int 1734b8f4d8feSjsing ssl3_get_client_kex_dhe(SSL *s, CBS *cbs) 17353395f70eSjsing { 173640050a94Sjsing uint8_t *key = NULL; 173740050a94Sjsing size_t key_len = 0; 17387dd422f8Sjsing int decode_error, invalid_key; 17399ccbb9b1Sjsing int ret = 0; 17403395f70eSjsing 174102876cc3Sjsing if (s->s3->hs.key_share == NULL) { 1742c9d7abb7Sbeck SSLerror(s, SSL_R_MISSING_TMP_DH_KEY); 17437dd422f8Sjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 17443395f70eSjsing goto err; 17453395f70eSjsing } 17463395f70eSjsing 174702876cc3Sjsing if (!tls_key_share_peer_public(s->s3->hs.key_share, cbs, 17487dd422f8Sjsing &decode_error, &invalid_key)) { 17497dd422f8Sjsing if (decode_error) { 17507dd422f8Sjsing SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 17517dd422f8Sjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 17527dd422f8Sjsing } 175340050a94Sjsing goto err; 17547dd422f8Sjsing } 175540050a94Sjsing if (invalid_key) { 175640050a94Sjsing SSLerror(s, SSL_R_BAD_DH_PUB_KEY_LENGTH); 17577dd422f8Sjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); 1758279748f8Sjsing goto err; 1759279748f8Sjsing } 176040050a94Sjsing 176102876cc3Sjsing if (!tls_key_share_derive(s->s3->hs.key_share, &key, &key_len)) 176240050a94Sjsing goto err; 17633395f70eSjsing 1764155a0ed0Sjsing if (!tls12_derive_master_secret(s, key, key_len)) 1765155a0ed0Sjsing goto err; 17663395f70eSjsing 176740050a94Sjsing ret = 1; 17683395f70eSjsing 17693395f70eSjsing err: 177040050a94Sjsing freezero(key, key_len); 17713395f70eSjsing 17729ccbb9b1Sjsing return ret; 17733395f70eSjsing } 17743395f70eSjsing 17753395f70eSjsing static int 1776b8f4d8feSjsing ssl3_get_client_kex_ecdhe(SSL *s, CBS *cbs) 17773395f70eSjsing { 1778aebe2672Sjsing uint8_t *key = NULL; 1779aebe2672Sjsing size_t key_len = 0; 17807dd422f8Sjsing int decode_error; 1781aebe2672Sjsing CBS public; 1782aebe2672Sjsing int ret = 0; 17833395f70eSjsing 178402876cc3Sjsing if (s->s3->hs.key_share == NULL) { 1785aebe2672Sjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1786aebe2672Sjsing SSLerror(s, SSL_R_MISSING_TMP_DH_KEY); 1787aebe2672Sjsing goto err; 1788aebe2672Sjsing } 1789aebe2672Sjsing 17907dd422f8Sjsing if (!CBS_get_u8_length_prefixed(cbs, &public)) { 17917dd422f8Sjsing SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 17927dd422f8Sjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1793aebe2672Sjsing goto err; 17947dd422f8Sjsing } 179502876cc3Sjsing if (!tls_key_share_peer_public(s->s3->hs.key_share, &public, 17967dd422f8Sjsing &decode_error, NULL)) { 17977dd422f8Sjsing if (decode_error) { 17987dd422f8Sjsing SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 17997dd422f8Sjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 18007dd422f8Sjsing } 1801aebe2672Sjsing goto err; 18027dd422f8Sjsing } 1803aebe2672Sjsing 180402876cc3Sjsing if (!tls_key_share_derive(s->s3->hs.key_share, &key, &key_len)) 1805aebe2672Sjsing goto err; 1806aebe2672Sjsing 1807aebe2672Sjsing if (!tls12_derive_master_secret(s, key, key_len)) 1808aebe2672Sjsing goto err; 1809aebe2672Sjsing 1810aebe2672Sjsing ret = 1; 1811aebe2672Sjsing 1812aebe2672Sjsing err: 1813aebe2672Sjsing freezero(key, key_len); 1814aebe2672Sjsing 1815aebe2672Sjsing return ret; 18163395f70eSjsing } 18173395f70eSjsing 18183395f70eSjsing static int 18193395f70eSjsing ssl3_get_client_key_exchange(SSL *s) 18203395f70eSjsing { 18213395f70eSjsing unsigned long alg_k; 1822f2c3e964Sjsing int al, ret; 1823b8f4d8feSjsing CBS cbs; 18243395f70eSjsing 18253395f70eSjsing /* 2048 maxlen is a guess. How long a key does that permit? */ 1826f2c3e964Sjsing if ((ret = ssl3_get_message(s, SSL3_ST_SR_KEY_EXCH_A, 1827f2c3e964Sjsing SSL3_ST_SR_KEY_EXCH_B, SSL3_MT_CLIENT_KEY_EXCHANGE, 2048)) <= 0) 1828f2c3e964Sjsing return ret; 18293395f70eSjsing 18306f7f653bSjsing if (s->init_num < 0) 1831b8f4d8feSjsing goto err; 1832b8f4d8feSjsing 18336f7f653bSjsing CBS_init(&cbs, s->init_msg, s->init_num); 18343395f70eSjsing 183502876cc3Sjsing alg_k = s->s3->hs.cipher->algorithm_mkey; 18363395f70eSjsing 18373395f70eSjsing if (alg_k & SSL_kRSA) { 18389ccbb9b1Sjsing if (!ssl3_get_client_kex_rsa(s, &cbs)) 18393395f70eSjsing goto err; 18403395f70eSjsing } else if (alg_k & SSL_kDHE) { 18419ccbb9b1Sjsing if (!ssl3_get_client_kex_dhe(s, &cbs)) 18423395f70eSjsing goto err; 18433395f70eSjsing } else if (alg_k & SSL_kECDHE) { 18449ccbb9b1Sjsing if (!ssl3_get_client_kex_ecdhe(s, &cbs)) 18453395f70eSjsing goto err; 18463395f70eSjsing } else { 18473395f70eSjsing al = SSL_AD_HANDSHAKE_FAILURE; 1848c9d7abb7Sbeck SSLerror(s, SSL_R_UNKNOWN_CIPHER_TYPE); 1849ba06b73eStb goto fatal_err; 18503395f70eSjsing } 18513395f70eSjsing 1852b8f4d8feSjsing if (CBS_len(&cbs) != 0) { 1853b8f4d8feSjsing al = SSL_AD_DECODE_ERROR; 1854b8f4d8feSjsing SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1855ba06b73eStb goto fatal_err; 1856b8f4d8feSjsing } 1857b8f4d8feSjsing 18583395f70eSjsing return (1); 18593395f70eSjsing 1860ba06b73eStb fatal_err: 18613395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, al); 18623395f70eSjsing err: 18633395f70eSjsing return (-1); 18643395f70eSjsing } 18653395f70eSjsing 1866eb1ff9efSjsing static int 18673395f70eSjsing ssl3_get_cert_verify(SSL *s) 18683395f70eSjsing { 186994139391Sjsing CBS cbs, signature; 18706428ecd5Sbcook const struct ssl_sigalg *sigalg = NULL; 187168cc6f9aSjsing uint16_t sigalg_value = SIGALG_NONE; 187215b5e1ecSjsing EVP_PKEY *pkey; 1873666c9986Sjsing X509 *peer_cert = NULL; 1874e0f8f021Stb EVP_MD_CTX *mctx = NULL; 1875f2c3e964Sjsing int al, verify; 1876cef855dcSjsing const unsigned char *hdata; 1877cef855dcSjsing size_t hdatalen; 187894139391Sjsing int type = 0; 1879f2c3e964Sjsing int ret; 188094139391Sjsing 1881f2c3e964Sjsing if ((ret = ssl3_get_message(s, SSL3_ST_SR_CERT_VRFY_A, 1882f2c3e964Sjsing SSL3_ST_SR_CERT_VRFY_B, -1, SSL3_RT_MAX_PLAIN_LENGTH)) <= 0) 1883f2c3e964Sjsing return ret; 18843395f70eSjsing 1885f2c3e964Sjsing ret = 0; 1886f2c3e964Sjsing 18876f7f653bSjsing if (s->init_num < 0) 188894139391Sjsing goto err; 188994139391Sjsing 1890e0f8f021Stb if ((mctx = EVP_MD_CTX_new()) == NULL) 1891e0f8f021Stb goto err; 1892e0f8f021Stb 18936f7f653bSjsing CBS_init(&cbs, s->init_msg, s->init_num); 189494139391Sjsing 1895666c9986Sjsing peer_cert = s->session->peer_cert; 189615b5e1ecSjsing pkey = X509_get0_pubkey(peer_cert); 1897666c9986Sjsing type = X509_certificate_type(peer_cert, pkey); 18983395f70eSjsing 189902876cc3Sjsing if (s->s3->hs.tls12.message_type != SSL3_MT_CERTIFICATE_VERIFY) { 190002876cc3Sjsing s->s3->hs.tls12.reuse_message = 1; 1901666c9986Sjsing if (peer_cert != NULL) { 19023395f70eSjsing al = SSL_AD_UNEXPECTED_MESSAGE; 1903c9d7abb7Sbeck SSLerror(s, SSL_R_MISSING_VERIFY_MESSAGE); 1904ba06b73eStb goto fatal_err; 19053395f70eSjsing } 19063395f70eSjsing ret = 1; 19073395f70eSjsing goto end; 19083395f70eSjsing } 19093395f70eSjsing 1910666c9986Sjsing if (peer_cert == NULL) { 1911c9d7abb7Sbeck SSLerror(s, SSL_R_NO_CLIENT_CERT_RECEIVED); 19123395f70eSjsing al = SSL_AD_UNEXPECTED_MESSAGE; 1913ba06b73eStb goto fatal_err; 19143395f70eSjsing } 19153395f70eSjsing 19163395f70eSjsing if (!(type & EVP_PKT_SIGN)) { 1917c9d7abb7Sbeck SSLerror(s, SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); 19183395f70eSjsing al = SSL_AD_ILLEGAL_PARAMETER; 1919ba06b73eStb goto fatal_err; 19203395f70eSjsing } 19213395f70eSjsing 192202876cc3Sjsing if (s->s3->change_cipher_spec) { 1923c9d7abb7Sbeck SSLerror(s, SSL_R_CCS_RECEIVED_EARLY); 19243395f70eSjsing al = SSL_AD_UNEXPECTED_MESSAGE; 1925ba06b73eStb goto fatal_err; 19263395f70eSjsing } 19273395f70eSjsing 192868cc6f9aSjsing if (SSL_USE_SIGALGS(s)) { 192968cc6f9aSjsing if (!CBS_get_u16(&cbs, &sigalg_value)) 193068cc6f9aSjsing goto decode_err; 193168cc6f9aSjsing } 19323ad5c273Stb if (!CBS_get_u16_length_prefixed(&cbs, &signature)) 193394139391Sjsing goto err; 19343ad5c273Stb if (CBS_len(&cbs) != 0) { 19353ad5c273Stb al = SSL_AD_DECODE_ERROR; 19363ad5c273Stb SSLerror(s, SSL_R_EXTRA_DATA_IN_MESSAGE); 1937ba06b73eStb goto fatal_err; 19383ad5c273Stb } 193968cc6f9aSjsing 194068cc6f9aSjsing if (CBS_len(&signature) > EVP_PKEY_size(pkey)) { 194168cc6f9aSjsing SSLerror(s, SSL_R_WRONG_SIGNATURE_SIZE); 194268cc6f9aSjsing al = SSL_AD_DECODE_ERROR; 194368cc6f9aSjsing goto fatal_err; 194468cc6f9aSjsing } 194568cc6f9aSjsing 194668cc6f9aSjsing if ((sigalg = ssl_sigalg_for_peer(s, pkey, 194768cc6f9aSjsing sigalg_value)) == NULL) { 194868cc6f9aSjsing al = SSL_AD_DECODE_ERROR; 194968cc6f9aSjsing goto fatal_err; 19503ad5c273Stb } 195102876cc3Sjsing s->s3->hs.peer_sigalg = sigalg; 19523ad5c273Stb 19533395f70eSjsing if (SSL_USE_SIGALGS(s)) { 19543ad5c273Stb EVP_PKEY_CTX *pctx; 19553395f70eSjsing 1956cef855dcSjsing if (!tls1_transcript_data(s, &hdata, &hdatalen)) { 1957c9d7abb7Sbeck SSLerror(s, ERR_R_INTERNAL_ERROR); 19583395f70eSjsing al = SSL_AD_INTERNAL_ERROR; 1959ba06b73eStb goto fatal_err; 19603395f70eSjsing } 1961e0f8f021Stb if (!EVP_DigestVerifyInit(mctx, &pctx, sigalg->md(), 196268cc6f9aSjsing NULL, pkey)) { 1963c9d7abb7Sbeck SSLerror(s, ERR_R_EVP_LIB); 19643395f70eSjsing al = SSL_AD_INTERNAL_ERROR; 1965ba06b73eStb goto fatal_err; 19663395f70eSjsing } 1967844b8990Sbeck if ((sigalg->flags & SIGALG_FLAG_RSA_PSS) && 196868cc6f9aSjsing (!EVP_PKEY_CTX_set_rsa_padding(pctx, 196968cc6f9aSjsing RSA_PKCS1_PSS_PADDING) || 1970844b8990Sbeck !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1))) { 1971844b8990Sbeck al = SSL_AD_INTERNAL_ERROR; 1972ba06b73eStb goto fatal_err; 1973844b8990Sbeck } 197480a887b4Stb if (EVP_DigestVerify(mctx, CBS_data(&signature), 197580a887b4Stb CBS_len(&signature), hdata, hdatalen) <= 0) { 1976bfb305e8Sbeck SSLerror(s, ERR_R_EVP_LIB); 1977bfb305e8Sbeck al = SSL_AD_INTERNAL_ERROR; 1978ba06b73eStb goto fatal_err; 1979bfb305e8Sbeck } 19805c273ab3Stb } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) { 19815c273ab3Stb RSA *rsa; 19825c273ab3Stb 19835c273ab3Stb if ((rsa = EVP_PKEY_get0_RSA(pkey)) == NULL) { 19845c273ab3Stb al = SSL_AD_INTERNAL_ERROR; 19855c273ab3Stb SSLerror(s, ERR_R_EVP_LIB); 19865c273ab3Stb goto fatal_err; 19875c273ab3Stb } 198802876cc3Sjsing verify = RSA_verify(NID_md5_sha1, s->s3->hs.tls12.cert_verify, 198994139391Sjsing MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, CBS_data(&signature), 19905c273ab3Stb CBS_len(&signature), rsa); 199194139391Sjsing if (verify < 0) { 19923395f70eSjsing al = SSL_AD_DECRYPT_ERROR; 1993c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_RSA_DECRYPT); 1994ba06b73eStb goto fatal_err; 19953395f70eSjsing } 199694139391Sjsing if (verify == 0) { 19973395f70eSjsing al = SSL_AD_DECRYPT_ERROR; 1998c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_RSA_SIGNATURE); 1999ba06b73eStb goto fatal_err; 20003395f70eSjsing } 20015c273ab3Stb } else if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) { 20025c273ab3Stb EC_KEY *eckey; 20035c273ab3Stb 20045c273ab3Stb if ((eckey = EVP_PKEY_get0_EC_KEY(pkey)) == NULL) { 20055c273ab3Stb al = SSL_AD_INTERNAL_ERROR; 20065c273ab3Stb SSLerror(s, ERR_R_EVP_LIB); 20075c273ab3Stb goto fatal_err; 20085c273ab3Stb } 20097aaded86Stb verify = ECDSA_verify(0, 201002876cc3Sjsing &(s->s3->hs.tls12.cert_verify[MD5_DIGEST_LENGTH]), 201194139391Sjsing SHA_DIGEST_LENGTH, CBS_data(&signature), 20125c273ab3Stb CBS_len(&signature), eckey); 201394139391Sjsing if (verify <= 0) { 20143395f70eSjsing al = SSL_AD_DECRYPT_ERROR; 2015c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_ECDSA_SIGNATURE); 2016ba06b73eStb goto fatal_err; 20173395f70eSjsing } 20183ad5c273Stb } else { 2019c9d7abb7Sbeck SSLerror(s, ERR_R_INTERNAL_ERROR); 20203395f70eSjsing al = SSL_AD_UNSUPPORTED_CERTIFICATE; 2021ba06b73eStb goto fatal_err; 20223395f70eSjsing } 20233395f70eSjsing 20243395f70eSjsing ret = 1; 20253395f70eSjsing if (0) { 2026e0d5994dStb decode_err: 20273395f70eSjsing al = SSL_AD_DECODE_ERROR; 2028c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 2029ba06b73eStb fatal_err: 20303395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, al); 20313395f70eSjsing } 20323395f70eSjsing end: 2033cef855dcSjsing tls1_transcript_free(s); 203494139391Sjsing err: 2035e0f8f021Stb EVP_MD_CTX_free(mctx); 203615b5e1ecSjsing 20373395f70eSjsing return (ret); 20383395f70eSjsing } 20393395f70eSjsing 2040eb1ff9efSjsing static int 20413395f70eSjsing ssl3_get_client_certificate(SSL *s) 20423395f70eSjsing { 2043a5c907f4Sjsing CBS cbs, cert_list, cert_data; 2044a5c907f4Sjsing STACK_OF(X509) *certs = NULL; 2045a5c907f4Sjsing X509 *cert = NULL; 2046a5c907f4Sjsing const uint8_t *p; 2047a5c907f4Sjsing int al, ret; 20483395f70eSjsing 2049f2c3e964Sjsing if ((ret = ssl3_get_message(s, SSL3_ST_SR_CERT_A, SSL3_ST_SR_CERT_B, 20506f7f653bSjsing -1, s->max_cert_list)) <= 0) 2051f2c3e964Sjsing return ret; 2052f2c3e964Sjsing 2053f2c3e964Sjsing ret = -1; 20543395f70eSjsing 205502876cc3Sjsing if (s->s3->hs.tls12.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) { 20563395f70eSjsing if ((s->verify_mode & SSL_VERIFY_PEER) && 20573395f70eSjsing (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { 2058c9d7abb7Sbeck SSLerror(s, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 20593395f70eSjsing al = SSL_AD_HANDSHAKE_FAILURE; 2060ba06b73eStb goto fatal_err; 20613395f70eSjsing } 2062ad618767Sjsing 2063ad618767Sjsing /* 2064ad618767Sjsing * If we asked for a client certificate and the client has none, 2065ad618767Sjsing * it must respond with a certificate list of length zero. 2066ad618767Sjsing */ 2067a5c907f4Sjsing if (s->s3->hs.tls12.cert_request != 0) { 2068a5c907f4Sjsing SSLerror(s, SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST); 20693395f70eSjsing al = SSL_AD_UNEXPECTED_MESSAGE; 2070ba06b73eStb goto fatal_err; 20713395f70eSjsing } 207202876cc3Sjsing s->s3->hs.tls12.reuse_message = 1; 20733395f70eSjsing return (1); 20743395f70eSjsing } 20753395f70eSjsing 207602876cc3Sjsing if (s->s3->hs.tls12.message_type != SSL3_MT_CERTIFICATE) { 20773395f70eSjsing al = SSL_AD_UNEXPECTED_MESSAGE; 2078c9d7abb7Sbeck SSLerror(s, SSL_R_WRONG_MESSAGE_TYPE); 2079ba06b73eStb goto fatal_err; 20803395f70eSjsing } 20813395f70eSjsing 20826f7f653bSjsing if (s->init_num < 0) 2083e0d5994dStb goto decode_err; 20843395f70eSjsing 20856f7f653bSjsing CBS_init(&cbs, s->init_msg, s->init_num); 20863395f70eSjsing 2087a5c907f4Sjsing if (!CBS_get_u24_length_prefixed(&cbs, &cert_list)) 2088a5c907f4Sjsing goto decode_err; 2089a5c907f4Sjsing if (CBS_len(&cbs) != 0) 2090e0d5994dStb goto decode_err; 20913395f70eSjsing 20923395f70eSjsing /* 2093a5c907f4Sjsing * A TLS client must send an empty certificate list, if no suitable 2094a5c907f4Sjsing * certificate is available (rather than omitting the Certificate 2095a5c907f4Sjsing * handshake message) - see RFC 5246 section 7.4.6. 20963395f70eSjsing */ 2097a5c907f4Sjsing if (CBS_len(&cert_list) == 0) { 20983395f70eSjsing if ((s->verify_mode & SSL_VERIFY_PEER) && 20993395f70eSjsing (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { 2100c9d7abb7Sbeck SSLerror(s, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 21013395f70eSjsing al = SSL_AD_HANDSHAKE_FAILURE; 2102ba06b73eStb goto fatal_err; 21033395f70eSjsing } 2104cef855dcSjsing /* No client certificate so free transcript. */ 2105cef855dcSjsing tls1_transcript_free(s); 2106a5c907f4Sjsing goto done; 2107a5c907f4Sjsing } 2108a5c907f4Sjsing 2109a5c907f4Sjsing if ((certs = sk_X509_new_null()) == NULL) { 2110a5c907f4Sjsing SSLerror(s, ERR_R_MALLOC_FAILURE); 2111a5c907f4Sjsing goto err; 2112a5c907f4Sjsing } 2113a5c907f4Sjsing 2114a5c907f4Sjsing while (CBS_len(&cert_list) > 0) { 2115a5c907f4Sjsing if (!CBS_get_u24_length_prefixed(&cert_list, &cert_data)) 2116a5c907f4Sjsing goto decode_err; 2117a5c907f4Sjsing p = CBS_data(&cert_data); 2118a5c907f4Sjsing if ((cert = d2i_X509(NULL, &p, CBS_len(&cert_data))) == NULL) { 2119a5c907f4Sjsing SSLerror(s, ERR_R_ASN1_LIB); 2120a5c907f4Sjsing goto err; 2121a5c907f4Sjsing } 2122a5c907f4Sjsing if (p != CBS_data(&cert_data) + CBS_len(&cert_data)) 2123a5c907f4Sjsing goto decode_err; 2124a5c907f4Sjsing if (!sk_X509_push(certs, cert)) { 2125a5c907f4Sjsing SSLerror(s, ERR_R_MALLOC_FAILURE); 2126a5c907f4Sjsing goto err; 2127a5c907f4Sjsing } 2128a5c907f4Sjsing cert = NULL; 2129a5c907f4Sjsing } 2130a5c907f4Sjsing 2131a5c907f4Sjsing if (ssl_verify_cert_chain(s, certs) <= 0) { 21323395f70eSjsing al = ssl_verify_alarm_type(s->verify_result); 2133c9d7abb7Sbeck SSLerror(s, SSL_R_NO_CERTIFICATE_RETURNED); 2134ba06b73eStb goto fatal_err; 21353395f70eSjsing } 21367cf55da3Sjsing s->session->verify_result = s->verify_result; 2137ad618767Sjsing ERR_clear_error(); 2138ad618767Sjsing 2139ad618767Sjsing if (!tls_process_peer_certs(s, certs)) 2140ad618767Sjsing goto err; 21417cf55da3Sjsing 2142a5c907f4Sjsing done: 21433395f70eSjsing ret = 1; 21443395f70eSjsing if (0) { 2145e0d5994dStb decode_err: 21463395f70eSjsing al = SSL_AD_DECODE_ERROR; 2147c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 2148ba06b73eStb fatal_err: 21493395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, al); 21503395f70eSjsing } 21513395f70eSjsing err: 2152a5c907f4Sjsing sk_X509_pop_free(certs, X509_free); 2153a5c907f4Sjsing X509_free(cert); 21543395f70eSjsing 21553395f70eSjsing return (ret); 21563395f70eSjsing } 21573395f70eSjsing 2158eb1ff9efSjsing static int 21593395f70eSjsing ssl3_send_server_certificate(SSL *s) 21603395f70eSjsing { 21613395f70eSjsing CBB cbb, server_cert; 2162ef36d1f9Sjsing SSL_CERT_PKEY *cpk; 21633395f70eSjsing 21643395f70eSjsing /* 21653395f70eSjsing * Server Certificate - RFC 5246, section 7.4.2. 21663395f70eSjsing */ 21673395f70eSjsing 21683395f70eSjsing memset(&cbb, 0, sizeof(cbb)); 21693395f70eSjsing 217002876cc3Sjsing if (s->s3->hs.state == SSL3_ST_SW_CERT_A) { 21710f99fabaSjsing if ((cpk = ssl_get_server_send_pkey(s)) == NULL) { 2172c9d7abb7Sbeck SSLerror(s, ERR_R_INTERNAL_ERROR); 21733395f70eSjsing return (0); 21743395f70eSjsing } 21753395f70eSjsing 2176312b4b14Sjsing if (!ssl3_handshake_msg_start(s, &cbb, &server_cert, 21773395f70eSjsing SSL3_MT_CERTIFICATE)) 21783395f70eSjsing goto err; 21790f99fabaSjsing if (!ssl3_output_cert_chain(s, &server_cert, cpk)) 21803395f70eSjsing goto err; 2181312b4b14Sjsing if (!ssl3_handshake_msg_finish(s, &cbb)) 21823395f70eSjsing goto err; 21833395f70eSjsing 218402876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_CERT_B; 21853395f70eSjsing } 21863395f70eSjsing 21873395f70eSjsing /* SSL3_ST_SW_CERT_B */ 21883395f70eSjsing return (ssl3_handshake_write(s)); 21893395f70eSjsing 21903395f70eSjsing err: 21913395f70eSjsing CBB_cleanup(&cbb); 21923395f70eSjsing 21933395f70eSjsing return (0); 21943395f70eSjsing } 21953395f70eSjsing 21963395f70eSjsing /* send a new session ticket (not necessarily for a new session) */ 2197eb1ff9efSjsing static int 21983395f70eSjsing ssl3_send_newsession_ticket(SSL *s) 21993395f70eSjsing { 2200183d400cSjsing CBB cbb, session_ticket, ticket; 220112fe7be0Sjsing SSL_CTX *tctx = s->initial_ctx; 220212fe7be0Sjsing size_t enc_session_len, enc_session_max_len, hmac_len; 220312fe7be0Sjsing size_t session_len = 0; 220412fe7be0Sjsing unsigned char *enc_session = NULL, *session = NULL; 220512fe7be0Sjsing unsigned char iv[EVP_MAX_IV_LENGTH]; 220612fe7be0Sjsing unsigned char key_name[16]; 220712fe7be0Sjsing unsigned char *hmac; 22083395f70eSjsing unsigned int hlen; 2209e0f8f021Stb EVP_CIPHER_CTX *ctx = NULL; 2210e0f8f021Stb HMAC_CTX *hctx = NULL; 221156b1ac2fStb int iv_len, len; 221212fe7be0Sjsing 221312fe7be0Sjsing /* 221412fe7be0Sjsing * New Session Ticket - RFC 5077, section 3.3. 221512fe7be0Sjsing */ 22163395f70eSjsing 22172a6cb758Stb memset(&cbb, 0, sizeof(cbb)); 22182a6cb758Stb 2219e0f8f021Stb if ((ctx = EVP_CIPHER_CTX_new()) == NULL) 2220e0f8f021Stb goto err; 2221e0f8f021Stb if ((hctx = HMAC_CTX_new()) == NULL) 2222e0f8f021Stb goto err; 2223183d400cSjsing 222402876cc3Sjsing if (s->s3->hs.state == SSL3_ST_SW_SESSION_TICKET_A) { 2225312b4b14Sjsing if (!ssl3_handshake_msg_start(s, &cbb, &session_ticket, 2226183d400cSjsing SSL3_MT_NEWSESSION_TICKET)) 2227183d400cSjsing goto err; 2228183d400cSjsing 222912fe7be0Sjsing if (!SSL_SESSION_ticket(s->session, &session, &session_len)) 22303395f70eSjsing goto err; 223112fe7be0Sjsing if (session_len > 0xffff) 22323395f70eSjsing goto err; 22333395f70eSjsing 22343395f70eSjsing /* 223512fe7be0Sjsing * Initialize HMAC and cipher contexts. If callback is present 223612fe7be0Sjsing * it does all the work, otherwise use generated values from 223712fe7be0Sjsing * parent context. 22383395f70eSjsing */ 22396f7f653bSjsing if (tctx->tlsext_ticket_key_cb != NULL) { 22406f7f653bSjsing if (tctx->tlsext_ticket_key_cb(s, 2241e0f8f021Stb key_name, iv, ctx, hctx, 1) < 0) 22423395f70eSjsing goto err; 22433395f70eSjsing } else { 22443395f70eSjsing arc4random_buf(iv, 16); 2245e0f8f021Stb EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, 22466f7f653bSjsing tctx->tlsext_tick_aes_key, iv); 22476f7f653bSjsing HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key, 224818e023dcSjsing 16, EVP_sha256(), NULL); 22496f7f653bSjsing memcpy(key_name, tctx->tlsext_tick_key_name, 16); 22503395f70eSjsing } 22513395f70eSjsing 225212fe7be0Sjsing /* Encrypt the session state. */ 225312fe7be0Sjsing enc_session_max_len = session_len + EVP_MAX_BLOCK_LENGTH; 225412fe7be0Sjsing if ((enc_session = calloc(1, enc_session_max_len)) == NULL) 2255183d400cSjsing goto err; 225612fe7be0Sjsing enc_session_len = 0; 2257e0f8f021Stb if (!EVP_EncryptUpdate(ctx, enc_session, &len, session, 225812fe7be0Sjsing session_len)) 2259183d400cSjsing goto err; 226012fe7be0Sjsing enc_session_len += len; 2261e0f8f021Stb if (!EVP_EncryptFinal_ex(ctx, enc_session + enc_session_len, 226212fe7be0Sjsing &len)) 2263ca930e64Sjsing goto err; 226412fe7be0Sjsing enc_session_len += len; 2265ca930e64Sjsing 226612fe7be0Sjsing if (enc_session_len > enc_session_max_len) 2267183d400cSjsing goto err; 2268183d400cSjsing 2269183d400cSjsing /* Generate the HMAC. */ 2270e0f8f021Stb if (!HMAC_Update(hctx, key_name, sizeof(key_name))) 2271183d400cSjsing goto err; 2272e0f8f021Stb if (!HMAC_Update(hctx, iv, EVP_CIPHER_CTX_iv_length(ctx))) 2273183d400cSjsing goto err; 2274e0f8f021Stb if (!HMAC_Update(hctx, enc_session, enc_session_len)) 2275183d400cSjsing goto err; 2276183d400cSjsing 2277e0f8f021Stb if ((hmac_len = HMAC_size(hctx)) <= 0) 2278183d400cSjsing goto err; 2279183d400cSjsing 22803395f70eSjsing /* 22813395f70eSjsing * Ticket lifetime hint (advisory only): 22823395f70eSjsing * We leave this unspecified for resumed session 22833395f70eSjsing * (for simplicity), and guess that tickets for new 22843395f70eSjsing * sessions will live as long as their sessions. 22853395f70eSjsing */ 2286183d400cSjsing if (!CBB_add_u32(&session_ticket, 22876f7f653bSjsing s->hit ? 0 : s->session->timeout)) 2288183d400cSjsing goto err; 22893395f70eSjsing 2290183d400cSjsing if (!CBB_add_u16_length_prefixed(&session_ticket, &ticket)) 2291183d400cSjsing goto err; 2292183d400cSjsing if (!CBB_add_bytes(&ticket, key_name, sizeof(key_name))) 2293183d400cSjsing goto err; 229456b1ac2fStb if ((iv_len = EVP_CIPHER_CTX_iv_length(ctx)) < 0) 229556b1ac2fStb goto err; 229656b1ac2fStb if (!CBB_add_bytes(&ticket, iv, iv_len)) 2297183d400cSjsing goto err; 229812fe7be0Sjsing if (!CBB_add_bytes(&ticket, enc_session, enc_session_len)) 2299183d400cSjsing goto err; 2300183d400cSjsing if (!CBB_add_space(&ticket, &hmac, hmac_len)) 2301183d400cSjsing goto err; 23023395f70eSjsing 2303e0f8f021Stb if (!HMAC_Final(hctx, hmac, &hlen)) 2304183d400cSjsing goto err; 230512fe7be0Sjsing if (hlen != hmac_len) 230612fe7be0Sjsing goto err; 23073395f70eSjsing 2308312b4b14Sjsing if (!ssl3_handshake_msg_finish(s, &cbb)) 2309183d400cSjsing goto err; 23103395f70eSjsing 231102876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_SESSION_TICKET_B; 23123395f70eSjsing } 23133395f70eSjsing 2314e0f8f021Stb EVP_CIPHER_CTX_free(ctx); 2315e0f8f021Stb HMAC_CTX_free(hctx); 231612fe7be0Sjsing freezero(session, session_len); 231712fe7be0Sjsing free(enc_session); 2318183d400cSjsing 23193395f70eSjsing /* SSL3_ST_SW_SESSION_TICKET_B */ 23203395f70eSjsing return (ssl3_handshake_write(s)); 23213395f70eSjsing 23223395f70eSjsing err: 2323183d400cSjsing CBB_cleanup(&cbb); 2324e0f8f021Stb EVP_CIPHER_CTX_free(ctx); 2325e0f8f021Stb HMAC_CTX_free(hctx); 232612fe7be0Sjsing freezero(session, session_len); 232712fe7be0Sjsing free(enc_session); 23283395f70eSjsing 23293395f70eSjsing return (-1); 23303395f70eSjsing } 23313395f70eSjsing 2332eb1ff9efSjsing static int 23333395f70eSjsing ssl3_send_cert_status(SSL *s) 23343395f70eSjsing { 2335a9e43a46Sjsing CBB cbb, certstatus, ocspresp; 2336a9e43a46Sjsing 2337a9e43a46Sjsing memset(&cbb, 0, sizeof(cbb)); 23383395f70eSjsing 233902876cc3Sjsing if (s->s3->hs.state == SSL3_ST_SW_CERT_STATUS_A) { 2340312b4b14Sjsing if (!ssl3_handshake_msg_start(s, &cbb, &certstatus, 2341a9e43a46Sjsing SSL3_MT_CERTIFICATE_STATUS)) 2342a9e43a46Sjsing goto err; 2343a9e43a46Sjsing if (!CBB_add_u8(&certstatus, s->tlsext_status_type)) 2344a9e43a46Sjsing goto err; 2345a9e43a46Sjsing if (!CBB_add_u24_length_prefixed(&certstatus, &ocspresp)) 2346a9e43a46Sjsing goto err; 23476f7f653bSjsing if (!CBB_add_bytes(&ocspresp, s->tlsext_ocsp_resp, 23486f7f653bSjsing s->tlsext_ocsp_resp_len)) 2349a9e43a46Sjsing goto err; 2350312b4b14Sjsing if (!ssl3_handshake_msg_finish(s, &cbb)) 2351a9e43a46Sjsing goto err; 23523395f70eSjsing 235302876cc3Sjsing s->s3->hs.state = SSL3_ST_SW_CERT_STATUS_B; 23543395f70eSjsing } 23553395f70eSjsing 23563395f70eSjsing /* SSL3_ST_SW_CERT_STATUS_B */ 23573395f70eSjsing return (ssl3_handshake_write(s)); 2358a9e43a46Sjsing 2359a9e43a46Sjsing err: 2360a9e43a46Sjsing CBB_cleanup(&cbb); 2361a9e43a46Sjsing 2362a9e43a46Sjsing return (-1); 23633395f70eSjsing } 2364eb1ff9efSjsing 2365eb1ff9efSjsing static int 2366eb1ff9efSjsing ssl3_send_server_change_cipher_spec(SSL *s) 2367eb1ff9efSjsing { 2368eb1ff9efSjsing size_t outlen; 2369eb1ff9efSjsing CBB cbb; 2370eb1ff9efSjsing 2371eb1ff9efSjsing memset(&cbb, 0, sizeof(cbb)); 2372eb1ff9efSjsing 2373eb1ff9efSjsing if (s->s3->hs.state == SSL3_ST_SW_CHANGE_A) { 23746f7f653bSjsing if (!CBB_init_fixed(&cbb, s->init_buf->data, 23756f7f653bSjsing s->init_buf->length)) 2376eb1ff9efSjsing goto err; 2377eb1ff9efSjsing if (!CBB_add_u8(&cbb, SSL3_MT_CCS)) 2378eb1ff9efSjsing goto err; 2379eb1ff9efSjsing if (!CBB_finish(&cbb, NULL, &outlen)) 2380eb1ff9efSjsing goto err; 2381eb1ff9efSjsing 2382eb1ff9efSjsing if (outlen > INT_MAX) 2383eb1ff9efSjsing goto err; 2384eb1ff9efSjsing 23856f7f653bSjsing s->init_num = (int)outlen; 23866f7f653bSjsing s->init_off = 0; 2387eb1ff9efSjsing 2388eb1ff9efSjsing if (SSL_is_dtls(s)) { 2389eb1ff9efSjsing s->d1->handshake_write_seq = 2390eb1ff9efSjsing s->d1->next_handshake_write_seq; 2391eb1ff9efSjsing dtls1_set_message_header_int(s, SSL3_MT_CCS, 0, 2392eb1ff9efSjsing s->d1->handshake_write_seq, 0, 0); 2393eb1ff9efSjsing dtls1_buffer_message(s, 1); 2394eb1ff9efSjsing } 2395eb1ff9efSjsing 2396eb1ff9efSjsing s->s3->hs.state = SSL3_ST_SW_CHANGE_B; 2397eb1ff9efSjsing } 2398eb1ff9efSjsing 2399eb1ff9efSjsing /* SSL3_ST_SW_CHANGE_B */ 2400eb1ff9efSjsing return ssl3_record_write(s, SSL3_RT_CHANGE_CIPHER_SPEC); 2401eb1ff9efSjsing 2402eb1ff9efSjsing err: 2403eb1ff9efSjsing CBB_cleanup(&cbb); 2404eb1ff9efSjsing 2405eb1ff9efSjsing return -1; 2406eb1ff9efSjsing } 2407eb1ff9efSjsing 2408eb1ff9efSjsing static int 2409eb1ff9efSjsing ssl3_get_client_finished(SSL *s) 2410eb1ff9efSjsing { 2411eb1ff9efSjsing int al, md_len, ret; 2412eb1ff9efSjsing CBS cbs; 2413eb1ff9efSjsing 2414eb1ff9efSjsing /* should actually be 36+4 :-) */ 2415eb1ff9efSjsing if ((ret = ssl3_get_message(s, SSL3_ST_SR_FINISHED_A, 2416eb1ff9efSjsing SSL3_ST_SR_FINISHED_B, SSL3_MT_FINISHED, 64)) <= 0) 2417eb1ff9efSjsing return ret; 2418eb1ff9efSjsing 2419eb1ff9efSjsing /* If this occurs, we have missed a message */ 2420eb1ff9efSjsing if (!s->s3->change_cipher_spec) { 2421eb1ff9efSjsing al = SSL_AD_UNEXPECTED_MESSAGE; 2422eb1ff9efSjsing SSLerror(s, SSL_R_GOT_A_FIN_BEFORE_A_CCS); 2423eb1ff9efSjsing goto fatal_err; 2424eb1ff9efSjsing } 2425eb1ff9efSjsing s->s3->change_cipher_spec = 0; 2426eb1ff9efSjsing 2427eb1ff9efSjsing md_len = TLS1_FINISH_MAC_LENGTH; 2428eb1ff9efSjsing 24296f7f653bSjsing if (s->init_num < 0) { 2430eb1ff9efSjsing al = SSL_AD_DECODE_ERROR; 2431eb1ff9efSjsing SSLerror(s, SSL_R_BAD_DIGEST_LENGTH); 2432eb1ff9efSjsing goto fatal_err; 2433eb1ff9efSjsing } 2434eb1ff9efSjsing 24356f7f653bSjsing CBS_init(&cbs, s->init_msg, s->init_num); 2436eb1ff9efSjsing 2437eb1ff9efSjsing if (s->s3->hs.peer_finished_len != md_len || 2438eb1ff9efSjsing CBS_len(&cbs) != md_len) { 2439eb1ff9efSjsing al = SSL_AD_DECODE_ERROR; 2440eb1ff9efSjsing SSLerror(s, SSL_R_BAD_DIGEST_LENGTH); 2441eb1ff9efSjsing goto fatal_err; 2442eb1ff9efSjsing } 2443eb1ff9efSjsing 2444eb1ff9efSjsing if (!CBS_mem_equal(&cbs, s->s3->hs.peer_finished, CBS_len(&cbs))) { 2445eb1ff9efSjsing al = SSL_AD_DECRYPT_ERROR; 2446eb1ff9efSjsing SSLerror(s, SSL_R_DIGEST_CHECK_FAILED); 2447eb1ff9efSjsing goto fatal_err; 2448eb1ff9efSjsing } 2449eb1ff9efSjsing 2450eb1ff9efSjsing /* Copy finished so we can use it for renegotiation checks. */ 2451eb1ff9efSjsing OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE); 2452eb1ff9efSjsing memcpy(s->s3->previous_client_finished, 2453eb1ff9efSjsing s->s3->hs.peer_finished, md_len); 2454eb1ff9efSjsing s->s3->previous_client_finished_len = md_len; 2455eb1ff9efSjsing 2456eb1ff9efSjsing return (1); 2457eb1ff9efSjsing fatal_err: 2458eb1ff9efSjsing ssl3_send_alert(s, SSL3_AL_FATAL, al); 2459eb1ff9efSjsing return (0); 2460eb1ff9efSjsing } 2461eb1ff9efSjsing 2462eb1ff9efSjsing static int 2463eb1ff9efSjsing ssl3_send_server_finished(SSL *s) 2464eb1ff9efSjsing { 2465eb1ff9efSjsing CBB cbb, finished; 2466eb1ff9efSjsing 2467eb1ff9efSjsing memset(&cbb, 0, sizeof(cbb)); 2468eb1ff9efSjsing 2469eb1ff9efSjsing if (s->s3->hs.state == SSL3_ST_SW_FINISHED_A) { 2470eb1ff9efSjsing if (!tls12_derive_finished(s)) 2471eb1ff9efSjsing goto err; 2472eb1ff9efSjsing 2473eb1ff9efSjsing /* Copy finished so we can use it for renegotiation checks. */ 2474eb1ff9efSjsing memcpy(s->s3->previous_server_finished, 2475eb1ff9efSjsing s->s3->hs.finished, s->s3->hs.finished_len); 2476eb1ff9efSjsing s->s3->previous_server_finished_len = s->s3->hs.finished_len; 2477eb1ff9efSjsing 2478eb1ff9efSjsing if (!ssl3_handshake_msg_start(s, &cbb, &finished, 2479eb1ff9efSjsing SSL3_MT_FINISHED)) 2480eb1ff9efSjsing goto err; 2481eb1ff9efSjsing if (!CBB_add_bytes(&finished, s->s3->hs.finished, 2482eb1ff9efSjsing s->s3->hs.finished_len)) 2483eb1ff9efSjsing goto err; 2484eb1ff9efSjsing if (!ssl3_handshake_msg_finish(s, &cbb)) 2485eb1ff9efSjsing goto err; 2486eb1ff9efSjsing 2487eb1ff9efSjsing s->s3->hs.state = SSL3_ST_SW_FINISHED_B; 2488eb1ff9efSjsing } 2489eb1ff9efSjsing 2490eb1ff9efSjsing return (ssl3_handshake_write(s)); 2491eb1ff9efSjsing 2492eb1ff9efSjsing err: 2493eb1ff9efSjsing CBB_cleanup(&cbb); 2494eb1ff9efSjsing 2495eb1ff9efSjsing return (-1); 2496eb1ff9efSjsing } 2497