s23_srvr.c revision 55714
11558Srgrimes/* ssl/s23_srvr.c */ 21558Srgrimes/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 31558Srgrimes * All rights reserved. 41558Srgrimes * 51558Srgrimes * This package is an SSL implementation written 61558Srgrimes * by Eric Young (eay@cryptsoft.com). 71558Srgrimes * The implementation was written so as to conform with Netscapes SSL. 81558Srgrimes * 91558Srgrimes * This library is free for commercial and non-commercial use as long as 101558Srgrimes * the following conditions are aheared to. The following conditions 111558Srgrimes * apply to all code found in this distribution, be it the RC4, RSA, 121558Srgrimes * lhash, DES, etc., code; not just the SSL code. The SSL documentation 131558Srgrimes * included with this distribution is covered by the same copyright terms 141558Srgrimes * except that the holder is Tim Hudson (tjh@cryptsoft.com). 151558Srgrimes * 161558Srgrimes * Copyright remains Eric Young's, and as such any Copyright notices in 171558Srgrimes * the code are not to be removed. 181558Srgrimes * If this package is used in a product, Eric Young should be given attribution 191558Srgrimes * as the author of the parts of the library used. 201558Srgrimes * This can be in the form of a textual message at program startup or 211558Srgrimes * in documentation (online or textual) provided with the package. 221558Srgrimes * 231558Srgrimes * Redistribution and use in source and binary forms, with or without 241558Srgrimes * modification, are permitted provided that the following conditions 251558Srgrimes * are met: 261558Srgrimes * 1. Redistributions of source code must retain the copyright 271558Srgrimes * notice, this list of conditions and the following disclaimer. 281558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 291558Srgrimes * notice, this list of conditions and the following disclaimer in the 301558Srgrimes * documentation and/or other materials provided with the distribution. 3137906Scharnier * 3. All advertising materials mentioning features or use of this software 3223685Speter * must display the following acknowledgement: 3337906Scharnier * "This product includes cryptographic software written by 3437906Scharnier * Eric Young (eay@cryptsoft.com)" 3550476Speter * The word 'cryptographic' can be left out if the rouines from the library 361558Srgrimes * being used are not cryptographic related :-). 371558Srgrimes * 4. If you include any Windows specific code (or a derivative thereof) from 381558Srgrimes * the apps directory (application code) you must include an acknowledgement: 391558Srgrimes * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 401558Srgrimes * 411558Srgrimes * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 421558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 431558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 441558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45103949Smike * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 461558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4778732Sdd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 481558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 491558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 501558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 511558Srgrimes * SUCH DAMAGE. 521558Srgrimes * 531558Srgrimes * The licence and distribution terms for any publically available version or 541558Srgrimes * derivative of this code cannot be changed. i.e. this code cannot simply be 551558Srgrimes * copied and put under another distribution licence 561558Srgrimes * [including the GNU Public Licence.] 571558Srgrimes */ 5892837Simp 591558Srgrimes#include <stdio.h> 6092806Sobrien#include <openssl/buffer.h> 611558Srgrimes#include <openssl/rand.h> 621558Srgrimes#include <openssl/objects.h> 631558Srgrimes#include <openssl/evp.h> 6423685Speter#include "ssl_locl.h" 651558Srgrimes 661558Srgrimesstatic SSL_METHOD *ssl23_get_server_method(int ver); 671558Srgrimesint ssl23_get_client_hello(SSL *s); 681558Srgrimesstatic SSL_METHOD *ssl23_get_server_method(int ver) 691558Srgrimes { 701558Srgrimes if (ver == SSL2_VERSION) 711558Srgrimes return(SSLv2_server_method()); 721558Srgrimes if (ver == SSL3_VERSION) 731558Srgrimes return(SSLv3_server_method()); 741558Srgrimes else if (ver == TLS1_VERSION) 751558Srgrimes return(TLSv1_server_method()); 761558Srgrimes else 771558Srgrimes return(NULL); 781558Srgrimes } 791558Srgrimes 801558SrgrimesSSL_METHOD *SSLv23_server_method(void) 811558Srgrimes { 821558Srgrimes static int init=1; 831558Srgrimes static SSL_METHOD SSLv23_server_data; 841558Srgrimes 851558Srgrimes if (init) 8692837Simp { 871558Srgrimes memcpy((char *)&SSLv23_server_data, 881558Srgrimes (char *)sslv23_base_method(),sizeof(SSL_METHOD)); 891558Srgrimes SSLv23_server_data.ssl_accept=ssl23_accept; 901558Srgrimes SSLv23_server_data.get_ssl_method=ssl23_get_server_method; 911558Srgrimes init=0; 921558Srgrimes } 931558Srgrimes return(&SSLv23_server_data); 941558Srgrimes } 951558Srgrimes 961558Srgrimesint ssl23_accept(SSL *s) 971558Srgrimes { 981558Srgrimes BUF_MEM *buf; 991558Srgrimes unsigned long Time=time(NULL); 1001558Srgrimes void (*cb)()=NULL; 1011558Srgrimes int ret= -1; 1021558Srgrimes int new_state,state; 1031558Srgrimes 10492837Simp RAND_seed(&Time,sizeof(Time)); 1051558Srgrimes ERR_clear_error(); 1061558Srgrimes clear_sys_error(); 1071558Srgrimes 1081558Srgrimes if (s->info_callback != NULL) 1091558Srgrimes cb=s->info_callback; 1101558Srgrimes else if (s->ctx->info_callback != NULL) 1111558Srgrimes cb=s->ctx->info_callback; 1121558Srgrimes 1131558Srgrimes if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 1141558Srgrimes s->in_handshake++; 11537240Sbde 1161558Srgrimes for (;;) 1171558Srgrimes { 1181558Srgrimes state=s->state; 1191558Srgrimes 1201558Srgrimes switch(s->state) 1211558Srgrimes { 1221558Srgrimes case SSL_ST_BEFORE: 12392837Simp case SSL_ST_ACCEPT: 1241558Srgrimes case SSL_ST_BEFORE|SSL_ST_ACCEPT: 1251558Srgrimes case SSL_ST_OK|SSL_ST_ACCEPT: 1261558Srgrimes 1271558Srgrimes s->server=1; 1281558Srgrimes if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); 1291558Srgrimes 1301558Srgrimes /* s->version=SSL3_VERSION; */ 1311558Srgrimes s->type=SSL_ST_ACCEPT; 1321558Srgrimes 1331558Srgrimes if (s->init_buf == NULL) 1341558Srgrimes { 1351558Srgrimes if ((buf=BUF_MEM_new()) == NULL) 1361558Srgrimes { 13792837Simp ret= -1; 1381558Srgrimes goto end; 1391558Srgrimes } 1401558Srgrimes if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) 1411558Srgrimes { 1421558Srgrimes ret= -1; 1431558Srgrimes goto end; 14435852Sjkh } 1451558Srgrimes s->init_buf=buf; 1461558Srgrimes } 1471558Srgrimes 1481558Srgrimes ssl3_init_finished_mac(s); 1491558Srgrimes 1501558Srgrimes s->state=SSL23_ST_SR_CLNT_HELLO_A; 1511558Srgrimes s->ctx->stats.sess_accept++; 1521558Srgrimes s->init_num=0; 1531558Srgrimes break; 1541558Srgrimes 1551558Srgrimes case SSL23_ST_SR_CLNT_HELLO_A: 15692837Simp case SSL23_ST_SR_CLNT_HELLO_B: 1571558Srgrimes 1581558Srgrimes s->shutdown=0; 1591558Srgrimes ret=ssl23_get_client_hello(s); 1601558Srgrimes if (ret >= 0) cb=NULL; 1611558Srgrimes goto end; 1621558Srgrimes /* break; */ 1631558Srgrimes 1641558Srgrimes default: 1651558Srgrimes SSLerr(SSL_F_SSL23_ACCEPT,SSL_R_UNKNOWN_STATE); 1661558Srgrimes ret= -1; 1671558Srgrimes goto end; 1681558Srgrimes /* break; */ 1691558Srgrimes } 1701558Srgrimes 1711558Srgrimes if ((cb != NULL) && (s->state != state)) 1721558Srgrimes { 1731558Srgrimes new_state=s->state; 1741558Srgrimes s->state=state; 1751558Srgrimes cb(s,SSL_CB_ACCEPT_LOOP,1); 1761558Srgrimes s->state=new_state; 1771558Srgrimes } 17892837Simp } 1791558Srgrimesend: 1801558Srgrimes if (cb != NULL) 1811558Srgrimes cb(s,SSL_CB_ACCEPT_EXIT,ret); 1821558Srgrimes s->in_handshake--; 1831558Srgrimes return(ret); 1841558Srgrimes } 1851558Srgrimes 1861558Srgrimes 1871558Srgrimesint ssl23_get_client_hello(SSL *s) 1881558Srgrimes { 1891558Srgrimes char buf_space[8]; 1901558Srgrimes char *buf= &(buf_space[0]); 1911558Srgrimes unsigned char *p,*d,*dd; 1921558Srgrimes unsigned int i; 1931558Srgrimes unsigned int csl,sil,cl; 1941558Srgrimes int n=0,j,tls1=0; 1951558Srgrimes int type=0,use_sslv2_strong=0; 1961558Srgrimes int v[2]; 1971558Srgrimes 19892837Simp /* read the initial header */ 1991558Srgrimes v[0]=v[1]=0; 2001558Srgrimes if (s->state == SSL23_ST_SR_CLNT_HELLO_A) 20135852Sjkh { 20235852Sjkh if (!ssl3_setup_buffers(s)) goto err; 20335852Sjkh 20435852Sjkh n=ssl23_read_bytes(s,7); 2051558Srgrimes if (n != 7) return(n); /* n == -1 || n == 0 */ 2061558Srgrimes 2071558Srgrimes p=s->packet; 2081558Srgrimes 2091558Srgrimes memcpy(buf,p,n); 2101558Srgrimes 2111558Srgrimes if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO)) 2121558Srgrimes { 21337523Sjdp /* SSLv2 header */ 21437523Sjdp if ((p[3] == 0x00) && (p[4] == 0x02)) 21537523Sjdp { 21637523Sjdp v[0]=p[3]; v[1]=p[4]; 21737523Sjdp /* SSLv2 */ 21837523Sjdp if (!(s->options & SSL_OP_NO_SSLv2)) 21937523Sjdp type=1; 22037523Sjdp } 22137523Sjdp else if (p[3] == SSL3_VERSION_MAJOR) 22237523Sjdp { 22337523Sjdp v[0]=p[3]; v[1]=p[4]; 22437523Sjdp /* SSLv3/TLSv1 */ 22537523Sjdp if (p[4] >= TLS1_VERSION_MINOR) 22637523Sjdp { 22737523Sjdp if (!(s->options & SSL_OP_NO_TLSv1)) 22837523Sjdp { 22937523Sjdp tls1=1; 23037523Sjdp s->state=SSL23_ST_SR_CLNT_HELLO_B; 23137523Sjdp } 23237523Sjdp else if (!(s->options & SSL_OP_NO_SSLv3)) 2331558Srgrimes { 2341558Srgrimes s->state=SSL23_ST_SR_CLNT_HELLO_B; 2351558Srgrimes } 2361558Srgrimes else if (!(s->options & SSL_OP_NO_SSLv2)) 2371558Srgrimes { 2381558Srgrimes type=1; 2391558Srgrimes } 2401558Srgrimes } 2411558Srgrimes else if (!(s->options & SSL_OP_NO_SSLv3)) 2421558Srgrimes s->state=SSL23_ST_SR_CLNT_HELLO_B; 2431558Srgrimes else if (!(s->options & SSL_OP_NO_SSLv2)) 24423685Speter type=1; 24523685Speter 24623685Speter if (s->options & SSL_OP_NON_EXPORT_FIRST) 24792837Simp { 24823685Speter STACK_OF(SSL_CIPHER) *sk; 24923685Speter SSL_CIPHER *c; 25023685Speter int ne2,ne3; 25123685Speter 25223685Speter j=((p[0]&0x7f)<<8)|p[1]; 25323685Speter if (j > (1024*4)) 25423685Speter { 25523685Speter SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE); 25623685Speter goto err; 25723685Speter } 25823685Speter 25923685Speter n=ssl23_read_bytes(s,j+2); 26023685Speter if (n <= 0) return(n); 26123685Speter p=s->packet; 26223685Speter 26392837Simp if ((buf=Malloc(n)) == NULL) 26423685Speter { 26523685Speter SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,ERR_R_MALLOC_FAILURE); 26623685Speter goto err; 26723685Speter } 26823685Speter memcpy(buf,p,n); 26923685Speter 27023685Speter p+=5; 27123685Speter n2s(p,csl); 27223685Speter p+=4; 27323685Speter 27423685Speter sk=ssl_bytes_to_cipher_list( 27523685Speter s,p,csl,NULL); 27623685Speter if (sk != NULL) 27723685Speter { 27823685Speter ne2=ne3=0; 27923685Speter for (j=0; j<sk_SSL_CIPHER_num(sk); j++) 28023685Speter { 2811558Srgrimes c=sk_SSL_CIPHER_value(sk,j); 2821558Srgrimes if (!SSL_C_IS_EXPORT(c)) 2831558Srgrimes { 28492837Simp if ((c->id>>24L) == 2L) 2851558Srgrimes ne2=1; 28692806Sobrien else 2871558Srgrimes ne3=1; 2881558Srgrimes } 2891558Srgrimes } 2901558Srgrimes if (ne2 && !ne3) 2911558Srgrimes { 2921558Srgrimes type=1; 2931558Srgrimes use_sslv2_strong=1; 2941558Srgrimes goto next_bit; 2951558Srgrimes } 2961558Srgrimes } 2971558Srgrimes } 2981558Srgrimes } 2991558Srgrimes } 3001558Srgrimes else if ((p[0] == SSL3_RT_HANDSHAKE) && 3011558Srgrimes (p[1] == SSL3_VERSION_MAJOR) && 30292837Simp (p[5] == SSL3_MT_CLIENT_HELLO)) 3031558Srgrimes { 30492806Sobrien v[0]=p[1]; v[1]=p[2]; 3051558Srgrimes /* true SSLv3 or tls1 */ 3061558Srgrimes if (p[2] >= TLS1_VERSION_MINOR) 3071558Srgrimes { 3081558Srgrimes if (!(s->options & SSL_OP_NO_TLSv1)) 3091558Srgrimes { 3101558Srgrimes type=3; 3111558Srgrimes tls1=1; 3121558Srgrimes } 3131558Srgrimes else if (!(s->options & SSL_OP_NO_SSLv3)) 3141558Srgrimes type=3; 3151558Srgrimes } 3161558Srgrimes else if (!(s->options & SSL_OP_NO_SSLv3)) 3171558Srgrimes type=3; 3181558Srgrimes } 3191558Srgrimes else if ((strncmp("GET ", (char *)p,4) == 0) || 32092837Simp (strncmp("POST ",(char *)p,5) == 0) || 3211558Srgrimes (strncmp("HEAD ",(char *)p,5) == 0) || 3221558Srgrimes (strncmp("PUT ", (char *)p,4) == 0)) 3231558Srgrimes { 3241558Srgrimes SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTP_REQUEST); 3251558Srgrimes goto err; 3261558Srgrimes } 3271558Srgrimes else if (strncmp("CONNECT",(char *)p,7) == 0) 3281558Srgrimes { 3291558Srgrimes SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTPS_PROXY_REQUEST); 3301558Srgrimes goto err; 3311558Srgrimes } 3321558Srgrimes } 3331558Srgrimes 3341558Srgrimesnext_bit: 3351558Srgrimes if (s->state == SSL23_ST_SR_CLNT_HELLO_B) 3361558Srgrimes { 33737240Sbde /* we have a SSLv3/TLSv1 in a SSLv2 header */ 3381558Srgrimes type=2; 3391558Srgrimes p=s->packet; 3401558Srgrimes n=((p[0]&0x7f)<<8)|p[1]; 3411558Srgrimes if (n > (1024*4)) 3421558Srgrimes { 3431558Srgrimes SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE); 3441558Srgrimes goto err; 34592837Simp } 3461558Srgrimes 3471558Srgrimes j=ssl23_read_bytes(s,n+2); 3481558Srgrimes if (j <= 0) return(j); 3491558Srgrimes 3501558Srgrimes ssl3_finish_mac(s,&(s->packet[2]),s->packet_length-2); 3511558Srgrimes 3521558Srgrimes p=s->packet; 3531558Srgrimes p+=5; 3541558Srgrimes n2s(p,csl); 3551558Srgrimes n2s(p,sil); 3561558Srgrimes n2s(p,cl); 3571558Srgrimes d=(unsigned char *)s->init_buf->data; 3581558Srgrimes if ((csl+sil+cl+11) != s->packet_length) 3591558Srgrimes { 3601558Srgrimes SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_LENGTH_MISMATCH); 3611558Srgrimes goto err; 3621558Srgrimes } 3631558Srgrimes 3641558Srgrimes *(d++)=SSL3_VERSION_MAJOR; 3651558Srgrimes if (tls1) 3661558Srgrimes *(d++)=TLS1_VERSION_MINOR; 3671558Srgrimes else 3681558Srgrimes *(d++)=SSL3_VERSION_MINOR; 3691558Srgrimes 37092837Simp /* lets populate the random area */ 3711558Srgrimes /* get the chalenge_length */ 3721558Srgrimes i=(cl > SSL3_RANDOM_SIZE)?SSL3_RANDOM_SIZE:cl; 3731558Srgrimes memset(d,0,SSL3_RANDOM_SIZE); 3748871Srgrimes memcpy(&(d[SSL3_RANDOM_SIZE-i]),&(p[csl+sil]),i); 3751558Srgrimes d+=SSL3_RANDOM_SIZE; 3761558Srgrimes 3771558Srgrimes /* no session-id reuse */ 3781558Srgrimes *(d++)=0; 3791558Srgrimes 3801558Srgrimes /* ciphers */ 3811558Srgrimes j=0; 3821558Srgrimes dd=d; 3831558Srgrimes d+=2; 3841558Srgrimes for (i=0; i<csl; i+=3) 3851558Srgrimes { 38692837Simp if (p[i] != 0) continue; 3871558Srgrimes *(d++)=p[i+1]; 38869906Siedowse *(d++)=p[i+2]; 3891558Srgrimes j+=2; 3901558Srgrimes } 3911558Srgrimes s2n(j,dd); 3921558Srgrimes 3931558Srgrimes /* COMPRESSION */ 3941558Srgrimes *(d++)=1; 39569906Siedowse *(d++)=0; 3961558Srgrimes 3971558Srgrimes i=(d-(unsigned char *)s->init_buf->data); 3981558Srgrimes 3991558Srgrimes /* get the data reused from the init_buf */ 4001558Srgrimes s->s3->tmp.reuse_message=1; 4011558Srgrimes s->s3->tmp.message_type=SSL3_MT_CLIENT_HELLO; 4021558Srgrimes s->s3->tmp.message_size=i; 4031558Srgrimes } 4041558Srgrimes 4051558Srgrimes if (type == 1) 4061558Srgrimes { 4071558Srgrimes /* we are talking sslv2 */ 4081558Srgrimes /* we need to clean up the SSLv3/TLSv1 setup and put in the 4091558Srgrimes * sslv2 stuff. */ 4101558Srgrimes 4111558Srgrimes if (s->s2 == NULL) 4121558Srgrimes { 4131558Srgrimes if (!ssl2_new(s)) 414236213Skevlo goto err; 4151558Srgrimes } 4161558Srgrimes else 4171558Srgrimes ssl2_clear(s); 4181558Srgrimes 4191558Srgrimes if (s->s3 != NULL) ssl3_free(s); 4201558Srgrimes 4211558Srgrimes if (!BUF_MEM_grow(s->init_buf, 4221558Srgrimes SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)) 423 { 424 goto err; 425 } 426 427 s->state=SSL2_ST_GET_CLIENT_HELLO_A; 428 if ((s->options & SSL_OP_MSIE_SSLV2_RSA_PADDING) || 429 use_sslv2_strong) 430 s->s2->ssl2_rollback=0; 431 else 432 s->s2->ssl2_rollback=1; 433 434 /* setup the 5 bytes we have read so we get them from 435 * the sslv2 buffer */ 436 s->rstate=SSL_ST_READ_HEADER; 437 s->packet_length=n; 438 s->packet= &(s->s2->rbuf[0]); 439 memcpy(s->packet,buf,n); 440 s->s2->rbuf_left=n; 441 s->s2->rbuf_offs=0; 442 443 s->method=SSLv2_server_method(); 444 s->handshake_func=s->method->ssl_accept; 445 } 446 447 if ((type == 2) || (type == 3)) 448 { 449 /* we have SSLv3/TLSv1 */ 450 451 if (!ssl_init_wbio_buffer(s,1)) goto err; 452 453 /* we are in this state */ 454 s->state=SSL3_ST_SR_CLNT_HELLO_A; 455 456 if (type == 3) 457 { 458 /* put the 'n' bytes we have read into the input buffer 459 * for SSLv3 */ 460 s->rstate=SSL_ST_READ_HEADER; 461 s->packet_length=n; 462 s->packet= &(s->s3->rbuf.buf[0]); 463 memcpy(s->packet,buf,n); 464 s->s3->rbuf.left=n; 465 s->s3->rbuf.offset=0; 466 } 467 else 468 { 469 s->packet_length=0; 470 s->s3->rbuf.left=0; 471 s->s3->rbuf.offset=0; 472 } 473 474 if (tls1) 475 { 476 s->version=TLS1_VERSION; 477 s->method=TLSv1_server_method(); 478 } 479 else 480 { 481 s->version=SSL3_VERSION; 482 s->method=SSLv3_server_method(); 483 } 484 s->client_version=(v[0]<<8)|v[1]; 485 s->handshake_func=s->method->ssl_accept; 486 } 487 488 if ((type < 1) || (type > 3)) 489 { 490 /* bad, very bad */ 491 SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNKNOWN_PROTOCOL); 492 goto err; 493 } 494 s->init_num=0; 495 496 if (buf != buf_space) Free(buf); 497 s->first_packet=1; 498 return(SSL_accept(s)); 499err: 500 if (buf != buf_space) Free(buf); 501 return(-1); 502 } 503 504