xref: /onnv-gate/usr/src/common/openssl/ssl/s23_srvr.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* ssl/s23_srvr.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate /* ====================================================================
590Sstevel@tonic-gate  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
620Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
630Sstevel@tonic-gate  * are met:
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
660Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
690Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
700Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
710Sstevel@tonic-gate  *    distribution.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
740Sstevel@tonic-gate  *    software must display the following acknowledgment:
750Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
760Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
790Sstevel@tonic-gate  *    endorse or promote products derived from this software without
800Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
810Sstevel@tonic-gate  *    openssl-core@openssl.org.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
840Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
850Sstevel@tonic-gate  *    permission of the OpenSSL Project.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
880Sstevel@tonic-gate  *    acknowledgment:
890Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
900Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
930Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
940Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
950Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
960Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
970Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
980Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
990Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1000Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1010Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1020Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1030Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
1040Sstevel@tonic-gate  * ====================================================================
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
1070Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
1080Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate #include <stdio.h>
1130Sstevel@tonic-gate #include "ssl_locl.h"
1140Sstevel@tonic-gate #include <openssl/buffer.h>
1150Sstevel@tonic-gate #include <openssl/rand.h>
1160Sstevel@tonic-gate #include <openssl/objects.h>
1170Sstevel@tonic-gate #include <openssl/evp.h>
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate static SSL_METHOD *ssl23_get_server_method(int ver);
1200Sstevel@tonic-gate int ssl23_get_client_hello(SSL *s);
ssl23_get_server_method(int ver)1210Sstevel@tonic-gate static SSL_METHOD *ssl23_get_server_method(int ver)
1220Sstevel@tonic-gate 	{
1230Sstevel@tonic-gate #ifndef OPENSSL_NO_SSL2
1240Sstevel@tonic-gate 	if (ver == SSL2_VERSION)
1250Sstevel@tonic-gate 		return(SSLv2_server_method());
1260Sstevel@tonic-gate #endif
1270Sstevel@tonic-gate 	if (ver == SSL3_VERSION)
1280Sstevel@tonic-gate 		return(SSLv3_server_method());
1290Sstevel@tonic-gate 	else if (ver == TLS1_VERSION)
1300Sstevel@tonic-gate 		return(TLSv1_server_method());
1310Sstevel@tonic-gate 	else
1320Sstevel@tonic-gate 		return(NULL);
1330Sstevel@tonic-gate 	}
1340Sstevel@tonic-gate 
IMPLEMENT_ssl23_meth_func(SSLv23_server_method,ssl23_accept,ssl_undefined_function,ssl23_get_server_method)135*2139Sjp161948 IMPLEMENT_ssl23_meth_func(SSLv23_server_method,
136*2139Sjp161948 			ssl23_accept,
137*2139Sjp161948 			ssl_undefined_function,
138*2139Sjp161948 			ssl23_get_server_method)
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate int ssl23_accept(SSL *s)
1410Sstevel@tonic-gate 	{
1420Sstevel@tonic-gate 	BUF_MEM *buf;
1430Sstevel@tonic-gate 	unsigned long Time=time(NULL);
1440Sstevel@tonic-gate 	void (*cb)(const SSL *ssl,int type,int val)=NULL;
1450Sstevel@tonic-gate 	int ret= -1;
1460Sstevel@tonic-gate 	int new_state,state;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	RAND_add(&Time,sizeof(Time),0);
1490Sstevel@tonic-gate 	ERR_clear_error();
1500Sstevel@tonic-gate 	clear_sys_error();
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if (s->info_callback != NULL)
1530Sstevel@tonic-gate 		cb=s->info_callback;
1540Sstevel@tonic-gate 	else if (s->ctx->info_callback != NULL)
1550Sstevel@tonic-gate 		cb=s->ctx->info_callback;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	s->in_handshake++;
1580Sstevel@tonic-gate 	if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	for (;;)
1610Sstevel@tonic-gate 		{
1620Sstevel@tonic-gate 		state=s->state;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 		switch(s->state)
1650Sstevel@tonic-gate 			{
1660Sstevel@tonic-gate 		case SSL_ST_BEFORE:
1670Sstevel@tonic-gate 		case SSL_ST_ACCEPT:
1680Sstevel@tonic-gate 		case SSL_ST_BEFORE|SSL_ST_ACCEPT:
1690Sstevel@tonic-gate 		case SSL_ST_OK|SSL_ST_ACCEPT:
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 			s->server=1;
1720Sstevel@tonic-gate 			if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 			/* s->version=SSL3_VERSION; */
1750Sstevel@tonic-gate 			s->type=SSL_ST_ACCEPT;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 			if (s->init_buf == NULL)
1780Sstevel@tonic-gate 				{
1790Sstevel@tonic-gate 				if ((buf=BUF_MEM_new()) == NULL)
1800Sstevel@tonic-gate 					{
1810Sstevel@tonic-gate 					ret= -1;
1820Sstevel@tonic-gate 					goto end;
1830Sstevel@tonic-gate 					}
1840Sstevel@tonic-gate 				if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
1850Sstevel@tonic-gate 					{
1860Sstevel@tonic-gate 					ret= -1;
1870Sstevel@tonic-gate 					goto end;
1880Sstevel@tonic-gate 					}
1890Sstevel@tonic-gate 				s->init_buf=buf;
1900Sstevel@tonic-gate 				}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 			ssl3_init_finished_mac(s);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 			s->state=SSL23_ST_SR_CLNT_HELLO_A;
1950Sstevel@tonic-gate 			s->ctx->stats.sess_accept++;
1960Sstevel@tonic-gate 			s->init_num=0;
1970Sstevel@tonic-gate 			break;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 		case SSL23_ST_SR_CLNT_HELLO_A:
2000Sstevel@tonic-gate 		case SSL23_ST_SR_CLNT_HELLO_B:
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 			s->shutdown=0;
2030Sstevel@tonic-gate 			ret=ssl23_get_client_hello(s);
2040Sstevel@tonic-gate 			if (ret >= 0) cb=NULL;
2050Sstevel@tonic-gate 			goto end;
2060Sstevel@tonic-gate 			/* break; */
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 		default:
2090Sstevel@tonic-gate 			SSLerr(SSL_F_SSL23_ACCEPT,SSL_R_UNKNOWN_STATE);
2100Sstevel@tonic-gate 			ret= -1;
2110Sstevel@tonic-gate 			goto end;
2120Sstevel@tonic-gate 			/* break; */
2130Sstevel@tonic-gate 			}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 		if ((cb != NULL) && (s->state != state))
2160Sstevel@tonic-gate 			{
2170Sstevel@tonic-gate 			new_state=s->state;
2180Sstevel@tonic-gate 			s->state=state;
2190Sstevel@tonic-gate 			cb(s,SSL_CB_ACCEPT_LOOP,1);
2200Sstevel@tonic-gate 			s->state=new_state;
2210Sstevel@tonic-gate 			}
2220Sstevel@tonic-gate 		}
2230Sstevel@tonic-gate end:
2240Sstevel@tonic-gate 	s->in_handshake--;
2250Sstevel@tonic-gate 	if (cb != NULL)
2260Sstevel@tonic-gate 		cb(s,SSL_CB_ACCEPT_EXIT,ret);
2270Sstevel@tonic-gate 	return(ret);
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 
ssl23_get_client_hello(SSL * s)2310Sstevel@tonic-gate int ssl23_get_client_hello(SSL *s)
2320Sstevel@tonic-gate 	{
2330Sstevel@tonic-gate 	char buf_space[11]; /* Request this many bytes in initial read.
2340Sstevel@tonic-gate 	                     * We can detect SSL 3.0/TLS 1.0 Client Hellos
2350Sstevel@tonic-gate 	                     * ('type == 3') correctly only when the following
2360Sstevel@tonic-gate 	                     * is in a single record, which is not guaranteed by
2370Sstevel@tonic-gate 	                     * the protocol specification:
2380Sstevel@tonic-gate 	                     * Byte  Content
2390Sstevel@tonic-gate 	                     *  0     type            \
2400Sstevel@tonic-gate 	                     *  1/2   version          > record header
2410Sstevel@tonic-gate 	                     *  3/4   length          /
2420Sstevel@tonic-gate 	                     *  5     msg_type        \
2430Sstevel@tonic-gate 	                     *  6-8   length           > Client Hello message
2440Sstevel@tonic-gate 	                     *  9/10  client_version  /
2450Sstevel@tonic-gate 	                     */
2460Sstevel@tonic-gate 	char *buf= &(buf_space[0]);
2470Sstevel@tonic-gate 	unsigned char *p,*d,*d_len,*dd;
2480Sstevel@tonic-gate 	unsigned int i;
2490Sstevel@tonic-gate 	unsigned int csl,sil,cl;
2500Sstevel@tonic-gate 	int n=0,j;
2510Sstevel@tonic-gate 	int type=0;
2520Sstevel@tonic-gate 	int v[2];
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	if (s->state ==	SSL23_ST_SR_CLNT_HELLO_A)
2550Sstevel@tonic-gate 		{
2560Sstevel@tonic-gate 		/* read the initial header */
2570Sstevel@tonic-gate 		v[0]=v[1]=0;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 		if (!ssl3_setup_buffers(s)) goto err;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 		n=ssl23_read_bytes(s, sizeof buf_space);
2620Sstevel@tonic-gate 		if (n != sizeof buf_space) return(n); /* n == -1 || n == 0 */
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 		p=s->packet;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 		memcpy(buf,p,n);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 		if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO))
2690Sstevel@tonic-gate 			{
2700Sstevel@tonic-gate 			/*
2710Sstevel@tonic-gate 			 * SSLv2 header
2720Sstevel@tonic-gate 			 */
2730Sstevel@tonic-gate 			if ((p[3] == 0x00) && (p[4] == 0x02))
2740Sstevel@tonic-gate 				{
2750Sstevel@tonic-gate 				v[0]=p[3]; v[1]=p[4];
2760Sstevel@tonic-gate 				/* SSLv2 */
2770Sstevel@tonic-gate 				if (!(s->options & SSL_OP_NO_SSLv2))
2780Sstevel@tonic-gate 					type=1;
2790Sstevel@tonic-gate 				}
2800Sstevel@tonic-gate 			else if (p[3] == SSL3_VERSION_MAJOR)
2810Sstevel@tonic-gate 				{
2820Sstevel@tonic-gate 				v[0]=p[3]; v[1]=p[4];
2830Sstevel@tonic-gate 				/* SSLv3/TLSv1 */
2840Sstevel@tonic-gate 				if (p[4] >= TLS1_VERSION_MINOR)
2850Sstevel@tonic-gate 					{
2860Sstevel@tonic-gate 					if (!(s->options & SSL_OP_NO_TLSv1))
2870Sstevel@tonic-gate 						{
2880Sstevel@tonic-gate 						s->version=TLS1_VERSION;
2890Sstevel@tonic-gate 						/* type=2; */ /* done later to survive restarts */
2900Sstevel@tonic-gate 						s->state=SSL23_ST_SR_CLNT_HELLO_B;
2910Sstevel@tonic-gate 						}
2920Sstevel@tonic-gate 					else if (!(s->options & SSL_OP_NO_SSLv3))
2930Sstevel@tonic-gate 						{
2940Sstevel@tonic-gate 						s->version=SSL3_VERSION;
2950Sstevel@tonic-gate 						/* type=2; */
2960Sstevel@tonic-gate 						s->state=SSL23_ST_SR_CLNT_HELLO_B;
2970Sstevel@tonic-gate 						}
2980Sstevel@tonic-gate 					else if (!(s->options & SSL_OP_NO_SSLv2))
2990Sstevel@tonic-gate 						{
3000Sstevel@tonic-gate 						type=1;
3010Sstevel@tonic-gate 						}
3020Sstevel@tonic-gate 					}
3030Sstevel@tonic-gate 				else if (!(s->options & SSL_OP_NO_SSLv3))
3040Sstevel@tonic-gate 					{
3050Sstevel@tonic-gate 					s->version=SSL3_VERSION;
3060Sstevel@tonic-gate 					/* type=2; */
3070Sstevel@tonic-gate 					s->state=SSL23_ST_SR_CLNT_HELLO_B;
3080Sstevel@tonic-gate 					}
3090Sstevel@tonic-gate 				else if (!(s->options & SSL_OP_NO_SSLv2))
3100Sstevel@tonic-gate 					type=1;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 				}
3130Sstevel@tonic-gate 			}
3140Sstevel@tonic-gate 		else if ((p[0] == SSL3_RT_HANDSHAKE) &&
3150Sstevel@tonic-gate 			 (p[1] == SSL3_VERSION_MAJOR) &&
3160Sstevel@tonic-gate 			 (p[5] == SSL3_MT_CLIENT_HELLO) &&
3170Sstevel@tonic-gate 			 ((p[3] == 0 && p[4] < 5 /* silly record length? */)
3180Sstevel@tonic-gate 				|| (p[9] == p[1])))
3190Sstevel@tonic-gate 			{
3200Sstevel@tonic-gate 			/*
3210Sstevel@tonic-gate 			 * SSLv3 or tls1 header
3220Sstevel@tonic-gate 			 */
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 			v[0]=p[1]; /* major version (= SSL3_VERSION_MAJOR) */
3250Sstevel@tonic-gate 			/* We must look at client_version inside the Client Hello message
3260Sstevel@tonic-gate 			 * to get the correct minor version.
3270Sstevel@tonic-gate 			 * However if we have only a pathologically small fragment of the
3280Sstevel@tonic-gate 			 * Client Hello message, this would be difficult, and we'd have
3290Sstevel@tonic-gate 			 * to read more records to find out.
3300Sstevel@tonic-gate 			 * No known SSL 3.0 client fragments ClientHello like this,
3310Sstevel@tonic-gate 			 * so we simply assume TLS 1.0 to avoid protocol version downgrade
3320Sstevel@tonic-gate 			 * attacks. */
3330Sstevel@tonic-gate 			if (p[3] == 0 && p[4] < 6)
3340Sstevel@tonic-gate 				{
3350Sstevel@tonic-gate #if 0
3360Sstevel@tonic-gate 				SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_SMALL);
3370Sstevel@tonic-gate 				goto err;
3380Sstevel@tonic-gate #else
3390Sstevel@tonic-gate 				v[1] = TLS1_VERSION_MINOR;
3400Sstevel@tonic-gate #endif
3410Sstevel@tonic-gate 				}
3420Sstevel@tonic-gate 			else
3430Sstevel@tonic-gate 				v[1]=p[10]; /* minor version according to client_version */
3440Sstevel@tonic-gate 			if (v[1] >= TLS1_VERSION_MINOR)
3450Sstevel@tonic-gate 				{
3460Sstevel@tonic-gate 				if (!(s->options & SSL_OP_NO_TLSv1))
3470Sstevel@tonic-gate 					{
3480Sstevel@tonic-gate 					s->version=TLS1_VERSION;
3490Sstevel@tonic-gate 					type=3;
3500Sstevel@tonic-gate 					}
3510Sstevel@tonic-gate 				else if (!(s->options & SSL_OP_NO_SSLv3))
3520Sstevel@tonic-gate 					{
3530Sstevel@tonic-gate 					s->version=SSL3_VERSION;
3540Sstevel@tonic-gate 					type=3;
3550Sstevel@tonic-gate 					}
3560Sstevel@tonic-gate 				}
3570Sstevel@tonic-gate 			else
3580Sstevel@tonic-gate 				{
3590Sstevel@tonic-gate 				/* client requests SSL 3.0 */
3600Sstevel@tonic-gate 				if (!(s->options & SSL_OP_NO_SSLv3))
3610Sstevel@tonic-gate 					{
3620Sstevel@tonic-gate 					s->version=SSL3_VERSION;
3630Sstevel@tonic-gate 					type=3;
3640Sstevel@tonic-gate 					}
3650Sstevel@tonic-gate 				else if (!(s->options & SSL_OP_NO_TLSv1))
3660Sstevel@tonic-gate 					{
3670Sstevel@tonic-gate 					/* we won't be able to use TLS of course,
3680Sstevel@tonic-gate 					 * but this will send an appropriate alert */
3690Sstevel@tonic-gate 					s->version=TLS1_VERSION;
3700Sstevel@tonic-gate 					type=3;
3710Sstevel@tonic-gate 					}
3720Sstevel@tonic-gate 				}
3730Sstevel@tonic-gate 			}
3740Sstevel@tonic-gate 		else if ((strncmp("GET ", (char *)p,4) == 0) ||
3750Sstevel@tonic-gate 			 (strncmp("POST ",(char *)p,5) == 0) ||
3760Sstevel@tonic-gate 			 (strncmp("HEAD ",(char *)p,5) == 0) ||
3770Sstevel@tonic-gate 			 (strncmp("PUT ", (char *)p,4) == 0))
3780Sstevel@tonic-gate 			{
3790Sstevel@tonic-gate 			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTP_REQUEST);
3800Sstevel@tonic-gate 			goto err;
3810Sstevel@tonic-gate 			}
3820Sstevel@tonic-gate 		else if (strncmp("CONNECT",(char *)p,7) == 0)
3830Sstevel@tonic-gate 			{
3840Sstevel@tonic-gate 			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTPS_PROXY_REQUEST);
3850Sstevel@tonic-gate 			goto err;
3860Sstevel@tonic-gate 			}
3870Sstevel@tonic-gate 		}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if (s->state == SSL23_ST_SR_CLNT_HELLO_B)
3900Sstevel@tonic-gate 		{
3910Sstevel@tonic-gate 		/* we have SSLv3/TLSv1 in an SSLv2 header
3920Sstevel@tonic-gate 		 * (other cases skip this state) */
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 		type=2;
3950Sstevel@tonic-gate 		p=s->packet;
3960Sstevel@tonic-gate 		v[0] = p[3]; /* == SSL3_VERSION_MAJOR */
3970Sstevel@tonic-gate 		v[1] = p[4];
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 		n=((p[0]&0x7f)<<8)|p[1];
4000Sstevel@tonic-gate 		if (n > (1024*4))
4010Sstevel@tonic-gate 			{
4020Sstevel@tonic-gate 			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE);
4030Sstevel@tonic-gate 			goto err;
4040Sstevel@tonic-gate 			}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 		j=ssl23_read_bytes(s,n+2);
4070Sstevel@tonic-gate 		if (j <= 0) return(j);
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 		ssl3_finish_mac(s, s->packet+2, s->packet_length-2);
4100Sstevel@tonic-gate 		if (s->msg_callback)
4110Sstevel@tonic-gate 			s->msg_callback(0, SSL2_VERSION, 0, s->packet+2, s->packet_length-2, s, s->msg_callback_arg); /* CLIENT-HELLO */
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 		p=s->packet;
4140Sstevel@tonic-gate 		p+=5;
4150Sstevel@tonic-gate 		n2s(p,csl);
4160Sstevel@tonic-gate 		n2s(p,sil);
4170Sstevel@tonic-gate 		n2s(p,cl);
4180Sstevel@tonic-gate 		d=(unsigned char *)s->init_buf->data;
4190Sstevel@tonic-gate 		if ((csl+sil+cl+11) != s->packet_length)
4200Sstevel@tonic-gate 			{
4210Sstevel@tonic-gate 			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_LENGTH_MISMATCH);
4220Sstevel@tonic-gate 			goto err;
4230Sstevel@tonic-gate 			}
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 		/* record header: msg_type ... */
4260Sstevel@tonic-gate 		*(d++) = SSL3_MT_CLIENT_HELLO;
4270Sstevel@tonic-gate 		/* ... and length (actual value will be written later) */
4280Sstevel@tonic-gate 		d_len = d;
4290Sstevel@tonic-gate 		d += 3;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 		/* client_version */
4320Sstevel@tonic-gate 		*(d++) = SSL3_VERSION_MAJOR; /* == v[0] */
4330Sstevel@tonic-gate 		*(d++) = v[1];
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 		/* lets populate the random area */
4360Sstevel@tonic-gate 		/* get the challenge_length */
4370Sstevel@tonic-gate 		i=(cl > SSL3_RANDOM_SIZE)?SSL3_RANDOM_SIZE:cl;
4380Sstevel@tonic-gate 		memset(d,0,SSL3_RANDOM_SIZE);
4390Sstevel@tonic-gate 		memcpy(&(d[SSL3_RANDOM_SIZE-i]),&(p[csl+sil]),i);
4400Sstevel@tonic-gate 		d+=SSL3_RANDOM_SIZE;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 		/* no session-id reuse */
4430Sstevel@tonic-gate 		*(d++)=0;
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 		/* ciphers */
4460Sstevel@tonic-gate 		j=0;
4470Sstevel@tonic-gate 		dd=d;
4480Sstevel@tonic-gate 		d+=2;
4490Sstevel@tonic-gate 		for (i=0; i<csl; i+=3)
4500Sstevel@tonic-gate 			{
4510Sstevel@tonic-gate 			if (p[i] != 0) continue;
4520Sstevel@tonic-gate 			*(d++)=p[i+1];
4530Sstevel@tonic-gate 			*(d++)=p[i+2];
4540Sstevel@tonic-gate 			j+=2;
4550Sstevel@tonic-gate 			}
4560Sstevel@tonic-gate 		s2n(j,dd);
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 		/* COMPRESSION */
4590Sstevel@tonic-gate 		*(d++)=1;
4600Sstevel@tonic-gate 		*(d++)=0;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 		i = (d-(unsigned char *)s->init_buf->data) - 4;
4630Sstevel@tonic-gate 		l2n3((long)i, d_len);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 		/* get the data reused from the init_buf */
4660Sstevel@tonic-gate 		s->s3->tmp.reuse_message=1;
4670Sstevel@tonic-gate 		s->s3->tmp.message_type=SSL3_MT_CLIENT_HELLO;
4680Sstevel@tonic-gate 		s->s3->tmp.message_size=i;
4690Sstevel@tonic-gate 		}
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	/* imaginary new state (for program structure): */
4720Sstevel@tonic-gate 	/* s->state = SSL23_SR_CLNT_HELLO_C */
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	if (type == 1)
4750Sstevel@tonic-gate 		{
4760Sstevel@tonic-gate #ifdef OPENSSL_NO_SSL2
4770Sstevel@tonic-gate 		SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
4780Sstevel@tonic-gate 		goto err;
4790Sstevel@tonic-gate #else
4800Sstevel@tonic-gate 		/* we are talking sslv2 */
4810Sstevel@tonic-gate 		/* we need to clean up the SSLv3/TLSv1 setup and put in the
4820Sstevel@tonic-gate 		 * sslv2 stuff. */
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 		if (s->s2 == NULL)
4850Sstevel@tonic-gate 			{
4860Sstevel@tonic-gate 			if (!ssl2_new(s))
4870Sstevel@tonic-gate 				goto err;
4880Sstevel@tonic-gate 			}
4890Sstevel@tonic-gate 		else
4900Sstevel@tonic-gate 			ssl2_clear(s);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 		if (s->s3 != NULL) ssl3_free(s);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 		if (!BUF_MEM_grow_clean(s->init_buf,
4950Sstevel@tonic-gate 			SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
4960Sstevel@tonic-gate 			{
4970Sstevel@tonic-gate 			goto err;
4980Sstevel@tonic-gate 			}
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 		s->state=SSL2_ST_GET_CLIENT_HELLO_A;
501682Sjp161948 		if (s->options & SSL_OP_NO_TLSv1 && s->options & SSL_OP_NO_SSLv3)
5020Sstevel@tonic-gate 			s->s2->ssl2_rollback=0;
5030Sstevel@tonic-gate 		else
5040Sstevel@tonic-gate 			/* reject SSL 2.0 session if client supports SSL 3.0 or TLS 1.0
5050Sstevel@tonic-gate 			 * (SSL 3.0 draft/RFC 2246, App. E.2) */
5060Sstevel@tonic-gate 			s->s2->ssl2_rollback=1;
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 		/* setup the n bytes we have read so we get them from
5090Sstevel@tonic-gate 		 * the sslv2 buffer */
5100Sstevel@tonic-gate 		s->rstate=SSL_ST_READ_HEADER;
5110Sstevel@tonic-gate 		s->packet_length=n;
5120Sstevel@tonic-gate 		s->packet= &(s->s2->rbuf[0]);
5130Sstevel@tonic-gate 		memcpy(s->packet,buf,n);
5140Sstevel@tonic-gate 		s->s2->rbuf_left=n;
5150Sstevel@tonic-gate 		s->s2->rbuf_offs=0;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 		s->method=SSLv2_server_method();
5180Sstevel@tonic-gate 		s->handshake_func=s->method->ssl_accept;
5190Sstevel@tonic-gate #endif
5200Sstevel@tonic-gate 		}
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	if ((type == 2) || (type == 3))
5230Sstevel@tonic-gate 		{
5240Sstevel@tonic-gate 		/* we have SSLv3/TLSv1 (type 2: SSL2 style, type 3: SSL3/TLS style) */
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 		if (!ssl_init_wbio_buffer(s,1)) goto err;
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 		/* we are in this state */
5290Sstevel@tonic-gate 		s->state=SSL3_ST_SR_CLNT_HELLO_A;
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 		if (type == 3)
5320Sstevel@tonic-gate 			{
5330Sstevel@tonic-gate 			/* put the 'n' bytes we have read into the input buffer
5340Sstevel@tonic-gate 			 * for SSLv3 */
5350Sstevel@tonic-gate 			s->rstate=SSL_ST_READ_HEADER;
5360Sstevel@tonic-gate 			s->packet_length=n;
5370Sstevel@tonic-gate 			s->packet= &(s->s3->rbuf.buf[0]);
5380Sstevel@tonic-gate 			memcpy(s->packet,buf,n);
5390Sstevel@tonic-gate 			s->s3->rbuf.left=n;
5400Sstevel@tonic-gate 			s->s3->rbuf.offset=0;
5410Sstevel@tonic-gate 			}
5420Sstevel@tonic-gate 		else
5430Sstevel@tonic-gate 			{
5440Sstevel@tonic-gate 			s->packet_length=0;
5450Sstevel@tonic-gate 			s->s3->rbuf.left=0;
5460Sstevel@tonic-gate 			s->s3->rbuf.offset=0;
5470Sstevel@tonic-gate 			}
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 		if (s->version == TLS1_VERSION)
5500Sstevel@tonic-gate 			s->method = TLSv1_server_method();
5510Sstevel@tonic-gate 		else
5520Sstevel@tonic-gate 			s->method = SSLv3_server_method();
5530Sstevel@tonic-gate #if 0 /* ssl3_get_client_hello does this */
5540Sstevel@tonic-gate 		s->client_version=(v[0]<<8)|v[1];
5550Sstevel@tonic-gate #endif
5560Sstevel@tonic-gate 		s->handshake_func=s->method->ssl_accept;
5570Sstevel@tonic-gate 		}
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	if ((type < 1) || (type > 3))
5600Sstevel@tonic-gate 		{
5610Sstevel@tonic-gate 		/* bad, very bad */
5620Sstevel@tonic-gate 		SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNKNOWN_PROTOCOL);
5630Sstevel@tonic-gate 		goto err;
5640Sstevel@tonic-gate 		}
5650Sstevel@tonic-gate 	s->init_num=0;
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	if (buf != buf_space) OPENSSL_free(buf);
5680Sstevel@tonic-gate 	s->first_packet=1;
5690Sstevel@tonic-gate 	return(SSL_accept(s));
5700Sstevel@tonic-gate err:
5710Sstevel@tonic-gate 	if (buf != buf_space) OPENSSL_free(buf);
5720Sstevel@tonic-gate 	return(-1);
5730Sstevel@tonic-gate 	}
574