xref: /onnv-gate/usr/src/common/openssl/ssl/s23_clnt.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* ssl/s23_clnt.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "ssl_locl.h"
610Sstevel@tonic-gate #include <openssl/buffer.h>
620Sstevel@tonic-gate #include <openssl/rand.h>
630Sstevel@tonic-gate #include <openssl/objects.h>
640Sstevel@tonic-gate #include <openssl/evp.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate static SSL_METHOD *ssl23_get_client_method(int ver);
670Sstevel@tonic-gate static int ssl23_client_hello(SSL *s);
680Sstevel@tonic-gate static int ssl23_get_server_hello(SSL *s);
ssl23_get_client_method(int ver)690Sstevel@tonic-gate static SSL_METHOD *ssl23_get_client_method(int ver)
700Sstevel@tonic-gate 	{
710Sstevel@tonic-gate #ifndef OPENSSL_NO_SSL2
720Sstevel@tonic-gate 	if (ver == SSL2_VERSION)
730Sstevel@tonic-gate 		return(SSLv2_client_method());
740Sstevel@tonic-gate #endif
750Sstevel@tonic-gate 	if (ver == SSL3_VERSION)
760Sstevel@tonic-gate 		return(SSLv3_client_method());
770Sstevel@tonic-gate 	else if (ver == TLS1_VERSION)
780Sstevel@tonic-gate 		return(TLSv1_client_method());
790Sstevel@tonic-gate 	else
800Sstevel@tonic-gate 		return(NULL);
810Sstevel@tonic-gate 	}
820Sstevel@tonic-gate 
IMPLEMENT_ssl23_meth_func(SSLv23_client_method,ssl_undefined_function,ssl23_connect,ssl23_get_client_method)83*2139Sjp161948 IMPLEMENT_ssl23_meth_func(SSLv23_client_method,
84*2139Sjp161948 			ssl_undefined_function,
85*2139Sjp161948 			ssl23_connect,
86*2139Sjp161948 			ssl23_get_client_method)
870Sstevel@tonic-gate 
880Sstevel@tonic-gate int ssl23_connect(SSL *s)
890Sstevel@tonic-gate 	{
900Sstevel@tonic-gate 	BUF_MEM *buf=NULL;
910Sstevel@tonic-gate 	unsigned long Time=time(NULL);
920Sstevel@tonic-gate 	void (*cb)(const SSL *ssl,int type,int val)=NULL;
930Sstevel@tonic-gate 	int ret= -1;
940Sstevel@tonic-gate 	int new_state,state;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	RAND_add(&Time,sizeof(Time),0);
970Sstevel@tonic-gate 	ERR_clear_error();
980Sstevel@tonic-gate 	clear_sys_error();
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	if (s->info_callback != NULL)
1010Sstevel@tonic-gate 		cb=s->info_callback;
1020Sstevel@tonic-gate 	else if (s->ctx->info_callback != NULL)
1030Sstevel@tonic-gate 		cb=s->ctx->info_callback;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	s->in_handshake++;
1060Sstevel@tonic-gate 	if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	for (;;)
1090Sstevel@tonic-gate 		{
1100Sstevel@tonic-gate 		state=s->state;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 		switch(s->state)
1130Sstevel@tonic-gate 			{
1140Sstevel@tonic-gate 		case SSL_ST_BEFORE:
1150Sstevel@tonic-gate 		case SSL_ST_CONNECT:
1160Sstevel@tonic-gate 		case SSL_ST_BEFORE|SSL_ST_CONNECT:
1170Sstevel@tonic-gate 		case SSL_ST_OK|SSL_ST_CONNECT:
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 			if (s->session != NULL)
1200Sstevel@tonic-gate 				{
1210Sstevel@tonic-gate 				SSLerr(SSL_F_SSL23_CONNECT,SSL_R_SSL23_DOING_SESSION_ID_REUSE);
1220Sstevel@tonic-gate 				ret= -1;
1230Sstevel@tonic-gate 				goto end;
1240Sstevel@tonic-gate 				}
1250Sstevel@tonic-gate 			s->server=0;
1260Sstevel@tonic-gate 			if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 			/* s->version=TLS1_VERSION; */
1290Sstevel@tonic-gate 			s->type=SSL_ST_CONNECT;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 			if (s->init_buf == NULL)
1320Sstevel@tonic-gate 				{
1330Sstevel@tonic-gate 				if ((buf=BUF_MEM_new()) == NULL)
1340Sstevel@tonic-gate 					{
1350Sstevel@tonic-gate 					ret= -1;
1360Sstevel@tonic-gate 					goto end;
1370Sstevel@tonic-gate 					}
1380Sstevel@tonic-gate 				if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
1390Sstevel@tonic-gate 					{
1400Sstevel@tonic-gate 					ret= -1;
1410Sstevel@tonic-gate 					goto end;
1420Sstevel@tonic-gate 					}
1430Sstevel@tonic-gate 				s->init_buf=buf;
1440Sstevel@tonic-gate 				buf=NULL;
1450Sstevel@tonic-gate 				}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 			if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 			ssl3_init_finished_mac(s);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 			s->state=SSL23_ST_CW_CLNT_HELLO_A;
1520Sstevel@tonic-gate 			s->ctx->stats.sess_connect++;
1530Sstevel@tonic-gate 			s->init_num=0;
1540Sstevel@tonic-gate 			break;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 		case SSL23_ST_CW_CLNT_HELLO_A:
1570Sstevel@tonic-gate 		case SSL23_ST_CW_CLNT_HELLO_B:
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 			s->shutdown=0;
1600Sstevel@tonic-gate 			ret=ssl23_client_hello(s);
1610Sstevel@tonic-gate 			if (ret <= 0) goto end;
1620Sstevel@tonic-gate 			s->state=SSL23_ST_CR_SRVR_HELLO_A;
1630Sstevel@tonic-gate 			s->init_num=0;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 			break;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 		case SSL23_ST_CR_SRVR_HELLO_A:
1680Sstevel@tonic-gate 		case SSL23_ST_CR_SRVR_HELLO_B:
1690Sstevel@tonic-gate 			ret=ssl23_get_server_hello(s);
1700Sstevel@tonic-gate 			if (ret >= 0) cb=NULL;
1710Sstevel@tonic-gate 			goto end;
1720Sstevel@tonic-gate 			/* break; */
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 		default:
1750Sstevel@tonic-gate 			SSLerr(SSL_F_SSL23_CONNECT,SSL_R_UNKNOWN_STATE);
1760Sstevel@tonic-gate 			ret= -1;
1770Sstevel@tonic-gate 			goto end;
1780Sstevel@tonic-gate 			/* break; */
1790Sstevel@tonic-gate 			}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 		if (s->debug) { (void)BIO_flush(s->wbio); }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 		if ((cb != NULL) && (s->state != state))
1840Sstevel@tonic-gate 			{
1850Sstevel@tonic-gate 			new_state=s->state;
1860Sstevel@tonic-gate 			s->state=state;
1870Sstevel@tonic-gate 			cb(s,SSL_CB_CONNECT_LOOP,1);
1880Sstevel@tonic-gate 			s->state=new_state;
1890Sstevel@tonic-gate 			}
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate end:
1920Sstevel@tonic-gate 	s->in_handshake--;
1930Sstevel@tonic-gate 	if (buf != NULL)
1940Sstevel@tonic-gate 		BUF_MEM_free(buf);
1950Sstevel@tonic-gate 	if (cb != NULL)
1960Sstevel@tonic-gate 		cb(s,SSL_CB_CONNECT_EXIT,ret);
1970Sstevel@tonic-gate 	return(ret);
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 
ssl23_client_hello(SSL * s)2010Sstevel@tonic-gate static int ssl23_client_hello(SSL *s)
2020Sstevel@tonic-gate 	{
2030Sstevel@tonic-gate 	unsigned char *buf;
2040Sstevel@tonic-gate 	unsigned char *p,*d;
205*2139Sjp161948 	int i,j,ch_len;
206*2139Sjp161948 	unsigned long Time,l;
207*2139Sjp161948 	int ssl2_compat;
208*2139Sjp161948 	int version = 0, version_major, version_minor;
209*2139Sjp161948 	SSL_COMP *comp;
2100Sstevel@tonic-gate 	int ret;
2110Sstevel@tonic-gate 
212*2139Sjp161948 	ssl2_compat = (s->options & SSL_OP_NO_SSLv2) ? 0 : 1;
213*2139Sjp161948 
214*2139Sjp161948 	if (!(s->options & SSL_OP_NO_TLSv1))
215*2139Sjp161948 		{
216*2139Sjp161948 		version = TLS1_VERSION;
217*2139Sjp161948 		}
218*2139Sjp161948 	else if (!(s->options & SSL_OP_NO_SSLv3))
219*2139Sjp161948 		{
220*2139Sjp161948 		version = SSL3_VERSION;
221*2139Sjp161948 		}
222*2139Sjp161948 	else if (!(s->options & SSL_OP_NO_SSLv2))
223*2139Sjp161948 		{
224*2139Sjp161948 		version = SSL2_VERSION;
225*2139Sjp161948 		}
226*2139Sjp161948 
2270Sstevel@tonic-gate 	buf=(unsigned char *)s->init_buf->data;
2280Sstevel@tonic-gate 	if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
2290Sstevel@tonic-gate 		{
2300Sstevel@tonic-gate #if 0
2310Sstevel@tonic-gate 		/* don't reuse session-id's */
2320Sstevel@tonic-gate 		if (!ssl_get_new_session(s,0))
2330Sstevel@tonic-gate 			{
2340Sstevel@tonic-gate 			return(-1);
2350Sstevel@tonic-gate 			}
2360Sstevel@tonic-gate #endif
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 		p=s->s3->client_random;
239*2139Sjp161948 		Time=time(NULL);			/* Time */
240*2139Sjp161948 		l2n(Time,p);
241*2139Sjp161948 		if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
242*2139Sjp161948 			return -1;
2430Sstevel@tonic-gate 
244*2139Sjp161948 		if (version == TLS1_VERSION)
2450Sstevel@tonic-gate 			{
246*2139Sjp161948 			version_major = TLS1_VERSION_MAJOR;
247*2139Sjp161948 			version_minor = TLS1_VERSION_MINOR;
2480Sstevel@tonic-gate 			}
249*2139Sjp161948 		else if (version == SSL3_VERSION)
2500Sstevel@tonic-gate 			{
251*2139Sjp161948 			version_major = SSL3_VERSION_MAJOR;
252*2139Sjp161948 			version_minor = SSL3_VERSION_MINOR;
2530Sstevel@tonic-gate 			}
254*2139Sjp161948 		else if (version == SSL2_VERSION)
2550Sstevel@tonic-gate 			{
256*2139Sjp161948 			version_major = SSL2_VERSION_MAJOR;
257*2139Sjp161948 			version_minor = SSL2_VERSION_MINOR;
2580Sstevel@tonic-gate 			}
2590Sstevel@tonic-gate 		else
2600Sstevel@tonic-gate 			{
2610Sstevel@tonic-gate 			SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_PROTOCOLS_AVAILABLE);
2620Sstevel@tonic-gate 			return(-1);
2630Sstevel@tonic-gate 			}
2640Sstevel@tonic-gate 
265*2139Sjp161948 		s->client_version = version;
266*2139Sjp161948 
267*2139Sjp161948 		if (ssl2_compat)
2680Sstevel@tonic-gate 			{
269*2139Sjp161948 			/* create SSL 2.0 compatible Client Hello */
270*2139Sjp161948 
271*2139Sjp161948 			/* two byte record header will be written last */
272*2139Sjp161948 			d = &(buf[2]);
273*2139Sjp161948 			p = d + 9; /* leave space for message type, version, individual length fields */
274*2139Sjp161948 
275*2139Sjp161948 			*(d++) = SSL2_MT_CLIENT_HELLO;
276*2139Sjp161948 			*(d++) = version_major;
277*2139Sjp161948 			*(d++) = version_minor;
278*2139Sjp161948 
279*2139Sjp161948 			/* Ciphers supported */
280*2139Sjp161948 			i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),p,0);
281*2139Sjp161948 			if (i == 0)
282*2139Sjp161948 				{
283*2139Sjp161948 				/* no ciphers */
284*2139Sjp161948 				SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
285*2139Sjp161948 				return -1;
286*2139Sjp161948 				}
287*2139Sjp161948 			s2n(i,d);
288*2139Sjp161948 			p+=i;
289*2139Sjp161948 
290*2139Sjp161948 			/* put in the session-id length (zero since there is no reuse) */
291*2139Sjp161948 #if 0
292*2139Sjp161948 			s->session->session_id_length=0;
293*2139Sjp161948 #endif
294*2139Sjp161948 			s2n(0,d);
295*2139Sjp161948 
296*2139Sjp161948 			if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
297*2139Sjp161948 				ch_len=SSL2_CHALLENGE_LENGTH;
298*2139Sjp161948 			else
299*2139Sjp161948 				ch_len=SSL2_MAX_CHALLENGE_LENGTH;
300*2139Sjp161948 
301*2139Sjp161948 			/* write out sslv2 challenge */
302*2139Sjp161948 			if (SSL3_RANDOM_SIZE < ch_len)
303*2139Sjp161948 				i=SSL3_RANDOM_SIZE;
304*2139Sjp161948 			else
305*2139Sjp161948 				i=ch_len;
306*2139Sjp161948 			s2n(i,d);
307*2139Sjp161948 			memset(&(s->s3->client_random[0]),0,SSL3_RANDOM_SIZE);
308*2139Sjp161948 			if (RAND_pseudo_bytes(&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i) <= 0)
309*2139Sjp161948 				return -1;
310*2139Sjp161948 
311*2139Sjp161948 			memcpy(p,&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
312*2139Sjp161948 			p+=i;
313*2139Sjp161948 
314*2139Sjp161948 			i= p- &(buf[2]);
315*2139Sjp161948 			buf[0]=((i>>8)&0xff)|0x80;
316*2139Sjp161948 			buf[1]=(i&0xff);
317*2139Sjp161948 
318*2139Sjp161948 			/* number of bytes to write */
319*2139Sjp161948 			s->init_num=i+2;
320*2139Sjp161948 			s->init_off=0;
321*2139Sjp161948 
322*2139Sjp161948 			ssl3_finish_mac(s,&(buf[2]),i);
3230Sstevel@tonic-gate 			}
324*2139Sjp161948 		else
325*2139Sjp161948 			{
326*2139Sjp161948 			/* create Client Hello in SSL 3.0/TLS 1.0 format */
3270Sstevel@tonic-gate 
328*2139Sjp161948 			/* do the record header (5 bytes) and handshake message header (4 bytes) last */
329*2139Sjp161948 			d = p = &(buf[9]);
330*2139Sjp161948 
331*2139Sjp161948 			*(p++) = version_major;
332*2139Sjp161948 			*(p++) = version_minor;
333*2139Sjp161948 
334*2139Sjp161948 			/* Random stuff */
335*2139Sjp161948 			memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
336*2139Sjp161948 			p += SSL3_RANDOM_SIZE;
337*2139Sjp161948 
338*2139Sjp161948 			/* Session ID (zero since there is no reuse) */
339*2139Sjp161948 			*(p++) = 0;
340*2139Sjp161948 
341*2139Sjp161948 			/* Ciphers supported (using SSL 3.0/TLS 1.0 format) */
342*2139Sjp161948 			i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),ssl3_put_cipher_by_char);
343*2139Sjp161948 			if (i == 0)
344*2139Sjp161948 				{
345*2139Sjp161948 				SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
346*2139Sjp161948 				return -1;
347*2139Sjp161948 				}
348*2139Sjp161948 			s2n(i,p);
349*2139Sjp161948 			p+=i;
3500Sstevel@tonic-gate 
351*2139Sjp161948 			/* COMPRESSION */
352*2139Sjp161948 			if (s->ctx->comp_methods == NULL)
353*2139Sjp161948 				j=0;
354*2139Sjp161948 			else
355*2139Sjp161948 				j=sk_SSL_COMP_num(s->ctx->comp_methods);
356*2139Sjp161948 			*(p++)=1+j;
357*2139Sjp161948 			for (i=0; i<j; i++)
358*2139Sjp161948 				{
359*2139Sjp161948 				comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
360*2139Sjp161948 				*(p++)=comp->id;
361*2139Sjp161948 				}
362*2139Sjp161948 			*(p++)=0; /* Add the NULL method */
363*2139Sjp161948 
364*2139Sjp161948 			l = p-d;
365*2139Sjp161948 			*p = 42;
366*2139Sjp161948 
367*2139Sjp161948 			/* fill in 4-byte handshake header */
368*2139Sjp161948 			d=&(buf[5]);
369*2139Sjp161948 			*(d++)=SSL3_MT_CLIENT_HELLO;
370*2139Sjp161948 			l2n3(l,d);
3710Sstevel@tonic-gate 
372*2139Sjp161948 			l += 4;
373*2139Sjp161948 
374*2139Sjp161948 			if (l > SSL3_RT_MAX_PLAIN_LENGTH)
375*2139Sjp161948 				{
376*2139Sjp161948 				SSLerr(SSL_F_SSL23_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
377*2139Sjp161948 				return -1;
378*2139Sjp161948 				}
379*2139Sjp161948 
380*2139Sjp161948 			/* fill in 5-byte record header */
381*2139Sjp161948 			d=buf;
382*2139Sjp161948 			*(d++) = SSL3_RT_HANDSHAKE;
383*2139Sjp161948 			*(d++) = version_major;
384*2139Sjp161948 			*(d++) = version_minor; /* arguably we should send the *lowest* suported version here
385*2139Sjp161948 			                         * (indicating, e.g., TLS 1.0 in "SSL 3.0 format") */
386*2139Sjp161948 			s2n((int)l,d);
387*2139Sjp161948 
388*2139Sjp161948 			/* number of bytes to write */
389*2139Sjp161948 			s->init_num=p-buf;
390*2139Sjp161948 			s->init_off=0;
391*2139Sjp161948 
392*2139Sjp161948 			ssl3_finish_mac(s,&(buf[5]), s->init_num - 5);
393*2139Sjp161948 			}
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 		s->state=SSL23_ST_CW_CLNT_HELLO_B;
3960Sstevel@tonic-gate 		s->init_off=0;
3970Sstevel@tonic-gate 		}
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	/* SSL3_ST_CW_CLNT_HELLO_B */
4000Sstevel@tonic-gate 	ret = ssl23_write_bytes(s);
401*2139Sjp161948 
402*2139Sjp161948 	if ((ret >= 2) && s->msg_callback)
403*2139Sjp161948 		{
404*2139Sjp161948 		/* Client Hello has been sent; tell msg_callback */
405*2139Sjp161948 
406*2139Sjp161948 		if (ssl2_compat)
407*2139Sjp161948 			s->msg_callback(1, SSL2_VERSION, 0, s->init_buf->data+2, ret-2, s, s->msg_callback_arg);
408*2139Sjp161948 		else
409*2139Sjp161948 			s->msg_callback(1, version, SSL3_RT_HANDSHAKE, s->init_buf->data+5, ret-5, s, s->msg_callback_arg);
410*2139Sjp161948 		}
411*2139Sjp161948 
4120Sstevel@tonic-gate 	return ret;
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 
ssl23_get_server_hello(SSL * s)4150Sstevel@tonic-gate static int ssl23_get_server_hello(SSL *s)
4160Sstevel@tonic-gate 	{
4170Sstevel@tonic-gate 	char buf[8];
4180Sstevel@tonic-gate 	unsigned char *p;
4190Sstevel@tonic-gate 	int i;
4200Sstevel@tonic-gate 	int n;
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	n=ssl23_read_bytes(s,7);
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	if (n != 7) return(n);
4250Sstevel@tonic-gate 	p=s->packet;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	memcpy(buf,p,n);
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	if ((p[0] & 0x80) && (p[2] == SSL2_MT_SERVER_HELLO) &&
4300Sstevel@tonic-gate 		(p[5] == 0x00) && (p[6] == 0x02))
4310Sstevel@tonic-gate 		{
4320Sstevel@tonic-gate #ifdef OPENSSL_NO_SSL2
4330Sstevel@tonic-gate 		SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
4340Sstevel@tonic-gate 		goto err;
4350Sstevel@tonic-gate #else
4360Sstevel@tonic-gate 		/* we are talking sslv2 */
4370Sstevel@tonic-gate 		/* we need to clean up the SSLv3 setup and put in the
4380Sstevel@tonic-gate 		 * sslv2 stuff. */
4390Sstevel@tonic-gate 		int ch_len;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 		if (s->options & SSL_OP_NO_SSLv2)
4420Sstevel@tonic-gate 			{
4430Sstevel@tonic-gate 			SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
4440Sstevel@tonic-gate 			goto err;
4450Sstevel@tonic-gate 			}
4460Sstevel@tonic-gate 		if (s->s2 == NULL)
4470Sstevel@tonic-gate 			{
4480Sstevel@tonic-gate 			if (!ssl2_new(s))
4490Sstevel@tonic-gate 				goto err;
4500Sstevel@tonic-gate 			}
4510Sstevel@tonic-gate 		else
4520Sstevel@tonic-gate 			ssl2_clear(s);
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 		if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
4550Sstevel@tonic-gate 			ch_len=SSL2_CHALLENGE_LENGTH;
4560Sstevel@tonic-gate 		else
4570Sstevel@tonic-gate 			ch_len=SSL2_MAX_CHALLENGE_LENGTH;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 		/* write out sslv2 challenge */
4600Sstevel@tonic-gate 		i=(SSL3_RANDOM_SIZE < ch_len)
4610Sstevel@tonic-gate 			?SSL3_RANDOM_SIZE:ch_len;
4620Sstevel@tonic-gate 		s->s2->challenge_length=i;
4630Sstevel@tonic-gate 		memcpy(s->s2->challenge,
4640Sstevel@tonic-gate 			&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 		if (s->s3 != NULL) ssl3_free(s);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		if (!BUF_MEM_grow_clean(s->init_buf,
4690Sstevel@tonic-gate 			SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
4700Sstevel@tonic-gate 			{
4710Sstevel@tonic-gate 			SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,ERR_R_BUF_LIB);
4720Sstevel@tonic-gate 			goto err;
4730Sstevel@tonic-gate 			}
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 		s->state=SSL2_ST_GET_SERVER_HELLO_A;
4760Sstevel@tonic-gate 		if (!(s->client_version == SSL2_VERSION))
4770Sstevel@tonic-gate 			/* use special padding (SSL 3.0 draft/RFC 2246, App. E.2) */
4780Sstevel@tonic-gate 			s->s2->ssl2_rollback=1;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 		/* setup the 5 bytes we have read so we get them from
4810Sstevel@tonic-gate 		 * the sslv2 buffer */
4820Sstevel@tonic-gate 		s->rstate=SSL_ST_READ_HEADER;
4830Sstevel@tonic-gate 		s->packet_length=n;
4840Sstevel@tonic-gate 		s->packet= &(s->s2->rbuf[0]);
4850Sstevel@tonic-gate 		memcpy(s->packet,buf,n);
4860Sstevel@tonic-gate 		s->s2->rbuf_left=n;
4870Sstevel@tonic-gate 		s->s2->rbuf_offs=0;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 		/* we have already written one */
4900Sstevel@tonic-gate 		s->s2->write_sequence=1;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 		s->method=SSLv2_client_method();
4930Sstevel@tonic-gate 		s->handshake_func=s->method->ssl_connect;
4940Sstevel@tonic-gate #endif
4950Sstevel@tonic-gate 		}
4960Sstevel@tonic-gate 	else if ((p[0] == SSL3_RT_HANDSHAKE) &&
4970Sstevel@tonic-gate 		 (p[1] == SSL3_VERSION_MAJOR) &&
4980Sstevel@tonic-gate 		 ((p[2] == SSL3_VERSION_MINOR) ||
4990Sstevel@tonic-gate 		  (p[2] == TLS1_VERSION_MINOR)) &&
5000Sstevel@tonic-gate 		 (p[5] == SSL3_MT_SERVER_HELLO))
5010Sstevel@tonic-gate 		{
5020Sstevel@tonic-gate 		/* we have sslv3 or tls1 */
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 		if (!ssl_init_wbio_buffer(s,1)) goto err;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 		/* we are in this state */
5070Sstevel@tonic-gate 		s->state=SSL3_ST_CR_SRVR_HELLO_A;
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 		/* put the 5 bytes we have read into the input buffer
5100Sstevel@tonic-gate 		 * for SSLv3 */
5110Sstevel@tonic-gate 		s->rstate=SSL_ST_READ_HEADER;
5120Sstevel@tonic-gate 		s->packet_length=n;
5130Sstevel@tonic-gate 		s->packet= &(s->s3->rbuf.buf[0]);
5140Sstevel@tonic-gate 		memcpy(s->packet,buf,n);
5150Sstevel@tonic-gate 		s->s3->rbuf.left=n;
5160Sstevel@tonic-gate 		s->s3->rbuf.offset=0;
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 		if ((p[2] == SSL3_VERSION_MINOR) &&
5190Sstevel@tonic-gate 			!(s->options & SSL_OP_NO_SSLv3))
5200Sstevel@tonic-gate 			{
5210Sstevel@tonic-gate 			s->version=SSL3_VERSION;
5220Sstevel@tonic-gate 			s->method=SSLv3_client_method();
5230Sstevel@tonic-gate 			}
5240Sstevel@tonic-gate 		else if ((p[2] == TLS1_VERSION_MINOR) &&
5250Sstevel@tonic-gate 			!(s->options & SSL_OP_NO_TLSv1))
5260Sstevel@tonic-gate 			{
5270Sstevel@tonic-gate 			s->version=TLS1_VERSION;
5280Sstevel@tonic-gate 			s->method=TLSv1_client_method();
5290Sstevel@tonic-gate 			}
5300Sstevel@tonic-gate 		else
5310Sstevel@tonic-gate 			{
5320Sstevel@tonic-gate 			SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
5330Sstevel@tonic-gate 			goto err;
5340Sstevel@tonic-gate 			}
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 		s->handshake_func=s->method->ssl_connect;
5370Sstevel@tonic-gate 		}
5380Sstevel@tonic-gate 	else if ((p[0] == SSL3_RT_ALERT) &&
5390Sstevel@tonic-gate 		 (p[1] == SSL3_VERSION_MAJOR) &&
5400Sstevel@tonic-gate 		 ((p[2] == SSL3_VERSION_MINOR) ||
5410Sstevel@tonic-gate 		  (p[2] == TLS1_VERSION_MINOR)) &&
5420Sstevel@tonic-gate 		 (p[3] == 0) &&
5430Sstevel@tonic-gate 		 (p[4] == 2))
5440Sstevel@tonic-gate 		{
5450Sstevel@tonic-gate 		void (*cb)(const SSL *ssl,int type,int val)=NULL;
5460Sstevel@tonic-gate 		int j;
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 		/* An alert */
5490Sstevel@tonic-gate 		if (s->info_callback != NULL)
5500Sstevel@tonic-gate 			cb=s->info_callback;
5510Sstevel@tonic-gate 		else if (s->ctx->info_callback != NULL)
5520Sstevel@tonic-gate 			cb=s->ctx->info_callback;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 		i=p[5];
5550Sstevel@tonic-gate 		if (cb != NULL)
5560Sstevel@tonic-gate 			{
5570Sstevel@tonic-gate 			j=(i<<8)|p[6];
5580Sstevel@tonic-gate 			cb(s,SSL_CB_READ_ALERT,j);
5590Sstevel@tonic-gate 			}
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 		s->rwstate=SSL_NOTHING;
5620Sstevel@tonic-gate 		SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_AD_REASON_OFFSET+p[6]);
5630Sstevel@tonic-gate 		goto err;
5640Sstevel@tonic-gate 		}
5650Sstevel@tonic-gate 	else
5660Sstevel@tonic-gate 		{
5670Sstevel@tonic-gate 		SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNKNOWN_PROTOCOL);
5680Sstevel@tonic-gate 		goto err;
5690Sstevel@tonic-gate 		}
5700Sstevel@tonic-gate 	s->init_num=0;
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	/* Since, if we are sending a ssl23 client hello, we are not
5730Sstevel@tonic-gate 	 * reusing a session-id */
5740Sstevel@tonic-gate 	if (!ssl_get_new_session(s,0))
5750Sstevel@tonic-gate 		goto err;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	s->first_packet=1;
5780Sstevel@tonic-gate 	return(SSL_connect(s));
5790Sstevel@tonic-gate err:
5800Sstevel@tonic-gate 	return(-1);
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate 
583