xref: /onnv-gate/usr/src/common/openssl/ssl/s3_srvr.c (revision 8545:a1b3fd5884fd)
10Sstevel@tonic-gate /* ssl/s3_srvr.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate /* ====================================================================
592139Sjp161948  * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
620Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
630Sstevel@tonic-gate  * are met:
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
660Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
690Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
700Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
710Sstevel@tonic-gate  *    distribution.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
740Sstevel@tonic-gate  *    software must display the following acknowledgment:
750Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
760Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
790Sstevel@tonic-gate  *    endorse or promote products derived from this software without
800Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
810Sstevel@tonic-gate  *    openssl-core@openssl.org.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
840Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
850Sstevel@tonic-gate  *    permission of the OpenSSL Project.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
880Sstevel@tonic-gate  *    acknowledgment:
890Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
900Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
930Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
940Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
950Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
960Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
970Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
980Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
990Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1000Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1010Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1020Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1030Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
1040Sstevel@tonic-gate  * ====================================================================
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
1070Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
1080Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  */
1112139Sjp161948 /* ====================================================================
1122139Sjp161948  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
1132139Sjp161948  *
1142139Sjp161948  * Portions of the attached software ("Contribution") are developed by
1152139Sjp161948  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
1162139Sjp161948  *
1172139Sjp161948  * The Contribution is licensed pursuant to the OpenSSL open source
1182139Sjp161948  * license provided above.
1192139Sjp161948  *
1202139Sjp161948  * ECC cipher suite support in OpenSSL originally written by
1212139Sjp161948  * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
1222139Sjp161948  *
1232139Sjp161948  */
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate #define REUSE_CIPHER_BUG
1260Sstevel@tonic-gate #define NETSCAPE_HANG_BUG
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate #include <stdio.h>
1290Sstevel@tonic-gate #include "ssl_locl.h"
1300Sstevel@tonic-gate #include "kssl_lcl.h"
1310Sstevel@tonic-gate #include <openssl/buffer.h>
1320Sstevel@tonic-gate #include <openssl/rand.h>
1330Sstevel@tonic-gate #include <openssl/objects.h>
1340Sstevel@tonic-gate #include <openssl/evp.h>
1350Sstevel@tonic-gate #include <openssl/x509.h>
1362139Sjp161948 #ifndef OPENSSL_NO_DH
1372139Sjp161948 #include <openssl/dh.h>
1382139Sjp161948 #endif
1392139Sjp161948 #include <openssl/bn.h>
1400Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5
1410Sstevel@tonic-gate #include <openssl/krb5_asn.h>
1420Sstevel@tonic-gate #endif
1430Sstevel@tonic-gate #include <openssl/md5.h>
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate static SSL_METHOD *ssl3_get_server_method(int ver);
1462139Sjp161948 
1472139Sjp161948 #ifndef OPENSSL_NO_ECDH
1482139Sjp161948 static int nid2curve_id(int nid);
1492139Sjp161948 #endif
1500Sstevel@tonic-gate 
ssl3_get_server_method(int ver)1510Sstevel@tonic-gate static SSL_METHOD *ssl3_get_server_method(int ver)
1520Sstevel@tonic-gate 	{
1530Sstevel@tonic-gate 	if (ver == SSL3_VERSION)
1540Sstevel@tonic-gate 		return(SSLv3_server_method());
1550Sstevel@tonic-gate 	else
1560Sstevel@tonic-gate 		return(NULL);
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 
IMPLEMENT_ssl3_meth_func(SSLv3_server_method,ssl3_accept,ssl_undefined_function,ssl3_get_server_method)1592139Sjp161948 IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
1602139Sjp161948 			ssl3_accept,
1612139Sjp161948 			ssl_undefined_function,
1622139Sjp161948 			ssl3_get_server_method)
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate int ssl3_accept(SSL *s)
1650Sstevel@tonic-gate 	{
1660Sstevel@tonic-gate 	BUF_MEM *buf;
1670Sstevel@tonic-gate 	unsigned long l,Time=time(NULL);
1680Sstevel@tonic-gate 	void (*cb)(const SSL *ssl,int type,int val)=NULL;
1690Sstevel@tonic-gate 	long num1;
1700Sstevel@tonic-gate 	int ret= -1;
1710Sstevel@tonic-gate 	int new_state,state,skip=0;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	RAND_add(&Time,sizeof(Time),0);
1740Sstevel@tonic-gate 	ERR_clear_error();
1750Sstevel@tonic-gate 	clear_sys_error();
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (s->info_callback != NULL)
1780Sstevel@tonic-gate 		cb=s->info_callback;
1790Sstevel@tonic-gate 	else if (s->ctx->info_callback != NULL)
1800Sstevel@tonic-gate 		cb=s->ctx->info_callback;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	/* init things to blank */
1830Sstevel@tonic-gate 	s->in_handshake++;
1840Sstevel@tonic-gate 	if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (s->cert == NULL)
1870Sstevel@tonic-gate 		{
1880Sstevel@tonic-gate 		SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
1890Sstevel@tonic-gate 		return(-1);
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	for (;;)
1930Sstevel@tonic-gate 		{
1940Sstevel@tonic-gate 		state=s->state;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 		switch (s->state)
1970Sstevel@tonic-gate 			{
1980Sstevel@tonic-gate 		case SSL_ST_RENEGOTIATE:
1990Sstevel@tonic-gate 			s->new_session=1;
2000Sstevel@tonic-gate 			/* s->state=SSL_ST_ACCEPT; */
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 		case SSL_ST_BEFORE:
2030Sstevel@tonic-gate 		case SSL_ST_ACCEPT:
2040Sstevel@tonic-gate 		case SSL_ST_BEFORE|SSL_ST_ACCEPT:
2050Sstevel@tonic-gate 		case SSL_ST_OK|SSL_ST_ACCEPT:
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 			s->server=1;
2080Sstevel@tonic-gate 			if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 			if ((s->version>>8) != 3)
2110Sstevel@tonic-gate 				{
2120Sstevel@tonic-gate 				SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
2130Sstevel@tonic-gate 				return -1;
2140Sstevel@tonic-gate 				}
2150Sstevel@tonic-gate 			s->type=SSL_ST_ACCEPT;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 			if (s->init_buf == NULL)
2180Sstevel@tonic-gate 				{
2190Sstevel@tonic-gate 				if ((buf=BUF_MEM_new()) == NULL)
2200Sstevel@tonic-gate 					{
2210Sstevel@tonic-gate 					ret= -1;
2220Sstevel@tonic-gate 					goto end;
2230Sstevel@tonic-gate 					}
2240Sstevel@tonic-gate 				if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
2250Sstevel@tonic-gate 					{
2260Sstevel@tonic-gate 					ret= -1;
2270Sstevel@tonic-gate 					goto end;
2280Sstevel@tonic-gate 					}
2290Sstevel@tonic-gate 				s->init_buf=buf;
2300Sstevel@tonic-gate 				}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 			if (!ssl3_setup_buffers(s))
2330Sstevel@tonic-gate 				{
2340Sstevel@tonic-gate 				ret= -1;
2350Sstevel@tonic-gate 				goto end;
2360Sstevel@tonic-gate 				}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 			s->init_num=0;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 			if (s->state != SSL_ST_RENEGOTIATE)
2410Sstevel@tonic-gate 				{
2420Sstevel@tonic-gate 				/* Ok, we now need to push on a buffering BIO so that
2430Sstevel@tonic-gate 				 * the output is sent in a way that TCP likes :-)
2440Sstevel@tonic-gate 				 */
2450Sstevel@tonic-gate 				if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 				ssl3_init_finished_mac(s);
2480Sstevel@tonic-gate 				s->state=SSL3_ST_SR_CLNT_HELLO_A;
2490Sstevel@tonic-gate 				s->ctx->stats.sess_accept++;
2500Sstevel@tonic-gate 				}
2510Sstevel@tonic-gate 			else
2520Sstevel@tonic-gate 				{
2530Sstevel@tonic-gate 				/* s->state == SSL_ST_RENEGOTIATE,
2540Sstevel@tonic-gate 				 * we will just send a HelloRequest */
2550Sstevel@tonic-gate 				s->ctx->stats.sess_accept_renegotiate++;
2560Sstevel@tonic-gate 				s->state=SSL3_ST_SW_HELLO_REQ_A;
2570Sstevel@tonic-gate 				}
2580Sstevel@tonic-gate 			break;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 		case SSL3_ST_SW_HELLO_REQ_A:
2610Sstevel@tonic-gate 		case SSL3_ST_SW_HELLO_REQ_B:
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 			s->shutdown=0;
2640Sstevel@tonic-gate 			ret=ssl3_send_hello_request(s);
2650Sstevel@tonic-gate 			if (ret <= 0) goto end;
2660Sstevel@tonic-gate 			s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
2670Sstevel@tonic-gate 			s->state=SSL3_ST_SW_FLUSH;
2680Sstevel@tonic-gate 			s->init_num=0;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 			ssl3_init_finished_mac(s);
2710Sstevel@tonic-gate 			break;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 		case SSL3_ST_SW_HELLO_REQ_C:
2740Sstevel@tonic-gate 			s->state=SSL_ST_OK;
2750Sstevel@tonic-gate 			break;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		case SSL3_ST_SR_CLNT_HELLO_A:
2780Sstevel@tonic-gate 		case SSL3_ST_SR_CLNT_HELLO_B:
2790Sstevel@tonic-gate 		case SSL3_ST_SR_CLNT_HELLO_C:
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 			s->shutdown=0;
2820Sstevel@tonic-gate 			ret=ssl3_get_client_hello(s);
2830Sstevel@tonic-gate 			if (ret <= 0) goto end;
2840Sstevel@tonic-gate 			s->new_session = 2;
2850Sstevel@tonic-gate 			s->state=SSL3_ST_SW_SRVR_HELLO_A;
2860Sstevel@tonic-gate 			s->init_num=0;
2870Sstevel@tonic-gate 			break;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 		case SSL3_ST_SW_SRVR_HELLO_A:
2900Sstevel@tonic-gate 		case SSL3_ST_SW_SRVR_HELLO_B:
2910Sstevel@tonic-gate 			ret=ssl3_send_server_hello(s);
2920Sstevel@tonic-gate 			if (ret <= 0) goto end;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 			if (s->hit)
2950Sstevel@tonic-gate 				s->state=SSL3_ST_SW_CHANGE_A;
2960Sstevel@tonic-gate 			else
2970Sstevel@tonic-gate 				s->state=SSL3_ST_SW_CERT_A;
2980Sstevel@tonic-gate 			s->init_num=0;
2990Sstevel@tonic-gate 			break;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 		case SSL3_ST_SW_CERT_A:
3020Sstevel@tonic-gate 		case SSL3_ST_SW_CERT_B:
3032139Sjp161948 			/* Check if it is anon DH or anon ECDH */
3040Sstevel@tonic-gate 			if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
3050Sstevel@tonic-gate 				{
3060Sstevel@tonic-gate 				ret=ssl3_send_server_certificate(s);
3070Sstevel@tonic-gate 				if (ret <= 0) goto end;
3080Sstevel@tonic-gate 				}
3090Sstevel@tonic-gate 			else
3100Sstevel@tonic-gate 				skip=1;
3110Sstevel@tonic-gate 			s->state=SSL3_ST_SW_KEY_EXCH_A;
3120Sstevel@tonic-gate 			s->init_num=0;
3130Sstevel@tonic-gate 			break;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		case SSL3_ST_SW_KEY_EXCH_A:
3160Sstevel@tonic-gate 		case SSL3_ST_SW_KEY_EXCH_B:
3170Sstevel@tonic-gate 			l=s->s3->tmp.new_cipher->algorithms;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 			/* clear this, it may get reset by
3200Sstevel@tonic-gate 			 * send_server_key_exchange */
3210Sstevel@tonic-gate 			if ((s->options & SSL_OP_EPHEMERAL_RSA)
3220Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5
3230Sstevel@tonic-gate 				&& !(l & SSL_KRB5)
3240Sstevel@tonic-gate #endif /* OPENSSL_NO_KRB5 */
3250Sstevel@tonic-gate 				)
3260Sstevel@tonic-gate 				/* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
3270Sstevel@tonic-gate 				 * even when forbidden by protocol specs
3280Sstevel@tonic-gate 				 * (handshake may fail as clients are not required to
3290Sstevel@tonic-gate 				 * be able to handle this) */
3300Sstevel@tonic-gate 				s->s3->tmp.use_rsa_tmp=1;
3310Sstevel@tonic-gate 			else
3320Sstevel@tonic-gate 				s->s3->tmp.use_rsa_tmp=0;
3330Sstevel@tonic-gate 
3342139Sjp161948 
3350Sstevel@tonic-gate 			/* only send if a DH key exchange, fortezza or
3362139Sjp161948 			 * RSA but we have a sign only certificate
3372139Sjp161948 			 *
3382139Sjp161948 			 * For ECC ciphersuites, we send a serverKeyExchange
3392139Sjp161948 			 * message only if the cipher suite is either
3402139Sjp161948 			 * ECDH-anon or ECDHE. In other cases, the
3412139Sjp161948 			 * server certificate contains the server's
3422139Sjp161948 			 * public key for key exchange.
3432139Sjp161948 			 */
3440Sstevel@tonic-gate 			if (s->s3->tmp.use_rsa_tmp
3452139Sjp161948 			    || (l & SSL_kECDHE)
3460Sstevel@tonic-gate 			    || (l & (SSL_DH|SSL_kFZA))
3470Sstevel@tonic-gate 			    || ((l & SSL_kRSA)
3480Sstevel@tonic-gate 				&& (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
3490Sstevel@tonic-gate 				    || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
3500Sstevel@tonic-gate 					&& EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
3510Sstevel@tonic-gate 					)
3520Sstevel@tonic-gate 				    )
3530Sstevel@tonic-gate 				)
3540Sstevel@tonic-gate 			    )
3550Sstevel@tonic-gate 				{
3560Sstevel@tonic-gate 				ret=ssl3_send_server_key_exchange(s);
3570Sstevel@tonic-gate 				if (ret <= 0) goto end;
3580Sstevel@tonic-gate 				}
3590Sstevel@tonic-gate 			else
3600Sstevel@tonic-gate 				skip=1;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 			s->state=SSL3_ST_SW_CERT_REQ_A;
3630Sstevel@tonic-gate 			s->init_num=0;
3640Sstevel@tonic-gate 			break;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 		case SSL3_ST_SW_CERT_REQ_A:
3670Sstevel@tonic-gate 		case SSL3_ST_SW_CERT_REQ_B:
3680Sstevel@tonic-gate 			if (/* don't request cert unless asked for it: */
3690Sstevel@tonic-gate 				!(s->verify_mode & SSL_VERIFY_PEER) ||
3700Sstevel@tonic-gate 				/* if SSL_VERIFY_CLIENT_ONCE is set,
3710Sstevel@tonic-gate 				 * don't request cert during re-negotiation: */
3720Sstevel@tonic-gate 				((s->session->peer != NULL) &&
3730Sstevel@tonic-gate 				 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
3740Sstevel@tonic-gate 				/* never request cert in anonymous ciphersuites
3750Sstevel@tonic-gate 				 * (see section "Certificate request" in SSL 3 drafts
3760Sstevel@tonic-gate 				 * and in RFC 2246): */
3770Sstevel@tonic-gate 				((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
3780Sstevel@tonic-gate 				 /* ... except when the application insists on verification
3790Sstevel@tonic-gate 				  * (against the specs, but s3_clnt.c accepts this for SSL 3) */
3800Sstevel@tonic-gate 				 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
3810Sstevel@tonic-gate                                  /* never request cert in Kerberos ciphersuites */
3820Sstevel@tonic-gate                                 (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))
3830Sstevel@tonic-gate 				{
3840Sstevel@tonic-gate 				/* no cert request */
3850Sstevel@tonic-gate 				skip=1;
3860Sstevel@tonic-gate 				s->s3->tmp.cert_request=0;
3870Sstevel@tonic-gate 				s->state=SSL3_ST_SW_SRVR_DONE_A;
3880Sstevel@tonic-gate 				}
3890Sstevel@tonic-gate 			else
3900Sstevel@tonic-gate 				{
3910Sstevel@tonic-gate 				s->s3->tmp.cert_request=1;
3920Sstevel@tonic-gate 				ret=ssl3_send_certificate_request(s);
3930Sstevel@tonic-gate 				if (ret <= 0) goto end;
3940Sstevel@tonic-gate #ifndef NETSCAPE_HANG_BUG
3950Sstevel@tonic-gate 				s->state=SSL3_ST_SW_SRVR_DONE_A;
3960Sstevel@tonic-gate #else
3970Sstevel@tonic-gate 				s->state=SSL3_ST_SW_FLUSH;
3980Sstevel@tonic-gate 				s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
3990Sstevel@tonic-gate #endif
4000Sstevel@tonic-gate 				s->init_num=0;
4010Sstevel@tonic-gate 				}
4020Sstevel@tonic-gate 			break;
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 		case SSL3_ST_SW_SRVR_DONE_A:
4050Sstevel@tonic-gate 		case SSL3_ST_SW_SRVR_DONE_B:
4060Sstevel@tonic-gate 			ret=ssl3_send_server_done(s);
4070Sstevel@tonic-gate 			if (ret <= 0) goto end;
4080Sstevel@tonic-gate 			s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
4090Sstevel@tonic-gate 			s->state=SSL3_ST_SW_FLUSH;
4100Sstevel@tonic-gate 			s->init_num=0;
4110Sstevel@tonic-gate 			break;
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 		case SSL3_ST_SW_FLUSH:
4140Sstevel@tonic-gate 			/* number of bytes to be flushed */
4150Sstevel@tonic-gate 			num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
4160Sstevel@tonic-gate 			if (num1 > 0)
4170Sstevel@tonic-gate 				{
4180Sstevel@tonic-gate 				s->rwstate=SSL_WRITING;
4190Sstevel@tonic-gate 				num1=BIO_flush(s->wbio);
4200Sstevel@tonic-gate 				if (num1 <= 0) { ret= -1; goto end; }
4210Sstevel@tonic-gate 				s->rwstate=SSL_NOTHING;
4220Sstevel@tonic-gate 				}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 			s->state=s->s3->tmp.next_state;
4250Sstevel@tonic-gate 			break;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 		case SSL3_ST_SR_CERT_A:
4280Sstevel@tonic-gate 		case SSL3_ST_SR_CERT_B:
4290Sstevel@tonic-gate 			/* Check for second client hello (MS SGC) */
4300Sstevel@tonic-gate 			ret = ssl3_check_client_hello(s);
4310Sstevel@tonic-gate 			if (ret <= 0)
4320Sstevel@tonic-gate 				goto end;
4330Sstevel@tonic-gate 			if (ret == 2)
4340Sstevel@tonic-gate 				s->state = SSL3_ST_SR_CLNT_HELLO_C;
4350Sstevel@tonic-gate 			else {
4360Sstevel@tonic-gate 				if (s->s3->tmp.cert_request)
4370Sstevel@tonic-gate 					{
4380Sstevel@tonic-gate 					ret=ssl3_get_client_certificate(s);
4390Sstevel@tonic-gate 					if (ret <= 0) goto end;
4400Sstevel@tonic-gate 					}
4410Sstevel@tonic-gate 				s->init_num=0;
4420Sstevel@tonic-gate 				s->state=SSL3_ST_SR_KEY_EXCH_A;
4430Sstevel@tonic-gate 			}
4440Sstevel@tonic-gate 			break;
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 		case SSL3_ST_SR_KEY_EXCH_A:
4470Sstevel@tonic-gate 		case SSL3_ST_SR_KEY_EXCH_B:
4480Sstevel@tonic-gate 			ret=ssl3_get_client_key_exchange(s);
4492139Sjp161948 			if (ret <= 0)
4502139Sjp161948 				goto end;
4512139Sjp161948 			if (ret == 2)
4522139Sjp161948 				{
4532139Sjp161948 				/* For the ECDH ciphersuites when
4542139Sjp161948 				 * the client sends its ECDH pub key in
4552139Sjp161948 				 * a certificate, the CertificateVerify
4562139Sjp161948 				 * message is not sent.
4572139Sjp161948 				 */
4582139Sjp161948 				s->state=SSL3_ST_SR_FINISHED_A;
4592139Sjp161948 				s->init_num = 0;
4602139Sjp161948 				}
4612139Sjp161948 			else
4622139Sjp161948 				{
4632139Sjp161948 				s->state=SSL3_ST_SR_CERT_VRFY_A;
4642139Sjp161948 				s->init_num=0;
4650Sstevel@tonic-gate 
4662139Sjp161948 				/* We need to get hashes here so if there is
4672139Sjp161948 				 * a client cert, it can be verified
4682139Sjp161948 				 */
4692139Sjp161948 				s->method->ssl3_enc->cert_verify_mac(s,
4702139Sjp161948 				    &(s->s3->finish_dgst1),
4712139Sjp161948 				    &(s->s3->tmp.cert_verify_md[0]));
4722139Sjp161948 				s->method->ssl3_enc->cert_verify_mac(s,
4732139Sjp161948 				    &(s->s3->finish_dgst2),
4742139Sjp161948 				    &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
4752139Sjp161948 				}
4760Sstevel@tonic-gate 			break;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 		case SSL3_ST_SR_CERT_VRFY_A:
4790Sstevel@tonic-gate 		case SSL3_ST_SR_CERT_VRFY_B:
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 			/* we should decide if we expected this one */
4820Sstevel@tonic-gate 			ret=ssl3_get_cert_verify(s);
4830Sstevel@tonic-gate 			if (ret <= 0) goto end;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 			s->state=SSL3_ST_SR_FINISHED_A;
4860Sstevel@tonic-gate 			s->init_num=0;
4870Sstevel@tonic-gate 			break;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 		case SSL3_ST_SR_FINISHED_A:
4900Sstevel@tonic-gate 		case SSL3_ST_SR_FINISHED_B:
4910Sstevel@tonic-gate 			ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
4920Sstevel@tonic-gate 				SSL3_ST_SR_FINISHED_B);
4930Sstevel@tonic-gate 			if (ret <= 0) goto end;
4940Sstevel@tonic-gate 			if (s->hit)
4950Sstevel@tonic-gate 				s->state=SSL_ST_OK;
4960Sstevel@tonic-gate 			else
4970Sstevel@tonic-gate 				s->state=SSL3_ST_SW_CHANGE_A;
4980Sstevel@tonic-gate 			s->init_num=0;
4990Sstevel@tonic-gate 			break;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 		case SSL3_ST_SW_CHANGE_A:
5020Sstevel@tonic-gate 		case SSL3_ST_SW_CHANGE_B:
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 			s->session->cipher=s->s3->tmp.new_cipher;
5050Sstevel@tonic-gate 			if (!s->method->ssl3_enc->setup_key_block(s))
5060Sstevel@tonic-gate 				{ ret= -1; goto end; }
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 			ret=ssl3_send_change_cipher_spec(s,
5090Sstevel@tonic-gate 				SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 			if (ret <= 0) goto end;
5120Sstevel@tonic-gate 			s->state=SSL3_ST_SW_FINISHED_A;
5130Sstevel@tonic-gate 			s->init_num=0;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 			if (!s->method->ssl3_enc->change_cipher_state(s,
5160Sstevel@tonic-gate 				SSL3_CHANGE_CIPHER_SERVER_WRITE))
5170Sstevel@tonic-gate 				{
5180Sstevel@tonic-gate 				ret= -1;
5190Sstevel@tonic-gate 				goto end;
5200Sstevel@tonic-gate 				}
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 			break;
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 		case SSL3_ST_SW_FINISHED_A:
5250Sstevel@tonic-gate 		case SSL3_ST_SW_FINISHED_B:
5260Sstevel@tonic-gate 			ret=ssl3_send_finished(s,
5270Sstevel@tonic-gate 				SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
5280Sstevel@tonic-gate 				s->method->ssl3_enc->server_finished_label,
5290Sstevel@tonic-gate 				s->method->ssl3_enc->server_finished_label_len);
5300Sstevel@tonic-gate 			if (ret <= 0) goto end;
5310Sstevel@tonic-gate 			s->state=SSL3_ST_SW_FLUSH;
5320Sstevel@tonic-gate 			if (s->hit)
5330Sstevel@tonic-gate 				s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
5340Sstevel@tonic-gate 			else
5350Sstevel@tonic-gate 				s->s3->tmp.next_state=SSL_ST_OK;
5360Sstevel@tonic-gate 			s->init_num=0;
5370Sstevel@tonic-gate 			break;
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 		case SSL_ST_OK:
5400Sstevel@tonic-gate 			/* clean a few things up */
5410Sstevel@tonic-gate 			ssl3_cleanup_key_block(s);
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 			BUF_MEM_free(s->init_buf);
5440Sstevel@tonic-gate 			s->init_buf=NULL;
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 			/* remove buffering on output */
5470Sstevel@tonic-gate 			ssl_free_wbio_buffer(s);
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 			s->init_num=0;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 			if (s->new_session == 2) /* skipped if we just sent a HelloRequest */
5520Sstevel@tonic-gate 				{
5530Sstevel@tonic-gate 				/* actually not necessarily a 'new' session unless
5540Sstevel@tonic-gate 				 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 				s->new_session=0;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 				ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 				s->ctx->stats.sess_accept_good++;
5610Sstevel@tonic-gate 				/* s->server=1; */
5620Sstevel@tonic-gate 				s->handshake_func=ssl3_accept;
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 				if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
5650Sstevel@tonic-gate 				}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 			ret = 1;
5680Sstevel@tonic-gate 			goto end;
5690Sstevel@tonic-gate 			/* break; */
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 		default:
5720Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
5730Sstevel@tonic-gate 			ret= -1;
5740Sstevel@tonic-gate 			goto end;
5750Sstevel@tonic-gate 			/* break; */
5760Sstevel@tonic-gate 			}
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 		if (!s->s3->tmp.reuse_message && !skip)
5790Sstevel@tonic-gate 			{
5800Sstevel@tonic-gate 			if (s->debug)
5810Sstevel@tonic-gate 				{
5820Sstevel@tonic-gate 				if ((ret=BIO_flush(s->wbio)) <= 0)
5830Sstevel@tonic-gate 					goto end;
5840Sstevel@tonic-gate 				}
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 			if ((cb != NULL) && (s->state != state))
5880Sstevel@tonic-gate 				{
5890Sstevel@tonic-gate 				new_state=s->state;
5900Sstevel@tonic-gate 				s->state=state;
5910Sstevel@tonic-gate 				cb(s,SSL_CB_ACCEPT_LOOP,1);
5920Sstevel@tonic-gate 				s->state=new_state;
5930Sstevel@tonic-gate 				}
5940Sstevel@tonic-gate 			}
5950Sstevel@tonic-gate 		skip=0;
5960Sstevel@tonic-gate 		}
5970Sstevel@tonic-gate end:
5980Sstevel@tonic-gate 	/* BIO_flush(s->wbio); */
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	s->in_handshake--;
6010Sstevel@tonic-gate 	if (cb != NULL)
6020Sstevel@tonic-gate 		cb(s,SSL_CB_ACCEPT_EXIT,ret);
6030Sstevel@tonic-gate 	return(ret);
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 
ssl3_send_hello_request(SSL * s)6062139Sjp161948 int ssl3_send_hello_request(SSL *s)
6070Sstevel@tonic-gate 	{
6080Sstevel@tonic-gate 	unsigned char *p;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	if (s->state == SSL3_ST_SW_HELLO_REQ_A)
6110Sstevel@tonic-gate 		{
6120Sstevel@tonic-gate 		p=(unsigned char *)s->init_buf->data;
6130Sstevel@tonic-gate 		*(p++)=SSL3_MT_HELLO_REQUEST;
6140Sstevel@tonic-gate 		*(p++)=0;
6150Sstevel@tonic-gate 		*(p++)=0;
6160Sstevel@tonic-gate 		*(p++)=0;
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 		s->state=SSL3_ST_SW_HELLO_REQ_B;
6190Sstevel@tonic-gate 		/* number of bytes to write */
6200Sstevel@tonic-gate 		s->init_num=4;
6210Sstevel@tonic-gate 		s->init_off=0;
6220Sstevel@tonic-gate 		}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	/* SSL3_ST_SW_HELLO_REQ_B */
6250Sstevel@tonic-gate 	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 
ssl3_check_client_hello(SSL * s)6282139Sjp161948 int ssl3_check_client_hello(SSL *s)
6290Sstevel@tonic-gate 	{
6300Sstevel@tonic-gate 	int ok;
6310Sstevel@tonic-gate 	long n;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	/* this function is called when we really expect a Certificate message,
6340Sstevel@tonic-gate 	 * so permit appropriate message length */
6352139Sjp161948 	n=s->method->ssl_get_message(s,
6360Sstevel@tonic-gate 		SSL3_ST_SR_CERT_A,
6370Sstevel@tonic-gate 		SSL3_ST_SR_CERT_B,
6380Sstevel@tonic-gate 		-1,
6390Sstevel@tonic-gate 		s->max_cert_list,
6400Sstevel@tonic-gate 		&ok);
6410Sstevel@tonic-gate 	if (!ok) return((int)n);
6420Sstevel@tonic-gate 	s->s3->tmp.reuse_message = 1;
6430Sstevel@tonic-gate 	if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
6440Sstevel@tonic-gate 		{
6450Sstevel@tonic-gate 		/* Throw away what we have done so far in the current handshake,
6460Sstevel@tonic-gate 		 * which will now be aborted. (A full SSL_clear would be too much.)
6470Sstevel@tonic-gate 		 * I hope that tmp.dh is the only thing that may need to be cleared
6480Sstevel@tonic-gate 		 * when a handshake is not completed ... */
6490Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
6500Sstevel@tonic-gate 		if (s->s3->tmp.dh != NULL)
6510Sstevel@tonic-gate 			{
6520Sstevel@tonic-gate 			DH_free(s->s3->tmp.dh);
6530Sstevel@tonic-gate 			s->s3->tmp.dh = NULL;
6540Sstevel@tonic-gate 			}
6550Sstevel@tonic-gate #endif
6560Sstevel@tonic-gate 		return 2;
6570Sstevel@tonic-gate 		}
6580Sstevel@tonic-gate 	return 1;
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate 
ssl3_get_client_hello(SSL * s)6612139Sjp161948 int ssl3_get_client_hello(SSL *s)
6620Sstevel@tonic-gate 	{
6630Sstevel@tonic-gate 	int i,j,ok,al,ret= -1;
6642139Sjp161948 	unsigned int cookie_len;
6650Sstevel@tonic-gate 	long n;
6660Sstevel@tonic-gate 	unsigned long id;
6670Sstevel@tonic-gate 	unsigned char *p,*d,*q;
6680Sstevel@tonic-gate 	SSL_CIPHER *c;
6692139Sjp161948 #ifndef OPENSSL_NO_COMP
6700Sstevel@tonic-gate 	SSL_COMP *comp=NULL;
6712139Sjp161948 #endif
6720Sstevel@tonic-gate 	STACK_OF(SSL_CIPHER) *ciphers=NULL;
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	/* We do this so that we will respond with our native type.
6750Sstevel@tonic-gate 	 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
6760Sstevel@tonic-gate 	 * This down switching should be handled by a different method.
6770Sstevel@tonic-gate 	 * If we are SSLv3, we will respond with SSLv3, even if prompted with
6780Sstevel@tonic-gate 	 * TLSv1.
6790Sstevel@tonic-gate 	 */
6800Sstevel@tonic-gate 	if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
6810Sstevel@tonic-gate 		{
6820Sstevel@tonic-gate 		s->first_packet=1;
6830Sstevel@tonic-gate 		s->state=SSL3_ST_SR_CLNT_HELLO_B;
6840Sstevel@tonic-gate 		}
6852139Sjp161948 	n=s->method->ssl_get_message(s,
6860Sstevel@tonic-gate 		SSL3_ST_SR_CLNT_HELLO_B,
6870Sstevel@tonic-gate 		SSL3_ST_SR_CLNT_HELLO_C,
6880Sstevel@tonic-gate 		SSL3_MT_CLIENT_HELLO,
6890Sstevel@tonic-gate 		SSL3_RT_MAX_PLAIN_LENGTH,
6900Sstevel@tonic-gate 		&ok);
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	if (!ok) return((int)n);
6930Sstevel@tonic-gate 	d=p=(unsigned char *)s->init_msg;
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	/* use version from inside client hello, not from record header
6960Sstevel@tonic-gate 	 * (may differ: see RFC 2246, Appendix E, second paragraph) */
6970Sstevel@tonic-gate 	s->client_version=(((int)p[0])<<8)|(int)p[1];
6980Sstevel@tonic-gate 	p+=2;
6990Sstevel@tonic-gate 
7005434Sjp161948 	if ((s->version == DTLS1_VERSION && s->client_version > s->version) ||
7015434Sjp161948 	    (s->version != DTLS1_VERSION && s->client_version < s->version))
7020Sstevel@tonic-gate 		{
7030Sstevel@tonic-gate 		SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
7040Sstevel@tonic-gate 		if ((s->client_version>>8) == SSL3_VERSION_MAJOR)
7050Sstevel@tonic-gate 			{
7060Sstevel@tonic-gate 			/* similar to ssl3_get_record, send alert using remote version number */
7070Sstevel@tonic-gate 			s->version = s->client_version;
7080Sstevel@tonic-gate 			}
7090Sstevel@tonic-gate 		al = SSL_AD_PROTOCOL_VERSION;
7100Sstevel@tonic-gate 		goto f_err;
7110Sstevel@tonic-gate 		}
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	/* load the client random */
7140Sstevel@tonic-gate 	memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
7150Sstevel@tonic-gate 	p+=SSL3_RANDOM_SIZE;
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	/* get the session-id */
7180Sstevel@tonic-gate 	j= *(p++);
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	s->hit=0;
7210Sstevel@tonic-gate 	/* Versions before 0.9.7 always allow session reuse during renegotiation
7220Sstevel@tonic-gate 	 * (i.e. when s->new_session is true), option
7230Sstevel@tonic-gate 	 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7.
7240Sstevel@tonic-gate 	 * Maybe this optional behaviour should always have been the default,
7250Sstevel@tonic-gate 	 * but we cannot safely change the default behaviour (or new applications
7260Sstevel@tonic-gate 	 * might be written that become totally unsecure when compiled with
7270Sstevel@tonic-gate 	 * an earlier library version)
7280Sstevel@tonic-gate 	 */
7290Sstevel@tonic-gate 	if (j == 0 || (s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
7300Sstevel@tonic-gate 		{
7310Sstevel@tonic-gate 		if (!ssl_get_new_session(s,1))
7320Sstevel@tonic-gate 			goto err;
7330Sstevel@tonic-gate 		}
7340Sstevel@tonic-gate 	else
7350Sstevel@tonic-gate 		{
7360Sstevel@tonic-gate 		i=ssl_get_prev_session(s,p,j);
7370Sstevel@tonic-gate 		if (i == 1)
7380Sstevel@tonic-gate 			{ /* previous session */
7390Sstevel@tonic-gate 			s->hit=1;
7400Sstevel@tonic-gate 			}
7410Sstevel@tonic-gate 		else if (i == -1)
7420Sstevel@tonic-gate 			goto err;
7430Sstevel@tonic-gate 		else /* i == 0 */
7440Sstevel@tonic-gate 			{
7450Sstevel@tonic-gate 			if (!ssl_get_new_session(s,1))
7460Sstevel@tonic-gate 				goto err;
7470Sstevel@tonic-gate 			}
7480Sstevel@tonic-gate 		}
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	p+=j;
7512139Sjp161948 
7525434Sjp161948 	if (s->version == DTLS1_VERSION)
7532139Sjp161948 		{
7542139Sjp161948 		/* cookie stuff */
7552139Sjp161948 		cookie_len = *(p++);
7562139Sjp161948 
7572139Sjp161948 		if ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
7582139Sjp161948 			s->d1->send_cookie == 0)
7592139Sjp161948 			{
7602139Sjp161948 			/* HelloVerifyMessage has already been sent */
7612139Sjp161948 			if ( cookie_len != s->d1->cookie_len)
7622139Sjp161948 				{
7632139Sjp161948 				al = SSL_AD_HANDSHAKE_FAILURE;
7642139Sjp161948 				SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
7652139Sjp161948 				goto f_err;
7662139Sjp161948 				}
7672139Sjp161948 			}
7682139Sjp161948 
7692139Sjp161948 		/*
7702139Sjp161948 		 * The ClientHello may contain a cookie even if the
7712139Sjp161948 		 * HelloVerify message has not been sent--make sure that it
7722139Sjp161948 		 * does not cause an overflow.
7732139Sjp161948 		 */
7742139Sjp161948 		if ( cookie_len > sizeof(s->d1->rcvd_cookie))
7752139Sjp161948 			{
7762139Sjp161948 			/* too much data */
7772139Sjp161948 			al = SSL_AD_DECODE_ERROR;
7782139Sjp161948 			SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
7792139Sjp161948 			goto f_err;
7802139Sjp161948 			}
7812139Sjp161948 
7822139Sjp161948 		/* verify the cookie if appropriate option is set. */
7832139Sjp161948 		if ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
7842139Sjp161948 			cookie_len > 0)
7852139Sjp161948 			{
7862139Sjp161948 			memcpy(s->d1->rcvd_cookie, p, cookie_len);
7872139Sjp161948 
7882139Sjp161948 			if ( s->ctx->app_verify_cookie_cb != NULL)
7892139Sjp161948 				{
7902139Sjp161948 				if ( s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
7912139Sjp161948 					cookie_len) == 0)
7922139Sjp161948 					{
7932139Sjp161948 					al=SSL_AD_HANDSHAKE_FAILURE;
7942139Sjp161948 					SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
7952139Sjp161948 						SSL_R_COOKIE_MISMATCH);
7962139Sjp161948 					goto f_err;
7972139Sjp161948 					}
7982139Sjp161948 				/* else cookie verification succeeded */
7992139Sjp161948 				}
8002139Sjp161948 			else if ( memcmp(s->d1->rcvd_cookie, s->d1->cookie,
8012139Sjp161948 						  s->d1->cookie_len) != 0) /* default verification */
8022139Sjp161948 				{
8032139Sjp161948 					al=SSL_AD_HANDSHAKE_FAILURE;
8042139Sjp161948 					SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
8052139Sjp161948 						SSL_R_COOKIE_MISMATCH);
8062139Sjp161948 					goto f_err;
8072139Sjp161948 				}
8082139Sjp161948 			}
8092139Sjp161948 
8102139Sjp161948 		p += cookie_len;
8112139Sjp161948 		}
8122139Sjp161948 
8130Sstevel@tonic-gate 	n2s(p,i);
8140Sstevel@tonic-gate 	if ((i == 0) && (j != 0))
8150Sstevel@tonic-gate 		{
8160Sstevel@tonic-gate 		/* we need a cipher if we are not resuming a session */
8170Sstevel@tonic-gate 		al=SSL_AD_ILLEGAL_PARAMETER;
8180Sstevel@tonic-gate 		SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
8190Sstevel@tonic-gate 		goto f_err;
8200Sstevel@tonic-gate 		}
8210Sstevel@tonic-gate 	if ((p+i) >= (d+n))
8220Sstevel@tonic-gate 		{
8230Sstevel@tonic-gate 		/* not enough data */
8240Sstevel@tonic-gate 		al=SSL_AD_DECODE_ERROR;
8250Sstevel@tonic-gate 		SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
8260Sstevel@tonic-gate 		goto f_err;
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 	if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
8290Sstevel@tonic-gate 		== NULL))
8300Sstevel@tonic-gate 		{
8310Sstevel@tonic-gate 		goto err;
8320Sstevel@tonic-gate 		}
8330Sstevel@tonic-gate 	p+=i;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	/* If it is a hit, check that the cipher is in the list */
8360Sstevel@tonic-gate 	if ((s->hit) && (i > 0))
8370Sstevel@tonic-gate 		{
8380Sstevel@tonic-gate 		j=0;
8390Sstevel@tonic-gate 		id=s->session->cipher->id;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate #ifdef CIPHER_DEBUG
8420Sstevel@tonic-gate 		printf("client sent %d ciphers\n",sk_num(ciphers));
8430Sstevel@tonic-gate #endif
8440Sstevel@tonic-gate 		for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
8450Sstevel@tonic-gate 			{
8460Sstevel@tonic-gate 			c=sk_SSL_CIPHER_value(ciphers,i);
8470Sstevel@tonic-gate #ifdef CIPHER_DEBUG
8480Sstevel@tonic-gate 			printf("client [%2d of %2d]:%s\n",
8490Sstevel@tonic-gate 				i,sk_num(ciphers),SSL_CIPHER_get_name(c));
8500Sstevel@tonic-gate #endif
8510Sstevel@tonic-gate 			if (c->id == id)
8520Sstevel@tonic-gate 				{
8530Sstevel@tonic-gate 				j=1;
8540Sstevel@tonic-gate 				break;
8550Sstevel@tonic-gate 				}
8560Sstevel@tonic-gate 			}
8570Sstevel@tonic-gate 		if (j == 0)
8580Sstevel@tonic-gate 			{
8590Sstevel@tonic-gate 			if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
8600Sstevel@tonic-gate 				{
8610Sstevel@tonic-gate 				/* Very bad for multi-threading.... */
8622139Sjp161948 				s->session->cipher=sk_SSL_CIPHER_value(ciphers, 0);
8630Sstevel@tonic-gate 				}
8640Sstevel@tonic-gate 			else
8650Sstevel@tonic-gate 				{
8660Sstevel@tonic-gate 				/* we need to have the cipher in the cipher
8670Sstevel@tonic-gate 				 * list if we are asked to reuse it */
8680Sstevel@tonic-gate 				al=SSL_AD_ILLEGAL_PARAMETER;
8690Sstevel@tonic-gate 				SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
8700Sstevel@tonic-gate 				goto f_err;
8710Sstevel@tonic-gate 				}
8720Sstevel@tonic-gate 			}
8730Sstevel@tonic-gate 		}
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	/* compression */
8760Sstevel@tonic-gate 	i= *(p++);
8770Sstevel@tonic-gate 	if ((p+i) > (d+n))
8780Sstevel@tonic-gate 		{
8790Sstevel@tonic-gate 		/* not enough data */
8800Sstevel@tonic-gate 		al=SSL_AD_DECODE_ERROR;
8810Sstevel@tonic-gate 		SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
8820Sstevel@tonic-gate 		goto f_err;
8830Sstevel@tonic-gate 		}
8840Sstevel@tonic-gate 	q=p;
8850Sstevel@tonic-gate 	for (j=0; j<i; j++)
8860Sstevel@tonic-gate 		{
8870Sstevel@tonic-gate 		if (p[j] == 0) break;
8880Sstevel@tonic-gate 		}
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	p+=i;
8910Sstevel@tonic-gate 	if (j >= i)
8920Sstevel@tonic-gate 		{
8930Sstevel@tonic-gate 		/* no compress */
8940Sstevel@tonic-gate 		al=SSL_AD_DECODE_ERROR;
8950Sstevel@tonic-gate 		SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
8960Sstevel@tonic-gate 		goto f_err;
8970Sstevel@tonic-gate 		}
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	/* Worst case, we will use the NULL compression, but if we have other
9000Sstevel@tonic-gate 	 * options, we will now look for them.  We have i-1 compression
9010Sstevel@tonic-gate 	 * algorithms from the client, starting at q. */
9020Sstevel@tonic-gate 	s->s3->tmp.new_compression=NULL;
9032139Sjp161948 #ifndef OPENSSL_NO_COMP
9040Sstevel@tonic-gate 	if (s->ctx->comp_methods != NULL)
9050Sstevel@tonic-gate 		{ /* See if we have a match */
9060Sstevel@tonic-gate 		int m,nn,o,v,done=0;
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 		nn=sk_SSL_COMP_num(s->ctx->comp_methods);
9090Sstevel@tonic-gate 		for (m=0; m<nn; m++)
9100Sstevel@tonic-gate 			{
9110Sstevel@tonic-gate 			comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
9120Sstevel@tonic-gate 			v=comp->id;
9130Sstevel@tonic-gate 			for (o=0; o<i; o++)
9140Sstevel@tonic-gate 				{
9150Sstevel@tonic-gate 				if (v == q[o])
9160Sstevel@tonic-gate 					{
9170Sstevel@tonic-gate 					done=1;
9180Sstevel@tonic-gate 					break;
9190Sstevel@tonic-gate 					}
9200Sstevel@tonic-gate 				}
9210Sstevel@tonic-gate 			if (done) break;
9220Sstevel@tonic-gate 			}
9230Sstevel@tonic-gate 		if (done)
9240Sstevel@tonic-gate 			s->s3->tmp.new_compression=comp;
9250Sstevel@tonic-gate 		else
9260Sstevel@tonic-gate 			comp=NULL;
9270Sstevel@tonic-gate 		}
9282139Sjp161948 #endif
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	/* TLS does not mind if there is extra stuff */
9310Sstevel@tonic-gate #if 0   /* SSL 3.0 does not mind either, so we should disable this test
9320Sstevel@tonic-gate          * (was enabled in 0.9.6d through 0.9.6j and 0.9.7 through 0.9.7b,
9330Sstevel@tonic-gate          * in earlier SSLeay/OpenSSL releases this test existed but was buggy) */
9340Sstevel@tonic-gate 	if (s->version == SSL3_VERSION)
9350Sstevel@tonic-gate 		{
9360Sstevel@tonic-gate 		if (p < (d+n))
9370Sstevel@tonic-gate 			{
9380Sstevel@tonic-gate 			/* wrong number of bytes,
9390Sstevel@tonic-gate 			 * there could be more to follow */
9400Sstevel@tonic-gate 			al=SSL_AD_DECODE_ERROR;
9410Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
9420Sstevel@tonic-gate 			goto f_err;
9430Sstevel@tonic-gate 			}
9440Sstevel@tonic-gate 		}
9450Sstevel@tonic-gate #endif
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	/* Given s->session->ciphers and SSL_get_ciphers, we must
9480Sstevel@tonic-gate 	 * pick a cipher */
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	if (!s->hit)
9510Sstevel@tonic-gate 		{
9522139Sjp161948 #ifdef OPENSSL_NO_COMP
9532139Sjp161948 		s->session->compress_meth=0;
9542139Sjp161948 #else
9550Sstevel@tonic-gate 		s->session->compress_meth=(comp == NULL)?0:comp->id;
9562139Sjp161948 #endif
9570Sstevel@tonic-gate 		if (s->session->ciphers != NULL)
9580Sstevel@tonic-gate 			sk_SSL_CIPHER_free(s->session->ciphers);
9590Sstevel@tonic-gate 		s->session->ciphers=ciphers;
9600Sstevel@tonic-gate 		if (ciphers == NULL)
9610Sstevel@tonic-gate 			{
9620Sstevel@tonic-gate 			al=SSL_AD_ILLEGAL_PARAMETER;
9630Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
9640Sstevel@tonic-gate 			goto f_err;
9650Sstevel@tonic-gate 			}
9660Sstevel@tonic-gate 		ciphers=NULL;
9670Sstevel@tonic-gate 		c=ssl3_choose_cipher(s,s->session->ciphers,
9680Sstevel@tonic-gate 				     SSL_get_ciphers(s));
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 		if (c == NULL)
9710Sstevel@tonic-gate 			{
9720Sstevel@tonic-gate 			al=SSL_AD_HANDSHAKE_FAILURE;
9730Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
9740Sstevel@tonic-gate 			goto f_err;
9750Sstevel@tonic-gate 			}
9760Sstevel@tonic-gate 		s->s3->tmp.new_cipher=c;
9770Sstevel@tonic-gate 		}
9780Sstevel@tonic-gate 	else
9790Sstevel@tonic-gate 		{
9800Sstevel@tonic-gate 		/* Session-id reuse */
9810Sstevel@tonic-gate #ifdef REUSE_CIPHER_BUG
9820Sstevel@tonic-gate 		STACK_OF(SSL_CIPHER) *sk;
9830Sstevel@tonic-gate 		SSL_CIPHER *nc=NULL;
9840Sstevel@tonic-gate 		SSL_CIPHER *ec=NULL;
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 		if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
9870Sstevel@tonic-gate 			{
9880Sstevel@tonic-gate 			sk=s->session->ciphers;
9890Sstevel@tonic-gate 			for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
9900Sstevel@tonic-gate 				{
9910Sstevel@tonic-gate 				c=sk_SSL_CIPHER_value(sk,i);
9920Sstevel@tonic-gate 				if (c->algorithms & SSL_eNULL)
9930Sstevel@tonic-gate 					nc=c;
9940Sstevel@tonic-gate 				if (SSL_C_IS_EXPORT(c))
9950Sstevel@tonic-gate 					ec=c;
9960Sstevel@tonic-gate 				}
9970Sstevel@tonic-gate 			if (nc != NULL)
9980Sstevel@tonic-gate 				s->s3->tmp.new_cipher=nc;
9990Sstevel@tonic-gate 			else if (ec != NULL)
10000Sstevel@tonic-gate 				s->s3->tmp.new_cipher=ec;
10010Sstevel@tonic-gate 			else
10020Sstevel@tonic-gate 				s->s3->tmp.new_cipher=s->session->cipher;
10030Sstevel@tonic-gate 			}
10040Sstevel@tonic-gate 		else
10050Sstevel@tonic-gate #endif
10060Sstevel@tonic-gate 		s->s3->tmp.new_cipher=s->session->cipher;
10070Sstevel@tonic-gate 		}
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	/* we now have the following setup.
10100Sstevel@tonic-gate 	 * client_random
10110Sstevel@tonic-gate 	 * cipher_list 		- our prefered list of ciphers
10120Sstevel@tonic-gate 	 * ciphers 		- the clients prefered list of ciphers
10130Sstevel@tonic-gate 	 * compression		- basically ignored right now
10140Sstevel@tonic-gate 	 * ssl version is set	- sslv3
10150Sstevel@tonic-gate 	 * s->session		- The ssl session has been setup.
10160Sstevel@tonic-gate 	 * s->hit		- session reuse flag
10170Sstevel@tonic-gate 	 * s->tmp.new_cipher	- the new cipher to use.
10180Sstevel@tonic-gate 	 */
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	ret=1;
10210Sstevel@tonic-gate 	if (0)
10220Sstevel@tonic-gate 		{
10230Sstevel@tonic-gate f_err:
10240Sstevel@tonic-gate 		ssl3_send_alert(s,SSL3_AL_FATAL,al);
10250Sstevel@tonic-gate 		}
10260Sstevel@tonic-gate err:
10270Sstevel@tonic-gate 	if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
10280Sstevel@tonic-gate 	return(ret);
10290Sstevel@tonic-gate 	}
10300Sstevel@tonic-gate 
ssl3_send_server_hello(SSL * s)10312139Sjp161948 int ssl3_send_server_hello(SSL *s)
10320Sstevel@tonic-gate 	{
10330Sstevel@tonic-gate 	unsigned char *buf;
10340Sstevel@tonic-gate 	unsigned char *p,*d;
10350Sstevel@tonic-gate 	int i,sl;
10360Sstevel@tonic-gate 	unsigned long l,Time;
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
10390Sstevel@tonic-gate 		{
10400Sstevel@tonic-gate 		buf=(unsigned char *)s->init_buf->data;
10410Sstevel@tonic-gate 		p=s->s3->server_random;
10420Sstevel@tonic-gate 		Time=time(NULL);			/* Time */
10430Sstevel@tonic-gate 		l2n(Time,p);
10442139Sjp161948 		if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
10452139Sjp161948 			return -1;
10460Sstevel@tonic-gate 		/* Do the message type and length last */
10470Sstevel@tonic-gate 		d=p= &(buf[4]);
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 		*(p++)=s->version>>8;
10500Sstevel@tonic-gate 		*(p++)=s->version&0xff;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 		/* Random stuff */
10530Sstevel@tonic-gate 		memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
10540Sstevel@tonic-gate 		p+=SSL3_RANDOM_SIZE;
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 		/* now in theory we have 3 options to sending back the
10570Sstevel@tonic-gate 		 * session id.  If it is a re-use, we send back the
10580Sstevel@tonic-gate 		 * old session-id, if it is a new session, we send
10590Sstevel@tonic-gate 		 * back the new session-id or we send back a 0 length
10600Sstevel@tonic-gate 		 * session-id if we want it to be single use.
10610Sstevel@tonic-gate 		 * Currently I will not implement the '0' length session-id
10620Sstevel@tonic-gate 		 * 12-Jan-98 - I'll now support the '0' length stuff.
10630Sstevel@tonic-gate 		 */
10640Sstevel@tonic-gate 		if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
10650Sstevel@tonic-gate 			s->session->session_id_length=0;
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 		sl=s->session->session_id_length;
10682139Sjp161948 		if (sl > (int)sizeof(s->session->session_id))
10690Sstevel@tonic-gate 			{
10700Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
10710Sstevel@tonic-gate 			return -1;
10720Sstevel@tonic-gate 			}
10730Sstevel@tonic-gate 		*(p++)=sl;
10740Sstevel@tonic-gate 		memcpy(p,s->session->session_id,sl);
10750Sstevel@tonic-gate 		p+=sl;
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 		/* put the cipher */
10780Sstevel@tonic-gate 		i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
10790Sstevel@tonic-gate 		p+=i;
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 		/* put the compression method */
10822139Sjp161948 #ifdef OPENSSL_NO_COMP
10832139Sjp161948 			*(p++)=0;
10842139Sjp161948 #else
10850Sstevel@tonic-gate 		if (s->s3->tmp.new_compression == NULL)
10860Sstevel@tonic-gate 			*(p++)=0;
10870Sstevel@tonic-gate 		else
10880Sstevel@tonic-gate 			*(p++)=s->s3->tmp.new_compression->id;
10892139Sjp161948 #endif
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 		/* do the header */
10920Sstevel@tonic-gate 		l=(p-d);
10930Sstevel@tonic-gate 		d=buf;
10940Sstevel@tonic-gate 		*(d++)=SSL3_MT_SERVER_HELLO;
10950Sstevel@tonic-gate 		l2n3(l,d);
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 		s->state=SSL3_ST_CW_CLNT_HELLO_B;
10980Sstevel@tonic-gate 		/* number of bytes to write */
10990Sstevel@tonic-gate 		s->init_num=p-buf;
11000Sstevel@tonic-gate 		s->init_off=0;
11010Sstevel@tonic-gate 		}
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	/* SSL3_ST_CW_CLNT_HELLO_B */
11040Sstevel@tonic-gate 	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
11050Sstevel@tonic-gate 	}
11060Sstevel@tonic-gate 
ssl3_send_server_done(SSL * s)11072139Sjp161948 int ssl3_send_server_done(SSL *s)
11080Sstevel@tonic-gate 	{
11090Sstevel@tonic-gate 	unsigned char *p;
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	if (s->state == SSL3_ST_SW_SRVR_DONE_A)
11120Sstevel@tonic-gate 		{
11130Sstevel@tonic-gate 		p=(unsigned char *)s->init_buf->data;
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 		/* do the header */
11160Sstevel@tonic-gate 		*(p++)=SSL3_MT_SERVER_DONE;
11170Sstevel@tonic-gate 		*(p++)=0;
11180Sstevel@tonic-gate 		*(p++)=0;
11190Sstevel@tonic-gate 		*(p++)=0;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 		s->state=SSL3_ST_SW_SRVR_DONE_B;
11220Sstevel@tonic-gate 		/* number of bytes to write */
11230Sstevel@tonic-gate 		s->init_num=4;
11240Sstevel@tonic-gate 		s->init_off=0;
11250Sstevel@tonic-gate 		}
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	/* SSL3_ST_CW_CLNT_HELLO_B */
11280Sstevel@tonic-gate 	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
11290Sstevel@tonic-gate 	}
11300Sstevel@tonic-gate 
ssl3_send_server_key_exchange(SSL * s)11312139Sjp161948 int ssl3_send_server_key_exchange(SSL *s)
11320Sstevel@tonic-gate 	{
11330Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
11340Sstevel@tonic-gate 	unsigned char *q;
11350Sstevel@tonic-gate 	int j,num;
11360Sstevel@tonic-gate 	RSA *rsa;
11370Sstevel@tonic-gate 	unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
11380Sstevel@tonic-gate 	unsigned int u;
11390Sstevel@tonic-gate #endif
11400Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
11410Sstevel@tonic-gate 	DH *dh=NULL,*dhp;
11420Sstevel@tonic-gate #endif
11432139Sjp161948 #ifndef OPENSSL_NO_ECDH
11442139Sjp161948 	EC_KEY *ecdh=NULL, *ecdhp;
11452139Sjp161948 	unsigned char *encodedPoint = NULL;
11462139Sjp161948 	int encodedlen = 0;
11472139Sjp161948 	int curve_id = 0;
11482139Sjp161948 	BN_CTX *bn_ctx = NULL;
11492139Sjp161948 #endif
11500Sstevel@tonic-gate 	EVP_PKEY *pkey;
11510Sstevel@tonic-gate 	unsigned char *p,*d;
11520Sstevel@tonic-gate 	int al,i;
11530Sstevel@tonic-gate 	unsigned long type;
11540Sstevel@tonic-gate 	int n;
11550Sstevel@tonic-gate 	CERT *cert;
11560Sstevel@tonic-gate 	BIGNUM *r[4];
11570Sstevel@tonic-gate 	int nr[4],kn;
11580Sstevel@tonic-gate 	BUF_MEM *buf;
11590Sstevel@tonic-gate 	EVP_MD_CTX md_ctx;
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	EVP_MD_CTX_init(&md_ctx);
11620Sstevel@tonic-gate 	if (s->state == SSL3_ST_SW_KEY_EXCH_A)
11630Sstevel@tonic-gate 		{
11640Sstevel@tonic-gate 		type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
11650Sstevel@tonic-gate 		cert=s->cert;
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 		buf=s->init_buf;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 		r[0]=r[1]=r[2]=r[3]=NULL;
11700Sstevel@tonic-gate 		n=0;
11710Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
11720Sstevel@tonic-gate 		if (type & SSL_kRSA)
11730Sstevel@tonic-gate 			{
11740Sstevel@tonic-gate 			rsa=cert->rsa_tmp;
11750Sstevel@tonic-gate 			if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
11760Sstevel@tonic-gate 				{
11770Sstevel@tonic-gate 				rsa=s->cert->rsa_tmp_cb(s,
11780Sstevel@tonic-gate 				      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
11790Sstevel@tonic-gate 				      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
11800Sstevel@tonic-gate 				if(rsa == NULL)
11810Sstevel@tonic-gate 				{
11820Sstevel@tonic-gate 					al=SSL_AD_HANDSHAKE_FAILURE;
11830Sstevel@tonic-gate 					SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
11840Sstevel@tonic-gate 					goto f_err;
11850Sstevel@tonic-gate 				}
11860Sstevel@tonic-gate 				RSA_up_ref(rsa);
11870Sstevel@tonic-gate 				cert->rsa_tmp=rsa;
11880Sstevel@tonic-gate 				}
11890Sstevel@tonic-gate 			if (rsa == NULL)
11900Sstevel@tonic-gate 				{
11910Sstevel@tonic-gate 				al=SSL_AD_HANDSHAKE_FAILURE;
11920Sstevel@tonic-gate 				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
11930Sstevel@tonic-gate 				goto f_err;
11940Sstevel@tonic-gate 				}
11950Sstevel@tonic-gate 			r[0]=rsa->n;
11960Sstevel@tonic-gate 			r[1]=rsa->e;
11970Sstevel@tonic-gate 			s->s3->tmp.use_rsa_tmp=1;
11980Sstevel@tonic-gate 			}
11990Sstevel@tonic-gate 		else
12000Sstevel@tonic-gate #endif
12010Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
12020Sstevel@tonic-gate 			if (type & SSL_kEDH)
12030Sstevel@tonic-gate 			{
12040Sstevel@tonic-gate 			dhp=cert->dh_tmp;
12050Sstevel@tonic-gate 			if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
12060Sstevel@tonic-gate 				dhp=s->cert->dh_tmp_cb(s,
12070Sstevel@tonic-gate 				      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
12080Sstevel@tonic-gate 				      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
12090Sstevel@tonic-gate 			if (dhp == NULL)
12100Sstevel@tonic-gate 				{
12110Sstevel@tonic-gate 				al=SSL_AD_HANDSHAKE_FAILURE;
12120Sstevel@tonic-gate 				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
12130Sstevel@tonic-gate 				goto f_err;
12140Sstevel@tonic-gate 				}
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 			if (s->s3->tmp.dh != NULL)
12170Sstevel@tonic-gate 				{
12180Sstevel@tonic-gate 				DH_free(dh);
12190Sstevel@tonic-gate 				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
12200Sstevel@tonic-gate 				goto err;
12210Sstevel@tonic-gate 				}
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 			if ((dh=DHparams_dup(dhp)) == NULL)
12240Sstevel@tonic-gate 				{
12250Sstevel@tonic-gate 				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
12260Sstevel@tonic-gate 				goto err;
12270Sstevel@tonic-gate 				}
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 			s->s3->tmp.dh=dh;
12300Sstevel@tonic-gate 			if ((dhp->pub_key == NULL ||
12310Sstevel@tonic-gate 			     dhp->priv_key == NULL ||
12320Sstevel@tonic-gate 			     (s->options & SSL_OP_SINGLE_DH_USE)))
12330Sstevel@tonic-gate 				{
12340Sstevel@tonic-gate 				if(!DH_generate_key(dh))
12350Sstevel@tonic-gate 				    {
12360Sstevel@tonic-gate 				    SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
12370Sstevel@tonic-gate 					   ERR_R_DH_LIB);
12380Sstevel@tonic-gate 				    goto err;
12390Sstevel@tonic-gate 				    }
12400Sstevel@tonic-gate 				}
12410Sstevel@tonic-gate 			else
12420Sstevel@tonic-gate 				{
12430Sstevel@tonic-gate 				dh->pub_key=BN_dup(dhp->pub_key);
12440Sstevel@tonic-gate 				dh->priv_key=BN_dup(dhp->priv_key);
12450Sstevel@tonic-gate 				if ((dh->pub_key == NULL) ||
12460Sstevel@tonic-gate 					(dh->priv_key == NULL))
12470Sstevel@tonic-gate 					{
12480Sstevel@tonic-gate 					SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
12490Sstevel@tonic-gate 					goto err;
12500Sstevel@tonic-gate 					}
12510Sstevel@tonic-gate 				}
12520Sstevel@tonic-gate 			r[0]=dh->p;
12530Sstevel@tonic-gate 			r[1]=dh->g;
12540Sstevel@tonic-gate 			r[2]=dh->pub_key;
12550Sstevel@tonic-gate 			}
12560Sstevel@tonic-gate 		else
12570Sstevel@tonic-gate #endif
12582139Sjp161948 #ifndef OPENSSL_NO_ECDH
12592139Sjp161948 			if (type & SSL_kECDHE)
12602139Sjp161948 			{
12612139Sjp161948 			const EC_GROUP *group;
12622139Sjp161948 
12632139Sjp161948 			ecdhp=cert->ecdh_tmp;
12642139Sjp161948 			if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL))
12652139Sjp161948 				{
12662139Sjp161948 				ecdhp=s->cert->ecdh_tmp_cb(s,
12672139Sjp161948 				      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
12682139Sjp161948 				      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
12692139Sjp161948 				}
12702139Sjp161948 			if (ecdhp == NULL)
12712139Sjp161948 				{
12722139Sjp161948 				al=SSL_AD_HANDSHAKE_FAILURE;
12732139Sjp161948 				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
12742139Sjp161948 				goto f_err;
12752139Sjp161948 				}
12762139Sjp161948 
12772139Sjp161948 			if (s->s3->tmp.ecdh != NULL)
12782139Sjp161948 				{
12792139Sjp161948 				EC_KEY_free(s->s3->tmp.ecdh);
12802139Sjp161948 				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
12812139Sjp161948 				goto err;
12822139Sjp161948 				}
12832139Sjp161948 
12842139Sjp161948 			/* Duplicate the ECDH structure. */
12852139Sjp161948 			if (ecdhp == NULL)
12862139Sjp161948 				{
12872139Sjp161948 				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
12882139Sjp161948 				goto err;
12892139Sjp161948 				}
12902139Sjp161948 			if (!EC_KEY_up_ref(ecdhp))
12912139Sjp161948 				{
12922139Sjp161948 				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
12932139Sjp161948 				goto err;
12942139Sjp161948 				}
12952139Sjp161948 			ecdh = ecdhp;
12962139Sjp161948 
12972139Sjp161948 			s->s3->tmp.ecdh=ecdh;
12982139Sjp161948 			if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
12992139Sjp161948 			    (EC_KEY_get0_private_key(ecdh) == NULL) ||
13002139Sjp161948 			    (s->options & SSL_OP_SINGLE_ECDH_USE))
13012139Sjp161948 				{
13022139Sjp161948 				if(!EC_KEY_generate_key(ecdh))
13032139Sjp161948 				    {
13042139Sjp161948 				    SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
13052139Sjp161948 				    goto err;
13062139Sjp161948 				    }
13072139Sjp161948 				}
13082139Sjp161948 
13092139Sjp161948 			if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
13102139Sjp161948 			    (EC_KEY_get0_public_key(ecdh)  == NULL) ||
13112139Sjp161948 			    (EC_KEY_get0_private_key(ecdh) == NULL))
13122139Sjp161948 				{
13132139Sjp161948 				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
13142139Sjp161948 				goto err;
13152139Sjp161948 				}
13162139Sjp161948 
13172139Sjp161948 			if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
13182139Sjp161948 			    (EC_GROUP_get_degree(group) > 163))
13192139Sjp161948 				{
13202139Sjp161948 				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
13212139Sjp161948 				goto err;
13222139Sjp161948 				}
13232139Sjp161948 
13242139Sjp161948 			/* XXX: For now, we only support ephemeral ECDH
13252139Sjp161948 			 * keys over named (not generic) curves. For
13262139Sjp161948 			 * supported named curves, curve_id is non-zero.
13272139Sjp161948 			 */
13282139Sjp161948 			if ((curve_id =
13292139Sjp161948 			    nid2curve_id(EC_GROUP_get_curve_name(group)))
13302139Sjp161948 			    == 0)
13312139Sjp161948 				{
13322139Sjp161948 				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
13332139Sjp161948 				goto err;
13342139Sjp161948 				}
13352139Sjp161948 
13362139Sjp161948 			/* Encode the public key.
13372139Sjp161948 			 * First check the size of encoding and
13382139Sjp161948 			 * allocate memory accordingly.
13392139Sjp161948 			 */
13402139Sjp161948 			encodedlen = EC_POINT_point2oct(group,
13412139Sjp161948 			    EC_KEY_get0_public_key(ecdh),
13422139Sjp161948 			    POINT_CONVERSION_UNCOMPRESSED,
13432139Sjp161948 			    NULL, 0, NULL);
13442139Sjp161948 
13452139Sjp161948 			encodedPoint = (unsigned char *)
13462139Sjp161948 			    OPENSSL_malloc(encodedlen*sizeof(unsigned char));
13472139Sjp161948 			bn_ctx = BN_CTX_new();
13482139Sjp161948 			if ((encodedPoint == NULL) || (bn_ctx == NULL))
13492139Sjp161948 				{
13502139Sjp161948 				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
13512139Sjp161948 				goto err;
13522139Sjp161948 				}
13532139Sjp161948 
13542139Sjp161948 
13552139Sjp161948 			encodedlen = EC_POINT_point2oct(group,
13562139Sjp161948 			    EC_KEY_get0_public_key(ecdh),
13572139Sjp161948 			    POINT_CONVERSION_UNCOMPRESSED,
13582139Sjp161948 			    encodedPoint, encodedlen, bn_ctx);
13592139Sjp161948 
13602139Sjp161948 			if (encodedlen == 0)
13612139Sjp161948 				{
13622139Sjp161948 				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
13632139Sjp161948 				goto err;
13642139Sjp161948 				}
13652139Sjp161948 
13662139Sjp161948 			BN_CTX_free(bn_ctx);  bn_ctx=NULL;
13672139Sjp161948 
13682139Sjp161948 			/* XXX: For now, we only support named (not
13692139Sjp161948 			 * generic) curves in ECDH ephemeral key exchanges.
13702139Sjp161948 			 * In this situation, we need three additional bytes
13712139Sjp161948 			 * to encode the entire ServerECDHParams
13722139Sjp161948 			 * structure.
13732139Sjp161948 			 */
13742139Sjp161948 			n = 3 + encodedlen;
13752139Sjp161948 
13762139Sjp161948 			/* We'll generate the serverKeyExchange message
13772139Sjp161948 			 * explicitly so we can set these to NULLs
13782139Sjp161948 			 */
13792139Sjp161948 			r[0]=NULL;
13802139Sjp161948 			r[1]=NULL;
13812139Sjp161948 			r[2]=NULL;
13822139Sjp161948 			}
13832139Sjp161948 		else
13842139Sjp161948 #endif /* !OPENSSL_NO_ECDH */
13850Sstevel@tonic-gate 			{
13860Sstevel@tonic-gate 			al=SSL_AD_HANDSHAKE_FAILURE;
13870Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
13880Sstevel@tonic-gate 			goto f_err;
13890Sstevel@tonic-gate 			}
13900Sstevel@tonic-gate 		for (i=0; r[i] != NULL; i++)
13910Sstevel@tonic-gate 			{
13920Sstevel@tonic-gate 			nr[i]=BN_num_bytes(r[i]);
13930Sstevel@tonic-gate 			n+=2+nr[i];
13940Sstevel@tonic-gate 			}
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 		if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
13970Sstevel@tonic-gate 			{
13980Sstevel@tonic-gate 			if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
13990Sstevel@tonic-gate 				== NULL)
14000Sstevel@tonic-gate 				{
14010Sstevel@tonic-gate 				al=SSL_AD_DECODE_ERROR;
14020Sstevel@tonic-gate 				goto f_err;
14030Sstevel@tonic-gate 				}
14040Sstevel@tonic-gate 			kn=EVP_PKEY_size(pkey);
14050Sstevel@tonic-gate 			}
14060Sstevel@tonic-gate 		else
14070Sstevel@tonic-gate 			{
14080Sstevel@tonic-gate 			pkey=NULL;
14090Sstevel@tonic-gate 			kn=0;
14100Sstevel@tonic-gate 			}
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 		if (!BUF_MEM_grow_clean(buf,n+4+kn))
14130Sstevel@tonic-gate 			{
14140Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
14150Sstevel@tonic-gate 			goto err;
14160Sstevel@tonic-gate 			}
14170Sstevel@tonic-gate 		d=(unsigned char *)s->init_buf->data;
14180Sstevel@tonic-gate 		p= &(d[4]);
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 		for (i=0; r[i] != NULL; i++)
14210Sstevel@tonic-gate 			{
14220Sstevel@tonic-gate 			s2n(nr[i],p);
14230Sstevel@tonic-gate 			BN_bn2bin(r[i],p);
14240Sstevel@tonic-gate 			p+=nr[i];
14250Sstevel@tonic-gate 			}
14260Sstevel@tonic-gate 
14272139Sjp161948 #ifndef OPENSSL_NO_ECDH
14282139Sjp161948 		if (type & SSL_kECDHE)
14292139Sjp161948 			{
14302139Sjp161948 			/* XXX: For now, we only support named (not generic) curves.
14312139Sjp161948 			 * In this situation, the serverKeyExchange message has:
14322139Sjp161948 			 * [1 byte CurveType], [1 byte CurveName]
14332139Sjp161948 			 * [1 byte length of encoded point], followed by
14342139Sjp161948 			 * the actual encoded point itself
14352139Sjp161948 			 */
14362139Sjp161948 			*p = NAMED_CURVE_TYPE;
14372139Sjp161948 			p += 1;
14382139Sjp161948 			*p = curve_id;
14392139Sjp161948 			p += 1;
14402139Sjp161948 			*p = encodedlen;
14412139Sjp161948 			p += 1;
14422139Sjp161948 			memcpy((unsigned char*)p,
14432139Sjp161948 			    (unsigned char *)encodedPoint,
14442139Sjp161948 			    encodedlen);
14452139Sjp161948 			OPENSSL_free(encodedPoint);
14462139Sjp161948 			p += encodedlen;
14472139Sjp161948 			}
14482139Sjp161948 #endif
14492139Sjp161948 
14500Sstevel@tonic-gate 		/* not anonymous */
14510Sstevel@tonic-gate 		if (pkey != NULL)
14520Sstevel@tonic-gate 			{
14530Sstevel@tonic-gate 			/* n is the length of the params, they start at &(d[4])
14540Sstevel@tonic-gate 			 * and p points to the space at the end. */
14550Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
14560Sstevel@tonic-gate 			if (pkey->type == EVP_PKEY_RSA)
14570Sstevel@tonic-gate 				{
14580Sstevel@tonic-gate 				q=md_buf;
14590Sstevel@tonic-gate 				j=0;
14600Sstevel@tonic-gate 				for (num=2; num > 0; num--)
14610Sstevel@tonic-gate 					{
14620Sstevel@tonic-gate 					EVP_DigestInit_ex(&md_ctx,(num == 2)
14630Sstevel@tonic-gate 						?s->ctx->md5:s->ctx->sha1, NULL);
14640Sstevel@tonic-gate 					EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
14650Sstevel@tonic-gate 					EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
14660Sstevel@tonic-gate 					EVP_DigestUpdate(&md_ctx,&(d[4]),n);
14670Sstevel@tonic-gate 					EVP_DigestFinal_ex(&md_ctx,q,
14680Sstevel@tonic-gate 						(unsigned int *)&i);
14690Sstevel@tonic-gate 					q+=i;
14700Sstevel@tonic-gate 					j+=i;
14710Sstevel@tonic-gate 					}
14720Sstevel@tonic-gate 				if (RSA_sign(NID_md5_sha1, md_buf, j,
14730Sstevel@tonic-gate 					&(p[2]), &u, pkey->pkey.rsa) <= 0)
14740Sstevel@tonic-gate 					{
14750Sstevel@tonic-gate 					SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
14760Sstevel@tonic-gate 					goto err;
14770Sstevel@tonic-gate 					}
14780Sstevel@tonic-gate 				s2n(u,p);
14790Sstevel@tonic-gate 				n+=u+2;
14800Sstevel@tonic-gate 				}
14810Sstevel@tonic-gate 			else
14820Sstevel@tonic-gate #endif
14830Sstevel@tonic-gate #if !defined(OPENSSL_NO_DSA)
14840Sstevel@tonic-gate 				if (pkey->type == EVP_PKEY_DSA)
14850Sstevel@tonic-gate 				{
14860Sstevel@tonic-gate 				/* lets do DSS */
14870Sstevel@tonic-gate 				EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
14880Sstevel@tonic-gate 				EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
14890Sstevel@tonic-gate 				EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
14900Sstevel@tonic-gate 				EVP_SignUpdate(&md_ctx,&(d[4]),n);
14910Sstevel@tonic-gate 				if (!EVP_SignFinal(&md_ctx,&(p[2]),
14920Sstevel@tonic-gate 					(unsigned int *)&i,pkey))
14930Sstevel@tonic-gate 					{
14940Sstevel@tonic-gate 					SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
14950Sstevel@tonic-gate 					goto err;
14960Sstevel@tonic-gate 					}
14970Sstevel@tonic-gate 				s2n(i,p);
14980Sstevel@tonic-gate 				n+=i+2;
14990Sstevel@tonic-gate 				}
15000Sstevel@tonic-gate 			else
15010Sstevel@tonic-gate #endif
15022139Sjp161948 #if !defined(OPENSSL_NO_ECDSA)
15032139Sjp161948 				if (pkey->type == EVP_PKEY_EC)
15042139Sjp161948 				{
15052139Sjp161948 				/* let's do ECDSA */
15062139Sjp161948 				EVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL);
15072139Sjp161948 				EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
15082139Sjp161948 				EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
15092139Sjp161948 				EVP_SignUpdate(&md_ctx,&(d[4]),n);
15102139Sjp161948 				if (!EVP_SignFinal(&md_ctx,&(p[2]),
15112139Sjp161948 					(unsigned int *)&i,pkey))
15122139Sjp161948 					{
15132139Sjp161948 					SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_ECDSA);
15142139Sjp161948 					goto err;
15152139Sjp161948 					}
15162139Sjp161948 				s2n(i,p);
15172139Sjp161948 				n+=i+2;
15182139Sjp161948 				}
15192139Sjp161948 			else
15202139Sjp161948 #endif
15210Sstevel@tonic-gate 				{
15220Sstevel@tonic-gate 				/* Is this error check actually needed? */
15230Sstevel@tonic-gate 				al=SSL_AD_HANDSHAKE_FAILURE;
15240Sstevel@tonic-gate 				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
15250Sstevel@tonic-gate 				goto f_err;
15260Sstevel@tonic-gate 				}
15270Sstevel@tonic-gate 			}
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 		*(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
15300Sstevel@tonic-gate 		l2n3(n,d);
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 		/* we should now have things packed up, so lets send
15330Sstevel@tonic-gate 		 * it off */
15340Sstevel@tonic-gate 		s->init_num=n+4;
15350Sstevel@tonic-gate 		s->init_off=0;
15360Sstevel@tonic-gate 		}
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	s->state = SSL3_ST_SW_KEY_EXCH_B;
15390Sstevel@tonic-gate 	EVP_MD_CTX_cleanup(&md_ctx);
15400Sstevel@tonic-gate 	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
15410Sstevel@tonic-gate f_err:
15420Sstevel@tonic-gate 	ssl3_send_alert(s,SSL3_AL_FATAL,al);
15430Sstevel@tonic-gate err:
15442139Sjp161948 #ifndef OPENSSL_NO_ECDH
15452139Sjp161948 	if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
15462139Sjp161948 	BN_CTX_free(bn_ctx);
15472139Sjp161948 #endif
15480Sstevel@tonic-gate 	EVP_MD_CTX_cleanup(&md_ctx);
15490Sstevel@tonic-gate 	return(-1);
15500Sstevel@tonic-gate 	}
15510Sstevel@tonic-gate 
ssl3_send_certificate_request(SSL * s)15522139Sjp161948 int ssl3_send_certificate_request(SSL *s)
15530Sstevel@tonic-gate 	{
15540Sstevel@tonic-gate 	unsigned char *p,*d;
15550Sstevel@tonic-gate 	int i,j,nl,off,n;
15560Sstevel@tonic-gate 	STACK_OF(X509_NAME) *sk=NULL;
15570Sstevel@tonic-gate 	X509_NAME *name;
15580Sstevel@tonic-gate 	BUF_MEM *buf;
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 	if (s->state == SSL3_ST_SW_CERT_REQ_A)
15610Sstevel@tonic-gate 		{
15620Sstevel@tonic-gate 		buf=s->init_buf;
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 		d=p=(unsigned char *)&(buf->data[4]);
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 		/* get the list of acceptable cert types */
15670Sstevel@tonic-gate 		p++;
15680Sstevel@tonic-gate 		n=ssl3_get_req_cert_type(s,p);
15690Sstevel@tonic-gate 		d[0]=n;
15700Sstevel@tonic-gate 		p+=n;
15710Sstevel@tonic-gate 		n++;
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 		off=n;
15740Sstevel@tonic-gate 		p+=2;
15750Sstevel@tonic-gate 		n+=2;
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 		sk=SSL_get_client_CA_list(s);
15780Sstevel@tonic-gate 		nl=0;
15790Sstevel@tonic-gate 		if (sk != NULL)
15800Sstevel@tonic-gate 			{
15810Sstevel@tonic-gate 			for (i=0; i<sk_X509_NAME_num(sk); i++)
15820Sstevel@tonic-gate 				{
15830Sstevel@tonic-gate 				name=sk_X509_NAME_value(sk,i);
15840Sstevel@tonic-gate 				j=i2d_X509_NAME(name,NULL);
15850Sstevel@tonic-gate 				if (!BUF_MEM_grow_clean(buf,4+n+j+2))
15860Sstevel@tonic-gate 					{
15870Sstevel@tonic-gate 					SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
15880Sstevel@tonic-gate 					goto err;
15890Sstevel@tonic-gate 					}
15900Sstevel@tonic-gate 				p=(unsigned char *)&(buf->data[4+n]);
15910Sstevel@tonic-gate 				if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
15920Sstevel@tonic-gate 					{
15930Sstevel@tonic-gate 					s2n(j,p);
15940Sstevel@tonic-gate 					i2d_X509_NAME(name,&p);
15950Sstevel@tonic-gate 					n+=2+j;
15960Sstevel@tonic-gate 					nl+=2+j;
15970Sstevel@tonic-gate 					}
15980Sstevel@tonic-gate 				else
15990Sstevel@tonic-gate 					{
16000Sstevel@tonic-gate 					d=p;
16010Sstevel@tonic-gate 					i2d_X509_NAME(name,&p);
16020Sstevel@tonic-gate 					j-=2; s2n(j,d); j+=2;
16030Sstevel@tonic-gate 					n+=j;
16040Sstevel@tonic-gate 					nl+=j;
16050Sstevel@tonic-gate 					}
16060Sstevel@tonic-gate 				}
16070Sstevel@tonic-gate 			}
16080Sstevel@tonic-gate 		/* else no CA names */
16090Sstevel@tonic-gate 		p=(unsigned char *)&(buf->data[4+off]);
16100Sstevel@tonic-gate 		s2n(nl,p);
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 		d=(unsigned char *)buf->data;
16130Sstevel@tonic-gate 		*(d++)=SSL3_MT_CERTIFICATE_REQUEST;
16140Sstevel@tonic-gate 		l2n3(n,d);
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 		/* we should now have things packed up, so lets send
16170Sstevel@tonic-gate 		 * it off */
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 		s->init_num=n+4;
16200Sstevel@tonic-gate 		s->init_off=0;
16210Sstevel@tonic-gate #ifdef NETSCAPE_HANG_BUG
16220Sstevel@tonic-gate 		p=(unsigned char *)s->init_buf->data + s->init_num;
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 		/* do the header */
16250Sstevel@tonic-gate 		*(p++)=SSL3_MT_SERVER_DONE;
16260Sstevel@tonic-gate 		*(p++)=0;
16270Sstevel@tonic-gate 		*(p++)=0;
16280Sstevel@tonic-gate 		*(p++)=0;
16290Sstevel@tonic-gate 		s->init_num += 4;
16300Sstevel@tonic-gate #endif
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 		s->state = SSL3_ST_SW_CERT_REQ_B;
16330Sstevel@tonic-gate 		}
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate 	/* SSL3_ST_SW_CERT_REQ_B */
16360Sstevel@tonic-gate 	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
16370Sstevel@tonic-gate err:
16380Sstevel@tonic-gate 	return(-1);
16390Sstevel@tonic-gate 	}
16400Sstevel@tonic-gate 
16412139Sjp161948 
16422139Sjp161948 #ifndef OPENSSL_NO_ECDH
16432139Sjp161948 static const int KDF1_SHA1_len = 20;
KDF1_SHA1(const void * in,size_t inlen,void * out,size_t * outlen)16442139Sjp161948 static void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen)
16452139Sjp161948 	{
16462139Sjp161948 #ifndef OPENSSL_NO_SHA
16472139Sjp161948 	if (*outlen < SHA_DIGEST_LENGTH)
16482139Sjp161948 		return NULL;
16492139Sjp161948 	else
16502139Sjp161948 		*outlen = SHA_DIGEST_LENGTH;
16512139Sjp161948 	return SHA1(in, inlen, out);
16522139Sjp161948 #else
16532139Sjp161948 	return NULL;
16542139Sjp161948 #endif	/* OPENSSL_NO_SHA */
16552139Sjp161948 	}
16562139Sjp161948 #endif	/* OPENSSL_NO_ECDH */
16572139Sjp161948 
ssl3_get_client_key_exchange(SSL * s)16582139Sjp161948 int ssl3_get_client_key_exchange(SSL *s)
16590Sstevel@tonic-gate 	{
16600Sstevel@tonic-gate 	int i,al,ok;
16610Sstevel@tonic-gate 	long n;
16620Sstevel@tonic-gate 	unsigned long l;
16630Sstevel@tonic-gate 	unsigned char *p;
16640Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
16650Sstevel@tonic-gate 	RSA *rsa=NULL;
16660Sstevel@tonic-gate 	EVP_PKEY *pkey=NULL;
16670Sstevel@tonic-gate #endif
16680Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
16690Sstevel@tonic-gate 	BIGNUM *pub=NULL;
16700Sstevel@tonic-gate 	DH *dh_srvr;
16710Sstevel@tonic-gate #endif
16720Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5
16730Sstevel@tonic-gate         KSSL_ERR kssl_err;
16740Sstevel@tonic-gate #endif /* OPENSSL_NO_KRB5 */
16750Sstevel@tonic-gate 
16762139Sjp161948 #ifndef OPENSSL_NO_ECDH
16772139Sjp161948 	EC_KEY *srvr_ecdh = NULL;
16782139Sjp161948 	EVP_PKEY *clnt_pub_pkey = NULL;
16792139Sjp161948 	EC_POINT *clnt_ecpoint = NULL;
16802139Sjp161948 	BN_CTX *bn_ctx = NULL;
16812139Sjp161948 #endif
16822139Sjp161948 
16832139Sjp161948 	n=s->method->ssl_get_message(s,
16840Sstevel@tonic-gate 		SSL3_ST_SR_KEY_EXCH_A,
16850Sstevel@tonic-gate 		SSL3_ST_SR_KEY_EXCH_B,
16860Sstevel@tonic-gate 		SSL3_MT_CLIENT_KEY_EXCHANGE,
16870Sstevel@tonic-gate 		2048, /* ??? */
16880Sstevel@tonic-gate 		&ok);
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 	if (!ok) return((int)n);
16910Sstevel@tonic-gate 	p=(unsigned char *)s->init_msg;
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 	l=s->s3->tmp.new_cipher->algorithms;
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
16960Sstevel@tonic-gate 	if (l & SSL_kRSA)
16970Sstevel@tonic-gate 		{
16980Sstevel@tonic-gate 		/* FIX THIS UP EAY EAY EAY EAY */
16990Sstevel@tonic-gate 		if (s->s3->tmp.use_rsa_tmp)
17000Sstevel@tonic-gate 			{
17010Sstevel@tonic-gate 			if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
17020Sstevel@tonic-gate 				rsa=s->cert->rsa_tmp;
17030Sstevel@tonic-gate 			/* Don't do a callback because rsa_tmp should
17040Sstevel@tonic-gate 			 * be sent already */
17050Sstevel@tonic-gate 			if (rsa == NULL)
17060Sstevel@tonic-gate 				{
17070Sstevel@tonic-gate 				al=SSL_AD_HANDSHAKE_FAILURE;
17080Sstevel@tonic-gate 				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
17090Sstevel@tonic-gate 				goto f_err;
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 				}
17120Sstevel@tonic-gate 			}
17130Sstevel@tonic-gate 		else
17140Sstevel@tonic-gate 			{
17150Sstevel@tonic-gate 			pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
17160Sstevel@tonic-gate 			if (	(pkey == NULL) ||
17170Sstevel@tonic-gate 				(pkey->type != EVP_PKEY_RSA) ||
17180Sstevel@tonic-gate 				(pkey->pkey.rsa == NULL))
17190Sstevel@tonic-gate 				{
17200Sstevel@tonic-gate 				al=SSL_AD_HANDSHAKE_FAILURE;
17210Sstevel@tonic-gate 				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
17220Sstevel@tonic-gate 				goto f_err;
17230Sstevel@tonic-gate 				}
17240Sstevel@tonic-gate 			rsa=pkey->pkey.rsa;
17250Sstevel@tonic-gate 			}
17260Sstevel@tonic-gate 
17275434Sjp161948 		/* TLS and [incidentally] DTLS, including pre-0.9.8f */
17285434Sjp161948 		if (s->version > SSL3_VERSION &&
17295434Sjp161948 		    s->client_version != DTLS1_BAD_VER)
17300Sstevel@tonic-gate 			{
17310Sstevel@tonic-gate 			n2s(p,i);
17320Sstevel@tonic-gate 			if (n != i+2)
17330Sstevel@tonic-gate 				{
17340Sstevel@tonic-gate 				if (!(s->options & SSL_OP_TLS_D5_BUG))
17350Sstevel@tonic-gate 					{
17360Sstevel@tonic-gate 					SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
17370Sstevel@tonic-gate 					goto err;
17380Sstevel@tonic-gate 					}
17390Sstevel@tonic-gate 				else
17400Sstevel@tonic-gate 					p-=2;
17410Sstevel@tonic-gate 				}
17420Sstevel@tonic-gate 			else
17430Sstevel@tonic-gate 				n=i;
17440Sstevel@tonic-gate 			}
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate 		i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate 		al = -1;
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 		if (i != SSL_MAX_MASTER_KEY_LENGTH)
17510Sstevel@tonic-gate 			{
17520Sstevel@tonic-gate 			al=SSL_AD_DECODE_ERROR;
17530Sstevel@tonic-gate 			/* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); */
17540Sstevel@tonic-gate 			}
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 		if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
17570Sstevel@tonic-gate 			{
17580Sstevel@tonic-gate 			/* The premaster secret must contain the same version number as the
17590Sstevel@tonic-gate 			 * ClientHello to detect version rollback attacks (strangely, the
17600Sstevel@tonic-gate 			 * protocol does not offer such protection for DH ciphersuites).
17610Sstevel@tonic-gate 			 * However, buggy clients exist that send the negotiated protocol
17620Sstevel@tonic-gate 			 * version instead if the server does not support the requested
17630Sstevel@tonic-gate 			 * protocol version.
17640Sstevel@tonic-gate 			 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
17650Sstevel@tonic-gate 			if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
17660Sstevel@tonic-gate 				(p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
17670Sstevel@tonic-gate 				{
17680Sstevel@tonic-gate 				al=SSL_AD_DECODE_ERROR;
17690Sstevel@tonic-gate 				/* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate 				/* The Klima-Pokorny-Rosa extension of Bleichenbacher's attack
17720Sstevel@tonic-gate 				 * (http://eprint.iacr.org/2003/052/) exploits the version
17730Sstevel@tonic-gate 				 * number check as a "bad version oracle" -- an alert would
17740Sstevel@tonic-gate 				 * reveal that the plaintext corresponding to some ciphertext
17750Sstevel@tonic-gate 				 * made up by the adversary is properly formatted except
17760Sstevel@tonic-gate 				 * that the version number is wrong.  To avoid such attacks,
17770Sstevel@tonic-gate 				 * we should treat this just like any other decryption error. */
17780Sstevel@tonic-gate 				}
17790Sstevel@tonic-gate 			}
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate 		if (al != -1)
17820Sstevel@tonic-gate 			{
17830Sstevel@tonic-gate 			/* Some decryption failure -- use random value instead as countermeasure
17840Sstevel@tonic-gate 			 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
17850Sstevel@tonic-gate 			 * (see RFC 2246, section 7.4.7.1). */
17860Sstevel@tonic-gate 			ERR_clear_error();
17870Sstevel@tonic-gate 			i = SSL_MAX_MASTER_KEY_LENGTH;
17880Sstevel@tonic-gate 			p[0] = s->client_version >> 8;
17890Sstevel@tonic-gate 			p[1] = s->client_version & 0xff;
17902139Sjp161948 			if (RAND_pseudo_bytes(p+2, i-2) <= 0) /* should be RAND_bytes, but we cannot work around a failure */
17912139Sjp161948 				goto err;
17920Sstevel@tonic-gate 			}
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 		s->session->master_key_length=
17950Sstevel@tonic-gate 			s->method->ssl3_enc->generate_master_secret(s,
17960Sstevel@tonic-gate 				s->session->master_key,
17970Sstevel@tonic-gate 				p,i);
17980Sstevel@tonic-gate 		OPENSSL_cleanse(p,i);
17990Sstevel@tonic-gate 		}
18000Sstevel@tonic-gate 	else
18010Sstevel@tonic-gate #endif
18020Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
18030Sstevel@tonic-gate 		if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
18040Sstevel@tonic-gate 		{
18050Sstevel@tonic-gate 		n2s(p,i);
18060Sstevel@tonic-gate 		if (n != i+2)
18070Sstevel@tonic-gate 			{
18080Sstevel@tonic-gate 			if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
18090Sstevel@tonic-gate 				{
18100Sstevel@tonic-gate 				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
18110Sstevel@tonic-gate 				goto err;
18120Sstevel@tonic-gate 				}
18130Sstevel@tonic-gate 			else
18140Sstevel@tonic-gate 				{
18150Sstevel@tonic-gate 				p-=2;
18160Sstevel@tonic-gate 				i=(int)n;
18170Sstevel@tonic-gate 				}
18180Sstevel@tonic-gate 			}
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 		if (n == 0L) /* the parameters are in the cert */
18210Sstevel@tonic-gate 			{
18220Sstevel@tonic-gate 			al=SSL_AD_HANDSHAKE_FAILURE;
18230Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
18240Sstevel@tonic-gate 			goto f_err;
18250Sstevel@tonic-gate 			}
18260Sstevel@tonic-gate 		else
18270Sstevel@tonic-gate 			{
18280Sstevel@tonic-gate 			if (s->s3->tmp.dh == NULL)
18290Sstevel@tonic-gate 				{
18300Sstevel@tonic-gate 				al=SSL_AD_HANDSHAKE_FAILURE;
18310Sstevel@tonic-gate 				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
18320Sstevel@tonic-gate 				goto f_err;
18330Sstevel@tonic-gate 				}
18340Sstevel@tonic-gate 			else
18350Sstevel@tonic-gate 				dh_srvr=s->s3->tmp.dh;
18360Sstevel@tonic-gate 			}
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 		pub=BN_bin2bn(p,i,NULL);
18390Sstevel@tonic-gate 		if (pub == NULL)
18400Sstevel@tonic-gate 			{
18410Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
18420Sstevel@tonic-gate 			goto err;
18430Sstevel@tonic-gate 			}
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate 		i=DH_compute_key(p,pub,dh_srvr);
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate 		if (i <= 0)
18480Sstevel@tonic-gate 			{
18490Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
18500Sstevel@tonic-gate 			goto err;
18510Sstevel@tonic-gate 			}
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 		DH_free(s->s3->tmp.dh);
18540Sstevel@tonic-gate 		s->s3->tmp.dh=NULL;
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 		BN_clear_free(pub);
18570Sstevel@tonic-gate 		pub=NULL;
18580Sstevel@tonic-gate 		s->session->master_key_length=
18590Sstevel@tonic-gate 			s->method->ssl3_enc->generate_master_secret(s,
18600Sstevel@tonic-gate 				s->session->master_key,p,i);
18610Sstevel@tonic-gate 		OPENSSL_cleanse(p,i);
18620Sstevel@tonic-gate 		}
18630Sstevel@tonic-gate 	else
18640Sstevel@tonic-gate #endif
18650Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5
18660Sstevel@tonic-gate         if (l & SSL_kKRB5)
18670Sstevel@tonic-gate                 {
18680Sstevel@tonic-gate                 krb5_error_code		krb5rc;
18690Sstevel@tonic-gate 		krb5_data		enc_ticket;
18700Sstevel@tonic-gate 		krb5_data		authenticator;
18710Sstevel@tonic-gate 		krb5_data		enc_pms;
18720Sstevel@tonic-gate                 KSSL_CTX		*kssl_ctx = s->kssl_ctx;
18730Sstevel@tonic-gate 		EVP_CIPHER_CTX		ciph_ctx;
18740Sstevel@tonic-gate 		EVP_CIPHER		*enc = NULL;
18750Sstevel@tonic-gate 		unsigned char		iv[EVP_MAX_IV_LENGTH];
18760Sstevel@tonic-gate 		unsigned char		pms[SSL_MAX_MASTER_KEY_LENGTH
18770Sstevel@tonic-gate                                                + EVP_MAX_BLOCK_LENGTH];
18780Sstevel@tonic-gate 		int                     padl, outl;
18790Sstevel@tonic-gate 		krb5_timestamp		authtime = 0;
18800Sstevel@tonic-gate 		krb5_ticket_times	ttimes;
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 		EVP_CIPHER_CTX_init(&ciph_ctx);
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate                 if (!kssl_ctx)  kssl_ctx = kssl_ctx_new();
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 		n2s(p,i);
18870Sstevel@tonic-gate 		enc_ticket.length = i;
18880Sstevel@tonic-gate 
18892139Sjp161948 		if (n < (int)enc_ticket.length + 6)
18900Sstevel@tonic-gate 			{
18910Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
18920Sstevel@tonic-gate 				SSL_R_DATA_LENGTH_TOO_LONG);
18930Sstevel@tonic-gate 			goto err;
18940Sstevel@tonic-gate 			}
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 		enc_ticket.data = (char *)p;
18970Sstevel@tonic-gate 		p+=enc_ticket.length;
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 		n2s(p,i);
19000Sstevel@tonic-gate 		authenticator.length = i;
19010Sstevel@tonic-gate 
19022139Sjp161948 		if (n < (int)(enc_ticket.length + authenticator.length) + 6)
19030Sstevel@tonic-gate 			{
19040Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
19050Sstevel@tonic-gate 				SSL_R_DATA_LENGTH_TOO_LONG);
19060Sstevel@tonic-gate 			goto err;
19070Sstevel@tonic-gate 			}
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 		authenticator.data = (char *)p;
19100Sstevel@tonic-gate 		p+=authenticator.length;
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 		n2s(p,i);
19130Sstevel@tonic-gate 		enc_pms.length = i;
19140Sstevel@tonic-gate 		enc_pms.data = (char *)p;
19150Sstevel@tonic-gate 		p+=enc_pms.length;
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 		/* Note that the length is checked again below,
19180Sstevel@tonic-gate 		** after decryption
19190Sstevel@tonic-gate 		*/
19200Sstevel@tonic-gate 		if(enc_pms.length > sizeof pms)
19210Sstevel@tonic-gate 			{
19220Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
19230Sstevel@tonic-gate 			       SSL_R_DATA_LENGTH_TOO_LONG);
19240Sstevel@tonic-gate 			goto err;
19250Sstevel@tonic-gate 			}
19260Sstevel@tonic-gate 
19272139Sjp161948 		if (n != (long)(enc_ticket.length + authenticator.length +
19282139Sjp161948 						enc_pms.length + 6))
19290Sstevel@tonic-gate 			{
19300Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
19310Sstevel@tonic-gate 				SSL_R_DATA_LENGTH_TOO_LONG);
19320Sstevel@tonic-gate 			goto err;
19330Sstevel@tonic-gate 			}
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate                 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
19360Sstevel@tonic-gate 					&kssl_err)) != 0)
19370Sstevel@tonic-gate                         {
19380Sstevel@tonic-gate #ifdef KSSL_DEBUG
19390Sstevel@tonic-gate                         printf("kssl_sget_tkt rtn %d [%d]\n",
19400Sstevel@tonic-gate                                 krb5rc, kssl_err.reason);
19410Sstevel@tonic-gate                         if (kssl_err.text)
19420Sstevel@tonic-gate                                 printf("kssl_err text= %s\n", kssl_err.text);
19430Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
19442139Sjp161948                         SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
19450Sstevel@tonic-gate                                 kssl_err.reason);
19460Sstevel@tonic-gate                         goto err;
19470Sstevel@tonic-gate                         }
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 		/*  Note: no authenticator is not considered an error,
19500Sstevel@tonic-gate 		**  but will return authtime == 0.
19510Sstevel@tonic-gate 		*/
19520Sstevel@tonic-gate 		if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
19530Sstevel@tonic-gate 					&authtime, &kssl_err)) != 0)
19540Sstevel@tonic-gate 			{
19550Sstevel@tonic-gate #ifdef KSSL_DEBUG
19560Sstevel@tonic-gate                         printf("kssl_check_authent rtn %d [%d]\n",
19570Sstevel@tonic-gate                                 krb5rc, kssl_err.reason);
19580Sstevel@tonic-gate                         if (kssl_err.text)
19590Sstevel@tonic-gate                                 printf("kssl_err text= %s\n", kssl_err.text);
19600Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
19612139Sjp161948                         SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
19620Sstevel@tonic-gate                                 kssl_err.reason);
19630Sstevel@tonic-gate                         goto err;
19640Sstevel@tonic-gate 			}
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 		if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
19670Sstevel@tonic-gate 			{
19682139Sjp161948 			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
19690Sstevel@tonic-gate                         goto err;
19700Sstevel@tonic-gate 			}
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate #ifdef KSSL_DEBUG
19730Sstevel@tonic-gate                 kssl_ctx_show(kssl_ctx);
19740Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate 		enc = kssl_map_enc(kssl_ctx->enctype);
19770Sstevel@tonic-gate                 if (enc == NULL)
19780Sstevel@tonic-gate                     goto err;
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate 		memset(iv, 0, sizeof iv);	/* per RFC 1510 */
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 		if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))
19830Sstevel@tonic-gate 			{
19840Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
19850Sstevel@tonic-gate 				SSL_R_DECRYPTION_FAILED);
19860Sstevel@tonic-gate 			goto err;
19870Sstevel@tonic-gate 			}
19880Sstevel@tonic-gate 		if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
19890Sstevel@tonic-gate 					(unsigned char *)enc_pms.data, enc_pms.length))
19900Sstevel@tonic-gate 			{
19910Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
19920Sstevel@tonic-gate 				SSL_R_DECRYPTION_FAILED);
19930Sstevel@tonic-gate 			goto err;
19940Sstevel@tonic-gate 			}
19950Sstevel@tonic-gate 		if (outl > SSL_MAX_MASTER_KEY_LENGTH)
19960Sstevel@tonic-gate 			{
19970Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
19980Sstevel@tonic-gate 				SSL_R_DATA_LENGTH_TOO_LONG);
19990Sstevel@tonic-gate 			goto err;
20000Sstevel@tonic-gate 			}
20010Sstevel@tonic-gate 		if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))
20020Sstevel@tonic-gate 			{
20030Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
20040Sstevel@tonic-gate 				SSL_R_DECRYPTION_FAILED);
20050Sstevel@tonic-gate 			goto err;
20060Sstevel@tonic-gate 			}
20070Sstevel@tonic-gate 		outl += padl;
20080Sstevel@tonic-gate 		if (outl > SSL_MAX_MASTER_KEY_LENGTH)
20090Sstevel@tonic-gate 			{
20100Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
20110Sstevel@tonic-gate 				SSL_R_DATA_LENGTH_TOO_LONG);
20120Sstevel@tonic-gate 			goto err;
20130Sstevel@tonic-gate 			}
20140Sstevel@tonic-gate 		EVP_CIPHER_CTX_cleanup(&ciph_ctx);
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate                 s->session->master_key_length=
20170Sstevel@tonic-gate                         s->method->ssl3_enc->generate_master_secret(s,
20180Sstevel@tonic-gate                                 s->session->master_key, pms, outl);
20190Sstevel@tonic-gate 
20200Sstevel@tonic-gate                 if (kssl_ctx->client_princ)
20210Sstevel@tonic-gate                         {
20223018Svk199839 			size_t len = strlen(kssl_ctx->client_princ);
20230Sstevel@tonic-gate                         if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )
20240Sstevel@tonic-gate                                 {
20250Sstevel@tonic-gate                                 s->session->krb5_client_princ_len = len;
20260Sstevel@tonic-gate                                 memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
20270Sstevel@tonic-gate                                 }
20280Sstevel@tonic-gate                         }
20290Sstevel@tonic-gate 
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate                 /*  Was doing kssl_ctx_free() here,
20320Sstevel@tonic-gate 		**  but it caused problems for apache.
20330Sstevel@tonic-gate                 **  kssl_ctx = kssl_ctx_free(kssl_ctx);
20340Sstevel@tonic-gate                 **  if (s->kssl_ctx)  s->kssl_ctx = NULL;
20350Sstevel@tonic-gate                 */
20360Sstevel@tonic-gate                 }
20370Sstevel@tonic-gate 	else
20380Sstevel@tonic-gate #endif	/* OPENSSL_NO_KRB5 */
20392139Sjp161948 
20402139Sjp161948 #ifndef OPENSSL_NO_ECDH
20412139Sjp161948 		if ((l & SSL_kECDH) || (l & SSL_kECDHE))
20422139Sjp161948 		{
20432139Sjp161948 		int ret = 1;
20442139Sjp161948 		int field_size = 0;
20452139Sjp161948 		const EC_KEY   *tkey;
20462139Sjp161948 		const EC_GROUP *group;
20472139Sjp161948 		const BIGNUM *priv_key;
20482139Sjp161948 
20492139Sjp161948                 /* initialize structures for server's ECDH key pair */
20502139Sjp161948 		if ((srvr_ecdh = EC_KEY_new()) == NULL)
20512139Sjp161948 			{
20522139Sjp161948                 	SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
20532139Sjp161948 			    ERR_R_MALLOC_FAILURE);
20542139Sjp161948                 	goto err;
20552139Sjp161948 			}
20562139Sjp161948 
20572139Sjp161948 		/* Let's get server private key and group information */
20582139Sjp161948 		if (l & SSL_kECDH)
20592139Sjp161948 			{
20602139Sjp161948                         /* use the certificate */
20612139Sjp161948 			tkey = s->cert->key->privatekey->pkey.ec;
20622139Sjp161948 			}
20632139Sjp161948 		else
20642139Sjp161948 			{
20652139Sjp161948 			/* use the ephermeral values we saved when
20662139Sjp161948 			 * generating the ServerKeyExchange msg.
20672139Sjp161948 			 */
20682139Sjp161948 			tkey = s->s3->tmp.ecdh;
20692139Sjp161948 			}
20702139Sjp161948 
20712139Sjp161948 		group    = EC_KEY_get0_group(tkey);
20722139Sjp161948 		priv_key = EC_KEY_get0_private_key(tkey);
20732139Sjp161948 
20742139Sjp161948 		if (!EC_KEY_set_group(srvr_ecdh, group) ||
20752139Sjp161948 		    !EC_KEY_set_private_key(srvr_ecdh, priv_key))
20762139Sjp161948 			{
20772139Sjp161948 			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
20782139Sjp161948 			       ERR_R_EC_LIB);
20792139Sjp161948 			goto err;
20802139Sjp161948 			}
20812139Sjp161948 
20822139Sjp161948 		/* Let's get client's public key */
20832139Sjp161948 		if ((clnt_ecpoint = EC_POINT_new(group)) == NULL)
20842139Sjp161948 			{
20852139Sjp161948 			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
20862139Sjp161948 			    ERR_R_MALLOC_FAILURE);
20872139Sjp161948 			goto err;
20882139Sjp161948 			}
20892139Sjp161948 
20902139Sjp161948                 if (n == 0L)
20912139Sjp161948                         {
20922139Sjp161948 			/* Client Publickey was in Client Certificate */
20932139Sjp161948 
20942139Sjp161948 			 if (l & SSL_kECDHE)
20952139Sjp161948 				 {
20962139Sjp161948 				 al=SSL_AD_HANDSHAKE_FAILURE;
20972139Sjp161948 				 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
20982139Sjp161948 				 goto f_err;
20992139Sjp161948 				 }
21002139Sjp161948                         if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))
21012139Sjp161948 			    == NULL) ||
21022139Sjp161948 			    (clnt_pub_pkey->type != EVP_PKEY_EC))
21032139Sjp161948                         	{
21042139Sjp161948 				/* XXX: For now, we do not support client
21052139Sjp161948 				 * authentication using ECDH certificates
21062139Sjp161948 				 * so this branch (n == 0L) of the code is
21072139Sjp161948 				 * never executed. When that support is
21082139Sjp161948 				 * added, we ought to ensure the key
21092139Sjp161948 				 * received in the certificate is
21102139Sjp161948 				 * authorized for key agreement.
21112139Sjp161948 				 * ECDH_compute_key implicitly checks that
21122139Sjp161948 				 * the two ECDH shares are for the same
21132139Sjp161948 				 * group.
21142139Sjp161948 				 */
21152139Sjp161948                            	al=SSL_AD_HANDSHAKE_FAILURE;
21162139Sjp161948                            	SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
21172139Sjp161948 				    SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
21182139Sjp161948                            	goto f_err;
21192139Sjp161948                            	}
21202139Sjp161948 
21212139Sjp161948 			EC_POINT_copy(clnt_ecpoint,
21222139Sjp161948 			    EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec));
21232139Sjp161948                         ret = 2; /* Skip certificate verify processing */
21242139Sjp161948                         }
21252139Sjp161948                 else
21262139Sjp161948                         {
21272139Sjp161948 			/* Get client's public key from encoded point
21282139Sjp161948 			 * in the ClientKeyExchange message.
21292139Sjp161948 			 */
21302139Sjp161948 			if ((bn_ctx = BN_CTX_new()) == NULL)
21312139Sjp161948 				{
21322139Sjp161948 				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
21332139Sjp161948 				    ERR_R_MALLOC_FAILURE);
21342139Sjp161948 				goto err;
21352139Sjp161948 				}
21362139Sjp161948 
21372139Sjp161948                         /* Get encoded point length */
21382139Sjp161948                         i = *p;
21392139Sjp161948 			p += 1;
21402139Sjp161948                         if (EC_POINT_oct2point(group,
21412139Sjp161948 			    clnt_ecpoint, p, i, bn_ctx) == 0)
21422139Sjp161948 				{
21432139Sjp161948 				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
21442139Sjp161948 				    ERR_R_EC_LIB);
21452139Sjp161948 				goto err;
21462139Sjp161948 				}
21472139Sjp161948                         /* p is pointing to somewhere in the buffer
21482139Sjp161948                          * currently, so set it to the start
21492139Sjp161948                          */
21502139Sjp161948                         p=(unsigned char *)s->init_buf->data;
21512139Sjp161948                         }
21522139Sjp161948 
21532139Sjp161948 		/* Compute the shared pre-master secret */
21542139Sjp161948 		field_size = EC_GROUP_get_degree(group);
21552139Sjp161948 		if (field_size <= 0)
21562139Sjp161948 			{
21572139Sjp161948 			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
21582139Sjp161948 			       ERR_R_ECDH_LIB);
21592139Sjp161948 			goto err;
21602139Sjp161948 			}
21612139Sjp161948 		/* If field size is not more than 24 octets, then use SHA-1 hash of result;
21622139Sjp161948 		 * otherwise, use result (see section 4.8 of draft-ietf-tls-ecc-03.txt;
21632139Sjp161948 		 * this is new with this version of the Internet Draft).
21642139Sjp161948 		 */
21652139Sjp161948 		if (field_size <= 24 * 8)
21662139Sjp161948 		    i = ECDH_compute_key(p, KDF1_SHA1_len, clnt_ecpoint, srvr_ecdh, KDF1_SHA1);
21672139Sjp161948 		else
21682139Sjp161948 		    i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL);
21692139Sjp161948                 if (i <= 0)
21702139Sjp161948                         {
21712139Sjp161948                         SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
21722139Sjp161948 			    ERR_R_ECDH_LIB);
21732139Sjp161948                         goto err;
21742139Sjp161948                         }
21752139Sjp161948 
21762139Sjp161948 		EVP_PKEY_free(clnt_pub_pkey);
21772139Sjp161948 		EC_POINT_free(clnt_ecpoint);
21782139Sjp161948 		if (srvr_ecdh != NULL)
21792139Sjp161948 			EC_KEY_free(srvr_ecdh);
21802139Sjp161948 		BN_CTX_free(bn_ctx);
21812139Sjp161948 
21822139Sjp161948 		/* Compute the master secret */
21832139Sjp161948                 s->session->master_key_length = s->method->ssl3_enc-> \
21842139Sjp161948 		    generate_master_secret(s, s->session->master_key, p, i);
21852139Sjp161948 
21862139Sjp161948                 OPENSSL_cleanse(p, i);
21872139Sjp161948                 return (ret);
21882139Sjp161948 		}
21892139Sjp161948 	else
21902139Sjp161948 #endif
21910Sstevel@tonic-gate 		{
21920Sstevel@tonic-gate 		al=SSL_AD_HANDSHAKE_FAILURE;
21930Sstevel@tonic-gate 		SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
21940Sstevel@tonic-gate 				SSL_R_UNKNOWN_CIPHER_TYPE);
21950Sstevel@tonic-gate 		goto f_err;
21960Sstevel@tonic-gate 		}
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate 	return(1);
21990Sstevel@tonic-gate f_err:
22000Sstevel@tonic-gate 	ssl3_send_alert(s,SSL3_AL_FATAL,al);
22012139Sjp161948 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH)
22020Sstevel@tonic-gate err:
22030Sstevel@tonic-gate #endif
22042139Sjp161948 #ifndef OPENSSL_NO_ECDH
22052139Sjp161948 	EVP_PKEY_free(clnt_pub_pkey);
22062139Sjp161948 	EC_POINT_free(clnt_ecpoint);
22072139Sjp161948 	if (srvr_ecdh != NULL)
22082139Sjp161948 		EC_KEY_free(srvr_ecdh);
22092139Sjp161948 	BN_CTX_free(bn_ctx);
22102139Sjp161948 #endif
22110Sstevel@tonic-gate 	return(-1);
22120Sstevel@tonic-gate 	}
22130Sstevel@tonic-gate 
ssl3_get_cert_verify(SSL * s)22142139Sjp161948 int ssl3_get_cert_verify(SSL *s)
22150Sstevel@tonic-gate 	{
22160Sstevel@tonic-gate 	EVP_PKEY *pkey=NULL;
22170Sstevel@tonic-gate 	unsigned char *p;
22180Sstevel@tonic-gate 	int al,ok,ret=0;
22190Sstevel@tonic-gate 	long n;
22200Sstevel@tonic-gate 	int type=0,i,j;
22210Sstevel@tonic-gate 	X509 *peer;
22220Sstevel@tonic-gate 
22232139Sjp161948 	n=s->method->ssl_get_message(s,
22240Sstevel@tonic-gate 		SSL3_ST_SR_CERT_VRFY_A,
22250Sstevel@tonic-gate 		SSL3_ST_SR_CERT_VRFY_B,
22260Sstevel@tonic-gate 		-1,
22270Sstevel@tonic-gate 		514, /* 514? */
22280Sstevel@tonic-gate 		&ok);
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 	if (!ok) return((int)n);
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 	if (s->session->peer != NULL)
22330Sstevel@tonic-gate 		{
22340Sstevel@tonic-gate 		peer=s->session->peer;
22350Sstevel@tonic-gate 		pkey=X509_get_pubkey(peer);
22360Sstevel@tonic-gate 		type=X509_certificate_type(peer,pkey);
22370Sstevel@tonic-gate 		}
22380Sstevel@tonic-gate 	else
22390Sstevel@tonic-gate 		{
22400Sstevel@tonic-gate 		peer=NULL;
22410Sstevel@tonic-gate 		pkey=NULL;
22420Sstevel@tonic-gate 		}
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate 	if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
22450Sstevel@tonic-gate 		{
22460Sstevel@tonic-gate 		s->s3->tmp.reuse_message=1;
22470Sstevel@tonic-gate 		if ((peer != NULL) && (type | EVP_PKT_SIGN))
22480Sstevel@tonic-gate 			{
22490Sstevel@tonic-gate 			al=SSL_AD_UNEXPECTED_MESSAGE;
22500Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
22510Sstevel@tonic-gate 			goto f_err;
22520Sstevel@tonic-gate 			}
22530Sstevel@tonic-gate 		ret=1;
22540Sstevel@tonic-gate 		goto end;
22550Sstevel@tonic-gate 		}
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 	if (peer == NULL)
22580Sstevel@tonic-gate 		{
22590Sstevel@tonic-gate 		SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
22600Sstevel@tonic-gate 		al=SSL_AD_UNEXPECTED_MESSAGE;
22610Sstevel@tonic-gate 		goto f_err;
22620Sstevel@tonic-gate 		}
22630Sstevel@tonic-gate 
22640Sstevel@tonic-gate 	if (!(type & EVP_PKT_SIGN))
22650Sstevel@tonic-gate 		{
22660Sstevel@tonic-gate 		SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
22670Sstevel@tonic-gate 		al=SSL_AD_ILLEGAL_PARAMETER;
22680Sstevel@tonic-gate 		goto f_err;
22690Sstevel@tonic-gate 		}
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 	if (s->s3->change_cipher_spec)
22720Sstevel@tonic-gate 		{
22730Sstevel@tonic-gate 		SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
22740Sstevel@tonic-gate 		al=SSL_AD_UNEXPECTED_MESSAGE;
22750Sstevel@tonic-gate 		goto f_err;
22760Sstevel@tonic-gate 		}
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 	/* we now have a signature that we need to verify */
22790Sstevel@tonic-gate 	p=(unsigned char *)s->init_msg;
22800Sstevel@tonic-gate 	n2s(p,i);
22810Sstevel@tonic-gate 	n-=2;
22820Sstevel@tonic-gate 	if (i > n)
22830Sstevel@tonic-gate 		{
22840Sstevel@tonic-gate 		SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
22850Sstevel@tonic-gate 		al=SSL_AD_DECODE_ERROR;
22860Sstevel@tonic-gate 		goto f_err;
22870Sstevel@tonic-gate 		}
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate 	j=EVP_PKEY_size(pkey);
22900Sstevel@tonic-gate 	if ((i > j) || (n > j) || (n <= 0))
22910Sstevel@tonic-gate 		{
22920Sstevel@tonic-gate 		SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
22930Sstevel@tonic-gate 		al=SSL_AD_DECODE_ERROR;
22940Sstevel@tonic-gate 		goto f_err;
22950Sstevel@tonic-gate 		}
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
22980Sstevel@tonic-gate 	if (pkey->type == EVP_PKEY_RSA)
22990Sstevel@tonic-gate 		{
23000Sstevel@tonic-gate 		i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
23010Sstevel@tonic-gate 			MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
23020Sstevel@tonic-gate 							pkey->pkey.rsa);
23030Sstevel@tonic-gate 		if (i < 0)
23040Sstevel@tonic-gate 			{
23050Sstevel@tonic-gate 			al=SSL_AD_DECRYPT_ERROR;
23060Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
23070Sstevel@tonic-gate 			goto f_err;
23080Sstevel@tonic-gate 			}
23090Sstevel@tonic-gate 		if (i == 0)
23100Sstevel@tonic-gate 			{
23110Sstevel@tonic-gate 			al=SSL_AD_DECRYPT_ERROR;
23120Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
23130Sstevel@tonic-gate 			goto f_err;
23140Sstevel@tonic-gate 			}
23150Sstevel@tonic-gate 		}
23160Sstevel@tonic-gate 	else
23170Sstevel@tonic-gate #endif
23180Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
23190Sstevel@tonic-gate 		if (pkey->type == EVP_PKEY_DSA)
23200Sstevel@tonic-gate 		{
23210Sstevel@tonic-gate 		j=DSA_verify(pkey->save_type,
23220Sstevel@tonic-gate 			&(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
23230Sstevel@tonic-gate 			SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
23240Sstevel@tonic-gate 		if (j <= 0)
23250Sstevel@tonic-gate 			{
23260Sstevel@tonic-gate 			/* bad signature */
23270Sstevel@tonic-gate 			al=SSL_AD_DECRYPT_ERROR;
23280Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
23290Sstevel@tonic-gate 			goto f_err;
23300Sstevel@tonic-gate 			}
23310Sstevel@tonic-gate 		}
23320Sstevel@tonic-gate 	else
23330Sstevel@tonic-gate #endif
23342139Sjp161948 #ifndef OPENSSL_NO_ECDSA
23352139Sjp161948 		if (pkey->type == EVP_PKEY_EC)
23362139Sjp161948 		{
23372139Sjp161948 		j=ECDSA_verify(pkey->save_type,
23382139Sjp161948 			&(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
23392139Sjp161948 			SHA_DIGEST_LENGTH,p,i,pkey->pkey.ec);
23402139Sjp161948 		if (j <= 0)
23412139Sjp161948 			{
23422139Sjp161948 			/* bad signature */
23432139Sjp161948 			al=SSL_AD_DECRYPT_ERROR;
23442139Sjp161948 			SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
23452139Sjp161948 			    SSL_R_BAD_ECDSA_SIGNATURE);
23462139Sjp161948 			goto f_err;
23472139Sjp161948 			}
23482139Sjp161948 		}
23492139Sjp161948 	else
23502139Sjp161948 #endif
23510Sstevel@tonic-gate 		{
23520Sstevel@tonic-gate 		SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
23530Sstevel@tonic-gate 		al=SSL_AD_UNSUPPORTED_CERTIFICATE;
23540Sstevel@tonic-gate 		goto f_err;
23550Sstevel@tonic-gate 		}
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate 	ret=1;
23590Sstevel@tonic-gate 	if (0)
23600Sstevel@tonic-gate 		{
23610Sstevel@tonic-gate f_err:
23620Sstevel@tonic-gate 		ssl3_send_alert(s,SSL3_AL_FATAL,al);
23630Sstevel@tonic-gate 		}
23640Sstevel@tonic-gate end:
23650Sstevel@tonic-gate 	EVP_PKEY_free(pkey);
23660Sstevel@tonic-gate 	return(ret);
23670Sstevel@tonic-gate 	}
23680Sstevel@tonic-gate 
ssl3_get_client_certificate(SSL * s)23692139Sjp161948 int ssl3_get_client_certificate(SSL *s)
23700Sstevel@tonic-gate 	{
23710Sstevel@tonic-gate 	int i,ok,al,ret= -1;
23720Sstevel@tonic-gate 	X509 *x=NULL;
23730Sstevel@tonic-gate 	unsigned long l,nc,llen,n;
23742139Sjp161948 	const unsigned char *p,*q;
23752139Sjp161948 	unsigned char *d;
23760Sstevel@tonic-gate 	STACK_OF(X509) *sk=NULL;
23770Sstevel@tonic-gate 
23782139Sjp161948 	n=s->method->ssl_get_message(s,
23790Sstevel@tonic-gate 		SSL3_ST_SR_CERT_A,
23800Sstevel@tonic-gate 		SSL3_ST_SR_CERT_B,
23810Sstevel@tonic-gate 		-1,
23820Sstevel@tonic-gate 		s->max_cert_list,
23830Sstevel@tonic-gate 		&ok);
23840Sstevel@tonic-gate 
23850Sstevel@tonic-gate 	if (!ok) return((int)n);
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate 	if	(s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
23880Sstevel@tonic-gate 		{
23890Sstevel@tonic-gate 		if (	(s->verify_mode & SSL_VERIFY_PEER) &&
23900Sstevel@tonic-gate 			(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
23910Sstevel@tonic-gate 			{
23920Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
23930Sstevel@tonic-gate 			al=SSL_AD_HANDSHAKE_FAILURE;
23940Sstevel@tonic-gate 			goto f_err;
23950Sstevel@tonic-gate 			}
23960Sstevel@tonic-gate 		/* If tls asked for a client cert, the client must return a 0 list */
23970Sstevel@tonic-gate 		if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
23980Sstevel@tonic-gate 			{
23990Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
24000Sstevel@tonic-gate 			al=SSL_AD_UNEXPECTED_MESSAGE;
24010Sstevel@tonic-gate 			goto f_err;
24020Sstevel@tonic-gate 			}
24030Sstevel@tonic-gate 		s->s3->tmp.reuse_message=1;
24040Sstevel@tonic-gate 		return(1);
24050Sstevel@tonic-gate 		}
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 	if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
24080Sstevel@tonic-gate 		{
24090Sstevel@tonic-gate 		al=SSL_AD_UNEXPECTED_MESSAGE;
24100Sstevel@tonic-gate 		SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
24110Sstevel@tonic-gate 		goto f_err;
24120Sstevel@tonic-gate 		}
24132139Sjp161948 	p=d=(unsigned char *)s->init_msg;
24140Sstevel@tonic-gate 
24150Sstevel@tonic-gate 	if ((sk=sk_X509_new_null()) == NULL)
24160Sstevel@tonic-gate 		{
24170Sstevel@tonic-gate 		SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
24180Sstevel@tonic-gate 		goto err;
24190Sstevel@tonic-gate 		}
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 	n2l3(p,llen);
24220Sstevel@tonic-gate 	if (llen+3 != n)
24230Sstevel@tonic-gate 		{
24240Sstevel@tonic-gate 		al=SSL_AD_DECODE_ERROR;
24250Sstevel@tonic-gate 		SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
24260Sstevel@tonic-gate 		goto f_err;
24270Sstevel@tonic-gate 		}
24280Sstevel@tonic-gate 	for (nc=0; nc<llen; )
24290Sstevel@tonic-gate 		{
24300Sstevel@tonic-gate 		n2l3(p,l);
24310Sstevel@tonic-gate 		if ((l+nc+3) > llen)
24320Sstevel@tonic-gate 			{
24330Sstevel@tonic-gate 			al=SSL_AD_DECODE_ERROR;
24340Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
24350Sstevel@tonic-gate 			goto f_err;
24360Sstevel@tonic-gate 			}
24370Sstevel@tonic-gate 
24380Sstevel@tonic-gate 		q=p;
24390Sstevel@tonic-gate 		x=d2i_X509(NULL,&p,l);
24400Sstevel@tonic-gate 		if (x == NULL)
24410Sstevel@tonic-gate 			{
24420Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
24430Sstevel@tonic-gate 			goto err;
24440Sstevel@tonic-gate 			}
24450Sstevel@tonic-gate 		if (p != (q+l))
24460Sstevel@tonic-gate 			{
24470Sstevel@tonic-gate 			al=SSL_AD_DECODE_ERROR;
24480Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
24490Sstevel@tonic-gate 			goto f_err;
24500Sstevel@tonic-gate 			}
24510Sstevel@tonic-gate 		if (!sk_X509_push(sk,x))
24520Sstevel@tonic-gate 			{
24530Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
24540Sstevel@tonic-gate 			goto err;
24550Sstevel@tonic-gate 			}
24560Sstevel@tonic-gate 		x=NULL;
24570Sstevel@tonic-gate 		nc+=l+3;
24580Sstevel@tonic-gate 		}
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 	if (sk_X509_num(sk) <= 0)
24610Sstevel@tonic-gate 		{
24620Sstevel@tonic-gate 		/* TLS does not mind 0 certs returned */
24630Sstevel@tonic-gate 		if (s->version == SSL3_VERSION)
24640Sstevel@tonic-gate 			{
24650Sstevel@tonic-gate 			al=SSL_AD_HANDSHAKE_FAILURE;
24660Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
24670Sstevel@tonic-gate 			goto f_err;
24680Sstevel@tonic-gate 			}
24690Sstevel@tonic-gate 		/* Fail for TLS only if we required a certificate */
24700Sstevel@tonic-gate 		else if ((s->verify_mode & SSL_VERIFY_PEER) &&
24710Sstevel@tonic-gate 			 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
24720Sstevel@tonic-gate 			{
24730Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
24740Sstevel@tonic-gate 			al=SSL_AD_HANDSHAKE_FAILURE;
24750Sstevel@tonic-gate 			goto f_err;
24760Sstevel@tonic-gate 			}
24770Sstevel@tonic-gate 		}
24780Sstevel@tonic-gate 	else
24790Sstevel@tonic-gate 		{
24800Sstevel@tonic-gate 		i=ssl_verify_cert_chain(s,sk);
2481*8545SJan.Pechanec@Sun.COM 		if (i <= 0)
24820Sstevel@tonic-gate 			{
24830Sstevel@tonic-gate 			al=ssl_verify_alarm_type(s->verify_result);
24840Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
24850Sstevel@tonic-gate 			goto f_err;
24860Sstevel@tonic-gate 			}
24870Sstevel@tonic-gate 		}
24880Sstevel@tonic-gate 
24890Sstevel@tonic-gate 	if (s->session->peer != NULL) /* This should not be needed */
24900Sstevel@tonic-gate 		X509_free(s->session->peer);
24910Sstevel@tonic-gate 	s->session->peer=sk_X509_shift(sk);
24920Sstevel@tonic-gate 	s->session->verify_result = s->verify_result;
24930Sstevel@tonic-gate 
24940Sstevel@tonic-gate 	/* With the current implementation, sess_cert will always be NULL
24950Sstevel@tonic-gate 	 * when we arrive here. */
24960Sstevel@tonic-gate 	if (s->session->sess_cert == NULL)
24970Sstevel@tonic-gate 		{
24980Sstevel@tonic-gate 		s->session->sess_cert = ssl_sess_cert_new();
24990Sstevel@tonic-gate 		if (s->session->sess_cert == NULL)
25000Sstevel@tonic-gate 			{
25010Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
25020Sstevel@tonic-gate 			goto err;
25030Sstevel@tonic-gate 			}
25040Sstevel@tonic-gate 		}
25050Sstevel@tonic-gate 	if (s->session->sess_cert->cert_chain != NULL)
25060Sstevel@tonic-gate 		sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
25070Sstevel@tonic-gate 	s->session->sess_cert->cert_chain=sk;
25080Sstevel@tonic-gate 	/* Inconsistency alert: cert_chain does *not* include the
25090Sstevel@tonic-gate 	 * peer's own certificate, while we do include it in s3_clnt.c */
25100Sstevel@tonic-gate 
25110Sstevel@tonic-gate 	sk=NULL;
25120Sstevel@tonic-gate 
25130Sstevel@tonic-gate 	ret=1;
25140Sstevel@tonic-gate 	if (0)
25150Sstevel@tonic-gate 		{
25160Sstevel@tonic-gate f_err:
25170Sstevel@tonic-gate 		ssl3_send_alert(s,SSL3_AL_FATAL,al);
25180Sstevel@tonic-gate 		}
25190Sstevel@tonic-gate err:
25200Sstevel@tonic-gate 	if (x != NULL) X509_free(x);
25210Sstevel@tonic-gate 	if (sk != NULL) sk_X509_pop_free(sk,X509_free);
25220Sstevel@tonic-gate 	return(ret);
25230Sstevel@tonic-gate 	}
25240Sstevel@tonic-gate 
ssl3_send_server_certificate(SSL * s)25250Sstevel@tonic-gate int ssl3_send_server_certificate(SSL *s)
25260Sstevel@tonic-gate 	{
25270Sstevel@tonic-gate 	unsigned long l;
25280Sstevel@tonic-gate 	X509 *x;
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 	if (s->state == SSL3_ST_SW_CERT_A)
25310Sstevel@tonic-gate 		{
25320Sstevel@tonic-gate 		x=ssl_get_server_send_cert(s);
25330Sstevel@tonic-gate 		if (x == NULL &&
25340Sstevel@tonic-gate                         /* VRS: allow null cert if auth == KRB5 */
25350Sstevel@tonic-gate                         (s->s3->tmp.new_cipher->algorithms
25360Sstevel@tonic-gate                                 & (SSL_MKEY_MASK|SSL_AUTH_MASK))
25370Sstevel@tonic-gate                         != (SSL_aKRB5|SSL_kKRB5))
25380Sstevel@tonic-gate 			{
25390Sstevel@tonic-gate 			SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
25400Sstevel@tonic-gate 			return(0);
25410Sstevel@tonic-gate 			}
25420Sstevel@tonic-gate 
25430Sstevel@tonic-gate 		l=ssl3_output_cert_chain(s,x);
25440Sstevel@tonic-gate 		s->state=SSL3_ST_SW_CERT_B;
25450Sstevel@tonic-gate 		s->init_num=(int)l;
25460Sstevel@tonic-gate 		s->init_off=0;
25470Sstevel@tonic-gate 		}
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate 	/* SSL3_ST_SW_CERT_B */
25500Sstevel@tonic-gate 	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
25510Sstevel@tonic-gate 	}
25522139Sjp161948 
25532139Sjp161948 
25542139Sjp161948 #ifndef OPENSSL_NO_ECDH
25552139Sjp161948 /* This is the complement of curve_id2nid in s3_clnt.c. */
nid2curve_id(int nid)25562139Sjp161948 static int nid2curve_id(int nid)
25572139Sjp161948 {
25582139Sjp161948 	/* ECC curves from draft-ietf-tls-ecc-01.txt (Mar 15, 2001)
25592139Sjp161948 	 * (no changes in draft-ietf-tls-ecc-03.txt [June 2003]) */
25602139Sjp161948 	switch (nid) {
25612139Sjp161948 	case NID_sect163k1: /* sect163k1 (1) */
25622139Sjp161948 		return 1;
25632139Sjp161948 	case NID_sect163r1: /* sect163r1 (2) */
25642139Sjp161948 		return 2;
25652139Sjp161948 	case NID_sect163r2: /* sect163r2 (3) */
25662139Sjp161948 		return 3;
25672139Sjp161948 	case NID_sect193r1: /* sect193r1 (4) */
25682139Sjp161948 		return 4;
25692139Sjp161948 	case NID_sect193r2: /* sect193r2 (5) */
25702139Sjp161948 		return 5;
25712139Sjp161948 	case NID_sect233k1: /* sect233k1 (6) */
25722139Sjp161948 		return 6;
25732139Sjp161948 	case NID_sect233r1: /* sect233r1 (7) */
25742139Sjp161948 		return 7;
25752139Sjp161948 	case NID_sect239k1: /* sect239k1 (8) */
25762139Sjp161948 		return 8;
25772139Sjp161948 	case NID_sect283k1: /* sect283k1 (9) */
25782139Sjp161948 		return 9;
25792139Sjp161948 	case NID_sect283r1: /* sect283r1 (10) */
25802139Sjp161948 		return 10;
25812139Sjp161948 	case NID_sect409k1: /* sect409k1 (11) */
25822139Sjp161948 		return 11;
25832139Sjp161948 	case NID_sect409r1: /* sect409r1 (12) */
25842139Sjp161948 		return 12;
25852139Sjp161948 	case NID_sect571k1: /* sect571k1 (13) */
25862139Sjp161948 		return 13;
25872139Sjp161948 	case NID_sect571r1: /* sect571r1 (14) */
25882139Sjp161948 		return 14;
25892139Sjp161948 	case NID_secp160k1: /* secp160k1 (15) */
25902139Sjp161948 		return 15;
25912139Sjp161948 	case NID_secp160r1: /* secp160r1 (16) */
25922139Sjp161948 		return 16;
25932139Sjp161948 	case NID_secp160r2: /* secp160r2 (17) */
25942139Sjp161948 		return 17;
25952139Sjp161948 	case NID_secp192k1: /* secp192k1 (18) */
25962139Sjp161948 		return 18;
25972139Sjp161948 	case NID_X9_62_prime192v1: /* secp192r1 (19) */
25982139Sjp161948 		return 19;
25992139Sjp161948 	case NID_secp224k1: /* secp224k1 (20) */
26002139Sjp161948 		return 20;
26012139Sjp161948 	case NID_secp224r1: /* secp224r1 (21) */
26022139Sjp161948 		return 21;
26032139Sjp161948 	case NID_secp256k1: /* secp256k1 (22) */
26042139Sjp161948 		return 22;
26052139Sjp161948 	case NID_X9_62_prime256v1: /* secp256r1 (23) */
26062139Sjp161948 		return 23;
26072139Sjp161948 	case NID_secp384r1: /* secp384r1 (24) */
26082139Sjp161948 		return 24;
26092139Sjp161948 	case NID_secp521r1:  /* secp521r1 (25) */
26102139Sjp161948 		return 25;
26112139Sjp161948 	default:
26122139Sjp161948 		return 0;
26132139Sjp161948 	}
26142139Sjp161948 }
26152139Sjp161948 #endif
2616