s23_srvr.c revision 273149
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/* ==================================================================== 59238405Sjkim * Copyright (c) 1998-2006 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> 118238405Sjkim#ifdef OPENSSL_FIPS 119238405Sjkim#include <openssl/fips.h> 120238405Sjkim#endif 12155714Skris 122238405Sjkimstatic const SSL_METHOD *ssl23_get_server_method(int ver); 12355714Skrisint ssl23_get_client_hello(SSL *s); 124238405Sjkimstatic const SSL_METHOD *ssl23_get_server_method(int ver) 12555714Skris { 126110007Smarkm#ifndef OPENSSL_NO_SSL2 12755714Skris if (ver == SSL2_VERSION) 12855714Skris return(SSLv2_server_method()); 12955949Skris#endif 130273149Sjkim#ifndef OPENSSL_NO_SSL3 13155714Skris if (ver == SSL3_VERSION) 13255714Skris return(SSLv3_server_method()); 133273149Sjkim#endif 134273149Sjkim if (ver == TLS1_VERSION) 13555714Skris return(TLSv1_server_method()); 136238405Sjkim else if (ver == TLS1_1_VERSION) 137238405Sjkim return(TLSv1_1_server_method()); 138238405Sjkim else if (ver == TLS1_2_VERSION) 139238405Sjkim return(TLSv1_2_server_method()); 14055714Skris else 14155714Skris return(NULL); 14255714Skris } 14355714Skris 144160817SsimonIMPLEMENT_ssl23_meth_func(SSLv23_server_method, 145160817Ssimon ssl23_accept, 146160817Ssimon ssl_undefined_function, 147160817Ssimon ssl23_get_server_method) 14855714Skris 14955714Skrisint ssl23_accept(SSL *s) 15055714Skris { 15155714Skris BUF_MEM *buf; 152160817Ssimon unsigned long Time=(unsigned long)time(NULL); 153110007Smarkm void (*cb)(const SSL *ssl,int type,int val)=NULL; 15455714Skris int ret= -1; 15555714Skris int new_state,state; 15655714Skris 15759194Skris RAND_add(&Time,sizeof(Time),0); 15855714Skris ERR_clear_error(); 15955714Skris clear_sys_error(); 16055714Skris 16155714Skris if (s->info_callback != NULL) 16255714Skris cb=s->info_callback; 16355714Skris else if (s->ctx->info_callback != NULL) 16455714Skris cb=s->ctx->info_callback; 16555714Skris 16689840Skris s->in_handshake++; 16755714Skris if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 16855714Skris 16955714Skris for (;;) 17055714Skris { 17155714Skris state=s->state; 17255714Skris 17355714Skris switch(s->state) 17455714Skris { 17555714Skris case SSL_ST_BEFORE: 17655714Skris case SSL_ST_ACCEPT: 17755714Skris case SSL_ST_BEFORE|SSL_ST_ACCEPT: 17855714Skris case SSL_ST_OK|SSL_ST_ACCEPT: 17955714Skris 18055714Skris s->server=1; 18155714Skris if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); 18255714Skris 18355714Skris /* s->version=SSL3_VERSION; */ 18455714Skris s->type=SSL_ST_ACCEPT; 18555714Skris 18655714Skris if (s->init_buf == NULL) 18755714Skris { 18855714Skris if ((buf=BUF_MEM_new()) == NULL) 18955714Skris { 19055714Skris ret= -1; 19155714Skris goto end; 19255714Skris } 19355714Skris if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) 19455714Skris { 19555714Skris ret= -1; 19655714Skris goto end; 19755714Skris } 19855714Skris s->init_buf=buf; 19955714Skris } 20055714Skris 20155714Skris ssl3_init_finished_mac(s); 20255714Skris 20355714Skris s->state=SSL23_ST_SR_CLNT_HELLO_A; 20455714Skris s->ctx->stats.sess_accept++; 20555714Skris s->init_num=0; 20655714Skris break; 20755714Skris 20855714Skris case SSL23_ST_SR_CLNT_HELLO_A: 20955714Skris case SSL23_ST_SR_CLNT_HELLO_B: 21055714Skris 21155714Skris s->shutdown=0; 21255714Skris ret=ssl23_get_client_hello(s); 21355714Skris if (ret >= 0) cb=NULL; 21455714Skris goto end; 21555714Skris /* break; */ 21655714Skris 21755714Skris default: 21855714Skris SSLerr(SSL_F_SSL23_ACCEPT,SSL_R_UNKNOWN_STATE); 21955714Skris ret= -1; 22055714Skris goto end; 22155714Skris /* break; */ 22255714Skris } 22355714Skris 22455714Skris if ((cb != NULL) && (s->state != state)) 22555714Skris { 22655714Skris new_state=s->state; 22755714Skris s->state=state; 22855714Skris cb(s,SSL_CB_ACCEPT_LOOP,1); 22955714Skris s->state=new_state; 23055714Skris } 23155714Skris } 23255714Skrisend: 23389840Skris s->in_handshake--; 23455714Skris if (cb != NULL) 23555714Skris cb(s,SSL_CB_ACCEPT_EXIT,ret); 23655714Skris return(ret); 23755714Skris } 23855714Skris 23955714Skris 24055714Skrisint ssl23_get_client_hello(SSL *s) 24155714Skris { 24259194Skris char buf_space[11]; /* Request this many bytes in initial read. 24359194Skris * We can detect SSL 3.0/TLS 1.0 Client Hellos 24459194Skris * ('type == 3') correctly only when the following 24559194Skris * is in a single record, which is not guaranteed by 24659194Skris * the protocol specification: 24759194Skris * Byte Content 24859194Skris * 0 type \ 24959194Skris * 1/2 version > record header 25059194Skris * 3/4 length / 25159194Skris * 5 msg_type \ 25259194Skris * 6-8 length > Client Hello message 25359194Skris * 9/10 client_version / 25459194Skris */ 25555714Skris char *buf= &(buf_space[0]); 256110007Smarkm unsigned char *p,*d,*d_len,*dd; 25755714Skris unsigned int i; 25855714Skris unsigned int csl,sil,cl; 25959194Skris int n=0,j; 26059194Skris int type=0; 26155714Skris int v[2]; 26255714Skris 26355714Skris if (s->state == SSL23_ST_SR_CLNT_HELLO_A) 26455714Skris { 26559194Skris /* read the initial header */ 26659194Skris v[0]=v[1]=0; 26759194Skris 26855714Skris if (!ssl3_setup_buffers(s)) goto err; 26955714Skris 27059194Skris n=ssl23_read_bytes(s, sizeof buf_space); 27159194Skris if (n != sizeof buf_space) return(n); /* n == -1 || n == 0 */ 27255714Skris 27355714Skris p=s->packet; 27455714Skris 27555714Skris memcpy(buf,p,n); 27655714Skris 27755714Skris if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO)) 27855714Skris { 27959194Skris /* 28059194Skris * SSLv2 header 28159194Skris */ 28255714Skris if ((p[3] == 0x00) && (p[4] == 0x02)) 28355714Skris { 28455714Skris v[0]=p[3]; v[1]=p[4]; 28555714Skris /* SSLv2 */ 28655714Skris if (!(s->options & SSL_OP_NO_SSLv2)) 28755714Skris type=1; 28855714Skris } 28955714Skris else if (p[3] == SSL3_VERSION_MAJOR) 29055714Skris { 29155714Skris v[0]=p[3]; v[1]=p[4]; 29255714Skris /* SSLv3/TLSv1 */ 29355714Skris if (p[4] >= TLS1_VERSION_MINOR) 29455714Skris { 295238405Sjkim if (p[4] >= TLS1_2_VERSION_MINOR && 296238405Sjkim !(s->options & SSL_OP_NO_TLSv1_2)) 29755714Skris { 298238405Sjkim s->version=TLS1_2_VERSION; 299238405Sjkim s->state=SSL23_ST_SR_CLNT_HELLO_B; 300238405Sjkim } 301238405Sjkim else if (p[4] >= TLS1_1_VERSION_MINOR && 302238405Sjkim !(s->options & SSL_OP_NO_TLSv1_1)) 303238405Sjkim { 304238405Sjkim s->version=TLS1_1_VERSION; 305238405Sjkim /* type=2; */ /* done later to survive restarts */ 306238405Sjkim s->state=SSL23_ST_SR_CLNT_HELLO_B; 307238405Sjkim } 308238405Sjkim else if (!(s->options & SSL_OP_NO_TLSv1)) 309238405Sjkim { 31059194Skris s->version=TLS1_VERSION; 31159194Skris /* type=2; */ /* done later to survive restarts */ 31255714Skris s->state=SSL23_ST_SR_CLNT_HELLO_B; 31355714Skris } 31455714Skris else if (!(s->options & SSL_OP_NO_SSLv3)) 31555714Skris { 31659194Skris s->version=SSL3_VERSION; 31759194Skris /* type=2; */ 31855714Skris s->state=SSL23_ST_SR_CLNT_HELLO_B; 31955714Skris } 32055714Skris else if (!(s->options & SSL_OP_NO_SSLv2)) 32155714Skris { 32255714Skris type=1; 32355714Skris } 32455714Skris } 32555714Skris else if (!(s->options & SSL_OP_NO_SSLv3)) 32659194Skris { 32759194Skris s->version=SSL3_VERSION; 32859194Skris /* type=2; */ 32955714Skris s->state=SSL23_ST_SR_CLNT_HELLO_B; 33059194Skris } 33155714Skris else if (!(s->options & SSL_OP_NO_SSLv2)) 33255714Skris type=1; 33355714Skris 33455714Skris } 33555714Skris } 33655714Skris else if ((p[0] == SSL3_RT_HANDSHAKE) && 33755714Skris (p[1] == SSL3_VERSION_MAJOR) && 33859194Skris (p[5] == SSL3_MT_CLIENT_HELLO) && 33959194Skris ((p[3] == 0 && p[4] < 5 /* silly record length? */) 340205128Ssimon || (p[9] >= p[1]))) 34155714Skris { 34259194Skris /* 34359194Skris * SSLv3 or tls1 header 34459194Skris */ 34559194Skris 34668654Skris v[0]=p[1]; /* major version (= SSL3_VERSION_MAJOR) */ 34759194Skris /* We must look at client_version inside the Client Hello message 34868654Skris * to get the correct minor version. 34968654Skris * However if we have only a pathologically small fragment of the 35089840Skris * Client Hello message, this would be difficult, and we'd have 35189840Skris * to read more records to find out. 35289840Skris * No known SSL 3.0 client fragments ClientHello like this, 353269686Sjkim * so we simply reject such connections to avoid 354269686Sjkim * protocol version downgrade attacks. */ 35559194Skris if (p[3] == 0 && p[4] < 6) 35668654Skris { 35768654Skris SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_SMALL); 35868654Skris goto err; 35968654Skris } 360205128Ssimon /* if major version number > 3 set minor to a value 361205128Ssimon * which will use the highest version 3 we support. 362205128Ssimon * If TLS 2.0 ever appears we will need to revise 363205128Ssimon * this.... 364205128Ssimon */ 365269686Sjkim if (p[9] > SSL3_VERSION_MAJOR) 366205128Ssimon v[1]=0xff; 36789840Skris else 36889840Skris v[1]=p[10]; /* minor version according to client_version */ 36959194Skris if (v[1] >= TLS1_VERSION_MINOR) 37055714Skris { 371238405Sjkim if (v[1] >= TLS1_2_VERSION_MINOR && 372238405Sjkim !(s->options & SSL_OP_NO_TLSv1_2)) 37355714Skris { 374238405Sjkim s->version=TLS1_2_VERSION; 375238405Sjkim type=3; 376238405Sjkim } 377238405Sjkim else if (v[1] >= TLS1_1_VERSION_MINOR && 378238405Sjkim !(s->options & SSL_OP_NO_TLSv1_1)) 379238405Sjkim { 380238405Sjkim s->version=TLS1_1_VERSION; 381238405Sjkim type=3; 382238405Sjkim } 383238405Sjkim else if (!(s->options & SSL_OP_NO_TLSv1)) 384238405Sjkim { 38559194Skris s->version=TLS1_VERSION; 38655714Skris type=3; 38755714Skris } 38855714Skris else if (!(s->options & SSL_OP_NO_SSLv3)) 38959194Skris { 39059194Skris s->version=SSL3_VERSION; 39155714Skris type=3; 39259194Skris } 39355714Skris } 39489840Skris else 39559194Skris { 39689840Skris /* client requests SSL 3.0 */ 39789840Skris if (!(s->options & SSL_OP_NO_SSLv3)) 39889840Skris { 39989840Skris s->version=SSL3_VERSION; 40089840Skris type=3; 40189840Skris } 40289840Skris else if (!(s->options & SSL_OP_NO_TLSv1)) 40389840Skris { 40489840Skris /* we won't be able to use TLS of course, 40589840Skris * but this will send an appropriate alert */ 40689840Skris s->version=TLS1_VERSION; 40789840Skris type=3; 40889840Skris } 40959194Skris } 41055714Skris } 41155714Skris else if ((strncmp("GET ", (char *)p,4) == 0) || 41255714Skris (strncmp("POST ",(char *)p,5) == 0) || 41355714Skris (strncmp("HEAD ",(char *)p,5) == 0) || 41455714Skris (strncmp("PUT ", (char *)p,4) == 0)) 41555714Skris { 41655714Skris SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTP_REQUEST); 41755714Skris goto err; 41855714Skris } 41955714Skris else if (strncmp("CONNECT",(char *)p,7) == 0) 42055714Skris { 42155714Skris SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTPS_PROXY_REQUEST); 42255714Skris goto err; 42355714Skris } 42455714Skris } 42555714Skris 426273149Sjkim /* ensure that TLS_MAX_VERSION is up-to-date */ 427273149Sjkim OPENSSL_assert(s->version <= TLS_MAX_VERSION); 428273149Sjkim 429194206Ssimon#ifdef OPENSSL_FIPS 430194206Ssimon if (FIPS_mode() && (s->version < TLS1_VERSION)) 431194206Ssimon { 432194206Ssimon SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, 433194206Ssimon SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE); 434194206Ssimon goto err; 435194206Ssimon } 436194206Ssimon#endif 437194206Ssimon 43855714Skris if (s->state == SSL23_ST_SR_CLNT_HELLO_B) 43955714Skris { 44059194Skris /* we have SSLv3/TLSv1 in an SSLv2 header 44159194Skris * (other cases skip this state) */ 44259194Skris 44355714Skris type=2; 44455714Skris p=s->packet; 44559194Skris v[0] = p[3]; /* == SSL3_VERSION_MAJOR */ 44659194Skris v[1] = p[4]; 44759194Skris 448269686Sjkim /* An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2 449269686Sjkim * header is sent directly on the wire, not wrapped as a TLS 450269686Sjkim * record. It's format is: 451269686Sjkim * Byte Content 452269686Sjkim * 0-1 msg_length 453269686Sjkim * 2 msg_type 454269686Sjkim * 3-4 version 455269686Sjkim * 5-6 cipher_spec_length 456269686Sjkim * 7-8 session_id_length 457269686Sjkim * 9-10 challenge_length 458269686Sjkim * ... ... 459269686Sjkim */ 46055714Skris n=((p[0]&0x7f)<<8)|p[1]; 46155714Skris if (n > (1024*4)) 46255714Skris { 46355714Skris SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE); 46455714Skris goto err; 46555714Skris } 466269686Sjkim if (n < 9) 467269686Sjkim { 468269686Sjkim SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_LENGTH_MISMATCH); 469269686Sjkim goto err; 470269686Sjkim } 47155714Skris 47255714Skris j=ssl23_read_bytes(s,n+2); 473269686Sjkim /* We previously read 11 bytes, so if j > 0, we must have 474269686Sjkim * j == n+2 == s->packet_length. We have at least 11 valid 475269686Sjkim * packet bytes. */ 47655714Skris if (j <= 0) return(j); 47755714Skris 478110007Smarkm ssl3_finish_mac(s, s->packet+2, s->packet_length-2); 479110007Smarkm if (s->msg_callback) 480110007Smarkm s->msg_callback(0, SSL2_VERSION, 0, s->packet+2, s->packet_length-2, s, s->msg_callback_arg); /* CLIENT-HELLO */ 48155714Skris 48255714Skris p=s->packet; 48355714Skris p+=5; 48455714Skris n2s(p,csl); 48555714Skris n2s(p,sil); 48655714Skris n2s(p,cl); 48755714Skris d=(unsigned char *)s->init_buf->data; 488238405Sjkim if ((csl+sil+cl+11) != s->packet_length) /* We can't have TLS extensions in SSL 2.0 format 489238405Sjkim * Client Hello, can we? Error condition should be 490238405Sjkim * '>' otherweise */ 49155714Skris { 49255714Skris SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_LENGTH_MISMATCH); 49355714Skris goto err; 49455714Skris } 49555714Skris 496110007Smarkm /* record header: msg_type ... */ 497110007Smarkm *(d++) = SSL3_MT_CLIENT_HELLO; 498110007Smarkm /* ... and length (actual value will be written later) */ 499110007Smarkm d_len = d; 500110007Smarkm d += 3; 501110007Smarkm 502110007Smarkm /* client_version */ 50359194Skris *(d++) = SSL3_VERSION_MAJOR; /* == v[0] */ 50459194Skris *(d++) = v[1]; 50555714Skris 50655714Skris /* lets populate the random area */ 50759194Skris /* get the challenge_length */ 50855714Skris i=(cl > SSL3_RANDOM_SIZE)?SSL3_RANDOM_SIZE:cl; 50955714Skris memset(d,0,SSL3_RANDOM_SIZE); 51055714Skris memcpy(&(d[SSL3_RANDOM_SIZE-i]),&(p[csl+sil]),i); 51155714Skris d+=SSL3_RANDOM_SIZE; 51255714Skris 51355714Skris /* no session-id reuse */ 51455714Skris *(d++)=0; 51555714Skris 51655714Skris /* ciphers */ 51755714Skris j=0; 51855714Skris dd=d; 51955714Skris d+=2; 52055714Skris for (i=0; i<csl; i+=3) 52155714Skris { 52255714Skris if (p[i] != 0) continue; 52355714Skris *(d++)=p[i+1]; 52455714Skris *(d++)=p[i+2]; 52555714Skris j+=2; 52655714Skris } 52755714Skris s2n(j,dd); 52855714Skris 52955714Skris /* COMPRESSION */ 53055714Skris *(d++)=1; 53155714Skris *(d++)=0; 53255714Skris 533238405Sjkim#if 0 534238405Sjkim /* copy any remaining data with may be extensions */ 535238405Sjkim p = p+csl+sil+cl; 536238405Sjkim while (p < s->packet+s->packet_length) 537238405Sjkim { 538238405Sjkim *(d++)=*(p++); 539238405Sjkim } 540238405Sjkim#endif 541238405Sjkim 542110007Smarkm i = (d-(unsigned char *)s->init_buf->data) - 4; 543110007Smarkm l2n3((long)i, d_len); 54455714Skris 54555714Skris /* get the data reused from the init_buf */ 54655714Skris s->s3->tmp.reuse_message=1; 54755714Skris s->s3->tmp.message_type=SSL3_MT_CLIENT_HELLO; 54855714Skris s->s3->tmp.message_size=i; 54955714Skris } 55055714Skris 55159194Skris /* imaginary new state (for program structure): */ 55259194Skris /* s->state = SSL23_SR_CLNT_HELLO_C */ 55359194Skris 55455714Skris if (type == 1) 55555714Skris { 556110007Smarkm#ifdef OPENSSL_NO_SSL2 55759194Skris SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNSUPPORTED_PROTOCOL); 55859194Skris goto err; 55955949Skris#else 56055714Skris /* we are talking sslv2 */ 56155714Skris /* we need to clean up the SSLv3/TLSv1 setup and put in the 56255714Skris * sslv2 stuff. */ 56355714Skris 56455714Skris if (s->s2 == NULL) 56555714Skris { 56655714Skris if (!ssl2_new(s)) 56755714Skris goto err; 56855714Skris } 56955714Skris else 57055714Skris ssl2_clear(s); 57155714Skris 57255714Skris if (s->s3 != NULL) ssl3_free(s); 57355714Skris 574110007Smarkm if (!BUF_MEM_grow_clean(s->init_buf, 57555714Skris SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)) 57655714Skris { 57755714Skris goto err; 57855714Skris } 57955714Skris 58055714Skris s->state=SSL2_ST_GET_CLIENT_HELLO_A; 581151233Scperciva if (s->options & SSL_OP_NO_TLSv1 && s->options & SSL_OP_NO_SSLv3) 58255714Skris s->s2->ssl2_rollback=0; 58355714Skris else 58468654Skris /* reject SSL 2.0 session if client supports SSL 3.0 or TLS 1.0 58568654Skris * (SSL 3.0 draft/RFC 2246, App. E.2) */ 58655714Skris s->s2->ssl2_rollback=1; 58755714Skris 58859194Skris /* setup the n bytes we have read so we get them from 58955714Skris * the sslv2 buffer */ 59055714Skris s->rstate=SSL_ST_READ_HEADER; 59155714Skris s->packet_length=n; 59255714Skris s->packet= &(s->s2->rbuf[0]); 59355714Skris memcpy(s->packet,buf,n); 59455714Skris s->s2->rbuf_left=n; 59555714Skris s->s2->rbuf_offs=0; 59655714Skris 59755714Skris s->method=SSLv2_server_method(); 59855714Skris s->handshake_func=s->method->ssl_accept; 59955949Skris#endif 60055714Skris } 60155714Skris 60255714Skris if ((type == 2) || (type == 3)) 60355714Skris { 60459194Skris /* we have SSLv3/TLSv1 (type 2: SSL2 style, type 3: SSL3/TLS style) */ 605273149Sjkim s->method = ssl23_get_server_method(s->version); 606273149Sjkim if (s->method == NULL) 607273149Sjkim { 608273149Sjkim SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNSUPPORTED_PROTOCOL); 609273149Sjkim goto err; 610273149Sjkim } 61155714Skris 61255714Skris if (!ssl_init_wbio_buffer(s,1)) goto err; 61355714Skris 61455714Skris /* we are in this state */ 61555714Skris s->state=SSL3_ST_SR_CLNT_HELLO_A; 61655714Skris 61755714Skris if (type == 3) 61855714Skris { 61955714Skris /* put the 'n' bytes we have read into the input buffer 62055714Skris * for SSLv3 */ 62155714Skris s->rstate=SSL_ST_READ_HEADER; 62255714Skris s->packet_length=n; 623238405Sjkim if (s->s3->rbuf.buf == NULL) 624238405Sjkim if (!ssl3_setup_read_buffer(s)) 625238405Sjkim goto err; 626238405Sjkim 62755714Skris s->packet= &(s->s3->rbuf.buf[0]); 62855714Skris memcpy(s->packet,buf,n); 62955714Skris s->s3->rbuf.left=n; 63055714Skris s->s3->rbuf.offset=0; 63155714Skris } 63255714Skris else 63355714Skris { 63455714Skris s->packet_length=0; 63555714Skris s->s3->rbuf.left=0; 63655714Skris s->s3->rbuf.offset=0; 63755714Skris } 63859194Skris#if 0 /* ssl3_get_client_hello does this */ 63955714Skris s->client_version=(v[0]<<8)|v[1]; 64059194Skris#endif 64155714Skris s->handshake_func=s->method->ssl_accept; 64255714Skris } 64355714Skris 64455714Skris if ((type < 1) || (type > 3)) 64555714Skris { 64655714Skris /* bad, very bad */ 64755714Skris SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNKNOWN_PROTOCOL); 64855714Skris goto err; 64955714Skris } 65055714Skris s->init_num=0; 65155714Skris 65268654Skris if (buf != buf_space) OPENSSL_free(buf); 65355714Skris return(SSL_accept(s)); 65455714Skriserr: 65568654Skris if (buf != buf_space) OPENSSL_free(buf); 65655714Skris return(-1); 65755714Skris } 658