s3_clnt.c revision 306196
10SN/A/* ssl/s3_clnt.c */ 216985Sserb/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 30SN/A * All rights reserved. 40SN/A * 50SN/A * This package is an SSL implementation written 60SN/A * by Eric Young (eay@cryptsoft.com). 72362SN/A * The implementation was written so as to conform with Netscapes SSL. 80SN/A * 92362SN/A * This library is free for commercial and non-commercial use as long as 100SN/A * the following conditions are aheared to. The following conditions 110SN/A * apply to all code found in this distribution, be it the RC4, RSA, 120SN/A * lhash, DES, etc., code; not just the SSL code. The SSL documentation 130SN/A * included with this distribution is covered by the same copyright terms 140SN/A * except that the holder is Tim Hudson (tjh@cryptsoft.com). 150SN/A * 160SN/A * Copyright remains Eric Young's, and as such any Copyright notices in 170SN/A * the code are not to be removed. 180SN/A * If this package is used in a product, Eric Young should be given attribution 190SN/A * as the author of the parts of the library used. 200SN/A * This can be in the form of a textual message at program startup or 212362SN/A * in documentation (online or textual) provided with the package. 222362SN/A * 232362SN/A * Redistribution and use in source and binary forms, with or without 240SN/A * modification, are permitted provided that the following conditions 250SN/A * are met: 260SN/A * 1. Redistributions of source code must retain the copyright 275258SN/A * notice, this list of conditions and the following disclaimer. 285258SN/A * 2. Redistributions in binary form must reproduce the above copyright 290SN/A * notice, this list of conditions and the following disclaimer in the 300SN/A * documentation and/or other materials provided with the distribution. 310SN/A * 3. All advertising materials mentioning features or use of this software 320SN/A * must display the following acknowledgement: 330SN/A * "This product includes cryptographic software written by 340SN/A * Eric Young (eay@cryptsoft.com)" 350SN/A * The word 'cryptographic' can be left out if the rouines from the library 360SN/A * being used are not cryptographic related :-). 370SN/A * 4. If you include any Windows specific code (or a derivative thereof) from 380SN/A * the apps directory (application code) you must include an acknowledgement: 390SN/A * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 400SN/A * 410SN/A * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 420SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 430SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 448951SN/A * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 450SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 469054SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 470SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 480SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 490SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 500SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 510SN/A * SUCH DAMAGE. 520SN/A * 530SN/A * The licence and distribution terms for any publically available version or 540SN/A * derivative of this code cannot be changed. i.e. this code cannot simply be 550SN/A * copied and put under another distribution licence 560SN/A * [including the GNU Public Licence.] 570SN/A */ 580SN/A/* ==================================================================== 590SN/A * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. 600SN/A * 610SN/A * Redistribution and use in source and binary forms, with or without 620SN/A * modification, are permitted provided that the following conditions 630SN/A * are met: 640SN/A * 650SN/A * 1. Redistributions of source code must retain the above copyright 660SN/A * notice, this list of conditions and the following disclaimer. 670SN/A * 680SN/A * 2. Redistributions in binary form must reproduce the above copyright 690SN/A * notice, this list of conditions and the following disclaimer in 700SN/A * the documentation and/or other materials provided with the 710SN/A * distribution. 720SN/A * 739660SN/A * 3. All advertising materials mentioning features or use of this 740SN/A * software must display the following acknowledgment: 750SN/A * "This product includes software developed by the OpenSSL Project 760SN/A * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 770SN/A * 780SN/A * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 790SN/A * endorse or promote products derived from this software without 800SN/A * prior written permission. For written permission, please contact 810SN/A * openssl-core@openssl.org. 820SN/A * 830SN/A * 5. Products derived from this software may not be called "OpenSSL" 840SN/A * nor may "OpenSSL" appear in their names without prior written 850SN/A * permission of the OpenSSL Project. 860SN/A * 870SN/A * 6. Redistributions of any form whatsoever must retain the following 880SN/A * acknowledgment: 890SN/A * "This product includes software developed by the OpenSSL Project 900SN/A * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 910SN/A * 920SN/A * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 930SN/A * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 940SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 950SN/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 960SN/A * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 970SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 980SN/A * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 990SN/A * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1000SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 1010SN/A * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1020SN/A * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 1030SN/A * OF THE POSSIBILITY OF SUCH DAMAGE. 1040SN/A * ==================================================================== 1050SN/A * 1060SN/A * This product includes cryptographic software written by Eric Young 1070SN/A * (eay@cryptsoft.com). This product includes software written by Tim 1080SN/A * Hudson (tjh@cryptsoft.com). 1090SN/A * 1100SN/A */ 1110SN/A/* ==================================================================== 1120SN/A * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 1130SN/A * 1140SN/A * Portions of the attached software ("Contribution") are developed by 1150SN/A * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. 1160SN/A * 1170SN/A * The Contribution is licensed pursuant to the OpenSSL open source 1180SN/A * license provided above. 1190SN/A * 1200SN/A * ECC cipher suite support in OpenSSL originally written by 1210SN/A * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. 1220SN/A * 1230SN/A */ 1240SN/A/* ==================================================================== 1250SN/A * Copyright 2005 Nokia. All rights reserved. 1260SN/A * 1270SN/A * The portions of the attached software ("Contribution") is developed by 1280SN/A * Nokia Corporation and is licensed pursuant to the OpenSSL open source 1290SN/A * license. 1300SN/A * 1310SN/A * The Contribution, originally written by Mika Kousa and Pasi Eronen of 1320SN/A * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites 1330SN/A * support (see RFC 4279) to OpenSSL. 1340SN/A * 1350SN/A * No patent licenses or other rights except those expressly stated in 1360SN/A * the OpenSSL open source license shall be deemed granted or received 1370SN/A * expressly, by implication, estoppel, or otherwise. 1380SN/A * 1390SN/A * No assurances are provided by Nokia that the Contribution does not 1400SN/A * infringe the patent or other intellectual property rights of any third 1410SN/A * party or that the license provides you with all the necessary rights 1420SN/A * to make use of the Contribution. 1430SN/A * 1448144SN/A * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN 1450SN/A * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA 1460SN/A * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY 1470SN/A * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR 1480SN/A * OTHERWISE. 1490SN/A */ 1500SN/A 1510SN/A#include <stdio.h> 1520SN/A#include "ssl_locl.h" 1530SN/A#include "kssl_lcl.h" 1540SN/A#include <openssl/buffer.h> 1550SN/A#include <openssl/rand.h> 1560SN/A#include <openssl/objects.h> 1570SN/A#include <openssl/evp.h> 1580SN/A#include <openssl/md5.h> 1590SN/A#ifdef OPENSSL_FIPS 1600SN/A# include <openssl/fips.h> 1610SN/A#endif 1628144SN/A#ifndef OPENSSL_NO_DH 1638144SN/A# include <openssl/dh.h> 1640SN/A#endif 1650SN/A#include <openssl/bn.h> 1660SN/A#ifndef OPENSSL_NO_ENGINE 1670SN/A# include <openssl/engine.h> 1680SN/A#endif 1690SN/A 1700SN/Astatic int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b); 1710SN/A#ifndef OPENSSL_NO_TLSEXT 1720SN/Astatic int ssl3_check_finished(SSL *s); 1730SN/A#endif 1740SN/A 1750SN/A#ifndef OPENSSL_NO_SSL3_METHOD 1760SN/Astatic const SSL_METHOD *ssl3_get_client_method(int ver) 1770SN/A{ 1780SN/A if (ver == SSL3_VERSION) 1790SN/A return (SSLv3_client_method()); 1800SN/A else 1810SN/A return (NULL); 1820SN/A} 1830SN/A 1840SN/AIMPLEMENT_ssl3_meth_func(SSLv3_client_method, 1850SN/A ssl_undefined_function, 1860SN/A ssl3_connect, ssl3_get_client_method) 1870SN/A#endif 1880SN/Aint ssl3_connect(SSL *s) 1890SN/A{ 1900SN/A BUF_MEM *buf = NULL; 1910SN/A unsigned long Time = (unsigned long)time(NULL); 1920SN/A void (*cb) (const SSL *ssl, int type, int val) = NULL; 1930SN/A int ret = -1; 1940SN/A int new_state, state, skip = 0; 1958144SN/A 1960SN/A RAND_add(&Time, sizeof(Time), 0); 1970SN/A ERR_clear_error(); 1980SN/A clear_sys_error(); 1990SN/A 2000SN/A if (s->info_callback != NULL) 2010SN/A cb = s->info_callback; 2020SN/A else if (s->ctx->info_callback != NULL) 2030SN/A cb = s->ctx->info_callback; 2040SN/A 2050SN/A s->in_handshake++; 2060SN/A if (!SSL_in_init(s) || SSL_in_before(s)) 2070SN/A SSL_clear(s); 2080SN/A 2090SN/A#ifndef OPENSSL_NO_HEARTBEATS 2100SN/A /* 2110SN/A * If we're awaiting a HeartbeatResponse, pretend we already got and 2120SN/A * don't await it anymore, because Heartbeats don't make sense during 2130SN/A * handshakes anyway. 2140SN/A */ 2150SN/A if (s->tlsext_hb_pending) { 2160SN/A s->tlsext_hb_pending = 0; 2170SN/A s->tlsext_hb_seq++; 2180SN/A } 2190SN/A#endif 2200SN/A 2210SN/A for (;;) { 2220SN/A state = s->state; 2230SN/A 2240SN/A switch (s->state) { 2250SN/A case SSL_ST_RENEGOTIATE: 2260SN/A s->renegotiate = 1; 2270SN/A s->state = SSL_ST_CONNECT; 2280SN/A s->ctx->stats.sess_connect_renegotiate++; 2290SN/A /* break */ 2300SN/A case SSL_ST_BEFORE: 2310SN/A case SSL_ST_CONNECT: 2320SN/A case SSL_ST_BEFORE | SSL_ST_CONNECT: 2330SN/A case SSL_ST_OK | SSL_ST_CONNECT: 2340SN/A 2350SN/A s->server = 0; 2360SN/A if (cb != NULL) 2370SN/A cb(s, SSL_CB_HANDSHAKE_START, 1); 2380SN/A 2390SN/A if ((s->version & 0xff00) != 0x0300) { 2400SN/A SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR); 2410SN/A s->state = SSL_ST_ERR; 2420SN/A ret = -1; 2430SN/A goto end; 2440SN/A } 2450SN/A 2460SN/A /* s->version=SSL3_VERSION; */ 2470SN/A s->type = SSL_ST_CONNECT; 2480SN/A 2490SN/A if (s->init_buf == NULL) { 2500SN/A if ((buf = BUF_MEM_new()) == NULL) { 2510SN/A ret = -1; 2520SN/A s->state = SSL_ST_ERR; 2530SN/A goto end; 2540SN/A } 2550SN/A if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { 2560SN/A ret = -1; 2570SN/A s->state = SSL_ST_ERR; 2580SN/A goto end; 2590SN/A } 2600SN/A s->init_buf = buf; 2610SN/A buf = NULL; 2620SN/A } 2630SN/A 2640SN/A if (!ssl3_setup_buffers(s)) { 2650SN/A ret = -1; 2660SN/A goto end; 2670SN/A } 2680SN/A 2690SN/A /* setup buffing BIO */ 2700SN/A if (!ssl_init_wbio_buffer(s, 0)) { 2710SN/A ret = -1; 2720SN/A s->state = SSL_ST_ERR; 2730SN/A goto end; 2740SN/A } 2750SN/A 2760SN/A /* don't push the buffering BIO quite yet */ 2770SN/A 2780SN/A ssl3_init_finished_mac(s); 2790SN/A 2800SN/A s->state = SSL3_ST_CW_CLNT_HELLO_A; 2810SN/A s->ctx->stats.sess_connect++; 2820SN/A s->init_num = 0; 2830SN/A s->s3->flags &= ~SSL3_FLAGS_CCS_OK; 2840SN/A /* 2850SN/A * Should have been reset by ssl3_get_finished, too. 2860SN/A */ 2870SN/A s->s3->change_cipher_spec = 0; 2880SN/A break; 2890SN/A 2900SN/A case SSL3_ST_CW_CLNT_HELLO_A: 2910SN/A case SSL3_ST_CW_CLNT_HELLO_B: 2920SN/A 2930SN/A s->shutdown = 0; 2940SN/A ret = ssl3_client_hello(s); 2950SN/A if (ret <= 0) 2960SN/A goto end; 2970SN/A s->state = SSL3_ST_CR_SRVR_HELLO_A; 2980SN/A s->init_num = 0; 2990SN/A 3000SN/A /* turn on buffering for the next lot of output */ 3010SN/A if (s->bbio != s->wbio) 3020SN/A s->wbio = BIO_push(s->bbio, s->wbio); 3030SN/A 3048144SN/A break; 3058144SN/A 3060SN/A case SSL3_ST_CR_SRVR_HELLO_A: 3070SN/A case SSL3_ST_CR_SRVR_HELLO_B: 3080SN/A ret = ssl3_get_server_hello(s); 3090SN/A if (ret <= 0) 3100SN/A goto end; 3110SN/A 3120SN/A if (s->hit) { 3130SN/A s->state = SSL3_ST_CR_FINISHED_A; 3140SN/A#ifndef OPENSSL_NO_TLSEXT 3150SN/A if (s->tlsext_ticket_expected) { 3160SN/A /* receive renewed session ticket */ 3170SN/A s->state = SSL3_ST_CR_SESSION_TICKET_A; 3180SN/A } 3190SN/A#endif 3200SN/A } else 3219459SN/A s->state = SSL3_ST_CR_CERT_A; 3220SN/A s->init_num = 0; 3230SN/A break; 3240SN/A 3250SN/A case SSL3_ST_CR_CERT_A: 3260SN/A case SSL3_ST_CR_CERT_B: 3270SN/A#ifndef OPENSSL_NO_TLSEXT 3280SN/A /* Noop (ret = 0) for everything but EAP-FAST. */ 3290SN/A ret = ssl3_check_finished(s); 3300SN/A if (ret < 0) 3310SN/A goto end; 3320SN/A if (ret == 1) { 3330SN/A s->hit = 1; 3340SN/A s->state = SSL3_ST_CR_FINISHED_A; 3350SN/A s->init_num = 0; 3360SN/A break; 3370SN/A } 3380SN/A#endif 3390SN/A /* Check if it is anon DH/ECDH, SRP auth */ 3400SN/A /* or PSK */ 3410SN/A if (! 3420SN/A (s->s3->tmp. 3430SN/A new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) 3440SN/A && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { 3450SN/A ret = ssl3_get_server_certificate(s); 3460SN/A if (ret <= 0) 3470SN/A goto end; 3480SN/A#ifndef OPENSSL_NO_TLSEXT 3490SN/A if (s->tlsext_status_expected) 3500SN/A s->state = SSL3_ST_CR_CERT_STATUS_A; 3510SN/A else 3520SN/A s->state = SSL3_ST_CR_KEY_EXCH_A; 3530SN/A } else { 3540SN/A skip = 1; 3550SN/A s->state = SSL3_ST_CR_KEY_EXCH_A; 3560SN/A } 3570SN/A#else 3580SN/A } else 3590SN/A skip = 1; 3600SN/A 3610SN/A s->state = SSL3_ST_CR_KEY_EXCH_A; 3620SN/A#endif 3630SN/A s->init_num = 0; 3640SN/A break; 3650SN/A 3660SN/A case SSL3_ST_CR_KEY_EXCH_A: 3670SN/A case SSL3_ST_CR_KEY_EXCH_B: 3680SN/A ret = ssl3_get_key_exchange(s); 3690SN/A if (ret <= 0) 3700SN/A goto end; 3710SN/A s->state = SSL3_ST_CR_CERT_REQ_A; 3720SN/A s->init_num = 0; 3730SN/A 3740SN/A /* 3750SN/A * at this point we check that we have the required stuff from 3760SN/A * the server 3770SN/A */ 3780SN/A if (!ssl3_check_cert_and_algorithm(s)) { 3790SN/A ret = -1; 3800SN/A s->state = SSL_ST_ERR; 3810SN/A goto end; 3820SN/A } 3830SN/A break; 3840SN/A 3850SN/A case SSL3_ST_CR_CERT_REQ_A: 3860SN/A case SSL3_ST_CR_CERT_REQ_B: 3870SN/A ret = ssl3_get_certificate_request(s); 3880SN/A if (ret <= 0) 3890SN/A goto end; 3900SN/A s->state = SSL3_ST_CR_SRVR_DONE_A; 3910SN/A s->init_num = 0; 3920SN/A break; 3930SN/A 3940SN/A case SSL3_ST_CR_SRVR_DONE_A: 3950SN/A case SSL3_ST_CR_SRVR_DONE_B: 3960SN/A ret = ssl3_get_server_done(s); 3970SN/A if (ret <= 0) 3980SN/A goto end; 3990SN/A#ifndef OPENSSL_NO_SRP 4000SN/A if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { 4010SN/A if ((ret = SRP_Calc_A_param(s)) <= 0) { 4020SN/A SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC); 4030SN/A ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); 4040SN/A s->state = SSL_ST_ERR; 4050SN/A goto end; 4060SN/A } 4070SN/A } 4080SN/A#endif 4090SN/A if (s->s3->tmp.cert_req) 4100SN/A s->state = SSL3_ST_CW_CERT_A; 4110SN/A else 4120SN/A s->state = SSL3_ST_CW_KEY_EXCH_A; 4130SN/A s->init_num = 0; 4140SN/A 4150SN/A break; 4160SN/A 4170SN/A case SSL3_ST_CW_CERT_A: 4180SN/A case SSL3_ST_CW_CERT_B: 4190SN/A case SSL3_ST_CW_CERT_C: 4200SN/A case SSL3_ST_CW_CERT_D: 4210SN/A ret = ssl3_send_client_certificate(s); 4220SN/A if (ret <= 0) 4230SN/A goto end; 4240SN/A s->state = SSL3_ST_CW_KEY_EXCH_A; 4250SN/A s->init_num = 0; 4260SN/A break; 4270SN/A 4280SN/A case SSL3_ST_CW_KEY_EXCH_A: 4290SN/A case SSL3_ST_CW_KEY_EXCH_B: 4300SN/A ret = ssl3_send_client_key_exchange(s); 4310SN/A if (ret <= 0) 4320SN/A goto end; 4330SN/A /* 4340SN/A * EAY EAY EAY need to check for DH fix cert sent back 4350SN/A */ 4360SN/A /* 4370SN/A * For TLS, cert_req is set to 2, so a cert chain of nothing is 4380SN/A * sent, but no verify packet is sent 4390SN/A */ 4400SN/A /* 4410SN/A * XXX: For now, we do not support client authentication in ECDH 4420SN/A * cipher suites with ECDH (rather than ECDSA) certificates. We 4430SN/A * need to skip the certificate verify message when client's 4440SN/A * ECDH public key is sent inside the client certificate. 4450SN/A */ 4460SN/A if (s->s3->tmp.cert_req == 1) { 4470SN/A s->state = SSL3_ST_CW_CERT_VRFY_A; 4480SN/A } else { 4490SN/A s->state = SSL3_ST_CW_CHANGE_A; 4500SN/A } 4510SN/A if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { 4520SN/A s->state = SSL3_ST_CW_CHANGE_A; 4530SN/A } 4540SN/A 4550SN/A s->init_num = 0; 4560SN/A break; 4570SN/A 4580SN/A case SSL3_ST_CW_CERT_VRFY_A: 4590SN/A case SSL3_ST_CW_CERT_VRFY_B: 4600SN/A ret = ssl3_send_client_verify(s); 4610SN/A if (ret <= 0) 4620SN/A goto end; 4630SN/A s->state = SSL3_ST_CW_CHANGE_A; 4640SN/A s->init_num = 0; 4650SN/A break; 4660SN/A 4670SN/A case SSL3_ST_CW_CHANGE_A: 4680SN/A case SSL3_ST_CW_CHANGE_B: 4690SN/A ret = ssl3_send_change_cipher_spec(s, 4700SN/A SSL3_ST_CW_CHANGE_A, 4710SN/A SSL3_ST_CW_CHANGE_B); 4720SN/A if (ret <= 0) 4730SN/A goto end; 4740SN/A 4750SN/A#if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG) 4760SN/A s->state = SSL3_ST_CW_FINISHED_A; 4770SN/A#else 4780SN/A if (s->s3->next_proto_neg_seen) 4790SN/A s->state = SSL3_ST_CW_NEXT_PROTO_A; 4800SN/A else 4810SN/A s->state = SSL3_ST_CW_FINISHED_A; 4820SN/A#endif 4830SN/A s->init_num = 0; 4840SN/A 4850SN/A s->session->cipher = s->s3->tmp.new_cipher; 4860SN/A#ifdef OPENSSL_NO_COMP 4870SN/A s->session->compress_meth = 0; 4880SN/A#else 4890SN/A if (s->s3->tmp.new_compression == NULL) 4900SN/A s->session->compress_meth = 0; 4910SN/A else 4920SN/A s->session->compress_meth = s->s3->tmp.new_compression->id; 4930SN/A#endif 4940SN/A if (!s->method->ssl3_enc->setup_key_block(s)) { 4950SN/A ret = -1; 4960SN/A s->state = SSL_ST_ERR; 4970SN/A goto end; 4980SN/A } 4990SN/A 5000SN/A if (!s->method->ssl3_enc->change_cipher_state(s, 5010SN/A SSL3_CHANGE_CIPHER_CLIENT_WRITE)) 5020SN/A { 5030SN/A ret = -1; 5040SN/A s->state = SSL_ST_ERR; 5050SN/A goto end; 5060SN/A } 5070SN/A 5080SN/A break; 5090SN/A 5100SN/A#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) 5110SN/A case SSL3_ST_CW_NEXT_PROTO_A: 5120SN/A case SSL3_ST_CW_NEXT_PROTO_B: 5130SN/A ret = ssl3_send_next_proto(s); 5140SN/A if (ret <= 0) 5150SN/A goto end; 5160SN/A s->state = SSL3_ST_CW_FINISHED_A; 5170SN/A break; 5180SN/A#endif 5190SN/A 5200SN/A case SSL3_ST_CW_FINISHED_A: 5210SN/A case SSL3_ST_CW_FINISHED_B: 5220SN/A ret = ssl3_send_finished(s, 5230SN/A SSL3_ST_CW_FINISHED_A, 5240SN/A SSL3_ST_CW_FINISHED_B, 5250SN/A s->method-> 5260SN/A ssl3_enc->client_finished_label, 5270SN/A s->method-> 5280SN/A ssl3_enc->client_finished_label_len); 5290SN/A if (ret <= 0) 5300SN/A goto end; 5310SN/A s->state = SSL3_ST_CW_FLUSH; 5320SN/A 5330SN/A /* clear flags */ 5340SN/A s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER; 5350SN/A if (s->hit) { 5360SN/A s->s3->tmp.next_state = SSL_ST_OK; 5370SN/A if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) { 5380SN/A s->state = SSL_ST_OK; 5390SN/A s->s3->flags |= SSL3_FLAGS_POP_BUFFER; 5400SN/A s->s3->delay_buf_pop_ret = 0; 5410SN/A } 5420SN/A } else { 5430SN/A#ifndef OPENSSL_NO_TLSEXT 5440SN/A /* 5450SN/A * Allow NewSessionTicket if ticket expected 5460SN/A */ 5470SN/A if (s->tlsext_ticket_expected) 5480SN/A s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A; 5490SN/A else 5500SN/A#endif 5510SN/A 5520SN/A s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A; 5530SN/A } 5540SN/A s->init_num = 0; 5550SN/A break; 5560SN/A 5570SN/A#ifndef OPENSSL_NO_TLSEXT 5580SN/A case SSL3_ST_CR_SESSION_TICKET_A: 5590SN/A case SSL3_ST_CR_SESSION_TICKET_B: 5600SN/A ret = ssl3_get_new_session_ticket(s); 5610SN/A if (ret <= 0) 5620SN/A goto end; 5630SN/A s->state = SSL3_ST_CR_FINISHED_A; 5640SN/A s->init_num = 0; 5650SN/A break; 5660SN/A 5670SN/A case SSL3_ST_CR_CERT_STATUS_A: 5680SN/A case SSL3_ST_CR_CERT_STATUS_B: 5690SN/A ret = ssl3_get_cert_status(s); 5700SN/A if (ret <= 0) 5710SN/A goto end; 5720SN/A s->state = SSL3_ST_CR_KEY_EXCH_A; 5730SN/A s->init_num = 0; 5740SN/A break; 5750SN/A#endif 5760SN/A 5770SN/A case SSL3_ST_CR_FINISHED_A: 5780SN/A case SSL3_ST_CR_FINISHED_B: 5790SN/A if (!s->s3->change_cipher_spec) 5800SN/A s->s3->flags |= SSL3_FLAGS_CCS_OK; 5810SN/A ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, 5820SN/A SSL3_ST_CR_FINISHED_B); 5830SN/A if (ret <= 0) 5840SN/A goto end; 5850SN/A 5860SN/A if (s->hit) 5870SN/A s->state = SSL3_ST_CW_CHANGE_A; 5880SN/A else 5890SN/A s->state = SSL_ST_OK; 5900SN/A s->init_num = 0; 5910SN/A break; 5920SN/A 5930SN/A case SSL3_ST_CW_FLUSH: 5940SN/A s->rwstate = SSL_WRITING; 5950SN/A if (BIO_flush(s->wbio) <= 0) { 5960SN/A ret = -1; 5970SN/A goto end; 5980SN/A } 5990SN/A s->rwstate = SSL_NOTHING; 6000SN/A s->state = s->s3->tmp.next_state; 6010SN/A break; 6020SN/A 6030SN/A case SSL_ST_OK: 6040SN/A /* clean a few things up */ 6050SN/A ssl3_cleanup_key_block(s); 6060SN/A 6070SN/A if (s->init_buf != NULL) { 6080SN/A BUF_MEM_free(s->init_buf); 6090SN/A s->init_buf = NULL; 6100SN/A } 6110SN/A 6120SN/A /* 6130SN/A * If we are not 'joining' the last two packets, remove the 6140SN/A * buffering now 6150SN/A */ 6160SN/A if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER)) 6170SN/A ssl_free_wbio_buffer(s); 6180SN/A /* else do it later in ssl3_write */ 6190SN/A 6200SN/A s->init_num = 0; 6210SN/A s->renegotiate = 0; 6220SN/A s->new_session = 0; 6230SN/A 6240SN/A ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); 6250SN/A if (s->hit) 6260SN/A s->ctx->stats.sess_hit++; 6270SN/A 6280SN/A ret = 1; 6290SN/A /* s->server=0; */ 6300SN/A s->handshake_func = ssl3_connect; 6310SN/A s->ctx->stats.sess_connect_good++; 6320SN/A 6330SN/A if (cb != NULL) 6340SN/A cb(s, SSL_CB_HANDSHAKE_DONE, 1); 6350SN/A 6360SN/A goto end; 6370SN/A /* break; */ 6380SN/A 6390SN/A case SSL_ST_ERR: 6400SN/A default: 6410SN/A SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE); 6420SN/A ret = -1; 6430SN/A goto end; 6440SN/A /* break; */ 6450SN/A } 6460SN/A 6470SN/A /* did we do anything */ 6480SN/A if (!s->s3->tmp.reuse_message && !skip) { 6490SN/A if (s->debug) { 6500SN/A if ((ret = BIO_flush(s->wbio)) <= 0) 6510SN/A goto end; 6520SN/A } 6530SN/A 6540SN/A if ((cb != NULL) && (s->state != state)) { 6550SN/A new_state = s->state; 6560SN/A s->state = state; 6570SN/A cb(s, SSL_CB_CONNECT_LOOP, 1); 6580SN/A s->state = new_state; 6590SN/A } 6600SN/A } 6610SN/A skip = 0; 6620SN/A } 6630SN/A end: 6640SN/A s->in_handshake--; 6650SN/A if (buf != NULL) 6660SN/A BUF_MEM_free(buf); 6670SN/A if (cb != NULL) 6680SN/A cb(s, SSL_CB_CONNECT_EXIT, ret); 6690SN/A return (ret); 6700SN/A} 6710SN/A 6720SN/Aint ssl3_client_hello(SSL *s) 6730SN/A{ 6740SN/A unsigned char *buf; 6750SN/A unsigned char *p, *d; 6760SN/A int i; 6770SN/A unsigned long l; 6780SN/A#ifndef OPENSSL_NO_COMP 6790SN/A int j; 6800SN/A SSL_COMP *comp; 6810SN/A#endif 6820SN/A 6830SN/A buf = (unsigned char *)s->init_buf->data; 6840SN/A if (s->state == SSL3_ST_CW_CLNT_HELLO_A) { 6850SN/A SSL_SESSION *sess = s->session; 6860SN/A if ((sess == NULL) || (sess->ssl_version != s->version) || 6870SN/A#ifdef OPENSSL_NO_TLSEXT 6880SN/A !sess->session_id_length || 6890SN/A#else 6900SN/A /* 6910SN/A * In the case of EAP-FAST, we can have a pre-shared 6920SN/A * "ticket" without a session ID. 6930SN/A */ 6940SN/A (!sess->session_id_length && !sess->tlsext_tick) || 6950SN/A#endif 6960SN/A (sess->not_resumable)) { 6970SN/A if (!ssl_get_new_session(s, 0)) 6980SN/A goto err; 6990SN/A } 7000SN/A /* else use the pre-loaded session */ 7010SN/A 7020SN/A p = s->s3->client_random; 7030SN/A 7040SN/A if (ssl_fill_hello_random(s, 0, p, SSL3_RANDOM_SIZE) <= 0) 7050SN/A goto err; 7060SN/A 7070SN/A /* Do the message type and length last */ 7080SN/A d = p = &(buf[4]); 7090SN/A 7100SN/A /*- 7110SN/A * version indicates the negotiated version: for example from 7120SN/A * an SSLv2/v3 compatible client hello). The client_version 7130SN/A * field is the maximum version we permit and it is also 7140SN/A * used in RSA encrypted premaster secrets. Some servers can 7150SN/A * choke if we initially report a higher version then 7160SN/A * renegotiate to a lower one in the premaster secret. This 7170SN/A * didn't happen with TLS 1.0 as most servers supported it 7180SN/A * but it can with TLS 1.1 or later if the server only supports 7190SN/A * 1.0. 7200SN/A * 7210SN/A * Possible scenario with previous logic: 7220SN/A * 1. Client hello indicates TLS 1.2 7230SN/A * 2. Server hello says TLS 1.0 7240SN/A * 3. RSA encrypted premaster secret uses 1.2. 7250SN/A * 4. Handhaked proceeds using TLS 1.0. 7260SN/A * 5. Server sends hello request to renegotiate. 7270SN/A * 6. Client hello indicates TLS v1.0 as we now 7280SN/A * know that is maximum server supports. 7290SN/A * 7. Server chokes on RSA encrypted premaster secret 7300SN/A * containing version 1.0. 7310SN/A * 7320SN/A * For interoperability it should be OK to always use the 7330SN/A * maximum version we support in client hello and then rely 7340SN/A * on the checking of version to ensure the servers isn't 7350SN/A * being inconsistent: for example initially negotiating with 7360SN/A * TLS 1.0 and renegotiating with TLS 1.2. We do this by using 7370SN/A * client_version in client hello and not resetting it to 7380SN/A * the negotiated version. 7390SN/A */ 7400SN/A#if 0 7410SN/A *(p++) = s->version >> 8; 7420SN/A *(p++) = s->version & 0xff; 7430SN/A s->client_version = s->version; 7440SN/A#else 7450SN/A *(p++) = s->client_version >> 8; 7460SN/A *(p++) = s->client_version & 0xff; 7470SN/A#endif 7480SN/A 7490SN/A /* Random stuff */ 7500SN/A memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE); 7510SN/A p += SSL3_RANDOM_SIZE; 7520SN/A 7530SN/A /* Session ID */ 7540SN/A if (s->new_session) 7550SN/A i = 0; 7560SN/A else 7570SN/A i = s->session->session_id_length; 7580SN/A *(p++) = i; 7590SN/A if (i != 0) { 7600SN/A if (i > (int)sizeof(s->session->session_id)) { 7610SN/A SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); 7620SN/A goto err; 7630SN/A } 7640SN/A memcpy(p, s->session->session_id, i); 7650SN/A p += i; 7660SN/A } 7670SN/A 7680SN/A /* Ciphers supported */ 7690SN/A i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0); 7700SN/A if (i == 0) { 7710SN/A SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE); 7720SN/A goto err; 7730SN/A } 7740SN/A#ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH 7750SN/A /* 7760SN/A * Some servers hang if client hello > 256 bytes as hack workaround 7770SN/A * chop number of supported ciphers to keep it well below this if we 7780SN/A * use TLS v1.2 7790SN/A */ 7800SN/A if (TLS1_get_version(s) >= TLS1_2_VERSION 7810SN/A && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH) 7820SN/A i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1; 7830SN/A#endif 7840SN/A s2n(i, p); 7850SN/A p += i; 7860SN/A 7870SN/A /* COMPRESSION */ 7880SN/A#ifdef OPENSSL_NO_COMP 7890SN/A *(p++) = 1; 7900SN/A#else 7910SN/A 7920SN/A if ((s->options & SSL_OP_NO_COMPRESSION) 7930SN/A || !s->ctx->comp_methods) 7940SN/A j = 0; 7950SN/A else 7960SN/A j = sk_SSL_COMP_num(s->ctx->comp_methods); 7970SN/A *(p++) = 1 + j; 7980SN/A for (i = 0; i < j; i++) { 7990SN/A comp = sk_SSL_COMP_value(s->ctx->comp_methods, i); 8000SN/A *(p++) = comp->id; 8010SN/A } 8020SN/A#endif 8030SN/A *(p++) = 0; /* Add the NULL method */ 8040SN/A 8050SN/A#ifndef OPENSSL_NO_TLSEXT 8060SN/A /* TLS extensions */ 8070SN/A if (ssl_prepare_clienthello_tlsext(s) <= 0) { 8080SN/A SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT); 8090SN/A goto err; 8100SN/A } 8110SN/A if ((p = 8120SN/A ssl_add_clienthello_tlsext(s, p, 8130SN/A buf + SSL3_RT_MAX_PLAIN_LENGTH)) == 8140SN/A NULL) { 8150SN/A SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); 8160SN/A goto err; 8170SN/A } 8180SN/A#endif 8190SN/A 8200SN/A l = (p - d); 8210SN/A d = buf; 8220SN/A *(d++) = SSL3_MT_CLIENT_HELLO; 8230SN/A l2n3(l, d); 8240SN/A 8250SN/A s->state = SSL3_ST_CW_CLNT_HELLO_B; 8260SN/A /* number of bytes to write */ 8270SN/A s->init_num = p - buf; 8280SN/A s->init_off = 0; 8290SN/A } 8300SN/A 8310SN/A /* SSL3_ST_CW_CLNT_HELLO_B */ 8320SN/A return (ssl3_do_write(s, SSL3_RT_HANDSHAKE)); 8330SN/A err: 8340SN/A s->state = SSL_ST_ERR; 8350SN/A return (-1); 8360SN/A} 8370SN/A 8380SN/Aint ssl3_get_server_hello(SSL *s) 8390SN/A{ 8400SN/A STACK_OF(SSL_CIPHER) *sk; 8410SN/A const SSL_CIPHER *c; 8428241SN/A unsigned char *p, *d; 8430SN/A int i, al, ok; 8440SN/A unsigned int j; 8450SN/A long n; 8460SN/A#ifndef OPENSSL_NO_COMP 8470SN/A SSL_COMP *comp; 8480SN/A#endif 8490SN/A 8500SN/A n = s->method->ssl_get_message(s, 8519167SN/A SSL3_ST_CR_SRVR_HELLO_A, 8520SN/A SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok); 8530SN/A 8540SN/A if (!ok) 8550SN/A return ((int)n); 8560SN/A 8570SN/A if (SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER) { 8580SN/A if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) { 8590SN/A if (s->d1->send_cookie == 0) { 8600SN/A s->s3->tmp.reuse_message = 1; 8610SN/A return 1; 8620SN/A } else { /* already sent a cookie */ 8630SN/A 8640SN/A al = SSL_AD_UNEXPECTED_MESSAGE; 8650SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE); 8660SN/A goto f_err; 8670SN/A } 8680SN/A } 8690SN/A } 8700SN/A 8710SN/A if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) { 8720SN/A al = SSL_AD_UNEXPECTED_MESSAGE; 8730SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE); 8745258SN/A goto f_err; 8755258SN/A } 8765258SN/A 8775258SN/A d = p = (unsigned char *)s->init_msg; 8785258SN/A 8795258SN/A if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) { 8805258SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION); 8815258SN/A s->version = (s->version & 0xff00) | p[1]; 8825258SN/A al = SSL_AD_PROTOCOL_VERSION; 8835258SN/A goto f_err; 8845258SN/A } 8855258SN/A p += 2; 8860SN/A 8870SN/A /* load the server hello data */ 8880SN/A /* load the server random */ 8890SN/A memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE); 8900SN/A p += SSL3_RANDOM_SIZE; 8910SN/A 8920SN/A s->hit = 0; 8930SN/A 8940SN/A /* get the session-id */ 8950SN/A j = *(p++); 8960SN/A 8970SN/A if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) { 8980SN/A al = SSL_AD_ILLEGAL_PARAMETER; 8990SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG); 9000SN/A goto f_err; 9010SN/A } 9028241SN/A#ifndef OPENSSL_NO_TLSEXT 9030SN/A /* 9040SN/A * Check if we can resume the session based on external pre-shared secret. 9050SN/A * EAP-FAST (RFC 4851) supports two types of session resumption. 9060SN/A * Resumption based on server-side state works with session IDs. 9070SN/A * Resumption based on pre-shared Protected Access Credentials (PACs) 9080SN/A * works by overriding the SessionTicket extension at the application 9099167SN/A * layer, and does not send a session ID. (We do not know whether EAP-FAST 9100SN/A * servers would honour the session ID.) Therefore, the session ID alone 9110SN/A * is not a reliable indicator of session resumption, so we first check if 9120SN/A * we can resume, and later peek at the next handshake message to see if the 9130SN/A * server wants to resume. 9140SN/A */ 9150SN/A if (s->version >= TLS1_VERSION && s->tls_session_secret_cb && 9160SN/A s->session->tlsext_tick) { 9170SN/A SSL_CIPHER *pref_cipher = NULL; 9180SN/A s->session->master_key_length = sizeof(s->session->master_key); 9190SN/A if (s->tls_session_secret_cb(s, s->session->master_key, 9200SN/A &s->session->master_key_length, 9210SN/A NULL, &pref_cipher, 9220SN/A s->tls_session_secret_cb_arg)) { 9230SN/A s->session->cipher = pref_cipher ? 9240SN/A pref_cipher : ssl_get_cipher_by_char(s, p + j); 9250SN/A } else { 9260SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR); 9270SN/A al = SSL_AD_INTERNAL_ERROR; 9280SN/A goto f_err; 9290SN/A } 9300SN/A } 9310SN/A#endif /* OPENSSL_NO_TLSEXT */ 9320SN/A 9330SN/A if (j != 0 && j == s->session->session_id_length 9340SN/A && memcmp(p, s->session->session_id, j) == 0) { 9350SN/A if (s->sid_ctx_length != s->session->sid_ctx_length 9360SN/A || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) { 9370SN/A /* actually a client application bug */ 9380SN/A al = SSL_AD_ILLEGAL_PARAMETER; 9390SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, 9400SN/A SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); 9410SN/A goto f_err; 9420SN/A } 9430SN/A s->hit = 1; 9440SN/A } else { 9450SN/A /* 9460SN/A * If we were trying for session-id reuse but the server 9470SN/A * didn't echo the ID, make a new SSL_SESSION. 9480SN/A * In the case of EAP-FAST and PAC, we do not send a session ID, 9490SN/A * so the PAC-based session secret is always preserved. It'll be 9500SN/A * overwritten if the server refuses resumption. 9518241SN/A */ 9520SN/A if (s->session->session_id_length > 0) { 9530SN/A if (!ssl_get_new_session(s, 0)) { 9540SN/A al = SSL_AD_INTERNAL_ERROR; 9550SN/A goto f_err; 9560SN/A } 9570SN/A } 9589167SN/A s->session->session_id_length = j; 9590SN/A memcpy(s->session->session_id, p, j); /* j could be 0 */ 9600SN/A } 9610SN/A p += j; 9620SN/A c = ssl_get_cipher_by_char(s, p); 9630SN/A if (c == NULL) { 9640SN/A /* unknown cipher */ 9650SN/A al = SSL_AD_ILLEGAL_PARAMETER; 9660SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED); 9670SN/A goto f_err; 9680SN/A } 9690SN/A /* TLS v1.2 only ciphersuites require v1.2 or later */ 9700SN/A if ((c->algorithm_ssl & SSL_TLSV1_2) && 9710SN/A (TLS1_get_version(s) < TLS1_2_VERSION)) { 9720SN/A al = SSL_AD_ILLEGAL_PARAMETER; 9730SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); 9740SN/A goto f_err; 9750SN/A } 9760SN/A#ifndef OPENSSL_NO_SRP 9770SN/A if (((c->algorithm_mkey & SSL_kSRP) || (c->algorithm_auth & SSL_aSRP)) && 9780SN/A !(s->srp_ctx.srp_Mask & SSL_kSRP)) { 9790SN/A al = SSL_AD_ILLEGAL_PARAMETER; 9800SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); 9810SN/A goto f_err; 9820SN/A } 9830SN/A#endif /* OPENSSL_NO_SRP */ 9840SN/A p += ssl_put_cipher_by_char(s, NULL, NULL); 9850SN/A 9860SN/A sk = ssl_get_ciphers_by_id(s); 9870SN/A i = sk_SSL_CIPHER_find(sk, c); 9880SN/A if (i < 0) { 9890SN/A /* we did not say we would use this cipher */ 9900SN/A al = SSL_AD_ILLEGAL_PARAMETER; 9910SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); 9920SN/A goto f_err; 9930SN/A } 9948241SN/A 9950SN/A /* 9960SN/A * Depending on the session caching (internal/external), the cipher 9970SN/A * and/or cipher_id values may not be set. Make sure that cipher_id is 9980SN/A * set and use it for comparison. 9990SN/A */ 10000SN/A if (s->session->cipher) 10019167SN/A s->session->cipher_id = s->session->cipher->id; 10020SN/A if (s->hit && (s->session->cipher_id != c->id)) { 10030SN/A/* Workaround is now obsolete */ 10040SN/A#if 0 10050SN/A if (!(s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)) 10060SN/A#endif 10070SN/A { 10080SN/A al = SSL_AD_ILLEGAL_PARAMETER; 10090SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, 10100SN/A SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); 10110SN/A goto f_err; 10120SN/A } 10130SN/A } 10140SN/A s->s3->tmp.new_cipher = c; 10150SN/A /* 10160SN/A * Don't digest cached records if TLS v1.2: we may need them for client 10170SN/A * authentication. 10180SN/A */ 10190SN/A if (TLS1_get_version(s) < TLS1_2_VERSION 10200SN/A && !ssl3_digest_cached_records(s)) { 10210SN/A al = SSL_AD_INTERNAL_ERROR; 10220SN/A goto f_err; 10230SN/A } 10240SN/A /* lets get the compression algorithm */ 10250SN/A /* COMPRESSION */ 10260SN/A#ifdef OPENSSL_NO_COMP 10270SN/A if (*(p++) != 0) { 10280SN/A al = SSL_AD_ILLEGAL_PARAMETER; 10290SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, 10300SN/A SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); 10310SN/A goto f_err; 10320SN/A } 10330SN/A /* 10349660SN/A * If compression is disabled we'd better not try to resume a session 10350SN/A * using compression. 10360SN/A */ 10370SN/A if (s->session->compress_meth != 0) { 10380SN/A al = SSL_AD_INTERNAL_ERROR; 10390SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION); 10400SN/A goto f_err; 10410SN/A } 10420SN/A#else 10430SN/A j = *(p++); 10440SN/A if (s->hit && j != s->session->compress_meth) { 10450SN/A al = SSL_AD_ILLEGAL_PARAMETER; 10460SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, 10470SN/A SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED); 10480SN/A goto f_err; 10490SN/A } 10500SN/A if (j == 0) 10510SN/A comp = NULL; 10520SN/A else if (s->options & SSL_OP_NO_COMPRESSION) { 10530SN/A al = SSL_AD_ILLEGAL_PARAMETER; 10540SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED); 10550SN/A goto f_err; 10560SN/A } else 10570SN/A comp = ssl3_comp_find(s->ctx->comp_methods, j); 10580SN/A 10590SN/A if ((j != 0) && (comp == NULL)) { 10600SN/A al = SSL_AD_ILLEGAL_PARAMETER; 10610SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, 10620SN/A SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); 10630SN/A goto f_err; 10640SN/A } else { 10650SN/A s->s3->tmp.new_compression = comp; 10660SN/A } 10670SN/A#endif 10680SN/A 10690SN/A#ifndef OPENSSL_NO_TLSEXT 10700SN/A /* TLS extensions */ 10710SN/A if (s->version >= SSL3_VERSION) { 10720SN/A if (!ssl_parse_serverhello_tlsext(s, &p, d, n, &al)) { 10730SN/A /* 'al' set by ssl_parse_serverhello_tlsext */ 10740SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT); 10750SN/A goto f_err; 10760SN/A } 10770SN/A if (ssl_check_serverhello_tlsext(s) <= 0) { 10780SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT); 10790SN/A goto err; 10800SN/A } 10810SN/A } 10820SN/A#endif 10830SN/A 10840SN/A if (p != (d + n)) { 10850SN/A /* wrong packet length */ 10860SN/A al = SSL_AD_DECODE_ERROR; 10870SN/A SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH); 10880SN/A goto f_err; 10890SN/A } 10900SN/A 10910SN/A return (1); 10920SN/A f_err: 10939660SN/A ssl3_send_alert(s, SSL3_AL_FATAL, al); 10940SN/A err: 10950SN/A s->state = SSL_ST_ERR; 10960SN/A return (-1); 10970SN/A} 10980SN/A 10990SN/Aint ssl3_get_server_certificate(SSL *s) 11000SN/A{ 11010SN/A int al, i, ok, ret = -1; 11020SN/A unsigned long n, nc, llen, l; 11030SN/A X509 *x = NULL; 11040SN/A const unsigned char *q, *p; 11050SN/A unsigned char *d; 11060SN/A STACK_OF(X509) *sk = NULL; 11070SN/A SESS_CERT *sc; 11080SN/A EVP_PKEY *pkey = NULL; 11090SN/A int need_cert = 1; /* VRS: 0=> will allow null cert if auth == 11100SN/A * KRB5 */ 11110SN/A 11120SN/A n = s->method->ssl_get_message(s, 11130SN/A SSL3_ST_CR_CERT_A, 11140SN/A SSL3_ST_CR_CERT_B, 11150SN/A -1, s->max_cert_list, &ok); 11160SN/A 11170SN/A if (!ok) 11180SN/A return ((int)n); 11190SN/A 11200SN/A if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) || 11210SN/A ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) && 11220SN/A (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) { 11230SN/A s->s3->tmp.reuse_message = 1; 11240SN/A return (1); 11250SN/A } 11260SN/A 11270SN/A if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) { 11280SN/A al = SSL_AD_UNEXPECTED_MESSAGE; 11290SN/A SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE); 11300SN/A goto f_err; 11310SN/A } 11320SN/A p = d = (unsigned char *)s->init_msg; 11330SN/A 11340SN/A if ((sk = sk_X509_new_null()) == NULL) { 11350SN/A SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); 11360SN/A goto err; 11370SN/A } 11380SN/A 11390SN/A n2l3(p, llen); 11400SN/A if (llen + 3 != n) { 11410SN/A al = SSL_AD_DECODE_ERROR; 11420SN/A SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH); 11430SN/A goto f_err; 11440SN/A } 11459660SN/A for (nc = 0; nc < llen;) { 11460SN/A if (nc + 3 > llen) { 11470SN/A al = SSL_AD_DECODE_ERROR; 11480SN/A SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, 11490SN/A SSL_R_CERT_LENGTH_MISMATCH); 11500SN/A goto f_err; 11510SN/A } 11520SN/A n2l3(p, l); 11530SN/A if ((l + nc + 3) > llen) { 11540SN/A al = SSL_AD_DECODE_ERROR; 11550SN/A SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, 11560SN/A SSL_R_CERT_LENGTH_MISMATCH); 11570SN/A goto f_err; 11580SN/A } 11590SN/A 11600SN/A q = p; 11610SN/A x = d2i_X509(NULL, &q, l); 11620SN/A if (x == NULL) { 11630SN/A al = SSL_AD_BAD_CERTIFICATE; 11640SN/A SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB); 11650SN/A goto f_err; 11660SN/A } 11670SN/A if (q != (p + l)) { 11680SN/A al = SSL_AD_DECODE_ERROR; 11690SN/A SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, 11700SN/A SSL_R_CERT_LENGTH_MISMATCH); 11710SN/A goto f_err; 11720SN/A } 11730SN/A if (!sk_X509_push(sk, x)) { 11740SN/A SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); 11750SN/A goto err; 11760SN/A } 11770SN/A x = NULL; 11780SN/A nc += l + 3; 11790SN/A p = q; 11800SN/A } 11810SN/A 11820SN/A i = ssl_verify_cert_chain(s, sk); 11830SN/A if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0) 11840SN/A#ifndef OPENSSL_NO_KRB5 11850SN/A && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) && 11860SN/A (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)) 11870SN/A#endif /* OPENSSL_NO_KRB5 */ 11880SN/A ) { 11890SN/A al = ssl_verify_alarm_type(s->verify_result); 11900SN/A SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, 11910SN/A SSL_R_CERTIFICATE_VERIFY_FAILED); 11920SN/A goto f_err; 11930SN/A } 11940SN/A ERR_clear_error(); /* but we keep s->verify_result */ 11950SN/A 11960SN/A sc = ssl_sess_cert_new(); 11970SN/A if (sc == NULL) 11980SN/A goto err; 11990SN/A 12000SN/A if (s->session->sess_cert) 12010SN/A ssl_sess_cert_free(s->session->sess_cert); 12020SN/A s->session->sess_cert = sc; 12030SN/A 12040SN/A sc->cert_chain = sk; 12050SN/A /* 12060SN/A * Inconsistency alert: cert_chain does include the peer's certificate, 12070SN/A * which we don't include in s3_srvr.c 12080SN/A */ 12090SN/A x = sk_X509_value(sk, 0); 12100SN/A sk = NULL; 12110SN/A /* 12120SN/A * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end 12130SN/A */ 12140SN/A 12150SN/A pkey = X509_get_pubkey(x); 12160SN/A 12170SN/A /* VRS: allow null cert if auth == KRB5 */ 12180SN/A need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) && 12190SN/A (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)) 12209660SN/A ? 0 : 1; 12210SN/A 12220SN/A#ifdef KSSL_DEBUG 12230SN/A fprintf(stderr, "pkey,x = %p, %p\n", pkey, x); 12240SN/A fprintf(stderr, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x, pkey)); 12250SN/A fprintf(stderr, "cipher, alg, nc = %s, %lx, %lx, %d\n", 12260SN/A s->s3->tmp.new_cipher->name, 12270SN/A s->s3->tmp.new_cipher->algorithm_mkey, 12280SN/A s->s3->tmp.new_cipher->algorithm_auth, need_cert); 12290SN/A#endif /* KSSL_DEBUG */ 12300SN/A 12310SN/A if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) { 12320SN/A x = NULL; 12330SN/A al = SSL3_AL_FATAL; 12340SN/A SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, 12350SN/A SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); 12360SN/A goto f_err; 12370SN/A } 12380SN/A 12390SN/A i = ssl_cert_type(x, pkey); 12400SN/A if (need_cert && i < 0) { 12410SN/A x = NULL; 12420SN/A al = SSL3_AL_FATAL; 12430SN/A SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, 12440SN/A SSL_R_UNKNOWN_CERTIFICATE_TYPE); 12450SN/A goto f_err; 12469660SN/A } 12470SN/A 12480SN/A if (need_cert) { 12490SN/A sc->peer_cert_type = i; 12500SN/A CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); 12510SN/A /* 12520SN/A * Why would the following ever happen? We just created sc a couple 12530SN/A * of lines ago. 12540SN/A */ 12550SN/A if (sc->peer_pkeys[i].x509 != NULL) 12560SN/A X509_free(sc->peer_pkeys[i].x509); 12570SN/A sc->peer_pkeys[i].x509 = x; 12580SN/A sc->peer_key = &(sc->peer_pkeys[i]); 12590SN/A 12600SN/A if (s->session->peer != NULL) 12610SN/A X509_free(s->session->peer); 12620SN/A CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); 12630SN/A s->session->peer = x; 12640SN/A } else { 12650SN/A sc->peer_cert_type = i; 12660SN/A sc->peer_key = NULL; 12670SN/A 12680SN/A if (s->session->peer != NULL) 12690SN/A X509_free(s->session->peer); 12700SN/A s->session->peer = NULL; 12710SN/A } 12720SN/A s->session->verify_result = s->verify_result; 12730SN/A 12740SN/A x = NULL; 12750SN/A ret = 1; 12768241SN/A 12770SN/A if (0) { 12780SN/A f_err: 12790SN/A ssl3_send_alert(s, SSL3_AL_FATAL, al); 12800SN/A err: 12810SN/A s->state = SSL_ST_ERR; 12820SN/A } 12839167SN/A 12840SN/A EVP_PKEY_free(pkey); 12850SN/A X509_free(x); 12860SN/A sk_X509_pop_free(sk, X509_free); 12870SN/A return (ret); 12880SN/A} 12890SN/A 12900SN/Aint ssl3_get_key_exchange(SSL *s) 12910SN/A{ 12920SN/A#ifndef OPENSSL_NO_RSA 12930SN/A unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2]; 12940SN/A#endif 12950SN/A EVP_MD_CTX md_ctx; 12960SN/A unsigned char *param, *p; 12970SN/A int al, j, ok; 12980SN/A long i, param_len, n, alg_k, alg_a; 12990SN/A EVP_PKEY *pkey = NULL; 13000SN/A const EVP_MD *md = NULL; 13010SN/A#ifndef OPENSSL_NO_RSA 13020SN/A RSA *rsa = NULL; 13030SN/A#endif 13040SN/A#ifndef OPENSSL_NO_DH 13050SN/A DH *dh = NULL; 13060SN/A#endif 13070SN/A#ifndef OPENSSL_NO_ECDH 13080SN/A EC_KEY *ecdh = NULL; 13090SN/A BN_CTX *bn_ctx = NULL; 13100SN/A EC_POINT *srvr_ecpoint = NULL; 13110SN/A int curve_nid = 0; 13120SN/A int encoded_pt_len = 0; 13138241SN/A#endif 13140SN/A 13150SN/A EVP_MD_CTX_init(&md_ctx); 13160SN/A 13170SN/A /* 13180SN/A * use same message size as in ssl3_get_certificate_request() as 13190SN/A * ServerKeyExchange message may be skipped 13209167SN/A */ 13210SN/A n = s->method->ssl_get_message(s, 13220SN/A SSL3_ST_CR_KEY_EXCH_A, 13230SN/A SSL3_ST_CR_KEY_EXCH_B, 13240SN/A -1, s->max_cert_list, &ok); 13250SN/A if (!ok) 13260SN/A return ((int)n); 13270SN/A 13280SN/A alg_k = s->s3->tmp.new_cipher->algorithm_mkey; 13290SN/A 13300SN/A if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) { 13310SN/A /* 13320SN/A * Can't skip server key exchange if this is an ephemeral 13330SN/A * ciphersuite. 13340SN/A */ 13350SN/A if (alg_k & (SSL_kEDH | SSL_kEECDH)) { 13360SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); 13370SN/A al = SSL_AD_UNEXPECTED_MESSAGE; 13380SN/A goto f_err; 13390SN/A } 13400SN/A#ifndef OPENSSL_NO_PSK 13410SN/A /* 13420SN/A * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no 13430SN/A * identity hint is sent. Set session->sess_cert anyway to avoid 13440SN/A * problems later. 13450SN/A */ 13460SN/A if (alg_k & SSL_kPSK) { 13470SN/A s->session->sess_cert = ssl_sess_cert_new(); 13480SN/A if (s->ctx->psk_identity_hint) 13490SN/A OPENSSL_free(s->ctx->psk_identity_hint); 13500SN/A s->ctx->psk_identity_hint = NULL; 13518241SN/A } 13520SN/A#endif 13530SN/A s->s3->tmp.reuse_message = 1; 13540SN/A return (1); 13550SN/A } 13560SN/A 13570SN/A param = p = (unsigned char *)s->init_msg; 13589167SN/A if (s->session->sess_cert != NULL) { 13590SN/A#ifndef OPENSSL_NO_RSA 13600SN/A if (s->session->sess_cert->peer_rsa_tmp != NULL) { 13610SN/A RSA_free(s->session->sess_cert->peer_rsa_tmp); 13620SN/A s->session->sess_cert->peer_rsa_tmp = NULL; 13630SN/A } 13640SN/A#endif 13650SN/A#ifndef OPENSSL_NO_DH 13660SN/A if (s->session->sess_cert->peer_dh_tmp) { 13670SN/A DH_free(s->session->sess_cert->peer_dh_tmp); 13680SN/A s->session->sess_cert->peer_dh_tmp = NULL; 13690SN/A } 13700SN/A#endif 13710SN/A#ifndef OPENSSL_NO_ECDH 13720SN/A if (s->session->sess_cert->peer_ecdh_tmp) { 13730SN/A EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp); 13740SN/A s->session->sess_cert->peer_ecdh_tmp = NULL; 13750SN/A } 13760SN/A#endif 13770SN/A } else { 13780SN/A s->session->sess_cert = ssl_sess_cert_new(); 13790SN/A } 13800SN/A 13810SN/A /* Total length of the parameters including the length prefix */ 13820SN/A param_len = 0; 13830SN/A 13840SN/A alg_a = s->s3->tmp.new_cipher->algorithm_auth; 13850SN/A 13860SN/A al = SSL_AD_DECODE_ERROR; 13878241SN/A 13880SN/A#ifndef OPENSSL_NO_PSK 13890SN/A if (alg_k & SSL_kPSK) { 13900SN/A param_len = 2; 13910SN/A if (param_len > n) { 13920SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 13930SN/A goto f_err; 13949167SN/A } 13950SN/A n2s(p, i); 13960SN/A 13970SN/A /* 13980SN/A * Store PSK identity hint for later use, hint is used in 13990SN/A * ssl3_send_client_key_exchange. Assume that the maximum length of 14000SN/A * a PSK identity hint can be as long as the maximum length of a PSK 14010SN/A * identity. 14020SN/A */ 14030SN/A if (i > PSK_MAX_IDENTITY_LEN) { 14040SN/A al = SSL_AD_HANDSHAKE_FAILURE; 14050SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG); 14060SN/A goto f_err; 14070SN/A } 14080SN/A if (i > n - param_len) { 14090SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, 14100SN/A SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH); 14110SN/A goto f_err; 14120SN/A } 14130SN/A param_len += i; 14140SN/A 14150SN/A s->session->psk_identity_hint = BUF_strndup((char *)p, i); 14160SN/A if (s->session->psk_identity_hint == NULL) { 14170SN/A al = SSL_AD_HANDSHAKE_FAILURE; 14180SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); 14190SN/A goto f_err; 14200SN/A } 14219660SN/A 14220SN/A p += i; 14230SN/A n -= param_len; 14240SN/A } else 14250SN/A#endif /* !OPENSSL_NO_PSK */ 14260SN/A#ifndef OPENSSL_NO_SRP 14270SN/A if (alg_k & SSL_kSRP) { 14280SN/A param_len = 2; 14290SN/A if (param_len > n) { 14300SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 14310SN/A goto f_err; 143216134Sprr } 14330SN/A n2s(p, i); 14340SN/A 14350SN/A if (i > n - param_len) { 14360SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH); 14370SN/A goto f_err; 14380SN/A } 14390SN/A param_len += i; 14400SN/A 14410SN/A if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) { 14420SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 14430SN/A goto err; 14440SN/A } 14450SN/A p += i; 14460SN/A 14470SN/A if (2 > n - param_len) { 14480SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 14490SN/A goto f_err; 14500SN/A } 14510SN/A param_len += 2; 14520SN/A 14530SN/A n2s(p, i); 14540SN/A 14550SN/A if (i > n - param_len) { 14560SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH); 14570SN/A goto f_err; 14580SN/A } 14590SN/A param_len += i; 14600SN/A 14610SN/A if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) { 14620SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 14630SN/A goto err; 14640SN/A } 14650SN/A p += i; 14660SN/A 14670SN/A if (1 > n - param_len) { 14680SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 14690SN/A goto f_err; 14700SN/A } 14710SN/A param_len += 1; 14720SN/A 14730SN/A i = (unsigned int)(p[0]); 14740SN/A p++; 14750SN/A 14760SN/A if (i > n - param_len) { 14770SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH); 14780SN/A goto f_err; 14790SN/A } 14800SN/A param_len += i; 14810SN/A 14820SN/A if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) { 14830SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 14840SN/A goto err; 14850SN/A } 14860SN/A p += i; 14872180SN/A 14882180SN/A if (2 > n - param_len) { 14892180SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 14902180SN/A goto f_err; 14912180SN/A } 14922180SN/A param_len += 2; 14932180SN/A 14942180SN/A n2s(p, i); 14952180SN/A 14962180SN/A if (i > n - param_len) { 14972180SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH); 14980SN/A goto f_err; 14990SN/A } 15000SN/A param_len += i; 15010SN/A 15020SN/A if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) { 15030SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 15040SN/A goto err; 15050SN/A } 15060SN/A p += i; 15070SN/A n -= param_len; 15080SN/A 15090SN/A if (!srp_verify_server_param(s, &al)) { 15100SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS); 15110SN/A goto f_err; 15120SN/A } 15130SN/A 15140SN/A/* We must check if there is a certificate */ 15150SN/A# ifndef OPENSSL_NO_RSA 15160SN/A if (alg_a & SSL_aRSA) 15170SN/A pkey = 15180SN/A X509_get_pubkey(s->session-> 15190SN/A sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); 15200SN/A# else 15210SN/A if (0) ; 15220SN/A# endif 15230SN/A# ifndef OPENSSL_NO_DSA 15240SN/A else if (alg_a & SSL_aDSS) 15250SN/A pkey = 15260SN/A X509_get_pubkey(s->session-> 15270SN/A sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN]. 15280SN/A x509); 15290SN/A# endif 15300SN/A } else 15310SN/A#endif /* !OPENSSL_NO_SRP */ 15320SN/A#ifndef OPENSSL_NO_RSA 15330SN/A if (alg_k & SSL_kRSA) { 15340SN/A /* Temporary RSA keys only allowed in export ciphersuites */ 15350SN/A if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) { 15360SN/A al = SSL_AD_UNEXPECTED_MESSAGE; 15370SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); 15380SN/A goto f_err; 15390SN/A } 15400SN/A if ((rsa = RSA_new()) == NULL) { 15410SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); 15420SN/A goto err; 15432180SN/A } 15440SN/A 15450SN/A param_len = 2; 154616134Sprr if (param_len > n) { 15472180SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 15482180SN/A goto f_err; 15492180SN/A } 15500SN/A n2s(p, i); 15510SN/A 15520SN/A if (i > n - param_len) { 15532180SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH); 15542180SN/A goto f_err; 15552180SN/A } 15562180SN/A param_len += i; 15572180SN/A 15582180SN/A if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) { 15590SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 15600SN/A goto err; 15612180SN/A } 15622180SN/A p += i; 15632180SN/A 15642180SN/A if (2 > n - param_len) { 15650SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 15660SN/A goto f_err; 15670SN/A } 15680SN/A param_len += 2; 15690SN/A 15700SN/A n2s(p, i); 15710SN/A 15720SN/A if (i > n - param_len) { 15730SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH); 15740SN/A goto f_err; 15750SN/A } 15760SN/A param_len += i; 15770SN/A 15780SN/A if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) { 15790SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 15800SN/A goto err; 15810SN/A } 15820SN/A p += i; 15839660SN/A n -= param_len; 15840SN/A 15850SN/A /* this should be because we are using an export cipher */ 15860SN/A if (alg_a & SSL_aRSA) 15870SN/A pkey = 15880SN/A X509_get_pubkey(s->session-> 15890SN/A sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); 15900SN/A else { 15910SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); 15920SN/A goto err; 15930SN/A } 159416134Sprr 15950SN/A if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { 15960SN/A al = SSL_AD_UNEXPECTED_MESSAGE; 15970SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); 15980SN/A goto f_err; 15990SN/A } 16000SN/A 16010SN/A s->session->sess_cert->peer_rsa_tmp = rsa; 16020SN/A rsa = NULL; 16030SN/A } 16040SN/A#else /* OPENSSL_NO_RSA */ 16050SN/A if (0) ; 16060SN/A#endif 16070SN/A#ifndef OPENSSL_NO_DH 16080SN/A else if (alg_k & SSL_kEDH) { 16090SN/A if ((dh = DH_new()) == NULL) { 16100SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB); 16110SN/A goto err; 16120SN/A } 16130SN/A 16140SN/A param_len = 2; 16150SN/A if (param_len > n) { 16160SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 16170SN/A goto f_err; 16180SN/A } 16190SN/A n2s(p, i); 16200SN/A 16210SN/A if (i > n - param_len) { 16220SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH); 16230SN/A goto f_err; 16240SN/A } 16250SN/A param_len += i; 16260SN/A 16270SN/A if (!(dh->p = BN_bin2bn(p, i, NULL))) { 16280SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 16290SN/A goto err; 16300SN/A } 16310SN/A p += i; 16320SN/A 16330SN/A if (BN_is_zero(dh->p)) { 16340SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_VALUE); 16350SN/A goto f_err; 16360SN/A } 16370SN/A 16380SN/A 16399660SN/A if (2 > n - param_len) { 16400SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 16410SN/A goto f_err; 16420SN/A } 16430SN/A param_len += 2; 16440SN/A 16450SN/A n2s(p, i); 16460SN/A 16470SN/A if (i > n - param_len) { 16480SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH); 16490SN/A goto f_err; 16500SN/A } 16510SN/A param_len += i; 16520SN/A 16530SN/A if (!(dh->g = BN_bin2bn(p, i, NULL))) { 16540SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 16550SN/A goto err; 16560SN/A } 16570SN/A p += i; 16580SN/A 16590SN/A if (BN_is_zero(dh->g)) { 16600SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_VALUE); 16610SN/A goto f_err; 16629660SN/A } 16630SN/A 16640SN/A if (2 > n - param_len) { 16650SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 16660SN/A goto f_err; 16670SN/A } 16680SN/A param_len += 2; 16690SN/A 16700SN/A n2s(p, i); 16710SN/A 16720SN/A if (i > n - param_len) { 16730SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH); 16740SN/A goto f_err; 16750SN/A } 16760SN/A param_len += i; 16770SN/A 167816134Sprr if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) { 16790SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 16800SN/A goto err; 16810SN/A } 16820SN/A p += i; 16830SN/A n -= param_len; 16840SN/A 16850SN/A if (BN_is_zero(dh->pub_key)) { 16860SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_VALUE); 16870SN/A goto f_err; 16880SN/A } 16890SN/A 16900SN/A# ifndef OPENSSL_NO_RSA 16910SN/A if (alg_a & SSL_aRSA) 16920SN/A pkey = 16930SN/A X509_get_pubkey(s->session-> 16940SN/A sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); 16950SN/A# else 16960SN/A if (0) ; 16970SN/A# endif 16980SN/A# ifndef OPENSSL_NO_DSA 16990SN/A else if (alg_a & SSL_aDSS) 17000SN/A pkey = 17010SN/A X509_get_pubkey(s->session-> 17020SN/A sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN]. 17030SN/A x509); 17040SN/A# endif 17050SN/A /* else anonymous DH, so no certificate or pkey. */ 17060SN/A 17070SN/A s->session->sess_cert->peer_dh_tmp = dh; 17080SN/A dh = NULL; 17090SN/A } else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd)) { 17100SN/A al = SSL_AD_ILLEGAL_PARAMETER; 17110SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, 17120SN/A SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER); 17130SN/A goto f_err; 17140SN/A } 17150SN/A#endif /* !OPENSSL_NO_DH */ 17160SN/A 17170SN/A#ifndef OPENSSL_NO_ECDH 17180SN/A else if (alg_k & SSL_kEECDH) { 17190SN/A EC_GROUP *ngroup; 17200SN/A const EC_GROUP *group; 17210SN/A 17220SN/A if ((ecdh = EC_KEY_new()) == NULL) { 17230SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); 17240SN/A goto err; 17250SN/A } 17260SN/A 17270SN/A /* 17280SN/A * Extract elliptic curve parameters and the server's ephemeral ECDH 17290SN/A * public key. Keep accumulating lengths of various components in 17300SN/A * param_len and make sure it never exceeds n. 17310SN/A */ 17320SN/A 17330SN/A /* 17340SN/A * XXX: For now we only support named (not generic) curves and the 17350SN/A * ECParameters in this case is just three bytes. We also need one 17360SN/A * byte for the length of the encoded point 17370SN/A */ 17380SN/A param_len = 4; 17390SN/A if (param_len > n) { 17400SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 17410SN/A goto f_err; 17420SN/A } 17430SN/A 17440SN/A if ((*p != NAMED_CURVE_TYPE) || 17450SN/A ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0)) { 17460SN/A al = SSL_AD_INTERNAL_ERROR; 17470SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, 17480SN/A SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); 17490SN/A goto f_err; 17500SN/A } 17519660SN/A 17520SN/A ngroup = EC_GROUP_new_by_curve_name(curve_nid); 17530SN/A if (ngroup == NULL) { 17540SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB); 17550SN/A goto err; 17560SN/A } 17570SN/A if (EC_KEY_set_group(ecdh, ngroup) == 0) { 17580SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB); 17590SN/A goto err; 17600SN/A } 17610SN/A EC_GROUP_free(ngroup); 17620SN/A 17630SN/A group = EC_KEY_get0_group(ecdh); 17640SN/A 17650SN/A if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && 17660SN/A (EC_GROUP_get_degree(group) > 163)) { 17670SN/A al = SSL_AD_EXPORT_RESTRICTION; 17680SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, 17690SN/A SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER); 17700SN/A goto f_err; 17710SN/A } 17720SN/A 17730SN/A p += 3; 17740SN/A 17750SN/A /* Next, get the encoded ECPoint */ 17760SN/A if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) || 17770SN/A ((bn_ctx = BN_CTX_new()) == NULL)) { 17780SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); 17790SN/A goto err; 17800SN/A } 17810SN/A 17820SN/A encoded_pt_len = *p; /* length of encoded point */ 17830SN/A p += 1; 17840SN/A 17850SN/A if ((encoded_pt_len > n - param_len) || 17860SN/A (EC_POINT_oct2point(group, srvr_ecpoint, 17870SN/A p, encoded_pt_len, bn_ctx) == 0)) { 17880SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT); 17890SN/A goto f_err; 17900SN/A } 17910SN/A param_len += encoded_pt_len; 17929660SN/A 17930SN/A n -= param_len; 17940SN/A p += encoded_pt_len; 17950SN/A 17960SN/A /* 17970SN/A * The ECC/TLS specification does not mention the use of DSA to sign 17980SN/A * ECParameters in the server key exchange message. We do support RSA 17990SN/A * and ECDSA. 18000SN/A */ 18010SN/A if (0) ; 18020SN/A# ifndef OPENSSL_NO_RSA 18030SN/A else if (alg_a & SSL_aRSA) 18040SN/A pkey = 18050SN/A X509_get_pubkey(s->session-> 18060SN/A sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); 18070SN/A# endif 18080SN/A# ifndef OPENSSL_NO_ECDSA 18090SN/A else if (alg_a & SSL_aECDSA) 18100SN/A pkey = 18110SN/A X509_get_pubkey(s->session-> 18120SN/A sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); 18130SN/A# endif 18140SN/A /* else anonymous ECDH, so no certificate or pkey. */ 18150SN/A EC_KEY_set_public_key(ecdh, srvr_ecpoint); 18160SN/A s->session->sess_cert->peer_ecdh_tmp = ecdh; 18170SN/A ecdh = NULL; 18180SN/A BN_CTX_free(bn_ctx); 18190SN/A bn_ctx = NULL; 18200SN/A EC_POINT_free(srvr_ecpoint); 18210SN/A srvr_ecpoint = NULL; 18220SN/A } else if (alg_k) { 18230SN/A al = SSL_AD_UNEXPECTED_MESSAGE; 18240SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); 18250SN/A goto f_err; 18260SN/A } 18270SN/A#endif /* !OPENSSL_NO_ECDH */ 18280SN/A 18290SN/A /* p points to the next byte, there are 'n' bytes left */ 18300SN/A 18310SN/A /* if it was signed, check the signature */ 18320SN/A if (pkey != NULL) { 18339660SN/A if (TLS1_get_version(s) >= TLS1_2_VERSION) { 18340SN/A int sigalg; 18350SN/A if (2 > n) { 18360SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 18370SN/A goto f_err; 18380SN/A } 18390SN/A 18400SN/A sigalg = tls12_get_sigid(pkey); 18410SN/A /* Should never happen */ 18420SN/A if (sigalg == -1) { 18430SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); 18440SN/A goto err; 18450SN/A } 18460SN/A /* Check key type is consistent with signature */ 18470SN/A if (sigalg != (int)p[1]) { 18480SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, 18490SN/A SSL_R_WRONG_SIGNATURE_TYPE); 18500SN/A al = SSL_AD_DECODE_ERROR; 18510SN/A goto f_err; 18520SN/A } 18530SN/A md = tls12_get_hash(p[0]); 18540SN/A if (md == NULL) { 18550SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNKNOWN_DIGEST); 18560SN/A goto f_err; 18570SN/A } 18580SN/A#ifdef SSL_DEBUG 18590SN/A fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); 18600SN/A#endif 18610SN/A p += 2; 18620SN/A n -= 2; 18630SN/A } else 18640SN/A md = EVP_sha1(); 18650SN/A 18660SN/A if (2 > n) { 18670SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 18680SN/A goto f_err; 18690SN/A } 18700SN/A n2s(p, i); 18710SN/A n -= 2; 18720SN/A j = EVP_PKEY_size(pkey); 18730SN/A 18740SN/A /* 18750SN/A * Check signature length. If n is 0 then signature is empty 18760SN/A */ 18770SN/A if ((i != n) || (n > j) || (n <= 0)) { 18780SN/A /* wrong packet length */ 18790SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH); 18800SN/A goto f_err; 18810SN/A } 18820SN/A#ifndef OPENSSL_NO_RSA 18830SN/A if (pkey->type == EVP_PKEY_RSA 18840SN/A && TLS1_get_version(s) < TLS1_2_VERSION) { 18850SN/A int num; 18860SN/A unsigned int size; 18870SN/A 18880SN/A j = 0; 18890SN/A q = md_buf; 18900SN/A for (num = 2; num > 0; num--) { 18910SN/A EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); 18920SN/A if (EVP_DigestInit_ex(&md_ctx, 18939660SN/A (num == 2) ? s->ctx->md5 : s->ctx->sha1, 18940SN/A NULL) <= 0 18950SN/A || EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]), 18960SN/A SSL3_RANDOM_SIZE) <= 0 18970SN/A || EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]), 18980SN/A SSL3_RANDOM_SIZE) <= 0 18990SN/A || EVP_DigestUpdate(&md_ctx, param, param_len) <= 0 19000SN/A || EVP_DigestFinal_ex(&md_ctx, q, &size) <= 0) { 19010SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, 19020SN/A ERR_R_INTERNAL_ERROR); 19030SN/A al = SSL_AD_INTERNAL_ERROR; 19040SN/A goto f_err; 19050SN/A } 19060SN/A q += size; 19070SN/A j += size; 19080SN/A } 19090SN/A i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa); 19100SN/A if (i < 0) { 19110SN/A al = SSL_AD_DECRYPT_ERROR; 19120SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT); 19130SN/A goto f_err; 19140SN/A } 19150SN/A if (i == 0) { 19160SN/A /* bad signature */ 19170SN/A al = SSL_AD_DECRYPT_ERROR; 19180SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE); 19190SN/A goto f_err; 19200SN/A } 19210SN/A } else 19220SN/A#endif 19230SN/A { 19240SN/A if (EVP_VerifyInit_ex(&md_ctx, md, NULL) <= 0 19250SN/A || EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]), 19260SN/A SSL3_RANDOM_SIZE) <= 0 19270SN/A || EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]), 19280SN/A SSL3_RANDOM_SIZE) <= 0 19290SN/A || EVP_VerifyUpdate(&md_ctx, param, param_len) <= 0) { 19300SN/A al = SSL_AD_INTERNAL_ERROR; 19310SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EVP_LIB); 19320SN/A goto f_err; 19330SN/A } 19340SN/A if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) { 19350SN/A /* bad signature */ 19360SN/A al = SSL_AD_DECRYPT_ERROR; 19370SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE); 19380SN/A goto f_err; 19390SN/A } 19400SN/A } 19410SN/A } else { 19420SN/A /* aNULL, aSRP or kPSK do not need public keys */ 19430SN/A if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) { 19440SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); 19450SN/A goto err; 19460SN/A } 19470SN/A /* still data left over */ 19480SN/A if (n != 0) { 19490SN/A SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE); 19500SN/A goto f_err; 19510SN/A } 19520SN/A } 19530SN/A EVP_PKEY_free(pkey); 19540SN/A EVP_MD_CTX_cleanup(&md_ctx); 19550SN/A return (1); 19560SN/A f_err: 19570SN/A ssl3_send_alert(s, SSL3_AL_FATAL, al); 19580SN/A err: 19599660SN/A EVP_PKEY_free(pkey); 19600SN/A#ifndef OPENSSL_NO_RSA 19610SN/A if (rsa != NULL) 19620SN/A RSA_free(rsa); 19630SN/A#endif 19640SN/A#ifndef OPENSSL_NO_DH 19650SN/A if (dh != NULL) 19660SN/A DH_free(dh); 19670SN/A#endif 19680SN/A#ifndef OPENSSL_NO_ECDH 19690SN/A BN_CTX_free(bn_ctx); 19700SN/A EC_POINT_free(srvr_ecpoint); 19710SN/A if (ecdh != NULL) 19720SN/A EC_KEY_free(ecdh); 19730SN/A#endif 19740SN/A EVP_MD_CTX_cleanup(&md_ctx); 19750SN/A s->state = SSL_ST_ERR; 19760SN/A return (-1); 19770SN/A} 19780SN/A 19790SN/Aint ssl3_get_certificate_request(SSL *s) 19800SN/A{ 19810SN/A int ok, ret = 0; 19820SN/A unsigned long n, nc, l; 19830SN/A unsigned int llen, ctype_num, i; 19840SN/A X509_NAME *xn = NULL; 19850SN/A const unsigned char *p, *q; 19860SN/A unsigned char *d; 19870SN/A STACK_OF(X509_NAME) *ca_sk = NULL; 19880SN/A 19890SN/A n = s->method->ssl_get_message(s, 19900SN/A SSL3_ST_CR_CERT_REQ_A, 19910SN/A SSL3_ST_CR_CERT_REQ_B, 19920SN/A -1, s->max_cert_list, &ok); 19930SN/A 19940SN/A if (!ok) 19950SN/A return ((int)n); 19960SN/A 19970SN/A s->s3->tmp.cert_req = 0; 19980SN/A 19990SN/A if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) { 20009660SN/A s->s3->tmp.reuse_message = 1; 20010SN/A /* 20020SN/A * If we get here we don't need any cached handshake records as we 20030SN/A * wont be doing client auth. 20040SN/A */ 20050SN/A if (s->s3->handshake_buffer) { 20060SN/A if (!ssl3_digest_cached_records(s)) 20070SN/A goto err; 20080SN/A } 20090SN/A return (1); 20100SN/A } 20110SN/A 20120SN/A if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) { 20130SN/A ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); 20140SN/A SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE); 20150SN/A goto err; 20160SN/A } 20170SN/A 20180SN/A /* TLS does not like anon-DH with client cert */ 20190SN/A if (s->version > SSL3_VERSION) { 20200SN/A if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) { 20210SN/A ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); 20220SN/A SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, 20230SN/A SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER); 20240SN/A goto err; 20250SN/A } 20260SN/A } 20270SN/A 20280SN/A p = d = (unsigned char *)s->init_msg; 20290SN/A 20300SN/A if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) { 20310SN/A SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); 20320SN/A goto err; 20330SN/A } 20340SN/A 20350SN/A /* get the certificate types */ 20360SN/A ctype_num = *(p++); 20370SN/A if (ctype_num > SSL3_CT_NUMBER) 20380SN/A ctype_num = SSL3_CT_NUMBER; 20390SN/A for (i = 0; i < ctype_num; i++) 20400SN/A s->s3->tmp.ctype[i] = p[i]; 20419660SN/A p += ctype_num; 20420SN/A if (TLS1_get_version(s) >= TLS1_2_VERSION) { 20430SN/A n2s(p, llen); 20440SN/A /* 20450SN/A * Check we have enough room for signature algorithms and following 20460SN/A * length value. 20470SN/A */ 20480SN/A if ((unsigned long)(p - d + llen + 2) > n) { 20490SN/A ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 20500SN/A SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, 20510SN/A SSL_R_DATA_LENGTH_TOO_LONG); 20520SN/A goto err; 20530SN/A } 20540SN/A if ((llen & 1) || !tls1_process_sigalgs(s, p, llen)) { 20550SN/A ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 20560SN/A SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, 20570SN/A SSL_R_SIGNATURE_ALGORITHMS_ERROR); 20580SN/A goto err; 20590SN/A } 20600SN/A p += llen; 20610SN/A } 20620SN/A 20630SN/A /* get the CA RDNs */ 20640SN/A n2s(p, llen); 20650SN/A#if 0 20660SN/A { 20670SN/A FILE *out; 20680SN/A out = fopen("/tmp/vsign.der", "w"); 20690SN/A fwrite(p, 1, llen, out); 20700SN/A fclose(out); 20710SN/A } 20720SN/A#endif 20730SN/A 20740SN/A if ((unsigned long)(p - d + llen) != n) { 20750SN/A ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 20760SN/A SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH); 20770SN/A goto err; 20780SN/A } 20799660SN/A 20800SN/A for (nc = 0; nc < llen;) { 20810SN/A if (nc + 2 > llen) { 20820SN/A ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 20830SN/A SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG); 20840SN/A goto err; 20850SN/A } 20860SN/A n2s(p, l); 20870SN/A if ((l + nc + 2) > llen) { 20880SN/A if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) 20890SN/A goto cont; /* netscape bugs */ 20900SN/A ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 20910SN/A SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG); 20920SN/A goto err; 20930SN/A } 20940SN/A 20950SN/A q = p; 20960SN/A 20970SN/A if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) { 20980SN/A /* If netscape tolerance is on, ignore errors */ 20990SN/A if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG) 21000SN/A goto cont; 21010SN/A else { 21020SN/A ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 21030SN/A SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB); 21040SN/A goto err; 21050SN/A } 21060SN/A } 21070SN/A 21080SN/A if (q != (p + l)) { 21090SN/A ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 21100SN/A SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, 21110SN/A SSL_R_CA_DN_LENGTH_MISMATCH); 21120SN/A goto err; 21130SN/A } 21140SN/A if (!sk_X509_NAME_push(ca_sk, xn)) { 21150SN/A SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); 21160SN/A goto err; 21179660SN/A } 21180SN/A xn = NULL; 21190SN/A 21200SN/A p += l; 21210SN/A nc += l + 2; 21220SN/A } 21230SN/A 21240SN/A if (0) { 21250SN/A cont: 21260SN/A ERR_clear_error(); 21270SN/A } 21280SN/A 21290SN/A /* we should setup a certificate to return.... */ 21300SN/A s->s3->tmp.cert_req = 1; 21310SN/A s->s3->tmp.ctype_num = ctype_num; 21320SN/A if (s->s3->tmp.ca_names != NULL) 21330SN/A sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free); 21340SN/A s->s3->tmp.ca_names = ca_sk; 21350SN/A ca_sk = NULL; 21360SN/A 21370SN/A ret = 1; 21380SN/A goto done; 21390SN/A err: 21400SN/A s->state = SSL_ST_ERR; 21410SN/A done: 21420SN/A X509_NAME_free(xn); 21430SN/A if (ca_sk != NULL) 21440SN/A sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); 21450SN/A return (ret); 21469660SN/A} 21470SN/A 21480SN/Astatic int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b) 21490SN/A{ 21500SN/A return (X509_NAME_cmp(*a, *b)); 21510SN/A} 21520SN/A 21530SN/A#ifndef OPENSSL_NO_TLSEXT 21540SN/Aint ssl3_get_new_session_ticket(SSL *s) 21550SN/A{ 21560SN/A int ok, al, ret = 0, ticklen; 21570SN/A long n; 21580SN/A const unsigned char *p; 21590SN/A unsigned char *d; 21600SN/A unsigned long ticket_lifetime_hint; 21610SN/A 21620SN/A n = s->method->ssl_get_message(s, 21630SN/A SSL3_ST_CR_SESSION_TICKET_A, 21640SN/A SSL3_ST_CR_SESSION_TICKET_B, 21650SN/A SSL3_MT_NEWSESSION_TICKET, 16384, &ok); 21660SN/A 21670SN/A if (!ok) 21680SN/A return ((int)n); 21690SN/A 21700SN/A if (n < 6) { 21710SN/A /* need at least ticket_lifetime_hint + ticket length */ 21720SN/A al = SSL_AD_DECODE_ERROR; 21730SN/A SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); 21740SN/A goto f_err; 21750SN/A } 21760SN/A 21779660SN/A p = d = (unsigned char *)s->init_msg; 21780SN/A 21790SN/A n2l(p, ticket_lifetime_hint); 21800SN/A n2s(p, ticklen); 21810SN/A /* ticket_lifetime_hint + ticket_length + ticket */ 21820SN/A if (ticklen + 6 != n) { 21830SN/A al = SSL_AD_DECODE_ERROR; 21840SN/A SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); 21850SN/A goto f_err; 21860SN/A } 21870SN/A 21880SN/A /* Server is allowed to change its mind and send an empty ticket. */ 21890SN/A if (ticklen == 0) 21900SN/A return 1; 21910SN/A 21920SN/A if (s->session->session_id_length > 0) { 21930SN/A int i = s->session_ctx->session_cache_mode; 21940SN/A SSL_SESSION *new_sess; 21950SN/A /* 21960SN/A * We reused an existing session, so we need to replace it with a new 21970SN/A * one 21980SN/A */ 21990SN/A if (i & SSL_SESS_CACHE_CLIENT) { 22000SN/A /* 22010SN/A * Remove the old session from the cache 22020SN/A */ 22030SN/A if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) { 22049660SN/A if (s->session_ctx->remove_session_cb != NULL) 22050SN/A s->session_ctx->remove_session_cb(s->session_ctx, 22060SN/A s->session); 22070SN/A } else { 22080SN/A /* We carry on if this fails */ 22090SN/A SSL_CTX_remove_session(s->session_ctx, s->session); 22100SN/A } 22110SN/A } 22120SN/A 22130SN/A if ((new_sess = ssl_session_dup(s->session, 0)) == 0) { 22140SN/A al = SSL_AD_INTERNAL_ERROR; 22150SN/A SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE); 22160SN/A goto f_err; 22170SN/A } 22180SN/A 22190SN/A SSL_SESSION_free(s->session); 22200SN/A s->session = new_sess; 22210SN/A } 22220SN/A 22230SN/A if (s->session->tlsext_tick) { 22240SN/A OPENSSL_free(s->session->tlsext_tick); 22250SN/A s->session->tlsext_ticklen = 0; 22260SN/A } 22270SN/A s->session->tlsext_tick = OPENSSL_malloc(ticklen); 22280SN/A if (!s->session->tlsext_tick) { 22290SN/A SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE); 22300SN/A goto err; 22319660SN/A } 22320SN/A memcpy(s->session->tlsext_tick, p, ticklen); 22330SN/A s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint; 22340SN/A s->session->tlsext_ticklen = ticklen; 22350SN/A /* 22360SN/A * There are two ways to detect a resumed ticket session. One is to set 22370SN/A * an appropriate session ID and then the server must return a match in 22380SN/A * ServerHello. This allows the normal client session ID matching to work 22390SN/A * and we know much earlier that the ticket has been accepted. The 22400SN/A * other way is to set zero length session ID when the ticket is 22410SN/A * presented and rely on the handshake to determine session resumption. 22420SN/A * We choose the former approach because this fits in with assumptions 22430SN/A * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is 22440SN/A * SHA256 is disabled) hash of the ticket. 22450SN/A */ 22460SN/A EVP_Digest(p, ticklen, 22470SN/A s->session->session_id, &s->session->session_id_length, 22480SN/A# ifndef OPENSSL_NO_SHA256 22490SN/A EVP_sha256(), NULL); 22500SN/A# else 22510SN/A EVP_sha1(), NULL); 22520SN/A# endif 22530SN/A ret = 1; 22540SN/A return (ret); 22550SN/A f_err: 22560SN/A ssl3_send_alert(s, SSL3_AL_FATAL, al); 22570SN/A err: 22589660SN/A s->state = SSL_ST_ERR; 22590SN/A return (-1); 22600SN/A} 22610SN/A 22620SN/Aint ssl3_get_cert_status(SSL *s) 22630SN/A{ 22640SN/A int ok, al; 22650SN/A unsigned long resplen, n; 22660SN/A const unsigned char *p; 22670SN/A 22680SN/A n = s->method->ssl_get_message(s, 22690SN/A SSL3_ST_CR_CERT_STATUS_A, 22700SN/A SSL3_ST_CR_CERT_STATUS_B, 22710SN/A -1, 16384, &ok); 22720SN/A 22730SN/A if (!ok) 22740SN/A return ((int)n); 22750SN/A 22760SN/A if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) { 22770SN/A /* 22780SN/A * The CertificateStatus message is optional even if 22790SN/A * tlsext_status_expected is set 22800SN/A */ 22810SN/A s->s3->tmp.reuse_message = 1; 22820SN/A } else { 22830SN/A if (n < 4) { 22840SN/A /* need at least status type + length */ 22859660SN/A al = SSL_AD_DECODE_ERROR; 22860SN/A SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH); 22870SN/A goto f_err; 22880SN/A } 22890SN/A p = (unsigned char *)s->init_msg; 22900SN/A if (*p++ != TLSEXT_STATUSTYPE_ocsp) { 22910SN/A al = SSL_AD_DECODE_ERROR; 22920SN/A SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE); 22930SN/A goto f_err; 22940SN/A } 22950SN/A n2l3(p, resplen); 22960SN/A if (resplen + 4 != n) { 22970SN/A al = SSL_AD_DECODE_ERROR; 22980SN/A SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH); 22990SN/A goto f_err; 23000SN/A } 23010SN/A s->tlsext_ocsp_resp = BUF_memdup(p, resplen); 23020SN/A if (s->tlsext_ocsp_resp == NULL) { 23030SN/A al = SSL_AD_INTERNAL_ERROR; 23040SN/A SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE); 23050SN/A goto f_err; 23060SN/A } 23070SN/A s->tlsext_ocsp_resplen = resplen; 23080SN/A } 23090SN/A if (s->ctx->tlsext_status_cb) { 23109660SN/A int ret; 23110SN/A ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); 23120SN/A if (ret == 0) { 23130SN/A al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; 23140SN/A SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE); 23150SN/A goto f_err; 23160SN/A } 23170SN/A if (ret < 0) { 23180SN/A al = SSL_AD_INTERNAL_ERROR; 23190SN/A SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE); 23200SN/A goto f_err; 23210SN/A } 23220SN/A } 23230SN/A return 1; 23240SN/A f_err: 23250SN/A ssl3_send_alert(s, SSL3_AL_FATAL, al); 23260SN/A s->state = SSL_ST_ERR; 23270SN/A return (-1); 23280SN/A} 23290SN/A#endif 23300SN/A 23310SN/Aint ssl3_get_server_done(SSL *s) 23320SN/A{ 23330SN/A int ok, ret = 0; 23340SN/A long n; 23350SN/A 23360SN/A /* Second to last param should be very small, like 0 :-) */ 2337 n = s->method->ssl_get_message(s, 2338 SSL3_ST_CR_SRVR_DONE_A, 2339 SSL3_ST_CR_SRVR_DONE_B, 2340 SSL3_MT_SERVER_DONE, 30, &ok); 2341 2342 if (!ok) 2343 return ((int)n); 2344 if (n > 0) { 2345 /* should contain no data */ 2346 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 2347 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH); 2348 s->state = SSL_ST_ERR; 2349 return -1; 2350 } 2351 ret = 1; 2352 return (ret); 2353} 2354 2355int ssl3_send_client_key_exchange(SSL *s) 2356{ 2357 unsigned char *p, *d; 2358 int n; 2359 unsigned long alg_k; 2360#ifndef OPENSSL_NO_RSA 2361 unsigned char *q; 2362 EVP_PKEY *pkey = NULL; 2363#endif 2364#ifndef OPENSSL_NO_KRB5 2365 KSSL_ERR kssl_err; 2366#endif /* OPENSSL_NO_KRB5 */ 2367#ifndef OPENSSL_NO_ECDH 2368 EC_KEY *clnt_ecdh = NULL; 2369 const EC_POINT *srvr_ecpoint = NULL; 2370 EVP_PKEY *srvr_pub_pkey = NULL; 2371 unsigned char *encodedPoint = NULL; 2372 int encoded_pt_len = 0; 2373 BN_CTX *bn_ctx = NULL; 2374#endif 2375 2376 if (s->state == SSL3_ST_CW_KEY_EXCH_A) { 2377 d = (unsigned char *)s->init_buf->data; 2378 p = &(d[4]); 2379 2380 alg_k = s->s3->tmp.new_cipher->algorithm_mkey; 2381 2382 /* Fool emacs indentation */ 2383 if (0) { 2384 } 2385#ifndef OPENSSL_NO_RSA 2386 else if (alg_k & SSL_kRSA) { 2387 RSA *rsa; 2388 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; 2389 2390 if (s->session->sess_cert == NULL) { 2391 /* 2392 * We should always have a server certificate with SSL_kRSA. 2393 */ 2394 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2395 ERR_R_INTERNAL_ERROR); 2396 goto err; 2397 } 2398 2399 if (s->session->sess_cert->peer_rsa_tmp != NULL) 2400 rsa = s->session->sess_cert->peer_rsa_tmp; 2401 else { 2402 pkey = 2403 X509_get_pubkey(s->session-> 2404 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC]. 2405 x509); 2406 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA) 2407 || (pkey->pkey.rsa == NULL)) { 2408 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2409 ERR_R_INTERNAL_ERROR); 2410 EVP_PKEY_free(pkey); 2411 goto err; 2412 } 2413 rsa = pkey->pkey.rsa; 2414 EVP_PKEY_free(pkey); 2415 } 2416 2417 tmp_buf[0] = s->client_version >> 8; 2418 tmp_buf[1] = s->client_version & 0xff; 2419 if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0) 2420 goto err; 2421 2422 s->session->master_key_length = sizeof tmp_buf; 2423 2424 q = p; 2425 /* Fix buf for TLS and beyond */ 2426 if (s->version > SSL3_VERSION) 2427 p += 2; 2428 n = RSA_public_encrypt(sizeof tmp_buf, 2429 tmp_buf, p, rsa, RSA_PKCS1_PADDING); 2430# ifdef PKCS1_CHECK 2431 if (s->options & SSL_OP_PKCS1_CHECK_1) 2432 p[1]++; 2433 if (s->options & SSL_OP_PKCS1_CHECK_2) 2434 tmp_buf[0] = 0x70; 2435# endif 2436 if (n <= 0) { 2437 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2438 SSL_R_BAD_RSA_ENCRYPT); 2439 goto err; 2440 } 2441 2442 /* Fix buf for TLS and beyond */ 2443 if (s->version > SSL3_VERSION) { 2444 s2n(n, q); 2445 n += 2; 2446 } 2447 2448 s->session->master_key_length = 2449 s->method->ssl3_enc->generate_master_secret(s, 2450 s-> 2451 session->master_key, 2452 tmp_buf, 2453 sizeof tmp_buf); 2454 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf); 2455 } 2456#endif 2457#ifndef OPENSSL_NO_KRB5 2458 else if (alg_k & SSL_kKRB5) { 2459 krb5_error_code krb5rc; 2460 KSSL_CTX *kssl_ctx = s->kssl_ctx; 2461 /* krb5_data krb5_ap_req; */ 2462 krb5_data *enc_ticket; 2463 krb5_data authenticator, *authp = NULL; 2464 EVP_CIPHER_CTX ciph_ctx; 2465 const EVP_CIPHER *enc = NULL; 2466 unsigned char iv[EVP_MAX_IV_LENGTH]; 2467 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; 2468 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH]; 2469 int padl, outl = sizeof(epms); 2470 2471 EVP_CIPHER_CTX_init(&ciph_ctx); 2472 2473# ifdef KSSL_DEBUG 2474 fprintf(stderr, "ssl3_send_client_key_exchange(%lx & %lx)\n", 2475 alg_k, SSL_kKRB5); 2476# endif /* KSSL_DEBUG */ 2477 2478 authp = NULL; 2479# ifdef KRB5SENDAUTH 2480 if (KRB5SENDAUTH) 2481 authp = &authenticator; 2482# endif /* KRB5SENDAUTH */ 2483 2484 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err); 2485 enc = kssl_map_enc(kssl_ctx->enctype); 2486 if (enc == NULL) 2487 goto err; 2488# ifdef KSSL_DEBUG 2489 { 2490 fprintf(stderr, "kssl_cget_tkt rtn %d\n", krb5rc); 2491 if (krb5rc && kssl_err.text) 2492 fprintf(stderr, "kssl_cget_tkt kssl_err=%s\n", 2493 kssl_err.text); 2494 } 2495# endif /* KSSL_DEBUG */ 2496 2497 if (krb5rc) { 2498 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 2499 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason); 2500 goto err; 2501 } 2502 2503 /*- 2504 * 20010406 VRS - Earlier versions used KRB5 AP_REQ 2505 * in place of RFC 2712 KerberosWrapper, as in: 2506 * 2507 * Send ticket (copy to *p, set n = length) 2508 * n = krb5_ap_req.length; 2509 * memcpy(p, krb5_ap_req.data, krb5_ap_req.length); 2510 * if (krb5_ap_req.data) 2511 * kssl_krb5_free_data_contents(NULL,&krb5_ap_req); 2512 * 2513 * Now using real RFC 2712 KerberosWrapper 2514 * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>) 2515 * Note: 2712 "opaque" types are here replaced 2516 * with a 2-byte length followed by the value. 2517 * Example: 2518 * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms 2519 * Where "xx xx" = length bytes. Shown here with 2520 * optional authenticator omitted. 2521 */ 2522 2523 /* KerberosWrapper.Ticket */ 2524 s2n(enc_ticket->length, p); 2525 memcpy(p, enc_ticket->data, enc_ticket->length); 2526 p += enc_ticket->length; 2527 n = enc_ticket->length + 2; 2528 2529 /* KerberosWrapper.Authenticator */ 2530 if (authp && authp->length) { 2531 s2n(authp->length, p); 2532 memcpy(p, authp->data, authp->length); 2533 p += authp->length; 2534 n += authp->length + 2; 2535 2536 free(authp->data); 2537 authp->data = NULL; 2538 authp->length = 0; 2539 } else { 2540 s2n(0, p); /* null authenticator length */ 2541 n += 2; 2542 } 2543 2544 tmp_buf[0] = s->client_version >> 8; 2545 tmp_buf[1] = s->client_version & 0xff; 2546 if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0) 2547 goto err; 2548 2549 /*- 2550 * 20010420 VRS. Tried it this way; failed. 2551 * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL); 2552 * EVP_CIPHER_CTX_set_key_length(&ciph_ctx, 2553 * kssl_ctx->length); 2554 * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv); 2555 */ 2556 2557 memset(iv, 0, sizeof iv); /* per RFC 1510 */ 2558 EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv); 2559 EVP_EncryptUpdate(&ciph_ctx, epms, &outl, tmp_buf, 2560 sizeof tmp_buf); 2561 EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl); 2562 outl += padl; 2563 if (outl > (int)sizeof epms) { 2564 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2565 ERR_R_INTERNAL_ERROR); 2566 goto err; 2567 } 2568 EVP_CIPHER_CTX_cleanup(&ciph_ctx); 2569 2570 /* KerberosWrapper.EncryptedPreMasterSecret */ 2571 s2n(outl, p); 2572 memcpy(p, epms, outl); 2573 p += outl; 2574 n += outl + 2; 2575 2576 s->session->master_key_length = 2577 s->method->ssl3_enc->generate_master_secret(s, 2578 s-> 2579 session->master_key, 2580 tmp_buf, 2581 sizeof tmp_buf); 2582 2583 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf); 2584 OPENSSL_cleanse(epms, outl); 2585 } 2586#endif 2587#ifndef OPENSSL_NO_DH 2588 else if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) { 2589 DH *dh_srvr, *dh_clnt; 2590 2591 if (s->session->sess_cert == NULL) { 2592 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); 2593 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2594 SSL_R_UNEXPECTED_MESSAGE); 2595 goto err; 2596 } 2597 2598 if (s->session->sess_cert->peer_dh_tmp != NULL) 2599 dh_srvr = s->session->sess_cert->peer_dh_tmp; 2600 else { 2601 /* we get them from the cert */ 2602 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 2603 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2604 SSL_R_UNABLE_TO_FIND_DH_PARAMETERS); 2605 goto err; 2606 } 2607 2608 /* generate a new random key */ 2609 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) { 2610 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); 2611 goto err; 2612 } 2613 if (!DH_generate_key(dh_clnt)) { 2614 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); 2615 DH_free(dh_clnt); 2616 goto err; 2617 } 2618 2619 /* 2620 * use the 'p' output buffer for the DH key, but make sure to 2621 * clear it out afterwards 2622 */ 2623 2624 n = DH_compute_key(p, dh_srvr->pub_key, dh_clnt); 2625 2626 if (n <= 0) { 2627 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); 2628 DH_free(dh_clnt); 2629 goto err; 2630 } 2631 2632 /* generate master key from the result */ 2633 s->session->master_key_length = 2634 s->method->ssl3_enc->generate_master_secret(s, 2635 s-> 2636 session->master_key, 2637 p, n); 2638 /* clean up */ 2639 memset(p, 0, n); 2640 2641 /* send off the data */ 2642 n = BN_num_bytes(dh_clnt->pub_key); 2643 s2n(n, p); 2644 BN_bn2bin(dh_clnt->pub_key, p); 2645 n += 2; 2646 2647 DH_free(dh_clnt); 2648 } 2649#endif 2650 2651#ifndef OPENSSL_NO_ECDH 2652 else if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) { 2653 const EC_GROUP *srvr_group = NULL; 2654 EC_KEY *tkey; 2655 int ecdh_clnt_cert = 0; 2656 int field_size = 0; 2657 2658 if (s->session->sess_cert == NULL) { 2659 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); 2660 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2661 SSL_R_UNEXPECTED_MESSAGE); 2662 goto err; 2663 } 2664 2665 /* 2666 * Did we send out the client's ECDH share for use in premaster 2667 * computation as part of client certificate? If so, set 2668 * ecdh_clnt_cert to 1. 2669 */ 2670 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) { 2671 /*- 2672 * XXX: For now, we do not support client 2673 * authentication using ECDH certificates. 2674 * To add such support, one needs to add 2675 * code that checks for appropriate 2676 * conditions and sets ecdh_clnt_cert to 1. 2677 * For example, the cert have an ECC 2678 * key on the same curve as the server's 2679 * and the key should be authorized for 2680 * key agreement. 2681 * 2682 * One also needs to add code in ssl3_connect 2683 * to skip sending the certificate verify 2684 * message. 2685 * 2686 * if ((s->cert->key->privatekey != NULL) && 2687 * (s->cert->key->privatekey->type == 2688 * EVP_PKEY_EC) && ...) 2689 * ecdh_clnt_cert = 1; 2690 */ 2691 } 2692 2693 if (s->session->sess_cert->peer_ecdh_tmp != NULL) { 2694 tkey = s->session->sess_cert->peer_ecdh_tmp; 2695 } else { 2696 /* Get the Server Public Key from Cert */ 2697 srvr_pub_pkey = 2698 X509_get_pubkey(s->session-> 2699 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); 2700 if ((srvr_pub_pkey == NULL) 2701 || (srvr_pub_pkey->type != EVP_PKEY_EC) 2702 || (srvr_pub_pkey->pkey.ec == NULL)) { 2703 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2704 ERR_R_INTERNAL_ERROR); 2705 goto err; 2706 } 2707 2708 tkey = srvr_pub_pkey->pkey.ec; 2709 } 2710 2711 srvr_group = EC_KEY_get0_group(tkey); 2712 srvr_ecpoint = EC_KEY_get0_public_key(tkey); 2713 2714 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) { 2715 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2716 ERR_R_INTERNAL_ERROR); 2717 goto err; 2718 } 2719 2720 if ((clnt_ecdh = EC_KEY_new()) == NULL) { 2721 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2722 ERR_R_MALLOC_FAILURE); 2723 goto err; 2724 } 2725 2726 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) { 2727 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB); 2728 goto err; 2729 } 2730 if (ecdh_clnt_cert) { 2731 /* 2732 * Reuse key info from our certificate We only need our 2733 * private key to perform the ECDH computation. 2734 */ 2735 const BIGNUM *priv_key; 2736 tkey = s->cert->key->privatekey->pkey.ec; 2737 priv_key = EC_KEY_get0_private_key(tkey); 2738 if (priv_key == NULL) { 2739 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2740 ERR_R_MALLOC_FAILURE); 2741 goto err; 2742 } 2743 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) { 2744 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB); 2745 goto err; 2746 } 2747 } else { 2748 /* Generate a new ECDH key pair */ 2749 if (!(EC_KEY_generate_key(clnt_ecdh))) { 2750 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2751 ERR_R_ECDH_LIB); 2752 goto err; 2753 } 2754 } 2755 2756 /* 2757 * use the 'p' output buffer for the ECDH key, but make sure to 2758 * clear it out afterwards 2759 */ 2760 2761 field_size = EC_GROUP_get_degree(srvr_group); 2762 if (field_size <= 0) { 2763 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); 2764 goto err; 2765 } 2766 n = ECDH_compute_key(p, (field_size + 7) / 8, srvr_ecpoint, 2767 clnt_ecdh, NULL); 2768 if (n <= 0) { 2769 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); 2770 goto err; 2771 } 2772 2773 /* generate master key from the result */ 2774 s->session->master_key_length = 2775 s->method->ssl3_enc->generate_master_secret(s, 2776 s-> 2777 session->master_key, 2778 p, n); 2779 2780 memset(p, 0, n); /* clean up */ 2781 2782 if (ecdh_clnt_cert) { 2783 /* Send empty client key exch message */ 2784 n = 0; 2785 } else { 2786 /* 2787 * First check the size of encoding and allocate memory 2788 * accordingly. 2789 */ 2790 encoded_pt_len = 2791 EC_POINT_point2oct(srvr_group, 2792 EC_KEY_get0_public_key(clnt_ecdh), 2793 POINT_CONVERSION_UNCOMPRESSED, 2794 NULL, 0, NULL); 2795 2796 encodedPoint = (unsigned char *) 2797 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char)); 2798 bn_ctx = BN_CTX_new(); 2799 if ((encodedPoint == NULL) || (bn_ctx == NULL)) { 2800 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2801 ERR_R_MALLOC_FAILURE); 2802 goto err; 2803 } 2804 2805 /* Encode the public key */ 2806 n = EC_POINT_point2oct(srvr_group, 2807 EC_KEY_get0_public_key(clnt_ecdh), 2808 POINT_CONVERSION_UNCOMPRESSED, 2809 encodedPoint, encoded_pt_len, bn_ctx); 2810 2811 *p = n; /* length of encoded point */ 2812 /* Encoded point will be copied here */ 2813 p += 1; 2814 /* copy the point */ 2815 memcpy((unsigned char *)p, encodedPoint, n); 2816 /* increment n to account for length field */ 2817 n += 1; 2818 } 2819 2820 /* Free allocated memory */ 2821 BN_CTX_free(bn_ctx); 2822 if (encodedPoint != NULL) 2823 OPENSSL_free(encodedPoint); 2824 if (clnt_ecdh != NULL) 2825 EC_KEY_free(clnt_ecdh); 2826 EVP_PKEY_free(srvr_pub_pkey); 2827 } 2828#endif /* !OPENSSL_NO_ECDH */ 2829 else if (alg_k & SSL_kGOST) { 2830 /* GOST key exchange message creation */ 2831 EVP_PKEY_CTX *pkey_ctx; 2832 X509 *peer_cert; 2833 size_t msglen; 2834 unsigned int md_len; 2835 int keytype; 2836 unsigned char premaster_secret[32], shared_ukm[32], tmp[256]; 2837 EVP_MD_CTX *ukm_hash; 2838 EVP_PKEY *pub_key; 2839 2840 /* 2841 * Get server sertificate PKEY and create ctx from it 2842 */ 2843 peer_cert = 2844 s->session-> 2845 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509; 2846 if (!peer_cert) 2847 peer_cert = 2848 s->session-> 2849 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509; 2850 if (!peer_cert) { 2851 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2852 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); 2853 goto err; 2854 } 2855 2856 pkey_ctx = EVP_PKEY_CTX_new(pub_key = 2857 X509_get_pubkey(peer_cert), NULL); 2858 if (pkey_ctx == NULL) { 2859 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2860 ERR_R_MALLOC_FAILURE); 2861 goto err; 2862 } 2863 /* 2864 * If we have send a certificate, and certificate key 2865 * 2866 * * parameters match those of server certificate, use 2867 * certificate key for key exchange 2868 */ 2869 2870 /* Otherwise, generate ephemeral key pair */ 2871 2872 if (pkey_ctx == NULL 2873 || EVP_PKEY_encrypt_init(pkey_ctx) <= 0 2874 /* Generate session key */ 2875 || RAND_bytes(premaster_secret, 32) <= 0) { 2876 EVP_PKEY_CTX_free(pkey_ctx); 2877 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2878 ERR_R_INTERNAL_ERROR); 2879 goto err; 2880 } 2881 /* 2882 * Compute shared IV and store it in algorithm-specific context 2883 * data 2884 */ 2885 ukm_hash = EVP_MD_CTX_create(); 2886 if (EVP_DigestInit(ukm_hash, 2887 EVP_get_digestbynid(NID_id_GostR3411_94)) <= 0 2888 || EVP_DigestUpdate(ukm_hash, s->s3->client_random, 2889 SSL3_RANDOM_SIZE) <= 0 2890 || EVP_DigestUpdate(ukm_hash, s->s3->server_random, 2891 SSL3_RANDOM_SIZE) <= 0 2892 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) { 2893 EVP_MD_CTX_destroy(ukm_hash); 2894 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2895 ERR_R_INTERNAL_ERROR); 2896 goto err; 2897 } 2898 EVP_MD_CTX_destroy(ukm_hash); 2899 if (EVP_PKEY_CTX_ctrl 2900 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8, 2901 shared_ukm) < 0) { 2902 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2903 SSL_R_LIBRARY_BUG); 2904 goto err; 2905 } 2906 /* Make GOST keytransport blob message */ 2907 /* 2908 * Encapsulate it into sequence 2909 */ 2910 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED; 2911 msglen = 255; 2912 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, premaster_secret, 32) 2913 <= 0) { 2914 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2915 SSL_R_LIBRARY_BUG); 2916 goto err; 2917 } 2918 if (msglen >= 0x80) { 2919 *(p++) = 0x81; 2920 *(p++) = msglen & 0xff; 2921 n = msglen + 3; 2922 } else { 2923 *(p++) = msglen & 0xff; 2924 n = msglen + 2; 2925 } 2926 memcpy(p, tmp, msglen); 2927 EVP_PKEY_CTX_free(pkey_ctx); 2928 s->session->master_key_length = 2929 s->method->ssl3_enc->generate_master_secret(s, 2930 s-> 2931 session->master_key, 2932 premaster_secret, 2933 32); 2934 EVP_PKEY_free(pub_key); 2935 2936 } 2937#ifndef OPENSSL_NO_SRP 2938 else if (alg_k & SSL_kSRP) { 2939 if (s->srp_ctx.A != NULL) { 2940 /* send off the data */ 2941 n = BN_num_bytes(s->srp_ctx.A); 2942 s2n(n, p); 2943 BN_bn2bin(s->srp_ctx.A, p); 2944 n += 2; 2945 } else { 2946 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2947 ERR_R_INTERNAL_ERROR); 2948 goto err; 2949 } 2950 if (s->session->srp_username != NULL) 2951 OPENSSL_free(s->session->srp_username); 2952 s->session->srp_username = BUF_strdup(s->srp_ctx.login); 2953 if (s->session->srp_username == NULL) { 2954 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2955 ERR_R_MALLOC_FAILURE); 2956 goto err; 2957 } 2958 2959 if ((s->session->master_key_length = 2960 SRP_generate_client_master_secret(s, 2961 s->session->master_key)) < 2962 0) { 2963 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2964 ERR_R_INTERNAL_ERROR); 2965 goto err; 2966 } 2967 } 2968#endif 2969#ifndef OPENSSL_NO_PSK 2970 else if (alg_k & SSL_kPSK) { 2971 /* 2972 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a 2973 * \0-terminated identity. The last byte is for us for simulating 2974 * strnlen. 2975 */ 2976 char identity[PSK_MAX_IDENTITY_LEN + 2]; 2977 size_t identity_len; 2978 unsigned char *t = NULL; 2979 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4]; 2980 unsigned int pre_ms_len = 0, psk_len = 0; 2981 int psk_err = 1; 2982 2983 n = 0; 2984 if (s->psk_client_callback == NULL) { 2985 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2986 SSL_R_PSK_NO_CLIENT_CB); 2987 goto err; 2988 } 2989 2990 memset(identity, 0, sizeof(identity)); 2991 psk_len = s->psk_client_callback(s, s->session->psk_identity_hint, 2992 identity, sizeof(identity) - 1, 2993 psk_or_pre_ms, 2994 sizeof(psk_or_pre_ms)); 2995 if (psk_len > PSK_MAX_PSK_LEN) { 2996 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2997 ERR_R_INTERNAL_ERROR); 2998 goto psk_err; 2999 } else if (psk_len == 0) { 3000 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 3001 SSL_R_PSK_IDENTITY_NOT_FOUND); 3002 goto psk_err; 3003 } 3004 identity[PSK_MAX_IDENTITY_LEN + 1] = '\0'; 3005 identity_len = strlen(identity); 3006 if (identity_len > PSK_MAX_IDENTITY_LEN) { 3007 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 3008 ERR_R_INTERNAL_ERROR); 3009 goto psk_err; 3010 } 3011 /* create PSK pre_master_secret */ 3012 pre_ms_len = 2 + psk_len + 2 + psk_len; 3013 t = psk_or_pre_ms; 3014 memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len); 3015 s2n(psk_len, t); 3016 memset(t, 0, psk_len); 3017 t += psk_len; 3018 s2n(psk_len, t); 3019 3020 if (s->session->psk_identity_hint != NULL) 3021 OPENSSL_free(s->session->psk_identity_hint); 3022 s->session->psk_identity_hint = 3023 BUF_strdup(s->ctx->psk_identity_hint); 3024 if (s->ctx->psk_identity_hint != NULL 3025 && s->session->psk_identity_hint == NULL) { 3026 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 3027 ERR_R_MALLOC_FAILURE); 3028 goto psk_err; 3029 } 3030 3031 if (s->session->psk_identity != NULL) 3032 OPENSSL_free(s->session->psk_identity); 3033 s->session->psk_identity = BUF_strdup(identity); 3034 if (s->session->psk_identity == NULL) { 3035 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 3036 ERR_R_MALLOC_FAILURE); 3037 goto psk_err; 3038 } 3039 3040 s->session->master_key_length = 3041 s->method->ssl3_enc->generate_master_secret(s, 3042 s-> 3043 session->master_key, 3044 psk_or_pre_ms, 3045 pre_ms_len); 3046 s2n(identity_len, p); 3047 memcpy(p, identity, identity_len); 3048 n = 2 + identity_len; 3049 psk_err = 0; 3050 psk_err: 3051 OPENSSL_cleanse(identity, sizeof(identity)); 3052 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms)); 3053 if (psk_err != 0) { 3054 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 3055 goto err; 3056 } 3057 } 3058#endif 3059 else { 3060 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 3061 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); 3062 goto err; 3063 } 3064 3065 *(d++) = SSL3_MT_CLIENT_KEY_EXCHANGE; 3066 l2n3(n, d); 3067 3068 s->state = SSL3_ST_CW_KEY_EXCH_B; 3069 /* number of bytes to write */ 3070 s->init_num = n + 4; 3071 s->init_off = 0; 3072 } 3073 3074 /* SSL3_ST_CW_KEY_EXCH_B */ 3075 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE)); 3076 err: 3077#ifndef OPENSSL_NO_ECDH 3078 BN_CTX_free(bn_ctx); 3079 if (encodedPoint != NULL) 3080 OPENSSL_free(encodedPoint); 3081 if (clnt_ecdh != NULL) 3082 EC_KEY_free(clnt_ecdh); 3083 EVP_PKEY_free(srvr_pub_pkey); 3084#endif 3085 s->state = SSL_ST_ERR; 3086 return (-1); 3087} 3088 3089int ssl3_send_client_verify(SSL *s) 3090{ 3091 unsigned char *p, *d; 3092 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH]; 3093 EVP_PKEY *pkey; 3094 EVP_PKEY_CTX *pctx = NULL; 3095 EVP_MD_CTX mctx; 3096 unsigned u = 0; 3097 unsigned long n; 3098 int j; 3099 3100 EVP_MD_CTX_init(&mctx); 3101 3102 if (s->state == SSL3_ST_CW_CERT_VRFY_A) { 3103 d = (unsigned char *)s->init_buf->data; 3104 p = &(d[4]); 3105 pkey = s->cert->key->privatekey; 3106/* Create context from key and test if sha1 is allowed as digest */ 3107 pctx = EVP_PKEY_CTX_new(pkey, NULL); 3108 if (pctx == NULL || EVP_PKEY_sign_init(pctx) <= 0) { 3109 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); 3110 goto err; 3111 } 3112 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) { 3113 if (TLS1_get_version(s) < TLS1_2_VERSION) 3114 s->method->ssl3_enc->cert_verify_mac(s, 3115 NID_sha1, 3116 &(data 3117 [MD5_DIGEST_LENGTH])); 3118 } else { 3119 ERR_clear_error(); 3120 } 3121 /* 3122 * For TLS v1.2 send signature algorithm and signature using agreed 3123 * digest and cached handshake records. 3124 */ 3125 if (TLS1_get_version(s) >= TLS1_2_VERSION) { 3126 long hdatalen = 0; 3127 void *hdata; 3128 const EVP_MD *md = s->cert->key->digest; 3129 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); 3130 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) { 3131 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); 3132 goto err; 3133 } 3134 p += 2; 3135#ifdef SSL_DEBUG 3136 fprintf(stderr, "Using TLS 1.2 with client alg %s\n", 3137 EVP_MD_name(md)); 3138#endif 3139 if (!EVP_SignInit_ex(&mctx, md, NULL) 3140 || !EVP_SignUpdate(&mctx, hdata, hdatalen) 3141 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) { 3142 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB); 3143 goto err; 3144 } 3145 s2n(u, p); 3146 n = u + 4; 3147 if (!ssl3_digest_cached_records(s)) 3148 goto err; 3149 } else 3150#ifndef OPENSSL_NO_RSA 3151 if (pkey->type == EVP_PKEY_RSA) { 3152 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0])); 3153 if (RSA_sign(NID_md5_sha1, data, 3154 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, 3155 &(p[2]), &u, pkey->pkey.rsa) <= 0) { 3156 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB); 3157 goto err; 3158 } 3159 s2n(u, p); 3160 n = u + 2; 3161 } else 3162#endif 3163#ifndef OPENSSL_NO_DSA 3164 if (pkey->type == EVP_PKEY_DSA) { 3165 if (!DSA_sign(pkey->save_type, 3166 &(data[MD5_DIGEST_LENGTH]), 3167 SHA_DIGEST_LENGTH, &(p[2]), 3168 (unsigned int *)&j, pkey->pkey.dsa)) { 3169 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB); 3170 goto err; 3171 } 3172 s2n(j, p); 3173 n = j + 2; 3174 } else 3175#endif 3176#ifndef OPENSSL_NO_ECDSA 3177 if (pkey->type == EVP_PKEY_EC) { 3178 if (!ECDSA_sign(pkey->save_type, 3179 &(data[MD5_DIGEST_LENGTH]), 3180 SHA_DIGEST_LENGTH, &(p[2]), 3181 (unsigned int *)&j, pkey->pkey.ec)) { 3182 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB); 3183 goto err; 3184 } 3185 s2n(j, p); 3186 n = j + 2; 3187 } else 3188#endif 3189 if (pkey->type == NID_id_GostR3410_94 3190 || pkey->type == NID_id_GostR3410_2001) { 3191 unsigned char signbuf[64]; 3192 int i; 3193 size_t sigsize = 64; 3194 s->method->ssl3_enc->cert_verify_mac(s, 3195 NID_id_GostR3411_94, data); 3196 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) { 3197 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); 3198 goto err; 3199 } 3200 for (i = 63, j = 0; i >= 0; j++, i--) { 3201 p[2 + j] = signbuf[i]; 3202 } 3203 s2n(j, p); 3204 n = j + 2; 3205 } else { 3206 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); 3207 goto err; 3208 } 3209 *(d++) = SSL3_MT_CERTIFICATE_VERIFY; 3210 l2n3(n, d); 3211 3212 s->state = SSL3_ST_CW_CERT_VRFY_B; 3213 s->init_num = (int)n + 4; 3214 s->init_off = 0; 3215 } 3216 EVP_MD_CTX_cleanup(&mctx); 3217 EVP_PKEY_CTX_free(pctx); 3218 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE)); 3219 err: 3220 EVP_MD_CTX_cleanup(&mctx); 3221 EVP_PKEY_CTX_free(pctx); 3222 s->state = SSL_ST_ERR; 3223 return (-1); 3224} 3225 3226int ssl3_send_client_certificate(SSL *s) 3227{ 3228 X509 *x509 = NULL; 3229 EVP_PKEY *pkey = NULL; 3230 int i; 3231 unsigned long l; 3232 3233 if (s->state == SSL3_ST_CW_CERT_A) { 3234 if ((s->cert == NULL) || 3235 (s->cert->key->x509 == NULL) || 3236 (s->cert->key->privatekey == NULL)) 3237 s->state = SSL3_ST_CW_CERT_B; 3238 else 3239 s->state = SSL3_ST_CW_CERT_C; 3240 } 3241 3242 /* We need to get a client cert */ 3243 if (s->state == SSL3_ST_CW_CERT_B) { 3244 /* 3245 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP; 3246 * return(-1); We then get retied later 3247 */ 3248 i = ssl_do_client_cert_cb(s, &x509, &pkey); 3249 if (i < 0) { 3250 s->rwstate = SSL_X509_LOOKUP; 3251 return (-1); 3252 } 3253 s->rwstate = SSL_NOTHING; 3254 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) { 3255 s->state = SSL3_ST_CW_CERT_B; 3256 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) 3257 i = 0; 3258 } else if (i == 1) { 3259 i = 0; 3260 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, 3261 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); 3262 } 3263 3264 if (x509 != NULL) 3265 X509_free(x509); 3266 if (pkey != NULL) 3267 EVP_PKEY_free(pkey); 3268 if (i == 0) { 3269 if (s->version == SSL3_VERSION) { 3270 s->s3->tmp.cert_req = 0; 3271 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE); 3272 return (1); 3273 } else { 3274 s->s3->tmp.cert_req = 2; 3275 } 3276 } 3277 3278 /* Ok, we have a cert */ 3279 s->state = SSL3_ST_CW_CERT_C; 3280 } 3281 3282 if (s->state == SSL3_ST_CW_CERT_C) { 3283 s->state = SSL3_ST_CW_CERT_D; 3284 l = ssl3_output_cert_chain(s, 3285 (s->s3->tmp.cert_req == 3286 2) ? NULL : s->cert->key->x509); 3287 if (!l) { 3288 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR); 3289 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); 3290 s->state = SSL_ST_ERR; 3291 return 0; 3292 } 3293 s->init_num = (int)l; 3294 s->init_off = 0; 3295 } 3296 /* SSL3_ST_CW_CERT_D */ 3297 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE)); 3298} 3299 3300#define has_bits(i,m) (((i)&(m)) == (m)) 3301 3302int ssl3_check_cert_and_algorithm(SSL *s) 3303{ 3304 int i, idx; 3305 long alg_k, alg_a; 3306 EVP_PKEY *pkey = NULL; 3307 int pkey_bits; 3308 SESS_CERT *sc; 3309#ifndef OPENSSL_NO_RSA 3310 RSA *rsa; 3311#endif 3312#ifndef OPENSSL_NO_DH 3313 DH *dh; 3314#endif 3315 int al = SSL_AD_HANDSHAKE_FAILURE; 3316 3317 alg_k = s->s3->tmp.new_cipher->algorithm_mkey; 3318 alg_a = s->s3->tmp.new_cipher->algorithm_auth; 3319 3320 /* we don't have a certificate */ 3321 if ((alg_a & (SSL_aDH | SSL_aNULL | SSL_aKRB5)) || (alg_k & SSL_kPSK)) 3322 return (1); 3323 3324 sc = s->session->sess_cert; 3325 if (sc == NULL) { 3326 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR); 3327 goto err; 3328 } 3329#ifndef OPENSSL_NO_RSA 3330 rsa = s->session->sess_cert->peer_rsa_tmp; 3331#endif 3332#ifndef OPENSSL_NO_DH 3333 dh = s->session->sess_cert->peer_dh_tmp; 3334#endif 3335 3336 /* This is the passed certificate */ 3337 3338 idx = sc->peer_cert_type; 3339#ifndef OPENSSL_NO_ECDH 3340 if (idx == SSL_PKEY_ECC) { 3341 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) { 3342 /* check failed */ 3343 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT); 3344 goto f_err; 3345 } else { 3346 return 1; 3347 } 3348 } 3349#endif 3350 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509); 3351 pkey_bits = EVP_PKEY_bits(pkey); 3352 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey); 3353 EVP_PKEY_free(pkey); 3354 3355 /* Check that we have a certificate if we require one */ 3356 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) { 3357 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3358 SSL_R_MISSING_RSA_SIGNING_CERT); 3359 goto f_err; 3360 } 3361#ifndef OPENSSL_NO_DSA 3362 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) { 3363 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3364 SSL_R_MISSING_DSA_SIGNING_CERT); 3365 goto f_err; 3366 } 3367#endif 3368#ifndef OPENSSL_NO_RSA 3369 if (alg_k & SSL_kRSA) { 3370 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && 3371 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) { 3372 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3373 SSL_R_MISSING_RSA_ENCRYPTING_CERT); 3374 goto f_err; 3375 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) { 3376 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { 3377 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) { 3378 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3379 SSL_R_MISSING_RSA_ENCRYPTING_CERT); 3380 goto f_err; 3381 } 3382 if (rsa != NULL) { 3383 /* server key exchange is not allowed. */ 3384 al = SSL_AD_INTERNAL_ERROR; 3385 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR); 3386 goto f_err; 3387 } 3388 } 3389 } 3390 } 3391#endif 3392#ifndef OPENSSL_NO_DH 3393 if ((alg_k & SSL_kEDH) && dh == NULL) { 3394 al = SSL_AD_INTERNAL_ERROR; 3395 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR); 3396 goto f_err; 3397 } 3398 if ((alg_k & SSL_kDHr) && !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) { 3399 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3400 SSL_R_MISSING_DH_RSA_CERT); 3401 goto f_err; 3402 } 3403# ifndef OPENSSL_NO_DSA 3404 if ((alg_k & SSL_kDHd) && !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) { 3405 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3406 SSL_R_MISSING_DH_DSA_CERT); 3407 goto f_err; 3408 } 3409# endif 3410 3411 /* Check DHE only: static DH not implemented. */ 3412 if (alg_k & SSL_kEDH) { 3413 int dh_size = BN_num_bits(dh->p); 3414 if ((!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 1024) 3415 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 512)) { 3416 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_DH_KEY_TOO_SMALL); 3417 goto f_err; 3418 } 3419 } 3420#endif /* !OPENSSL_NO_DH */ 3421 3422 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && 3423 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { 3424#ifndef OPENSSL_NO_RSA 3425 if (alg_k & SSL_kRSA) { 3426 if (rsa == NULL) { 3427 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3428 SSL_R_MISSING_EXPORT_TMP_RSA_KEY); 3429 goto f_err; 3430 } else if (BN_num_bits(rsa->n) > 3431 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { 3432 /* We have a temporary RSA key but it's too large. */ 3433 al = SSL_AD_EXPORT_RESTRICTION; 3434 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3435 SSL_R_MISSING_EXPORT_TMP_RSA_KEY); 3436 goto f_err; 3437 } 3438 } else 3439#endif 3440#ifndef OPENSSL_NO_DH 3441 if (alg_k & SSL_kEDH) { 3442 if (BN_num_bits(dh->p) > 3443 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { 3444 /* We have a temporary DH key but it's too large. */ 3445 al = SSL_AD_EXPORT_RESTRICTION; 3446 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3447 SSL_R_MISSING_EXPORT_TMP_DH_KEY); 3448 goto f_err; 3449 } 3450 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) { 3451 /* The cert should have had an export DH key. */ 3452 al = SSL_AD_EXPORT_RESTRICTION; 3453 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3454 SSL_R_MISSING_EXPORT_TMP_DH_KEY); 3455 goto f_err; 3456 } else 3457#endif 3458 { 3459 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3460 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); 3461 goto f_err; 3462 } 3463 } 3464 return (1); 3465 f_err: 3466 ssl3_send_alert(s, SSL3_AL_FATAL, al); 3467 err: 3468 return (0); 3469} 3470 3471#ifndef OPENSSL_NO_TLSEXT 3472/* 3473 * Normally, we can tell if the server is resuming the session from 3474 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server 3475 * message after the ServerHello to determine if the server is resuming. 3476 * Therefore, we allow EAP-FAST to peek ahead. 3477 * ssl3_check_finished returns 1 if we are resuming from an external 3478 * pre-shared secret, we have a "ticket" and the next server handshake message 3479 * is Finished; and 0 otherwise. It returns -1 upon an error. 3480 */ 3481static int ssl3_check_finished(SSL *s) 3482{ 3483 int ok = 0; 3484 3485 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb || 3486 !s->session->tlsext_tick) 3487 return 0; 3488 3489 /* Need to permit this temporarily, in case the next message is Finished. */ 3490 s->s3->flags |= SSL3_FLAGS_CCS_OK; 3491 /* 3492 * This function is called when we might get a Certificate message instead, 3493 * so permit appropriate message length. 3494 * We ignore the return value as we're only interested in the message type 3495 * and not its length. 3496 */ 3497 s->method->ssl_get_message(s, 3498 SSL3_ST_CR_CERT_A, 3499 SSL3_ST_CR_CERT_B, 3500 -1, s->max_cert_list, &ok); 3501 s->s3->flags &= ~SSL3_FLAGS_CCS_OK; 3502 3503 if (!ok) 3504 return -1; 3505 3506 s->s3->tmp.reuse_message = 1; 3507 3508 if (s->s3->tmp.message_type == SSL3_MT_FINISHED) 3509 return 1; 3510 3511 /* If we're not done, then the CCS arrived early and we should bail. */ 3512 if (s->s3->change_cipher_spec) { 3513 SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY); 3514 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); 3515 return -1; 3516 } 3517 3518 return 0; 3519} 3520 3521# ifndef OPENSSL_NO_NEXTPROTONEG 3522int ssl3_send_next_proto(SSL *s) 3523{ 3524 unsigned int len, padding_len; 3525 unsigned char *d; 3526 3527 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) { 3528 len = s->next_proto_negotiated_len; 3529 padding_len = 32 - ((len + 2) % 32); 3530 d = (unsigned char *)s->init_buf->data; 3531 d[4] = len; 3532 memcpy(d + 5, s->next_proto_negotiated, len); 3533 d[5 + len] = padding_len; 3534 memset(d + 6 + len, 0, padding_len); 3535 *(d++) = SSL3_MT_NEXT_PROTO; 3536 l2n3(2 + len + padding_len, d); 3537 s->state = SSL3_ST_CW_NEXT_PROTO_B; 3538 s->init_num = 4 + 2 + len + padding_len; 3539 s->init_off = 0; 3540 } 3541 3542 return ssl3_do_write(s, SSL3_RT_HANDSHAKE); 3543} 3544#endif /* !OPENSSL_NO_NEXTPROTONEG */ 3545#endif /* !OPENSSL_NO_TLSEXT */ 3546 3547int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) 3548{ 3549 int i = 0; 3550#ifndef OPENSSL_NO_ENGINE 3551 if (s->ctx->client_cert_engine) { 3552 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s, 3553 SSL_get_client_CA_list(s), 3554 px509, ppkey, NULL, NULL, NULL); 3555 if (i != 0) 3556 return i; 3557 } 3558#endif 3559 if (s->ctx->client_cert_cb) 3560 i = s->ctx->client_cert_cb(s, px509, ppkey); 3561 return i; 3562} 3563