s23_srvr.c revision 273415
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
127273415Sdelphij#ifndef OPENSSL_NO_SSL3
12855714Skris	if (ver == SSL3_VERSION)
12955714Skris		return(SSLv3_server_method());
130273415Sdelphij#endif
131273415Sdelphij	if (ver == TLS1_VERSION)
13255714Skris		return(TLSv1_server_method());
13355714Skris	else
13455714Skris		return(NULL);
13555714Skris	}
13655714Skris
137160817SsimonIMPLEMENT_ssl23_meth_func(SSLv23_server_method,
138160817Ssimon			ssl23_accept,
139160817Ssimon			ssl_undefined_function,
140160817Ssimon			ssl23_get_server_method)
14155714Skris
14255714Skrisint ssl23_accept(SSL *s)
14355714Skris	{
14455714Skris	BUF_MEM *buf;
145160817Ssimon	unsigned long Time=(unsigned long)time(NULL);
146110007Smarkm	void (*cb)(const SSL *ssl,int type,int val)=NULL;
14755714Skris	int ret= -1;
14855714Skris	int new_state,state;
14955714Skris
15059194Skris	RAND_add(&Time,sizeof(Time),0);
15155714Skris	ERR_clear_error();
15255714Skris	clear_sys_error();
15355714Skris
15455714Skris	if (s->info_callback != NULL)
15555714Skris		cb=s->info_callback;
15655714Skris	else if (s->ctx->info_callback != NULL)
15755714Skris		cb=s->ctx->info_callback;
15855714Skris
15989840Skris	s->in_handshake++;
16055714Skris	if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
16155714Skris
16255714Skris	for (;;)
16355714Skris		{
16455714Skris		state=s->state;
16555714Skris
16655714Skris		switch(s->state)
16755714Skris			{
16855714Skris		case SSL_ST_BEFORE:
16955714Skris		case SSL_ST_ACCEPT:
17055714Skris		case SSL_ST_BEFORE|SSL_ST_ACCEPT:
17155714Skris		case SSL_ST_OK|SSL_ST_ACCEPT:
17255714Skris
17355714Skris			s->server=1;
17455714Skris			if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
17555714Skris
17655714Skris			/* s->version=SSL3_VERSION; */
17755714Skris			s->type=SSL_ST_ACCEPT;
17855714Skris
17955714Skris			if (s->init_buf == NULL)
18055714Skris				{
18155714Skris				if ((buf=BUF_MEM_new()) == NULL)
18255714Skris					{
18355714Skris					ret= -1;
18455714Skris					goto end;
18555714Skris					}
18655714Skris				if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
18755714Skris					{
18855714Skris					ret= -1;
18955714Skris					goto end;
19055714Skris					}
19155714Skris				s->init_buf=buf;
19255714Skris				}
19355714Skris
19455714Skris			ssl3_init_finished_mac(s);
19555714Skris
19655714Skris			s->state=SSL23_ST_SR_CLNT_HELLO_A;
19755714Skris			s->ctx->stats.sess_accept++;
19855714Skris			s->init_num=0;
19955714Skris			break;
20055714Skris
20155714Skris		case SSL23_ST_SR_CLNT_HELLO_A:
20255714Skris		case SSL23_ST_SR_CLNT_HELLO_B:
20355714Skris
20455714Skris			s->shutdown=0;
20555714Skris			ret=ssl23_get_client_hello(s);
20655714Skris			if (ret >= 0) cb=NULL;
20755714Skris			goto end;
20855714Skris			/* break; */
20955714Skris
21055714Skris		default:
21155714Skris			SSLerr(SSL_F_SSL23_ACCEPT,SSL_R_UNKNOWN_STATE);
21255714Skris			ret= -1;
21355714Skris			goto end;
21455714Skris			/* break; */
21555714Skris			}
21655714Skris
21755714Skris		if ((cb != NULL) && (s->state != state))
21855714Skris			{
21955714Skris			new_state=s->state;
22055714Skris			s->state=state;
22155714Skris			cb(s,SSL_CB_ACCEPT_LOOP,1);
22255714Skris			s->state=new_state;
22355714Skris			}
22455714Skris		}
22555714Skrisend:
22689840Skris	s->in_handshake--;
22755714Skris	if (cb != NULL)
22855714Skris		cb(s,SSL_CB_ACCEPT_EXIT,ret);
22955714Skris	return(ret);
23055714Skris	}
23155714Skris
23255714Skris
23355714Skrisint ssl23_get_client_hello(SSL *s)
23455714Skris	{
23559194Skris	char buf_space[11]; /* Request this many bytes in initial read.
23659194Skris	                     * We can detect SSL 3.0/TLS 1.0 Client Hellos
23759194Skris	                     * ('type == 3') correctly only when the following
23859194Skris	                     * is in a single record, which is not guaranteed by
23959194Skris	                     * the protocol specification:
24059194Skris	                     * Byte  Content
24159194Skris	                     *  0     type            \
24259194Skris	                     *  1/2   version          > record header
24359194Skris	                     *  3/4   length          /
24459194Skris	                     *  5     msg_type        \
24559194Skris	                     *  6-8   length           > Client Hello message
24659194Skris	                     *  9/10  client_version  /
24759194Skris	                     */
24855714Skris	char *buf= &(buf_space[0]);
249110007Smarkm	unsigned char *p,*d,*d_len,*dd;
25055714Skris	unsigned int i;
25155714Skris	unsigned int csl,sil,cl;
25259194Skris	int n=0,j;
25359194Skris	int type=0;
25455714Skris	int v[2];
25555714Skris
25655714Skris	if (s->state ==	SSL23_ST_SR_CLNT_HELLO_A)
25755714Skris		{
25859194Skris		/* read the initial header */
25959194Skris		v[0]=v[1]=0;
26059194Skris
26155714Skris		if (!ssl3_setup_buffers(s)) goto err;
26255714Skris
26359194Skris		n=ssl23_read_bytes(s, sizeof buf_space);
26459194Skris		if (n != sizeof buf_space) return(n); /* n == -1 || n == 0 */
26555714Skris
26655714Skris		p=s->packet;
26755714Skris
26855714Skris		memcpy(buf,p,n);
26955714Skris
27055714Skris		if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO))
27155714Skris			{
27259194Skris			/*
27359194Skris			 * SSLv2 header
27459194Skris			 */
27555714Skris			if ((p[3] == 0x00) && (p[4] == 0x02))
27655714Skris				{
27755714Skris				v[0]=p[3]; v[1]=p[4];
27855714Skris				/* SSLv2 */
27955714Skris				if (!(s->options & SSL_OP_NO_SSLv2))
28055714Skris					type=1;
28155714Skris				}
28255714Skris			else if (p[3] == SSL3_VERSION_MAJOR)
28355714Skris				{
28455714Skris				v[0]=p[3]; v[1]=p[4];
28555714Skris				/* SSLv3/TLSv1 */
28655714Skris				if (p[4] >= TLS1_VERSION_MINOR)
28755714Skris					{
28855714Skris					if (!(s->options & SSL_OP_NO_TLSv1))
28955714Skris						{
29059194Skris						s->version=TLS1_VERSION;
29159194Skris						/* type=2; */ /* done later to survive restarts */
29255714Skris						s->state=SSL23_ST_SR_CLNT_HELLO_B;
29355714Skris						}
29455714Skris					else if (!(s->options & SSL_OP_NO_SSLv3))
29555714Skris						{
29659194Skris						s->version=SSL3_VERSION;
29759194Skris						/* type=2; */
29855714Skris						s->state=SSL23_ST_SR_CLNT_HELLO_B;
29955714Skris						}
30055714Skris					else if (!(s->options & SSL_OP_NO_SSLv2))
30155714Skris						{
30255714Skris						type=1;
30355714Skris						}
30455714Skris					}
30555714Skris				else if (!(s->options & SSL_OP_NO_SSLv3))
30659194Skris					{
30759194Skris					s->version=SSL3_VERSION;
30859194Skris					/* type=2; */
30955714Skris					s->state=SSL23_ST_SR_CLNT_HELLO_B;
31059194Skris					}
31155714Skris				else if (!(s->options & SSL_OP_NO_SSLv2))
31255714Skris					type=1;
31355714Skris
31455714Skris				}
31555714Skris			}
31655714Skris		else if ((p[0] == SSL3_RT_HANDSHAKE) &&
31755714Skris			 (p[1] == SSL3_VERSION_MAJOR) &&
31859194Skris			 (p[5] == SSL3_MT_CLIENT_HELLO) &&
31959194Skris			 ((p[3] == 0 && p[4] < 5 /* silly record length? */)
320205128Ssimon				|| (p[9] >= p[1])))
32155714Skris			{
32259194Skris			/*
32359194Skris			 * SSLv3 or tls1 header
32459194Skris			 */
32559194Skris
32668654Skris			v[0]=p[1]; /* major version (= SSL3_VERSION_MAJOR) */
32759194Skris			/* We must look at client_version inside the Client Hello message
32868654Skris			 * to get the correct minor version.
32968654Skris			 * However if we have only a pathologically small fragment of the
33089840Skris			 * Client Hello message, this would be difficult, and we'd have
33189840Skris			 * to read more records to find out.
33289840Skris			 * No known SSL 3.0 client fragments ClientHello like this,
333271305Sdelphij			 * so we simply reject such connections to avoid
334271305Sdelphij			 * protocol version downgrade attacks. */
33559194Skris			if (p[3] == 0 && p[4] < 6)
33668654Skris				{
33768654Skris				SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_SMALL);
33868654Skris				goto err;
33968654Skris				}
340205128Ssimon			/* if major version number > 3 set minor to a value
341205128Ssimon			 * which will use the highest version 3 we support.
342205128Ssimon			 * If TLS 2.0 ever appears we will need to revise
343205128Ssimon			 * this....
344205128Ssimon			 */
345271305Sdelphij			if (p[9] > SSL3_VERSION_MAJOR)
346205128Ssimon				v[1]=0xff;
34789840Skris			else
34889840Skris				v[1]=p[10]; /* minor version according to client_version */
34959194Skris			if (v[1] >= TLS1_VERSION_MINOR)
35055714Skris				{
35155714Skris				if (!(s->options & SSL_OP_NO_TLSv1))
35255714Skris					{
35359194Skris					s->version=TLS1_VERSION;
35455714Skris					type=3;
35555714Skris					}
35655714Skris				else if (!(s->options & SSL_OP_NO_SSLv3))
35759194Skris					{
35859194Skris					s->version=SSL3_VERSION;
35955714Skris					type=3;
36059194Skris					}
36155714Skris				}
36289840Skris			else
36359194Skris				{
36489840Skris				/* client requests SSL 3.0 */
36589840Skris				if (!(s->options & SSL_OP_NO_SSLv3))
36689840Skris					{
36789840Skris					s->version=SSL3_VERSION;
36889840Skris					type=3;
36989840Skris					}
37089840Skris				else if (!(s->options & SSL_OP_NO_TLSv1))
37189840Skris					{
37289840Skris					/* we won't be able to use TLS of course,
37389840Skris					 * but this will send an appropriate alert */
37489840Skris					s->version=TLS1_VERSION;
37589840Skris					type=3;
37689840Skris					}
37759194Skris				}
37855714Skris			}
37955714Skris		else if ((strncmp("GET ", (char *)p,4) == 0) ||
38055714Skris			 (strncmp("POST ",(char *)p,5) == 0) ||
38155714Skris			 (strncmp("HEAD ",(char *)p,5) == 0) ||
38255714Skris			 (strncmp("PUT ", (char *)p,4) == 0))
38355714Skris			{
38455714Skris			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTP_REQUEST);
38555714Skris			goto err;
38655714Skris			}
38755714Skris		else if (strncmp("CONNECT",(char *)p,7) == 0)
38855714Skris			{
38955714Skris			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTPS_PROXY_REQUEST);
39055714Skris			goto err;
39155714Skris			}
39255714Skris		}
39355714Skris
394194206Ssimon#ifdef OPENSSL_FIPS
395194206Ssimon	if (FIPS_mode() && (s->version < TLS1_VERSION))
396194206Ssimon		{
397194206Ssimon		SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,
398194206Ssimon					SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
399194206Ssimon		goto err;
400194206Ssimon		}
401194206Ssimon#endif
402194206Ssimon
403273415Sdelphij	/* ensure that TLS_MAX_VERSION is up-to-date */
404273415Sdelphij	OPENSSL_assert(s->version <= TLS_MAX_VERSION);
405273415Sdelphij
40655714Skris	if (s->state == SSL23_ST_SR_CLNT_HELLO_B)
40755714Skris		{
40859194Skris		/* we have SSLv3/TLSv1 in an SSLv2 header
40959194Skris		 * (other cases skip this state) */
41059194Skris
41155714Skris		type=2;
41255714Skris		p=s->packet;
41359194Skris		v[0] = p[3]; /* == SSL3_VERSION_MAJOR */
41459194Skris		v[1] = p[4];
41559194Skris
416271305Sdelphij		/* An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
417271305Sdelphij		 * header is sent directly on the wire, not wrapped as a TLS
418271305Sdelphij		 * record. It's format is:
419271305Sdelphij		 * Byte  Content
420271305Sdelphij		 * 0-1   msg_length
421271305Sdelphij		 * 2     msg_type
422271305Sdelphij		 * 3-4   version
423271305Sdelphij		 * 5-6   cipher_spec_length
424271305Sdelphij		 * 7-8   session_id_length
425271305Sdelphij		 * 9-10  challenge_length
426271305Sdelphij		 * ...   ...
427271305Sdelphij		 */
42855714Skris		n=((p[0]&0x7f)<<8)|p[1];
42955714Skris		if (n > (1024*4))
43055714Skris			{
43155714Skris			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE);
43255714Skris			goto err;
43355714Skris			}
434271305Sdelphij		if (n < 9)
435271305Sdelphij			{
436271305Sdelphij			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_LENGTH_MISMATCH);
437271305Sdelphij			goto err;
438271305Sdelphij			}
43955714Skris
44055714Skris		j=ssl23_read_bytes(s,n+2);
441271305Sdelphij		/* We previously read 11 bytes, so if j > 0, we must have
442271305Sdelphij		 * j == n+2 == s->packet_length. We have at least 11 valid
443271305Sdelphij		 * packet bytes. */
44455714Skris		if (j <= 0) return(j);
44555714Skris
446110007Smarkm		ssl3_finish_mac(s, s->packet+2, s->packet_length-2);
447110007Smarkm		if (s->msg_callback)
448110007Smarkm			s->msg_callback(0, SSL2_VERSION, 0, s->packet+2, s->packet_length-2, s, s->msg_callback_arg); /* CLIENT-HELLO */
44955714Skris
45055714Skris		p=s->packet;
45155714Skris		p+=5;
45255714Skris		n2s(p,csl);
45355714Skris		n2s(p,sil);
45455714Skris		n2s(p,cl);
45555714Skris		d=(unsigned char *)s->init_buf->data;
45655714Skris		if ((csl+sil+cl+11) != s->packet_length)
45755714Skris			{
45855714Skris			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_LENGTH_MISMATCH);
45955714Skris			goto err;
46055714Skris			}
46155714Skris
462110007Smarkm		/* record header: msg_type ... */
463110007Smarkm		*(d++) = SSL3_MT_CLIENT_HELLO;
464110007Smarkm		/* ... and length (actual value will be written later) */
465110007Smarkm		d_len = d;
466110007Smarkm		d += 3;
467110007Smarkm
468110007Smarkm		/* client_version */
46959194Skris		*(d++) = SSL3_VERSION_MAJOR; /* == v[0] */
47059194Skris		*(d++) = v[1];
47155714Skris
47255714Skris		/* lets populate the random area */
47359194Skris		/* get the challenge_length */
47455714Skris		i=(cl > SSL3_RANDOM_SIZE)?SSL3_RANDOM_SIZE:cl;
47555714Skris		memset(d,0,SSL3_RANDOM_SIZE);
47655714Skris		memcpy(&(d[SSL3_RANDOM_SIZE-i]),&(p[csl+sil]),i);
47755714Skris		d+=SSL3_RANDOM_SIZE;
47855714Skris
47955714Skris		/* no session-id reuse */
48055714Skris		*(d++)=0;
48155714Skris
48255714Skris		/* ciphers */
48355714Skris		j=0;
48455714Skris		dd=d;
48555714Skris		d+=2;
48655714Skris		for (i=0; i<csl; i+=3)
48755714Skris			{
48855714Skris			if (p[i] != 0) continue;
48955714Skris			*(d++)=p[i+1];
49055714Skris			*(d++)=p[i+2];
49155714Skris			j+=2;
49255714Skris			}
49355714Skris		s2n(j,dd);
49455714Skris
49555714Skris		/* COMPRESSION */
49655714Skris		*(d++)=1;
49755714Skris		*(d++)=0;
49855714Skris
499110007Smarkm		i = (d-(unsigned char *)s->init_buf->data) - 4;
500110007Smarkm		l2n3((long)i, d_len);
50155714Skris
50255714Skris		/* get the data reused from the init_buf */
50355714Skris		s->s3->tmp.reuse_message=1;
50455714Skris		s->s3->tmp.message_type=SSL3_MT_CLIENT_HELLO;
50555714Skris		s->s3->tmp.message_size=i;
50655714Skris		}
50755714Skris
50859194Skris	/* imaginary new state (for program structure): */
50959194Skris	/* s->state = SSL23_SR_CLNT_HELLO_C */
51059194Skris
51155714Skris	if (type == 1)
51255714Skris		{
513110007Smarkm#ifdef OPENSSL_NO_SSL2
51459194Skris		SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
51559194Skris		goto err;
51655949Skris#else
51755714Skris		/* we are talking sslv2 */
51855714Skris		/* we need to clean up the SSLv3/TLSv1 setup and put in the
51955714Skris		 * sslv2 stuff. */
52055714Skris
52155714Skris		if (s->s2 == NULL)
52255714Skris			{
52355714Skris			if (!ssl2_new(s))
52455714Skris				goto err;
52555714Skris			}
52655714Skris		else
52755714Skris			ssl2_clear(s);
52855714Skris
52955714Skris		if (s->s3 != NULL) ssl3_free(s);
53055714Skris
531110007Smarkm		if (!BUF_MEM_grow_clean(s->init_buf,
53255714Skris			SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
53355714Skris			{
53455714Skris			goto err;
53555714Skris			}
53655714Skris
53755714Skris		s->state=SSL2_ST_GET_CLIENT_HELLO_A;
538151233Scperciva		if (s->options & SSL_OP_NO_TLSv1 && s->options & SSL_OP_NO_SSLv3)
53955714Skris			s->s2->ssl2_rollback=0;
54055714Skris		else
54168654Skris			/* reject SSL 2.0 session if client supports SSL 3.0 or TLS 1.0
54268654Skris			 * (SSL 3.0 draft/RFC 2246, App. E.2) */
54355714Skris			s->s2->ssl2_rollback=1;
54455714Skris
54559194Skris		/* setup the n bytes we have read so we get them from
54655714Skris		 * the sslv2 buffer */
54755714Skris		s->rstate=SSL_ST_READ_HEADER;
54855714Skris		s->packet_length=n;
54955714Skris		s->packet= &(s->s2->rbuf[0]);
55055714Skris		memcpy(s->packet,buf,n);
55155714Skris		s->s2->rbuf_left=n;
55255714Skris		s->s2->rbuf_offs=0;
55355714Skris
55455714Skris		s->method=SSLv2_server_method();
55555714Skris		s->handshake_func=s->method->ssl_accept;
55655949Skris#endif
55755714Skris		}
55855714Skris
55955714Skris	if ((type == 2) || (type == 3))
56055714Skris		{
56159194Skris		/* we have SSLv3/TLSv1 (type 2: SSL2 style, type 3: SSL3/TLS style) */
562273415Sdelphij                s->method = ssl23_get_server_method(s->version);
563273415Sdelphij		if (s->method == NULL)
564273415Sdelphij			{
565273415Sdelphij			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
566273415Sdelphij			goto err;
567273415Sdelphij			}
56855714Skris
56955714Skris		if (!ssl_init_wbio_buffer(s,1)) goto err;
57055714Skris
57155714Skris		/* we are in this state */
57255714Skris		s->state=SSL3_ST_SR_CLNT_HELLO_A;
57355714Skris
57455714Skris		if (type == 3)
57555714Skris			{
57655714Skris			/* put the 'n' bytes we have read into the input buffer
57755714Skris			 * for SSLv3 */
57855714Skris			s->rstate=SSL_ST_READ_HEADER;
57955714Skris			s->packet_length=n;
58055714Skris			s->packet= &(s->s3->rbuf.buf[0]);
58155714Skris			memcpy(s->packet,buf,n);
58255714Skris			s->s3->rbuf.left=n;
58355714Skris			s->s3->rbuf.offset=0;
58455714Skris			}
58555714Skris		else
58655714Skris			{
58755714Skris			s->packet_length=0;
58855714Skris			s->s3->rbuf.left=0;
58955714Skris			s->s3->rbuf.offset=0;
59055714Skris			}
59159194Skris#if 0 /* ssl3_get_client_hello does this */
59255714Skris		s->client_version=(v[0]<<8)|v[1];
59359194Skris#endif
59455714Skris		s->handshake_func=s->method->ssl_accept;
59555714Skris		}
59655714Skris
59755714Skris	if ((type < 1) || (type > 3))
59855714Skris		{
59955714Skris		/* bad, very bad */
60055714Skris		SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNKNOWN_PROTOCOL);
60155714Skris		goto err;
60255714Skris		}
60355714Skris	s->init_num=0;
60455714Skris
60568654Skris	if (buf != buf_space) OPENSSL_free(buf);
60655714Skris	return(SSL_accept(s));
60755714Skriserr:
60868654Skris	if (buf != buf_space) OPENSSL_free(buf);
60955714Skris	return(-1);
61055714Skris	}
611