s23_srvr.c revision 110007
155714Skris/* ssl/s23_srvr.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5889840Skris/* ====================================================================
5989840Skris * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
6089840Skris *
6189840Skris * Redistribution and use in source and binary forms, with or without
6289840Skris * modification, are permitted provided that the following conditions
6389840Skris * are met:
6489840Skris *
6589840Skris * 1. Redistributions of source code must retain the above copyright
6689840Skris *    notice, this list of conditions and the following disclaimer.
6789840Skris *
6889840Skris * 2. Redistributions in binary form must reproduce the above copyright
6989840Skris *    notice, this list of conditions and the following disclaimer in
7089840Skris *    the documentation and/or other materials provided with the
7189840Skris *    distribution.
7289840Skris *
7389840Skris * 3. All advertising materials mentioning features or use of this
7489840Skris *    software must display the following acknowledgment:
7589840Skris *    "This product includes software developed by the OpenSSL Project
7689840Skris *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
7789840Skris *
7889840Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
7989840Skris *    endorse or promote products derived from this software without
8089840Skris *    prior written permission. For written permission, please contact
8189840Skris *    openssl-core@openssl.org.
8289840Skris *
8389840Skris * 5. Products derived from this software may not be called "OpenSSL"
8489840Skris *    nor may "OpenSSL" appear in their names without prior written
8589840Skris *    permission of the OpenSSL Project.
8689840Skris *
8789840Skris * 6. Redistributions of any form whatsoever must retain the following
8889840Skris *    acknowledgment:
8989840Skris *    "This product includes software developed by the OpenSSL Project
9089840Skris *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
9189840Skris *
9289840Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
9389840Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9489840Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
9589840Skris * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
9689840Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9789840Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
9889840Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
9989840Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
10089840Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
10189840Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
10289840Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
10389840Skris * OF THE POSSIBILITY OF SUCH DAMAGE.
10489840Skris * ====================================================================
10589840Skris *
10689840Skris * This product includes cryptographic software written by Eric Young
10789840Skris * (eay@cryptsoft.com).  This product includes software written by Tim
10889840Skris * Hudson (tjh@cryptsoft.com).
10989840Skris *
11089840Skris */
11155714Skris
11255714Skris#include <stdio.h>
113110007Smarkm#include "ssl_locl.h"
11455714Skris#include <openssl/buffer.h>
11555714Skris#include <openssl/rand.h>
11655714Skris#include <openssl/objects.h>
11755714Skris#include <openssl/evp.h>
11855714Skris
11955714Skrisstatic SSL_METHOD *ssl23_get_server_method(int ver);
12055714Skrisint ssl23_get_client_hello(SSL *s);
12155714Skrisstatic SSL_METHOD *ssl23_get_server_method(int ver)
12255714Skris	{
123110007Smarkm#ifndef OPENSSL_NO_SSL2
12455714Skris	if (ver == SSL2_VERSION)
12555714Skris		return(SSLv2_server_method());
12655949Skris#endif
12755714Skris	if (ver == SSL3_VERSION)
12855714Skris		return(SSLv3_server_method());
12955714Skris	else if (ver == TLS1_VERSION)
13055714Skris		return(TLSv1_server_method());
13155714Skris	else
13255714Skris		return(NULL);
13355714Skris	}
13455714Skris
13555714SkrisSSL_METHOD *SSLv23_server_method(void)
13655714Skris	{
13755714Skris	static int init=1;
13855714Skris	static SSL_METHOD SSLv23_server_data;
13955714Skris
14055714Skris	if (init)
14155714Skris		{
142110007Smarkm		CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD);
143110007Smarkm
144110007Smarkm		if (init)
145110007Smarkm			{
146110007Smarkm			memcpy((char *)&SSLv23_server_data,
147110007Smarkm				(char *)sslv23_base_method(),sizeof(SSL_METHOD));
148110007Smarkm			SSLv23_server_data.ssl_accept=ssl23_accept;
149110007Smarkm			SSLv23_server_data.get_ssl_method=ssl23_get_server_method;
150110007Smarkm			init=0;
151110007Smarkm			}
152110007Smarkm
153110007Smarkm		CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD);
15455714Skris		}
15555714Skris	return(&SSLv23_server_data);
15655714Skris	}
15755714Skris
15855714Skrisint ssl23_accept(SSL *s)
15955714Skris	{
16055714Skris	BUF_MEM *buf;
16155714Skris	unsigned long Time=time(NULL);
162110007Smarkm	void (*cb)(const SSL *ssl,int type,int val)=NULL;
16355714Skris	int ret= -1;
16455714Skris	int new_state,state;
16555714Skris
16659194Skris	RAND_add(&Time,sizeof(Time),0);
16755714Skris	ERR_clear_error();
16855714Skris	clear_sys_error();
16955714Skris
17055714Skris	if (s->info_callback != NULL)
17155714Skris		cb=s->info_callback;
17255714Skris	else if (s->ctx->info_callback != NULL)
17355714Skris		cb=s->ctx->info_callback;
17455714Skris
17589840Skris	s->in_handshake++;
17655714Skris	if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
17755714Skris
17855714Skris	for (;;)
17955714Skris		{
18055714Skris		state=s->state;
18155714Skris
18255714Skris		switch(s->state)
18355714Skris			{
18455714Skris		case SSL_ST_BEFORE:
18555714Skris		case SSL_ST_ACCEPT:
18655714Skris		case SSL_ST_BEFORE|SSL_ST_ACCEPT:
18755714Skris		case SSL_ST_OK|SSL_ST_ACCEPT:
18855714Skris
18955714Skris			s->server=1;
19055714Skris			if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
19155714Skris
19255714Skris			/* s->version=SSL3_VERSION; */
19355714Skris			s->type=SSL_ST_ACCEPT;
19455714Skris
19555714Skris			if (s->init_buf == NULL)
19655714Skris				{
19755714Skris				if ((buf=BUF_MEM_new()) == NULL)
19855714Skris					{
19955714Skris					ret= -1;
20055714Skris					goto end;
20155714Skris					}
20255714Skris				if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
20355714Skris					{
20455714Skris					ret= -1;
20555714Skris					goto end;
20655714Skris					}
20755714Skris				s->init_buf=buf;
20855714Skris				}
20955714Skris
21055714Skris			ssl3_init_finished_mac(s);
21155714Skris
21255714Skris			s->state=SSL23_ST_SR_CLNT_HELLO_A;
21355714Skris			s->ctx->stats.sess_accept++;
21455714Skris			s->init_num=0;
21555714Skris			break;
21655714Skris
21755714Skris		case SSL23_ST_SR_CLNT_HELLO_A:
21855714Skris		case SSL23_ST_SR_CLNT_HELLO_B:
21955714Skris
22055714Skris			s->shutdown=0;
22155714Skris			ret=ssl23_get_client_hello(s);
22255714Skris			if (ret >= 0) cb=NULL;
22355714Skris			goto end;
22455714Skris			/* break; */
22555714Skris
22655714Skris		default:
22755714Skris			SSLerr(SSL_F_SSL23_ACCEPT,SSL_R_UNKNOWN_STATE);
22855714Skris			ret= -1;
22955714Skris			goto end;
23055714Skris			/* break; */
23155714Skris			}
23255714Skris
23355714Skris		if ((cb != NULL) && (s->state != state))
23455714Skris			{
23555714Skris			new_state=s->state;
23655714Skris			s->state=state;
23755714Skris			cb(s,SSL_CB_ACCEPT_LOOP,1);
23855714Skris			s->state=new_state;
23955714Skris			}
24055714Skris		}
24155714Skrisend:
24289840Skris	s->in_handshake--;
24355714Skris	if (cb != NULL)
24455714Skris		cb(s,SSL_CB_ACCEPT_EXIT,ret);
24555714Skris	return(ret);
24655714Skris	}
24755714Skris
24855714Skris
24955714Skrisint ssl23_get_client_hello(SSL *s)
25055714Skris	{
25159194Skris	char buf_space[11]; /* Request this many bytes in initial read.
25259194Skris	                     * We can detect SSL 3.0/TLS 1.0 Client Hellos
25359194Skris	                     * ('type == 3') correctly only when the following
25459194Skris	                     * is in a single record, which is not guaranteed by
25559194Skris	                     * the protocol specification:
25659194Skris	                     * Byte  Content
25759194Skris	                     *  0     type            \
25859194Skris	                     *  1/2   version          > record header
25959194Skris	                     *  3/4   length          /
26059194Skris	                     *  5     msg_type        \
26159194Skris	                     *  6-8   length           > Client Hello message
26259194Skris	                     *  9/10  client_version  /
26359194Skris	                     */
26455714Skris	char *buf= &(buf_space[0]);
265110007Smarkm	unsigned char *p,*d,*d_len,*dd;
26655714Skris	unsigned int i;
26755714Skris	unsigned int csl,sil,cl;
26859194Skris	int n=0,j;
26959194Skris	int type=0;
27055714Skris	int v[2];
271110007Smarkm#ifndef OPENSSL_NO_RSA
27259194Skris	int use_sslv2_strong=0;
27359194Skris#endif
27455714Skris
27555714Skris	if (s->state ==	SSL23_ST_SR_CLNT_HELLO_A)
27655714Skris		{
27759194Skris		/* read the initial header */
27859194Skris		v[0]=v[1]=0;
27959194Skris
28055714Skris		if (!ssl3_setup_buffers(s)) goto err;
28155714Skris
28259194Skris		n=ssl23_read_bytes(s, sizeof buf_space);
28359194Skris		if (n != sizeof buf_space) return(n); /* n == -1 || n == 0 */
28455714Skris
28555714Skris		p=s->packet;
28655714Skris
28755714Skris		memcpy(buf,p,n);
28855714Skris
28955714Skris		if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO))
29055714Skris			{
29159194Skris			/*
29259194Skris			 * SSLv2 header
29359194Skris			 */
29455714Skris			if ((p[3] == 0x00) && (p[4] == 0x02))
29555714Skris				{
29655714Skris				v[0]=p[3]; v[1]=p[4];
29755714Skris				/* SSLv2 */
29855714Skris				if (!(s->options & SSL_OP_NO_SSLv2))
29955714Skris					type=1;
30055714Skris				}
30155714Skris			else if (p[3] == SSL3_VERSION_MAJOR)
30255714Skris				{
30355714Skris				v[0]=p[3]; v[1]=p[4];
30455714Skris				/* SSLv3/TLSv1 */
30555714Skris				if (p[4] >= TLS1_VERSION_MINOR)
30655714Skris					{
30755714Skris					if (!(s->options & SSL_OP_NO_TLSv1))
30855714Skris						{
30959194Skris						s->version=TLS1_VERSION;
31059194Skris						/* type=2; */ /* done later to survive restarts */
31155714Skris						s->state=SSL23_ST_SR_CLNT_HELLO_B;
31255714Skris						}
31355714Skris					else if (!(s->options & SSL_OP_NO_SSLv3))
31455714Skris						{
31559194Skris						s->version=SSL3_VERSION;
31659194Skris						/* type=2; */
31755714Skris						s->state=SSL23_ST_SR_CLNT_HELLO_B;
31855714Skris						}
31955714Skris					else if (!(s->options & SSL_OP_NO_SSLv2))
32055714Skris						{
32155714Skris						type=1;
32255714Skris						}
32355714Skris					}
32455714Skris				else if (!(s->options & SSL_OP_NO_SSLv3))
32559194Skris					{
32659194Skris					s->version=SSL3_VERSION;
32759194Skris					/* type=2; */
32855714Skris					s->state=SSL23_ST_SR_CLNT_HELLO_B;
32959194Skris					}
33055714Skris				else if (!(s->options & SSL_OP_NO_SSLv2))
33155714Skris					type=1;
33255714Skris
33355714Skris				}
33455714Skris			}
33555714Skris		else if ((p[0] == SSL3_RT_HANDSHAKE) &&
33655714Skris			 (p[1] == SSL3_VERSION_MAJOR) &&
33759194Skris			 (p[5] == SSL3_MT_CLIENT_HELLO) &&
33859194Skris			 ((p[3] == 0 && p[4] < 5 /* silly record length? */)
33959194Skris				|| (p[9] == p[1])))
34055714Skris			{
34159194Skris			/*
34259194Skris			 * SSLv3 or tls1 header
34359194Skris			 */
34459194Skris
34568654Skris			v[0]=p[1]; /* major version (= SSL3_VERSION_MAJOR) */
34659194Skris			/* We must look at client_version inside the Client Hello message
34768654Skris			 * to get the correct minor version.
34868654Skris			 * However if we have only a pathologically small fragment of the
34989840Skris			 * Client Hello message, this would be difficult, and we'd have
35089840Skris			 * to read more records to find out.
35189840Skris			 * No known SSL 3.0 client fragments ClientHello like this,
35289840Skris			 * so we simply assume TLS 1.0 to avoid protocol version downgrade
35389840Skris			 * attacks. */
35459194Skris			if (p[3] == 0 && p[4] < 6)
35568654Skris				{
35689840Skris#if 0
35768654Skris				SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_SMALL);
35868654Skris				goto err;
35989840Skris#else
36089840Skris				v[1] = TLS1_VERSION_MINOR;
36189840Skris#endif
36268654Skris				}
36389840Skris			else
36489840Skris				v[1]=p[10]; /* minor version according to client_version */
36559194Skris			if (v[1] >= TLS1_VERSION_MINOR)
36655714Skris				{
36755714Skris				if (!(s->options & SSL_OP_NO_TLSv1))
36855714Skris					{
36959194Skris					s->version=TLS1_VERSION;
37055714Skris					type=3;
37155714Skris					}
37255714Skris				else if (!(s->options & SSL_OP_NO_SSLv3))
37359194Skris					{
37459194Skris					s->version=SSL3_VERSION;
37555714Skris					type=3;
37659194Skris					}
37755714Skris				}
37889840Skris			else
37959194Skris				{
38089840Skris				/* client requests SSL 3.0 */
38189840Skris				if (!(s->options & SSL_OP_NO_SSLv3))
38289840Skris					{
38389840Skris					s->version=SSL3_VERSION;
38489840Skris					type=3;
38589840Skris					}
38689840Skris				else if (!(s->options & SSL_OP_NO_TLSv1))
38789840Skris					{
38889840Skris					/* we won't be able to use TLS of course,
38989840Skris					 * but this will send an appropriate alert */
39089840Skris					s->version=TLS1_VERSION;
39189840Skris					type=3;
39289840Skris					}
39359194Skris				}
39455714Skris			}
39555714Skris		else if ((strncmp("GET ", (char *)p,4) == 0) ||
39655714Skris			 (strncmp("POST ",(char *)p,5) == 0) ||
39755714Skris			 (strncmp("HEAD ",(char *)p,5) == 0) ||
39855714Skris			 (strncmp("PUT ", (char *)p,4) == 0))
39955714Skris			{
40055714Skris			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTP_REQUEST);
40155714Skris			goto err;
40255714Skris			}
40355714Skris		else if (strncmp("CONNECT",(char *)p,7) == 0)
40455714Skris			{
40555714Skris			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTPS_PROXY_REQUEST);
40655714Skris			goto err;
40755714Skris			}
40855714Skris		}
40955714Skris
41055714Skris	if (s->state == SSL23_ST_SR_CLNT_HELLO_B)
41155714Skris		{
41259194Skris		/* we have SSLv3/TLSv1 in an SSLv2 header
41359194Skris		 * (other cases skip this state) */
41459194Skris
41555714Skris		type=2;
41655714Skris		p=s->packet;
41759194Skris		v[0] = p[3]; /* == SSL3_VERSION_MAJOR */
41859194Skris		v[1] = p[4];
41959194Skris
42055714Skris		n=((p[0]&0x7f)<<8)|p[1];
42155714Skris		if (n > (1024*4))
42255714Skris			{
42355714Skris			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE);
42455714Skris			goto err;
42555714Skris			}
42655714Skris
42755714Skris		j=ssl23_read_bytes(s,n+2);
42855714Skris		if (j <= 0) return(j);
42955714Skris
430110007Smarkm		ssl3_finish_mac(s, s->packet+2, s->packet_length-2);
431110007Smarkm		if (s->msg_callback)
432110007Smarkm			s->msg_callback(0, SSL2_VERSION, 0, s->packet+2, s->packet_length-2, s, s->msg_callback_arg); /* CLIENT-HELLO */
43355714Skris
43455714Skris		p=s->packet;
43555714Skris		p+=5;
43655714Skris		n2s(p,csl);
43755714Skris		n2s(p,sil);
43855714Skris		n2s(p,cl);
43955714Skris		d=(unsigned char *)s->init_buf->data;
44055714Skris		if ((csl+sil+cl+11) != s->packet_length)
44155714Skris			{
44255714Skris			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_LENGTH_MISMATCH);
44355714Skris			goto err;
44455714Skris			}
44555714Skris
446110007Smarkm		/* record header: msg_type ... */
447110007Smarkm		*(d++) = SSL3_MT_CLIENT_HELLO;
448110007Smarkm		/* ... and length (actual value will be written later) */
449110007Smarkm		d_len = d;
450110007Smarkm		d += 3;
451110007Smarkm
452110007Smarkm		/* client_version */
45359194Skris		*(d++) = SSL3_VERSION_MAJOR; /* == v[0] */
45459194Skris		*(d++) = v[1];
45555714Skris
45655714Skris		/* lets populate the random area */
45759194Skris		/* get the challenge_length */
45855714Skris		i=(cl > SSL3_RANDOM_SIZE)?SSL3_RANDOM_SIZE:cl;
45955714Skris		memset(d,0,SSL3_RANDOM_SIZE);
46055714Skris		memcpy(&(d[SSL3_RANDOM_SIZE-i]),&(p[csl+sil]),i);
46155714Skris		d+=SSL3_RANDOM_SIZE;
46255714Skris
46355714Skris		/* no session-id reuse */
46455714Skris		*(d++)=0;
46555714Skris
46655714Skris		/* ciphers */
46755714Skris		j=0;
46855714Skris		dd=d;
46955714Skris		d+=2;
47055714Skris		for (i=0; i<csl; i+=3)
47155714Skris			{
47255714Skris			if (p[i] != 0) continue;
47355714Skris			*(d++)=p[i+1];
47455714Skris			*(d++)=p[i+2];
47555714Skris			j+=2;
47655714Skris			}
47755714Skris		s2n(j,dd);
47855714Skris
47955714Skris		/* COMPRESSION */
48055714Skris		*(d++)=1;
48155714Skris		*(d++)=0;
48255714Skris
483110007Smarkm		i = (d-(unsigned char *)s->init_buf->data) - 4;
484110007Smarkm		l2n3((long)i, d_len);
48555714Skris
48655714Skris		/* get the data reused from the init_buf */
48755714Skris		s->s3->tmp.reuse_message=1;
48855714Skris		s->s3->tmp.message_type=SSL3_MT_CLIENT_HELLO;
48955714Skris		s->s3->tmp.message_size=i;
49055714Skris		}
49155714Skris
49259194Skris	/* imaginary new state (for program structure): */
49359194Skris	/* s->state = SSL23_SR_CLNT_HELLO_C */
49459194Skris
49555714Skris	if (type == 1)
49655714Skris		{
497110007Smarkm#ifdef OPENSSL_NO_SSL2
49859194Skris		SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
49959194Skris		goto err;
50055949Skris#else
50155714Skris		/* we are talking sslv2 */
50255714Skris		/* we need to clean up the SSLv3/TLSv1 setup and put in the
50355714Skris		 * sslv2 stuff. */
50455714Skris
50555714Skris		if (s->s2 == NULL)
50655714Skris			{
50755714Skris			if (!ssl2_new(s))
50855714Skris				goto err;
50955714Skris			}
51055714Skris		else
51155714Skris			ssl2_clear(s);
51255714Skris
51355714Skris		if (s->s3 != NULL) ssl3_free(s);
51455714Skris
515110007Smarkm		if (!BUF_MEM_grow_clean(s->init_buf,
51655714Skris			SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
51755714Skris			{
51855714Skris			goto err;
51955714Skris			}
52055714Skris
52155714Skris		s->state=SSL2_ST_GET_CLIENT_HELLO_A;
52255714Skris		if ((s->options & SSL_OP_MSIE_SSLV2_RSA_PADDING) ||
52368654Skris			use_sslv2_strong ||
52468654Skris			(s->options & SSL_OP_NO_TLSv1 && s->options & SSL_OP_NO_SSLv3))
52555714Skris			s->s2->ssl2_rollback=0;
52655714Skris		else
52768654Skris			/* reject SSL 2.0 session if client supports SSL 3.0 or TLS 1.0
52868654Skris			 * (SSL 3.0 draft/RFC 2246, App. E.2) */
52955714Skris			s->s2->ssl2_rollback=1;
53055714Skris
53159194Skris		/* setup the n bytes we have read so we get them from
53255714Skris		 * the sslv2 buffer */
53355714Skris		s->rstate=SSL_ST_READ_HEADER;
53455714Skris		s->packet_length=n;
53555714Skris		s->packet= &(s->s2->rbuf[0]);
53655714Skris		memcpy(s->packet,buf,n);
53755714Skris		s->s2->rbuf_left=n;
53855714Skris		s->s2->rbuf_offs=0;
53955714Skris
54055714Skris		s->method=SSLv2_server_method();
54155714Skris		s->handshake_func=s->method->ssl_accept;
54255949Skris#endif
54355714Skris		}
54455714Skris
54555714Skris	if ((type == 2) || (type == 3))
54655714Skris		{
54759194Skris		/* we have SSLv3/TLSv1 (type 2: SSL2 style, type 3: SSL3/TLS style) */
54855714Skris
54955714Skris		if (!ssl_init_wbio_buffer(s,1)) goto err;
55055714Skris
55155714Skris		/* we are in this state */
55255714Skris		s->state=SSL3_ST_SR_CLNT_HELLO_A;
55355714Skris
55455714Skris		if (type == 3)
55555714Skris			{
55655714Skris			/* put the 'n' bytes we have read into the input buffer
55755714Skris			 * for SSLv3 */
55855714Skris			s->rstate=SSL_ST_READ_HEADER;
55955714Skris			s->packet_length=n;
56055714Skris			s->packet= &(s->s3->rbuf.buf[0]);
56155714Skris			memcpy(s->packet,buf,n);
56255714Skris			s->s3->rbuf.left=n;
56355714Skris			s->s3->rbuf.offset=0;
56455714Skris			}
56555714Skris		else
56655714Skris			{
56755714Skris			s->packet_length=0;
56855714Skris			s->s3->rbuf.left=0;
56955714Skris			s->s3->rbuf.offset=0;
57055714Skris			}
57155714Skris
57259194Skris		if (s->version == TLS1_VERSION)
57359194Skris			s->method = TLSv1_server_method();
57455714Skris		else
57559194Skris			s->method = SSLv3_server_method();
57659194Skris#if 0 /* ssl3_get_client_hello does this */
57755714Skris		s->client_version=(v[0]<<8)|v[1];
57859194Skris#endif
57955714Skris		s->handshake_func=s->method->ssl_accept;
58055714Skris		}
58155714Skris
58255714Skris	if ((type < 1) || (type > 3))
58355714Skris		{
58455714Skris		/* bad, very bad */
58555714Skris		SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNKNOWN_PROTOCOL);
58655714Skris		goto err;
58755714Skris		}
58855714Skris	s->init_num=0;
58955714Skris
59068654Skris	if (buf != buf_space) OPENSSL_free(buf);
59155714Skris	s->first_packet=1;
59255714Skris	return(SSL_accept(s));
59355714Skriserr:
59468654Skris	if (buf != buf_space) OPENSSL_free(buf);
59555714Skris	return(-1);
59655714Skris	}
597