1*0Sstevel@tonic-gate /* ssl/s23_srvr.c */ 2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3*0Sstevel@tonic-gate * All rights reserved. 4*0Sstevel@tonic-gate * 5*0Sstevel@tonic-gate * This package is an SSL implementation written 6*0Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com). 7*0Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as 10*0Sstevel@tonic-gate * the following conditions are aheared to. The following conditions 11*0Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA, 12*0Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13*0Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms 14*0Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15*0Sstevel@tonic-gate * 16*0Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in 17*0Sstevel@tonic-gate * the code are not to be removed. 18*0Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution 19*0Sstevel@tonic-gate * as the author of the parts of the library used. 20*0Sstevel@tonic-gate * This can be in the form of a textual message at program startup or 21*0Sstevel@tonic-gate * in documentation (online or textual) provided with the package. 22*0Sstevel@tonic-gate * 23*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 24*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 25*0Sstevel@tonic-gate * are met: 26*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright 27*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 28*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 29*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 30*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 31*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 32*0Sstevel@tonic-gate * must display the following acknowledgement: 33*0Sstevel@tonic-gate * "This product includes cryptographic software written by 34*0Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)" 35*0Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library 36*0Sstevel@tonic-gate * being used are not cryptographic related :-). 37*0Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from 38*0Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement: 39*0Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40*0Sstevel@tonic-gate * 41*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51*0Sstevel@tonic-gate * SUCH DAMAGE. 52*0Sstevel@tonic-gate * 53*0Sstevel@tonic-gate * The licence and distribution terms for any publically available version or 54*0Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be 55*0Sstevel@tonic-gate * copied and put under another distribution licence 56*0Sstevel@tonic-gate * [including the GNU Public Licence.] 57*0Sstevel@tonic-gate */ 58*0Sstevel@tonic-gate /* ==================================================================== 59*0Sstevel@tonic-gate * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. 60*0Sstevel@tonic-gate * 61*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 62*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 63*0Sstevel@tonic-gate * are met: 64*0Sstevel@tonic-gate * 65*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 66*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 67*0Sstevel@tonic-gate * 68*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 69*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in 70*0Sstevel@tonic-gate * the documentation and/or other materials provided with the 71*0Sstevel@tonic-gate * distribution. 72*0Sstevel@tonic-gate * 73*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this 74*0Sstevel@tonic-gate * software must display the following acknowledgment: 75*0Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 76*0Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 77*0Sstevel@tonic-gate * 78*0Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 79*0Sstevel@tonic-gate * endorse or promote products derived from this software without 80*0Sstevel@tonic-gate * prior written permission. For written permission, please contact 81*0Sstevel@tonic-gate * openssl-core@openssl.org. 82*0Sstevel@tonic-gate * 83*0Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL" 84*0Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written 85*0Sstevel@tonic-gate * permission of the OpenSSL Project. 86*0Sstevel@tonic-gate * 87*0Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following 88*0Sstevel@tonic-gate * acknowledgment: 89*0Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 90*0Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 91*0Sstevel@tonic-gate * 92*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 93*0Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 94*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 95*0Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 96*0Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 97*0Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 98*0Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 99*0Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 100*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 101*0Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 102*0Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 103*0Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE. 104*0Sstevel@tonic-gate * ==================================================================== 105*0Sstevel@tonic-gate * 106*0Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young 107*0Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim 108*0Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com). 109*0Sstevel@tonic-gate * 110*0Sstevel@tonic-gate */ 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate #include <stdio.h> 113*0Sstevel@tonic-gate #include "ssl_locl.h" 114*0Sstevel@tonic-gate #include <openssl/buffer.h> 115*0Sstevel@tonic-gate #include <openssl/rand.h> 116*0Sstevel@tonic-gate #include <openssl/objects.h> 117*0Sstevel@tonic-gate #include <openssl/evp.h> 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate static SSL_METHOD *ssl23_get_server_method(int ver); 120*0Sstevel@tonic-gate int ssl23_get_client_hello(SSL *s); 121*0Sstevel@tonic-gate static SSL_METHOD *ssl23_get_server_method(int ver) 122*0Sstevel@tonic-gate { 123*0Sstevel@tonic-gate #ifndef OPENSSL_NO_SSL2 124*0Sstevel@tonic-gate if (ver == SSL2_VERSION) 125*0Sstevel@tonic-gate return(SSLv2_server_method()); 126*0Sstevel@tonic-gate #endif 127*0Sstevel@tonic-gate if (ver == SSL3_VERSION) 128*0Sstevel@tonic-gate return(SSLv3_server_method()); 129*0Sstevel@tonic-gate else if (ver == TLS1_VERSION) 130*0Sstevel@tonic-gate return(TLSv1_server_method()); 131*0Sstevel@tonic-gate else 132*0Sstevel@tonic-gate return(NULL); 133*0Sstevel@tonic-gate } 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate SSL_METHOD *SSLv23_server_method(void) 136*0Sstevel@tonic-gate { 137*0Sstevel@tonic-gate static int init=1; 138*0Sstevel@tonic-gate static SSL_METHOD SSLv23_server_data; 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate if (init) 141*0Sstevel@tonic-gate { 142*0Sstevel@tonic-gate CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD); 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate if (init) 145*0Sstevel@tonic-gate { 146*0Sstevel@tonic-gate memcpy((char *)&SSLv23_server_data, 147*0Sstevel@tonic-gate (char *)sslv23_base_method(),sizeof(SSL_METHOD)); 148*0Sstevel@tonic-gate SSLv23_server_data.ssl_accept=ssl23_accept; 149*0Sstevel@tonic-gate SSLv23_server_data.get_ssl_method=ssl23_get_server_method; 150*0Sstevel@tonic-gate init=0; 151*0Sstevel@tonic-gate } 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD); 154*0Sstevel@tonic-gate } 155*0Sstevel@tonic-gate return(&SSLv23_server_data); 156*0Sstevel@tonic-gate } 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate int ssl23_accept(SSL *s) 159*0Sstevel@tonic-gate { 160*0Sstevel@tonic-gate BUF_MEM *buf; 161*0Sstevel@tonic-gate unsigned long Time=time(NULL); 162*0Sstevel@tonic-gate void (*cb)(const SSL *ssl,int type,int val)=NULL; 163*0Sstevel@tonic-gate int ret= -1; 164*0Sstevel@tonic-gate int new_state,state; 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate RAND_add(&Time,sizeof(Time),0); 167*0Sstevel@tonic-gate ERR_clear_error(); 168*0Sstevel@tonic-gate clear_sys_error(); 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate if (s->info_callback != NULL) 171*0Sstevel@tonic-gate cb=s->info_callback; 172*0Sstevel@tonic-gate else if (s->ctx->info_callback != NULL) 173*0Sstevel@tonic-gate cb=s->ctx->info_callback; 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate s->in_handshake++; 176*0Sstevel@tonic-gate if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate for (;;) 179*0Sstevel@tonic-gate { 180*0Sstevel@tonic-gate state=s->state; 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate switch(s->state) 183*0Sstevel@tonic-gate { 184*0Sstevel@tonic-gate case SSL_ST_BEFORE: 185*0Sstevel@tonic-gate case SSL_ST_ACCEPT: 186*0Sstevel@tonic-gate case SSL_ST_BEFORE|SSL_ST_ACCEPT: 187*0Sstevel@tonic-gate case SSL_ST_OK|SSL_ST_ACCEPT: 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate s->server=1; 190*0Sstevel@tonic-gate if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate /* s->version=SSL3_VERSION; */ 193*0Sstevel@tonic-gate s->type=SSL_ST_ACCEPT; 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate if (s->init_buf == NULL) 196*0Sstevel@tonic-gate { 197*0Sstevel@tonic-gate if ((buf=BUF_MEM_new()) == NULL) 198*0Sstevel@tonic-gate { 199*0Sstevel@tonic-gate ret= -1; 200*0Sstevel@tonic-gate goto end; 201*0Sstevel@tonic-gate } 202*0Sstevel@tonic-gate if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) 203*0Sstevel@tonic-gate { 204*0Sstevel@tonic-gate ret= -1; 205*0Sstevel@tonic-gate goto end; 206*0Sstevel@tonic-gate } 207*0Sstevel@tonic-gate s->init_buf=buf; 208*0Sstevel@tonic-gate } 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate ssl3_init_finished_mac(s); 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate s->state=SSL23_ST_SR_CLNT_HELLO_A; 213*0Sstevel@tonic-gate s->ctx->stats.sess_accept++; 214*0Sstevel@tonic-gate s->init_num=0; 215*0Sstevel@tonic-gate break; 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gate case SSL23_ST_SR_CLNT_HELLO_A: 218*0Sstevel@tonic-gate case SSL23_ST_SR_CLNT_HELLO_B: 219*0Sstevel@tonic-gate 220*0Sstevel@tonic-gate s->shutdown=0; 221*0Sstevel@tonic-gate ret=ssl23_get_client_hello(s); 222*0Sstevel@tonic-gate if (ret >= 0) cb=NULL; 223*0Sstevel@tonic-gate goto end; 224*0Sstevel@tonic-gate /* break; */ 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate default: 227*0Sstevel@tonic-gate SSLerr(SSL_F_SSL23_ACCEPT,SSL_R_UNKNOWN_STATE); 228*0Sstevel@tonic-gate ret= -1; 229*0Sstevel@tonic-gate goto end; 230*0Sstevel@tonic-gate /* break; */ 231*0Sstevel@tonic-gate } 232*0Sstevel@tonic-gate 233*0Sstevel@tonic-gate if ((cb != NULL) && (s->state != state)) 234*0Sstevel@tonic-gate { 235*0Sstevel@tonic-gate new_state=s->state; 236*0Sstevel@tonic-gate s->state=state; 237*0Sstevel@tonic-gate cb(s,SSL_CB_ACCEPT_LOOP,1); 238*0Sstevel@tonic-gate s->state=new_state; 239*0Sstevel@tonic-gate } 240*0Sstevel@tonic-gate } 241*0Sstevel@tonic-gate end: 242*0Sstevel@tonic-gate s->in_handshake--; 243*0Sstevel@tonic-gate if (cb != NULL) 244*0Sstevel@tonic-gate cb(s,SSL_CB_ACCEPT_EXIT,ret); 245*0Sstevel@tonic-gate return(ret); 246*0Sstevel@tonic-gate } 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate 249*0Sstevel@tonic-gate int ssl23_get_client_hello(SSL *s) 250*0Sstevel@tonic-gate { 251*0Sstevel@tonic-gate char buf_space[11]; /* Request this many bytes in initial read. 252*0Sstevel@tonic-gate * We can detect SSL 3.0/TLS 1.0 Client Hellos 253*0Sstevel@tonic-gate * ('type == 3') correctly only when the following 254*0Sstevel@tonic-gate * is in a single record, which is not guaranteed by 255*0Sstevel@tonic-gate * the protocol specification: 256*0Sstevel@tonic-gate * Byte Content 257*0Sstevel@tonic-gate * 0 type \ 258*0Sstevel@tonic-gate * 1/2 version > record header 259*0Sstevel@tonic-gate * 3/4 length / 260*0Sstevel@tonic-gate * 5 msg_type \ 261*0Sstevel@tonic-gate * 6-8 length > Client Hello message 262*0Sstevel@tonic-gate * 9/10 client_version / 263*0Sstevel@tonic-gate */ 264*0Sstevel@tonic-gate char *buf= &(buf_space[0]); 265*0Sstevel@tonic-gate unsigned char *p,*d,*d_len,*dd; 266*0Sstevel@tonic-gate unsigned int i; 267*0Sstevel@tonic-gate unsigned int csl,sil,cl; 268*0Sstevel@tonic-gate int n=0,j; 269*0Sstevel@tonic-gate int type=0; 270*0Sstevel@tonic-gate int v[2]; 271*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA 272*0Sstevel@tonic-gate int use_sslv2_strong=0; 273*0Sstevel@tonic-gate #endif 274*0Sstevel@tonic-gate 275*0Sstevel@tonic-gate if (s->state == SSL23_ST_SR_CLNT_HELLO_A) 276*0Sstevel@tonic-gate { 277*0Sstevel@tonic-gate /* read the initial header */ 278*0Sstevel@tonic-gate v[0]=v[1]=0; 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gate if (!ssl3_setup_buffers(s)) goto err; 281*0Sstevel@tonic-gate 282*0Sstevel@tonic-gate n=ssl23_read_bytes(s, sizeof buf_space); 283*0Sstevel@tonic-gate if (n != sizeof buf_space) return(n); /* n == -1 || n == 0 */ 284*0Sstevel@tonic-gate 285*0Sstevel@tonic-gate p=s->packet; 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate memcpy(buf,p,n); 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gate if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO)) 290*0Sstevel@tonic-gate { 291*0Sstevel@tonic-gate /* 292*0Sstevel@tonic-gate * SSLv2 header 293*0Sstevel@tonic-gate */ 294*0Sstevel@tonic-gate if ((p[3] == 0x00) && (p[4] == 0x02)) 295*0Sstevel@tonic-gate { 296*0Sstevel@tonic-gate v[0]=p[3]; v[1]=p[4]; 297*0Sstevel@tonic-gate /* SSLv2 */ 298*0Sstevel@tonic-gate if (!(s->options & SSL_OP_NO_SSLv2)) 299*0Sstevel@tonic-gate type=1; 300*0Sstevel@tonic-gate } 301*0Sstevel@tonic-gate else if (p[3] == SSL3_VERSION_MAJOR) 302*0Sstevel@tonic-gate { 303*0Sstevel@tonic-gate v[0]=p[3]; v[1]=p[4]; 304*0Sstevel@tonic-gate /* SSLv3/TLSv1 */ 305*0Sstevel@tonic-gate if (p[4] >= TLS1_VERSION_MINOR) 306*0Sstevel@tonic-gate { 307*0Sstevel@tonic-gate if (!(s->options & SSL_OP_NO_TLSv1)) 308*0Sstevel@tonic-gate { 309*0Sstevel@tonic-gate s->version=TLS1_VERSION; 310*0Sstevel@tonic-gate /* type=2; */ /* done later to survive restarts */ 311*0Sstevel@tonic-gate s->state=SSL23_ST_SR_CLNT_HELLO_B; 312*0Sstevel@tonic-gate } 313*0Sstevel@tonic-gate else if (!(s->options & SSL_OP_NO_SSLv3)) 314*0Sstevel@tonic-gate { 315*0Sstevel@tonic-gate s->version=SSL3_VERSION; 316*0Sstevel@tonic-gate /* type=2; */ 317*0Sstevel@tonic-gate s->state=SSL23_ST_SR_CLNT_HELLO_B; 318*0Sstevel@tonic-gate } 319*0Sstevel@tonic-gate else if (!(s->options & SSL_OP_NO_SSLv2)) 320*0Sstevel@tonic-gate { 321*0Sstevel@tonic-gate type=1; 322*0Sstevel@tonic-gate } 323*0Sstevel@tonic-gate } 324*0Sstevel@tonic-gate else if (!(s->options & SSL_OP_NO_SSLv3)) 325*0Sstevel@tonic-gate { 326*0Sstevel@tonic-gate s->version=SSL3_VERSION; 327*0Sstevel@tonic-gate /* type=2; */ 328*0Sstevel@tonic-gate s->state=SSL23_ST_SR_CLNT_HELLO_B; 329*0Sstevel@tonic-gate } 330*0Sstevel@tonic-gate else if (!(s->options & SSL_OP_NO_SSLv2)) 331*0Sstevel@tonic-gate type=1; 332*0Sstevel@tonic-gate 333*0Sstevel@tonic-gate } 334*0Sstevel@tonic-gate } 335*0Sstevel@tonic-gate else if ((p[0] == SSL3_RT_HANDSHAKE) && 336*0Sstevel@tonic-gate (p[1] == SSL3_VERSION_MAJOR) && 337*0Sstevel@tonic-gate (p[5] == SSL3_MT_CLIENT_HELLO) && 338*0Sstevel@tonic-gate ((p[3] == 0 && p[4] < 5 /* silly record length? */) 339*0Sstevel@tonic-gate || (p[9] == p[1]))) 340*0Sstevel@tonic-gate { 341*0Sstevel@tonic-gate /* 342*0Sstevel@tonic-gate * SSLv3 or tls1 header 343*0Sstevel@tonic-gate */ 344*0Sstevel@tonic-gate 345*0Sstevel@tonic-gate v[0]=p[1]; /* major version (= SSL3_VERSION_MAJOR) */ 346*0Sstevel@tonic-gate /* We must look at client_version inside the Client Hello message 347*0Sstevel@tonic-gate * to get the correct minor version. 348*0Sstevel@tonic-gate * However if we have only a pathologically small fragment of the 349*0Sstevel@tonic-gate * Client Hello message, this would be difficult, and we'd have 350*0Sstevel@tonic-gate * to read more records to find out. 351*0Sstevel@tonic-gate * No known SSL 3.0 client fragments ClientHello like this, 352*0Sstevel@tonic-gate * so we simply assume TLS 1.0 to avoid protocol version downgrade 353*0Sstevel@tonic-gate * attacks. */ 354*0Sstevel@tonic-gate if (p[3] == 0 && p[4] < 6) 355*0Sstevel@tonic-gate { 356*0Sstevel@tonic-gate #if 0 357*0Sstevel@tonic-gate SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_SMALL); 358*0Sstevel@tonic-gate goto err; 359*0Sstevel@tonic-gate #else 360*0Sstevel@tonic-gate v[1] = TLS1_VERSION_MINOR; 361*0Sstevel@tonic-gate #endif 362*0Sstevel@tonic-gate } 363*0Sstevel@tonic-gate else 364*0Sstevel@tonic-gate v[1]=p[10]; /* minor version according to client_version */ 365*0Sstevel@tonic-gate if (v[1] >= TLS1_VERSION_MINOR) 366*0Sstevel@tonic-gate { 367*0Sstevel@tonic-gate if (!(s->options & SSL_OP_NO_TLSv1)) 368*0Sstevel@tonic-gate { 369*0Sstevel@tonic-gate s->version=TLS1_VERSION; 370*0Sstevel@tonic-gate type=3; 371*0Sstevel@tonic-gate } 372*0Sstevel@tonic-gate else if (!(s->options & SSL_OP_NO_SSLv3)) 373*0Sstevel@tonic-gate { 374*0Sstevel@tonic-gate s->version=SSL3_VERSION; 375*0Sstevel@tonic-gate type=3; 376*0Sstevel@tonic-gate } 377*0Sstevel@tonic-gate } 378*0Sstevel@tonic-gate else 379*0Sstevel@tonic-gate { 380*0Sstevel@tonic-gate /* client requests SSL 3.0 */ 381*0Sstevel@tonic-gate if (!(s->options & SSL_OP_NO_SSLv3)) 382*0Sstevel@tonic-gate { 383*0Sstevel@tonic-gate s->version=SSL3_VERSION; 384*0Sstevel@tonic-gate type=3; 385*0Sstevel@tonic-gate } 386*0Sstevel@tonic-gate else if (!(s->options & SSL_OP_NO_TLSv1)) 387*0Sstevel@tonic-gate { 388*0Sstevel@tonic-gate /* we won't be able to use TLS of course, 389*0Sstevel@tonic-gate * but this will send an appropriate alert */ 390*0Sstevel@tonic-gate s->version=TLS1_VERSION; 391*0Sstevel@tonic-gate type=3; 392*0Sstevel@tonic-gate } 393*0Sstevel@tonic-gate } 394*0Sstevel@tonic-gate } 395*0Sstevel@tonic-gate else if ((strncmp("GET ", (char *)p,4) == 0) || 396*0Sstevel@tonic-gate (strncmp("POST ",(char *)p,5) == 0) || 397*0Sstevel@tonic-gate (strncmp("HEAD ",(char *)p,5) == 0) || 398*0Sstevel@tonic-gate (strncmp("PUT ", (char *)p,4) == 0)) 399*0Sstevel@tonic-gate { 400*0Sstevel@tonic-gate SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTP_REQUEST); 401*0Sstevel@tonic-gate goto err; 402*0Sstevel@tonic-gate } 403*0Sstevel@tonic-gate else if (strncmp("CONNECT",(char *)p,7) == 0) 404*0Sstevel@tonic-gate { 405*0Sstevel@tonic-gate SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTPS_PROXY_REQUEST); 406*0Sstevel@tonic-gate goto err; 407*0Sstevel@tonic-gate } 408*0Sstevel@tonic-gate } 409*0Sstevel@tonic-gate 410*0Sstevel@tonic-gate if (s->state == SSL23_ST_SR_CLNT_HELLO_B) 411*0Sstevel@tonic-gate { 412*0Sstevel@tonic-gate /* we have SSLv3/TLSv1 in an SSLv2 header 413*0Sstevel@tonic-gate * (other cases skip this state) */ 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gate type=2; 416*0Sstevel@tonic-gate p=s->packet; 417*0Sstevel@tonic-gate v[0] = p[3]; /* == SSL3_VERSION_MAJOR */ 418*0Sstevel@tonic-gate v[1] = p[4]; 419*0Sstevel@tonic-gate 420*0Sstevel@tonic-gate n=((p[0]&0x7f)<<8)|p[1]; 421*0Sstevel@tonic-gate if (n > (1024*4)) 422*0Sstevel@tonic-gate { 423*0Sstevel@tonic-gate SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE); 424*0Sstevel@tonic-gate goto err; 425*0Sstevel@tonic-gate } 426*0Sstevel@tonic-gate 427*0Sstevel@tonic-gate j=ssl23_read_bytes(s,n+2); 428*0Sstevel@tonic-gate if (j <= 0) return(j); 429*0Sstevel@tonic-gate 430*0Sstevel@tonic-gate ssl3_finish_mac(s, s->packet+2, s->packet_length-2); 431*0Sstevel@tonic-gate if (s->msg_callback) 432*0Sstevel@tonic-gate s->msg_callback(0, SSL2_VERSION, 0, s->packet+2, s->packet_length-2, s, s->msg_callback_arg); /* CLIENT-HELLO */ 433*0Sstevel@tonic-gate 434*0Sstevel@tonic-gate p=s->packet; 435*0Sstevel@tonic-gate p+=5; 436*0Sstevel@tonic-gate n2s(p,csl); 437*0Sstevel@tonic-gate n2s(p,sil); 438*0Sstevel@tonic-gate n2s(p,cl); 439*0Sstevel@tonic-gate d=(unsigned char *)s->init_buf->data; 440*0Sstevel@tonic-gate if ((csl+sil+cl+11) != s->packet_length) 441*0Sstevel@tonic-gate { 442*0Sstevel@tonic-gate SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_LENGTH_MISMATCH); 443*0Sstevel@tonic-gate goto err; 444*0Sstevel@tonic-gate } 445*0Sstevel@tonic-gate 446*0Sstevel@tonic-gate /* record header: msg_type ... */ 447*0Sstevel@tonic-gate *(d++) = SSL3_MT_CLIENT_HELLO; 448*0Sstevel@tonic-gate /* ... and length (actual value will be written later) */ 449*0Sstevel@tonic-gate d_len = d; 450*0Sstevel@tonic-gate d += 3; 451*0Sstevel@tonic-gate 452*0Sstevel@tonic-gate /* client_version */ 453*0Sstevel@tonic-gate *(d++) = SSL3_VERSION_MAJOR; /* == v[0] */ 454*0Sstevel@tonic-gate *(d++) = v[1]; 455*0Sstevel@tonic-gate 456*0Sstevel@tonic-gate /* lets populate the random area */ 457*0Sstevel@tonic-gate /* get the challenge_length */ 458*0Sstevel@tonic-gate i=(cl > SSL3_RANDOM_SIZE)?SSL3_RANDOM_SIZE:cl; 459*0Sstevel@tonic-gate memset(d,0,SSL3_RANDOM_SIZE); 460*0Sstevel@tonic-gate memcpy(&(d[SSL3_RANDOM_SIZE-i]),&(p[csl+sil]),i); 461*0Sstevel@tonic-gate d+=SSL3_RANDOM_SIZE; 462*0Sstevel@tonic-gate 463*0Sstevel@tonic-gate /* no session-id reuse */ 464*0Sstevel@tonic-gate *(d++)=0; 465*0Sstevel@tonic-gate 466*0Sstevel@tonic-gate /* ciphers */ 467*0Sstevel@tonic-gate j=0; 468*0Sstevel@tonic-gate dd=d; 469*0Sstevel@tonic-gate d+=2; 470*0Sstevel@tonic-gate for (i=0; i<csl; i+=3) 471*0Sstevel@tonic-gate { 472*0Sstevel@tonic-gate if (p[i] != 0) continue; 473*0Sstevel@tonic-gate *(d++)=p[i+1]; 474*0Sstevel@tonic-gate *(d++)=p[i+2]; 475*0Sstevel@tonic-gate j+=2; 476*0Sstevel@tonic-gate } 477*0Sstevel@tonic-gate s2n(j,dd); 478*0Sstevel@tonic-gate 479*0Sstevel@tonic-gate /* COMPRESSION */ 480*0Sstevel@tonic-gate *(d++)=1; 481*0Sstevel@tonic-gate *(d++)=0; 482*0Sstevel@tonic-gate 483*0Sstevel@tonic-gate i = (d-(unsigned char *)s->init_buf->data) - 4; 484*0Sstevel@tonic-gate l2n3((long)i, d_len); 485*0Sstevel@tonic-gate 486*0Sstevel@tonic-gate /* get the data reused from the init_buf */ 487*0Sstevel@tonic-gate s->s3->tmp.reuse_message=1; 488*0Sstevel@tonic-gate s->s3->tmp.message_type=SSL3_MT_CLIENT_HELLO; 489*0Sstevel@tonic-gate s->s3->tmp.message_size=i; 490*0Sstevel@tonic-gate } 491*0Sstevel@tonic-gate 492*0Sstevel@tonic-gate /* imaginary new state (for program structure): */ 493*0Sstevel@tonic-gate /* s->state = SSL23_SR_CLNT_HELLO_C */ 494*0Sstevel@tonic-gate 495*0Sstevel@tonic-gate if (type == 1) 496*0Sstevel@tonic-gate { 497*0Sstevel@tonic-gate #ifdef OPENSSL_NO_SSL2 498*0Sstevel@tonic-gate SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNSUPPORTED_PROTOCOL); 499*0Sstevel@tonic-gate goto err; 500*0Sstevel@tonic-gate #else 501*0Sstevel@tonic-gate /* we are talking sslv2 */ 502*0Sstevel@tonic-gate /* we need to clean up the SSLv3/TLSv1 setup and put in the 503*0Sstevel@tonic-gate * sslv2 stuff. */ 504*0Sstevel@tonic-gate 505*0Sstevel@tonic-gate if (s->s2 == NULL) 506*0Sstevel@tonic-gate { 507*0Sstevel@tonic-gate if (!ssl2_new(s)) 508*0Sstevel@tonic-gate goto err; 509*0Sstevel@tonic-gate } 510*0Sstevel@tonic-gate else 511*0Sstevel@tonic-gate ssl2_clear(s); 512*0Sstevel@tonic-gate 513*0Sstevel@tonic-gate if (s->s3 != NULL) ssl3_free(s); 514*0Sstevel@tonic-gate 515*0Sstevel@tonic-gate if (!BUF_MEM_grow_clean(s->init_buf, 516*0Sstevel@tonic-gate SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)) 517*0Sstevel@tonic-gate { 518*0Sstevel@tonic-gate goto err; 519*0Sstevel@tonic-gate } 520*0Sstevel@tonic-gate 521*0Sstevel@tonic-gate s->state=SSL2_ST_GET_CLIENT_HELLO_A; 522*0Sstevel@tonic-gate if ((s->options & SSL_OP_MSIE_SSLV2_RSA_PADDING) || 523*0Sstevel@tonic-gate use_sslv2_strong || 524*0Sstevel@tonic-gate (s->options & SSL_OP_NO_TLSv1 && s->options & SSL_OP_NO_SSLv3)) 525*0Sstevel@tonic-gate s->s2->ssl2_rollback=0; 526*0Sstevel@tonic-gate else 527*0Sstevel@tonic-gate /* reject SSL 2.0 session if client supports SSL 3.0 or TLS 1.0 528*0Sstevel@tonic-gate * (SSL 3.0 draft/RFC 2246, App. E.2) */ 529*0Sstevel@tonic-gate s->s2->ssl2_rollback=1; 530*0Sstevel@tonic-gate 531*0Sstevel@tonic-gate /* setup the n bytes we have read so we get them from 532*0Sstevel@tonic-gate * the sslv2 buffer */ 533*0Sstevel@tonic-gate s->rstate=SSL_ST_READ_HEADER; 534*0Sstevel@tonic-gate s->packet_length=n; 535*0Sstevel@tonic-gate s->packet= &(s->s2->rbuf[0]); 536*0Sstevel@tonic-gate memcpy(s->packet,buf,n); 537*0Sstevel@tonic-gate s->s2->rbuf_left=n; 538*0Sstevel@tonic-gate s->s2->rbuf_offs=0; 539*0Sstevel@tonic-gate 540*0Sstevel@tonic-gate s->method=SSLv2_server_method(); 541*0Sstevel@tonic-gate s->handshake_func=s->method->ssl_accept; 542*0Sstevel@tonic-gate #endif 543*0Sstevel@tonic-gate } 544*0Sstevel@tonic-gate 545*0Sstevel@tonic-gate if ((type == 2) || (type == 3)) 546*0Sstevel@tonic-gate { 547*0Sstevel@tonic-gate /* we have SSLv3/TLSv1 (type 2: SSL2 style, type 3: SSL3/TLS style) */ 548*0Sstevel@tonic-gate 549*0Sstevel@tonic-gate if (!ssl_init_wbio_buffer(s,1)) goto err; 550*0Sstevel@tonic-gate 551*0Sstevel@tonic-gate /* we are in this state */ 552*0Sstevel@tonic-gate s->state=SSL3_ST_SR_CLNT_HELLO_A; 553*0Sstevel@tonic-gate 554*0Sstevel@tonic-gate if (type == 3) 555*0Sstevel@tonic-gate { 556*0Sstevel@tonic-gate /* put the 'n' bytes we have read into the input buffer 557*0Sstevel@tonic-gate * for SSLv3 */ 558*0Sstevel@tonic-gate s->rstate=SSL_ST_READ_HEADER; 559*0Sstevel@tonic-gate s->packet_length=n; 560*0Sstevel@tonic-gate s->packet= &(s->s3->rbuf.buf[0]); 561*0Sstevel@tonic-gate memcpy(s->packet,buf,n); 562*0Sstevel@tonic-gate s->s3->rbuf.left=n; 563*0Sstevel@tonic-gate s->s3->rbuf.offset=0; 564*0Sstevel@tonic-gate } 565*0Sstevel@tonic-gate else 566*0Sstevel@tonic-gate { 567*0Sstevel@tonic-gate s->packet_length=0; 568*0Sstevel@tonic-gate s->s3->rbuf.left=0; 569*0Sstevel@tonic-gate s->s3->rbuf.offset=0; 570*0Sstevel@tonic-gate } 571*0Sstevel@tonic-gate 572*0Sstevel@tonic-gate if (s->version == TLS1_VERSION) 573*0Sstevel@tonic-gate s->method = TLSv1_server_method(); 574*0Sstevel@tonic-gate else 575*0Sstevel@tonic-gate s->method = SSLv3_server_method(); 576*0Sstevel@tonic-gate #if 0 /* ssl3_get_client_hello does this */ 577*0Sstevel@tonic-gate s->client_version=(v[0]<<8)|v[1]; 578*0Sstevel@tonic-gate #endif 579*0Sstevel@tonic-gate s->handshake_func=s->method->ssl_accept; 580*0Sstevel@tonic-gate } 581*0Sstevel@tonic-gate 582*0Sstevel@tonic-gate if ((type < 1) || (type > 3)) 583*0Sstevel@tonic-gate { 584*0Sstevel@tonic-gate /* bad, very bad */ 585*0Sstevel@tonic-gate SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNKNOWN_PROTOCOL); 586*0Sstevel@tonic-gate goto err; 587*0Sstevel@tonic-gate } 588*0Sstevel@tonic-gate s->init_num=0; 589*0Sstevel@tonic-gate 590*0Sstevel@tonic-gate if (buf != buf_space) OPENSSL_free(buf); 591*0Sstevel@tonic-gate s->first_packet=1; 592*0Sstevel@tonic-gate return(SSL_accept(s)); 593*0Sstevel@tonic-gate err: 594*0Sstevel@tonic-gate if (buf != buf_space) OPENSSL_free(buf); 595*0Sstevel@tonic-gate return(-1); 596*0Sstevel@tonic-gate } 597