os/ossrv/ssl/libssl/src/s23_clnt.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libssl/src/s23_clnt.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,595 @@
     1.4 +/* ssl/s23_clnt.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 + © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
    1.63 + */
    1.64 + 
    1.65 +#include <stdio.h>
    1.66 +#include "ssl_locl.h"
    1.67 +#include <openssl/buffer.h>
    1.68 +#include <openssl/rand.h>
    1.69 +#include <openssl/objects.h>
    1.70 +#include <openssl/evp.h>
    1.71 +
    1.72 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
    1.73 +#include "libssl_wsd.h"
    1.74 +#endif
    1.75 +
    1.76 +#ifdef EMULATOR
    1.77 +
    1.78 +	GET_STATIC_VAR_FROM_TLS(SSLv23_client_method_data,s23_clnt,SSL_METHOD)
    1.79 +	
    1.80 +	#define SSLv23_client_method_data (*GET_WSD_VAR_NAME(SSLv23_client_method_data,s23_clnt,s)())
    1.81 +	
    1.82 +#endif 
    1.83 +static SSL_METHOD *ssl23_get_client_method(int ver);
    1.84 +static int ssl23_client_hello(SSL *s);
    1.85 +static int ssl23_get_server_hello(SSL *s);
    1.86 +static SSL_METHOD *ssl23_get_client_method(int ver)
    1.87 +	{
    1.88 +#ifndef OPENSSL_NO_SSL2
    1.89 +	if (ver == SSL2_VERSION)
    1.90 +		return(SSLv2_client_method());
    1.91 +#endif
    1.92 +	if (ver == SSL3_VERSION)
    1.93 +		return(SSLv3_client_method());
    1.94 +	else if (ver == TLS1_VERSION)
    1.95 +		return(TLSv1_client_method());
    1.96 +	else
    1.97 +		return(NULL);
    1.98 +	}
    1.99 +
   1.100 +EXPORT_C IMPLEMENT_ssl23_meth_func(SSLv23_client_method,
   1.101 +			ssl_undefined_function,
   1.102 +			ssl23_connect,
   1.103 +			ssl23_get_client_method)
   1.104 +
   1.105 +int ssl23_connect(SSL *s)
   1.106 +	{
   1.107 +	BUF_MEM *buf=NULL;
   1.108 +	unsigned long Time=(unsigned long)time(NULL);
   1.109 +	void (*cb)(const SSL *ssl,int type,int val)=NULL;
   1.110 +	int ret= -1;
   1.111 +	int new_state,state;
   1.112 +
   1.113 +	RAND_add(&Time,sizeof(Time),0);
   1.114 +	ERR_clear_error();
   1.115 +	clear_sys_error();
   1.116 +
   1.117 +	if (s->info_callback != NULL)
   1.118 +		cb=s->info_callback;
   1.119 +	else if (s->ctx->info_callback != NULL)
   1.120 +		cb=s->ctx->info_callback;
   1.121 +	
   1.122 +	s->in_handshake++;
   1.123 +	if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 
   1.124 +
   1.125 +	for (;;)
   1.126 +		{
   1.127 +		state=s->state;
   1.128 +
   1.129 +		switch(s->state)
   1.130 +			{
   1.131 +		case SSL_ST_BEFORE:
   1.132 +		case SSL_ST_CONNECT:
   1.133 +		case SSL_ST_BEFORE|SSL_ST_CONNECT:
   1.134 +		case SSL_ST_OK|SSL_ST_CONNECT:
   1.135 +
   1.136 +			if (s->session != NULL)
   1.137 +				{
   1.138 +				SSLerr(SSL_F_SSL23_CONNECT,SSL_R_SSL23_DOING_SESSION_ID_REUSE);
   1.139 +				ret= -1;
   1.140 +				goto end;
   1.141 +				}
   1.142 +			s->server=0;
   1.143 +			if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
   1.144 +
   1.145 +			/* s->version=TLS1_VERSION; */
   1.146 +			s->type=SSL_ST_CONNECT;
   1.147 +
   1.148 +			if (s->init_buf == NULL)
   1.149 +				{
   1.150 +				if ((buf=BUF_MEM_new()) == NULL)
   1.151 +					{
   1.152 +					ret= -1;
   1.153 +					goto end;
   1.154 +					}
   1.155 +				if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
   1.156 +					{
   1.157 +					ret= -1;
   1.158 +					goto end;
   1.159 +					}
   1.160 +				s->init_buf=buf;
   1.161 +				buf=NULL;
   1.162 +				}
   1.163 +
   1.164 +			if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
   1.165 +
   1.166 +			ssl3_init_finished_mac(s);
   1.167 +
   1.168 +			s->state=SSL23_ST_CW_CLNT_HELLO_A;
   1.169 +			s->ctx->stats.sess_connect++;
   1.170 +			s->init_num=0;
   1.171 +			break;
   1.172 +
   1.173 +		case SSL23_ST_CW_CLNT_HELLO_A:
   1.174 +		case SSL23_ST_CW_CLNT_HELLO_B:
   1.175 +
   1.176 +			s->shutdown=0;
   1.177 +			ret=ssl23_client_hello(s);
   1.178 +			if (ret <= 0) goto end;
   1.179 +			s->state=SSL23_ST_CR_SRVR_HELLO_A;
   1.180 +			s->init_num=0;
   1.181 +
   1.182 +			break;
   1.183 +
   1.184 +		case SSL23_ST_CR_SRVR_HELLO_A:
   1.185 +		case SSL23_ST_CR_SRVR_HELLO_B:
   1.186 +			ret=ssl23_get_server_hello(s);
   1.187 +			if (ret >= 0) cb=NULL;
   1.188 +			goto end;
   1.189 +			/* break; */
   1.190 +
   1.191 +		default:
   1.192 +			SSLerr(SSL_F_SSL23_CONNECT,SSL_R_UNKNOWN_STATE);
   1.193 +			ret= -1;
   1.194 +			goto end;
   1.195 +			/* break; */
   1.196 +			}
   1.197 +
   1.198 +		if (s->debug) { (void)BIO_flush(s->wbio); }
   1.199 +
   1.200 +		if ((cb != NULL) && (s->state != state))
   1.201 +			{
   1.202 +			new_state=s->state;
   1.203 +			s->state=state;
   1.204 +			cb(s,SSL_CB_CONNECT_LOOP,1);
   1.205 +			s->state=new_state;
   1.206 +			}
   1.207 +		}
   1.208 +end:
   1.209 +	s->in_handshake--;
   1.210 +	if (buf != NULL)
   1.211 +		BUF_MEM_free(buf);
   1.212 +	if (cb != NULL)
   1.213 +		cb(s,SSL_CB_CONNECT_EXIT,ret);
   1.214 +	return(ret);
   1.215 +	}
   1.216 +
   1.217 +
   1.218 +static int ssl23_client_hello(SSL *s)
   1.219 +	{
   1.220 +	unsigned char *buf;
   1.221 +	unsigned char *p,*d;
   1.222 +	int i,j,ch_len;
   1.223 +	unsigned long Time,l;
   1.224 +	int ssl2_compat;
   1.225 +	int version = 0, version_major, version_minor;
   1.226 +	SSL_COMP *comp;
   1.227 +	int ret;
   1.228 +
   1.229 +	ssl2_compat = (s->options & SSL_OP_NO_SSLv2) ? 0 : 1;
   1.230 +
   1.231 +	if (!(s->options & SSL_OP_NO_TLSv1))
   1.232 +		{
   1.233 +		version = TLS1_VERSION;
   1.234 +		}
   1.235 +	else if (!(s->options & SSL_OP_NO_SSLv3))
   1.236 +		{
   1.237 +		version = SSL3_VERSION;
   1.238 +		}
   1.239 +	else if (!(s->options & SSL_OP_NO_SSLv2))
   1.240 +		{
   1.241 +		version = SSL2_VERSION;
   1.242 +		}
   1.243 +
   1.244 +	buf=(unsigned char *)s->init_buf->data;
   1.245 +	if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
   1.246 +		{
   1.247 +#if 0
   1.248 +		/* don't reuse session-id's */
   1.249 +		if (!ssl_get_new_session(s,0))
   1.250 +			{
   1.251 +			return(-1);
   1.252 +			}
   1.253 +#endif
   1.254 +
   1.255 +		p=s->s3->client_random;
   1.256 +		Time=(unsigned long)time(NULL);			/* Time */
   1.257 +		l2n(Time,p);
   1.258 +		if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
   1.259 +			return -1;
   1.260 +
   1.261 +		if (version == TLS1_VERSION)
   1.262 +			{
   1.263 +			version_major = TLS1_VERSION_MAJOR;
   1.264 +			version_minor = TLS1_VERSION_MINOR;
   1.265 +			}
   1.266 +		else if (version == SSL3_VERSION)
   1.267 +			{
   1.268 +			version_major = SSL3_VERSION_MAJOR;
   1.269 +			version_minor = SSL3_VERSION_MINOR;
   1.270 +			}
   1.271 +		else if (version == SSL2_VERSION)
   1.272 +			{
   1.273 +			version_major = SSL2_VERSION_MAJOR;
   1.274 +			version_minor = SSL2_VERSION_MINOR;
   1.275 +			}
   1.276 +		else
   1.277 +			{
   1.278 +			SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_PROTOCOLS_AVAILABLE);
   1.279 +			return(-1);
   1.280 +			}
   1.281 +
   1.282 +		s->client_version = version;
   1.283 +
   1.284 +		if (ssl2_compat)
   1.285 +			{
   1.286 +			/* create SSL 2.0 compatible Client Hello */
   1.287 +
   1.288 +			/* two byte record header will be written last */
   1.289 +			d = &(buf[2]);
   1.290 +			p = d + 9; /* leave space for message type, version, individual length fields */
   1.291 +
   1.292 +			*(d++) = SSL2_MT_CLIENT_HELLO;
   1.293 +			*(d++) = version_major;
   1.294 +			*(d++) = version_minor;
   1.295 +			
   1.296 +			/* Ciphers supported */
   1.297 +			i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),p,0);
   1.298 +			if (i == 0)
   1.299 +				{
   1.300 +				/* no ciphers */
   1.301 +				SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
   1.302 +				return -1;
   1.303 +				}
   1.304 +			s2n(i,d);
   1.305 +			p+=i;
   1.306 +			
   1.307 +			/* put in the session-id length (zero since there is no reuse) */
   1.308 +#if 0
   1.309 +			s->session->session_id_length=0;
   1.310 +#endif
   1.311 +			s2n(0,d);
   1.312 +
   1.313 +			if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
   1.314 +				ch_len=SSL2_CHALLENGE_LENGTH;
   1.315 +			else
   1.316 +				ch_len=SSL2_MAX_CHALLENGE_LENGTH;
   1.317 +
   1.318 +			/* write out sslv2 challenge */
   1.319 +			if (SSL3_RANDOM_SIZE < ch_len)
   1.320 +				i=SSL3_RANDOM_SIZE;
   1.321 +			else
   1.322 +				i=ch_len;
   1.323 +			s2n(i,d);
   1.324 +			memset(&(s->s3->client_random[0]),0,SSL3_RANDOM_SIZE);
   1.325 +			if (RAND_pseudo_bytes(&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i) <= 0)
   1.326 +				return -1;
   1.327 +
   1.328 +			memcpy(p,&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
   1.329 +			p+=i;
   1.330 +
   1.331 +			i= p- &(buf[2]);
   1.332 +			buf[0]=((i>>8)&0xff)|0x80;
   1.333 +			buf[1]=(i&0xff);
   1.334 +
   1.335 +			/* number of bytes to write */
   1.336 +			s->init_num=i+2;
   1.337 +			s->init_off=0;
   1.338 +
   1.339 +			ssl3_finish_mac(s,&(buf[2]),i);
   1.340 +			}
   1.341 +		else
   1.342 +			{
   1.343 +			/* create Client Hello in SSL 3.0/TLS 1.0 format */
   1.344 +
   1.345 +			/* do the record header (5 bytes) and handshake message header (4 bytes) last */
   1.346 +			d = p = &(buf[9]);
   1.347 +			
   1.348 +			*(p++) = version_major;
   1.349 +			*(p++) = version_minor;
   1.350 +
   1.351 +			/* Random stuff */
   1.352 +			memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
   1.353 +			p += SSL3_RANDOM_SIZE;
   1.354 +
   1.355 +			/* Session ID (zero since there is no reuse) */
   1.356 +			*(p++) = 0;
   1.357 +
   1.358 +			/* Ciphers supported (using SSL 3.0/TLS 1.0 format) */
   1.359 +			i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),ssl3_put_cipher_by_char);
   1.360 +			if (i == 0)
   1.361 +				{
   1.362 +				SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
   1.363 +				return -1;
   1.364 +				}
   1.365 +			s2n(i,p);
   1.366 +			p+=i;
   1.367 +
   1.368 +			/* COMPRESSION */
   1.369 +			if (s->ctx->comp_methods == NULL)
   1.370 +				j=0;
   1.371 +			else
   1.372 +				j=sk_SSL_COMP_num(s->ctx->comp_methods);
   1.373 +			*(p++)=1+j;
   1.374 +			for (i=0; i<j; i++)
   1.375 +				{
   1.376 +				comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
   1.377 +				*(p++)=comp->id;
   1.378 +				}
   1.379 +			*(p++)=0; /* Add the NULL method */
   1.380 +			
   1.381 +			l = p-d;
   1.382 +			*p = 42;
   1.383 +
   1.384 +			/* fill in 4-byte handshake header */
   1.385 +			d=&(buf[5]);
   1.386 +			*(d++)=SSL3_MT_CLIENT_HELLO;
   1.387 +			l2n3(l,d);
   1.388 +
   1.389 +			l += 4;
   1.390 +
   1.391 +			if (l > SSL3_RT_MAX_PLAIN_LENGTH)
   1.392 +				{
   1.393 +				SSLerr(SSL_F_SSL23_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
   1.394 +				return -1;
   1.395 +				}
   1.396 +			
   1.397 +			/* fill in 5-byte record header */
   1.398 +			d=buf;
   1.399 +			*(d++) = SSL3_RT_HANDSHAKE;
   1.400 +			*(d++) = version_major;
   1.401 +			*(d++) = version_minor; /* arguably we should send the *lowest* suported version here
   1.402 +			                         * (indicating, e.g., TLS 1.0 in "SSL 3.0 format") */
   1.403 +			s2n((int)l,d);
   1.404 +
   1.405 +			/* number of bytes to write */
   1.406 +			s->init_num=p-buf;
   1.407 +			s->init_off=0;
   1.408 +
   1.409 +			ssl3_finish_mac(s,&(buf[5]), s->init_num - 5);
   1.410 +			}
   1.411 +
   1.412 +		s->state=SSL23_ST_CW_CLNT_HELLO_B;
   1.413 +		s->init_off=0;
   1.414 +		}
   1.415 +
   1.416 +	/* SSL3_ST_CW_CLNT_HELLO_B */
   1.417 +	ret = ssl23_write_bytes(s);
   1.418 +
   1.419 +	if ((ret >= 2) && s->msg_callback)
   1.420 +		{
   1.421 +		/* Client Hello has been sent; tell msg_callback */
   1.422 +
   1.423 +		if (ssl2_compat)
   1.424 +			s->msg_callback(1, SSL2_VERSION, 0, s->init_buf->data+2, ret-2, s, s->msg_callback_arg);
   1.425 +		else
   1.426 +			s->msg_callback(1, version, SSL3_RT_HANDSHAKE, s->init_buf->data+5, ret-5, s, s->msg_callback_arg);
   1.427 +		}
   1.428 +
   1.429 +	return ret;
   1.430 +	}
   1.431 +
   1.432 +static int ssl23_get_server_hello(SSL *s)
   1.433 +	{
   1.434 +	char buf[8];
   1.435 +	unsigned char *p;
   1.436 +	int i;
   1.437 +	int n;
   1.438 +
   1.439 +	n=ssl23_read_bytes(s,7);
   1.440 +
   1.441 +	if (n != 7) return(n);
   1.442 +	p=s->packet;
   1.443 +
   1.444 +	memcpy(buf,p,n);
   1.445 +
   1.446 +	if ((p[0] & 0x80) && (p[2] == SSL2_MT_SERVER_HELLO) &&
   1.447 +		(p[5] == 0x00) && (p[6] == 0x02))
   1.448 +		{
   1.449 +#ifdef OPENSSL_NO_SSL2
   1.450 +		SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
   1.451 +		goto err;
   1.452 +#else
   1.453 +		/* we are talking sslv2 */
   1.454 +		/* we need to clean up the SSLv3 setup and put in the
   1.455 +		 * sslv2 stuff. */
   1.456 +		int ch_len;
   1.457 +
   1.458 +		if (s->options & SSL_OP_NO_SSLv2)
   1.459 +			{
   1.460 +			SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
   1.461 +			goto err;
   1.462 +			}
   1.463 +		if (s->s2 == NULL)
   1.464 +			{
   1.465 +			if (!ssl2_new(s))
   1.466 +				goto err;
   1.467 +			}
   1.468 +		else
   1.469 +			ssl2_clear(s);
   1.470 +
   1.471 +		if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
   1.472 +			ch_len=SSL2_CHALLENGE_LENGTH;
   1.473 +		else
   1.474 +			ch_len=SSL2_MAX_CHALLENGE_LENGTH;
   1.475 +
   1.476 +		/* write out sslv2 challenge */
   1.477 +		i=(SSL3_RANDOM_SIZE < ch_len)
   1.478 +			?SSL3_RANDOM_SIZE:ch_len;
   1.479 +		s->s2->challenge_length=i;
   1.480 +		memcpy(s->s2->challenge,
   1.481 +			&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
   1.482 +
   1.483 +		if (s->s3 != NULL) ssl3_free(s);
   1.484 +
   1.485 +		if (!BUF_MEM_grow_clean(s->init_buf,
   1.486 +			SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
   1.487 +			{
   1.488 +			SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,ERR_R_BUF_LIB);
   1.489 +			goto err;
   1.490 +			}
   1.491 +
   1.492 +		s->state=SSL2_ST_GET_SERVER_HELLO_A;
   1.493 +		if (!(s->client_version == SSL2_VERSION))
   1.494 +			/* use special padding (SSL 3.0 draft/RFC 2246, App. E.2) */
   1.495 +			s->s2->ssl2_rollback=1;
   1.496 +
   1.497 +		/* setup the 5 bytes we have read so we get them from
   1.498 +		 * the sslv2 buffer */
   1.499 +		s->rstate=SSL_ST_READ_HEADER;
   1.500 +		s->packet_length=n;
   1.501 +		s->packet= &(s->s2->rbuf[0]);
   1.502 +		memcpy(s->packet,buf,n);
   1.503 +		s->s2->rbuf_left=n;
   1.504 +		s->s2->rbuf_offs=0;
   1.505 +
   1.506 +		/* we have already written one */
   1.507 +		s->s2->write_sequence=1;
   1.508 +
   1.509 +		s->method=SSLv2_client_method();
   1.510 +		s->handshake_func=s->method->ssl_connect;
   1.511 +#endif
   1.512 +		}
   1.513 +	else if ((p[0] == SSL3_RT_HANDSHAKE) &&
   1.514 +		 (p[1] == SSL3_VERSION_MAJOR) &&
   1.515 +		 ((p[2] == SSL3_VERSION_MINOR) ||
   1.516 +		  (p[2] == TLS1_VERSION_MINOR)) &&
   1.517 +		 (p[5] == SSL3_MT_SERVER_HELLO))
   1.518 +		{
   1.519 +		/* we have sslv3 or tls1 */
   1.520 +
   1.521 +		if (!ssl_init_wbio_buffer(s,1)) goto err;
   1.522 +
   1.523 +		/* we are in this state */
   1.524 +		s->state=SSL3_ST_CR_SRVR_HELLO_A;
   1.525 +
   1.526 +		/* put the 5 bytes we have read into the input buffer
   1.527 +		 * for SSLv3 */
   1.528 +		s->rstate=SSL_ST_READ_HEADER;
   1.529 +		s->packet_length=n;
   1.530 +		s->packet= &(s->s3->rbuf.buf[0]);
   1.531 +		memcpy(s->packet,buf,n);
   1.532 +		s->s3->rbuf.left=n;
   1.533 +		s->s3->rbuf.offset=0;
   1.534 +
   1.535 +		if ((p[2] == SSL3_VERSION_MINOR) &&
   1.536 +			!(s->options & SSL_OP_NO_SSLv3))
   1.537 +			{
   1.538 +			s->version=SSL3_VERSION;
   1.539 +			s->method=SSLv3_client_method();
   1.540 +			}
   1.541 +		else if ((p[2] == TLS1_VERSION_MINOR) &&
   1.542 +			!(s->options & SSL_OP_NO_TLSv1))
   1.543 +			{
   1.544 +			s->version=TLS1_VERSION;
   1.545 +			s->method=TLSv1_client_method();
   1.546 +			}
   1.547 +		else
   1.548 +			{
   1.549 +			SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
   1.550 +			goto err;
   1.551 +			}
   1.552 +			
   1.553 +		s->handshake_func=s->method->ssl_connect;
   1.554 +		}
   1.555 +	else if ((p[0] == SSL3_RT_ALERT) &&
   1.556 +		 (p[1] == SSL3_VERSION_MAJOR) &&
   1.557 +		 ((p[2] == SSL3_VERSION_MINOR) ||
   1.558 +		  (p[2] == TLS1_VERSION_MINOR)) &&
   1.559 +		 (p[3] == 0) &&
   1.560 +		 (p[4] == 2))
   1.561 +		{
   1.562 +		void (*cb)(const SSL *ssl,int type,int val)=NULL;
   1.563 +		int j;
   1.564 +
   1.565 +		/* An alert */
   1.566 +		if (s->info_callback != NULL)
   1.567 +			cb=s->info_callback;
   1.568 +		else if (s->ctx->info_callback != NULL)
   1.569 +			cb=s->ctx->info_callback;
   1.570 + 
   1.571 +		i=p[5];
   1.572 +		if (cb != NULL)
   1.573 +			{
   1.574 +			j=(i<<8)|p[6];
   1.575 +			cb(s,SSL_CB_READ_ALERT,j);
   1.576 +			}
   1.577 +
   1.578 +		s->rwstate=SSL_NOTHING;
   1.579 +		SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_AD_REASON_OFFSET+p[6]);
   1.580 +		goto err;
   1.581 +		}
   1.582 +	else
   1.583 +		{
   1.584 +		SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNKNOWN_PROTOCOL);
   1.585 +		goto err;
   1.586 +		}
   1.587 +	s->init_num=0;
   1.588 +
   1.589 +	/* Since, if we are sending a ssl23 client hello, we are not
   1.590 +	 * reusing a session-id */
   1.591 +	if (!ssl_get_new_session(s,0))
   1.592 +		goto err;
   1.593 +
   1.594 +	return(SSL_connect(s));
   1.595 +err:
   1.596 +	return(-1);
   1.597 +	}
   1.598 +