s2_srvr.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/
/* ====================================================================
* Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* openssl-core@openssl.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include "ssl_locl.h"
#ifndef OPENSSL_NO_SSL2
#include <stdio.h>
static int get_client_master_key(SSL *s);
static int get_client_hello(SSL *s);
static int server_hello(SSL *s);
static int get_client_finished(SSL *s);
static int server_verify(SSL *s);
static int server_finish(SSL *s);
static int request_certificate(SSL *s);
#define BREAK break
{
if (ver == SSL2_VERSION)
return(SSLv2_server_method());
else
return(NULL);
}
SSL_METHOD *SSLv2_server_method(void)
{
static int init=1;
static SSL_METHOD SSLv2_server_data;
if (init)
{
if (init)
{
sizeof(SSL_METHOD));
init=0;
}
}
return(&SSLv2_server_data);
}
int ssl2_accept(SSL *s)
{
int ret= -1;
long num1;
RAND_add(&l,sizeof(l),0);
if (s->info_callback != NULL)
cb=s->info_callback;
/* init things to blank */
s->in_handshake++;
{
return(-1);
}
for (;;)
{
switch (s->state)
{
case SSL_ST_BEFORE:
case SSL_ST_ACCEPT:
case SSL_ST_BEFORE|SSL_ST_ACCEPT:
case SSL_ST_OK|SSL_ST_ACCEPT:
s->server=1;
s->version=SSL2_VERSION;
s->type=SSL_ST_ACCEPT;
if (!BUF_MEM_grow(buf,(int)
s->init_num=0;
s->shutdown=0;
ret=get_client_hello(s);
s->init_num=0;
ret=server_hello(s);
s->init_num=0;
if (!s->hit)
{
}
else
{
}
s->init_num=0;
/* Ok we how have sent all the stuff needed to
* start encrypting, the next packet back will
* be encrypted. */
if (!ssl2_enc_init(s,0))
s->s2->clear_text=0;
ret=server_verify(s);
s->init_num=0;
if (s->hit)
{
/* If we are in here, we have been
* buffering the output, so we need to
* flush it and remove buffering from
* future traffic */
}
else
{
break;
}
/* get the number of bytes to write */
if (num1 != 0)
{
s->rwstate=SSL_WRITING;
s->rwstate=SSL_NOTHING;
}
/* flushed and now remove buffering */
if (ret <= 0)
goto end;
s->init_num=0;
/* don't do a 'request certificate' if we
* don't want to, or we already have one, and
* we only want to do it once. */
if (!(s->verify_mode & SSL_VERIFY_PEER) ||
(s->verify_mode & SSL_VERIFY_CLIENT_ONCE)))
{
break;
}
else
{
s->init_num=0;
}
ret=server_finish(s);
s->init_num=0;
break;
case SSL_ST_OK:
BUF_MEM_free(s->init_buf);
s->init_num=0;
/* ERR_clear_error();*/
/* s->server=1; */
ret=1;
goto end;
/* BREAK; */
default:
ret= -1;
goto end;
/* BREAK; */
}
{
}
}
end:
s->in_handshake--;
return(ret);
}
static int get_client_master_key(SSL *s)
{
unsigned long len;
unsigned char *p;
SSL_CIPHER *cp;
const EVP_CIPHER *c;
if (s->state == SSL2_ST_GET_CLIENT_MASTER_KEY_A)
{
if (i < (10-s->init_num))
return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i));
s->init_num = 10;
if (*(p++) != SSL2_MT_CLIENT_MASTER_KEY)
{
if (p[-1] != SSL2_MT_ERROR)
{
}
else
return(-1);
}
{
return(-1);
}
p+=3;
{
return -1;
}
}
/* SSL2_ST_GET_CLIENT_MASTER_KEY_B */
{
return -1;
}
{
return -1;
}
if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i));
if (s->msg_callback)
p += 10;
(unsigned int)keya);
{
return(-1);
}
{
return(0);
}
{
is_export=1;
ek=8;
}
else
ek=5;
/* bad decrypt */
#if 1
/* If a bad decrypt, continue with protocol but with a
* random master secret (Bleichenbacher attack) */
if ((i < 0) ||
((!is_export && (i != EVP_CIPHER_key_length(c)))
(unsigned int)EVP_CIPHER_key_length(c))))))
{
if (is_export)
i=ek;
else
i=EVP_CIPHER_key_length(c);
RAND_pseudo_bytes(p,i);
}
#else
if (i < 0)
{
error=1;
}
/* incorrect number of key bytes for non export cipher */
else if ((!is_export && (i != EVP_CIPHER_key_length(c)))
EVP_CIPHER_key_length(c)))))
{
error=1;
}
if (error)
{
return(-1);
}
#endif
if (i > SSL_MAX_MASTER_KEY_LENGTH)
{
return -1;
}
s->session->master_key_length=i;
return(1);
}
static int get_client_hello(SSL *s)
{
int i,n;
unsigned long len;
unsigned char *p;
int z;
/* This is a bit of a hack to check for the correct packet
* type the first time round. */
if (s->state == SSL2_ST_GET_CLIENT_HELLO_A)
{
s->first_packet=1;
}
if (s->state == SSL2_ST_GET_CLIENT_HELLO_B)
{
if (i < (9-s->init_num))
return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
s->init_num = 9;
if (*(p++) != SSL2_MT_CLIENT_HELLO)
{
if (p[-1] != SSL2_MT_ERROR)
{
}
else
return(-1);
}
n2s(p,i);
if ( (i < SSL2_MIN_CHALLENGE_LENGTH) ||
(i > SSL2_MAX_CHALLENGE_LENGTH))
{
return(-1);
}
}
/* SSL2_ST_GET_CLIENT_HELLO_C */
len = 9 + (unsigned long)s->s2->tmp.cipher_spec_length + (unsigned long)s->s2->challenge_length + (unsigned long)s->s2->tmp.session_id_length;
{
return -1;
}
if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
if (s->msg_callback)
p += 9;
/* get session-id before cipher stuff so we can get out session
* structure if it is cached */
/* session-id */
{
return(-1);
}
{
if (!ssl_get_new_session(s,1))
{
return(-1);
}
}
else
{
if (i == 1)
{ /* previous session */
s->hit=1;
}
else if (i == -1)
{
return(-1);
}
else
{
{
return(-1);
}
if (!ssl_get_new_session(s,1))
{
return(-1);
}
}
}
if (!s->hit)
{
cl=SSL_get_ciphers(s);
if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
{
}
else
{
}
for (z=0; z<sk_SSL_CIPHER_num(prio); z++)
{
{
z--;
}
}
if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
{
}
/* s->session->ciphers should now have a list of
* ciphers that are on both the client and server.
* This list is ordered by the order the client sent
* the ciphers or in the order of the server's preference
* if SSL_OP_CIPHER_SERVER_PREFERENCE was set.
*/
}
/* done cipher selection */
/* session id extracted already */
/* challenge */
{
return -1;
}
return(1);
return(0);
}
static int server_hello(SSL *s)
{
unsigned char *p,*d;
int n,hit;
if (s->state == SSL2_ST_SEND_SERVER_HELLO_A)
{
d=p+11;
*(p++)=SSL2_MT_SERVER_HELLO; /* type */
*(p++)=(unsigned char)hit;
#if 1
if (!hit)
{
/* This can't really happen because get_client_hello
* has called ssl_get_new_session, which does not set
* sess_cert. */
{
return(-1);
}
}
/* If 'hit' is set, then s->sess_cert may be non-NULL or NULL,
* depending on whether it survived in the internal cache
* or was retrieved from an external cache.
* If it is NULL, we cannot put any useful data in it anyway,
* so we don't touch it.
*/
#else /* That's what used to be done when cert_st and sess_cert_st were
* the same. */
if (!hit)
{ /* else add cert to session */
}
else /* We have a session id-cache hit, if the
* session-id has no certificate listed against
* the 'cert' structure, grab the 'old' one
* listed against the SSL connection */
{
{
}
}
#endif
{
return(-1);
}
if (hit)
{
*(p++)=0; /* no certificate type */
s2n(0,p); /* cert len */
s2n(0,p); /* ciphers len */
}
else
{
/* EAY EAY */
/* put certificate type */
*(p++)=SSL2_CT_X509_CERTIFICATE;
s2n(n,p); /* certificate length */
n=0;
/* lets send out the ciphers we like in the
* prefered order */
d+=n;
s2n(n,p); /* add cipher length */
}
/* make and send conn_id */
s->init_off=0;
}
/* SSL2_ST_SEND_SERVER_HELLO_B */
* writes without a read between them. This occurs when
* Session-id reuse is used, so I will put in a buffering module
*/
if (s->hit)
{
}
return(ssl2_do_write(s));
}
static int get_client_finished(SSL *s)
{
unsigned char *p;
int i, n;
unsigned long len;
if (s->state == SSL2_ST_GET_CLIENT_FINISHED_A)
{
if (i < 1-s->init_num)
return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
s->init_num += i;
if (*p != SSL2_MT_CLIENT_FINISHED)
{
if (*p != SSL2_MT_ERROR)
{
}
else
{
/* try to read the error message */
return ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i);
}
return(-1);
}
}
/* SSL2_ST_GET_CLIENT_FINISHED_B */
{
return -1;
}
if (i < n)
{
return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
}
if (s->msg_callback)
p += 1;
{
return(-1);
}
return(1);
}
static int server_verify(SSL *s)
{
unsigned char *p;
if (s->state == SSL2_ST_SEND_SERVER_VERIFY_A)
{
*(p++)=SSL2_MT_SERVER_VERIFY;
{
return -1;
}
/* p+=s->s2->challenge_length; */
s->init_off=0;
}
return(ssl2_do_write(s));
}
static int server_finish(SSL *s)
{
unsigned char *p;
if (s->state == SSL2_ST_SEND_SERVER_FINISHED_A)
{
*(p++)=SSL2_MT_SERVER_FINISHED;
{
return -1;
}
/* p+=s->session->session_id_length; */
s->init_off=0;
}
/* SSL2_ST_SEND_SERVER_FINISHED_B */
return(ssl2_do_write(s));
}
/* send the request and check the response */
static int request_certificate(SSL *s)
{
unsigned char *ccd;
unsigned long len;
if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_A)
{
*(p++)=SSL2_MT_REQUEST_CERTIFICATE;
*(p++)=SSL2_AT_MD5_WITH_RSA_ENCRYPTION;
s->init_off=0;
}
if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_B)
{
i=ssl2_do_write(s);
if (i <= 0)
{
ret=i;
goto end;
}
s->init_num=0;
}
if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_C)
{
* (probably NO-CERTIFICATE-ERROR) */
{
goto end;
}
s->init_num += i;
{
n2s(p,i);
if (i != SSL2_PE_NO_CERTIFICATE)
{
/* not the error message we expected -- let ssl2_part_read handle it */
s->init_num -= 3;
goto end;
}
if (s->msg_callback)
/* this is the one place where we can recover from an SSL 2.0 error */
if (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
{
goto end;
}
ret=1;
goto end;
}
{
goto end;
}
if (s->init_num != 6)
{
goto end;
}
/* ok we have a response */
/* certificate type, there is only one right now. */
ctype= *(p++);
if (ctype != SSL2_AT_MD5_WITH_RSA_ENCRYPTION)
{
goto end;
}
}
/* SSL2_ST_SEND_REQUEST_CERTIFICATE_D */
{
goto end;
}
if (i < j)
{
goto end;
}
if (s->msg_callback)
p += 6;
{
goto msg_end;
}
{
goto msg_end;
}
i=ssl_verify_cert_chain(s,sk);
if (i) /* we like the packet, now check the chksum */
{
s->s2->key_material_length);
buf2=OPENSSL_malloc((unsigned int)i);
{
goto msg_end;
}
if (i)
{
ret=1;
goto end;
}
else
{
goto msg_end;
}
}
else
{
}
end:
return(ret);
}
{
int i;
{
return(-1);
}
{
return(-1);
}
/* we have the public key */
if (i < 0)
return(i);
}
#else /* !OPENSSL_NO_SSL2 */
# if PEDANTIC
# endif
#endif