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); 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 1350Sstevel@tonic-gate SSL_METHOD *SSLv23_server_method(void) 1360Sstevel@tonic-gate { 1370Sstevel@tonic-gate static int init=1; 1380Sstevel@tonic-gate static SSL_METHOD SSLv23_server_data; 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate if (init) 1410Sstevel@tonic-gate { 1420Sstevel@tonic-gate CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD); 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate if (init) 1450Sstevel@tonic-gate { 1460Sstevel@tonic-gate memcpy((char *)&SSLv23_server_data, 1470Sstevel@tonic-gate (char *)sslv23_base_method(),sizeof(SSL_METHOD)); 1480Sstevel@tonic-gate SSLv23_server_data.ssl_accept=ssl23_accept; 1490Sstevel@tonic-gate SSLv23_server_data.get_ssl_method=ssl23_get_server_method; 1500Sstevel@tonic-gate init=0; 1510Sstevel@tonic-gate } 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD); 1540Sstevel@tonic-gate } 1550Sstevel@tonic-gate return(&SSLv23_server_data); 1560Sstevel@tonic-gate } 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate int ssl23_accept(SSL *s) 1590Sstevel@tonic-gate { 1600Sstevel@tonic-gate BUF_MEM *buf; 1610Sstevel@tonic-gate unsigned long Time=time(NULL); 1620Sstevel@tonic-gate void (*cb)(const SSL *ssl,int type,int val)=NULL; 1630Sstevel@tonic-gate int ret= -1; 1640Sstevel@tonic-gate int new_state,state; 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate RAND_add(&Time,sizeof(Time),0); 1670Sstevel@tonic-gate ERR_clear_error(); 1680Sstevel@tonic-gate clear_sys_error(); 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate if (s->info_callback != NULL) 1710Sstevel@tonic-gate cb=s->info_callback; 1720Sstevel@tonic-gate else if (s->ctx->info_callback != NULL) 1730Sstevel@tonic-gate cb=s->ctx->info_callback; 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate s->in_handshake++; 1760Sstevel@tonic-gate if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate for (;;) 1790Sstevel@tonic-gate { 1800Sstevel@tonic-gate state=s->state; 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate switch(s->state) 1830Sstevel@tonic-gate { 1840Sstevel@tonic-gate case SSL_ST_BEFORE: 1850Sstevel@tonic-gate case SSL_ST_ACCEPT: 1860Sstevel@tonic-gate case SSL_ST_BEFORE|SSL_ST_ACCEPT: 1870Sstevel@tonic-gate case SSL_ST_OK|SSL_ST_ACCEPT: 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate s->server=1; 1900Sstevel@tonic-gate if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate /* s->version=SSL3_VERSION; */ 1930Sstevel@tonic-gate s->type=SSL_ST_ACCEPT; 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate if (s->init_buf == NULL) 1960Sstevel@tonic-gate { 1970Sstevel@tonic-gate if ((buf=BUF_MEM_new()) == NULL) 1980Sstevel@tonic-gate { 1990Sstevel@tonic-gate ret= -1; 2000Sstevel@tonic-gate goto end; 2010Sstevel@tonic-gate } 2020Sstevel@tonic-gate if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) 2030Sstevel@tonic-gate { 2040Sstevel@tonic-gate ret= -1; 2050Sstevel@tonic-gate goto end; 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate s->init_buf=buf; 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate ssl3_init_finished_mac(s); 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate s->state=SSL23_ST_SR_CLNT_HELLO_A; 2130Sstevel@tonic-gate s->ctx->stats.sess_accept++; 2140Sstevel@tonic-gate s->init_num=0; 2150Sstevel@tonic-gate break; 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate case SSL23_ST_SR_CLNT_HELLO_A: 2180Sstevel@tonic-gate case SSL23_ST_SR_CLNT_HELLO_B: 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate s->shutdown=0; 2210Sstevel@tonic-gate ret=ssl23_get_client_hello(s); 2220Sstevel@tonic-gate if (ret >= 0) cb=NULL; 2230Sstevel@tonic-gate goto end; 2240Sstevel@tonic-gate /* break; */ 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate default: 2270Sstevel@tonic-gate SSLerr(SSL_F_SSL23_ACCEPT,SSL_R_UNKNOWN_STATE); 2280Sstevel@tonic-gate ret= -1; 2290Sstevel@tonic-gate goto end; 2300Sstevel@tonic-gate /* break; */ 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate if ((cb != NULL) && (s->state != state)) 2340Sstevel@tonic-gate { 2350Sstevel@tonic-gate new_state=s->state; 2360Sstevel@tonic-gate s->state=state; 2370Sstevel@tonic-gate cb(s,SSL_CB_ACCEPT_LOOP,1); 2380Sstevel@tonic-gate s->state=new_state; 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate end: 2420Sstevel@tonic-gate s->in_handshake--; 2430Sstevel@tonic-gate if (cb != NULL) 2440Sstevel@tonic-gate cb(s,SSL_CB_ACCEPT_EXIT,ret); 2450Sstevel@tonic-gate return(ret); 2460Sstevel@tonic-gate } 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate int ssl23_get_client_hello(SSL *s) 2500Sstevel@tonic-gate { 2510Sstevel@tonic-gate char buf_space[11]; /* Request this many bytes in initial read. 2520Sstevel@tonic-gate * We can detect SSL 3.0/TLS 1.0 Client Hellos 2530Sstevel@tonic-gate * ('type == 3') correctly only when the following 2540Sstevel@tonic-gate * is in a single record, which is not guaranteed by 2550Sstevel@tonic-gate * the protocol specification: 2560Sstevel@tonic-gate * Byte Content 2570Sstevel@tonic-gate * 0 type \ 2580Sstevel@tonic-gate * 1/2 version > record header 2590Sstevel@tonic-gate * 3/4 length / 2600Sstevel@tonic-gate * 5 msg_type \ 2610Sstevel@tonic-gate * 6-8 length > Client Hello message 2620Sstevel@tonic-gate * 9/10 client_version / 2630Sstevel@tonic-gate */ 2640Sstevel@tonic-gate char *buf= &(buf_space[0]); 2650Sstevel@tonic-gate unsigned char *p,*d,*d_len,*dd; 2660Sstevel@tonic-gate unsigned int i; 2670Sstevel@tonic-gate unsigned int csl,sil,cl; 2680Sstevel@tonic-gate int n=0,j; 2690Sstevel@tonic-gate int type=0; 2700Sstevel@tonic-gate int v[2]; 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate if (s->state == SSL23_ST_SR_CLNT_HELLO_A) 2730Sstevel@tonic-gate { 2740Sstevel@tonic-gate /* read the initial header */ 2750Sstevel@tonic-gate v[0]=v[1]=0; 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate if (!ssl3_setup_buffers(s)) goto err; 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate n=ssl23_read_bytes(s, sizeof buf_space); 2800Sstevel@tonic-gate if (n != sizeof buf_space) return(n); /* n == -1 || n == 0 */ 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate p=s->packet; 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate memcpy(buf,p,n); 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO)) 2870Sstevel@tonic-gate { 2880Sstevel@tonic-gate /* 2890Sstevel@tonic-gate * SSLv2 header 2900Sstevel@tonic-gate */ 2910Sstevel@tonic-gate if ((p[3] == 0x00) && (p[4] == 0x02)) 2920Sstevel@tonic-gate { 2930Sstevel@tonic-gate v[0]=p[3]; v[1]=p[4]; 2940Sstevel@tonic-gate /* SSLv2 */ 2950Sstevel@tonic-gate if (!(s->options & SSL_OP_NO_SSLv2)) 2960Sstevel@tonic-gate type=1; 2970Sstevel@tonic-gate } 2980Sstevel@tonic-gate else if (p[3] == SSL3_VERSION_MAJOR) 2990Sstevel@tonic-gate { 3000Sstevel@tonic-gate v[0]=p[3]; v[1]=p[4]; 3010Sstevel@tonic-gate /* SSLv3/TLSv1 */ 3020Sstevel@tonic-gate if (p[4] >= TLS1_VERSION_MINOR) 3030Sstevel@tonic-gate { 3040Sstevel@tonic-gate if (!(s->options & SSL_OP_NO_TLSv1)) 3050Sstevel@tonic-gate { 3060Sstevel@tonic-gate s->version=TLS1_VERSION; 3070Sstevel@tonic-gate /* type=2; */ /* done later to survive restarts */ 3080Sstevel@tonic-gate s->state=SSL23_ST_SR_CLNT_HELLO_B; 3090Sstevel@tonic-gate } 3100Sstevel@tonic-gate else if (!(s->options & SSL_OP_NO_SSLv3)) 3110Sstevel@tonic-gate { 3120Sstevel@tonic-gate s->version=SSL3_VERSION; 3130Sstevel@tonic-gate /* type=2; */ 3140Sstevel@tonic-gate s->state=SSL23_ST_SR_CLNT_HELLO_B; 3150Sstevel@tonic-gate } 3160Sstevel@tonic-gate else if (!(s->options & SSL_OP_NO_SSLv2)) 3170Sstevel@tonic-gate { 3180Sstevel@tonic-gate type=1; 3190Sstevel@tonic-gate } 3200Sstevel@tonic-gate } 3210Sstevel@tonic-gate else if (!(s->options & SSL_OP_NO_SSLv3)) 3220Sstevel@tonic-gate { 3230Sstevel@tonic-gate s->version=SSL3_VERSION; 3240Sstevel@tonic-gate /* type=2; */ 3250Sstevel@tonic-gate s->state=SSL23_ST_SR_CLNT_HELLO_B; 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate else if (!(s->options & SSL_OP_NO_SSLv2)) 3280Sstevel@tonic-gate type=1; 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate } 3310Sstevel@tonic-gate } 3320Sstevel@tonic-gate else if ((p[0] == SSL3_RT_HANDSHAKE) && 3330Sstevel@tonic-gate (p[1] == SSL3_VERSION_MAJOR) && 3340Sstevel@tonic-gate (p[5] == SSL3_MT_CLIENT_HELLO) && 3350Sstevel@tonic-gate ((p[3] == 0 && p[4] < 5 /* silly record length? */) 3360Sstevel@tonic-gate || (p[9] == p[1]))) 3370Sstevel@tonic-gate { 3380Sstevel@tonic-gate /* 3390Sstevel@tonic-gate * SSLv3 or tls1 header 3400Sstevel@tonic-gate */ 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate v[0]=p[1]; /* major version (= SSL3_VERSION_MAJOR) */ 3430Sstevel@tonic-gate /* We must look at client_version inside the Client Hello message 3440Sstevel@tonic-gate * to get the correct minor version. 3450Sstevel@tonic-gate * However if we have only a pathologically small fragment of the 3460Sstevel@tonic-gate * Client Hello message, this would be difficult, and we'd have 3470Sstevel@tonic-gate * to read more records to find out. 3480Sstevel@tonic-gate * No known SSL 3.0 client fragments ClientHello like this, 3490Sstevel@tonic-gate * so we simply assume TLS 1.0 to avoid protocol version downgrade 3500Sstevel@tonic-gate * attacks. */ 3510Sstevel@tonic-gate if (p[3] == 0 && p[4] < 6) 3520Sstevel@tonic-gate { 3530Sstevel@tonic-gate #if 0 3540Sstevel@tonic-gate SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_SMALL); 3550Sstevel@tonic-gate goto err; 3560Sstevel@tonic-gate #else 3570Sstevel@tonic-gate v[1] = TLS1_VERSION_MINOR; 3580Sstevel@tonic-gate #endif 3590Sstevel@tonic-gate } 3600Sstevel@tonic-gate else 3610Sstevel@tonic-gate v[1]=p[10]; /* minor version according to client_version */ 3620Sstevel@tonic-gate if (v[1] >= TLS1_VERSION_MINOR) 3630Sstevel@tonic-gate { 3640Sstevel@tonic-gate if (!(s->options & SSL_OP_NO_TLSv1)) 3650Sstevel@tonic-gate { 3660Sstevel@tonic-gate s->version=TLS1_VERSION; 3670Sstevel@tonic-gate type=3; 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate else if (!(s->options & SSL_OP_NO_SSLv3)) 3700Sstevel@tonic-gate { 3710Sstevel@tonic-gate s->version=SSL3_VERSION; 3720Sstevel@tonic-gate type=3; 3730Sstevel@tonic-gate } 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate else 3760Sstevel@tonic-gate { 3770Sstevel@tonic-gate /* client requests SSL 3.0 */ 3780Sstevel@tonic-gate if (!(s->options & SSL_OP_NO_SSLv3)) 3790Sstevel@tonic-gate { 3800Sstevel@tonic-gate s->version=SSL3_VERSION; 3810Sstevel@tonic-gate type=3; 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate else if (!(s->options & SSL_OP_NO_TLSv1)) 3840Sstevel@tonic-gate { 3850Sstevel@tonic-gate /* we won't be able to use TLS of course, 3860Sstevel@tonic-gate * but this will send an appropriate alert */ 3870Sstevel@tonic-gate s->version=TLS1_VERSION; 3880Sstevel@tonic-gate type=3; 3890Sstevel@tonic-gate } 3900Sstevel@tonic-gate } 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate else if ((strncmp("GET ", (char *)p,4) == 0) || 3930Sstevel@tonic-gate (strncmp("POST ",(char *)p,5) == 0) || 3940Sstevel@tonic-gate (strncmp("HEAD ",(char *)p,5) == 0) || 3950Sstevel@tonic-gate (strncmp("PUT ", (char *)p,4) == 0)) 3960Sstevel@tonic-gate { 3970Sstevel@tonic-gate SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTP_REQUEST); 3980Sstevel@tonic-gate goto err; 3990Sstevel@tonic-gate } 4000Sstevel@tonic-gate else if (strncmp("CONNECT",(char *)p,7) == 0) 4010Sstevel@tonic-gate { 4020Sstevel@tonic-gate SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTPS_PROXY_REQUEST); 4030Sstevel@tonic-gate goto err; 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate } 4060Sstevel@tonic-gate 4070Sstevel@tonic-gate if (s->state == SSL23_ST_SR_CLNT_HELLO_B) 4080Sstevel@tonic-gate { 4090Sstevel@tonic-gate /* we have SSLv3/TLSv1 in an SSLv2 header 4100Sstevel@tonic-gate * (other cases skip this state) */ 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate type=2; 4130Sstevel@tonic-gate p=s->packet; 4140Sstevel@tonic-gate v[0] = p[3]; /* == SSL3_VERSION_MAJOR */ 4150Sstevel@tonic-gate v[1] = p[4]; 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate n=((p[0]&0x7f)<<8)|p[1]; 4180Sstevel@tonic-gate if (n > (1024*4)) 4190Sstevel@tonic-gate { 4200Sstevel@tonic-gate SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE); 4210Sstevel@tonic-gate goto err; 4220Sstevel@tonic-gate } 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate j=ssl23_read_bytes(s,n+2); 4250Sstevel@tonic-gate if (j <= 0) return(j); 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate ssl3_finish_mac(s, s->packet+2, s->packet_length-2); 4280Sstevel@tonic-gate if (s->msg_callback) 4290Sstevel@tonic-gate s->msg_callback(0, SSL2_VERSION, 0, s->packet+2, s->packet_length-2, s, s->msg_callback_arg); /* CLIENT-HELLO */ 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate p=s->packet; 4320Sstevel@tonic-gate p+=5; 4330Sstevel@tonic-gate n2s(p,csl); 4340Sstevel@tonic-gate n2s(p,sil); 4350Sstevel@tonic-gate n2s(p,cl); 4360Sstevel@tonic-gate d=(unsigned char *)s->init_buf->data; 4370Sstevel@tonic-gate if ((csl+sil+cl+11) != s->packet_length) 4380Sstevel@tonic-gate { 4390Sstevel@tonic-gate SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_LENGTH_MISMATCH); 4400Sstevel@tonic-gate goto err; 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate /* record header: msg_type ... */ 4440Sstevel@tonic-gate *(d++) = SSL3_MT_CLIENT_HELLO; 4450Sstevel@tonic-gate /* ... and length (actual value will be written later) */ 4460Sstevel@tonic-gate d_len = d; 4470Sstevel@tonic-gate d += 3; 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate /* client_version */ 4500Sstevel@tonic-gate *(d++) = SSL3_VERSION_MAJOR; /* == v[0] */ 4510Sstevel@tonic-gate *(d++) = v[1]; 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate /* lets populate the random area */ 4540Sstevel@tonic-gate /* get the challenge_length */ 4550Sstevel@tonic-gate i=(cl > SSL3_RANDOM_SIZE)?SSL3_RANDOM_SIZE:cl; 4560Sstevel@tonic-gate memset(d,0,SSL3_RANDOM_SIZE); 4570Sstevel@tonic-gate memcpy(&(d[SSL3_RANDOM_SIZE-i]),&(p[csl+sil]),i); 4580Sstevel@tonic-gate d+=SSL3_RANDOM_SIZE; 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate /* no session-id reuse */ 4610Sstevel@tonic-gate *(d++)=0; 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate /* ciphers */ 4640Sstevel@tonic-gate j=0; 4650Sstevel@tonic-gate dd=d; 4660Sstevel@tonic-gate d+=2; 4670Sstevel@tonic-gate for (i=0; i<csl; i+=3) 4680Sstevel@tonic-gate { 4690Sstevel@tonic-gate if (p[i] != 0) continue; 4700Sstevel@tonic-gate *(d++)=p[i+1]; 4710Sstevel@tonic-gate *(d++)=p[i+2]; 4720Sstevel@tonic-gate j+=2; 4730Sstevel@tonic-gate } 4740Sstevel@tonic-gate s2n(j,dd); 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate /* COMPRESSION */ 4770Sstevel@tonic-gate *(d++)=1; 4780Sstevel@tonic-gate *(d++)=0; 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate i = (d-(unsigned char *)s->init_buf->data) - 4; 4810Sstevel@tonic-gate l2n3((long)i, d_len); 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate /* get the data reused from the init_buf */ 4840Sstevel@tonic-gate s->s3->tmp.reuse_message=1; 4850Sstevel@tonic-gate s->s3->tmp.message_type=SSL3_MT_CLIENT_HELLO; 4860Sstevel@tonic-gate s->s3->tmp.message_size=i; 4870Sstevel@tonic-gate } 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate /* imaginary new state (for program structure): */ 4900Sstevel@tonic-gate /* s->state = SSL23_SR_CLNT_HELLO_C */ 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate if (type == 1) 4930Sstevel@tonic-gate { 4940Sstevel@tonic-gate #ifdef OPENSSL_NO_SSL2 4950Sstevel@tonic-gate SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNSUPPORTED_PROTOCOL); 4960Sstevel@tonic-gate goto err; 4970Sstevel@tonic-gate #else 4980Sstevel@tonic-gate /* we are talking sslv2 */ 4990Sstevel@tonic-gate /* we need to clean up the SSLv3/TLSv1 setup and put in the 5000Sstevel@tonic-gate * sslv2 stuff. */ 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate if (s->s2 == NULL) 5030Sstevel@tonic-gate { 5040Sstevel@tonic-gate if (!ssl2_new(s)) 5050Sstevel@tonic-gate goto err; 5060Sstevel@tonic-gate } 5070Sstevel@tonic-gate else 5080Sstevel@tonic-gate ssl2_clear(s); 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate if (s->s3 != NULL) ssl3_free(s); 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate if (!BUF_MEM_grow_clean(s->init_buf, 5130Sstevel@tonic-gate SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)) 5140Sstevel@tonic-gate { 5150Sstevel@tonic-gate goto err; 5160Sstevel@tonic-gate } 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate s->state=SSL2_ST_GET_CLIENT_HELLO_A; 519*682Sjp161948 if (s->options & SSL_OP_NO_TLSv1 && s->options & SSL_OP_NO_SSLv3) 5200Sstevel@tonic-gate s->s2->ssl2_rollback=0; 5210Sstevel@tonic-gate else 5220Sstevel@tonic-gate /* reject SSL 2.0 session if client supports SSL 3.0 or TLS 1.0 5230Sstevel@tonic-gate * (SSL 3.0 draft/RFC 2246, App. E.2) */ 5240Sstevel@tonic-gate s->s2->ssl2_rollback=1; 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate /* setup the n bytes we have read so we get them from 5270Sstevel@tonic-gate * the sslv2 buffer */ 5280Sstevel@tonic-gate s->rstate=SSL_ST_READ_HEADER; 5290Sstevel@tonic-gate s->packet_length=n; 5300Sstevel@tonic-gate s->packet= &(s->s2->rbuf[0]); 5310Sstevel@tonic-gate memcpy(s->packet,buf,n); 5320Sstevel@tonic-gate s->s2->rbuf_left=n; 5330Sstevel@tonic-gate s->s2->rbuf_offs=0; 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate s->method=SSLv2_server_method(); 5360Sstevel@tonic-gate s->handshake_func=s->method->ssl_accept; 5370Sstevel@tonic-gate #endif 5380Sstevel@tonic-gate } 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate if ((type == 2) || (type == 3)) 5410Sstevel@tonic-gate { 5420Sstevel@tonic-gate /* we have SSLv3/TLSv1 (type 2: SSL2 style, type 3: SSL3/TLS style) */ 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate if (!ssl_init_wbio_buffer(s,1)) goto err; 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate /* we are in this state */ 5470Sstevel@tonic-gate s->state=SSL3_ST_SR_CLNT_HELLO_A; 5480Sstevel@tonic-gate 5490Sstevel@tonic-gate if (type == 3) 5500Sstevel@tonic-gate { 5510Sstevel@tonic-gate /* put the 'n' bytes we have read into the input buffer 5520Sstevel@tonic-gate * for SSLv3 */ 5530Sstevel@tonic-gate s->rstate=SSL_ST_READ_HEADER; 5540Sstevel@tonic-gate s->packet_length=n; 5550Sstevel@tonic-gate s->packet= &(s->s3->rbuf.buf[0]); 5560Sstevel@tonic-gate memcpy(s->packet,buf,n); 5570Sstevel@tonic-gate s->s3->rbuf.left=n; 5580Sstevel@tonic-gate s->s3->rbuf.offset=0; 5590Sstevel@tonic-gate } 5600Sstevel@tonic-gate else 5610Sstevel@tonic-gate { 5620Sstevel@tonic-gate s->packet_length=0; 5630Sstevel@tonic-gate s->s3->rbuf.left=0; 5640Sstevel@tonic-gate s->s3->rbuf.offset=0; 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate if (s->version == TLS1_VERSION) 5680Sstevel@tonic-gate s->method = TLSv1_server_method(); 5690Sstevel@tonic-gate else 5700Sstevel@tonic-gate s->method = SSLv3_server_method(); 5710Sstevel@tonic-gate #if 0 /* ssl3_get_client_hello does this */ 5720Sstevel@tonic-gate s->client_version=(v[0]<<8)|v[1]; 5730Sstevel@tonic-gate #endif 5740Sstevel@tonic-gate s->handshake_func=s->method->ssl_accept; 5750Sstevel@tonic-gate } 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate if ((type < 1) || (type > 3)) 5780Sstevel@tonic-gate { 5790Sstevel@tonic-gate /* bad, very bad */ 5800Sstevel@tonic-gate SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNKNOWN_PROTOCOL); 5810Sstevel@tonic-gate goto err; 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate s->init_num=0; 5840Sstevel@tonic-gate 5850Sstevel@tonic-gate if (buf != buf_space) OPENSSL_free(buf); 5860Sstevel@tonic-gate s->first_packet=1; 5870Sstevel@tonic-gate return(SSL_accept(s)); 5880Sstevel@tonic-gate err: 5890Sstevel@tonic-gate if (buf != buf_space) OPENSSL_free(buf); 5900Sstevel@tonic-gate return(-1); 5910Sstevel@tonic-gate } 592