os/ossrv/ssl/libssl/src/s3_srvr.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libssl/src/s3_srvr.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,2638 @@
     1.4 +/* ssl/s3_srvr.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +/* ====================================================================
    1.62 + * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
    1.63 + *
    1.64 + * Redistribution and use in source and binary forms, with or without
    1.65 + * modification, are permitted provided that the following conditions
    1.66 + * are met:
    1.67 + *
    1.68 + * 1. Redistributions of source code must retain the above copyright
    1.69 + *    notice, this list of conditions and the following disclaimer. 
    1.70 + *
    1.71 + * 2. Redistributions in binary form must reproduce the above copyright
    1.72 + *    notice, this list of conditions and the following disclaimer in
    1.73 + *    the documentation and/or other materials provided with the
    1.74 + *    distribution.
    1.75 + *
    1.76 + * 3. All advertising materials mentioning features or use of this
    1.77 + *    software must display the following acknowledgment:
    1.78 + *    "This product includes software developed by the OpenSSL Project
    1.79 + *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
    1.80 + *
    1.81 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
    1.82 + *    endorse or promote products derived from this software without
    1.83 + *    prior written permission. For written permission, please contact
    1.84 + *    openssl-core@openssl.org.
    1.85 + *
    1.86 + * 5. Products derived from this software may not be called "OpenSSL"
    1.87 + *    nor may "OpenSSL" appear in their names without prior written
    1.88 + *    permission of the OpenSSL Project.
    1.89 + *
    1.90 + * 6. Redistributions of any form whatsoever must retain the following
    1.91 + *    acknowledgment:
    1.92 + *    "This product includes software developed by the OpenSSL Project
    1.93 + *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
    1.94 + *
    1.95 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
    1.96 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.97 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.98 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
    1.99 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   1.100 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   1.101 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   1.102 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   1.103 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   1.104 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   1.105 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   1.106 + * OF THE POSSIBILITY OF SUCH DAMAGE.
   1.107 + * ====================================================================
   1.108 + *
   1.109 + * This product includes cryptographic software written by Eric Young
   1.110 + * (eay@cryptsoft.com).  This product includes software written by Tim
   1.111 + * Hudson (tjh@cryptsoft.com).
   1.112 + *
   1.113 + */
   1.114 +/* ====================================================================
   1.115 + * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
   1.116 + *
   1.117 + * Portions of the attached software ("Contribution") are developed by 
   1.118 + * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
   1.119 + *
   1.120 + * The Contribution is licensed pursuant to the OpenSSL open source
   1.121 + * license provided above.
   1.122 + *
   1.123 + * ECC cipher suite support in OpenSSL originally written by
   1.124 + * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
   1.125 + *
   1.126 + */
   1.127 +/*
   1.128 + © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
   1.129 + */
   1.130 + 
   1.131 +#define REUSE_CIPHER_BUG
   1.132 +#define NETSCAPE_HANG_BUG
   1.133 +
   1.134 +#include <stdio.h>
   1.135 +#include "ssl_locl.h"
   1.136 +#include "kssl_lcl.h"
   1.137 +#include <openssl/buffer.h>
   1.138 +#include <openssl/rand.h>
   1.139 +#include <openssl/objects.h>
   1.140 +#include <openssl/evp.h>
   1.141 +#include <openssl/hmac.h>
   1.142 +#include <openssl/x509.h>
   1.143 +#ifndef OPENSSL_NO_DH
   1.144 +#include <openssl/dh.h>
   1.145 +#endif
   1.146 +#include <openssl/bn.h>
   1.147 +#ifndef OPENSSL_NO_KRB5
   1.148 +#include <openssl/krb5_asn.h>
   1.149 +#endif
   1.150 +#include <openssl/md5.h>
   1.151 +
   1.152 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
   1.153 +#include "libssl_wsd.h"
   1.154 +#endif
   1.155 +
   1.156 +#ifdef EMULATOR
   1.157 +
   1.158 +	GET_STATIC_VAR_FROM_TLS(SSLv3_server_method_data,s3_srvr,SSL_METHOD)
   1.159 +	
   1.160 +	#define SSLv3_server_method_data (*GET_WSD_VAR_NAME(SSLv3_server_method_data,s3_srvr,s)())
   1.161 +	
   1.162 +#endif
   1.163 +
   1.164 +static SSL_METHOD *ssl3_get_server_method(int ver);
   1.165 +
   1.166 +#ifndef OPENSSL_NO_ECDH
   1.167 +static int nid2curve_id(int nid);
   1.168 +#endif
   1.169 +
   1.170 +static SSL_METHOD *ssl3_get_server_method(int ver)
   1.171 +	{
   1.172 +	if (ver == SSL3_VERSION)
   1.173 +		return(SSLv3_server_method());
   1.174 +	else
   1.175 +		return(NULL);
   1.176 +	}
   1.177 +
   1.178 +EXPORT_C IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
   1.179 +			ssl3_accept,
   1.180 +			ssl_undefined_function,
   1.181 +			ssl3_get_server_method)
   1.182 +
   1.183 +int ssl3_accept(SSL *s)
   1.184 +	{
   1.185 +	BUF_MEM *buf;
   1.186 +	unsigned long l,Time=(unsigned long)time(NULL);
   1.187 +	void (*cb)(const SSL *ssl,int type,int val)=NULL;
   1.188 +	long num1;
   1.189 +	int ret= -1;
   1.190 +	int new_state,state,skip=0;
   1.191 +
   1.192 +	RAND_add(&Time,sizeof(Time),0);
   1.193 +	ERR_clear_error();
   1.194 +	clear_sys_error();
   1.195 +
   1.196 +	if (s->info_callback != NULL)
   1.197 +		cb=s->info_callback;
   1.198 +	else if (s->ctx->info_callback != NULL)
   1.199 +		cb=s->ctx->info_callback;
   1.200 +
   1.201 +	/* init things to blank */
   1.202 +	s->in_handshake++;
   1.203 +	if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
   1.204 +
   1.205 +	if (s->cert == NULL)
   1.206 +		{
   1.207 +		SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
   1.208 +		return(-1);
   1.209 +		}
   1.210 +
   1.211 +	for (;;)
   1.212 +		{
   1.213 +		state=s->state;
   1.214 +
   1.215 +		switch (s->state)
   1.216 +			{
   1.217 +		case SSL_ST_RENEGOTIATE:
   1.218 +			s->new_session=1;
   1.219 +			/* s->state=SSL_ST_ACCEPT; */
   1.220 +
   1.221 +		case SSL_ST_BEFORE:
   1.222 +		case SSL_ST_ACCEPT:
   1.223 +		case SSL_ST_BEFORE|SSL_ST_ACCEPT:
   1.224 +		case SSL_ST_OK|SSL_ST_ACCEPT:
   1.225 +
   1.226 +			s->server=1;
   1.227 +			if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
   1.228 +
   1.229 +			if ((s->version>>8) != 3)
   1.230 +				{
   1.231 +				SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
   1.232 +				return -1;
   1.233 +				}
   1.234 +			s->type=SSL_ST_ACCEPT;
   1.235 +
   1.236 +			if (s->init_buf == NULL)
   1.237 +				{
   1.238 +				if ((buf=BUF_MEM_new()) == NULL)
   1.239 +					{
   1.240 +					ret= -1;
   1.241 +					goto end;
   1.242 +					}
   1.243 +				if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
   1.244 +					{
   1.245 +					ret= -1;
   1.246 +					goto end;
   1.247 +					}
   1.248 +				s->init_buf=buf;
   1.249 +				}
   1.250 +
   1.251 +			if (!ssl3_setup_buffers(s))
   1.252 +				{
   1.253 +				ret= -1;
   1.254 +				goto end;
   1.255 +				}
   1.256 +
   1.257 +			s->init_num=0;
   1.258 +
   1.259 +			if (s->state != SSL_ST_RENEGOTIATE)
   1.260 +				{
   1.261 +				/* Ok, we now need to push on a buffering BIO so that
   1.262 +				 * the output is sent in a way that TCP likes :-)
   1.263 +				 */
   1.264 +				if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
   1.265 +				
   1.266 +				ssl3_init_finished_mac(s);
   1.267 +				s->state=SSL3_ST_SR_CLNT_HELLO_A;
   1.268 +				s->ctx->stats.sess_accept++;
   1.269 +				}
   1.270 +			else
   1.271 +				{
   1.272 +				/* s->state == SSL_ST_RENEGOTIATE,
   1.273 +				 * we will just send a HelloRequest */
   1.274 +				s->ctx->stats.sess_accept_renegotiate++;
   1.275 +				s->state=SSL3_ST_SW_HELLO_REQ_A;
   1.276 +				}
   1.277 +			break;
   1.278 +
   1.279 +		case SSL3_ST_SW_HELLO_REQ_A:
   1.280 +		case SSL3_ST_SW_HELLO_REQ_B:
   1.281 +
   1.282 +			s->shutdown=0;
   1.283 +			ret=ssl3_send_hello_request(s);
   1.284 +			if (ret <= 0) goto end;
   1.285 +			s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
   1.286 +			s->state=SSL3_ST_SW_FLUSH;
   1.287 +			s->init_num=0;
   1.288 +
   1.289 +			ssl3_init_finished_mac(s);
   1.290 +			break;
   1.291 +
   1.292 +		case SSL3_ST_SW_HELLO_REQ_C:
   1.293 +			s->state=SSL_ST_OK;
   1.294 +			break;
   1.295 +
   1.296 +		case SSL3_ST_SR_CLNT_HELLO_A:
   1.297 +		case SSL3_ST_SR_CLNT_HELLO_B:
   1.298 +		case SSL3_ST_SR_CLNT_HELLO_C:
   1.299 +
   1.300 +			s->shutdown=0;
   1.301 +			ret=ssl3_get_client_hello(s);
   1.302 +			if (ret <= 0) goto end;
   1.303 +			s->new_session = 2;
   1.304 +			s->state=SSL3_ST_SW_SRVR_HELLO_A;
   1.305 +			s->init_num=0;
   1.306 +			break;
   1.307 +
   1.308 +		case SSL3_ST_SW_SRVR_HELLO_A:
   1.309 +		case SSL3_ST_SW_SRVR_HELLO_B:
   1.310 +			ret=ssl3_send_server_hello(s);
   1.311 +			if (ret <= 0) goto end;
   1.312 +
   1.313 +			if (s->hit)
   1.314 +				s->state=SSL3_ST_SW_CHANGE_A;
   1.315 +			else
   1.316 +				s->state=SSL3_ST_SW_CERT_A;
   1.317 +			s->init_num=0;
   1.318 +			break;
   1.319 +
   1.320 +		case SSL3_ST_SW_CERT_A:
   1.321 +		case SSL3_ST_SW_CERT_B:
   1.322 +			/* Check if it is anon DH or anon ECDH or KRB5 */
   1.323 +			if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)
   1.324 +				&& !(s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))
   1.325 +				{
   1.326 +				ret=ssl3_send_server_certificate(s);
   1.327 +				if (ret <= 0) goto end;
   1.328 +				}
   1.329 +			else
   1.330 +				skip=1;
   1.331 +			s->state=SSL3_ST_SW_KEY_EXCH_A;
   1.332 +			s->init_num=0;
   1.333 +			break;
   1.334 +
   1.335 +		case SSL3_ST_SW_KEY_EXCH_A:
   1.336 +		case SSL3_ST_SW_KEY_EXCH_B:
   1.337 +			l=s->s3->tmp.new_cipher->algorithms;
   1.338 +
   1.339 +			/* clear this, it may get reset by
   1.340 +			 * send_server_key_exchange */
   1.341 +			if ((s->options & SSL_OP_EPHEMERAL_RSA)
   1.342 +#ifndef OPENSSL_NO_KRB5
   1.343 +				&& !(l & SSL_KRB5)
   1.344 +#endif /* OPENSSL_NO_KRB5 */
   1.345 +				)
   1.346 +				/* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
   1.347 +				 * even when forbidden by protocol specs
   1.348 +				 * (handshake may fail as clients are not required to
   1.349 +				 * be able to handle this) */
   1.350 +				s->s3->tmp.use_rsa_tmp=1;
   1.351 +			else
   1.352 +				s->s3->tmp.use_rsa_tmp=0;
   1.353 +
   1.354 +
   1.355 +			/* only send if a DH key exchange, fortezza or
   1.356 +			 * RSA but we have a sign only certificate
   1.357 +			 *
   1.358 +			 * For ECC ciphersuites, we send a serverKeyExchange
   1.359 +			 * message only if the cipher suite is either
   1.360 +			 * ECDH-anon or ECDHE. In other cases, the
   1.361 +			 * server certificate contains the server's 
   1.362 +			 * public key for key exchange.
   1.363 +			 */
   1.364 +			if (s->s3->tmp.use_rsa_tmp
   1.365 +			    || (l & SSL_kECDHE)
   1.366 +			    || (l & (SSL_DH|SSL_kFZA))
   1.367 +			    || ((l & SSL_kRSA)
   1.368 +				&& (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
   1.369 +				    || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
   1.370 +					&& EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
   1.371 +					)
   1.372 +				    )
   1.373 +				)
   1.374 +			    )
   1.375 +				{
   1.376 +				ret=ssl3_send_server_key_exchange(s);
   1.377 +				if (ret <= 0) goto end;
   1.378 +				}
   1.379 +			else
   1.380 +				skip=1;
   1.381 +
   1.382 +			s->state=SSL3_ST_SW_CERT_REQ_A;
   1.383 +			s->init_num=0;
   1.384 +			break;
   1.385 +
   1.386 +		case SSL3_ST_SW_CERT_REQ_A:
   1.387 +		case SSL3_ST_SW_CERT_REQ_B:
   1.388 +			if (/* don't request cert unless asked for it: */
   1.389 +				!(s->verify_mode & SSL_VERIFY_PEER) ||
   1.390 +				/* if SSL_VERIFY_CLIENT_ONCE is set,
   1.391 +				 * don't request cert during re-negotiation: */
   1.392 +				((s->session->peer != NULL) &&
   1.393 +				 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
   1.394 +				/* never request cert in anonymous ciphersuites
   1.395 +				 * (see section "Certificate request" in SSL 3 drafts
   1.396 +				 * and in RFC 2246): */
   1.397 +				((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
   1.398 +				 /* ... except when the application insists on verification
   1.399 +				  * (against the specs, but s3_clnt.c accepts this for SSL 3) */
   1.400 +				 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
   1.401 +                                 /* never request cert in Kerberos ciphersuites */
   1.402 +                                (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))
   1.403 +				{
   1.404 +				/* no cert request */
   1.405 +				skip=1;
   1.406 +				s->s3->tmp.cert_request=0;
   1.407 +				s->state=SSL3_ST_SW_SRVR_DONE_A;
   1.408 +				}
   1.409 +			else
   1.410 +				{
   1.411 +				s->s3->tmp.cert_request=1;
   1.412 +				ret=ssl3_send_certificate_request(s);
   1.413 +				if (ret <= 0) goto end;
   1.414 +#ifndef NETSCAPE_HANG_BUG
   1.415 +				s->state=SSL3_ST_SW_SRVR_DONE_A;
   1.416 +#else
   1.417 +				s->state=SSL3_ST_SW_FLUSH;
   1.418 +				s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
   1.419 +#endif
   1.420 +				s->init_num=0;
   1.421 +				}
   1.422 +			break;
   1.423 +
   1.424 +		case SSL3_ST_SW_SRVR_DONE_A:
   1.425 +		case SSL3_ST_SW_SRVR_DONE_B:
   1.426 +			ret=ssl3_send_server_done(s);
   1.427 +			if (ret <= 0) goto end;
   1.428 +			s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
   1.429 +			s->state=SSL3_ST_SW_FLUSH;
   1.430 +			s->init_num=0;
   1.431 +			break;
   1.432 +		
   1.433 +		case SSL3_ST_SW_FLUSH:
   1.434 +			/* number of bytes to be flushed */
   1.435 +			num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
   1.436 +			if (num1 > 0)
   1.437 +				{
   1.438 +				s->rwstate=SSL_WRITING;
   1.439 +				num1=BIO_flush(s->wbio);
   1.440 +				if (num1 <= 0) { ret= -1; goto end; }
   1.441 +				s->rwstate=SSL_NOTHING;
   1.442 +				}
   1.443 +
   1.444 +			s->state=s->s3->tmp.next_state;
   1.445 +			break;
   1.446 +
   1.447 +		case SSL3_ST_SR_CERT_A:
   1.448 +		case SSL3_ST_SR_CERT_B:
   1.449 +			/* Check for second client hello (MS SGC) */
   1.450 +			ret = ssl3_check_client_hello(s);
   1.451 +			if (ret <= 0)
   1.452 +				goto end;
   1.453 +			if (ret == 2)
   1.454 +				s->state = SSL3_ST_SR_CLNT_HELLO_C;
   1.455 +			else {
   1.456 +				if (s->s3->tmp.cert_request)
   1.457 +					{
   1.458 +					ret=ssl3_get_client_certificate(s);
   1.459 +					if (ret <= 0) goto end;
   1.460 +					}
   1.461 +				s->init_num=0;
   1.462 +				s->state=SSL3_ST_SR_KEY_EXCH_A;
   1.463 +			}
   1.464 +			break;
   1.465 +
   1.466 +		case SSL3_ST_SR_KEY_EXCH_A:
   1.467 +		case SSL3_ST_SR_KEY_EXCH_B:
   1.468 +			ret=ssl3_get_client_key_exchange(s);
   1.469 +			if (ret <= 0) 
   1.470 +				goto end;
   1.471 +			if (ret == 2)
   1.472 +				{
   1.473 +				/* For the ECDH ciphersuites when
   1.474 +				 * the client sends its ECDH pub key in
   1.475 +				 * a certificate, the CertificateVerify
   1.476 +				 * message is not sent.
   1.477 +				 */
   1.478 +				s->state=SSL3_ST_SR_FINISHED_A;
   1.479 +				s->init_num = 0;
   1.480 +				}
   1.481 +			else   
   1.482 +				{
   1.483 +				s->state=SSL3_ST_SR_CERT_VRFY_A;
   1.484 +				s->init_num=0;
   1.485 +
   1.486 +				/* We need to get hashes here so if there is
   1.487 +				 * a client cert, it can be verified
   1.488 +				 */ 
   1.489 +				s->method->ssl3_enc->cert_verify_mac(s,
   1.490 +				    &(s->s3->finish_dgst1),
   1.491 +				    &(s->s3->tmp.cert_verify_md[0]));
   1.492 +				s->method->ssl3_enc->cert_verify_mac(s,
   1.493 +				    &(s->s3->finish_dgst2),
   1.494 +				    &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
   1.495 +				}
   1.496 +			break;
   1.497 +
   1.498 +		case SSL3_ST_SR_CERT_VRFY_A:
   1.499 +		case SSL3_ST_SR_CERT_VRFY_B:
   1.500 +
   1.501 +			/* we should decide if we expected this one */
   1.502 +			ret=ssl3_get_cert_verify(s);
   1.503 +			if (ret <= 0) goto end;
   1.504 +
   1.505 +			s->state=SSL3_ST_SR_FINISHED_A;
   1.506 +			s->init_num=0;
   1.507 +			break;
   1.508 +
   1.509 +		case SSL3_ST_SR_FINISHED_A:
   1.510 +		case SSL3_ST_SR_FINISHED_B:
   1.511 +			ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
   1.512 +				SSL3_ST_SR_FINISHED_B);
   1.513 +			if (ret <= 0) goto end;
   1.514 +			if (s->hit)
   1.515 +				s->state=SSL_ST_OK;
   1.516 +
   1.517 +			else
   1.518 +				s->state=SSL3_ST_SW_CHANGE_A;
   1.519 +			s->init_num=0;
   1.520 +			break;
   1.521 +
   1.522 +
   1.523 +
   1.524 +		case SSL3_ST_SW_CHANGE_A:
   1.525 +		case SSL3_ST_SW_CHANGE_B:
   1.526 +
   1.527 +			s->session->cipher=s->s3->tmp.new_cipher;
   1.528 +			if (!s->method->ssl3_enc->setup_key_block(s))
   1.529 +				{ ret= -1; goto end; }
   1.530 +
   1.531 +			ret=ssl3_send_change_cipher_spec(s,
   1.532 +				SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
   1.533 +
   1.534 +			if (ret <= 0) goto end;
   1.535 +			s->state=SSL3_ST_SW_FINISHED_A;
   1.536 +			s->init_num=0;
   1.537 +
   1.538 +			if (!s->method->ssl3_enc->change_cipher_state(s,
   1.539 +				SSL3_CHANGE_CIPHER_SERVER_WRITE))
   1.540 +				{
   1.541 +				ret= -1;
   1.542 +				goto end;
   1.543 +				}
   1.544 +
   1.545 +			break;
   1.546 +
   1.547 +		case SSL3_ST_SW_FINISHED_A:
   1.548 +		case SSL3_ST_SW_FINISHED_B:
   1.549 +			ret=ssl3_send_finished(s,
   1.550 +				SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
   1.551 +				s->method->ssl3_enc->server_finished_label,
   1.552 +				s->method->ssl3_enc->server_finished_label_len);
   1.553 +			if (ret <= 0) goto end;
   1.554 +			s->state=SSL3_ST_SW_FLUSH;
   1.555 +			if (s->hit)
   1.556 +				s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
   1.557 +			else
   1.558 +				s->s3->tmp.next_state=SSL_ST_OK;
   1.559 +			s->init_num=0;
   1.560 +			break;
   1.561 +
   1.562 +		case SSL_ST_OK:
   1.563 +			/* clean a few things up */
   1.564 +			ssl3_cleanup_key_block(s);
   1.565 +
   1.566 +			BUF_MEM_free(s->init_buf);
   1.567 +			s->init_buf=NULL;
   1.568 +
   1.569 +			/* remove buffering on output */
   1.570 +			ssl_free_wbio_buffer(s);
   1.571 +
   1.572 +			s->init_num=0;
   1.573 +
   1.574 +			if (s->new_session == 2) /* skipped if we just sent a HelloRequest */
   1.575 +				{
   1.576 +				/* actually not necessarily a 'new' session unless
   1.577 +				 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
   1.578 +				
   1.579 +				s->new_session=0;
   1.580 +				
   1.581 +				ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
   1.582 +				
   1.583 +				s->ctx->stats.sess_accept_good++;
   1.584 +				/* s->server=1; */
   1.585 +				s->handshake_func=ssl3_accept;
   1.586 +
   1.587 +				if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
   1.588 +				}
   1.589 +			
   1.590 +			ret = 1;
   1.591 +			goto end;
   1.592 +			/* break; */
   1.593 +
   1.594 +		default:
   1.595 +			SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
   1.596 +			ret= -1;
   1.597 +			goto end;
   1.598 +			/* break; */
   1.599 +			}
   1.600 +		
   1.601 +		if (!s->s3->tmp.reuse_message && !skip)
   1.602 +			{
   1.603 +			if (s->debug)
   1.604 +				{
   1.605 +				if ((ret=BIO_flush(s->wbio)) <= 0)
   1.606 +					goto end;
   1.607 +				}
   1.608 +
   1.609 +
   1.610 +			if ((cb != NULL) && (s->state != state))
   1.611 +				{
   1.612 +				new_state=s->state;
   1.613 +				s->state=state;
   1.614 +				cb(s,SSL_CB_ACCEPT_LOOP,1);
   1.615 +				s->state=new_state;
   1.616 +				}
   1.617 +			}
   1.618 +		skip=0;
   1.619 +		}
   1.620 +end:
   1.621 +	/* BIO_flush(s->wbio); */
   1.622 +
   1.623 +	s->in_handshake--;
   1.624 +	if (cb != NULL)
   1.625 +		cb(s,SSL_CB_ACCEPT_EXIT,ret);
   1.626 +	return(ret);
   1.627 +	}
   1.628 +
   1.629 +int ssl3_send_hello_request(SSL *s)
   1.630 +	{
   1.631 +	unsigned char *p;
   1.632 +
   1.633 +	if (s->state == SSL3_ST_SW_HELLO_REQ_A)
   1.634 +		{
   1.635 +		p=(unsigned char *)s->init_buf->data;
   1.636 +		*(p++)=SSL3_MT_HELLO_REQUEST;
   1.637 +		*(p++)=0;
   1.638 +		*(p++)=0;
   1.639 +		*(p++)=0;
   1.640 +
   1.641 +		s->state=SSL3_ST_SW_HELLO_REQ_B;
   1.642 +		/* number of bytes to write */
   1.643 +		s->init_num=4;
   1.644 +		s->init_off=0;
   1.645 +		}
   1.646 +
   1.647 +	/* SSL3_ST_SW_HELLO_REQ_B */
   1.648 +	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
   1.649 +	}
   1.650 +
   1.651 +int ssl3_check_client_hello(SSL *s)
   1.652 +	{
   1.653 +	int ok;
   1.654 +	long n;
   1.655 +
   1.656 +	/* this function is called when we really expect a Certificate message,
   1.657 +	 * so permit appropriate message length */
   1.658 +	n=s->method->ssl_get_message(s,
   1.659 +		SSL3_ST_SR_CERT_A,
   1.660 +		SSL3_ST_SR_CERT_B,
   1.661 +		-1,
   1.662 +		s->max_cert_list,
   1.663 +		&ok);
   1.664 +	if (!ok) return((int)n);
   1.665 +	s->s3->tmp.reuse_message = 1;
   1.666 +	if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
   1.667 +		{
   1.668 +		/* Throw away what we have done so far in the current handshake,
   1.669 +		 * which will now be aborted. (A full SSL_clear would be too much.)
   1.670 +		 * I hope that tmp.dh is the only thing that may need to be cleared
   1.671 +		 * when a handshake is not completed ... */
   1.672 +#ifndef OPENSSL_NO_DH
   1.673 +		if (s->s3->tmp.dh != NULL)
   1.674 +			{
   1.675 +			DH_free(s->s3->tmp.dh);
   1.676 +			s->s3->tmp.dh = NULL;
   1.677 +			}
   1.678 +#endif
   1.679 +		return 2;
   1.680 +		}
   1.681 +	return 1;
   1.682 +}
   1.683 +
   1.684 +int ssl3_get_client_hello(SSL *s)
   1.685 +	{
   1.686 +	int i,j,ok,al,ret= -1;
   1.687 +	unsigned int cookie_len;
   1.688 +	long n;
   1.689 +	unsigned long id;
   1.690 +	unsigned char *p,*d,*q;
   1.691 +	SSL_CIPHER *c;
   1.692 +#ifndef OPENSSL_NO_COMP
   1.693 +	SSL_COMP *comp=NULL;
   1.694 +#endif
   1.695 +	STACK_OF(SSL_CIPHER) *ciphers=NULL;
   1.696 +
   1.697 +	/* We do this so that we will respond with our native type.
   1.698 +	 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
   1.699 +	 * This down switching should be handled by a different method.
   1.700 +	 * If we are SSLv3, we will respond with SSLv3, even if prompted with
   1.701 +	 * TLSv1.
   1.702 +	 */
   1.703 +	if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
   1.704 +		{
   1.705 +		s->state=SSL3_ST_SR_CLNT_HELLO_B;
   1.706 +		}
   1.707 +	s->first_packet=1;
   1.708 +	n=s->method->ssl_get_message(s,
   1.709 +		SSL3_ST_SR_CLNT_HELLO_B,
   1.710 +		SSL3_ST_SR_CLNT_HELLO_C,
   1.711 +		SSL3_MT_CLIENT_HELLO,
   1.712 +		SSL3_RT_MAX_PLAIN_LENGTH,
   1.713 +		&ok);
   1.714 +
   1.715 +	if (!ok) return((int)n);
   1.716 +	s->first_packet=0;
   1.717 +	d=p=(unsigned char *)s->init_msg;
   1.718 +
   1.719 +	/* use version from inside client hello, not from record header
   1.720 +	 * (may differ: see RFC 2246, Appendix E, second paragraph) */
   1.721 +	s->client_version=(((int)p[0])<<8)|(int)p[1];
   1.722 +	p+=2;
   1.723 +
   1.724 +	if ((s->version == DTLS1_VERSION && s->client_version > s->version) ||
   1.725 +	    (s->version != DTLS1_VERSION && s->client_version < s->version))
   1.726 +		{
   1.727 +		SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
   1.728 +		if ((s->client_version>>8) == SSL3_VERSION_MAJOR) 
   1.729 +			{
   1.730 +			/* similar to ssl3_get_record, send alert using remote version number */
   1.731 +			s->version = s->client_version;
   1.732 +			}
   1.733 +		al = SSL_AD_PROTOCOL_VERSION;
   1.734 +		goto f_err;
   1.735 +		}
   1.736 +
   1.737 +	/* load the client random */
   1.738 +	memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
   1.739 +	p+=SSL3_RANDOM_SIZE;
   1.740 +
   1.741 +	/* get the session-id */
   1.742 +	j= *(p++);
   1.743 +
   1.744 +	s->hit=0;
   1.745 +	/* Versions before 0.9.7 always allow session reuse during renegotiation
   1.746 +	 * (i.e. when s->new_session is true), option
   1.747 +	 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7.
   1.748 +	 * Maybe this optional behaviour should always have been the default,
   1.749 +	 * but we cannot safely change the default behaviour (or new applications
   1.750 +	 * might be written that become totally unsecure when compiled with
   1.751 +	 * an earlier library version)
   1.752 +	 */
   1.753 +	if ((s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
   1.754 +		{
   1.755 +		if (!ssl_get_new_session(s,1))
   1.756 +			goto err;
   1.757 +		}
   1.758 +	else
   1.759 +		{
   1.760 +		i=ssl_get_prev_session(s, p, j, d + n);
   1.761 +		if (i == 1)
   1.762 +			{ /* previous session */
   1.763 +			s->hit=1;
   1.764 +			}
   1.765 +		else if (i == -1)
   1.766 +			goto err;
   1.767 +		else /* i == 0 */
   1.768 +			{
   1.769 +			if (!ssl_get_new_session(s,1))
   1.770 +				goto err;
   1.771 +			}
   1.772 +		}
   1.773 +
   1.774 +	p+=j;
   1.775 +
   1.776 +	if (s->version == DTLS1_VERSION)
   1.777 +		{
   1.778 +		/* cookie stuff */
   1.779 +		cookie_len = *(p++);
   1.780 +
   1.781 +		if ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
   1.782 +			s->d1->send_cookie == 0)
   1.783 +			{
   1.784 +			/* HelloVerifyMessage has already been sent */
   1.785 +			if ( cookie_len != s->d1->cookie_len)
   1.786 +				{
   1.787 +				al = SSL_AD_HANDSHAKE_FAILURE;
   1.788 +				SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
   1.789 +				goto f_err;
   1.790 +				}
   1.791 +			}
   1.792 +
   1.793 +		/* 
   1.794 +		 * The ClientHello may contain a cookie even if the
   1.795 +		 * HelloVerify message has not been sent--make sure that it
   1.796 +		 * does not cause an overflow.
   1.797 +		 */
   1.798 +		if ( cookie_len > sizeof(s->d1->rcvd_cookie))
   1.799 +			{
   1.800 +			/* too much data */
   1.801 +			al = SSL_AD_DECODE_ERROR;
   1.802 +			SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
   1.803 +			goto f_err;
   1.804 +			}
   1.805 +
   1.806 +		/* verify the cookie if appropriate option is set. */
   1.807 +		if ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
   1.808 +			cookie_len > 0)
   1.809 +			{
   1.810 +			memcpy(s->d1->rcvd_cookie, p, cookie_len);
   1.811 +
   1.812 +			if ( s->ctx->app_verify_cookie_cb != NULL)
   1.813 +				{
   1.814 +				if ( s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
   1.815 +					cookie_len) == 0)
   1.816 +					{
   1.817 +					al=SSL_AD_HANDSHAKE_FAILURE;
   1.818 +					SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, 
   1.819 +						SSL_R_COOKIE_MISMATCH);
   1.820 +					goto f_err;
   1.821 +					}
   1.822 +				/* else cookie verification succeeded */
   1.823 +				}
   1.824 +			else if ( memcmp(s->d1->rcvd_cookie, s->d1->cookie, 
   1.825 +						  s->d1->cookie_len) != 0) /* default verification */
   1.826 +				{
   1.827 +					al=SSL_AD_HANDSHAKE_FAILURE;
   1.828 +					SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, 
   1.829 +						SSL_R_COOKIE_MISMATCH);
   1.830 +					goto f_err;
   1.831 +				}
   1.832 +			}
   1.833 +
   1.834 +		p += cookie_len;
   1.835 +		}
   1.836 +
   1.837 +	n2s(p,i);
   1.838 +	if ((i == 0) && (j != 0))
   1.839 +		{
   1.840 +		/* we need a cipher if we are not resuming a session */
   1.841 +		al=SSL_AD_ILLEGAL_PARAMETER;
   1.842 +		SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
   1.843 +		goto f_err;
   1.844 +		}
   1.845 +	if ((p+i) >= (d+n))
   1.846 +		{
   1.847 +		/* not enough data */
   1.848 +		al=SSL_AD_DECODE_ERROR;
   1.849 +		SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
   1.850 +		goto f_err;
   1.851 +		}
   1.852 +	if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
   1.853 +		== NULL))
   1.854 +		{
   1.855 +		goto err;
   1.856 +		}
   1.857 +	p+=i;
   1.858 +
   1.859 +	/* If it is a hit, check that the cipher is in the list */
   1.860 +	if ((s->hit) && (i > 0))
   1.861 +		{
   1.862 +		j=0;
   1.863 +		id=s->session->cipher->id;
   1.864 +
   1.865 +#ifdef CIPHER_DEBUG
   1.866 +		printf("client sent %d ciphers\n",sk_num(ciphers));
   1.867 +#endif
   1.868 +		for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
   1.869 +			{
   1.870 +			c=sk_SSL_CIPHER_value(ciphers,i);
   1.871 +#ifdef CIPHER_DEBUG
   1.872 +			printf("client [%2d of %2d]:%s\n",
   1.873 +				i,sk_num(ciphers),SSL_CIPHER_get_name(c));
   1.874 +#endif
   1.875 +			if (c->id == id)
   1.876 +				{
   1.877 +				j=1;
   1.878 +				break;
   1.879 +				}
   1.880 +			}
   1.881 +		if (j == 0)
   1.882 +			{
   1.883 +			if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
   1.884 +				{
   1.885 +				/* Very bad for multi-threading.... */
   1.886 +				s->session->cipher=sk_SSL_CIPHER_value(ciphers, 0);
   1.887 +				}
   1.888 +			else
   1.889 +				{
   1.890 +				/* we need to have the cipher in the cipher
   1.891 +				 * list if we are asked to reuse it */
   1.892 +				al=SSL_AD_ILLEGAL_PARAMETER;
   1.893 +				SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
   1.894 +				goto f_err;
   1.895 +				}
   1.896 +			}
   1.897 +		}
   1.898 +
   1.899 +	/* compression */
   1.900 +	i= *(p++);
   1.901 +	if ((p+i) > (d+n))
   1.902 +		{
   1.903 +		/* not enough data */
   1.904 +		al=SSL_AD_DECODE_ERROR;
   1.905 +		SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
   1.906 +		goto f_err;
   1.907 +		}
   1.908 +	q=p;
   1.909 +	for (j=0; j<i; j++)
   1.910 +		{
   1.911 +		if (p[j] == 0) break;
   1.912 +		}
   1.913 +
   1.914 +	p+=i;
   1.915 +	if (j >= i)
   1.916 +		{
   1.917 +		/* no compress */
   1.918 +		al=SSL_AD_DECODE_ERROR;
   1.919 +		SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
   1.920 +		goto f_err;
   1.921 +		}
   1.922 +
   1.923 +
   1.924 +	/* Worst case, we will use the NULL compression, but if we have other
   1.925 +	 * options, we will now look for them.  We have i-1 compression
   1.926 +	 * algorithms from the client, starting at q. */
   1.927 +	s->s3->tmp.new_compression=NULL;
   1.928 +#ifndef OPENSSL_NO_COMP
   1.929 +	if (s->ctx->comp_methods != NULL)
   1.930 +		{ /* See if we have a match */
   1.931 +		int m,nn,o,v,done=0;
   1.932 +
   1.933 +		nn=sk_SSL_COMP_num(s->ctx->comp_methods);
   1.934 +		for (m=0; m<nn; m++)
   1.935 +			{
   1.936 +			comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
   1.937 +			v=comp->id;
   1.938 +			for (o=0; o<i; o++)
   1.939 +				{
   1.940 +				if (v == q[o])
   1.941 +					{
   1.942 +					done=1;
   1.943 +					break;
   1.944 +					}
   1.945 +				}
   1.946 +			if (done) break;
   1.947 +			}
   1.948 +		if (done)
   1.949 +			s->s3->tmp.new_compression=comp;
   1.950 +		else
   1.951 +			comp=NULL;
   1.952 +		}
   1.953 +#endif
   1.954 +
   1.955 +	/* TLS does not mind if there is extra stuff */
   1.956 +#if 0   /* SSL 3.0 does not mind either, so we should disable this test
   1.957 +         * (was enabled in 0.9.6d through 0.9.6j and 0.9.7 through 0.9.7b,
   1.958 +         * in earlier SSLeay/OpenSSL releases this test existed but was buggy) */
   1.959 +	if (s->version == SSL3_VERSION)
   1.960 +		{
   1.961 +		if (p < (d+n))
   1.962 +			{
   1.963 +			/* wrong number of bytes,
   1.964 +			 * there could be more to follow */
   1.965 +			al=SSL_AD_DECODE_ERROR;
   1.966 +			SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
   1.967 +			goto f_err;
   1.968 +			}
   1.969 +		}
   1.970 +#endif
   1.971 +
   1.972 +	/* Given s->session->ciphers and SSL_get_ciphers, we must
   1.973 +	 * pick a cipher */
   1.974 +
   1.975 +	if (!s->hit)
   1.976 +		{
   1.977 +#ifdef OPENSSL_NO_COMP
   1.978 +		s->session->compress_meth=0;
   1.979 +#else
   1.980 +		s->session->compress_meth=(comp == NULL)?0:comp->id;
   1.981 +#endif
   1.982 +		if (s->session->ciphers != NULL)
   1.983 +			sk_SSL_CIPHER_free(s->session->ciphers);
   1.984 +		s->session->ciphers=ciphers;
   1.985 +		if (ciphers == NULL)
   1.986 +			{
   1.987 +			al=SSL_AD_ILLEGAL_PARAMETER;
   1.988 +			SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
   1.989 +			goto f_err;
   1.990 +			}
   1.991 +		ciphers=NULL;
   1.992 +		c=ssl3_choose_cipher(s,s->session->ciphers,
   1.993 +				     SSL_get_ciphers(s));
   1.994 +
   1.995 +		if (c == NULL)
   1.996 +			{
   1.997 +			al=SSL_AD_HANDSHAKE_FAILURE;
   1.998 +			SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
   1.999 +			goto f_err;
  1.1000 +			}
  1.1001 +		s->s3->tmp.new_cipher=c;
  1.1002 +		}
  1.1003 +	else
  1.1004 +		{
  1.1005 +		/* Session-id reuse */
  1.1006 +#ifdef REUSE_CIPHER_BUG
  1.1007 +		STACK_OF(SSL_CIPHER) *sk;
  1.1008 +		SSL_CIPHER *nc=NULL;
  1.1009 +		SSL_CIPHER *ec=NULL;
  1.1010 +
  1.1011 +		if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
  1.1012 +			{
  1.1013 +			sk=s->session->ciphers;
  1.1014 +			for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
  1.1015 +				{
  1.1016 +				c=sk_SSL_CIPHER_value(sk,i);
  1.1017 +				if (c->algorithms & SSL_eNULL)
  1.1018 +					nc=c;
  1.1019 +				if (SSL_C_IS_EXPORT(c))
  1.1020 +					ec=c;
  1.1021 +				}
  1.1022 +			if (nc != NULL)
  1.1023 +				s->s3->tmp.new_cipher=nc;
  1.1024 +			else if (ec != NULL)
  1.1025 +				s->s3->tmp.new_cipher=ec;
  1.1026 +			else
  1.1027 +				s->s3->tmp.new_cipher=s->session->cipher;
  1.1028 +			}
  1.1029 +		else
  1.1030 +#endif
  1.1031 +		s->s3->tmp.new_cipher=s->session->cipher;
  1.1032 +		}
  1.1033 +	
  1.1034 +	/* we now have the following setup. 
  1.1035 +	 * client_random
  1.1036 +	 * cipher_list 		- our prefered list of ciphers
  1.1037 +	 * ciphers 		- the clients prefered list of ciphers
  1.1038 +	 * compression		- basically ignored right now
  1.1039 +	 * ssl version is set	- sslv3
  1.1040 +	 * s->session		- The ssl session has been setup.
  1.1041 +	 * s->hit		- session reuse flag
  1.1042 +	 * s->tmp.new_cipher	- the new cipher to use.
  1.1043 +	 */
  1.1044 +
  1.1045 +	ret=1;
  1.1046 +	if (0)
  1.1047 +		{
  1.1048 +f_err:
  1.1049 +		ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1.1050 +		}
  1.1051 +err:
  1.1052 +	if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
  1.1053 +	return(ret);
  1.1054 +	}
  1.1055 +
  1.1056 +int ssl3_send_server_hello(SSL *s)
  1.1057 +	{
  1.1058 +	unsigned char *buf;
  1.1059 +	unsigned char *p,*d;
  1.1060 +	int i,sl;
  1.1061 +	unsigned long l,Time;
  1.1062 +
  1.1063 +	if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
  1.1064 +		{
  1.1065 +		buf=(unsigned char *)s->init_buf->data;
  1.1066 +		p=s->s3->server_random;
  1.1067 +		Time=(unsigned long)time(NULL);			/* Time */
  1.1068 +		l2n(Time,p);
  1.1069 +		if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
  1.1070 +			return -1;
  1.1071 +		/* Do the message type and length last */
  1.1072 +		d=p= &(buf[4]);
  1.1073 +
  1.1074 +		*(p++)=s->version>>8;
  1.1075 +		*(p++)=s->version&0xff;
  1.1076 +
  1.1077 +		/* Random stuff */
  1.1078 +		memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
  1.1079 +		p+=SSL3_RANDOM_SIZE;
  1.1080 +
  1.1081 +		/* now in theory we have 3 options to sending back the
  1.1082 +		 * session id.  If it is a re-use, we send back the
  1.1083 +		 * old session-id, if it is a new session, we send
  1.1084 +		 * back the new session-id or we send back a 0 length
  1.1085 +		 * session-id if we want it to be single use.
  1.1086 +		 * Currently I will not implement the '0' length session-id
  1.1087 +		 * 12-Jan-98 - I'll now support the '0' length stuff.
  1.1088 +		 */
  1.1089 +		if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
  1.1090 +			s->session->session_id_length=0;
  1.1091 +
  1.1092 +		sl=s->session->session_id_length;
  1.1093 +		if (sl > (int)sizeof(s->session->session_id))
  1.1094 +			{
  1.1095 +			SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  1.1096 +			return -1;
  1.1097 +			}
  1.1098 +		*(p++)=sl;
  1.1099 +		memcpy(p,s->session->session_id,sl);
  1.1100 +		p+=sl;
  1.1101 +
  1.1102 +		/* put the cipher */
  1.1103 +		i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
  1.1104 +		p+=i;
  1.1105 +
  1.1106 +		/* put the compression method */
  1.1107 +#ifdef OPENSSL_NO_COMP
  1.1108 +			*(p++)=0;
  1.1109 +#else
  1.1110 +		if (s->s3->tmp.new_compression == NULL)
  1.1111 +			*(p++)=0;
  1.1112 +		else
  1.1113 +			*(p++)=s->s3->tmp.new_compression->id;
  1.1114 +#endif
  1.1115 +
  1.1116 +		/* do the header */
  1.1117 +		l=(p-d);
  1.1118 +		d=buf;
  1.1119 +		*(d++)=SSL3_MT_SERVER_HELLO;
  1.1120 +		l2n3(l,d);
  1.1121 +
  1.1122 +		s->state=SSL3_ST_CW_CLNT_HELLO_B;
  1.1123 +		/* number of bytes to write */
  1.1124 +		s->init_num=p-buf;
  1.1125 +		s->init_off=0;
  1.1126 +		}
  1.1127 +
  1.1128 +	/* SSL3_ST_CW_CLNT_HELLO_B */
  1.1129 +	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1.1130 +	}
  1.1131 +
  1.1132 +int ssl3_send_server_done(SSL *s)
  1.1133 +	{
  1.1134 +	unsigned char *p;
  1.1135 +
  1.1136 +	if (s->state == SSL3_ST_SW_SRVR_DONE_A)
  1.1137 +		{
  1.1138 +		p=(unsigned char *)s->init_buf->data;
  1.1139 +
  1.1140 +		/* do the header */
  1.1141 +		*(p++)=SSL3_MT_SERVER_DONE;
  1.1142 +		*(p++)=0;
  1.1143 +		*(p++)=0;
  1.1144 +		*(p++)=0;
  1.1145 +
  1.1146 +		s->state=SSL3_ST_SW_SRVR_DONE_B;
  1.1147 +		/* number of bytes to write */
  1.1148 +		s->init_num=4;
  1.1149 +		s->init_off=0;
  1.1150 +		}
  1.1151 +
  1.1152 +	/* SSL3_ST_CW_CLNT_HELLO_B */
  1.1153 +	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1.1154 +	}
  1.1155 +
  1.1156 +int ssl3_send_server_key_exchange(SSL *s)
  1.1157 +	{
  1.1158 +#ifndef OPENSSL_NO_RSA
  1.1159 +	unsigned char *q;
  1.1160 +	int j,num;
  1.1161 +	RSA *rsa;
  1.1162 +	unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  1.1163 +	unsigned int u;
  1.1164 +#endif
  1.1165 +#ifndef OPENSSL_NO_DH
  1.1166 +	DH *dh=NULL,*dhp;
  1.1167 +#endif
  1.1168 +#ifndef OPENSSL_NO_ECDH
  1.1169 +	EC_KEY *ecdh=NULL, *ecdhp;
  1.1170 +	unsigned char *encodedPoint = NULL;
  1.1171 +	int encodedlen = 0;
  1.1172 +	int curve_id = 0;
  1.1173 +	BN_CTX *bn_ctx = NULL; 
  1.1174 +#endif
  1.1175 +	EVP_PKEY *pkey;
  1.1176 +	unsigned char *p,*d;
  1.1177 +	int al,i;
  1.1178 +	unsigned long type;
  1.1179 +	int n;
  1.1180 +	CERT *cert;
  1.1181 +	BIGNUM *r[4];
  1.1182 +	int nr[4],kn;
  1.1183 +	BUF_MEM *buf;
  1.1184 +	EVP_MD_CTX md_ctx;
  1.1185 +
  1.1186 +	EVP_MD_CTX_init(&md_ctx);
  1.1187 +	if (s->state == SSL3_ST_SW_KEY_EXCH_A)
  1.1188 +		{
  1.1189 +		type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
  1.1190 +		cert=s->cert;
  1.1191 +
  1.1192 +		buf=s->init_buf;
  1.1193 +
  1.1194 +		r[0]=r[1]=r[2]=r[3]=NULL;
  1.1195 +		n=0;
  1.1196 +#ifndef OPENSSL_NO_RSA
  1.1197 +		if (type & SSL_kRSA)
  1.1198 +			{
  1.1199 +			rsa=cert->rsa_tmp;
  1.1200 +			if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
  1.1201 +				{
  1.1202 +				rsa=s->cert->rsa_tmp_cb(s,
  1.1203 +				      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  1.1204 +				      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  1.1205 +				if(rsa == NULL)
  1.1206 +				{
  1.1207 +					al=SSL_AD_HANDSHAKE_FAILURE;
  1.1208 +					SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
  1.1209 +					goto f_err;
  1.1210 +				}
  1.1211 +				RSA_up_ref(rsa);
  1.1212 +				cert->rsa_tmp=rsa;
  1.1213 +				}
  1.1214 +			if (rsa == NULL)
  1.1215 +				{
  1.1216 +				al=SSL_AD_HANDSHAKE_FAILURE;
  1.1217 +				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
  1.1218 +				goto f_err;
  1.1219 +				}
  1.1220 +			r[0]=rsa->n;
  1.1221 +			r[1]=rsa->e;
  1.1222 +			s->s3->tmp.use_rsa_tmp=1;
  1.1223 +			}
  1.1224 +		else
  1.1225 +#endif
  1.1226 +#ifndef OPENSSL_NO_DH
  1.1227 +			if (type & SSL_kEDH)
  1.1228 +			{
  1.1229 +			dhp=cert->dh_tmp;
  1.1230 +			if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
  1.1231 +				dhp=s->cert->dh_tmp_cb(s,
  1.1232 +				      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  1.1233 +				      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  1.1234 +			if (dhp == NULL)
  1.1235 +				{
  1.1236 +				al=SSL_AD_HANDSHAKE_FAILURE;
  1.1237 +				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
  1.1238 +				goto f_err;
  1.1239 +				}
  1.1240 +
  1.1241 +			if (s->s3->tmp.dh != NULL)
  1.1242 +				{
  1.1243 +				DH_free(dh);
  1.1244 +				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1.1245 +				goto err;
  1.1246 +				}
  1.1247 +
  1.1248 +			if ((dh=DHparams_dup(dhp)) == NULL)
  1.1249 +				{
  1.1250 +				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  1.1251 +				goto err;
  1.1252 +				}
  1.1253 +
  1.1254 +			s->s3->tmp.dh=dh;
  1.1255 +			if ((dhp->pub_key == NULL ||
  1.1256 +			     dhp->priv_key == NULL ||
  1.1257 +			     (s->options & SSL_OP_SINGLE_DH_USE)))
  1.1258 +				{
  1.1259 +				if(!DH_generate_key(dh))
  1.1260 +				    {
  1.1261 +				    SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1.1262 +					   ERR_R_DH_LIB);
  1.1263 +				    goto err;
  1.1264 +				    }
  1.1265 +				}
  1.1266 +			else
  1.1267 +				{
  1.1268 +				dh->pub_key=BN_dup(dhp->pub_key);
  1.1269 +				dh->priv_key=BN_dup(dhp->priv_key);
  1.1270 +				if ((dh->pub_key == NULL) ||
  1.1271 +					(dh->priv_key == NULL))
  1.1272 +					{
  1.1273 +					SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  1.1274 +					goto err;
  1.1275 +					}
  1.1276 +				}
  1.1277 +			r[0]=dh->p;
  1.1278 +			r[1]=dh->g;
  1.1279 +			r[2]=dh->pub_key;
  1.1280 +			}
  1.1281 +		else 
  1.1282 +#endif
  1.1283 +#ifndef OPENSSL_NO_ECDH
  1.1284 +			if (type & SSL_kECDHE)
  1.1285 +			{
  1.1286 +			const EC_GROUP *group;
  1.1287 +
  1.1288 +			ecdhp=cert->ecdh_tmp;
  1.1289 +			if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL))
  1.1290 +				{
  1.1291 +				ecdhp=s->cert->ecdh_tmp_cb(s,
  1.1292 +				      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  1.1293 +				      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  1.1294 +				}
  1.1295 +			if (ecdhp == NULL)
  1.1296 +				{
  1.1297 +				al=SSL_AD_HANDSHAKE_FAILURE;
  1.1298 +				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
  1.1299 +				goto f_err;
  1.1300 +				}
  1.1301 +
  1.1302 +			if (s->s3->tmp.ecdh != NULL)
  1.1303 +				{
  1.1304 +				EC_KEY_free(s->s3->tmp.ecdh); 
  1.1305 +				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1.1306 +				goto err;
  1.1307 +				}
  1.1308 +
  1.1309 +			/* Duplicate the ECDH structure. */
  1.1310 +			if (ecdhp == NULL)
  1.1311 +				{
  1.1312 +				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1.1313 +				goto err;
  1.1314 +				}
  1.1315 +			if (!EC_KEY_up_ref(ecdhp))
  1.1316 +				{
  1.1317 +				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1.1318 +				goto err;
  1.1319 +				}
  1.1320 +			ecdh = ecdhp;
  1.1321 +
  1.1322 +			s->s3->tmp.ecdh=ecdh;
  1.1323 +			if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
  1.1324 +			    (EC_KEY_get0_private_key(ecdh) == NULL) ||
  1.1325 +			    (s->options & SSL_OP_SINGLE_ECDH_USE))
  1.1326 +				{
  1.1327 +				if(!EC_KEY_generate_key(ecdh))
  1.1328 +				    {
  1.1329 +				    SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1.1330 +				    goto err;
  1.1331 +				    }
  1.1332 +				}
  1.1333 +
  1.1334 +			if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
  1.1335 +			    (EC_KEY_get0_public_key(ecdh)  == NULL) ||
  1.1336 +			    (EC_KEY_get0_private_key(ecdh) == NULL))
  1.1337 +				{
  1.1338 +				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1.1339 +				goto err;
  1.1340 +				}
  1.1341 +
  1.1342 +			if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  1.1343 +			    (EC_GROUP_get_degree(group) > 163)) 
  1.1344 +				{
  1.1345 +				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  1.1346 +				goto err;
  1.1347 +				}
  1.1348 +
  1.1349 +			/* XXX: For now, we only support ephemeral ECDH
  1.1350 +			 * keys over named (not generic) curves. For 
  1.1351 +			 * supported named curves, curve_id is non-zero.
  1.1352 +			 */
  1.1353 +			if ((curve_id = 
  1.1354 +			    nid2curve_id(EC_GROUP_get_curve_name(group)))
  1.1355 +			    == 0)
  1.1356 +				{
  1.1357 +				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  1.1358 +				goto err;
  1.1359 +				}
  1.1360 +
  1.1361 +			/* Encode the public key.
  1.1362 +			 * First check the size of encoding and
  1.1363 +			 * allocate memory accordingly.
  1.1364 +			 */
  1.1365 +			encodedlen = EC_POINT_point2oct(group, 
  1.1366 +			    EC_KEY_get0_public_key(ecdh),
  1.1367 +			    POINT_CONVERSION_UNCOMPRESSED, 
  1.1368 +			    NULL, 0, NULL);
  1.1369 +
  1.1370 +			encodedPoint = (unsigned char *) 
  1.1371 +			    OPENSSL_malloc(encodedlen*sizeof(unsigned char)); 
  1.1372 +			bn_ctx = BN_CTX_new();
  1.1373 +			if ((encodedPoint == NULL) || (bn_ctx == NULL))
  1.1374 +				{
  1.1375 +				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1.1376 +				goto err;
  1.1377 +				}
  1.1378 +
  1.1379 +
  1.1380 +			encodedlen = EC_POINT_point2oct(group, 
  1.1381 +			    EC_KEY_get0_public_key(ecdh), 
  1.1382 +			    POINT_CONVERSION_UNCOMPRESSED, 
  1.1383 +			    encodedPoint, encodedlen, bn_ctx);
  1.1384 +
  1.1385 +			if (encodedlen == 0) 
  1.1386 +				{
  1.1387 +				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1.1388 +				goto err;
  1.1389 +				}
  1.1390 +
  1.1391 +			BN_CTX_free(bn_ctx);  bn_ctx=NULL;
  1.1392 +
  1.1393 +			/* XXX: For now, we only support named (not 
  1.1394 +			 * generic) curves in ECDH ephemeral key exchanges.
  1.1395 +			 * In this situation, we need four additional bytes
  1.1396 +			 * to encode the entire ServerECDHParams
  1.1397 +			 * structure. 
  1.1398 +			 */
  1.1399 +			n = 4 + encodedlen;
  1.1400 +
  1.1401 +			/* We'll generate the serverKeyExchange message
  1.1402 +			 * explicitly so we can set these to NULLs
  1.1403 +			 */
  1.1404 +			r[0]=NULL;
  1.1405 +			r[1]=NULL;
  1.1406 +			r[2]=NULL;
  1.1407 +			r[3]=NULL;
  1.1408 +			}
  1.1409 +		else 
  1.1410 +#endif /* !OPENSSL_NO_ECDH */
  1.1411 +			{
  1.1412 +			al=SSL_AD_HANDSHAKE_FAILURE;
  1.1413 +			SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  1.1414 +			goto f_err;
  1.1415 +			}
  1.1416 +		for (i=0; r[i] != NULL; i++)
  1.1417 +			{
  1.1418 +			nr[i]=BN_num_bytes(r[i]);
  1.1419 +			n+=2+nr[i];
  1.1420 +			}
  1.1421 +
  1.1422 +		if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
  1.1423 +			{
  1.1424 +			if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
  1.1425 +				== NULL)
  1.1426 +				{
  1.1427 +				al=SSL_AD_DECODE_ERROR;
  1.1428 +				goto f_err;
  1.1429 +				}
  1.1430 +			kn=EVP_PKEY_size(pkey);
  1.1431 +			}
  1.1432 +		else
  1.1433 +			{
  1.1434 +			pkey=NULL;
  1.1435 +			kn=0;
  1.1436 +			}
  1.1437 +
  1.1438 +		if (!BUF_MEM_grow_clean(buf,n+4+kn))
  1.1439 +			{
  1.1440 +			SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
  1.1441 +			goto err;
  1.1442 +			}
  1.1443 +		d=(unsigned char *)s->init_buf->data;
  1.1444 +		p= &(d[4]);
  1.1445 +
  1.1446 +		for (i=0; r[i] != NULL; i++)
  1.1447 +			{
  1.1448 +			s2n(nr[i],p);
  1.1449 +			BN_bn2bin(r[i],p);
  1.1450 +			p+=nr[i];
  1.1451 +			}
  1.1452 +
  1.1453 +#ifndef OPENSSL_NO_ECDH
  1.1454 +		if (type & SSL_kECDHE) 
  1.1455 +			{
  1.1456 +			/* XXX: For now, we only support named (not generic) curves.
  1.1457 +			 * In this situation, the serverKeyExchange message has:
  1.1458 +			 * [1 byte CurveType], [2 byte CurveName]
  1.1459 +			 * [1 byte length of encoded point], followed by
  1.1460 +			 * the actual encoded point itself
  1.1461 +			 */
  1.1462 +			*p = NAMED_CURVE_TYPE;
  1.1463 +			p += 1;
  1.1464 +			*p = 0;
  1.1465 +			p += 1;
  1.1466 +			*p = curve_id;
  1.1467 +			p += 1;
  1.1468 +			*p = encodedlen;
  1.1469 +			p += 1;
  1.1470 +			memcpy((unsigned char*)p, 
  1.1471 +			    (unsigned char *)encodedPoint, 
  1.1472 +			    encodedlen);
  1.1473 +			OPENSSL_free(encodedPoint);
  1.1474 +			p += encodedlen;
  1.1475 +			}
  1.1476 +#endif
  1.1477 +
  1.1478 +		/* not anonymous */
  1.1479 +		if (pkey != NULL)
  1.1480 +			{
  1.1481 +			/* n is the length of the params, they start at &(d[4])
  1.1482 +			 * and p points to the space at the end. */
  1.1483 +#ifndef OPENSSL_NO_RSA
  1.1484 +			if (pkey->type == EVP_PKEY_RSA)
  1.1485 +				{
  1.1486 +				q=md_buf;
  1.1487 +				j=0;
  1.1488 +				for (num=2; num > 0; num--)
  1.1489 +					{
  1.1490 +					EVP_DigestInit_ex(&md_ctx,(num == 2)
  1.1491 +						?s->ctx->md5:s->ctx->sha1, NULL);
  1.1492 +					EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1.1493 +					EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1.1494 +					EVP_DigestUpdate(&md_ctx,&(d[4]),n);
  1.1495 +					EVP_DigestFinal_ex(&md_ctx,q,
  1.1496 +						(unsigned int *)&i);
  1.1497 +					q+=i;
  1.1498 +					j+=i;
  1.1499 +					}
  1.1500 +				if (RSA_sign(NID_md5_sha1, md_buf, j,
  1.1501 +					&(p[2]), &u, pkey->pkey.rsa) <= 0)
  1.1502 +					{
  1.1503 +					SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
  1.1504 +					goto err;
  1.1505 +					}
  1.1506 +				s2n(u,p);
  1.1507 +				n+=u+2;
  1.1508 +				}
  1.1509 +			else
  1.1510 +#endif
  1.1511 +#if !defined(OPENSSL_NO_DSA)
  1.1512 +				if (pkey->type == EVP_PKEY_DSA)
  1.1513 +				{
  1.1514 +				/* lets do DSS */
  1.1515 +				EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
  1.1516 +				EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1.1517 +				EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1.1518 +				EVP_SignUpdate(&md_ctx,&(d[4]),n);
  1.1519 +				if (!EVP_SignFinal(&md_ctx,&(p[2]),
  1.1520 +					(unsigned int *)&i,pkey))
  1.1521 +					{
  1.1522 +					SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
  1.1523 +					goto err;
  1.1524 +					}
  1.1525 +				s2n(i,p);
  1.1526 +				n+=i+2;
  1.1527 +				}
  1.1528 +			else
  1.1529 +#endif
  1.1530 +#if !defined(OPENSSL_NO_ECDSA)
  1.1531 +				if (pkey->type == EVP_PKEY_EC)
  1.1532 +				{
  1.1533 +				/* let's do ECDSA */
  1.1534 +				EVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL);
  1.1535 +				EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1.1536 +				EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1.1537 +				EVP_SignUpdate(&md_ctx,&(d[4]),n);
  1.1538 +				if (!EVP_SignFinal(&md_ctx,&(p[2]),
  1.1539 +					(unsigned int *)&i,pkey))
  1.1540 +					{
  1.1541 +					SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_ECDSA);
  1.1542 +					goto err;
  1.1543 +					}
  1.1544 +				s2n(i,p);
  1.1545 +				n+=i+2;
  1.1546 +				}
  1.1547 +			else
  1.1548 +#endif
  1.1549 +				{
  1.1550 +				/* Is this error check actually needed? */
  1.1551 +				al=SSL_AD_HANDSHAKE_FAILURE;
  1.1552 +				SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
  1.1553 +				goto f_err;
  1.1554 +				}
  1.1555 +			}
  1.1556 +
  1.1557 +		*(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
  1.1558 +		l2n3(n,d);
  1.1559 +
  1.1560 +		/* we should now have things packed up, so lets send
  1.1561 +		 * it off */
  1.1562 +		s->init_num=n+4;
  1.1563 +		s->init_off=0;
  1.1564 +		}
  1.1565 +
  1.1566 +	s->state = SSL3_ST_SW_KEY_EXCH_B;
  1.1567 +	EVP_MD_CTX_cleanup(&md_ctx);
  1.1568 +	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1.1569 +f_err:
  1.1570 +	ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1.1571 +err:
  1.1572 +#ifndef OPENSSL_NO_ECDH
  1.1573 +	if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  1.1574 +	BN_CTX_free(bn_ctx);
  1.1575 +#endif
  1.1576 +	EVP_MD_CTX_cleanup(&md_ctx);
  1.1577 +	return(-1);
  1.1578 +	}
  1.1579 +
  1.1580 +int ssl3_send_certificate_request(SSL *s)
  1.1581 +	{
  1.1582 +	unsigned char *p,*d;
  1.1583 +	int i,j,nl,off,n;
  1.1584 +	STACK_OF(X509_NAME) *sk=NULL;
  1.1585 +	X509_NAME *name;
  1.1586 +	BUF_MEM *buf;
  1.1587 +
  1.1588 +	if (s->state == SSL3_ST_SW_CERT_REQ_A)
  1.1589 +		{
  1.1590 +		buf=s->init_buf;
  1.1591 +
  1.1592 +		d=p=(unsigned char *)&(buf->data[4]);
  1.1593 +
  1.1594 +		/* get the list of acceptable cert types */
  1.1595 +		p++;
  1.1596 +		n=ssl3_get_req_cert_type(s,p);
  1.1597 +		d[0]=n;
  1.1598 +		p+=n;
  1.1599 +		n++;
  1.1600 +
  1.1601 +		off=n;
  1.1602 +		p+=2;
  1.1603 +		n+=2;
  1.1604 +
  1.1605 +		sk=SSL_get_client_CA_list(s);
  1.1606 +		nl=0;
  1.1607 +		if (sk != NULL)
  1.1608 +			{
  1.1609 +			for (i=0; i<sk_X509_NAME_num(sk); i++)
  1.1610 +				{
  1.1611 +				name=sk_X509_NAME_value(sk,i);
  1.1612 +				j=i2d_X509_NAME(name,NULL);
  1.1613 +				if (!BUF_MEM_grow_clean(buf,4+n+j+2))
  1.1614 +					{
  1.1615 +					SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
  1.1616 +					goto err;
  1.1617 +					}
  1.1618 +				p=(unsigned char *)&(buf->data[4+n]);
  1.1619 +				if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1.1620 +					{
  1.1621 +					s2n(j,p);
  1.1622 +					i2d_X509_NAME(name,&p);
  1.1623 +					n+=2+j;
  1.1624 +					nl+=2+j;
  1.1625 +					}
  1.1626 +				else
  1.1627 +					{
  1.1628 +					d=p;
  1.1629 +					i2d_X509_NAME(name,&p);
  1.1630 +					j-=2; s2n(j,d); j+=2;
  1.1631 +					n+=j;
  1.1632 +					nl+=j;
  1.1633 +					}
  1.1634 +				}
  1.1635 +			}
  1.1636 +		/* else no CA names */
  1.1637 +		p=(unsigned char *)&(buf->data[4+off]);
  1.1638 +		s2n(nl,p);
  1.1639 +
  1.1640 +		d=(unsigned char *)buf->data;
  1.1641 +		*(d++)=SSL3_MT_CERTIFICATE_REQUEST;
  1.1642 +		l2n3(n,d);
  1.1643 +
  1.1644 +		/* we should now have things packed up, so lets send
  1.1645 +		 * it off */
  1.1646 +
  1.1647 +		s->init_num=n+4;
  1.1648 +		s->init_off=0;
  1.1649 +#ifdef NETSCAPE_HANG_BUG
  1.1650 +		p=(unsigned char *)s->init_buf->data + s->init_num;
  1.1651 +
  1.1652 +		/* do the header */
  1.1653 +		*(p++)=SSL3_MT_SERVER_DONE;
  1.1654 +		*(p++)=0;
  1.1655 +		*(p++)=0;
  1.1656 +		*(p++)=0;
  1.1657 +		s->init_num += 4;
  1.1658 +#endif
  1.1659 +
  1.1660 +		s->state = SSL3_ST_SW_CERT_REQ_B;
  1.1661 +		}
  1.1662 +
  1.1663 +	/* SSL3_ST_SW_CERT_REQ_B */
  1.1664 +	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1.1665 +err:
  1.1666 +	return(-1);
  1.1667 +	}
  1.1668 +
  1.1669 +int ssl3_get_client_key_exchange(SSL *s)
  1.1670 +	{
  1.1671 +	int i,al,ok;
  1.1672 +	long n;
  1.1673 +	unsigned long l;
  1.1674 +	unsigned char *p;
  1.1675 +#ifndef OPENSSL_NO_RSA
  1.1676 +	RSA *rsa=NULL;
  1.1677 +	EVP_PKEY *pkey=NULL;
  1.1678 +#endif
  1.1679 +#ifndef OPENSSL_NO_DH
  1.1680 +	BIGNUM *pub=NULL;
  1.1681 +	DH *dh_srvr;
  1.1682 +#endif
  1.1683 +#ifndef OPENSSL_NO_KRB5
  1.1684 +        KSSL_ERR kssl_err;
  1.1685 +#endif /* OPENSSL_NO_KRB5 */
  1.1686 +
  1.1687 +#ifndef OPENSSL_NO_ECDH
  1.1688 +	EC_KEY *srvr_ecdh = NULL;
  1.1689 +	EVP_PKEY *clnt_pub_pkey = NULL;
  1.1690 +	EC_POINT *clnt_ecpoint = NULL;
  1.1691 +	BN_CTX *bn_ctx = NULL; 
  1.1692 +#endif
  1.1693 +
  1.1694 +	n=s->method->ssl_get_message(s,
  1.1695 +		SSL3_ST_SR_KEY_EXCH_A,
  1.1696 +		SSL3_ST_SR_KEY_EXCH_B,
  1.1697 +		SSL3_MT_CLIENT_KEY_EXCHANGE,
  1.1698 +		2048, /* ??? */
  1.1699 +		&ok);
  1.1700 +
  1.1701 +	if (!ok) return((int)n);
  1.1702 +	p=(unsigned char *)s->init_msg;
  1.1703 +
  1.1704 +	l=s->s3->tmp.new_cipher->algorithms;
  1.1705 +
  1.1706 +#ifndef OPENSSL_NO_RSA
  1.1707 +	if (l & SSL_kRSA)
  1.1708 +		{
  1.1709 +		/* FIX THIS UP EAY EAY EAY EAY */
  1.1710 +		if (s->s3->tmp.use_rsa_tmp)
  1.1711 +			{
  1.1712 +			if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
  1.1713 +				rsa=s->cert->rsa_tmp;
  1.1714 +			/* Don't do a callback because rsa_tmp should
  1.1715 +			 * be sent already */
  1.1716 +			if (rsa == NULL)
  1.1717 +				{
  1.1718 +				al=SSL_AD_HANDSHAKE_FAILURE;
  1.1719 +				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
  1.1720 +				goto f_err;
  1.1721 +
  1.1722 +				}
  1.1723 +			}
  1.1724 +		else
  1.1725 +			{
  1.1726 +			pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
  1.1727 +			if (	(pkey == NULL) ||
  1.1728 +				(pkey->type != EVP_PKEY_RSA) ||
  1.1729 +				(pkey->pkey.rsa == NULL))
  1.1730 +				{
  1.1731 +				al=SSL_AD_HANDSHAKE_FAILURE;
  1.1732 +				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
  1.1733 +				goto f_err;
  1.1734 +				}
  1.1735 +			rsa=pkey->pkey.rsa;
  1.1736 +			}
  1.1737 +
  1.1738 +		/* TLS and [incidentally] DTLS, including pre-0.9.8f */
  1.1739 +		if (s->version > SSL3_VERSION &&
  1.1740 +		    s->client_version != DTLS1_BAD_VER)
  1.1741 +			{
  1.1742 +			n2s(p,i);
  1.1743 +			if (n != i+2)
  1.1744 +				{
  1.1745 +				if (!(s->options & SSL_OP_TLS_D5_BUG))
  1.1746 +					{
  1.1747 +					SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
  1.1748 +					goto err;
  1.1749 +					}
  1.1750 +				else
  1.1751 +					p-=2;
  1.1752 +				}
  1.1753 +			else
  1.1754 +				n=i;
  1.1755 +			}
  1.1756 +
  1.1757 +		i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
  1.1758 +
  1.1759 +		al = -1;
  1.1760 +		
  1.1761 +		if (i != SSL_MAX_MASTER_KEY_LENGTH)
  1.1762 +			{
  1.1763 +			al=SSL_AD_DECODE_ERROR;
  1.1764 +			/* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); */
  1.1765 +			}
  1.1766 +
  1.1767 +		if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
  1.1768 +			{
  1.1769 +			/* The premaster secret must contain the same version number as the
  1.1770 +			 * ClientHello to detect version rollback attacks (strangely, the
  1.1771 +			 * protocol does not offer such protection for DH ciphersuites).
  1.1772 +			 * However, buggy clients exist that send the negotiated protocol
  1.1773 +			 * version instead if the server does not support the requested
  1.1774 +			 * protocol version.
  1.1775 +			 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
  1.1776 +			if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
  1.1777 +				(p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
  1.1778 +				{
  1.1779 +				al=SSL_AD_DECODE_ERROR;
  1.1780 +				/* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */
  1.1781 +
  1.1782 +				/* The Klima-Pokorny-Rosa extension of Bleichenbacher's attack
  1.1783 +				 * (http://eprint.iacr.org/2003/052/) exploits the version
  1.1784 +				 * number check as a "bad version oracle" -- an alert would
  1.1785 +				 * reveal that the plaintext corresponding to some ciphertext
  1.1786 +				 * made up by the adversary is properly formatted except
  1.1787 +				 * that the version number is wrong.  To avoid such attacks,
  1.1788 +				 * we should treat this just like any other decryption error. */
  1.1789 +				}
  1.1790 +			}
  1.1791 +
  1.1792 +		if (al != -1)
  1.1793 +			{
  1.1794 +			/* Some decryption failure -- use random value instead as countermeasure
  1.1795 +			 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
  1.1796 +			 * (see RFC 2246, section 7.4.7.1). */
  1.1797 +			ERR_clear_error();
  1.1798 +			i = SSL_MAX_MASTER_KEY_LENGTH;
  1.1799 +			p[0] = s->client_version >> 8;
  1.1800 +			p[1] = s->client_version & 0xff;
  1.1801 +			if (RAND_pseudo_bytes(p+2, i-2) <= 0) /* should be RAND_bytes, but we cannot work around a failure */
  1.1802 +				goto err;
  1.1803 +			}
  1.1804 +	
  1.1805 +		s->session->master_key_length=
  1.1806 +			s->method->ssl3_enc->generate_master_secret(s,
  1.1807 +				s->session->master_key,
  1.1808 +				p,i);
  1.1809 +		OPENSSL_cleanse(p,i);
  1.1810 +		}
  1.1811 +	else
  1.1812 +#endif
  1.1813 +#ifndef OPENSSL_NO_DH
  1.1814 +		if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  1.1815 +		{
  1.1816 +		n2s(p,i);
  1.1817 +		if (n != i+2)
  1.1818 +			{
  1.1819 +			if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
  1.1820 +				{
  1.1821 +				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
  1.1822 +				goto err;
  1.1823 +				}
  1.1824 +			else
  1.1825 +				{
  1.1826 +				p-=2;
  1.1827 +				i=(int)n;
  1.1828 +				}
  1.1829 +			}
  1.1830 +
  1.1831 +		if (n == 0L) /* the parameters are in the cert */
  1.1832 +			{
  1.1833 +			al=SSL_AD_HANDSHAKE_FAILURE;
  1.1834 +			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
  1.1835 +			goto f_err;
  1.1836 +			}
  1.1837 +		else
  1.1838 +			{
  1.1839 +			if (s->s3->tmp.dh == NULL)
  1.1840 +				{
  1.1841 +				al=SSL_AD_HANDSHAKE_FAILURE;
  1.1842 +				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
  1.1843 +				goto f_err;
  1.1844 +				}
  1.1845 +			else
  1.1846 +				dh_srvr=s->s3->tmp.dh;
  1.1847 +			}
  1.1848 +
  1.1849 +		pub=BN_bin2bn(p,i,NULL);
  1.1850 +		if (pub == NULL)
  1.1851 +			{
  1.1852 +			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
  1.1853 +			goto err;
  1.1854 +			}
  1.1855 +
  1.1856 +		i=DH_compute_key(p,pub,dh_srvr);
  1.1857 +
  1.1858 +		if (i <= 0)
  1.1859 +			{
  1.1860 +			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1.1861 +			goto err;
  1.1862 +			}
  1.1863 +
  1.1864 +		DH_free(s->s3->tmp.dh);
  1.1865 +		s->s3->tmp.dh=NULL;
  1.1866 +
  1.1867 +		BN_clear_free(pub);
  1.1868 +		pub=NULL;
  1.1869 +		s->session->master_key_length=
  1.1870 +			s->method->ssl3_enc->generate_master_secret(s,
  1.1871 +				s->session->master_key,p,i);
  1.1872 +		OPENSSL_cleanse(p,i);
  1.1873 +		}
  1.1874 +	else
  1.1875 +#endif
  1.1876 +#ifndef OPENSSL_NO_KRB5
  1.1877 +        if (l & SSL_kKRB5)
  1.1878 +                {
  1.1879 +                krb5_error_code		krb5rc;
  1.1880 +		krb5_data		enc_ticket;
  1.1881 +		krb5_data		authenticator;
  1.1882 +		krb5_data		enc_pms;
  1.1883 +                KSSL_CTX		*kssl_ctx = s->kssl_ctx;
  1.1884 +		EVP_CIPHER_CTX		ciph_ctx;
  1.1885 +		EVP_CIPHER		*enc = NULL;
  1.1886 +		unsigned char		iv[EVP_MAX_IV_LENGTH];
  1.1887 +		unsigned char		pms[SSL_MAX_MASTER_KEY_LENGTH
  1.1888 +                                               + EVP_MAX_BLOCK_LENGTH];
  1.1889 +		int                     padl, outl;
  1.1890 +		krb5_timestamp		authtime = 0;
  1.1891 +		krb5_ticket_times	ttimes;
  1.1892 +
  1.1893 +		EVP_CIPHER_CTX_init(&ciph_ctx);
  1.1894 +
  1.1895 +                if (!kssl_ctx)  kssl_ctx = kssl_ctx_new();
  1.1896 +
  1.1897 +		n2s(p,i);
  1.1898 +		enc_ticket.length = i;
  1.1899 +
  1.1900 +		if (n < (int)enc_ticket.length + 6)
  1.1901 +			{
  1.1902 +			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.1903 +				SSL_R_DATA_LENGTH_TOO_LONG);
  1.1904 +			goto err;
  1.1905 +			}
  1.1906 +
  1.1907 +		enc_ticket.data = (char *)p;
  1.1908 +		p+=enc_ticket.length;
  1.1909 +
  1.1910 +		n2s(p,i);
  1.1911 +		authenticator.length = i;
  1.1912 +
  1.1913 +		if (n < (int)(enc_ticket.length + authenticator.length) + 6)
  1.1914 +			{
  1.1915 +			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.1916 +				SSL_R_DATA_LENGTH_TOO_LONG);
  1.1917 +			goto err;
  1.1918 +			}
  1.1919 +
  1.1920 +		authenticator.data = (char *)p;
  1.1921 +		p+=authenticator.length;
  1.1922 +
  1.1923 +		n2s(p,i);
  1.1924 +		enc_pms.length = i;
  1.1925 +		enc_pms.data = (char *)p;
  1.1926 +		p+=enc_pms.length;
  1.1927 +
  1.1928 +		/* Note that the length is checked again below,
  1.1929 +		** after decryption
  1.1930 +		*/
  1.1931 +		if(enc_pms.length > sizeof pms)
  1.1932 +			{
  1.1933 +			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.1934 +			       SSL_R_DATA_LENGTH_TOO_LONG);
  1.1935 +			goto err;
  1.1936 +			}
  1.1937 +
  1.1938 +		if (n != (long)(enc_ticket.length + authenticator.length +
  1.1939 +						enc_pms.length + 6))
  1.1940 +			{
  1.1941 +			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.1942 +				SSL_R_DATA_LENGTH_TOO_LONG);
  1.1943 +			goto err;
  1.1944 +			}
  1.1945 +
  1.1946 +                if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
  1.1947 +					&kssl_err)) != 0)
  1.1948 +                        {
  1.1949 +#ifdef KSSL_DEBUG
  1.1950 +                        printf("kssl_sget_tkt rtn %d [%d]\n",
  1.1951 +                                krb5rc, kssl_err.reason);
  1.1952 +                        if (kssl_err.text)
  1.1953 +                                printf("kssl_err text= %s\n", kssl_err.text);
  1.1954 +#endif	/* KSSL_DEBUG */
  1.1955 +                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.1956 +                                kssl_err.reason);
  1.1957 +                        goto err;
  1.1958 +                        }
  1.1959 +
  1.1960 +		/*  Note: no authenticator is not considered an error,
  1.1961 +		**  but will return authtime == 0.
  1.1962 +		*/
  1.1963 +		if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
  1.1964 +					&authtime, &kssl_err)) != 0)
  1.1965 +			{
  1.1966 +#ifdef KSSL_DEBUG
  1.1967 +                        printf("kssl_check_authent rtn %d [%d]\n",
  1.1968 +                                krb5rc, kssl_err.reason);
  1.1969 +                        if (kssl_err.text)
  1.1970 +                                printf("kssl_err text= %s\n", kssl_err.text);
  1.1971 +#endif	/* KSSL_DEBUG */
  1.1972 +                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.1973 +                                kssl_err.reason);
  1.1974 +                        goto err;
  1.1975 +			}
  1.1976 +
  1.1977 +		if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
  1.1978 +			{
  1.1979 +			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
  1.1980 +                        goto err;
  1.1981 +			}
  1.1982 +
  1.1983 +#ifdef KSSL_DEBUG
  1.1984 +                kssl_ctx_show(kssl_ctx);
  1.1985 +#endif	/* KSSL_DEBUG */
  1.1986 +
  1.1987 +		enc = kssl_map_enc(kssl_ctx->enctype);
  1.1988 +                if (enc == NULL)
  1.1989 +                    goto err;
  1.1990 +
  1.1991 +		memset(iv, 0, sizeof iv);	/* per RFC 1510 */
  1.1992 +
  1.1993 +		if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))
  1.1994 +			{
  1.1995 +			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.1996 +				SSL_R_DECRYPTION_FAILED);
  1.1997 +			goto err;
  1.1998 +			}
  1.1999 +		if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
  1.2000 +					(unsigned char *)enc_pms.data, enc_pms.length))
  1.2001 +			{
  1.2002 +			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.2003 +				SSL_R_DECRYPTION_FAILED);
  1.2004 +			goto err;
  1.2005 +			}
  1.2006 +		if (outl > SSL_MAX_MASTER_KEY_LENGTH)
  1.2007 +			{
  1.2008 +			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.2009 +				SSL_R_DATA_LENGTH_TOO_LONG);
  1.2010 +			goto err;
  1.2011 +			}
  1.2012 +		if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))
  1.2013 +			{
  1.2014 +			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.2015 +				SSL_R_DECRYPTION_FAILED);
  1.2016 +			goto err;
  1.2017 +			}
  1.2018 +		outl += padl;
  1.2019 +		if (outl > SSL_MAX_MASTER_KEY_LENGTH)
  1.2020 +			{
  1.2021 +			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.2022 +				SSL_R_DATA_LENGTH_TOO_LONG);
  1.2023 +			goto err;
  1.2024 +			}
  1.2025 +		if (!((pms[0] == (s->client_version>>8)) && (pms[1] == (s->client_version & 0xff))))
  1.2026 +		    {
  1.2027 +		    /* The premaster secret must contain the same version number as the
  1.2028 +		     * ClientHello to detect version rollback attacks (strangely, the
  1.2029 +		     * protocol does not offer such protection for DH ciphersuites).
  1.2030 +		     * However, buggy clients exist that send random bytes instead of
  1.2031 +		     * the protocol version.
  1.2032 +		     * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. 
  1.2033 +		     * (Perhaps we should have a separate BUG value for the Kerberos cipher)
  1.2034 +		     */
  1.2035 +		    if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG))
  1.2036 +		        {
  1.2037 +			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.2038 +			       SSL_AD_DECODE_ERROR);
  1.2039 +			goto err;
  1.2040 +			}
  1.2041 +		    }
  1.2042 +		EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  1.2043 +
  1.2044 +                s->session->master_key_length=
  1.2045 +                        s->method->ssl3_enc->generate_master_secret(s,
  1.2046 +                                s->session->master_key, pms, outl);
  1.2047 +
  1.2048 +                if (kssl_ctx->client_princ)
  1.2049 +                        {
  1.2050 +                        size_t len = strlen(kssl_ctx->client_princ);
  1.2051 +                        if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH ) 
  1.2052 +                                {
  1.2053 +                                s->session->krb5_client_princ_len = len;
  1.2054 +                                memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
  1.2055 +                                }
  1.2056 +                        }
  1.2057 +
  1.2058 +
  1.2059 +                /*  Was doing kssl_ctx_free() here,
  1.2060 +		**  but it caused problems for apache.
  1.2061 +                **  kssl_ctx = kssl_ctx_free(kssl_ctx);
  1.2062 +                **  if (s->kssl_ctx)  s->kssl_ctx = NULL;
  1.2063 +                */
  1.2064 +                }
  1.2065 +	else
  1.2066 +#endif	/* OPENSSL_NO_KRB5 */
  1.2067 +
  1.2068 +#ifndef OPENSSL_NO_ECDH
  1.2069 +		if ((l & SSL_kECDH) || (l & SSL_kECDHE))
  1.2070 +		{
  1.2071 +		int ret = 1;
  1.2072 +		int field_size = 0;
  1.2073 +		const EC_KEY   *tkey;
  1.2074 +		const EC_GROUP *group;
  1.2075 +		const BIGNUM *priv_key;
  1.2076 +
  1.2077 +                /* initialize structures for server's ECDH key pair */
  1.2078 +		if ((srvr_ecdh = EC_KEY_new()) == NULL) 
  1.2079 +			{
  1.2080 +                	SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.2081 +			    ERR_R_MALLOC_FAILURE);
  1.2082 +                	goto err;
  1.2083 +			}
  1.2084 +
  1.2085 +		/* Let's get server private key and group information */
  1.2086 +		if (l & SSL_kECDH) 
  1.2087 +			{ 
  1.2088 +                        /* use the certificate */
  1.2089 +			tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
  1.2090 +			}
  1.2091 +		else
  1.2092 +			{
  1.2093 +			/* use the ephermeral values we saved when
  1.2094 +			 * generating the ServerKeyExchange msg.
  1.2095 +			 */
  1.2096 +			tkey = s->s3->tmp.ecdh;
  1.2097 +			}
  1.2098 +
  1.2099 +		group    = EC_KEY_get0_group(tkey);
  1.2100 +		priv_key = EC_KEY_get0_private_key(tkey);
  1.2101 +
  1.2102 +		if (!EC_KEY_set_group(srvr_ecdh, group) ||
  1.2103 +		    !EC_KEY_set_private_key(srvr_ecdh, priv_key))
  1.2104 +			{
  1.2105 +			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.2106 +			       ERR_R_EC_LIB);
  1.2107 +			goto err;
  1.2108 +			}
  1.2109 +
  1.2110 +		/* Let's get client's public key */
  1.2111 +		if ((clnt_ecpoint = EC_POINT_new(group)) == NULL)
  1.2112 +			{
  1.2113 +			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.2114 +			    ERR_R_MALLOC_FAILURE);
  1.2115 +			goto err;
  1.2116 +			}
  1.2117 +
  1.2118 +                if (n == 0L) 
  1.2119 +                        {
  1.2120 +			/* Client Publickey was in Client Certificate */
  1.2121 +
  1.2122 +			 if (l & SSL_kECDHE) 
  1.2123 +				 {
  1.2124 +				 al=SSL_AD_HANDSHAKE_FAILURE;
  1.2125 +				 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
  1.2126 +				 goto f_err;
  1.2127 +				 }
  1.2128 +                        if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))
  1.2129 +			    == NULL) || 
  1.2130 +			    (clnt_pub_pkey->type != EVP_PKEY_EC))
  1.2131 +                        	{
  1.2132 +				/* XXX: For now, we do not support client
  1.2133 +				 * authentication using ECDH certificates
  1.2134 +				 * so this branch (n == 0L) of the code is
  1.2135 +				 * never executed. When that support is
  1.2136 +				 * added, we ought to ensure the key 
  1.2137 +				 * received in the certificate is 
  1.2138 +				 * authorized for key agreement.
  1.2139 +				 * ECDH_compute_key implicitly checks that
  1.2140 +				 * the two ECDH shares are for the same
  1.2141 +				 * group.
  1.2142 +				 */
  1.2143 +                           	al=SSL_AD_HANDSHAKE_FAILURE;
  1.2144 +                           	SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.2145 +				    SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
  1.2146 +                           	goto f_err;
  1.2147 +                           	}
  1.2148 +
  1.2149 +			if (EC_POINT_copy(clnt_ecpoint,
  1.2150 +			    EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec)) == 0)
  1.2151 +				{
  1.2152 +				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.2153 +					ERR_R_EC_LIB);
  1.2154 +				goto err;
  1.2155 +				}
  1.2156 +                        ret = 2; /* Skip certificate verify processing */
  1.2157 +                        }
  1.2158 +                else
  1.2159 +                        {
  1.2160 +			/* Get client's public key from encoded point
  1.2161 +			 * in the ClientKeyExchange message.
  1.2162 +			 */
  1.2163 +			if ((bn_ctx = BN_CTX_new()) == NULL)
  1.2164 +				{
  1.2165 +				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.2166 +				    ERR_R_MALLOC_FAILURE);
  1.2167 +				goto err;
  1.2168 +				}
  1.2169 +
  1.2170 +                        /* Get encoded point length */
  1.2171 +                        i = *p; 
  1.2172 +			p += 1;
  1.2173 +                        if (EC_POINT_oct2point(group, 
  1.2174 +			    clnt_ecpoint, p, i, bn_ctx) == 0)
  1.2175 +				{
  1.2176 +				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.2177 +				    ERR_R_EC_LIB);
  1.2178 +				goto err;
  1.2179 +				}
  1.2180 +                        /* p is pointing to somewhere in the buffer
  1.2181 +                         * currently, so set it to the start 
  1.2182 +                         */ 
  1.2183 +                        p=(unsigned char *)s->init_buf->data;
  1.2184 +                        }
  1.2185 +
  1.2186 +		/* Compute the shared pre-master secret */
  1.2187 +		field_size = EC_GROUP_get_degree(group);
  1.2188 +		if (field_size <= 0)
  1.2189 +			{
  1.2190 +			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 
  1.2191 +			       ERR_R_ECDH_LIB);
  1.2192 +			goto err;
  1.2193 +			}
  1.2194 +		i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL);
  1.2195 +                if (i <= 0)
  1.2196 +                        {
  1.2197 +                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.2198 +			    ERR_R_ECDH_LIB);
  1.2199 +                        goto err;
  1.2200 +                        }
  1.2201 +
  1.2202 +		EVP_PKEY_free(clnt_pub_pkey);
  1.2203 +		EC_POINT_free(clnt_ecpoint);
  1.2204 +		if (srvr_ecdh != NULL) 
  1.2205 +			EC_KEY_free(srvr_ecdh);
  1.2206 +		BN_CTX_free(bn_ctx);
  1.2207 +
  1.2208 +		/* Compute the master secret */
  1.2209 +                s->session->master_key_length = s->method->ssl3_enc-> \
  1.2210 +		    generate_master_secret(s, s->session->master_key, p, i);
  1.2211 +		
  1.2212 +                OPENSSL_cleanse(p, i);
  1.2213 +                return (ret);
  1.2214 +		}
  1.2215 +	else
  1.2216 +#endif
  1.2217 +		{
  1.2218 +		al=SSL_AD_HANDSHAKE_FAILURE;
  1.2219 +		SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1.2220 +				SSL_R_UNKNOWN_CIPHER_TYPE);
  1.2221 +		goto f_err;
  1.2222 +		}
  1.2223 +
  1.2224 +	return(1);
  1.2225 +f_err:
  1.2226 +	ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1.2227 +#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH)
  1.2228 +err:
  1.2229 +#endif
  1.2230 +#ifndef OPENSSL_NO_ECDH
  1.2231 +	EVP_PKEY_free(clnt_pub_pkey);
  1.2232 +	EC_POINT_free(clnt_ecpoint);
  1.2233 +	if (srvr_ecdh != NULL) 
  1.2234 +		EC_KEY_free(srvr_ecdh);
  1.2235 +	BN_CTX_free(bn_ctx);
  1.2236 +#endif
  1.2237 +	return(-1);
  1.2238 +	}
  1.2239 +
  1.2240 +int ssl3_get_cert_verify(SSL *s)
  1.2241 +	{
  1.2242 +	EVP_PKEY *pkey=NULL;
  1.2243 +	unsigned char *p;
  1.2244 +	int al,ok,ret=0;
  1.2245 +	long n;
  1.2246 +	int type=0,i,j;
  1.2247 +	X509 *peer;
  1.2248 +
  1.2249 +	n=s->method->ssl_get_message(s,
  1.2250 +		SSL3_ST_SR_CERT_VRFY_A,
  1.2251 +		SSL3_ST_SR_CERT_VRFY_B,
  1.2252 +		-1,
  1.2253 +		514, /* 514? */
  1.2254 +		&ok);
  1.2255 +
  1.2256 +	if (!ok) return((int)n);
  1.2257 +
  1.2258 +	if (s->session->peer != NULL)
  1.2259 +		{
  1.2260 +		peer=s->session->peer;
  1.2261 +		pkey=X509_get_pubkey(peer);
  1.2262 +		type=X509_certificate_type(peer,pkey);
  1.2263 +		}
  1.2264 +	else
  1.2265 +		{
  1.2266 +		peer=NULL;
  1.2267 +		pkey=NULL;
  1.2268 +		}
  1.2269 +
  1.2270 +	if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
  1.2271 +		{
  1.2272 +		s->s3->tmp.reuse_message=1;
  1.2273 +		if ((peer != NULL) && (type | EVP_PKT_SIGN))
  1.2274 +			{
  1.2275 +			al=SSL_AD_UNEXPECTED_MESSAGE;
  1.2276 +			SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
  1.2277 +			goto f_err;
  1.2278 +			}
  1.2279 +		ret=1;
  1.2280 +		goto end;
  1.2281 +		}
  1.2282 +
  1.2283 +	if (peer == NULL)
  1.2284 +		{
  1.2285 +		SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
  1.2286 +		al=SSL_AD_UNEXPECTED_MESSAGE;
  1.2287 +		goto f_err;
  1.2288 +		}
  1.2289 +
  1.2290 +	if (!(type & EVP_PKT_SIGN))
  1.2291 +		{
  1.2292 +		SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
  1.2293 +		al=SSL_AD_ILLEGAL_PARAMETER;
  1.2294 +		goto f_err;
  1.2295 +		}
  1.2296 +
  1.2297 +	if (s->s3->change_cipher_spec)
  1.2298 +		{
  1.2299 +		SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
  1.2300 +		al=SSL_AD_UNEXPECTED_MESSAGE;
  1.2301 +		goto f_err;
  1.2302 +		}
  1.2303 +
  1.2304 +	/* we now have a signature that we need to verify */
  1.2305 +	p=(unsigned char *)s->init_msg;
  1.2306 +	n2s(p,i);
  1.2307 +	n-=2;
  1.2308 +	if (i > n)
  1.2309 +		{
  1.2310 +		SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
  1.2311 +		al=SSL_AD_DECODE_ERROR;
  1.2312 +		goto f_err;
  1.2313 +		}
  1.2314 +
  1.2315 +	j=EVP_PKEY_size(pkey);
  1.2316 +	if ((i > j) || (n > j) || (n <= 0))
  1.2317 +		{
  1.2318 +		SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
  1.2319 +		al=SSL_AD_DECODE_ERROR;
  1.2320 +		goto f_err;
  1.2321 +		}
  1.2322 +
  1.2323 +#ifndef OPENSSL_NO_RSA 
  1.2324 +	if (pkey->type == EVP_PKEY_RSA)
  1.2325 +		{
  1.2326 +		i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
  1.2327 +			MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i, 
  1.2328 +							pkey->pkey.rsa);
  1.2329 +		if (i < 0)
  1.2330 +			{
  1.2331 +			al=SSL_AD_DECRYPT_ERROR;
  1.2332 +			SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
  1.2333 +			goto f_err;
  1.2334 +			}
  1.2335 +		if (i == 0)
  1.2336 +			{
  1.2337 +			al=SSL_AD_DECRYPT_ERROR;
  1.2338 +			SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
  1.2339 +			goto f_err;
  1.2340 +			}
  1.2341 +		}
  1.2342 +	else
  1.2343 +#endif
  1.2344 +#ifndef OPENSSL_NO_DSA
  1.2345 +		if (pkey->type == EVP_PKEY_DSA)
  1.2346 +		{
  1.2347 +		j=DSA_verify(pkey->save_type,
  1.2348 +			&(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
  1.2349 +			SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
  1.2350 +		if (j <= 0)
  1.2351 +			{
  1.2352 +			/* bad signature */
  1.2353 +			al=SSL_AD_DECRYPT_ERROR;
  1.2354 +			SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
  1.2355 +			goto f_err;
  1.2356 +			}
  1.2357 +		}
  1.2358 +	else
  1.2359 +#endif
  1.2360 +#ifndef OPENSSL_NO_ECDSA
  1.2361 +		if (pkey->type == EVP_PKEY_EC)
  1.2362 +		{
  1.2363 +		j=ECDSA_verify(pkey->save_type,
  1.2364 +			&(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
  1.2365 +			SHA_DIGEST_LENGTH,p,i,pkey->pkey.ec);
  1.2366 +		if (j <= 0)
  1.2367 +			{
  1.2368 +			/* bad signature */
  1.2369 +			al=SSL_AD_DECRYPT_ERROR;
  1.2370 +			SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
  1.2371 +			    SSL_R_BAD_ECDSA_SIGNATURE);
  1.2372 +			goto f_err;
  1.2373 +			}
  1.2374 +		}
  1.2375 +	else
  1.2376 +#endif
  1.2377 +		{
  1.2378 +		SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
  1.2379 +		al=SSL_AD_UNSUPPORTED_CERTIFICATE;
  1.2380 +		goto f_err;
  1.2381 +		}
  1.2382 +
  1.2383 +
  1.2384 +	ret=1;
  1.2385 +	if (0)
  1.2386 +		{
  1.2387 +f_err:
  1.2388 +		ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1.2389 +		}
  1.2390 +end:
  1.2391 +	EVP_PKEY_free(pkey);
  1.2392 +	return(ret);
  1.2393 +	}
  1.2394 +
  1.2395 +int ssl3_get_client_certificate(SSL *s)
  1.2396 +	{
  1.2397 +	int i,ok,al,ret= -1;
  1.2398 +	X509 *x=NULL;
  1.2399 +	unsigned long l,nc,llen,n;
  1.2400 +	const unsigned char *p,*q;
  1.2401 +	unsigned char *d;
  1.2402 +	STACK_OF(X509) *sk=NULL;
  1.2403 +
  1.2404 +	n=s->method->ssl_get_message(s,
  1.2405 +		SSL3_ST_SR_CERT_A,
  1.2406 +		SSL3_ST_SR_CERT_B,
  1.2407 +		-1,
  1.2408 +		s->max_cert_list,
  1.2409 +		&ok);
  1.2410 +
  1.2411 +	if (!ok) return((int)n);
  1.2412 +
  1.2413 +	if	(s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
  1.2414 +		{
  1.2415 +		if (	(s->verify_mode & SSL_VERIFY_PEER) &&
  1.2416 +			(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  1.2417 +			{
  1.2418 +			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  1.2419 +			al=SSL_AD_HANDSHAKE_FAILURE;
  1.2420 +			goto f_err;
  1.2421 +			}
  1.2422 +		/* If tls asked for a client cert, the client must return a 0 list */
  1.2423 +		if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
  1.2424 +			{
  1.2425 +			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
  1.2426 +			al=SSL_AD_UNEXPECTED_MESSAGE;
  1.2427 +			goto f_err;
  1.2428 +			}
  1.2429 +		s->s3->tmp.reuse_message=1;
  1.2430 +		return(1);
  1.2431 +		}
  1.2432 +
  1.2433 +	if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
  1.2434 +		{
  1.2435 +		al=SSL_AD_UNEXPECTED_MESSAGE;
  1.2436 +		SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
  1.2437 +		goto f_err;
  1.2438 +		}
  1.2439 +	p=d=(unsigned char *)s->init_msg;
  1.2440 +
  1.2441 +	if ((sk=sk_X509_new_null()) == NULL)
  1.2442 +		{
  1.2443 +		SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  1.2444 +		goto err;
  1.2445 +		}
  1.2446 +
  1.2447 +	n2l3(p,llen);
  1.2448 +	if (llen+3 != n)
  1.2449 +		{
  1.2450 +		al=SSL_AD_DECODE_ERROR;
  1.2451 +		SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
  1.2452 +		goto f_err;
  1.2453 +		}
  1.2454 +	for (nc=0; nc<llen; )
  1.2455 +		{
  1.2456 +		n2l3(p,l);
  1.2457 +		if ((l+nc+3) > llen)
  1.2458 +			{
  1.2459 +			al=SSL_AD_DECODE_ERROR;
  1.2460 +			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  1.2461 +			goto f_err;
  1.2462 +			}
  1.2463 +
  1.2464 +		q=p;
  1.2465 +		x=d2i_X509(NULL,&p,l);
  1.2466 +		if (x == NULL)
  1.2467 +			{
  1.2468 +			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
  1.2469 +			goto err;
  1.2470 +			}
  1.2471 +		if (p != (q+l))
  1.2472 +			{
  1.2473 +			al=SSL_AD_DECODE_ERROR;
  1.2474 +			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  1.2475 +			goto f_err;
  1.2476 +			}
  1.2477 +		if (!sk_X509_push(sk,x))
  1.2478 +			{
  1.2479 +			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  1.2480 +			goto err;
  1.2481 +			}
  1.2482 +		x=NULL;
  1.2483 +		nc+=l+3;
  1.2484 +		}
  1.2485 +
  1.2486 +	if (sk_X509_num(sk) <= 0)
  1.2487 +		{
  1.2488 +		/* TLS does not mind 0 certs returned */
  1.2489 +		if (s->version == SSL3_VERSION)
  1.2490 +			{
  1.2491 +			al=SSL_AD_HANDSHAKE_FAILURE;
  1.2492 +			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
  1.2493 +			goto f_err;
  1.2494 +			}
  1.2495 +		/* Fail for TLS only if we required a certificate */
  1.2496 +		else if ((s->verify_mode & SSL_VERIFY_PEER) &&
  1.2497 +			 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  1.2498 +			{
  1.2499 +			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  1.2500 +			al=SSL_AD_HANDSHAKE_FAILURE;
  1.2501 +			goto f_err;
  1.2502 +			}
  1.2503 +		}
  1.2504 +	else
  1.2505 +		{
  1.2506 +		i=ssl_verify_cert_chain(s,sk);
  1.2507 +		if (!i)
  1.2508 +			{
  1.2509 +			al=ssl_verify_alarm_type(s->verify_result);
  1.2510 +			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
  1.2511 +			goto f_err;
  1.2512 +			}
  1.2513 +		}
  1.2514 +
  1.2515 +	if (s->session->peer != NULL) /* This should not be needed */
  1.2516 +		X509_free(s->session->peer);
  1.2517 +	s->session->peer=sk_X509_shift(sk);
  1.2518 +	s->session->verify_result = s->verify_result;
  1.2519 +
  1.2520 +	/* With the current implementation, sess_cert will always be NULL
  1.2521 +	 * when we arrive here. */
  1.2522 +	if (s->session->sess_cert == NULL)
  1.2523 +		{
  1.2524 +		s->session->sess_cert = ssl_sess_cert_new();
  1.2525 +		if (s->session->sess_cert == NULL)
  1.2526 +			{
  1.2527 +			SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  1.2528 +			goto err;
  1.2529 +			}
  1.2530 +		}
  1.2531 +	if (s->session->sess_cert->cert_chain != NULL)
  1.2532 +		sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
  1.2533 +	s->session->sess_cert->cert_chain=sk;
  1.2534 +	/* Inconsistency alert: cert_chain does *not* include the
  1.2535 +	 * peer's own certificate, while we do include it in s3_clnt.c */
  1.2536 +
  1.2537 +	sk=NULL;
  1.2538 +
  1.2539 +	ret=1;
  1.2540 +	if (0)
  1.2541 +		{
  1.2542 +f_err:
  1.2543 +		ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1.2544 +		}
  1.2545 +err:
  1.2546 +	if (x != NULL) X509_free(x);
  1.2547 +	if (sk != NULL) sk_X509_pop_free(sk,X509_free);
  1.2548 +	return(ret);
  1.2549 +	}
  1.2550 +
  1.2551 +int ssl3_send_server_certificate(SSL *s)
  1.2552 +	{
  1.2553 +	unsigned long l;
  1.2554 +	X509 *x;
  1.2555 +
  1.2556 +	if (s->state == SSL3_ST_SW_CERT_A)
  1.2557 +		{
  1.2558 +		x=ssl_get_server_send_cert(s);
  1.2559 +		if (x == NULL &&
  1.2560 +                        /* VRS: allow null cert if auth == KRB5 */
  1.2561 +                        (s->s3->tmp.new_cipher->algorithms
  1.2562 +                                & (SSL_MKEY_MASK|SSL_AUTH_MASK))
  1.2563 +                        != (SSL_aKRB5|SSL_kKRB5))
  1.2564 +			{
  1.2565 +			SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
  1.2566 +			return(0);
  1.2567 +			}
  1.2568 +
  1.2569 +		l=ssl3_output_cert_chain(s,x);
  1.2570 +		s->state=SSL3_ST_SW_CERT_B;
  1.2571 +		s->init_num=(int)l;
  1.2572 +		s->init_off=0;
  1.2573 +		}
  1.2574 +
  1.2575 +	/* SSL3_ST_SW_CERT_B */
  1.2576 +	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1.2577 +	}
  1.2578 +
  1.2579 +
  1.2580 +#ifndef OPENSSL_NO_ECDH
  1.2581 +/* This is the complement of curve_id2nid in s3_clnt.c. */
  1.2582 +static int nid2curve_id(int nid)
  1.2583 +{
  1.2584 +	/* ECC curves from draft-ietf-tls-ecc-01.txt (Mar 15, 2001)
  1.2585 +	 * (no changes in draft-ietf-tls-ecc-03.txt [June 2003]) */
  1.2586 +	switch (nid) {
  1.2587 +	case NID_sect163k1: /* sect163k1 (1) */
  1.2588 +		return 1;
  1.2589 +	case NID_sect163r1: /* sect163r1 (2) */
  1.2590 +		return 2;
  1.2591 +	case NID_sect163r2: /* sect163r2 (3) */
  1.2592 +		return 3;
  1.2593 +	case NID_sect193r1: /* sect193r1 (4) */ 
  1.2594 +		return 4;
  1.2595 +	case NID_sect193r2: /* sect193r2 (5) */ 
  1.2596 +		return 5;
  1.2597 +	case NID_sect233k1: /* sect233k1 (6) */
  1.2598 +		return 6;
  1.2599 +	case NID_sect233r1: /* sect233r1 (7) */ 
  1.2600 +		return 7;
  1.2601 +	case NID_sect239k1: /* sect239k1 (8) */ 
  1.2602 +		return 8;
  1.2603 +	case NID_sect283k1: /* sect283k1 (9) */
  1.2604 +		return 9;
  1.2605 +	case NID_sect283r1: /* sect283r1 (10) */ 
  1.2606 +		return 10;
  1.2607 +	case NID_sect409k1: /* sect409k1 (11) */ 
  1.2608 +		return 11;
  1.2609 +	case NID_sect409r1: /* sect409r1 (12) */
  1.2610 +		return 12;
  1.2611 +	case NID_sect571k1: /* sect571k1 (13) */ 
  1.2612 +		return 13;
  1.2613 +	case NID_sect571r1: /* sect571r1 (14) */ 
  1.2614 +		return 14;
  1.2615 +	case NID_secp160k1: /* secp160k1 (15) */
  1.2616 +		return 15;
  1.2617 +	case NID_secp160r1: /* secp160r1 (16) */ 
  1.2618 +		return 16;
  1.2619 +	case NID_secp160r2: /* secp160r2 (17) */ 
  1.2620 +		return 17;
  1.2621 +	case NID_secp192k1: /* secp192k1 (18) */
  1.2622 +		return 18;
  1.2623 +	case NID_X9_62_prime192v1: /* secp192r1 (19) */ 
  1.2624 +		return 19;
  1.2625 +	case NID_secp224k1: /* secp224k1 (20) */ 
  1.2626 +		return 20;
  1.2627 +	case NID_secp224r1: /* secp224r1 (21) */
  1.2628 +		return 21;
  1.2629 +	case NID_secp256k1: /* secp256k1 (22) */ 
  1.2630 +		return 22;
  1.2631 +	case NID_X9_62_prime256v1: /* secp256r1 (23) */ 
  1.2632 +		return 23;
  1.2633 +	case NID_secp384r1: /* secp384r1 (24) */
  1.2634 +		return 24;
  1.2635 +	case NID_secp521r1:  /* secp521r1 (25) */	
  1.2636 +		return 25;
  1.2637 +	default:
  1.2638 +		return 0;
  1.2639 +	}
  1.2640 +}
  1.2641 +#endif