1*f4fe6251Sjsing /* $OpenBSD: ssl_clnt.c,v 1.168 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 1513395f70eSjsing #include <limits.h> 1523395f70eSjsing #include <stdint.h> 1533395f70eSjsing #include <stdio.h> 1543395f70eSjsing 1553395f70eSjsing #include <openssl/bn.h> 1563395f70eSjsing #include <openssl/buffer.h> 1573395f70eSjsing #include <openssl/curve25519.h> 1583395f70eSjsing #include <openssl/dh.h> 1593395f70eSjsing #include <openssl/evp.h> 1603395f70eSjsing #include <openssl/md5.h> 1613395f70eSjsing #include <openssl/objects.h> 162f4dd87b5Sjsing #include <openssl/opensslconf.h> 1633395f70eSjsing 1643395f70eSjsing #include "bytestring.h" 165c9675a23Stb #include "dtls_local.h" 166c9675a23Stb #include "ssl_local.h" 16796b1ac03Sbeck #include "ssl_sigalgs.h" 168f63b027dSjsing #include "ssl_tlsext.h" 1693395f70eSjsing 1703395f70eSjsing static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b); 1713395f70eSjsing 172eb1ff9efSjsing static int ssl3_send_client_hello(SSL *s); 173eb1ff9efSjsing static int ssl3_get_dtls_hello_verify(SSL *s); 174eb1ff9efSjsing static int ssl3_get_server_hello(SSL *s); 175eb1ff9efSjsing static int ssl3_get_certificate_request(SSL *s); 176eb1ff9efSjsing static int ssl3_get_new_session_ticket(SSL *s); 177eb1ff9efSjsing static int ssl3_get_cert_status(SSL *s); 178eb1ff9efSjsing static int ssl3_get_server_done(SSL *s); 179eb1ff9efSjsing static int ssl3_send_client_verify(SSL *s); 180eb1ff9efSjsing static int ssl3_send_client_certificate(SSL *s); 181eb1ff9efSjsing static int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey); 182eb1ff9efSjsing static int ssl3_send_client_key_exchange(SSL *s); 183eb1ff9efSjsing static int ssl3_get_server_key_exchange(SSL *s); 184eb1ff9efSjsing static int ssl3_get_server_certificate(SSL *s); 185eb1ff9efSjsing static int ssl3_check_cert_and_algorithm(SSL *s); 186eb1ff9efSjsing static int ssl3_check_finished(SSL *s); 187eb1ff9efSjsing static int ssl3_send_client_change_cipher_spec(SSL *s); 188eb1ff9efSjsing static int ssl3_send_client_finished(SSL *s); 189eb1ff9efSjsing static int ssl3_get_server_finished(SSL *s); 190eb1ff9efSjsing 1913395f70eSjsing int 1923395f70eSjsing ssl3_connect(SSL *s) 1933395f70eSjsing { 1943395f70eSjsing int new_state, state, skip = 0; 195545b2b63Sjsing int ret = -1; 1963395f70eSjsing 1973395f70eSjsing ERR_clear_error(); 1983395f70eSjsing errno = 0; 1993395f70eSjsing 2006f7f653bSjsing s->in_handshake++; 2013395f70eSjsing if (!SSL_in_init(s) || SSL_in_before(s)) 2023395f70eSjsing SSL_clear(s); 2033395f70eSjsing 2043395f70eSjsing for (;;) { 20502876cc3Sjsing state = s->s3->hs.state; 2063395f70eSjsing 20702876cc3Sjsing switch (s->s3->hs.state) { 2083395f70eSjsing case SSL_ST_RENEGOTIATE: 2096f7f653bSjsing s->renegotiate = 1; 21002876cc3Sjsing s->s3->hs.state = SSL_ST_CONNECT; 2116f7f653bSjsing s->ctx->stats.sess_connect_renegotiate++; 2123395f70eSjsing /* break */ 2133395f70eSjsing case SSL_ST_BEFORE: 2143395f70eSjsing case SSL_ST_CONNECT: 2153395f70eSjsing case SSL_ST_BEFORE|SSL_ST_CONNECT: 2163395f70eSjsing case SSL_ST_OK|SSL_ST_CONNECT: 2173395f70eSjsing 2183395f70eSjsing s->server = 0; 219545b2b63Sjsing 220545b2b63Sjsing ssl_info_callback(s, SSL_CB_HANDSHAKE_START, 1); 2213395f70eSjsing 222b81375ccSjsing if (!ssl_legacy_stack_version(s, s->version)) { 223dee84dc5Sjsing SSLerror(s, ERR_R_INTERNAL_ERROR); 224dee84dc5Sjsing ret = -1; 225dee84dc5Sjsing goto end; 226dee84dc5Sjsing } 2273395f70eSjsing 228c5e6469dSjsing if (!ssl_supported_tls_version_range(s, 22902876cc3Sjsing &s->s3->hs.our_min_tls_version, 23002876cc3Sjsing &s->s3->hs.our_max_tls_version)) { 231c5e6469dSjsing SSLerror(s, SSL_R_NO_PROTOCOLS_AVAILABLE); 232c5e6469dSjsing ret = -1; 233c5e6469dSjsing goto end; 234c5e6469dSjsing } 235c5e6469dSjsing 23689b4969bStb if (!ssl_security_version(s, 23789b4969bStb s->s3->hs.our_min_tls_version)) { 23889b4969bStb SSLerror(s, SSL_R_VERSION_TOO_LOW); 23989b4969bStb ret = -1; 24089b4969bStb goto end; 24189b4969bStb } 24289b4969bStb 2433395f70eSjsing if (!ssl3_setup_init_buffer(s)) { 2443395f70eSjsing ret = -1; 2453395f70eSjsing goto end; 2463395f70eSjsing } 2473395f70eSjsing if (!ssl3_setup_buffers(s)) { 2483395f70eSjsing ret = -1; 2493395f70eSjsing goto end; 2503395f70eSjsing } 2513395f70eSjsing if (!ssl_init_wbio_buffer(s, 0)) { 2523395f70eSjsing ret = -1; 2533395f70eSjsing goto end; 2543395f70eSjsing } 2553395f70eSjsing 2563395f70eSjsing /* don't push the buffering BIO quite yet */ 2573395f70eSjsing 258cef855dcSjsing if (!tls1_transcript_init(s)) { 2593395f70eSjsing ret = -1; 2603395f70eSjsing goto end; 2613395f70eSjsing } 2623395f70eSjsing 26302876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_CLNT_HELLO_A; 2646f7f653bSjsing s->ctx->stats.sess_connect++; 2656f7f653bSjsing s->init_num = 0; 266dee84dc5Sjsing 2679e659261Sjsing if (SSL_is_dtls(s)) { 268dee84dc5Sjsing /* mark client_random uninitialized */ 269dee84dc5Sjsing memset(s->s3->client_random, 0, 270dee84dc5Sjsing sizeof(s->s3->client_random)); 271f19d9718Sjsing s->d1->send_cookie = 0; 2726f7f653bSjsing s->hit = 0; 273dee84dc5Sjsing } 2743395f70eSjsing break; 2753395f70eSjsing 2763395f70eSjsing case SSL3_ST_CW_CLNT_HELLO_A: 2773395f70eSjsing case SSL3_ST_CW_CLNT_HELLO_B: 2786f7f653bSjsing s->shutdown = 0; 279dee84dc5Sjsing 2809e659261Sjsing if (SSL_is_dtls(s)) { 281dee84dc5Sjsing /* every DTLS ClientHello resets Finished MAC */ 282372de723Sjsing tls1_transcript_reset(s); 283dee84dc5Sjsing 284dee84dc5Sjsing dtls1_start_timer(s); 285dee84dc5Sjsing } 286dee84dc5Sjsing 287e39acaa9Sjsing ret = ssl3_send_client_hello(s); 2883395f70eSjsing if (ret <= 0) 2893395f70eSjsing goto end; 290dee84dc5Sjsing 291f19d9718Sjsing if (SSL_is_dtls(s) && s->d1->send_cookie) { 29202876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_FLUSH; 29302876cc3Sjsing s->s3->hs.tls12.next_state = SSL3_ST_CR_SRVR_HELLO_A; 294dee84dc5Sjsing } else 29502876cc3Sjsing s->s3->hs.state = SSL3_ST_CR_SRVR_HELLO_A; 296dee84dc5Sjsing 2976f7f653bSjsing s->init_num = 0; 2983395f70eSjsing 2993395f70eSjsing /* turn on buffering for the next lot of output */ 3003395f70eSjsing if (s->bbio != s->wbio) 3013395f70eSjsing s->wbio = BIO_push(s->bbio, s->wbio); 3023395f70eSjsing 3033395f70eSjsing break; 3043395f70eSjsing 3053395f70eSjsing case SSL3_ST_CR_SRVR_HELLO_A: 3063395f70eSjsing case SSL3_ST_CR_SRVR_HELLO_B: 3073395f70eSjsing ret = ssl3_get_server_hello(s); 3083395f70eSjsing if (ret <= 0) 3093395f70eSjsing goto end; 3103395f70eSjsing 3116f7f653bSjsing if (s->hit) { 31202876cc3Sjsing s->s3->hs.state = SSL3_ST_CR_FINISHED_A; 3139e659261Sjsing if (!SSL_is_dtls(s)) { 3146f7f653bSjsing if (s->tlsext_ticket_expected) { 3153395f70eSjsing /* receive renewed session ticket */ 31602876cc3Sjsing s->s3->hs.state = SSL3_ST_CR_SESSION_TICKET_A; 3173395f70eSjsing } 3181cfaeb64Sjsing 3191cfaeb64Sjsing /* No client certificate verification. */ 3201cfaeb64Sjsing tls1_transcript_free(s); 321dee84dc5Sjsing } 3229e659261Sjsing } else if (SSL_is_dtls(s)) { 32302876cc3Sjsing s->s3->hs.state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A; 324dee84dc5Sjsing } else { 32502876cc3Sjsing s->s3->hs.state = SSL3_ST_CR_CERT_A; 326dee84dc5Sjsing } 3276f7f653bSjsing s->init_num = 0; 328dee84dc5Sjsing break; 329dee84dc5Sjsing 330dee84dc5Sjsing case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A: 331dee84dc5Sjsing case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B: 332f6d60a7aSjsing ret = ssl3_get_dtls_hello_verify(s); 333dee84dc5Sjsing if (ret <= 0) 334dee84dc5Sjsing goto end; 335dee84dc5Sjsing dtls1_stop_timer(s); 336f19d9718Sjsing if (s->d1->send_cookie) /* start again, with a cookie */ 33702876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_CLNT_HELLO_A; 338dee84dc5Sjsing else 33902876cc3Sjsing s->s3->hs.state = SSL3_ST_CR_CERT_A; 3406f7f653bSjsing s->init_num = 0; 3413395f70eSjsing break; 3423395f70eSjsing 3433395f70eSjsing case SSL3_ST_CR_CERT_A: 3443395f70eSjsing case SSL3_ST_CR_CERT_B: 3453395f70eSjsing ret = ssl3_check_finished(s); 3463395f70eSjsing if (ret <= 0) 3473395f70eSjsing goto end; 3483395f70eSjsing if (ret == 2) { 3496f7f653bSjsing s->hit = 1; 3506f7f653bSjsing if (s->tlsext_ticket_expected) 35102876cc3Sjsing s->s3->hs.state = SSL3_ST_CR_SESSION_TICKET_A; 3523395f70eSjsing else 35302876cc3Sjsing s->s3->hs.state = SSL3_ST_CR_FINISHED_A; 3546f7f653bSjsing s->init_num = 0; 3553395f70eSjsing break; 3563395f70eSjsing } 3573395f70eSjsing /* Check if it is anon DH/ECDH. */ 35802876cc3Sjsing if (!(s->s3->hs.cipher->algorithm_auth & 3593395f70eSjsing SSL_aNULL)) { 3603395f70eSjsing ret = ssl3_get_server_certificate(s); 3613395f70eSjsing if (ret <= 0) 3623395f70eSjsing goto end; 3636f7f653bSjsing if (s->tlsext_status_expected) 36402876cc3Sjsing s->s3->hs.state = SSL3_ST_CR_CERT_STATUS_A; 3653395f70eSjsing else 36602876cc3Sjsing s->s3->hs.state = SSL3_ST_CR_KEY_EXCH_A; 3673395f70eSjsing } else { 3683395f70eSjsing skip = 1; 36902876cc3Sjsing s->s3->hs.state = SSL3_ST_CR_KEY_EXCH_A; 3703395f70eSjsing } 3716f7f653bSjsing s->init_num = 0; 3723395f70eSjsing break; 3733395f70eSjsing 3743395f70eSjsing case SSL3_ST_CR_KEY_EXCH_A: 3753395f70eSjsing case SSL3_ST_CR_KEY_EXCH_B: 3763395f70eSjsing ret = ssl3_get_server_key_exchange(s); 3773395f70eSjsing if (ret <= 0) 3783395f70eSjsing goto end; 37902876cc3Sjsing s->s3->hs.state = SSL3_ST_CR_CERT_REQ_A; 3806f7f653bSjsing s->init_num = 0; 3813395f70eSjsing 3823395f70eSjsing /* 3833395f70eSjsing * At this point we check that we have the 3843395f70eSjsing * required stuff from the server. 3853395f70eSjsing */ 3863395f70eSjsing if (!ssl3_check_cert_and_algorithm(s)) { 3873395f70eSjsing ret = -1; 3883395f70eSjsing goto end; 3893395f70eSjsing } 3903395f70eSjsing break; 3913395f70eSjsing 3923395f70eSjsing case SSL3_ST_CR_CERT_REQ_A: 3933395f70eSjsing case SSL3_ST_CR_CERT_REQ_B: 3943395f70eSjsing ret = ssl3_get_certificate_request(s); 3953395f70eSjsing if (ret <= 0) 3963395f70eSjsing goto end; 39702876cc3Sjsing s->s3->hs.state = SSL3_ST_CR_SRVR_DONE_A; 3986f7f653bSjsing s->init_num = 0; 3993395f70eSjsing break; 4003395f70eSjsing 4013395f70eSjsing case SSL3_ST_CR_SRVR_DONE_A: 4023395f70eSjsing case SSL3_ST_CR_SRVR_DONE_B: 4033395f70eSjsing ret = ssl3_get_server_done(s); 4043395f70eSjsing if (ret <= 0) 4053395f70eSjsing goto end; 4069e659261Sjsing if (SSL_is_dtls(s)) 407dee84dc5Sjsing dtls1_stop_timer(s); 40802876cc3Sjsing if (s->s3->hs.tls12.cert_request) 40902876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_CERT_A; 4103395f70eSjsing else 41102876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_KEY_EXCH_A; 4126f7f653bSjsing s->init_num = 0; 4133395f70eSjsing 4143395f70eSjsing break; 4153395f70eSjsing 4163395f70eSjsing case SSL3_ST_CW_CERT_A: 4173395f70eSjsing case SSL3_ST_CW_CERT_B: 4183395f70eSjsing case SSL3_ST_CW_CERT_C: 4193395f70eSjsing case SSL3_ST_CW_CERT_D: 4209e659261Sjsing if (SSL_is_dtls(s)) 421dee84dc5Sjsing dtls1_start_timer(s); 4223395f70eSjsing ret = ssl3_send_client_certificate(s); 4233395f70eSjsing if (ret <= 0) 4243395f70eSjsing goto end; 42502876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_KEY_EXCH_A; 4266f7f653bSjsing s->init_num = 0; 4273395f70eSjsing break; 4283395f70eSjsing 4293395f70eSjsing case SSL3_ST_CW_KEY_EXCH_A: 4303395f70eSjsing case SSL3_ST_CW_KEY_EXCH_B: 4319e659261Sjsing if (SSL_is_dtls(s)) 432dee84dc5Sjsing dtls1_start_timer(s); 4333395f70eSjsing ret = ssl3_send_client_key_exchange(s); 4343395f70eSjsing if (ret <= 0) 4353395f70eSjsing goto end; 4363395f70eSjsing /* 4373395f70eSjsing * EAY EAY EAY need to check for DH fix cert 4383395f70eSjsing * sent back 4393395f70eSjsing */ 4403395f70eSjsing /* 4413395f70eSjsing * For TLS, cert_req is set to 2, so a cert chain 4423395f70eSjsing * of nothing is sent, but no verify packet is sent 4433395f70eSjsing */ 4443395f70eSjsing /* 4453395f70eSjsing * XXX: For now, we do not support client 4463395f70eSjsing * authentication in ECDH cipher suites with 4473395f70eSjsing * ECDH (rather than ECDSA) certificates. 4483395f70eSjsing * We need to skip the certificate verify 4493395f70eSjsing * message when client's ECDH public key is sent 4503395f70eSjsing * inside the client certificate. 4513395f70eSjsing */ 45202876cc3Sjsing if (s->s3->hs.tls12.cert_request == 1) { 45302876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_CERT_VRFY_A; 4543395f70eSjsing } else { 45502876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_CHANGE_A; 45602876cc3Sjsing s->s3->change_cipher_spec = 0; 4573395f70eSjsing } 4583395f70eSjsing 4596f7f653bSjsing s->init_num = 0; 4603395f70eSjsing break; 4613395f70eSjsing 4623395f70eSjsing case SSL3_ST_CW_CERT_VRFY_A: 4633395f70eSjsing case SSL3_ST_CW_CERT_VRFY_B: 4649e659261Sjsing if (SSL_is_dtls(s)) 465dee84dc5Sjsing dtls1_start_timer(s); 4663395f70eSjsing ret = ssl3_send_client_verify(s); 4673395f70eSjsing if (ret <= 0) 4683395f70eSjsing goto end; 46902876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_CHANGE_A; 4706f7f653bSjsing s->init_num = 0; 47102876cc3Sjsing s->s3->change_cipher_spec = 0; 4723395f70eSjsing break; 4733395f70eSjsing 4743395f70eSjsing case SSL3_ST_CW_CHANGE_A: 4753395f70eSjsing case SSL3_ST_CW_CHANGE_B: 4766f7f653bSjsing if (SSL_is_dtls(s) && !s->hit) 477dee84dc5Sjsing dtls1_start_timer(s); 478eb1ff9efSjsing ret = ssl3_send_client_change_cipher_spec(s); 4793395f70eSjsing if (ret <= 0) 4803395f70eSjsing goto end; 4813395f70eSjsing 48202876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_FINISHED_A; 4836f7f653bSjsing s->init_num = 0; 484*f4fe6251Sjsing s->session->cipher_value = s->s3->hs.cipher->value; 485b3d9ef4bSjsing 48653f78dfdSjsing if (!tls1_setup_key_block(s)) { 4873395f70eSjsing ret = -1; 4883395f70eSjsing goto end; 4893395f70eSjsing } 490b3d9ef4bSjsing if (!tls1_change_write_cipher_state(s)) { 4913395f70eSjsing ret = -1; 4923395f70eSjsing goto end; 4933395f70eSjsing } 4943395f70eSjsing break; 4953395f70eSjsing 4963395f70eSjsing case SSL3_ST_CW_FINISHED_A: 4973395f70eSjsing case SSL3_ST_CW_FINISHED_B: 4986f7f653bSjsing if (SSL_is_dtls(s) && !s->hit) 499dee84dc5Sjsing dtls1_start_timer(s); 500eb1ff9efSjsing ret = ssl3_send_client_finished(s); 5013395f70eSjsing if (ret <= 0) 5023395f70eSjsing goto end; 5039e659261Sjsing if (!SSL_is_dtls(s)) 5043395f70eSjsing s->s3->flags |= SSL3_FLAGS_CCS_OK; 50502876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_FLUSH; 5063395f70eSjsing 5073395f70eSjsing /* clear flags */ 5086f7f653bSjsing if (s->hit) { 50902876cc3Sjsing s->s3->hs.tls12.next_state = SSL_ST_OK; 5103395f70eSjsing } else { 5113395f70eSjsing /* Allow NewSessionTicket if ticket expected */ 5126f7f653bSjsing if (s->tlsext_ticket_expected) 51302876cc3Sjsing s->s3->hs.tls12.next_state = 5143395f70eSjsing SSL3_ST_CR_SESSION_TICKET_A; 5153395f70eSjsing else 51602876cc3Sjsing s->s3->hs.tls12.next_state = 517ba168297Sjsing SSL3_ST_CR_FINISHED_A; 5183395f70eSjsing } 5196f7f653bSjsing s->init_num = 0; 5203395f70eSjsing break; 5213395f70eSjsing 5223395f70eSjsing case SSL3_ST_CR_SESSION_TICKET_A: 5233395f70eSjsing case SSL3_ST_CR_SESSION_TICKET_B: 5243395f70eSjsing ret = ssl3_get_new_session_ticket(s); 5253395f70eSjsing if (ret <= 0) 5263395f70eSjsing goto end; 52702876cc3Sjsing s->s3->hs.state = SSL3_ST_CR_FINISHED_A; 5286f7f653bSjsing s->init_num = 0; 5293395f70eSjsing break; 5303395f70eSjsing 5313395f70eSjsing case SSL3_ST_CR_CERT_STATUS_A: 5323395f70eSjsing case SSL3_ST_CR_CERT_STATUS_B: 5333395f70eSjsing ret = ssl3_get_cert_status(s); 5343395f70eSjsing if (ret <= 0) 5353395f70eSjsing goto end; 53602876cc3Sjsing s->s3->hs.state = SSL3_ST_CR_KEY_EXCH_A; 5376f7f653bSjsing s->init_num = 0; 5383395f70eSjsing break; 5393395f70eSjsing 5403395f70eSjsing case SSL3_ST_CR_FINISHED_A: 5413395f70eSjsing case SSL3_ST_CR_FINISHED_B: 5429e659261Sjsing if (SSL_is_dtls(s)) 543f19d9718Sjsing s->d1->change_cipher_spec_ok = 1; 544dee84dc5Sjsing else 5453395f70eSjsing s->s3->flags |= SSL3_FLAGS_CCS_OK; 546eb1ff9efSjsing ret = ssl3_get_server_finished(s); 5473395f70eSjsing if (ret <= 0) 5483395f70eSjsing goto end; 5499e659261Sjsing if (SSL_is_dtls(s)) 550dee84dc5Sjsing dtls1_stop_timer(s); 5513395f70eSjsing 5526f7f653bSjsing if (s->hit) 55302876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_CHANGE_A; 5543395f70eSjsing else 55502876cc3Sjsing s->s3->hs.state = SSL_ST_OK; 5566f7f653bSjsing s->init_num = 0; 5573395f70eSjsing break; 5583395f70eSjsing 5593395f70eSjsing case SSL3_ST_CW_FLUSH: 5606f7f653bSjsing s->rwstate = SSL_WRITING; 5613395f70eSjsing if (BIO_flush(s->wbio) <= 0) { 5629e659261Sjsing if (SSL_is_dtls(s)) { 563dee84dc5Sjsing /* If the write error was fatal, stop trying */ 564dee84dc5Sjsing if (!BIO_should_retry(s->wbio)) { 5656f7f653bSjsing s->rwstate = SSL_NOTHING; 56602876cc3Sjsing s->s3->hs.state = s->s3->hs.tls12.next_state; 567dee84dc5Sjsing } 568dee84dc5Sjsing } 5693395f70eSjsing ret = -1; 5703395f70eSjsing goto end; 5713395f70eSjsing } 5726f7f653bSjsing s->rwstate = SSL_NOTHING; 57302876cc3Sjsing s->s3->hs.state = s->s3->hs.tls12.next_state; 5743395f70eSjsing break; 5753395f70eSjsing 5763395f70eSjsing case SSL_ST_OK: 5773395f70eSjsing /* clean a few things up */ 5783395f70eSjsing tls1_cleanup_key_block(s); 5793395f70eSjsing 58002876cc3Sjsing if (s->s3->handshake_transcript != NULL) { 581d250fe7cSjsing SSLerror(s, ERR_R_INTERNAL_ERROR); 582d250fe7cSjsing ret = -1; 583d250fe7cSjsing goto end; 584d250fe7cSjsing } 585d250fe7cSjsing 5869e659261Sjsing if (!SSL_is_dtls(s)) 587beb7767dSjsing ssl3_release_init_buffer(s); 5883395f70eSjsing 5893395f70eSjsing ssl_free_wbio_buffer(s); 5903395f70eSjsing 5916f7f653bSjsing s->init_num = 0; 5926f7f653bSjsing s->renegotiate = 0; 5936f7f653bSjsing s->new_session = 0; 5943395f70eSjsing 5953395f70eSjsing ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); 5966f7f653bSjsing if (s->hit) 5976f7f653bSjsing s->ctx->stats.sess_hit++; 5983395f70eSjsing 5993395f70eSjsing ret = 1; 6003395f70eSjsing /* s->server=0; */ 6016f7f653bSjsing s->handshake_func = ssl3_connect; 6026f7f653bSjsing s->ctx->stats.sess_connect_good++; 6033395f70eSjsing 604545b2b63Sjsing ssl_info_callback(s, SSL_CB_HANDSHAKE_DONE, 1); 6053395f70eSjsing 6069e659261Sjsing if (SSL_is_dtls(s)) { 607dee84dc5Sjsing /* done with handshaking */ 608f19d9718Sjsing s->d1->handshake_read_seq = 0; 609f19d9718Sjsing s->d1->next_handshake_write_seq = 0; 610dee84dc5Sjsing } 611dee84dc5Sjsing 6123395f70eSjsing goto end; 6133395f70eSjsing /* break; */ 6143395f70eSjsing 6153395f70eSjsing default: 616c9d7abb7Sbeck SSLerror(s, SSL_R_UNKNOWN_STATE); 6173395f70eSjsing ret = -1; 6183395f70eSjsing goto end; 6193395f70eSjsing /* break; */ 6203395f70eSjsing } 6213395f70eSjsing 6223395f70eSjsing /* did we do anything */ 62302876cc3Sjsing if (!s->s3->hs.tls12.reuse_message && !skip) { 62402876cc3Sjsing if (s->s3->hs.state != state) { 62502876cc3Sjsing new_state = s->s3->hs.state; 62602876cc3Sjsing s->s3->hs.state = state; 627545b2b63Sjsing ssl_info_callback(s, SSL_CB_CONNECT_LOOP, 1); 62802876cc3Sjsing s->s3->hs.state = new_state; 6293395f70eSjsing } 6303395f70eSjsing } 6313395f70eSjsing skip = 0; 6323395f70eSjsing } 6333395f70eSjsing 6343395f70eSjsing end: 6356f7f653bSjsing s->in_handshake--; 636545b2b63Sjsing ssl_info_callback(s, SSL_CB_CONNECT_EXIT, ret); 6373395f70eSjsing 6383395f70eSjsing return (ret); 6393395f70eSjsing } 6403395f70eSjsing 641eb1ff9efSjsing static int 642e39acaa9Sjsing ssl3_send_client_hello(SSL *s) 6433395f70eSjsing { 644f63b027dSjsing CBB cbb, client_hello, session_id, cookie, cipher_suites; 645f63b027dSjsing CBB compression_methods; 6463395f70eSjsing uint16_t max_version; 647f63b027dSjsing size_t sl; 6483395f70eSjsing 649f63b027dSjsing memset(&cbb, 0, sizeof(cbb)); 6503395f70eSjsing 65102876cc3Sjsing if (s->s3->hs.state == SSL3_ST_CW_CLNT_HELLO_A) { 6523395f70eSjsing SSL_SESSION *sess = s->session; 6533395f70eSjsing 654305a9276Sjsing if (!ssl_max_supported_version(s, &max_version)) { 655c9d7abb7Sbeck SSLerror(s, SSL_R_NO_PROTOCOLS_AVAILABLE); 6563395f70eSjsing return (-1); 6573395f70eSjsing } 65801f29c58Sjsing s->version = max_version; 6593395f70eSjsing 660b7965c0dStb if (sess == NULL || sess->ssl_version != s->version || 661b7965c0dStb (sess->session_id_length == 0 && sess->tlsext_tick == NULL) || 6627e086035Sjsing sess->not_resumable) { 6633395f70eSjsing if (!ssl_get_new_session(s, 0)) 6643395f70eSjsing goto err; 6653395f70eSjsing } 6663395f70eSjsing /* else use the pre-loaded session */ 6673395f70eSjsing 6683395f70eSjsing /* 6693395f70eSjsing * If a DTLS ClientHello message is being resent after a 6703395f70eSjsing * HelloVerifyRequest, we must retain the original client 6713395f70eSjsing * random value. 6723395f70eSjsing */ 673f19d9718Sjsing if (!SSL_is_dtls(s) || s->d1->send_cookie == 0) 6743395f70eSjsing arc4random_buf(s->s3->client_random, SSL3_RANDOM_SIZE); 6753395f70eSjsing 676312b4b14Sjsing if (!ssl3_handshake_msg_start(s, &cbb, &client_hello, 677f63b027dSjsing SSL3_MT_CLIENT_HELLO)) 678f63b027dSjsing goto err; 6793395f70eSjsing 68001f29c58Sjsing if (!CBB_add_u16(&client_hello, s->version)) 681f63b027dSjsing goto err; 6823395f70eSjsing 6833395f70eSjsing /* Random stuff */ 684f63b027dSjsing if (!CBB_add_bytes(&client_hello, s->s3->client_random, 685f63b027dSjsing sizeof(s->s3->client_random))) 686f63b027dSjsing goto err; 6873395f70eSjsing 6883395f70eSjsing /* Session ID */ 689f63b027dSjsing if (!CBB_add_u8_length_prefixed(&client_hello, &session_id)) 690f63b027dSjsing goto err; 6916f7f653bSjsing if (!s->new_session && 692f63b027dSjsing s->session->session_id_length > 0) { 693f63b027dSjsing sl = s->session->session_id_length; 694f63b027dSjsing if (sl > sizeof(s->session->session_id)) { 695c9d7abb7Sbeck SSLerror(s, ERR_R_INTERNAL_ERROR); 6963395f70eSjsing goto err; 6973395f70eSjsing } 698f63b027dSjsing if (!CBB_add_bytes(&session_id, 699f63b027dSjsing s->session->session_id, sl)) 700f63b027dSjsing goto err; 7013395f70eSjsing } 7023395f70eSjsing 7033395f70eSjsing /* DTLS Cookie. */ 7049e659261Sjsing if (SSL_is_dtls(s)) { 705f19d9718Sjsing if (s->d1->cookie_len > sizeof(s->d1->cookie)) { 706c9d7abb7Sbeck SSLerror(s, ERR_R_INTERNAL_ERROR); 7073395f70eSjsing goto err; 7083395f70eSjsing } 709f63b027dSjsing if (!CBB_add_u8_length_prefixed(&client_hello, &cookie)) 710f63b027dSjsing goto err; 711f19d9718Sjsing if (!CBB_add_bytes(&cookie, s->d1->cookie, 712f19d9718Sjsing s->d1->cookie_len)) 713f63b027dSjsing goto err; 7143395f70eSjsing } 7153395f70eSjsing 7163395f70eSjsing /* Ciphers supported */ 717f63b027dSjsing if (!CBB_add_u16_length_prefixed(&client_hello, &cipher_suites)) 718f63b027dSjsing return 0; 719f63b027dSjsing if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), 720f63b027dSjsing &cipher_suites)) { 721c9d7abb7Sbeck SSLerror(s, SSL_R_NO_CIPHERS_AVAILABLE); 7223395f70eSjsing goto err; 7233395f70eSjsing } 7243395f70eSjsing 725f63b027dSjsing /* Add in compression methods (null) */ 726f63b027dSjsing if (!CBB_add_u8_length_prefixed(&client_hello, 727f63b027dSjsing &compression_methods)) 728f63b027dSjsing goto err; 729f63b027dSjsing if (!CBB_add_u8(&compression_methods, 0)) 730f63b027dSjsing goto err; 7313395f70eSjsing 7323395f70eSjsing /* TLS extensions */ 7339b8a142fStb if (!tlsext_client_build(s, SSL_TLSEXT_MSG_CH, &client_hello)) { 734c9d7abb7Sbeck SSLerror(s, ERR_R_INTERNAL_ERROR); 7353395f70eSjsing goto err; 7363395f70eSjsing } 7373395f70eSjsing 738312b4b14Sjsing if (!ssl3_handshake_msg_finish(s, &cbb)) 739f63b027dSjsing goto err; 7403395f70eSjsing 74102876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_CLNT_HELLO_B; 7423395f70eSjsing } 7433395f70eSjsing 7443395f70eSjsing /* SSL3_ST_CW_CLNT_HELLO_B */ 7453395f70eSjsing return (ssl3_handshake_write(s)); 7463395f70eSjsing 7473395f70eSjsing err: 748f63b027dSjsing CBB_cleanup(&cbb); 749f63b027dSjsing 7503395f70eSjsing return (-1); 7513395f70eSjsing } 7523395f70eSjsing 753eb1ff9efSjsing static int 754f6d60a7aSjsing ssl3_get_dtls_hello_verify(SSL *s) 755f6d60a7aSjsing { 756f2c3e964Sjsing CBS hello_verify_request, cookie; 757f6d60a7aSjsing size_t cookie_len; 758f6d60a7aSjsing uint16_t ssl_version; 759f2c3e964Sjsing int al, ret; 760f6d60a7aSjsing 761f2c3e964Sjsing if ((ret = ssl3_get_message(s, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A, 7626f7f653bSjsing DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B, -1, s->max_cert_list)) <= 0) 763f2c3e964Sjsing return ret; 764f6d60a7aSjsing 76502876cc3Sjsing if (s->s3->hs.tls12.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) { 766f19d9718Sjsing s->d1->send_cookie = 0; 76702876cc3Sjsing s->s3->hs.tls12.reuse_message = 1; 768f6d60a7aSjsing return (1); 769f6d60a7aSjsing } 770f6d60a7aSjsing 7716f7f653bSjsing if (s->init_num < 0) 772e0d5994dStb goto decode_err; 773f6d60a7aSjsing 7746f7f653bSjsing CBS_init(&hello_verify_request, s->init_msg, 7756f7f653bSjsing s->init_num); 776f6d60a7aSjsing 777f6d60a7aSjsing if (!CBS_get_u16(&hello_verify_request, &ssl_version)) 778e0d5994dStb goto decode_err; 7792daeb91fSjsing if (!CBS_get_u8_length_prefixed(&hello_verify_request, &cookie)) 780e0d5994dStb goto decode_err; 7812daeb91fSjsing if (CBS_len(&hello_verify_request) != 0) 782e0d5994dStb goto decode_err; 783f6d60a7aSjsing 7844ddbf855Sjsing /* 7854ddbf855Sjsing * Per RFC 6347 section 4.2.1, the HelloVerifyRequest should always 7864ddbf855Sjsing * contain DTLSv1.0 the version that is going to be negotiated. 7874ddbf855Sjsing * Tolerate DTLSv1.2 just in case. 7884ddbf855Sjsing */ 7894ddbf855Sjsing if (ssl_version != DTLS1_VERSION && ssl_version != DTLS1_2_VERSION) { 790f6d60a7aSjsing SSLerror(s, SSL_R_WRONG_SSL_VERSION); 791f6d60a7aSjsing s->version = (s->version & 0xff00) | (ssl_version & 0xff); 792f6d60a7aSjsing al = SSL_AD_PROTOCOL_VERSION; 793ba06b73eStb goto fatal_err; 794f6d60a7aSjsing } 795f6d60a7aSjsing 796f19d9718Sjsing if (!CBS_write_bytes(&cookie, s->d1->cookie, 797f19d9718Sjsing sizeof(s->d1->cookie), &cookie_len)) { 798f19d9718Sjsing s->d1->cookie_len = 0; 799f6d60a7aSjsing al = SSL_AD_ILLEGAL_PARAMETER; 800ba06b73eStb goto fatal_err; 801f6d60a7aSjsing } 802f19d9718Sjsing s->d1->cookie_len = cookie_len; 803f19d9718Sjsing s->d1->send_cookie = 1; 804f6d60a7aSjsing 805f6d60a7aSjsing return 1; 806f6d60a7aSjsing 807e0d5994dStb decode_err: 808f6d60a7aSjsing al = SSL_AD_DECODE_ERROR; 809ba06b73eStb fatal_err: 810f6d60a7aSjsing ssl3_send_alert(s, SSL3_AL_FATAL, al); 811f6d60a7aSjsing return -1; 812f6d60a7aSjsing } 813f6d60a7aSjsing 814eb1ff9efSjsing static int 8153395f70eSjsing ssl3_get_server_hello(SSL *s) 8163395f70eSjsing { 8173395f70eSjsing CBS cbs, server_random, session_id; 8183395f70eSjsing uint16_t server_version, cipher_suite; 8193395f70eSjsing uint8_t compression_method; 8203395f70eSjsing const SSL_CIPHER *cipher; 8213395f70eSjsing const SSL_METHOD *method; 822f2c3e964Sjsing int al, ret; 8233395f70eSjsing 8246f7f653bSjsing s->first_packet = 1; 825f2c3e964Sjsing if ((ret = ssl3_get_message(s, SSL3_ST_CR_SRVR_HELLO_A, 826f2c3e964Sjsing SSL3_ST_CR_SRVR_HELLO_B, -1, 20000 /* ?? */)) <= 0) 827f2c3e964Sjsing return ret; 8286f7f653bSjsing s->first_packet = 0; 8293395f70eSjsing 8306f7f653bSjsing if (s->init_num < 0) 831e0d5994dStb goto decode_err; 8323395f70eSjsing 8336f7f653bSjsing CBS_init(&cbs, s->init_msg, s->init_num); 8343395f70eSjsing 8359e659261Sjsing if (SSL_is_dtls(s)) { 83602876cc3Sjsing if (s->s3->hs.tls12.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) { 837f19d9718Sjsing if (s->d1->send_cookie == 0) { 83802876cc3Sjsing s->s3->hs.tls12.reuse_message = 1; 8393395f70eSjsing return (1); 8403395f70eSjsing } else { 8413395f70eSjsing /* Already sent a cookie. */ 8423395f70eSjsing al = SSL_AD_UNEXPECTED_MESSAGE; 843c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_MESSAGE_TYPE); 844ba06b73eStb goto fatal_err; 8453395f70eSjsing } 8463395f70eSjsing } 8473395f70eSjsing } 8483395f70eSjsing 84902876cc3Sjsing if (s->s3->hs.tls12.message_type != SSL3_MT_SERVER_HELLO) { 8503395f70eSjsing al = SSL_AD_UNEXPECTED_MESSAGE; 851c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_MESSAGE_TYPE); 852ba06b73eStb goto fatal_err; 8533395f70eSjsing } 8543395f70eSjsing 8553395f70eSjsing if (!CBS_get_u16(&cbs, &server_version)) 856e0d5994dStb goto decode_err; 8573395f70eSjsing 858305a9276Sjsing if (!ssl_check_version_from_server(s, server_version)) { 859c9d7abb7Sbeck SSLerror(s, SSL_R_WRONG_SSL_VERSION); 8603395f70eSjsing s->version = (s->version & 0xff00) | (server_version & 0xff); 8613395f70eSjsing al = SSL_AD_PROTOCOL_VERSION; 862ba06b73eStb goto fatal_err; 8633395f70eSjsing } 86402876cc3Sjsing s->s3->hs.peer_legacy_version = server_version; 8653395f70eSjsing s->version = server_version; 8663395f70eSjsing 86702876cc3Sjsing s->s3->hs.negotiated_tls_version = ssl_tls_version(server_version); 86802876cc3Sjsing if (s->s3->hs.negotiated_tls_version == 0) { 869c5e6469dSjsing SSLerror(s, ERR_R_INTERNAL_ERROR); 870c5e6469dSjsing goto err; 871c5e6469dSjsing } 872c5e6469dSjsing 8739fef1c44Sjsing if ((method = ssl_get_method(server_version)) == NULL) { 874c9d7abb7Sbeck SSLerror(s, ERR_R_INTERNAL_ERROR); 8753395f70eSjsing goto err; 8763395f70eSjsing } 8773395f70eSjsing s->method = method; 8783395f70eSjsing 8793395f70eSjsing /* Server random. */ 8803395f70eSjsing if (!CBS_get_bytes(&cbs, &server_random, SSL3_RANDOM_SIZE)) 881e0d5994dStb goto decode_err; 8823395f70eSjsing if (!CBS_write_bytes(&server_random, s->s3->server_random, 8833395f70eSjsing sizeof(s->s3->server_random), NULL)) 8843395f70eSjsing goto err; 8853395f70eSjsing 88602876cc3Sjsing if (s->s3->hs.our_max_tls_version >= TLS1_2_VERSION && 88702876cc3Sjsing s->s3->hs.negotiated_tls_version < s->s3->hs.our_max_tls_version) { 88875c0f652Stb /* 88975c0f652Stb * RFC 8446 section 4.1.3. We must not downgrade if the server 89075c0f652Stb * random value contains the TLS 1.2 or TLS 1.1 magical value. 89175c0f652Stb */ 89275c0f652Stb if (!CBS_skip(&server_random, 89375c0f652Stb CBS_len(&server_random) - sizeof(tls13_downgrade_12))) 89475c0f652Stb goto err; 89502876cc3Sjsing if (s->s3->hs.negotiated_tls_version == TLS1_2_VERSION && 89675c0f652Stb CBS_mem_equal(&server_random, tls13_downgrade_12, 89775c0f652Stb sizeof(tls13_downgrade_12))) { 89875c0f652Stb al = SSL_AD_ILLEGAL_PARAMETER; 89975c0f652Stb SSLerror(s, SSL_R_INAPPROPRIATE_FALLBACK); 900ba06b73eStb goto fatal_err; 90175c0f652Stb } 90275c0f652Stb if (CBS_mem_equal(&server_random, tls13_downgrade_11, 90375c0f652Stb sizeof(tls13_downgrade_11))) { 90475c0f652Stb al = SSL_AD_ILLEGAL_PARAMETER; 90575c0f652Stb SSLerror(s, SSL_R_INAPPROPRIATE_FALLBACK); 906ba06b73eStb goto fatal_err; 90775c0f652Stb } 90875c0f652Stb } 90975c0f652Stb 9103395f70eSjsing /* Session ID. */ 9113395f70eSjsing if (!CBS_get_u8_length_prefixed(&cbs, &session_id)) 912e0d5994dStb goto decode_err; 9133395f70eSjsing 9149cf2d9d3Stb if (CBS_len(&session_id) > SSL3_SESSION_ID_SIZE) { 9153395f70eSjsing al = SSL_AD_ILLEGAL_PARAMETER; 916c9d7abb7Sbeck SSLerror(s, SSL_R_SSL3_SESSION_ID_TOO_LONG); 917ba06b73eStb goto fatal_err; 9183395f70eSjsing } 9193395f70eSjsing 9203395f70eSjsing /* Cipher suite. */ 9213395f70eSjsing if (!CBS_get_u16(&cbs, &cipher_suite)) 922e0d5994dStb goto decode_err; 9233395f70eSjsing 9243395f70eSjsing /* 9253395f70eSjsing * Check if we want to resume the session based on external 9263395f70eSjsing * pre-shared secret. 9273395f70eSjsing */ 9286f7f653bSjsing if (s->tls_session_secret_cb != NULL) { 9293395f70eSjsing SSL_CIPHER *pref_cipher = NULL; 930573b2ffaStb int master_key_length = sizeof(s->session->master_key); 931573b2ffaStb 9326f7f653bSjsing if (!s->tls_session_secret_cb(s, 933573b2ffaStb s->session->master_key, &master_key_length, NULL, 9346f7f653bSjsing &pref_cipher, s->tls_session_secret_cb_arg)) { 935573b2ffaStb SSLerror(s, ERR_R_INTERNAL_ERROR); 936573b2ffaStb goto err; 937573b2ffaStb } 938573b2ffaStb if (master_key_length <= 0) { 939573b2ffaStb SSLerror(s, ERR_R_INTERNAL_ERROR); 940573b2ffaStb goto err; 941573b2ffaStb } 942573b2ffaStb s->session->master_key_length = master_key_length; 943573b2ffaStb 94459ec10beSjsing /* 94559ec10beSjsing * XXX - this appears to be completely broken. The 94659ec10beSjsing * client cannot change the cipher at this stage, 94759ec10beSjsing * as the server has already made a selection. 94859ec10beSjsing */ 949387303bbSjsing if ((s->s3->hs.cipher = pref_cipher) == NULL) 950387303bbSjsing s->s3->hs.cipher = 9513395f70eSjsing ssl3_get_cipher_by_value(cipher_suite); 9523395f70eSjsing s->s3->flags |= SSL3_FLAGS_CCS_OK; 9533395f70eSjsing } 9543395f70eSjsing 9553395f70eSjsing if (s->session->session_id_length != 0 && 9563395f70eSjsing CBS_mem_equal(&session_id, s->session->session_id, 9573395f70eSjsing s->session->session_id_length)) { 9583395f70eSjsing if (s->sid_ctx_length != s->session->sid_ctx_length || 9593395f70eSjsing timingsafe_memcmp(s->session->sid_ctx, 9603395f70eSjsing s->sid_ctx, s->sid_ctx_length) != 0) { 9613395f70eSjsing /* actually a client application bug */ 9623395f70eSjsing al = SSL_AD_ILLEGAL_PARAMETER; 963c9d7abb7Sbeck SSLerror(s, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); 964ba06b73eStb goto fatal_err; 9653395f70eSjsing } 9663395f70eSjsing s->s3->flags |= SSL3_FLAGS_CCS_OK; 9676f7f653bSjsing s->hit = 1; 9683395f70eSjsing } else { 9693395f70eSjsing /* a miss or crap from the other end */ 9703395f70eSjsing 9713395f70eSjsing /* If we were trying for session-id reuse, make a new 9723395f70eSjsing * SSL_SESSION so we don't stuff up other people */ 9736f7f653bSjsing s->hit = 0; 9743395f70eSjsing if (s->session->session_id_length > 0) { 9753395f70eSjsing if (!ssl_get_new_session(s, 0)) { 9763395f70eSjsing al = SSL_AD_INTERNAL_ERROR; 977ba06b73eStb goto fatal_err; 9783395f70eSjsing } 9793395f70eSjsing } 9803395f70eSjsing 9813395f70eSjsing /* 9823395f70eSjsing * XXX - improve the handling for the case where there is a 9833395f70eSjsing * zero length session identifier. 9843395f70eSjsing */ 9853395f70eSjsing if (!CBS_write_bytes(&session_id, s->session->session_id, 986ef6795cfStb sizeof(s->session->session_id), 987ef6795cfStb &s->session->session_id_length)) 9883395f70eSjsing goto err; 9893395f70eSjsing 9903395f70eSjsing s->session->ssl_version = s->version; 9913395f70eSjsing } 9923395f70eSjsing 9933395f70eSjsing if ((cipher = ssl3_get_cipher_by_value(cipher_suite)) == NULL) { 9943395f70eSjsing al = SSL_AD_ILLEGAL_PARAMETER; 995c9d7abb7Sbeck SSLerror(s, SSL_R_UNKNOWN_CIPHER_RETURNED); 996ba06b73eStb goto fatal_err; 9973395f70eSjsing } 9983395f70eSjsing 9993395f70eSjsing /* TLS v1.2 only ciphersuites require v1.2 or later. */ 10003395f70eSjsing if ((cipher->algorithm_ssl & SSL_TLSV1_2) && 100102876cc3Sjsing s->s3->hs.negotiated_tls_version < TLS1_2_VERSION) { 10023395f70eSjsing al = SSL_AD_ILLEGAL_PARAMETER; 1003c9d7abb7Sbeck SSLerror(s, SSL_R_WRONG_CIPHER_RETURNED); 1004ba06b73eStb goto fatal_err; 10053395f70eSjsing } 10063395f70eSjsing 10078b316ce8Sjsing if (!ssl_cipher_in_list(SSL_get_ciphers(s), cipher)) { 10083395f70eSjsing /* we did not say we would use this cipher */ 10093395f70eSjsing al = SSL_AD_ILLEGAL_PARAMETER; 1010c9d7abb7Sbeck SSLerror(s, SSL_R_WRONG_CIPHER_RETURNED); 1011ba06b73eStb goto fatal_err; 10123395f70eSjsing } 10133395f70eSjsing 10143395f70eSjsing /* 10153395f70eSjsing * Depending on the session caching (internal/external), the cipher 10163395f70eSjsing * and/or cipher_id values may not be set. Make sure that 10173395f70eSjsing * cipher_id is set and use it for comparison. 10183395f70eSjsing */ 1019*f4fe6251Sjsing if (s->hit && (s->session->cipher_value != cipher->value)) { 10203395f70eSjsing al = SSL_AD_ILLEGAL_PARAMETER; 1021c9d7abb7Sbeck SSLerror(s, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); 1022ba06b73eStb goto fatal_err; 10233395f70eSjsing } 102402876cc3Sjsing s->s3->hs.cipher = cipher; 1025*f4fe6251Sjsing s->session->cipher_value = cipher->value; 10263395f70eSjsing 1027df40b63bSjsing if (!tls1_transcript_hash_init(s)) 1028366dc2a2Sjsing goto err; 1029366dc2a2Sjsing 10303395f70eSjsing /* 10313395f70eSjsing * Don't digest cached records if no sigalgs: we may need them for 10323395f70eSjsing * client authentication. 10333395f70eSjsing */ 1034689a9b7eSbeck if (!SSL_USE_SIGALGS(s)) 1035cef855dcSjsing tls1_transcript_free(s); 10363395f70eSjsing 10373395f70eSjsing if (!CBS_get_u8(&cbs, &compression_method)) 1038e0d5994dStb goto decode_err; 10393395f70eSjsing 10403395f70eSjsing if (compression_method != 0) { 10413395f70eSjsing al = SSL_AD_ILLEGAL_PARAMETER; 1042c9d7abb7Sbeck SSLerror(s, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); 1043ba06b73eStb goto fatal_err; 10443395f70eSjsing } 10453395f70eSjsing 10469b8a142fStb if (!tlsext_client_parse(s, SSL_TLSEXT_MSG_SH, &cbs, &al)) { 1047c9d7abb7Sbeck SSLerror(s, SSL_R_PARSE_TLSEXT); 1048ba06b73eStb goto fatal_err; 10493395f70eSjsing } 105028c1d79cSjsing 10514d783bdbSjsing if (CBS_len(&cbs) != 0) 10524d783bdbSjsing goto decode_err; 10534d783bdbSjsing 105428c1d79cSjsing /* 105528c1d79cSjsing * Determine if we need to see RI. Strictly speaking if we want to 105628c1d79cSjsing * avoid an attack we should *always* see RI even on initial server 105728c1d79cSjsing * hello because the client doesn't see any renegotiation during an 105828c1d79cSjsing * attack. However this would mean we could not connect to any server 105928c1d79cSjsing * which doesn't support RI so for the immediate future tolerate RI 106028c1d79cSjsing * absence on initial connect only. 106128c1d79cSjsing */ 106202876cc3Sjsing if (!s->s3->renegotiate_seen && 10636f7f653bSjsing !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)) { 106428c1d79cSjsing al = SSL_AD_HANDSHAKE_FAILURE; 106528c1d79cSjsing SSLerror(s, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); 1066ba06b73eStb goto fatal_err; 106728c1d79cSjsing } 106828c1d79cSjsing 10693395f70eSjsing if (ssl_check_serverhello_tlsext(s) <= 0) { 1070c9d7abb7Sbeck SSLerror(s, SSL_R_SERVERHELLO_TLSEXT); 10713395f70eSjsing goto err; 10723395f70eSjsing } 10733395f70eSjsing 10743395f70eSjsing return (1); 10753395f70eSjsing 1076e0d5994dStb decode_err: 10773395f70eSjsing /* wrong packet length */ 10783395f70eSjsing al = SSL_AD_DECODE_ERROR; 1079c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1080ba06b73eStb fatal_err: 10813395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, al); 10823395f70eSjsing err: 10833395f70eSjsing return (-1); 10843395f70eSjsing } 10853395f70eSjsing 1086eb1ff9efSjsing static int 10873395f70eSjsing ssl3_get_server_certificate(SSL *s) 10883395f70eSjsing { 10898c35f870Sjsing CBS cbs, cert_list, cert_data; 10908c35f870Sjsing STACK_OF(X509) *certs = NULL; 10918c35f870Sjsing X509 *cert = NULL; 10928c35f870Sjsing const uint8_t *p; 109315b5e1ecSjsing int al, ret; 10943395f70eSjsing 1095f2c3e964Sjsing if ((ret = ssl3_get_message(s, SSL3_ST_CR_CERT_A, 10966f7f653bSjsing SSL3_ST_CR_CERT_B, -1, s->max_cert_list)) <= 0) 1097f2c3e964Sjsing return ret; 1098f2c3e964Sjsing 1099f2c3e964Sjsing ret = -1; 11003395f70eSjsing 110102876cc3Sjsing if (s->s3->hs.tls12.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) { 110202876cc3Sjsing s->s3->hs.tls12.reuse_message = 1; 11033395f70eSjsing return (1); 11043395f70eSjsing } 11053395f70eSjsing 110602876cc3Sjsing if (s->s3->hs.tls12.message_type != SSL3_MT_CERTIFICATE) { 11073395f70eSjsing al = SSL_AD_UNEXPECTED_MESSAGE; 1108c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_MESSAGE_TYPE); 1109ba06b73eStb goto fatal_err; 11103395f70eSjsing } 11113395f70eSjsing 11128c35f870Sjsing if ((certs = sk_X509_new_null()) == NULL) { 1113c9d7abb7Sbeck SSLerror(s, ERR_R_MALLOC_FAILURE); 11143395f70eSjsing goto err; 11153395f70eSjsing } 11163395f70eSjsing 11176f7f653bSjsing if (s->init_num < 0) 1118e0d5994dStb goto decode_err; 11193395f70eSjsing 11206f7f653bSjsing CBS_init(&cbs, s->init_msg, s->init_num); 11213395f70eSjsing 11228c35f870Sjsing if (!CBS_get_u24_length_prefixed(&cbs, &cert_list)) 11238c35f870Sjsing goto decode_err; 11248c35f870Sjsing if (CBS_len(&cbs) != 0) 11258c35f870Sjsing goto decode_err; 11263395f70eSjsing 11273395f70eSjsing while (CBS_len(&cert_list) > 0) { 11288c35f870Sjsing if (!CBS_get_u24_length_prefixed(&cert_list, &cert_data)) 1129e0d5994dStb goto decode_err; 11308c35f870Sjsing p = CBS_data(&cert_data); 11318c35f870Sjsing if ((cert = d2i_X509(NULL, &p, CBS_len(&cert_data))) == NULL) { 11323395f70eSjsing al = SSL_AD_BAD_CERTIFICATE; 1133c9d7abb7Sbeck SSLerror(s, ERR_R_ASN1_LIB); 1134ba06b73eStb goto fatal_err; 11353395f70eSjsing } 11368c35f870Sjsing if (p != CBS_data(&cert_data) + CBS_len(&cert_data)) 11378c35f870Sjsing goto decode_err; 11388c35f870Sjsing if (!sk_X509_push(certs, cert)) { 1139c9d7abb7Sbeck SSLerror(s, ERR_R_MALLOC_FAILURE); 11403395f70eSjsing goto err; 11413395f70eSjsing } 11428c35f870Sjsing cert = NULL; 11433395f70eSjsing } 11443395f70eSjsing 11458c35f870Sjsing /* A server must always provide a non-empty certificate list. */ 11468c35f870Sjsing if (sk_X509_num(certs) < 1) { 11478c35f870Sjsing SSLerror(s, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 11488c35f870Sjsing goto decode_err; 11498c35f870Sjsing } 11508c35f870Sjsing 11518c35f870Sjsing if (ssl_verify_cert_chain(s, certs) <= 0 && 115215b5e1ecSjsing s->verify_mode != SSL_VERIFY_NONE) { 11533395f70eSjsing al = ssl_verify_alarm_type(s->verify_result); 1154c9d7abb7Sbeck SSLerror(s, SSL_R_CERTIFICATE_VERIFY_FAILED); 1155ba06b73eStb goto fatal_err; 11563395f70eSjsing } 11573395f70eSjsing s->session->verify_result = s->verify_result; 1158ad618767Sjsing ERR_clear_error(); 11593395f70eSjsing 1160ad618767Sjsing if (!tls_process_peer_certs(s, certs)) 1161ad618767Sjsing goto err; 116215b5e1ecSjsing 11633395f70eSjsing ret = 1; 11643395f70eSjsing 11653395f70eSjsing if (0) { 1166e0d5994dStb decode_err: 11673395f70eSjsing /* wrong packet length */ 11683395f70eSjsing al = SSL_AD_DECODE_ERROR; 1169c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1170ba06b73eStb fatal_err: 11713395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, al); 11723395f70eSjsing } 11733395f70eSjsing err: 11748c35f870Sjsing sk_X509_pop_free(certs, X509_free); 11758c35f870Sjsing X509_free(cert); 11763395f70eSjsing 11773395f70eSjsing return (ret); 11783395f70eSjsing } 11793395f70eSjsing 11803395f70eSjsing static int 1181de3a2796Sjsing ssl3_get_server_kex_dhe(SSL *s, CBS *cbs) 11823395f70eSjsing { 11837dd422f8Sjsing int decode_error, invalid_params, invalid_key; 1184de3a2796Sjsing int nid = NID_dhKeyAgreement; 11853395f70eSjsing 118602876cc3Sjsing tls_key_share_free(s->s3->hs.key_share); 118702876cc3Sjsing if ((s->s3->hs.key_share = tls_key_share_new_nid(nid)) == NULL) 11883395f70eSjsing goto err; 11893395f70eSjsing 119002876cc3Sjsing if (!tls_key_share_peer_params(s->s3->hs.key_share, cbs, 11917dd422f8Sjsing &decode_error, &invalid_params)) { 11927dd422f8Sjsing if (decode_error) { 11937dd422f8Sjsing SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 11947dd422f8Sjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 11957dd422f8Sjsing } 11967dd422f8Sjsing goto err; 11977dd422f8Sjsing } 119802876cc3Sjsing if (!tls_key_share_peer_public(s->s3->hs.key_share, cbs, 11997dd422f8Sjsing &decode_error, &invalid_key)) { 12007dd422f8Sjsing if (decode_error) { 12017dd422f8Sjsing SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 12027dd422f8Sjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 12037dd422f8Sjsing } 12047dd422f8Sjsing goto err; 12057dd422f8Sjsing } 12063395f70eSjsing 1207dfc82f2aSjsing if (invalid_params) { 1208c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_DH_P_LENGTH); 12097dd422f8Sjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); 12103395f70eSjsing goto err; 12113395f70eSjsing } 12122caab47bSjsing if (invalid_key) { 12132caab47bSjsing SSLerror(s, SSL_R_BAD_DH_PUB_KEY_LENGTH); 12147dd422f8Sjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); 12152caab47bSjsing goto err; 12162caab47bSjsing } 12173395f70eSjsing 12182c1036d8Stb if (!tls_key_share_peer_security(s, s->s3->hs.key_share)) { 12192c1036d8Stb SSLerror(s, SSL_R_DH_KEY_TOO_SMALL); 12202c1036d8Stb ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 12212c1036d8Stb return 0; 12222c1036d8Stb } 12232c1036d8Stb 12249ccbb9b1Sjsing return 1; 12253395f70eSjsing 12263395f70eSjsing err: 12279ccbb9b1Sjsing return 0; 12283395f70eSjsing } 12293395f70eSjsing 12303395f70eSjsing static int 1231de3a2796Sjsing ssl3_get_server_kex_ecdhe(SSL *s, CBS *cbs) 12323395f70eSjsing { 12333395f70eSjsing uint8_t curve_type; 1234c5270c5dStb uint16_t group_id; 12357dd422f8Sjsing int decode_error; 1236de3a2796Sjsing CBS public; 12373395f70eSjsing 1238ca11234cSjsing if (!CBS_get_u8(cbs, &curve_type)) 1239ca11234cSjsing goto decode_err; 1240c5270c5dStb if (!CBS_get_u16(cbs, &group_id)) 1241ca11234cSjsing goto decode_err; 1242ca11234cSjsing 12433395f70eSjsing /* Only named curves are supported. */ 1244ca11234cSjsing if (curve_type != NAMED_CURVE_TYPE) { 1245ca11234cSjsing SSLerror(s, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); 1246892af70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1247ca11234cSjsing goto err; 12483395f70eSjsing } 12493395f70eSjsing 1250ca11234cSjsing if (!CBS_get_u8_length_prefixed(cbs, &public)) 1251ca11234cSjsing goto decode_err; 1252ca11234cSjsing 12533395f70eSjsing /* 1254c5270c5dStb * Check that the group is one of our preferences - if it is not, 1255c5270c5dStb * the server has sent us an invalid group. 12563395f70eSjsing */ 1257c5270c5dStb if (!tls1_check_group(s, group_id)) { 1258c9d7abb7Sbeck SSLerror(s, SSL_R_WRONG_CURVE); 1259ca11234cSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); 1260ca11234cSjsing goto err; 12613395f70eSjsing } 12623395f70eSjsing 126302876cc3Sjsing tls_key_share_free(s->s3->hs.key_share); 1264c5270c5dStb if ((s->s3->hs.key_share = tls_key_share_new(group_id)) == NULL) 1265ca11234cSjsing goto err; 12663395f70eSjsing 126702876cc3Sjsing if (!tls_key_share_peer_public(s->s3->hs.key_share, &public, 12687dd422f8Sjsing &decode_error, NULL)) { 12697dd422f8Sjsing if (decode_error) 12707dd422f8Sjsing goto decode_err; 12713395f70eSjsing goto err; 12727dd422f8Sjsing } 12733395f70eSjsing 12749ccbb9b1Sjsing return 1; 12753395f70eSjsing 1276e0d5994dStb decode_err: 1277c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 12787dd422f8Sjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 12793395f70eSjsing err: 12809ccbb9b1Sjsing return 0; 12813395f70eSjsing } 12823395f70eSjsing 1283eb1ff9efSjsing static int 12843395f70eSjsing ssl3_get_server_key_exchange(SSL *s) 12853395f70eSjsing { 1286bf68ec7fStb CBB cbb; 1287bf68ec7fStb CBS cbs, params, signature; 128830838a50Stb EVP_MD_CTX *md_ctx; 1289bf68ec7fStb unsigned char *signed_params = NULL; 1290bf68ec7fStb size_t signed_params_len; 1291bf68ec7fStb size_t params_len; 1292f2c3e964Sjsing long alg_k, alg_a; 1293f2c3e964Sjsing int al, ret; 1294af461493Sjsing 1295bf68ec7fStb memset(&cbb, 0, sizeof(cbb)); 1296bf68ec7fStb 129702876cc3Sjsing alg_k = s->s3->hs.cipher->algorithm_mkey; 129802876cc3Sjsing alg_a = s->s3->hs.cipher->algorithm_auth; 12993395f70eSjsing 13003395f70eSjsing /* 13013395f70eSjsing * Use same message size as in ssl3_get_certificate_request() 13023395f70eSjsing * as ServerKeyExchange message may be skipped. 13033395f70eSjsing */ 1304f2c3e964Sjsing if ((ret = ssl3_get_message(s, SSL3_ST_CR_KEY_EXCH_A, 13056f7f653bSjsing SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list)) <= 0) 1306f2c3e964Sjsing return ret; 13073395f70eSjsing 130830838a50Stb if ((md_ctx = EVP_MD_CTX_new()) == NULL) 130930838a50Stb goto err; 131030838a50Stb 13116f7f653bSjsing if (s->init_num < 0) 1312af461493Sjsing goto err; 1313af461493Sjsing 13146f7f653bSjsing CBS_init(&cbs, s->init_msg, s->init_num); 13153395f70eSjsing 131602876cc3Sjsing if (s->s3->hs.tls12.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) { 13173395f70eSjsing /* 13183395f70eSjsing * Do not skip server key exchange if this cipher suite uses 13193395f70eSjsing * ephemeral keys. 13203395f70eSjsing */ 13213395f70eSjsing if (alg_k & (SSL_kDHE|SSL_kECDHE)) { 1322c9d7abb7Sbeck SSLerror(s, SSL_R_UNEXPECTED_MESSAGE); 13233395f70eSjsing al = SSL_AD_UNEXPECTED_MESSAGE; 1324ba06b73eStb goto fatal_err; 13253395f70eSjsing } 13263395f70eSjsing 132702876cc3Sjsing s->s3->hs.tls12.reuse_message = 1; 132830838a50Stb EVP_MD_CTX_free(md_ctx); 13293395f70eSjsing return (1); 13303395f70eSjsing } 13313395f70eSjsing 1332bf68ec7fStb if (!CBB_init(&cbb, 0)) 1333bf68ec7fStb goto err; 1334bf68ec7fStb if (!CBB_add_bytes(&cbb, s->s3->client_random, SSL3_RANDOM_SIZE)) 1335bf68ec7fStb goto err; 1336bf68ec7fStb if (!CBB_add_bytes(&cbb, s->s3->server_random, SSL3_RANDOM_SIZE)) 1337bf68ec7fStb goto err; 1338bf68ec7fStb 1339bf68ec7fStb CBS_dup(&cbs, ¶ms); 13403395f70eSjsing 13413395f70eSjsing if (alg_k & SSL_kDHE) { 1342de3a2796Sjsing if (!ssl3_get_server_kex_dhe(s, &cbs)) 13433395f70eSjsing goto err; 13443395f70eSjsing } else if (alg_k & SSL_kECDHE) { 1345de3a2796Sjsing if (!ssl3_get_server_kex_ecdhe(s, &cbs)) 13463395f70eSjsing goto err; 13473395f70eSjsing } else if (alg_k != 0) { 13483395f70eSjsing al = SSL_AD_UNEXPECTED_MESSAGE; 1349c9d7abb7Sbeck SSLerror(s, SSL_R_UNEXPECTED_MESSAGE); 1350ba06b73eStb goto fatal_err; 13513395f70eSjsing } 13523395f70eSjsing 1353bf68ec7fStb if ((params_len = CBS_offset(&cbs)) > CBS_len(¶ms)) 1354bf68ec7fStb goto err; 1355bf68ec7fStb if (!CBB_add_bytes(&cbb, CBS_data(¶ms), params_len)) 1356bf68ec7fStb goto err; 1357bf68ec7fStb if (!CBB_finish(&cbb, &signed_params, &signed_params_len)) 1358bf68ec7fStb goto err; 13593395f70eSjsing 13603395f70eSjsing /* if it was signed, check the signature */ 1361de3a2796Sjsing if ((alg_a & SSL_aNULL) == 0) { 1362dab9fedaSjsing uint16_t sigalg_value = SIGALG_NONE; 1363fbe97c86Sbeck const struct ssl_sigalg *sigalg; 1364dab9fedaSjsing EVP_PKEY_CTX *pctx; 1365de3a2796Sjsing EVP_PKEY *pkey = NULL; 1366de3a2796Sjsing 136715b5e1ecSjsing if ((alg_a & SSL_aRSA) != 0 && 136815b5e1ecSjsing s->session->peer_cert_type == SSL_PKEY_RSA) { 136915b5e1ecSjsing pkey = X509_get0_pubkey(s->session->peer_cert); 137015b5e1ecSjsing } else if ((alg_a & SSL_aECDSA) != 0 && 137115b5e1ecSjsing s->session->peer_cert_type == SSL_PKEY_ECC) { 137215b5e1ecSjsing pkey = X509_get0_pubkey(s->session->peer_cert); 1373de3a2796Sjsing } 1374de3a2796Sjsing if (pkey == NULL) { 1375de3a2796Sjsing al = SSL_AD_ILLEGAL_PARAMETER; 1376de3a2796Sjsing SSLerror(s, SSL_R_UNKNOWN_CERTIFICATE_TYPE); 137702f90b19Stb goto fatal_err; 1378de3a2796Sjsing } 1379bfb305e8Sbeck 1380bfb305e8Sbeck if (SSL_USE_SIGALGS(s)) { 1381fbe97c86Sbeck if (!CBS_get_u16(&cbs, &sigalg_value)) 1382e0d5994dStb goto decode_err; 1383bfb305e8Sbeck } 1384af461493Sjsing if (!CBS_get_u16_length_prefixed(&cbs, &signature)) 1385e0d5994dStb goto decode_err; 1386af461493Sjsing if (CBS_len(&signature) > EVP_PKEY_size(pkey)) { 13873395f70eSjsing al = SSL_AD_DECODE_ERROR; 1388c9d7abb7Sbeck SSLerror(s, SSL_R_WRONG_SIGNATURE_LENGTH); 1389ba06b73eStb goto fatal_err; 13903395f70eSjsing } 13913395f70eSjsing 1392dab9fedaSjsing if ((sigalg = ssl_sigalg_for_peer(s, pkey, 1393dab9fedaSjsing sigalg_value)) == NULL) { 1394dab9fedaSjsing al = SSL_AD_DECODE_ERROR; 1395dab9fedaSjsing goto fatal_err; 1396dab9fedaSjsing } 139702876cc3Sjsing s->s3->hs.peer_sigalg = sigalg; 1398dab9fedaSjsing 139930838a50Stb if (!EVP_DigestVerifyInit(md_ctx, &pctx, sigalg->md(), 1400dab9fedaSjsing NULL, pkey)) 1401c65d0191Sjsing goto err; 1402844b8990Sbeck if ((sigalg->flags & SIGALG_FLAG_RSA_PSS) && 1403844b8990Sbeck (!EVP_PKEY_CTX_set_rsa_padding(pctx, 1404844b8990Sbeck RSA_PKCS1_PSS_PADDING) || 1405844b8990Sbeck !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1))) 1406844b8990Sbeck goto err; 1407bf68ec7fStb if (EVP_DigestVerify(md_ctx, CBS_data(&signature), 1408bf68ec7fStb CBS_len(&signature), signed_params, signed_params_len) <= 0) { 14093395f70eSjsing al = SSL_AD_DECRYPT_ERROR; 1410c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_SIGNATURE); 1411ba06b73eStb goto fatal_err; 14123395f70eSjsing } 1413af461493Sjsing } 1414af461493Sjsing 1415af461493Sjsing if (CBS_len(&cbs) != 0) { 14163395f70eSjsing al = SSL_AD_DECODE_ERROR; 1417c9d7abb7Sbeck SSLerror(s, SSL_R_EXTRA_DATA_IN_MESSAGE); 1418ba06b73eStb goto fatal_err; 14193395f70eSjsing } 14203395f70eSjsing 142130838a50Stb EVP_MD_CTX_free(md_ctx); 1422bf68ec7fStb free(signed_params); 14233395f70eSjsing 14243395f70eSjsing return (1); 14253395f70eSjsing 1426e0d5994dStb decode_err: 14273395f70eSjsing al = SSL_AD_DECODE_ERROR; 1428c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 14293395f70eSjsing 1430ba06b73eStb fatal_err: 14313395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, al); 14323395f70eSjsing 14333395f70eSjsing err: 1434bf68ec7fStb CBB_cleanup(&cbb); 143530838a50Stb EVP_MD_CTX_free(md_ctx); 1436bf68ec7fStb free(signed_params); 14373395f70eSjsing 14383395f70eSjsing return (-1); 14393395f70eSjsing } 14403395f70eSjsing 1441eb1ff9efSjsing static int 14423395f70eSjsing ssl3_get_certificate_request(SSL *s) 14433395f70eSjsing { 14441f38de1dSjsing CBS cert_request, cert_types, rdn_list; 14453395f70eSjsing X509_NAME *xn = NULL; 14463395f70eSjsing const unsigned char *q; 14473395f70eSjsing STACK_OF(X509_NAME) *ca_sk = NULL; 1448f2c3e964Sjsing int ret; 14493395f70eSjsing 1450f2c3e964Sjsing if ((ret = ssl3_get_message(s, SSL3_ST_CR_CERT_REQ_A, 14516f7f653bSjsing SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list)) <= 0) 1452f2c3e964Sjsing return ret; 1453f2c3e964Sjsing 1454f2c3e964Sjsing ret = 0; 14553395f70eSjsing 145602876cc3Sjsing s->s3->hs.tls12.cert_request = 0; 14573395f70eSjsing 145802876cc3Sjsing if (s->s3->hs.tls12.message_type == SSL3_MT_SERVER_DONE) { 145902876cc3Sjsing s->s3->hs.tls12.reuse_message = 1; 14603395f70eSjsing /* 14613395f70eSjsing * If we get here we don't need any cached handshake records 14623395f70eSjsing * as we wont be doing client auth. 14633395f70eSjsing */ 1464cef855dcSjsing tls1_transcript_free(s); 14653395f70eSjsing return (1); 14663395f70eSjsing } 14673395f70eSjsing 146802876cc3Sjsing if (s->s3->hs.tls12.message_type != SSL3_MT_CERTIFICATE_REQUEST) { 14693395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); 1470c9d7abb7Sbeck SSLerror(s, SSL_R_WRONG_MESSAGE_TYPE); 14713395f70eSjsing goto err; 14723395f70eSjsing } 14733395f70eSjsing 14743395f70eSjsing /* TLS does not like anon-DH with client cert */ 147502876cc3Sjsing if (s->s3->hs.cipher->algorithm_auth & SSL_aNULL) { 14763395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); 1477c9d7abb7Sbeck SSLerror(s, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER); 14783395f70eSjsing goto err; 14793395f70eSjsing } 14803395f70eSjsing 14816f7f653bSjsing if (s->init_num < 0) 1482e0d5994dStb goto decode_err; 14836f7f653bSjsing CBS_init(&cert_request, s->init_msg, s->init_num); 14843395f70eSjsing 14853395f70eSjsing if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) { 1486c9d7abb7Sbeck SSLerror(s, ERR_R_MALLOC_FAILURE); 14873395f70eSjsing goto err; 14883395f70eSjsing } 14893395f70eSjsing 14901f38de1dSjsing if (!CBS_get_u8_length_prefixed(&cert_request, &cert_types)) 1491e0d5994dStb goto decode_err; 14923395f70eSjsing 14933395f70eSjsing if (SSL_USE_SIGALGS(s)) { 14943395f70eSjsing CBS sigalgs; 14953395f70eSjsing 14963395f70eSjsing if (CBS_len(&cert_request) < 2) { 1497c9d7abb7Sbeck SSLerror(s, SSL_R_DATA_LENGTH_TOO_LONG); 14983395f70eSjsing goto err; 14993395f70eSjsing } 15003395f70eSjsing if (!CBS_get_u16_length_prefixed(&cert_request, &sigalgs)) { 15013395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1502c9d7abb7Sbeck SSLerror(s, SSL_R_DATA_LENGTH_TOO_LONG); 15033395f70eSjsing goto err; 15043395f70eSjsing } 150596b13b45Sjsing if (CBS_len(&sigalgs) % 2 != 0 || CBS_len(&sigalgs) > 64) { 15063395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1507c9d7abb7Sbeck SSLerror(s, SSL_R_SIGNATURE_ALGORITHMS_ERROR); 15083395f70eSjsing goto err; 15093395f70eSjsing } 151002876cc3Sjsing if (!CBS_stow(&sigalgs, &s->s3->hs.sigalgs, 151102876cc3Sjsing &s->s3->hs.sigalgs_len)) 151296b13b45Sjsing goto err; 15133395f70eSjsing } 15143395f70eSjsing 15153395f70eSjsing /* get the CA RDNs */ 15163395f70eSjsing if (CBS_len(&cert_request) < 2) { 1517c9d7abb7Sbeck SSLerror(s, SSL_R_DATA_LENGTH_TOO_LONG); 15183395f70eSjsing goto err; 15193395f70eSjsing } 15203395f70eSjsing 15213395f70eSjsing if (!CBS_get_u16_length_prefixed(&cert_request, &rdn_list) || 15223395f70eSjsing CBS_len(&cert_request) != 0) { 15233395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1524c9d7abb7Sbeck SSLerror(s, SSL_R_LENGTH_MISMATCH); 15253395f70eSjsing goto err; 15263395f70eSjsing } 15273395f70eSjsing 15283395f70eSjsing while (CBS_len(&rdn_list) > 0) { 15293395f70eSjsing CBS rdn; 15303395f70eSjsing 15313395f70eSjsing if (CBS_len(&rdn_list) < 2) { 1532c9d7abb7Sbeck SSLerror(s, SSL_R_DATA_LENGTH_TOO_LONG); 15333395f70eSjsing goto err; 15343395f70eSjsing } 15353395f70eSjsing 15363395f70eSjsing if (!CBS_get_u16_length_prefixed(&rdn_list, &rdn)) { 15373395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1538c9d7abb7Sbeck SSLerror(s, SSL_R_CA_DN_TOO_LONG); 15393395f70eSjsing goto err; 15403395f70eSjsing } 15413395f70eSjsing 15423395f70eSjsing q = CBS_data(&rdn); 15433395f70eSjsing if ((xn = d2i_X509_NAME(NULL, &q, CBS_len(&rdn))) == NULL) { 15443395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, 15453395f70eSjsing SSL_AD_DECODE_ERROR); 1546c9d7abb7Sbeck SSLerror(s, ERR_R_ASN1_LIB); 15473395f70eSjsing goto err; 15483395f70eSjsing } 15493395f70eSjsing 15503395f70eSjsing if (q != CBS_data(&rdn) + CBS_len(&rdn)) { 15513395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1552c9d7abb7Sbeck SSLerror(s, SSL_R_CA_DN_LENGTH_MISMATCH); 15533395f70eSjsing goto err; 15543395f70eSjsing } 15553395f70eSjsing if (!sk_X509_NAME_push(ca_sk, xn)) { 1556c9d7abb7Sbeck SSLerror(s, ERR_R_MALLOC_FAILURE); 15573395f70eSjsing goto err; 15583395f70eSjsing } 15593395f70eSjsing xn = NULL; /* avoid free in err block */ 15603395f70eSjsing } 15613395f70eSjsing 15623395f70eSjsing /* we should setup a certificate to return.... */ 156302876cc3Sjsing s->s3->hs.tls12.cert_request = 1; 156402876cc3Sjsing sk_X509_NAME_pop_free(s->s3->hs.tls12.ca_names, X509_NAME_free); 156502876cc3Sjsing s->s3->hs.tls12.ca_names = ca_sk; 15663395f70eSjsing ca_sk = NULL; 15673395f70eSjsing 15683395f70eSjsing ret = 1; 15693395f70eSjsing if (0) { 1570e0d5994dStb decode_err: 1571c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 15723395f70eSjsing } 15733395f70eSjsing err: 15743395f70eSjsing X509_NAME_free(xn); 15753395f70eSjsing sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); 15763395f70eSjsing return (ret); 15773395f70eSjsing } 15783395f70eSjsing 15793395f70eSjsing static int 15803395f70eSjsing ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b) 15813395f70eSjsing { 15823395f70eSjsing return (X509_NAME_cmp(*a, *b)); 15833395f70eSjsing } 15843395f70eSjsing 1585eb1ff9efSjsing static int 15863395f70eSjsing ssl3_get_new_session_ticket(SSL *s) 15873395f70eSjsing { 15883395f70eSjsing uint32_t lifetime_hint; 15893395f70eSjsing CBS cbs, session_ticket; 15900dbf1972Stb unsigned int session_id_length = 0; 1591f2c3e964Sjsing int al, ret; 15923395f70eSjsing 1593f2c3e964Sjsing if ((ret = ssl3_get_message(s, SSL3_ST_CR_SESSION_TICKET_A, 1594f2c3e964Sjsing SSL3_ST_CR_SESSION_TICKET_B, -1, 16384)) <= 0) 1595f2c3e964Sjsing return ret; 1596f2c3e964Sjsing 159702876cc3Sjsing if (s->s3->hs.tls12.message_type == SSL3_MT_FINISHED) { 159802876cc3Sjsing s->s3->hs.tls12.reuse_message = 1; 15993395f70eSjsing return (1); 16003395f70eSjsing } 160102876cc3Sjsing if (s->s3->hs.tls12.message_type != SSL3_MT_NEWSESSION_TICKET) { 16023395f70eSjsing al = SSL_AD_UNEXPECTED_MESSAGE; 1603c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_MESSAGE_TYPE); 1604ba06b73eStb goto fatal_err; 16053395f70eSjsing } 16063395f70eSjsing 16076f7f653bSjsing if (s->init_num < 0) { 16083395f70eSjsing al = SSL_AD_DECODE_ERROR; 1609c9d7abb7Sbeck SSLerror(s, SSL_R_LENGTH_MISMATCH); 1610ba06b73eStb goto fatal_err; 16113395f70eSjsing } 16123395f70eSjsing 16136f7f653bSjsing CBS_init(&cbs, s->init_msg, s->init_num); 16143395f70eSjsing if (!CBS_get_u32(&cbs, &lifetime_hint) || 16153395f70eSjsing !CBS_get_u16_length_prefixed(&cbs, &session_ticket) || 16163395f70eSjsing CBS_len(&cbs) != 0) { 16173395f70eSjsing al = SSL_AD_DECODE_ERROR; 1618c9d7abb7Sbeck SSLerror(s, SSL_R_LENGTH_MISMATCH); 1619ba06b73eStb goto fatal_err; 16203395f70eSjsing } 162118631d23Sjsing s->session->tlsext_tick_lifetime_hint = lifetime_hint; 16223395f70eSjsing 16233395f70eSjsing if (!CBS_stow(&session_ticket, &s->session->tlsext_tick, 16243395f70eSjsing &s->session->tlsext_ticklen)) { 1625c9d7abb7Sbeck SSLerror(s, ERR_R_MALLOC_FAILURE); 16263395f70eSjsing goto err; 16273395f70eSjsing } 16283395f70eSjsing 16293395f70eSjsing /* 163040ca958bSjmc * There are two ways to detect a resumed ticket session. 16313395f70eSjsing * One is to set an appropriate session ID and then the server 16323395f70eSjsing * must return a match in ServerHello. This allows the normal 16333395f70eSjsing * client session ID matching to work and we know much 16343395f70eSjsing * earlier that the ticket has been accepted. 16353395f70eSjsing * 16363395f70eSjsing * The other way is to set zero length session ID when the 16373395f70eSjsing * ticket is presented and rely on the handshake to determine 16383395f70eSjsing * session resumption. 16393395f70eSjsing * 16403395f70eSjsing * We choose the former approach because this fits in with 16413395f70eSjsing * assumptions elsewhere in OpenSSL. The session ID is set 1642c00a0ce3Stb * to the SHA256 hash of the ticket. 16433395f70eSjsing */ 164404bb98f1Stb /* XXX - ensure this doesn't overflow session_id if hash is changed. */ 16450dbf1972Stb if (!EVP_Digest(CBS_data(&session_ticket), CBS_len(&session_ticket), 16460dbf1972Stb s->session->session_id, &session_id_length, EVP_sha256(), NULL)) { 16470dbf1972Stb al = SSL_AD_INTERNAL_ERROR; 16480dbf1972Stb SSLerror(s, ERR_R_EVP_LIB); 16490dbf1972Stb goto fatal_err; 16500dbf1972Stb } 16510dbf1972Stb s->session->session_id_length = session_id_length; 1652690cd05dStb 1653690cd05dStb return (1); 1654690cd05dStb 1655ba06b73eStb fatal_err: 16563395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, al); 16573395f70eSjsing err: 16583395f70eSjsing return (-1); 16593395f70eSjsing } 16603395f70eSjsing 1661eb1ff9efSjsing static int 16623395f70eSjsing ssl3_get_cert_status(SSL *s) 16633395f70eSjsing { 16643395f70eSjsing CBS cert_status, response; 16653395f70eSjsing uint8_t status_type; 1666f2c3e964Sjsing int al, ret; 16673395f70eSjsing 1668f2c3e964Sjsing if ((ret = ssl3_get_message(s, SSL3_ST_CR_CERT_STATUS_A, 1669f2c3e964Sjsing SSL3_ST_CR_CERT_STATUS_B, -1, 16384)) <= 0) 1670f2c3e964Sjsing return ret; 16713395f70eSjsing 167202876cc3Sjsing if (s->s3->hs.tls12.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) { 1673cb9be768Sbeck /* 1674cb9be768Sbeck * Tell the callback the server did not send us an OSCP 1675cb9be768Sbeck * response, and has decided to head directly to key exchange. 1676cb9be768Sbeck */ 16776f7f653bSjsing if (s->ctx->tlsext_status_cb) { 16786f7f653bSjsing free(s->tlsext_ocsp_resp); 16796f7f653bSjsing s->tlsext_ocsp_resp = NULL; 16806f7f653bSjsing s->tlsext_ocsp_resp_len = 0; 1681cb9be768Sbeck 16826f7f653bSjsing ret = s->ctx->tlsext_status_cb(s, 16836f7f653bSjsing s->ctx->tlsext_status_arg); 1684cb9be768Sbeck if (ret == 0) { 1685cb9be768Sbeck al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; 1686cb9be768Sbeck SSLerror(s, SSL_R_INVALID_STATUS_RESPONSE); 1687cb9be768Sbeck goto fatal_err; 1688cb9be768Sbeck } 1689cb9be768Sbeck if (ret < 0) { 1690cb9be768Sbeck al = SSL_AD_INTERNAL_ERROR; 1691cb9be768Sbeck SSLerror(s, ERR_R_MALLOC_FAILURE); 1692cb9be768Sbeck goto fatal_err; 1693cb9be768Sbeck } 1694cb9be768Sbeck } 169502876cc3Sjsing s->s3->hs.tls12.reuse_message = 1; 1696cb9be768Sbeck return (1); 1697cb9be768Sbeck } 1698cb9be768Sbeck 169902876cc3Sjsing if (s->s3->hs.tls12.message_type != SSL3_MT_CERTIFICATE && 170002876cc3Sjsing s->s3->hs.tls12.message_type != SSL3_MT_CERTIFICATE_STATUS) { 1701cb9be768Sbeck al = SSL_AD_UNEXPECTED_MESSAGE; 1702cb9be768Sbeck SSLerror(s, SSL_R_BAD_MESSAGE_TYPE); 1703cb9be768Sbeck goto fatal_err; 1704cb9be768Sbeck } 1705cb9be768Sbeck 17066f7f653bSjsing if (s->init_num < 0) { 17073395f70eSjsing /* need at least status type + length */ 17083395f70eSjsing al = SSL_AD_DECODE_ERROR; 1709c9d7abb7Sbeck SSLerror(s, SSL_R_LENGTH_MISMATCH); 1710ba06b73eStb goto fatal_err; 17113395f70eSjsing } 17123395f70eSjsing 17136f7f653bSjsing CBS_init(&cert_status, s->init_msg, s->init_num); 17143395f70eSjsing if (!CBS_get_u8(&cert_status, &status_type) || 17153395f70eSjsing CBS_len(&cert_status) < 3) { 17163395f70eSjsing /* need at least status type + length */ 17173395f70eSjsing al = SSL_AD_DECODE_ERROR; 1718c9d7abb7Sbeck SSLerror(s, SSL_R_LENGTH_MISMATCH); 1719ba06b73eStb goto fatal_err; 17203395f70eSjsing } 17213395f70eSjsing 17223395f70eSjsing if (status_type != TLSEXT_STATUSTYPE_ocsp) { 17233395f70eSjsing al = SSL_AD_DECODE_ERROR; 1724c9d7abb7Sbeck SSLerror(s, SSL_R_UNSUPPORTED_STATUS_TYPE); 1725ba06b73eStb goto fatal_err; 17263395f70eSjsing } 17273395f70eSjsing 17283395f70eSjsing if (!CBS_get_u24_length_prefixed(&cert_status, &response) || 17293395f70eSjsing CBS_len(&cert_status) != 0) { 17303395f70eSjsing al = SSL_AD_DECODE_ERROR; 1731c9d7abb7Sbeck SSLerror(s, SSL_R_LENGTH_MISMATCH); 1732ba06b73eStb goto fatal_err; 17333395f70eSjsing } 17343395f70eSjsing 17356f7f653bSjsing if (!CBS_stow(&response, &s->tlsext_ocsp_resp, 17366f7f653bSjsing &s->tlsext_ocsp_resp_len)) { 17373395f70eSjsing al = SSL_AD_INTERNAL_ERROR; 1738c9d7abb7Sbeck SSLerror(s, ERR_R_MALLOC_FAILURE); 1739ba06b73eStb goto fatal_err; 17403395f70eSjsing } 17413395f70eSjsing 17426f7f653bSjsing if (s->ctx->tlsext_status_cb) { 17436f7f653bSjsing ret = s->ctx->tlsext_status_cb(s, 17446f7f653bSjsing s->ctx->tlsext_status_arg); 17453395f70eSjsing if (ret == 0) { 17463395f70eSjsing al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; 1747c9d7abb7Sbeck SSLerror(s, SSL_R_INVALID_STATUS_RESPONSE); 1748ba06b73eStb goto fatal_err; 17493395f70eSjsing } 17503395f70eSjsing if (ret < 0) { 17513395f70eSjsing al = SSL_AD_INTERNAL_ERROR; 1752c9d7abb7Sbeck SSLerror(s, ERR_R_MALLOC_FAILURE); 1753ba06b73eStb goto fatal_err; 17543395f70eSjsing } 17553395f70eSjsing } 17563395f70eSjsing return (1); 1757ba06b73eStb fatal_err: 17583395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, al); 17593395f70eSjsing return (-1); 17603395f70eSjsing } 17613395f70eSjsing 1762eb1ff9efSjsing static int 17633395f70eSjsing ssl3_get_server_done(SSL *s) 17643395f70eSjsing { 1765f2c3e964Sjsing int ret; 17663395f70eSjsing 1767f2c3e964Sjsing if ((ret = ssl3_get_message(s, SSL3_ST_CR_SRVR_DONE_A, 17683395f70eSjsing SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE, 1769f2c3e964Sjsing 30 /* should be very small, like 0 :-) */)) <= 0) 1770f2c3e964Sjsing return ret; 1771dc77a1b6Sjsing 17726f7f653bSjsing if (s->init_num != 0) { 17733395f70eSjsing /* should contain no data */ 17743395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1775c9d7abb7Sbeck SSLerror(s, SSL_R_LENGTH_MISMATCH); 1776f2c3e964Sjsing return -1; 17773395f70eSjsing } 1778f2c3e964Sjsing 1779f2c3e964Sjsing return 1; 17803395f70eSjsing } 17813395f70eSjsing 17823395f70eSjsing static int 17837cf55da3Sjsing ssl3_send_client_kex_rsa(SSL *s, CBB *cbb) 17843395f70eSjsing { 17853395f70eSjsing unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH]; 17863395f70eSjsing unsigned char *enc_pms = NULL; 178701f29c58Sjsing uint16_t max_legacy_version; 178815b5e1ecSjsing EVP_PKEY *pkey; 17895c273ab3Stb RSA *rsa; 17909ccbb9b1Sjsing int ret = 0; 17913395f70eSjsing int enc_len; 17923395f70eSjsing CBB epms; 17933395f70eSjsing 17943395f70eSjsing /* 17953395f70eSjsing * RSA-Encrypted Premaster Secret Message - RFC 5246 section 7.4.7.1. 17963395f70eSjsing */ 17973395f70eSjsing 179815b5e1ecSjsing pkey = X509_get0_pubkey(s->session->peer_cert); 17995c273ab3Stb if (pkey == NULL || (rsa = EVP_PKEY_get0_RSA(pkey)) == NULL) { 1800c9d7abb7Sbeck SSLerror(s, ERR_R_INTERNAL_ERROR); 18013395f70eSjsing goto err; 18023395f70eSjsing } 18033395f70eSjsing 180401f29c58Sjsing /* 180501f29c58Sjsing * Our maximum legacy protocol version - while RFC 5246 section 7.4.7.1 180601f29c58Sjsing * says "The latest (newest) version supported by the client", if we're 180701f29c58Sjsing * doing RSA key exchange then we have to presume that we're talking to 180801f29c58Sjsing * a server that does not understand the supported versions extension 180901f29c58Sjsing * and therefore our maximum version is that sent in the ClientHello. 181001f29c58Sjsing */ 181101f29c58Sjsing if (!ssl_max_legacy_version(s, &max_legacy_version)) 181201f29c58Sjsing goto err; 181301f29c58Sjsing pms[0] = max_legacy_version >> 8; 181401f29c58Sjsing pms[1] = max_legacy_version & 0xff; 18153395f70eSjsing arc4random_buf(&pms[2], sizeof(pms) - 2); 18163395f70eSjsing 18175c273ab3Stb if ((enc_pms = malloc(RSA_size(rsa))) == NULL) { 1818c9d7abb7Sbeck SSLerror(s, ERR_R_MALLOC_FAILURE); 18193395f70eSjsing goto err; 18203395f70eSjsing } 18213395f70eSjsing 18225c273ab3Stb enc_len = RSA_public_encrypt(sizeof(pms), pms, enc_pms, rsa, 18233395f70eSjsing RSA_PKCS1_PADDING); 18243395f70eSjsing if (enc_len <= 0) { 1825c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_RSA_ENCRYPT); 18263395f70eSjsing goto err; 18273395f70eSjsing } 18283395f70eSjsing 18293395f70eSjsing if (!CBB_add_u16_length_prefixed(cbb, &epms)) 18303395f70eSjsing goto err; 18313395f70eSjsing if (!CBB_add_bytes(&epms, enc_pms, enc_len)) 18323395f70eSjsing goto err; 18333395f70eSjsing if (!CBB_flush(cbb)) 18343395f70eSjsing goto err; 18353395f70eSjsing 1836155a0ed0Sjsing if (!tls12_derive_master_secret(s, pms, sizeof(pms))) 1837155a0ed0Sjsing goto err; 18383395f70eSjsing 18393395f70eSjsing ret = 1; 18403395f70eSjsing 18413395f70eSjsing err: 18423395f70eSjsing explicit_bzero(pms, sizeof(pms)); 18433395f70eSjsing free(enc_pms); 18443395f70eSjsing 18459ccbb9b1Sjsing return ret; 18463395f70eSjsing } 18473395f70eSjsing 18483395f70eSjsing static int 18497cf55da3Sjsing ssl3_send_client_kex_dhe(SSL *s, CBB *cbb) 18503395f70eSjsing { 185176bb7203Sjsing uint8_t *key = NULL; 185276bb7203Sjsing size_t key_len = 0; 18539ccbb9b1Sjsing int ret = 0; 18543395f70eSjsing 185576bb7203Sjsing /* Ensure that we have an ephemeral key from the server for DHE. */ 185602876cc3Sjsing if (s->s3->hs.key_share == NULL) { 18573395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1858c9d7abb7Sbeck SSLerror(s, SSL_R_UNABLE_TO_FIND_DH_PARAMETERS); 18593395f70eSjsing goto err; 18603395f70eSjsing } 18613395f70eSjsing 186202876cc3Sjsing if (!tls_key_share_generate(s->s3->hs.key_share)) 18633395f70eSjsing goto err; 186402876cc3Sjsing if (!tls_key_share_public(s->s3->hs.key_share, cbb)) 18653395f70eSjsing goto err; 186602876cc3Sjsing if (!tls_key_share_derive(s->s3->hs.key_share, &key, &key_len)) 186769428dcaSjsing goto err; 186876bb7203Sjsing 186957e262bfStb if (!tls_key_share_peer_security(s, s->s3->hs.key_share)) { 187057e262bfStb SSLerror(s, SSL_R_DH_KEY_TOO_SMALL); 187157e262bfStb ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 187257e262bfStb return 0; 187357e262bfStb } 187457e262bfStb 1875155a0ed0Sjsing if (!tls12_derive_master_secret(s, key, key_len)) 1876155a0ed0Sjsing goto err; 187769428dcaSjsing 18783395f70eSjsing ret = 1; 18793395f70eSjsing 18803395f70eSjsing err: 188169428dcaSjsing freezero(key, key_len); 18823395f70eSjsing 18839ccbb9b1Sjsing return ret; 18843395f70eSjsing } 18853395f70eSjsing 18863395f70eSjsing static int 18877cf55da3Sjsing ssl3_send_client_kex_ecdhe(SSL *s, CBB *cbb) 18883395f70eSjsing { 1889892af70eSjsing uint8_t *key = NULL; 1890892af70eSjsing size_t key_len = 0; 1891892af70eSjsing CBB public; 1892892af70eSjsing int ret = 0; 1893892af70eSjsing 1894892af70eSjsing /* Ensure that we have an ephemeral key for ECDHE. */ 189502876cc3Sjsing if (s->s3->hs.key_share == NULL) { 18963395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1897c9d7abb7Sbeck SSLerror(s, ERR_R_INTERNAL_ERROR); 18983395f70eSjsing goto err; 18993395f70eSjsing } 19003395f70eSjsing 190102876cc3Sjsing if (!tls_key_share_generate(s->s3->hs.key_share)) 1902892af70eSjsing goto err; 1903892af70eSjsing 1904892af70eSjsing if (!CBB_add_u8_length_prefixed(cbb, &public)) 1905892af70eSjsing return 0; 190602876cc3Sjsing if (!tls_key_share_public(s->s3->hs.key_share, &public)) 1907892af70eSjsing goto err; 1908892af70eSjsing if (!CBB_flush(cbb)) 1909892af70eSjsing goto err; 1910892af70eSjsing 191102876cc3Sjsing if (!tls_key_share_derive(s->s3->hs.key_share, &key, &key_len)) 1912892af70eSjsing goto err; 1913892af70eSjsing 1914892af70eSjsing if (!tls12_derive_master_secret(s, key, key_len)) 1915892af70eSjsing goto err; 1916892af70eSjsing 1917892af70eSjsing ret = 1; 19183395f70eSjsing 19193395f70eSjsing err: 1920892af70eSjsing freezero(key, key_len); 1921892af70eSjsing 1922892af70eSjsing return ret; 19233395f70eSjsing } 19243395f70eSjsing 19253395f70eSjsing static int 19263395f70eSjsing ssl3_send_client_key_exchange(SSL *s) 19273395f70eSjsing { 19283395f70eSjsing unsigned long alg_k; 19293395f70eSjsing CBB cbb, kex; 19303395f70eSjsing 19313395f70eSjsing memset(&cbb, 0, sizeof(cbb)); 19323395f70eSjsing 193302876cc3Sjsing if (s->s3->hs.state == SSL3_ST_CW_KEY_EXCH_A) { 193402876cc3Sjsing alg_k = s->s3->hs.cipher->algorithm_mkey; 19353395f70eSjsing 1936312b4b14Sjsing if (!ssl3_handshake_msg_start(s, &cbb, &kex, 19373395f70eSjsing SSL3_MT_CLIENT_KEY_EXCHANGE)) 19383395f70eSjsing goto err; 19393395f70eSjsing 19403395f70eSjsing if (alg_k & SSL_kRSA) { 19417cf55da3Sjsing if (!ssl3_send_client_kex_rsa(s, &kex)) 19423395f70eSjsing goto err; 19433395f70eSjsing } else if (alg_k & SSL_kDHE) { 19447cf55da3Sjsing if (!ssl3_send_client_kex_dhe(s, &kex)) 19453395f70eSjsing goto err; 19463395f70eSjsing } else if (alg_k & SSL_kECDHE) { 19477cf55da3Sjsing if (!ssl3_send_client_kex_ecdhe(s, &kex)) 19483395f70eSjsing goto err; 19493395f70eSjsing } else { 19503395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, 19513395f70eSjsing SSL_AD_HANDSHAKE_FAILURE); 1952c9d7abb7Sbeck SSLerror(s, ERR_R_INTERNAL_ERROR); 19533395f70eSjsing goto err; 19543395f70eSjsing } 19553395f70eSjsing 1956312b4b14Sjsing if (!ssl3_handshake_msg_finish(s, &cbb)) 19573395f70eSjsing goto err; 19583395f70eSjsing 195902876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_KEY_EXCH_B; 19603395f70eSjsing } 19613395f70eSjsing 19623395f70eSjsing /* SSL3_ST_CW_KEY_EXCH_B */ 19633395f70eSjsing return (ssl3_handshake_write(s)); 19643395f70eSjsing 19653395f70eSjsing err: 19663395f70eSjsing CBB_cleanup(&cbb); 19673395f70eSjsing 19683395f70eSjsing return (-1); 19693395f70eSjsing } 19703395f70eSjsing 1971d6477fdbStb static int 1972a75794adSjsing ssl3_send_client_verify_sigalgs(SSL *s, EVP_PKEY *pkey, 1973a75794adSjsing const struct ssl_sigalg *sigalg, CBB *cert_verify) 19743395f70eSjsing { 1975d6477fdbStb CBB cbb_signature; 19763395f70eSjsing EVP_PKEY_CTX *pctx = NULL; 197730838a50Stb EVP_MD_CTX *mctx = NULL; 1978d6477fdbStb const unsigned char *hdata; 1979d6477fdbStb unsigned char *signature = NULL; 198025768e2bSjsing size_t signature_len, hdata_len; 1981d6477fdbStb int ret = 0; 19823395f70eSjsing 198330838a50Stb if ((mctx = EVP_MD_CTX_new()) == NULL) 198430838a50Stb goto err; 19853395f70eSjsing 198625768e2bSjsing if (!tls1_transcript_data(s, &hdata, &hdata_len)) { 1987c9d7abb7Sbeck SSLerror(s, ERR_R_INTERNAL_ERROR); 19883395f70eSjsing goto err; 19893395f70eSjsing } 199030838a50Stb if (!EVP_DigestSignInit(mctx, &pctx, sigalg->md(), NULL, pkey)) { 1991c9d7abb7Sbeck SSLerror(s, ERR_R_EVP_LIB); 19923395f70eSjsing goto err; 19933395f70eSjsing } 199496b13b45Sjsing if ((sigalg->flags & SIGALG_FLAG_RSA_PSS) && 1995d6477fdbStb (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) || 1996844b8990Sbeck !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1))) { 1997844b8990Sbeck SSLerror(s, ERR_R_EVP_LIB); 1998844b8990Sbeck goto err; 1999844b8990Sbeck } 200080a887b4Stb if (!EVP_DigestSign(mctx, NULL, &signature_len, hdata, hdata_len)) { 2001bfb305e8Sbeck SSLerror(s, ERR_R_EVP_LIB); 2002bfb305e8Sbeck goto err; 2003bfb305e8Sbeck } 200425768e2bSjsing if ((signature = calloc(1, signature_len)) == NULL) { 2005bfb305e8Sbeck SSLerror(s, ERR_R_MALLOC_FAILURE); 2006bfb305e8Sbeck goto err; 2007bfb305e8Sbeck } 200880a887b4Stb if (!EVP_DigestSign(mctx, signature, &signature_len, hdata, hdata_len)) { 2009bfb305e8Sbeck SSLerror(s, ERR_R_EVP_LIB); 2010bfb305e8Sbeck goto err; 2011bfb305e8Sbeck } 2012d6477fdbStb 201325768e2bSjsing if (!CBB_add_u16(cert_verify, sigalg->value)) 201425768e2bSjsing goto err; 2015d6477fdbStb if (!CBB_add_u16_length_prefixed(cert_verify, &cbb_signature)) 2016d6477fdbStb goto err; 2017d6477fdbStb if (!CBB_add_bytes(&cbb_signature, signature, signature_len)) 2018d6477fdbStb goto err; 2019d6477fdbStb if (!CBB_flush(cert_verify)) 2020d6477fdbStb goto err; 2021d6477fdbStb 2022d6477fdbStb ret = 1; 202325768e2bSjsing 2024d6477fdbStb err: 202530838a50Stb EVP_MD_CTX_free(mctx); 2026d6477fdbStb free(signature); 2027d6477fdbStb return ret; 2028d6477fdbStb } 2029d6477fdbStb 2030d6477fdbStb static int 203150aa6b55Sjsing ssl3_send_client_verify_rsa(SSL *s, EVP_PKEY *pkey, CBB *cert_verify) 2032d6477fdbStb { 2033d6477fdbStb CBB cbb_signature; 20345c273ab3Stb RSA *rsa; 203525768e2bSjsing unsigned char data[EVP_MAX_MD_SIZE]; 2036d6477fdbStb unsigned char *signature = NULL; 203725768e2bSjsing unsigned int signature_len; 203825768e2bSjsing size_t data_len; 2039d6477fdbStb int ret = 0; 2040d6477fdbStb 204125768e2bSjsing if (!tls1_transcript_hash_value(s, data, sizeof(data), &data_len)) 204225768e2bSjsing goto err; 2043bfb305e8Sbeck if ((signature = calloc(1, EVP_PKEY_size(pkey))) == NULL) 2044bfb305e8Sbeck goto err; 20455c273ab3Stb if ((rsa = EVP_PKEY_get0_RSA(pkey)) == NULL) 20465c273ab3Stb goto err; 20475c273ab3Stb if (RSA_sign(NID_md5_sha1, data, data_len, signature, &signature_len, 20485c273ab3Stb rsa) <= 0 ) { 2049c9d7abb7Sbeck SSLerror(s, ERR_R_RSA_LIB); 20503395f70eSjsing goto err; 20513395f70eSjsing } 2052d6477fdbStb 2053d6477fdbStb if (!CBB_add_u16_length_prefixed(cert_verify, &cbb_signature)) 2054d6477fdbStb goto err; 2055d6477fdbStb if (!CBB_add_bytes(&cbb_signature, signature, signature_len)) 2056d6477fdbStb goto err; 2057d6477fdbStb if (!CBB_flush(cert_verify)) 2058d6477fdbStb goto err; 2059d6477fdbStb 2060d6477fdbStb ret = 1; 2061d6477fdbStb err: 2062d6477fdbStb free(signature); 2063d6477fdbStb return ret; 2064d6477fdbStb } 2065d6477fdbStb 2066d6477fdbStb static int 206750aa6b55Sjsing ssl3_send_client_verify_ec(SSL *s, EVP_PKEY *pkey, CBB *cert_verify) 2068d6477fdbStb { 2069d6477fdbStb CBB cbb_signature; 20705c273ab3Stb EC_KEY *eckey; 207125768e2bSjsing unsigned char data[EVP_MAX_MD_SIZE]; 2072d6477fdbStb unsigned char *signature = NULL; 207325768e2bSjsing unsigned int signature_len; 2074d6477fdbStb int ret = 0; 2075d6477fdbStb 2076df40b63bSjsing if (!tls1_transcript_hash_value(s, data, sizeof(data), NULL)) 2077d6477fdbStb goto err; 2078bfb305e8Sbeck if ((signature = calloc(1, EVP_PKEY_size(pkey))) == NULL) 2079bfb305e8Sbeck goto err; 20805c273ab3Stb if ((eckey = EVP_PKEY_get0_EC_KEY(pkey)) == NULL) 20815c273ab3Stb goto err; 20827aaded86Stb if (!ECDSA_sign(0, &data[MD5_DIGEST_LENGTH], SHA_DIGEST_LENGTH, 20835c273ab3Stb signature, &signature_len, eckey)) { 2084c9d7abb7Sbeck SSLerror(s, ERR_R_ECDSA_LIB); 20853395f70eSjsing goto err; 20863395f70eSjsing } 2087d6477fdbStb 2088d6477fdbStb if (!CBB_add_u16_length_prefixed(cert_verify, &cbb_signature)) 2089d6477fdbStb goto err; 2090d6477fdbStb if (!CBB_add_bytes(&cbb_signature, signature, signature_len)) 2091d6477fdbStb goto err; 2092d6477fdbStb if (!CBB_flush(cert_verify)) 2093d6477fdbStb goto err; 2094d6477fdbStb 2095d6477fdbStb ret = 1; 2096d6477fdbStb err: 2097d6477fdbStb free(signature); 2098d6477fdbStb return ret; 2099d6477fdbStb } 2100d6477fdbStb 2101eb1ff9efSjsing static int 2102d6477fdbStb ssl3_send_client_verify(SSL *s) 2103d6477fdbStb { 2104a75794adSjsing const struct ssl_sigalg *sigalg; 2105d6477fdbStb CBB cbb, cert_verify; 2106d6477fdbStb EVP_PKEY *pkey; 2107d6477fdbStb 2108d6477fdbStb memset(&cbb, 0, sizeof(cbb)); 2109d6477fdbStb 211002876cc3Sjsing if (s->s3->hs.state == SSL3_ST_CW_CERT_VRFY_A) { 2111d6477fdbStb if (!ssl3_handshake_msg_start(s, &cbb, &cert_verify, 2112d6477fdbStb SSL3_MT_CERTIFICATE_VERIFY)) 2113d6477fdbStb goto err; 2114d6477fdbStb 2115d6477fdbStb pkey = s->cert->key->privatekey; 2116a75794adSjsing if ((sigalg = ssl_sigalg_select(s, pkey)) == NULL) { 2117a75794adSjsing SSLerror(s, SSL_R_SIGNATURE_ALGORITHMS_ERROR); 2118a75794adSjsing goto err; 2119a75794adSjsing } 212002876cc3Sjsing s->s3->hs.our_sigalg = sigalg; 2121d6477fdbStb 2122d6477fdbStb /* 212350aa6b55Sjsing * For TLS v1.2 send signature algorithm and signature using 212450aa6b55Sjsing * agreed digest and cached handshake records. 2125d6477fdbStb */ 2126d6477fdbStb if (SSL_USE_SIGALGS(s)) { 2127a75794adSjsing if (!ssl3_send_client_verify_sigalgs(s, pkey, sigalg, 2128a75794adSjsing &cert_verify)) 2129d6477fdbStb goto err; 21305c273ab3Stb } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) { 213150aa6b55Sjsing if (!ssl3_send_client_verify_rsa(s, pkey, &cert_verify)) 2132d6477fdbStb goto err; 21335c273ab3Stb } else if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) { 213450aa6b55Sjsing if (!ssl3_send_client_verify_ec(s, pkey, &cert_verify)) 2135d6477fdbStb goto err; 21363395f70eSjsing } else { 2137c9d7abb7Sbeck SSLerror(s, ERR_R_INTERNAL_ERROR); 21383395f70eSjsing goto err; 21393395f70eSjsing } 21403395f70eSjsing 2141d6477fdbStb tls1_transcript_free(s); 21423395f70eSjsing 2143312b4b14Sjsing if (!ssl3_handshake_msg_finish(s, &cbb)) 2144125f035fSjsing goto err; 2145125f035fSjsing 214602876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_CERT_VRFY_B; 21473395f70eSjsing } 21483395f70eSjsing 21493395f70eSjsing return (ssl3_handshake_write(s)); 21503395f70eSjsing 21513395f70eSjsing err: 2152125f035fSjsing CBB_cleanup(&cbb); 2153125f035fSjsing 21543395f70eSjsing return (-1); 21553395f70eSjsing } 21563395f70eSjsing 2157eb1ff9efSjsing static int 21583395f70eSjsing ssl3_send_client_certificate(SSL *s) 21593395f70eSjsing { 21603395f70eSjsing EVP_PKEY *pkey = NULL; 21613395f70eSjsing X509 *x509 = NULL; 21623395f70eSjsing CBB cbb, client_cert; 21633395f70eSjsing int i; 21643395f70eSjsing 21653395f70eSjsing memset(&cbb, 0, sizeof(cbb)); 21663395f70eSjsing 216702876cc3Sjsing if (s->s3->hs.state == SSL3_ST_CW_CERT_A) { 2168ac64f7f4Sjsing if (s->cert->key->x509 == NULL || 2169ac64f7f4Sjsing s->cert->key->privatekey == NULL) 217002876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_CERT_B; 21713395f70eSjsing else 217202876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_CERT_C; 21733395f70eSjsing } 21743395f70eSjsing 21753395f70eSjsing /* We need to get a client cert */ 217602876cc3Sjsing if (s->s3->hs.state == SSL3_ST_CW_CERT_B) { 21773395f70eSjsing /* 21783395f70eSjsing * If we get an error, we need to 21796f7f653bSjsing * ssl->rwstate = SSL_X509_LOOKUP; return(-1); 21808243b58fStb * We then get retried later. 21813395f70eSjsing */ 21823395f70eSjsing i = ssl_do_client_cert_cb(s, &x509, &pkey); 21833395f70eSjsing if (i < 0) { 21846f7f653bSjsing s->rwstate = SSL_X509_LOOKUP; 21853395f70eSjsing return (-1); 21863395f70eSjsing } 21876f7f653bSjsing s->rwstate = SSL_NOTHING; 21883395f70eSjsing if ((i == 1) && (pkey != NULL) && (x509 != NULL)) { 218902876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_CERT_B; 21903395f70eSjsing if (!SSL_use_certificate(s, x509) || 21913395f70eSjsing !SSL_use_PrivateKey(s, pkey)) 21923395f70eSjsing i = 0; 21933395f70eSjsing } else if (i == 1) { 21943395f70eSjsing i = 0; 2195c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); 21963395f70eSjsing } 21973395f70eSjsing 21983395f70eSjsing X509_free(x509); 21993395f70eSjsing EVP_PKEY_free(pkey); 2200d250fe7cSjsing if (i == 0) { 220102876cc3Sjsing s->s3->hs.tls12.cert_request = 2; 22023395f70eSjsing 2203d250fe7cSjsing /* There is no client certificate to verify. */ 2204cef855dcSjsing tls1_transcript_free(s); 2205d250fe7cSjsing } 2206d250fe7cSjsing 22073395f70eSjsing /* Ok, we have a cert */ 220802876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_CERT_C; 22093395f70eSjsing } 22103395f70eSjsing 221102876cc3Sjsing if (s->s3->hs.state == SSL3_ST_CW_CERT_C) { 2212312b4b14Sjsing if (!ssl3_handshake_msg_start(s, &cbb, &client_cert, 22133395f70eSjsing SSL3_MT_CERTIFICATE)) 22143395f70eSjsing goto err; 22153395f70eSjsing if (!ssl3_output_cert_chain(s, &client_cert, 221602876cc3Sjsing (s->s3->hs.tls12.cert_request == 2) ? NULL : s->cert->key)) 22173395f70eSjsing goto err; 2218312b4b14Sjsing if (!ssl3_handshake_msg_finish(s, &cbb)) 22193395f70eSjsing goto err; 22203395f70eSjsing 222102876cc3Sjsing s->s3->hs.state = SSL3_ST_CW_CERT_D; 22223395f70eSjsing } 22233395f70eSjsing 22243395f70eSjsing /* SSL3_ST_CW_CERT_D */ 222511766112Sjsing return (ssl3_handshake_write(s)); 22263395f70eSjsing 22273395f70eSjsing err: 22283395f70eSjsing CBB_cleanup(&cbb); 22293395f70eSjsing 22303395f70eSjsing return (0); 22313395f70eSjsing } 22323395f70eSjsing 22333395f70eSjsing #define has_bits(i,m) (((i)&(m)) == (m)) 22343395f70eSjsing 2235eb1ff9efSjsing static int 22363395f70eSjsing ssl3_check_cert_and_algorithm(SSL *s) 22373395f70eSjsing { 22383395f70eSjsing long alg_k, alg_a; 2239892af70eSjsing int nid = NID_undef; 224015b5e1ecSjsing int i; 22413395f70eSjsing 224202876cc3Sjsing alg_k = s->s3->hs.cipher->algorithm_mkey; 224302876cc3Sjsing alg_a = s->s3->hs.cipher->algorithm_auth; 22443395f70eSjsing 22453395f70eSjsing /* We don't have a certificate. */ 22463395f70eSjsing if (alg_a & SSL_aNULL) 22473395f70eSjsing return (1); 22483395f70eSjsing 224902876cc3Sjsing if (s->s3->hs.key_share != NULL) 225002876cc3Sjsing nid = tls_key_share_nid(s->s3->hs.key_share); 22513395f70eSjsing 22523395f70eSjsing /* This is the passed certificate. */ 22533395f70eSjsing 225415b5e1ecSjsing if (s->session->peer_cert_type == SSL_PKEY_ECC) { 225515b5e1ecSjsing if (!ssl_check_srvr_ecc_cert_and_alg(s, s->session->peer_cert)) { 2256c9d7abb7Sbeck SSLerror(s, SSL_R_BAD_ECC_CERT); 2257ba06b73eStb goto fatal_err; 225815b5e1ecSjsing } 22593395f70eSjsing return (1); 22603395f70eSjsing } 226115b5e1ecSjsing 226215b5e1ecSjsing i = X509_certificate_type(s->session->peer_cert, NULL); 22633395f70eSjsing 22643395f70eSjsing /* Check that we have a certificate if we require one. */ 22653395f70eSjsing if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA|EVP_PKT_SIGN)) { 2266c9d7abb7Sbeck SSLerror(s, SSL_R_MISSING_RSA_SIGNING_CERT); 2267ba06b73eStb goto fatal_err; 22683395f70eSjsing } 22694722f988Sjsing if ((alg_k & SSL_kRSA) && !has_bits(i, EVP_PK_RSA|EVP_PKT_ENC)) { 2270c9d7abb7Sbeck SSLerror(s, SSL_R_MISSING_RSA_ENCRYPTING_CERT); 2271ba06b73eStb goto fatal_err; 22723395f70eSjsing } 22733395f70eSjsing if ((alg_k & SSL_kDHE) && 2274892af70eSjsing !(has_bits(i, EVP_PK_DH|EVP_PKT_EXCH) || (nid == NID_dhKeyAgreement))) { 2275c9d7abb7Sbeck SSLerror(s, SSL_R_MISSING_DH_KEY); 2276ba06b73eStb goto fatal_err; 22773395f70eSjsing } 22783395f70eSjsing 22793395f70eSjsing return (1); 22807cf55da3Sjsing 2281ba06b73eStb fatal_err: 22823395f70eSjsing ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 22837cf55da3Sjsing 22843395f70eSjsing return (0); 22853395f70eSjsing } 22863395f70eSjsing 22873395f70eSjsing /* 22883395f70eSjsing * Check to see if handshake is full or resumed. Usually this is just a 22893395f70eSjsing * case of checking to see if a cache hit has occurred. In the case of 22903395f70eSjsing * session tickets we have to check the next message to be sure. 22913395f70eSjsing */ 22923395f70eSjsing 2293eb1ff9efSjsing static int 22943395f70eSjsing ssl3_check_finished(SSL *s) 22953395f70eSjsing { 2296f2c3e964Sjsing int ret; 22973395f70eSjsing 22983395f70eSjsing /* If we have no ticket it cannot be a resumed session. */ 22993395f70eSjsing if (!s->session->tlsext_tick) 23003395f70eSjsing return (1); 23013395f70eSjsing /* this function is called when we really expect a Certificate 23023395f70eSjsing * message, so permit appropriate message length */ 2303f2c3e964Sjsing if ((ret = ssl3_get_message(s, SSL3_ST_CR_CERT_A, 23046f7f653bSjsing SSL3_ST_CR_CERT_B, -1, s->max_cert_list)) <= 0) 2305f2c3e964Sjsing return ret; 2306dc77a1b6Sjsing 230702876cc3Sjsing s->s3->hs.tls12.reuse_message = 1; 230802876cc3Sjsing if ((s->s3->hs.tls12.message_type == SSL3_MT_FINISHED) || 230902876cc3Sjsing (s->s3->hs.tls12.message_type == SSL3_MT_NEWSESSION_TICKET)) 23103395f70eSjsing return (2); 23113395f70eSjsing 23123395f70eSjsing return (1); 23133395f70eSjsing } 23143395f70eSjsing 2315eb1ff9efSjsing static int 23163395f70eSjsing ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) 23173395f70eSjsing { 2318ba1de450Stb if (s->ctx->client_cert_cb == NULL) 2319ba1de450Stb return 0; 23203395f70eSjsing 2321ba1de450Stb return s->ctx->client_cert_cb(s, px509, ppkey); 23223395f70eSjsing } 2323eb1ff9efSjsing 2324eb1ff9efSjsing static int 2325eb1ff9efSjsing ssl3_send_client_change_cipher_spec(SSL *s) 2326eb1ff9efSjsing { 2327eb1ff9efSjsing size_t outlen; 2328eb1ff9efSjsing CBB cbb; 2329eb1ff9efSjsing 2330eb1ff9efSjsing memset(&cbb, 0, sizeof(cbb)); 2331eb1ff9efSjsing 2332eb1ff9efSjsing if (s->s3->hs.state == SSL3_ST_CW_CHANGE_A) { 23336f7f653bSjsing if (!CBB_init_fixed(&cbb, s->init_buf->data, 23346f7f653bSjsing s->init_buf->length)) 2335eb1ff9efSjsing goto err; 2336eb1ff9efSjsing if (!CBB_add_u8(&cbb, SSL3_MT_CCS)) 2337eb1ff9efSjsing goto err; 2338eb1ff9efSjsing if (!CBB_finish(&cbb, NULL, &outlen)) 2339eb1ff9efSjsing goto err; 2340eb1ff9efSjsing 2341eb1ff9efSjsing if (outlen > INT_MAX) 2342eb1ff9efSjsing goto err; 2343eb1ff9efSjsing 23446f7f653bSjsing s->init_num = (int)outlen; 23456f7f653bSjsing s->init_off = 0; 2346eb1ff9efSjsing 2347eb1ff9efSjsing if (SSL_is_dtls(s)) { 2348eb1ff9efSjsing s->d1->handshake_write_seq = 2349eb1ff9efSjsing s->d1->next_handshake_write_seq; 2350eb1ff9efSjsing dtls1_set_message_header_int(s, SSL3_MT_CCS, 0, 2351eb1ff9efSjsing s->d1->handshake_write_seq, 0, 0); 2352eb1ff9efSjsing dtls1_buffer_message(s, 1); 2353eb1ff9efSjsing } 2354eb1ff9efSjsing 2355eb1ff9efSjsing s->s3->hs.state = SSL3_ST_CW_CHANGE_B; 2356eb1ff9efSjsing } 2357eb1ff9efSjsing 2358eb1ff9efSjsing /* SSL3_ST_CW_CHANGE_B */ 2359eb1ff9efSjsing return ssl3_record_write(s, SSL3_RT_CHANGE_CIPHER_SPEC); 2360eb1ff9efSjsing 2361eb1ff9efSjsing err: 2362eb1ff9efSjsing CBB_cleanup(&cbb); 2363eb1ff9efSjsing 2364eb1ff9efSjsing return -1; 2365eb1ff9efSjsing } 2366eb1ff9efSjsing 2367eb1ff9efSjsing static int 2368eb1ff9efSjsing ssl3_send_client_finished(SSL *s) 2369eb1ff9efSjsing { 2370eb1ff9efSjsing CBB cbb, finished; 2371eb1ff9efSjsing 2372eb1ff9efSjsing memset(&cbb, 0, sizeof(cbb)); 2373eb1ff9efSjsing 2374eb1ff9efSjsing if (s->s3->hs.state == SSL3_ST_CW_FINISHED_A) { 2375eb1ff9efSjsing if (!tls12_derive_finished(s)) 2376eb1ff9efSjsing goto err; 2377eb1ff9efSjsing 2378eb1ff9efSjsing /* Copy finished so we can use it for renegotiation checks. */ 2379eb1ff9efSjsing memcpy(s->s3->previous_client_finished, 2380eb1ff9efSjsing s->s3->hs.finished, s->s3->hs.finished_len); 2381eb1ff9efSjsing s->s3->previous_client_finished_len = 2382eb1ff9efSjsing s->s3->hs.finished_len; 2383eb1ff9efSjsing 2384eb1ff9efSjsing if (!ssl3_handshake_msg_start(s, &cbb, &finished, 2385eb1ff9efSjsing SSL3_MT_FINISHED)) 2386eb1ff9efSjsing goto err; 2387eb1ff9efSjsing if (!CBB_add_bytes(&finished, s->s3->hs.finished, 2388eb1ff9efSjsing s->s3->hs.finished_len)) 2389eb1ff9efSjsing goto err; 2390eb1ff9efSjsing if (!ssl3_handshake_msg_finish(s, &cbb)) 2391eb1ff9efSjsing goto err; 2392eb1ff9efSjsing 2393eb1ff9efSjsing s->s3->hs.state = SSL3_ST_CW_FINISHED_B; 2394eb1ff9efSjsing } 2395eb1ff9efSjsing 2396eb1ff9efSjsing return (ssl3_handshake_write(s)); 2397eb1ff9efSjsing 2398eb1ff9efSjsing err: 2399eb1ff9efSjsing CBB_cleanup(&cbb); 2400eb1ff9efSjsing 2401eb1ff9efSjsing return (-1); 2402eb1ff9efSjsing } 2403eb1ff9efSjsing 2404eb1ff9efSjsing static int 2405eb1ff9efSjsing ssl3_get_server_finished(SSL *s) 2406eb1ff9efSjsing { 2407eb1ff9efSjsing int al, md_len, ret; 2408eb1ff9efSjsing CBS cbs; 2409eb1ff9efSjsing 2410eb1ff9efSjsing /* should actually be 36+4 :-) */ 2411eb1ff9efSjsing if ((ret = ssl3_get_message(s, SSL3_ST_CR_FINISHED_A, 2412eb1ff9efSjsing SSL3_ST_CR_FINISHED_B, SSL3_MT_FINISHED, 64)) <= 0) 2413eb1ff9efSjsing return ret; 2414eb1ff9efSjsing 2415eb1ff9efSjsing /* If this occurs, we have missed a message */ 2416eb1ff9efSjsing if (!s->s3->change_cipher_spec) { 2417eb1ff9efSjsing al = SSL_AD_UNEXPECTED_MESSAGE; 2418eb1ff9efSjsing SSLerror(s, SSL_R_GOT_A_FIN_BEFORE_A_CCS); 2419eb1ff9efSjsing goto fatal_err; 2420eb1ff9efSjsing } 2421eb1ff9efSjsing s->s3->change_cipher_spec = 0; 2422eb1ff9efSjsing 2423eb1ff9efSjsing md_len = TLS1_FINISH_MAC_LENGTH; 2424eb1ff9efSjsing 24256f7f653bSjsing if (s->init_num < 0) { 2426eb1ff9efSjsing al = SSL_AD_DECODE_ERROR; 2427eb1ff9efSjsing SSLerror(s, SSL_R_BAD_DIGEST_LENGTH); 2428eb1ff9efSjsing goto fatal_err; 2429eb1ff9efSjsing } 2430eb1ff9efSjsing 24316f7f653bSjsing CBS_init(&cbs, s->init_msg, s->init_num); 2432eb1ff9efSjsing 2433eb1ff9efSjsing if (s->s3->hs.peer_finished_len != md_len || 2434eb1ff9efSjsing CBS_len(&cbs) != md_len) { 2435eb1ff9efSjsing al = SSL_AD_DECODE_ERROR; 2436eb1ff9efSjsing SSLerror(s, SSL_R_BAD_DIGEST_LENGTH); 2437eb1ff9efSjsing goto fatal_err; 2438eb1ff9efSjsing } 2439eb1ff9efSjsing 2440eb1ff9efSjsing if (!CBS_mem_equal(&cbs, s->s3->hs.peer_finished, CBS_len(&cbs))) { 2441eb1ff9efSjsing al = SSL_AD_DECRYPT_ERROR; 2442eb1ff9efSjsing SSLerror(s, SSL_R_DIGEST_CHECK_FAILED); 2443eb1ff9efSjsing goto fatal_err; 2444eb1ff9efSjsing } 2445eb1ff9efSjsing 2446eb1ff9efSjsing /* Copy finished so we can use it for renegotiation checks. */ 2447eb1ff9efSjsing OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE); 2448eb1ff9efSjsing memcpy(s->s3->previous_server_finished, 2449eb1ff9efSjsing s->s3->hs.peer_finished, md_len); 2450eb1ff9efSjsing s->s3->previous_server_finished_len = md_len; 2451eb1ff9efSjsing 2452eb1ff9efSjsing return (1); 2453eb1ff9efSjsing fatal_err: 2454eb1ff9efSjsing ssl3_send_alert(s, SSL3_AL_FATAL, al); 2455eb1ff9efSjsing return (0); 2456eb1ff9efSjsing } 2457