s23_srvr.c revision 89840
1161651Skan/* ssl/s23_srvr.c */
2161651Skan/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3161651Skan * All rights reserved.
4161651Skan *
5161651Skan * This package is an SSL implementation written
6161651Skan * by Eric Young (eay@cryptsoft.com).
7161651Skan * The implementation was written so as to conform with Netscapes SSL.
8161651Skan *
9161651Skan * This library is free for commercial and non-commercial use as long as
10161651Skan * the following conditions are aheared to.  The following conditions
11161651Skan * apply to all code found in this distribution, be it the RC4, RSA,
12161651Skan * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13161651Skan * included with this distribution is covered by the same copyright terms
14161651Skan * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15161651Skan *
16161651Skan * Copyright remains Eric Young's, and as such any Copyright notices in
17161651Skan * the code are not to be removed.
18161651Skan * If this package is used in a product, Eric Young should be given attribution
19161651Skan * as the author of the parts of the library used.
20161651Skan * This can be in the form of a textual message at program startup or
21161651Skan * in documentation (online or textual) provided with the package.
22161651Skan *
23161651Skan * Redistribution and use in source and binary forms, with or without
24161651Skan * modification, are permitted provided that the following conditions
25161651Skan * are met:
26161651Skan * 1. Redistributions of source code must retain the copyright
27161651Skan *    notice, this list of conditions and the following disclaimer.
28161651Skan * 2. Redistributions in binary form must reproduce the above copyright
29161651Skan *    notice, this list of conditions and the following disclaimer in the
30161651Skan *    documentation and/or other materials provided with the distribution.
31161651Skan * 3. All advertising materials mentioning features or use of this software
32161651Skan *    must display the following acknowledgement:
33161651Skan *    "This product includes cryptographic software written by
34161651Skan *     Eric Young (eay@cryptsoft.com)"
35161651Skan *    The word 'cryptographic' can be left out if the rouines from the library
36161651Skan *    being used are not cryptographic related :-).
37161651Skan * 4. If you include any Windows specific code (or a derivative thereof) from
38161651Skan *    the apps directory (application code) you must include an acknowledgement:
39161651Skan *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40161651Skan *
41161651Skan * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42161651Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43161651Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44161651Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45161651Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46161651Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47161651Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48161651Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49161651Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50161651Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51161651Skan * SUCH DAMAGE.
52161651Skan *
53161651Skan * The licence and distribution terms for any publically available version or
54161651Skan * derivative of this code cannot be changed.  i.e. this code cannot simply be
55161651Skan * copied and put under another distribution licence
56161651Skan * [including the GNU Public Licence.]
57161651Skan *
58161651Skan * $FreeBSD: head/crypto/openssl/ssl/s23_srvr.c 89840 2002-01-27 03:17:13Z kris $
59161651Skan */
60161651Skan/* ====================================================================
61161651Skan * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
62161651Skan *
63161651Skan * Redistribution and use in source and binary forms, with or without
64161651Skan * modification, are permitted provided that the following conditions
65161651Skan * are met:
66161651Skan *
67161651Skan * 1. Redistributions of source code must retain the above copyright
68161651Skan *    notice, this list of conditions and the following disclaimer.
69161651Skan *
70161651Skan * 2. Redistributions in binary form must reproduce the above copyright
71161651Skan *    notice, this list of conditions and the following disclaimer in
72161651Skan *    the documentation and/or other materials provided with the
73161651Skan *    distribution.
74161651Skan *
75161651Skan * 3. All advertising materials mentioning features or use of this
76161651Skan *    software must display the following acknowledgment:
77161651Skan *    "This product includes software developed by the OpenSSL Project
78161651Skan *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
79161651Skan *
80161651Skan * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
81161651Skan *    endorse or promote products derived from this software without
82161651Skan *    prior written permission. For written permission, please contact
83161651Skan *    openssl-core@openssl.org.
84161651Skan *
85161651Skan * 5. Products derived from this software may not be called "OpenSSL"
86161651Skan *    nor may "OpenSSL" appear in their names without prior written
87161651Skan *    permission of the OpenSSL Project.
88161651Skan *
89161651Skan * 6. Redistributions of any form whatsoever must retain the following
90161651Skan *    acknowledgment:
91161651Skan *    "This product includes software developed by the OpenSSL Project
92161651Skan *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
93161651Skan *
94161651Skan * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
95161651Skan * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96161651Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
97161651Skan * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
98161651Skan * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
99161651Skan * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
100161651Skan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
101161651Skan * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
102161651Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
103161651Skan * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
104161651Skan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
105161651Skan * OF THE POSSIBILITY OF SUCH DAMAGE.
106161651Skan * ====================================================================
107161651Skan *
108161651Skan * This product includes cryptographic software written by Eric Young
109161651Skan * (eay@cryptsoft.com).  This product includes software written by Tim
110161651Skan * Hudson (tjh@cryptsoft.com).
111161651Skan *
112161651Skan */
113161651Skan
114161651Skan#include <stdio.h>
115161651Skan#include <openssl/buffer.h>
116161651Skan#include <openssl/rand.h>
117161651Skan#include <openssl/objects.h>
118161651Skan#include <openssl/evp.h>
119161651Skan#include "ssl_locl.h"
120161651Skan
121161651Skanstatic SSL_METHOD *ssl23_get_server_method(int ver);
122161651Skanint ssl23_get_client_hello(SSL *s);
123161651Skanstatic SSL_METHOD *ssl23_get_server_method(int ver)
124161651Skan	{
125161651Skan#ifndef NO_SSL2
126161651Skan	if (ver == SSL2_VERSION)
127161651Skan		return(SSLv2_server_method());
128161651Skan#endif
129161651Skan	if (ver == SSL3_VERSION)
130161651Skan		return(SSLv3_server_method());
131161651Skan	else if (ver == TLS1_VERSION)
132161651Skan		return(TLSv1_server_method());
133161651Skan	else
134161651Skan		return(NULL);
135161651Skan	}
136161651Skan
137161651SkanSSL_METHOD *SSLv23_server_method(void)
138161651Skan	{
139161651Skan	static int init=1;
140161651Skan	static SSL_METHOD SSLv23_server_data;
141161651Skan
142161651Skan	if (init)
143161651Skan		{
144161651Skan		memcpy((char *)&SSLv23_server_data,
145161651Skan			(char *)sslv23_base_method(),sizeof(SSL_METHOD));
146161651Skan		SSLv23_server_data.ssl_accept=ssl23_accept;
147161651Skan		SSLv23_server_data.get_ssl_method=ssl23_get_server_method;
148161651Skan		init=0;
149161651Skan		}
150161651Skan	return(&SSLv23_server_data);
151161651Skan	}
152161651Skan
153161651Skanint ssl23_accept(SSL *s)
154161651Skan	{
155161651Skan	BUF_MEM *buf;
156161651Skan	unsigned long Time=time(NULL);
157161651Skan	void (*cb)()=NULL;
158161651Skan	int ret= -1;
159161651Skan	int new_state,state;
160161651Skan
161161651Skan	RAND_add(&Time,sizeof(Time),0);
162161651Skan	ERR_clear_error();
163161651Skan	clear_sys_error();
164161651Skan
165161651Skan	if (s->info_callback != NULL)
166161651Skan		cb=s->info_callback;
167161651Skan	else if (s->ctx->info_callback != NULL)
168161651Skan		cb=s->ctx->info_callback;
169161651Skan
170161651Skan	s->in_handshake++;
171161651Skan	if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
172161651Skan
173161651Skan	for (;;)
174161651Skan		{
175161651Skan		state=s->state;
176161651Skan
177161651Skan		switch(s->state)
178161651Skan			{
179161651Skan		case SSL_ST_BEFORE:
180161651Skan		case SSL_ST_ACCEPT:
181161651Skan		case SSL_ST_BEFORE|SSL_ST_ACCEPT:
182161651Skan		case SSL_ST_OK|SSL_ST_ACCEPT:
183161651Skan
184161651Skan			s->server=1;
185161651Skan			if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
186161651Skan
187161651Skan			/* s->version=SSL3_VERSION; */
188161651Skan			s->type=SSL_ST_ACCEPT;
189161651Skan
190161651Skan			if (s->init_buf == NULL)
191161651Skan				{
192161651Skan				if ((buf=BUF_MEM_new()) == NULL)
193161651Skan					{
194161651Skan					ret= -1;
195161651Skan					goto end;
196161651Skan					}
197161651Skan				if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
198161651Skan					{
199161651Skan					ret= -1;
200161651Skan					goto end;
201161651Skan					}
202161651Skan				s->init_buf=buf;
203161651Skan				}
204161651Skan
205161651Skan			ssl3_init_finished_mac(s);
206161651Skan
207161651Skan			s->state=SSL23_ST_SR_CLNT_HELLO_A;
208161651Skan			s->ctx->stats.sess_accept++;
209161651Skan			s->init_num=0;
210161651Skan			break;
211161651Skan
212161651Skan		case SSL23_ST_SR_CLNT_HELLO_A:
213161651Skan		case SSL23_ST_SR_CLNT_HELLO_B:
214161651Skan
215161651Skan			s->shutdown=0;
216161651Skan			ret=ssl23_get_client_hello(s);
217161651Skan			if (ret >= 0) cb=NULL;
218161651Skan			goto end;
219161651Skan			/* break; */
220
221		default:
222			SSLerr(SSL_F_SSL23_ACCEPT,SSL_R_UNKNOWN_STATE);
223			ret= -1;
224			goto end;
225			/* break; */
226			}
227
228		if ((cb != NULL) && (s->state != state))
229			{
230			new_state=s->state;
231			s->state=state;
232			cb(s,SSL_CB_ACCEPT_LOOP,1);
233			s->state=new_state;
234			}
235		}
236end:
237	s->in_handshake--;
238	if (cb != NULL)
239		cb(s,SSL_CB_ACCEPT_EXIT,ret);
240	return(ret);
241	}
242
243
244int ssl23_get_client_hello(SSL *s)
245	{
246	char buf_space[11]; /* Request this many bytes in initial read.
247	                     * We can detect SSL 3.0/TLS 1.0 Client Hellos
248	                     * ('type == 3') correctly only when the following
249	                     * is in a single record, which is not guaranteed by
250	                     * the protocol specification:
251	                     * Byte  Content
252	                     *  0     type            \
253	                     *  1/2   version          > record header
254	                     *  3/4   length          /
255	                     *  5     msg_type        \
256	                     *  6-8   length           > Client Hello message
257	                     *  9/10  client_version  /
258	                     */
259	char *buf= &(buf_space[0]);
260	unsigned char *p,*d,*dd;
261	unsigned int i;
262	unsigned int csl,sil,cl;
263	int n=0,j;
264	int type=0;
265	int v[2];
266#ifndef NO_RSA
267	int use_sslv2_strong=0;
268#endif
269
270	if (s->state ==	SSL23_ST_SR_CLNT_HELLO_A)
271		{
272		/* read the initial header */
273		v[0]=v[1]=0;
274
275		if (!ssl3_setup_buffers(s)) goto err;
276
277		n=ssl23_read_bytes(s, sizeof buf_space);
278		if (n != sizeof buf_space) return(n); /* n == -1 || n == 0 */
279
280		p=s->packet;
281
282		memcpy(buf,p,n);
283
284		if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO))
285			{
286			/*
287			 * SSLv2 header
288			 */
289			if ((p[3] == 0x00) && (p[4] == 0x02))
290				{
291				v[0]=p[3]; v[1]=p[4];
292				/* SSLv2 */
293				if (!(s->options & SSL_OP_NO_SSLv2))
294					type=1;
295				}
296			else if (p[3] == SSL3_VERSION_MAJOR)
297				{
298				v[0]=p[3]; v[1]=p[4];
299				/* SSLv3/TLSv1 */
300				if (p[4] >= TLS1_VERSION_MINOR)
301					{
302					if (!(s->options & SSL_OP_NO_TLSv1))
303						{
304						s->version=TLS1_VERSION;
305						/* type=2; */ /* done later to survive restarts */
306						s->state=SSL23_ST_SR_CLNT_HELLO_B;
307						}
308					else if (!(s->options & SSL_OP_NO_SSLv3))
309						{
310						s->version=SSL3_VERSION;
311						/* type=2; */
312						s->state=SSL23_ST_SR_CLNT_HELLO_B;
313						}
314					else if (!(s->options & SSL_OP_NO_SSLv2))
315						{
316						type=1;
317						}
318					}
319				else if (!(s->options & SSL_OP_NO_SSLv3))
320					{
321					s->version=SSL3_VERSION;
322					/* type=2; */
323					s->state=SSL23_ST_SR_CLNT_HELLO_B;
324					}
325				else if (!(s->options & SSL_OP_NO_SSLv2))
326					type=1;
327
328				if (s->options & SSL_OP_NON_EXPORT_FIRST)
329					/* Not only utterly confusing, but broken
330					 * ('fractured programming'?) -- the details
331					 * of this block nearly make it work
332					 * as intended in this environment, but on one
333					 * of the fine points (w.r.t. restarts) it fails.
334					 * The obvious fix would be even more devastating
335					 * to program structure; if you want the functionality,
336					 * throw this away and implement it in a way
337					 * that makes sense */
338					{
339#if 0
340					STACK_OF(SSL_CIPHER) *sk;
341					SSL_CIPHER *c;
342					int ne2,ne3;
343
344					j=((p[0]&0x7f)<<8)|p[1];
345					if (j > (1024*4))
346						{
347						SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE);
348						goto err;
349						}
350
351					n=ssl23_read_bytes(s,j+2);
352					if (n <= 0) return(n);
353					p=s->packet;
354
355					if ((buf=OPENSSL_malloc(n)) == NULL)
356						{
357						SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,ERR_R_MALLOC_FAILURE);
358						goto err;
359						}
360					memcpy(buf,p,n);
361
362					p+=5;
363					n2s(p,csl);
364					p+=4;
365
366					sk=ssl_bytes_to_cipher_list(
367						s,p,csl,NULL);
368					if (sk != NULL)
369						{
370						ne2=ne3=0;
371						for (j=0; j<sk_SSL_CIPHER_num(sk); j++)
372							{
373							c=sk_SSL_CIPHER_value(sk,j);
374							if (!SSL_C_IS_EXPORT(c))
375								{
376								if ((c->id>>24L) == 2L)
377									ne2=1;
378								else
379									ne3=1;
380								}
381							}
382						if (ne2 && !ne3)
383							{
384							type=1;
385							use_sslv2_strong=1;
386							goto next_bit;
387							}
388						}
389#else
390					SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNSUPPORTED_OPTION);
391					goto err;
392#endif
393					}
394				}
395			}
396		else if ((p[0] == SSL3_RT_HANDSHAKE) &&
397			 (p[1] == SSL3_VERSION_MAJOR) &&
398			 (p[5] == SSL3_MT_CLIENT_HELLO) &&
399			 ((p[3] == 0 && p[4] < 5 /* silly record length? */)
400				|| (p[9] == p[1])))
401			{
402			/*
403			 * SSLv3 or tls1 header
404			 */
405
406			v[0]=p[1]; /* major version (= SSL3_VERSION_MAJOR) */
407			/* We must look at client_version inside the Client Hello message
408			 * to get the correct minor version.
409			 * However if we have only a pathologically small fragment of the
410			 * Client Hello message, this would be difficult, and we'd have
411			 * to read more records to find out.
412			 * No known SSL 3.0 client fragments ClientHello like this,
413			 * so we simply assume TLS 1.0 to avoid protocol version downgrade
414			 * attacks. */
415			if (p[3] == 0 && p[4] < 6)
416				{
417#if 0
418				SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_SMALL);
419				goto err;
420#else
421				v[1] = TLS1_VERSION_MINOR;
422#endif
423				}
424			else
425				v[1]=p[10]; /* minor version according to client_version */
426			if (v[1] >= TLS1_VERSION_MINOR)
427				{
428				if (!(s->options & SSL_OP_NO_TLSv1))
429					{
430					s->version=TLS1_VERSION;
431					type=3;
432					}
433				else if (!(s->options & SSL_OP_NO_SSLv3))
434					{
435					s->version=SSL3_VERSION;
436					type=3;
437					}
438				}
439			else
440				{
441				/* client requests SSL 3.0 */
442				if (!(s->options & SSL_OP_NO_SSLv3))
443					{
444					s->version=SSL3_VERSION;
445					type=3;
446					}
447				else if (!(s->options & SSL_OP_NO_TLSv1))
448					{
449					/* we won't be able to use TLS of course,
450					 * but this will send an appropriate alert */
451					s->version=TLS1_VERSION;
452					type=3;
453					}
454				}
455			}
456		else if ((strncmp("GET ", (char *)p,4) == 0) ||
457			 (strncmp("POST ",(char *)p,5) == 0) ||
458			 (strncmp("HEAD ",(char *)p,5) == 0) ||
459			 (strncmp("PUT ", (char *)p,4) == 0))
460			{
461			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTP_REQUEST);
462			goto err;
463			}
464		else if (strncmp("CONNECT",(char *)p,7) == 0)
465			{
466			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTPS_PROXY_REQUEST);
467			goto err;
468			}
469		}
470
471	if (s->state == SSL23_ST_SR_CLNT_HELLO_B)
472		{
473		/* we have SSLv3/TLSv1 in an SSLv2 header
474		 * (other cases skip this state) */
475
476		type=2;
477		p=s->packet;
478		v[0] = p[3]; /* == SSL3_VERSION_MAJOR */
479		v[1] = p[4];
480
481		n=((p[0]&0x7f)<<8)|p[1];
482		if (n > (1024*4))
483			{
484			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE);
485			goto err;
486			}
487
488		j=ssl23_read_bytes(s,n+2);
489		if (j <= 0) return(j);
490
491		ssl3_finish_mac(s,&(s->packet[2]),s->packet_length-2);
492
493		p=s->packet;
494		p+=5;
495		n2s(p,csl);
496		n2s(p,sil);
497		n2s(p,cl);
498		d=(unsigned char *)s->init_buf->data;
499		if ((csl+sil+cl+11) != s->packet_length)
500			{
501			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_LENGTH_MISMATCH);
502			goto err;
503			}
504
505		*(d++) = SSL3_VERSION_MAJOR; /* == v[0] */
506		*(d++) = v[1];
507
508		/* lets populate the random area */
509		/* get the challenge_length */
510		i=(cl > SSL3_RANDOM_SIZE)?SSL3_RANDOM_SIZE:cl;
511		memset(d,0,SSL3_RANDOM_SIZE);
512		memcpy(&(d[SSL3_RANDOM_SIZE-i]),&(p[csl+sil]),i);
513		d+=SSL3_RANDOM_SIZE;
514
515		/* no session-id reuse */
516		*(d++)=0;
517
518		/* ciphers */
519		j=0;
520		dd=d;
521		d+=2;
522		for (i=0; i<csl; i+=3)
523			{
524			if (p[i] != 0) continue;
525			*(d++)=p[i+1];
526			*(d++)=p[i+2];
527			j+=2;
528			}
529		s2n(j,dd);
530
531		/* COMPRESSION */
532		*(d++)=1;
533		*(d++)=0;
534
535		i=(d-(unsigned char *)s->init_buf->data);
536
537		/* get the data reused from the init_buf */
538		s->s3->tmp.reuse_message=1;
539		s->s3->tmp.message_type=SSL3_MT_CLIENT_HELLO;
540		s->s3->tmp.message_size=i;
541		}
542
543	/* imaginary new state (for program structure): */
544	/* s->state = SSL23_SR_CLNT_HELLO_C */
545
546	if (type == 1)
547		{
548#ifdef NO_SSL2
549		SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
550		goto err;
551#else
552		/* we are talking sslv2 */
553		/* we need to clean up the SSLv3/TLSv1 setup and put in the
554		 * sslv2 stuff. */
555
556		if (s->s2 == NULL)
557			{
558			if (!ssl2_new(s))
559				goto err;
560			}
561		else
562			ssl2_clear(s);
563
564		if (s->s3 != NULL) ssl3_free(s);
565
566		if (!BUF_MEM_grow(s->init_buf,
567			SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
568			{
569			goto err;
570			}
571
572		s->state=SSL2_ST_GET_CLIENT_HELLO_A;
573		if ((s->options & SSL_OP_MSIE_SSLV2_RSA_PADDING) ||
574			use_sslv2_strong ||
575			(s->options & SSL_OP_NO_TLSv1 && s->options & SSL_OP_NO_SSLv3))
576			s->s2->ssl2_rollback=0;
577		else
578			/* reject SSL 2.0 session if client supports SSL 3.0 or TLS 1.0
579			 * (SSL 3.0 draft/RFC 2246, App. E.2) */
580			s->s2->ssl2_rollback=1;
581
582		/* setup the n bytes we have read so we get them from
583		 * the sslv2 buffer */
584		s->rstate=SSL_ST_READ_HEADER;
585		s->packet_length=n;
586		s->packet= &(s->s2->rbuf[0]);
587		memcpy(s->packet,buf,n);
588		s->s2->rbuf_left=n;
589		s->s2->rbuf_offs=0;
590
591		s->method=SSLv2_server_method();
592		s->handshake_func=s->method->ssl_accept;
593#endif
594		}
595
596	if ((type == 2) || (type == 3))
597		{
598		/* we have SSLv3/TLSv1 (type 2: SSL2 style, type 3: SSL3/TLS style) */
599
600		if (!ssl_init_wbio_buffer(s,1)) goto err;
601
602		/* we are in this state */
603		s->state=SSL3_ST_SR_CLNT_HELLO_A;
604
605		if (type == 3)
606			{
607			/* put the 'n' bytes we have read into the input buffer
608			 * for SSLv3 */
609			s->rstate=SSL_ST_READ_HEADER;
610			s->packet_length=n;
611			s->packet= &(s->s3->rbuf.buf[0]);
612			memcpy(s->packet,buf,n);
613			s->s3->rbuf.left=n;
614			s->s3->rbuf.offset=0;
615			}
616		else
617			{
618			s->packet_length=0;
619			s->s3->rbuf.left=0;
620			s->s3->rbuf.offset=0;
621			}
622
623		if (s->version == TLS1_VERSION)
624			s->method = TLSv1_server_method();
625		else
626			s->method = SSLv3_server_method();
627#if 0 /* ssl3_get_client_hello does this */
628		s->client_version=(v[0]<<8)|v[1];
629#endif
630		s->handshake_func=s->method->ssl_accept;
631		}
632
633	if ((type < 1) || (type > 3))
634		{
635		/* bad, very bad */
636		SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNKNOWN_PROTOCOL);
637		goto err;
638		}
639	s->init_num=0;
640
641	if (buf != buf_space) OPENSSL_free(buf);
642	s->first_packet=1;
643	return(SSL_accept(s));
644err:
645	if (buf != buf_space) OPENSSL_free(buf);
646	return(-1);
647	}
648