xref: /onnv-gate/usr/src/common/openssl/ssl/s23_clnt.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* ssl/s23_clnt.c */
2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * This package is an SSL implementation written
6*0Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
7*0Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
10*0Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
11*0Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
12*0Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*0Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
14*0Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
17*0Sstevel@tonic-gate  * the code are not to be removed.
18*0Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
19*0Sstevel@tonic-gate  * as the author of the parts of the library used.
20*0Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
21*0Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate  * are met:
26*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
27*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate  *    must display the following acknowledgement:
33*0Sstevel@tonic-gate  *    "This product includes cryptographic software written by
34*0Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
35*0Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
36*0Sstevel@tonic-gate  *    being used are not cryptographic related :-).
37*0Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
38*0Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
39*0Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*0Sstevel@tonic-gate  * SUCH DAMAGE.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
54*0Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*0Sstevel@tonic-gate  * copied and put under another distribution licence
56*0Sstevel@tonic-gate  * [including the GNU Public Licence.]
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include <stdio.h>
60*0Sstevel@tonic-gate #include "ssl_locl.h"
61*0Sstevel@tonic-gate #include <openssl/buffer.h>
62*0Sstevel@tonic-gate #include <openssl/rand.h>
63*0Sstevel@tonic-gate #include <openssl/objects.h>
64*0Sstevel@tonic-gate #include <openssl/evp.h>
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate static SSL_METHOD *ssl23_get_client_method(int ver);
67*0Sstevel@tonic-gate static int ssl23_client_hello(SSL *s);
68*0Sstevel@tonic-gate static int ssl23_get_server_hello(SSL *s);
69*0Sstevel@tonic-gate static SSL_METHOD *ssl23_get_client_method(int ver)
70*0Sstevel@tonic-gate 	{
71*0Sstevel@tonic-gate #ifndef OPENSSL_NO_SSL2
72*0Sstevel@tonic-gate 	if (ver == SSL2_VERSION)
73*0Sstevel@tonic-gate 		return(SSLv2_client_method());
74*0Sstevel@tonic-gate #endif
75*0Sstevel@tonic-gate 	if (ver == SSL3_VERSION)
76*0Sstevel@tonic-gate 		return(SSLv3_client_method());
77*0Sstevel@tonic-gate 	else if (ver == TLS1_VERSION)
78*0Sstevel@tonic-gate 		return(TLSv1_client_method());
79*0Sstevel@tonic-gate 	else
80*0Sstevel@tonic-gate 		return(NULL);
81*0Sstevel@tonic-gate 	}
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate SSL_METHOD *SSLv23_client_method(void)
84*0Sstevel@tonic-gate 	{
85*0Sstevel@tonic-gate 	static int init=1;
86*0Sstevel@tonic-gate 	static SSL_METHOD SSLv23_client_data;
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	if (init)
89*0Sstevel@tonic-gate 		{
90*0Sstevel@tonic-gate 		CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD);
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 		if (init)
93*0Sstevel@tonic-gate 			{
94*0Sstevel@tonic-gate 			memcpy((char *)&SSLv23_client_data,
95*0Sstevel@tonic-gate 				(char *)sslv23_base_method(),sizeof(SSL_METHOD));
96*0Sstevel@tonic-gate 			SSLv23_client_data.ssl_connect=ssl23_connect;
97*0Sstevel@tonic-gate 			SSLv23_client_data.get_ssl_method=ssl23_get_client_method;
98*0Sstevel@tonic-gate 			init=0;
99*0Sstevel@tonic-gate 			}
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 		CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD);
102*0Sstevel@tonic-gate 		}
103*0Sstevel@tonic-gate 	return(&SSLv23_client_data);
104*0Sstevel@tonic-gate 	}
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate int ssl23_connect(SSL *s)
107*0Sstevel@tonic-gate 	{
108*0Sstevel@tonic-gate 	BUF_MEM *buf=NULL;
109*0Sstevel@tonic-gate 	unsigned long Time=time(NULL);
110*0Sstevel@tonic-gate 	void (*cb)(const SSL *ssl,int type,int val)=NULL;
111*0Sstevel@tonic-gate 	int ret= -1;
112*0Sstevel@tonic-gate 	int new_state,state;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	RAND_add(&Time,sizeof(Time),0);
115*0Sstevel@tonic-gate 	ERR_clear_error();
116*0Sstevel@tonic-gate 	clear_sys_error();
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	if (s->info_callback != NULL)
119*0Sstevel@tonic-gate 		cb=s->info_callback;
120*0Sstevel@tonic-gate 	else if (s->ctx->info_callback != NULL)
121*0Sstevel@tonic-gate 		cb=s->ctx->info_callback;
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	s->in_handshake++;
124*0Sstevel@tonic-gate 	if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	for (;;)
127*0Sstevel@tonic-gate 		{
128*0Sstevel@tonic-gate 		state=s->state;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 		switch(s->state)
131*0Sstevel@tonic-gate 			{
132*0Sstevel@tonic-gate 		case SSL_ST_BEFORE:
133*0Sstevel@tonic-gate 		case SSL_ST_CONNECT:
134*0Sstevel@tonic-gate 		case SSL_ST_BEFORE|SSL_ST_CONNECT:
135*0Sstevel@tonic-gate 		case SSL_ST_OK|SSL_ST_CONNECT:
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 			if (s->session != NULL)
138*0Sstevel@tonic-gate 				{
139*0Sstevel@tonic-gate 				SSLerr(SSL_F_SSL23_CONNECT,SSL_R_SSL23_DOING_SESSION_ID_REUSE);
140*0Sstevel@tonic-gate 				ret= -1;
141*0Sstevel@tonic-gate 				goto end;
142*0Sstevel@tonic-gate 				}
143*0Sstevel@tonic-gate 			s->server=0;
144*0Sstevel@tonic-gate 			if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 			/* s->version=TLS1_VERSION; */
147*0Sstevel@tonic-gate 			s->type=SSL_ST_CONNECT;
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 			if (s->init_buf == NULL)
150*0Sstevel@tonic-gate 				{
151*0Sstevel@tonic-gate 				if ((buf=BUF_MEM_new()) == NULL)
152*0Sstevel@tonic-gate 					{
153*0Sstevel@tonic-gate 					ret= -1;
154*0Sstevel@tonic-gate 					goto end;
155*0Sstevel@tonic-gate 					}
156*0Sstevel@tonic-gate 				if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
157*0Sstevel@tonic-gate 					{
158*0Sstevel@tonic-gate 					ret= -1;
159*0Sstevel@tonic-gate 					goto end;
160*0Sstevel@tonic-gate 					}
161*0Sstevel@tonic-gate 				s->init_buf=buf;
162*0Sstevel@tonic-gate 				buf=NULL;
163*0Sstevel@tonic-gate 				}
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 			if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 			ssl3_init_finished_mac(s);
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 			s->state=SSL23_ST_CW_CLNT_HELLO_A;
170*0Sstevel@tonic-gate 			s->ctx->stats.sess_connect++;
171*0Sstevel@tonic-gate 			s->init_num=0;
172*0Sstevel@tonic-gate 			break;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 		case SSL23_ST_CW_CLNT_HELLO_A:
175*0Sstevel@tonic-gate 		case SSL23_ST_CW_CLNT_HELLO_B:
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 			s->shutdown=0;
178*0Sstevel@tonic-gate 			ret=ssl23_client_hello(s);
179*0Sstevel@tonic-gate 			if (ret <= 0) goto end;
180*0Sstevel@tonic-gate 			s->state=SSL23_ST_CR_SRVR_HELLO_A;
181*0Sstevel@tonic-gate 			s->init_num=0;
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 			break;
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 		case SSL23_ST_CR_SRVR_HELLO_A:
186*0Sstevel@tonic-gate 		case SSL23_ST_CR_SRVR_HELLO_B:
187*0Sstevel@tonic-gate 			ret=ssl23_get_server_hello(s);
188*0Sstevel@tonic-gate 			if (ret >= 0) cb=NULL;
189*0Sstevel@tonic-gate 			goto end;
190*0Sstevel@tonic-gate 			/* break; */
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 		default:
193*0Sstevel@tonic-gate 			SSLerr(SSL_F_SSL23_CONNECT,SSL_R_UNKNOWN_STATE);
194*0Sstevel@tonic-gate 			ret= -1;
195*0Sstevel@tonic-gate 			goto end;
196*0Sstevel@tonic-gate 			/* break; */
197*0Sstevel@tonic-gate 			}
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 		if (s->debug) { (void)BIO_flush(s->wbio); }
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 		if ((cb != NULL) && (s->state != state))
202*0Sstevel@tonic-gate 			{
203*0Sstevel@tonic-gate 			new_state=s->state;
204*0Sstevel@tonic-gate 			s->state=state;
205*0Sstevel@tonic-gate 			cb(s,SSL_CB_CONNECT_LOOP,1);
206*0Sstevel@tonic-gate 			s->state=new_state;
207*0Sstevel@tonic-gate 			}
208*0Sstevel@tonic-gate 		}
209*0Sstevel@tonic-gate end:
210*0Sstevel@tonic-gate 	s->in_handshake--;
211*0Sstevel@tonic-gate 	if (buf != NULL)
212*0Sstevel@tonic-gate 		BUF_MEM_free(buf);
213*0Sstevel@tonic-gate 	if (cb != NULL)
214*0Sstevel@tonic-gate 		cb(s,SSL_CB_CONNECT_EXIT,ret);
215*0Sstevel@tonic-gate 	return(ret);
216*0Sstevel@tonic-gate 	}
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate static int ssl23_client_hello(SSL *s)
220*0Sstevel@tonic-gate 	{
221*0Sstevel@tonic-gate 	unsigned char *buf;
222*0Sstevel@tonic-gate 	unsigned char *p,*d;
223*0Sstevel@tonic-gate 	int i,ch_len;
224*0Sstevel@tonic-gate 	int ret;
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	buf=(unsigned char *)s->init_buf->data;
227*0Sstevel@tonic-gate 	if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
228*0Sstevel@tonic-gate 		{
229*0Sstevel@tonic-gate #if 0
230*0Sstevel@tonic-gate 		/* don't reuse session-id's */
231*0Sstevel@tonic-gate 		if (!ssl_get_new_session(s,0))
232*0Sstevel@tonic-gate 			{
233*0Sstevel@tonic-gate 			return(-1);
234*0Sstevel@tonic-gate 			}
235*0Sstevel@tonic-gate #endif
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 		p=s->s3->client_random;
238*0Sstevel@tonic-gate 		RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE);
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 		/* Do the message type and length last */
241*0Sstevel@tonic-gate 		d= &(buf[2]);
242*0Sstevel@tonic-gate 		p=d+9;
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 		*(d++)=SSL2_MT_CLIENT_HELLO;
245*0Sstevel@tonic-gate 		if (!(s->options & SSL_OP_NO_TLSv1))
246*0Sstevel@tonic-gate 			{
247*0Sstevel@tonic-gate 			*(d++)=TLS1_VERSION_MAJOR;
248*0Sstevel@tonic-gate 			*(d++)=TLS1_VERSION_MINOR;
249*0Sstevel@tonic-gate 			s->client_version=TLS1_VERSION;
250*0Sstevel@tonic-gate 			}
251*0Sstevel@tonic-gate 		else if (!(s->options & SSL_OP_NO_SSLv3))
252*0Sstevel@tonic-gate 			{
253*0Sstevel@tonic-gate 			*(d++)=SSL3_VERSION_MAJOR;
254*0Sstevel@tonic-gate 			*(d++)=SSL3_VERSION_MINOR;
255*0Sstevel@tonic-gate 			s->client_version=SSL3_VERSION;
256*0Sstevel@tonic-gate 			}
257*0Sstevel@tonic-gate 		else if (!(s->options & SSL_OP_NO_SSLv2))
258*0Sstevel@tonic-gate 			{
259*0Sstevel@tonic-gate 			*(d++)=SSL2_VERSION_MAJOR;
260*0Sstevel@tonic-gate 			*(d++)=SSL2_VERSION_MINOR;
261*0Sstevel@tonic-gate 			s->client_version=SSL2_VERSION;
262*0Sstevel@tonic-gate 			}
263*0Sstevel@tonic-gate 		else
264*0Sstevel@tonic-gate 			{
265*0Sstevel@tonic-gate 			SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_PROTOCOLS_AVAILABLE);
266*0Sstevel@tonic-gate 			return(-1);
267*0Sstevel@tonic-gate 			}
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 		/* Ciphers supported */
270*0Sstevel@tonic-gate 		i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),p);
271*0Sstevel@tonic-gate 		if (i == 0)
272*0Sstevel@tonic-gate 			{
273*0Sstevel@tonic-gate 			/* no ciphers */
274*0Sstevel@tonic-gate 			SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
275*0Sstevel@tonic-gate 			return(-1);
276*0Sstevel@tonic-gate 			}
277*0Sstevel@tonic-gate 		s2n(i,d);
278*0Sstevel@tonic-gate 		p+=i;
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 		/* put in the session-id, zero since there is no
281*0Sstevel@tonic-gate 		 * reuse. */
282*0Sstevel@tonic-gate #if 0
283*0Sstevel@tonic-gate 		s->session->session_id_length=0;
284*0Sstevel@tonic-gate #endif
285*0Sstevel@tonic-gate 		s2n(0,d);
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 		if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
288*0Sstevel@tonic-gate 			ch_len=SSL2_CHALLENGE_LENGTH;
289*0Sstevel@tonic-gate 		else
290*0Sstevel@tonic-gate 			ch_len=SSL2_MAX_CHALLENGE_LENGTH;
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 		/* write out sslv2 challenge */
293*0Sstevel@tonic-gate 		if (SSL3_RANDOM_SIZE < ch_len)
294*0Sstevel@tonic-gate 			i=SSL3_RANDOM_SIZE;
295*0Sstevel@tonic-gate 		else
296*0Sstevel@tonic-gate 			i=ch_len;
297*0Sstevel@tonic-gate 		s2n(i,d);
298*0Sstevel@tonic-gate 		memset(&(s->s3->client_random[0]),0,SSL3_RANDOM_SIZE);
299*0Sstevel@tonic-gate 		RAND_pseudo_bytes(&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
300*0Sstevel@tonic-gate 		memcpy(p,&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
301*0Sstevel@tonic-gate 		p+=i;
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 		i= p- &(buf[2]);
304*0Sstevel@tonic-gate 		buf[0]=((i>>8)&0xff)|0x80;
305*0Sstevel@tonic-gate 		buf[1]=(i&0xff);
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 		s->state=SSL23_ST_CW_CLNT_HELLO_B;
308*0Sstevel@tonic-gate 		/* number of bytes to write */
309*0Sstevel@tonic-gate 		s->init_num=i+2;
310*0Sstevel@tonic-gate 		s->init_off=0;
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 		ssl3_finish_mac(s,&(buf[2]),i);
313*0Sstevel@tonic-gate 		}
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	/* SSL3_ST_CW_CLNT_HELLO_B */
316*0Sstevel@tonic-gate 	ret = ssl23_write_bytes(s);
317*0Sstevel@tonic-gate 	if (ret >= 2)
318*0Sstevel@tonic-gate 		if (s->msg_callback)
319*0Sstevel@tonic-gate 			s->msg_callback(1, SSL2_VERSION, 0, s->init_buf->data+2, ret-2, s, s->msg_callback_arg); /* CLIENT-HELLO */
320*0Sstevel@tonic-gate 	return ret;
321*0Sstevel@tonic-gate 	}
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate static int ssl23_get_server_hello(SSL *s)
324*0Sstevel@tonic-gate 	{
325*0Sstevel@tonic-gate 	char buf[8];
326*0Sstevel@tonic-gate 	unsigned char *p;
327*0Sstevel@tonic-gate 	int i;
328*0Sstevel@tonic-gate 	int n;
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	n=ssl23_read_bytes(s,7);
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 	if (n != 7) return(n);
333*0Sstevel@tonic-gate 	p=s->packet;
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 	memcpy(buf,p,n);
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	if ((p[0] & 0x80) && (p[2] == SSL2_MT_SERVER_HELLO) &&
338*0Sstevel@tonic-gate 		(p[5] == 0x00) && (p[6] == 0x02))
339*0Sstevel@tonic-gate 		{
340*0Sstevel@tonic-gate #ifdef OPENSSL_NO_SSL2
341*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
342*0Sstevel@tonic-gate 		goto err;
343*0Sstevel@tonic-gate #else
344*0Sstevel@tonic-gate 		/* we are talking sslv2 */
345*0Sstevel@tonic-gate 		/* we need to clean up the SSLv3 setup and put in the
346*0Sstevel@tonic-gate 		 * sslv2 stuff. */
347*0Sstevel@tonic-gate 		int ch_len;
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 		if (s->options & SSL_OP_NO_SSLv2)
350*0Sstevel@tonic-gate 			{
351*0Sstevel@tonic-gate 			SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
352*0Sstevel@tonic-gate 			goto err;
353*0Sstevel@tonic-gate 			}
354*0Sstevel@tonic-gate 		if (s->s2 == NULL)
355*0Sstevel@tonic-gate 			{
356*0Sstevel@tonic-gate 			if (!ssl2_new(s))
357*0Sstevel@tonic-gate 				goto err;
358*0Sstevel@tonic-gate 			}
359*0Sstevel@tonic-gate 		else
360*0Sstevel@tonic-gate 			ssl2_clear(s);
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 		if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
363*0Sstevel@tonic-gate 			ch_len=SSL2_CHALLENGE_LENGTH;
364*0Sstevel@tonic-gate 		else
365*0Sstevel@tonic-gate 			ch_len=SSL2_MAX_CHALLENGE_LENGTH;
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 		/* write out sslv2 challenge */
368*0Sstevel@tonic-gate 		i=(SSL3_RANDOM_SIZE < ch_len)
369*0Sstevel@tonic-gate 			?SSL3_RANDOM_SIZE:ch_len;
370*0Sstevel@tonic-gate 		s->s2->challenge_length=i;
371*0Sstevel@tonic-gate 		memcpy(s->s2->challenge,
372*0Sstevel@tonic-gate 			&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 		if (s->s3 != NULL) ssl3_free(s);
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate 		if (!BUF_MEM_grow_clean(s->init_buf,
377*0Sstevel@tonic-gate 			SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
378*0Sstevel@tonic-gate 			{
379*0Sstevel@tonic-gate 			SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,ERR_R_BUF_LIB);
380*0Sstevel@tonic-gate 			goto err;
381*0Sstevel@tonic-gate 			}
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 		s->state=SSL2_ST_GET_SERVER_HELLO_A;
384*0Sstevel@tonic-gate 		if (!(s->client_version == SSL2_VERSION))
385*0Sstevel@tonic-gate 			/* use special padding (SSL 3.0 draft/RFC 2246, App. E.2) */
386*0Sstevel@tonic-gate 			s->s2->ssl2_rollback=1;
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 		/* setup the 5 bytes we have read so we get them from
389*0Sstevel@tonic-gate 		 * the sslv2 buffer */
390*0Sstevel@tonic-gate 		s->rstate=SSL_ST_READ_HEADER;
391*0Sstevel@tonic-gate 		s->packet_length=n;
392*0Sstevel@tonic-gate 		s->packet= &(s->s2->rbuf[0]);
393*0Sstevel@tonic-gate 		memcpy(s->packet,buf,n);
394*0Sstevel@tonic-gate 		s->s2->rbuf_left=n;
395*0Sstevel@tonic-gate 		s->s2->rbuf_offs=0;
396*0Sstevel@tonic-gate 
397*0Sstevel@tonic-gate 		/* we have already written one */
398*0Sstevel@tonic-gate 		s->s2->write_sequence=1;
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 		s->method=SSLv2_client_method();
401*0Sstevel@tonic-gate 		s->handshake_func=s->method->ssl_connect;
402*0Sstevel@tonic-gate #endif
403*0Sstevel@tonic-gate 		}
404*0Sstevel@tonic-gate 	else if ((p[0] == SSL3_RT_HANDSHAKE) &&
405*0Sstevel@tonic-gate 		 (p[1] == SSL3_VERSION_MAJOR) &&
406*0Sstevel@tonic-gate 		 ((p[2] == SSL3_VERSION_MINOR) ||
407*0Sstevel@tonic-gate 		  (p[2] == TLS1_VERSION_MINOR)) &&
408*0Sstevel@tonic-gate 		 (p[5] == SSL3_MT_SERVER_HELLO))
409*0Sstevel@tonic-gate 		{
410*0Sstevel@tonic-gate 		/* we have sslv3 or tls1 */
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate 		if (!ssl_init_wbio_buffer(s,1)) goto err;
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 		/* we are in this state */
415*0Sstevel@tonic-gate 		s->state=SSL3_ST_CR_SRVR_HELLO_A;
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate 		/* put the 5 bytes we have read into the input buffer
418*0Sstevel@tonic-gate 		 * for SSLv3 */
419*0Sstevel@tonic-gate 		s->rstate=SSL_ST_READ_HEADER;
420*0Sstevel@tonic-gate 		s->packet_length=n;
421*0Sstevel@tonic-gate 		s->packet= &(s->s3->rbuf.buf[0]);
422*0Sstevel@tonic-gate 		memcpy(s->packet,buf,n);
423*0Sstevel@tonic-gate 		s->s3->rbuf.left=n;
424*0Sstevel@tonic-gate 		s->s3->rbuf.offset=0;
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 		if ((p[2] == SSL3_VERSION_MINOR) &&
427*0Sstevel@tonic-gate 			!(s->options & SSL_OP_NO_SSLv3))
428*0Sstevel@tonic-gate 			{
429*0Sstevel@tonic-gate 			s->version=SSL3_VERSION;
430*0Sstevel@tonic-gate 			s->method=SSLv3_client_method();
431*0Sstevel@tonic-gate 			}
432*0Sstevel@tonic-gate 		else if ((p[2] == TLS1_VERSION_MINOR) &&
433*0Sstevel@tonic-gate 			!(s->options & SSL_OP_NO_TLSv1))
434*0Sstevel@tonic-gate 			{
435*0Sstevel@tonic-gate 			s->version=TLS1_VERSION;
436*0Sstevel@tonic-gate 			s->method=TLSv1_client_method();
437*0Sstevel@tonic-gate 			}
438*0Sstevel@tonic-gate 		else
439*0Sstevel@tonic-gate 			{
440*0Sstevel@tonic-gate 			SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
441*0Sstevel@tonic-gate 			goto err;
442*0Sstevel@tonic-gate 			}
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 		s->handshake_func=s->method->ssl_connect;
445*0Sstevel@tonic-gate 		}
446*0Sstevel@tonic-gate 	else if ((p[0] == SSL3_RT_ALERT) &&
447*0Sstevel@tonic-gate 		 (p[1] == SSL3_VERSION_MAJOR) &&
448*0Sstevel@tonic-gate 		 ((p[2] == SSL3_VERSION_MINOR) ||
449*0Sstevel@tonic-gate 		  (p[2] == TLS1_VERSION_MINOR)) &&
450*0Sstevel@tonic-gate 		 (p[3] == 0) &&
451*0Sstevel@tonic-gate 		 (p[4] == 2))
452*0Sstevel@tonic-gate 		{
453*0Sstevel@tonic-gate 		void (*cb)(const SSL *ssl,int type,int val)=NULL;
454*0Sstevel@tonic-gate 		int j;
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 		/* An alert */
457*0Sstevel@tonic-gate 		if (s->info_callback != NULL)
458*0Sstevel@tonic-gate 			cb=s->info_callback;
459*0Sstevel@tonic-gate 		else if (s->ctx->info_callback != NULL)
460*0Sstevel@tonic-gate 			cb=s->ctx->info_callback;
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 		i=p[5];
463*0Sstevel@tonic-gate 		if (cb != NULL)
464*0Sstevel@tonic-gate 			{
465*0Sstevel@tonic-gate 			j=(i<<8)|p[6];
466*0Sstevel@tonic-gate 			cb(s,SSL_CB_READ_ALERT,j);
467*0Sstevel@tonic-gate 			}
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 		s->rwstate=SSL_NOTHING;
470*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_AD_REASON_OFFSET+p[6]);
471*0Sstevel@tonic-gate 		goto err;
472*0Sstevel@tonic-gate 		}
473*0Sstevel@tonic-gate 	else
474*0Sstevel@tonic-gate 		{
475*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNKNOWN_PROTOCOL);
476*0Sstevel@tonic-gate 		goto err;
477*0Sstevel@tonic-gate 		}
478*0Sstevel@tonic-gate 	s->init_num=0;
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 	/* Since, if we are sending a ssl23 client hello, we are not
481*0Sstevel@tonic-gate 	 * reusing a session-id */
482*0Sstevel@tonic-gate 	if (!ssl_get_new_session(s,0))
483*0Sstevel@tonic-gate 		goto err;
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 	s->first_packet=1;
486*0Sstevel@tonic-gate 	return(SSL_connect(s));
487*0Sstevel@tonic-gate err:
488*0Sstevel@tonic-gate 	return(-1);
489*0Sstevel@tonic-gate 	}
490*0Sstevel@tonic-gate 
491