s2_clnt.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_server_finished(SSL *s);
static int get_server_verify(SSL *s);
static int get_server_hello(SSL *s);
static int client_hello(SSL *s);
static int client_master_key(SSL *s);
static int client_finished(SSL *s);
static int client_certificate(SSL *s);
#define BREAK break
{
if (ver == SSL2_VERSION)
return(SSLv2_client_method());
else
return(NULL);
}
SSL_METHOD *SSLv2_client_method(void)
{
static int init=1;
static SSL_METHOD SSLv2_client_data;
if (init)
{
if (init)
{
sizeof(SSL_METHOD));
init=0;
}
}
return(&SSLv2_client_data);
}
int ssl2_connect(SSL *s)
{
int ret= -1;
RAND_add(&l,sizeof(l),0);
if (s->info_callback != NULL)
cb=s->info_callback;
/* init things to blank */
s->in_handshake++;
for (;;)
{
switch (s->state)
{
case SSL_ST_BEFORE:
case SSL_ST_CONNECT:
case SSL_ST_BEFORE|SSL_ST_CONNECT:
case SSL_ST_OK|SSL_ST_CONNECT:
s->server=0;
s->version=SSL2_VERSION;
s->type=SSL_ST_CONNECT;
{
ret= -1;
goto end;
}
if (!BUF_MEM_grow(buf,
{
ret= -1;
goto end;
}
s->init_num=0;
s->shutdown=0;
ret=client_hello(s);
s->init_num=0;
ret=get_server_hello(s);
s->init_num=0;
if (!s->hit) /* new session */
{
}
else
{
break;
}
ret=client_master_key(s);
s->init_num=0;
break;
/* Ok, we now have all the stuff needed to
* start encrypting, so lets fire it up :-) */
if (!ssl2_enc_init(s,1))
{
ret= -1;
goto end;
}
s->s2->clear_text=0;
break;
ret=client_finished(s);
s->init_num=0;
break;
ret=get_server_verify(s);
s->init_num=0;
break;
break;
s->init_num=0;
break;
case SSL_ST_OK:
{
BUF_MEM_free(s->init_buf);
}
s->init_num=0;
/* ERR_clear_error();*/
/* If we want to cache session-ids in the client
* and we successfully add the session-id to the
* cache, and there is a callback, then pass it out.
* 26/11/96 - eay - only add if not a re-used session.
*/
ret=1;
/* s->server=0; */
goto end;
/* break; */
default:
return(-1);
/* break; */
}
{
}
}
end:
s->in_handshake--;
return(ret);
}
static int get_server_hello(SSL *s)
{
unsigned char *buf;
unsigned char *p;
int i,j;
unsigned long len;
p=buf;
if (s->state == SSL2_ST_GET_SERVER_HELLO_A)
{
if (i < (11-s->init_num))
return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i));
s->init_num = 11;
if (*(p++) != SSL2_MT_SERVER_HELLO)
{
if (p[-1] != SSL2_MT_ERROR)
{
}
else
return(-1);
}
#ifdef __APPLE_CC__
/* The Rhapsody 5.5 (a.k.a. MacOS X) compiler bug
* workaround. <appro@fy.chalmers.se> */
s->hit=(i=*(p++))?1:0;
#else
s->hit=(*(p++))?1:0;
#endif
n2s(p,i);
}
/* SSL2_ST_GET_SERVER_HELLO_B */
len = 11 + (unsigned long)s->s2->tmp.cert_length + (unsigned long)s->s2->tmp.csl + (unsigned long)s->s2->tmp.conn_id_length;
{
return -1;
}
if (i != j) return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i));
if (s->msg_callback)
/* things are looking good */
p = buf + 11;
if (s->hit)
{
{
return(-1);
}
{
if (!(s->options &
{
return(-1);
}
}
{
return(-1);
}
}
else
{
#ifdef undef
/* very bad */
s->session->session_id_length=0;
*/
#endif
/* we need to do this in case we were trying to reuse a
* client session but others are already reusing it.
* If this was a new 'blank' session ID, the session-id
* length will still be 0 */
if (s->session->session_id_length > 0)
{
if (!ssl_get_new_session(s,0))
{
return(-1);
}
}
{
return(-1);
}
{
return(-1);
}
/* We have just received a list of ciphers back from the
* server. We need to get the ones that match, then select
* the one we want the most :-). */
/* load the ciphers */
{
return(-1);
}
/* get the array of ciphers we will accept */
cl=SSL_get_ciphers(s);
/*
* If server preference flag set, choose the first
* (highest priority) cipher the server sends, otherwise
* client preference has priority.
*/
if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
{
}
else
{
}
/* In theory we could have ciphers sent back that we
* don't want to use but that does not matter since we
* will check against the list we originally sent and
* for performance reasons we should not bother to match
* the two lists up just to check. */
for (i=0; i<sk_SSL_CIPHER_num(prio); i++)
{
if (sk_SSL_CIPHER_find(allow,
sk_SSL_CIPHER_value(prio,i)) >= 0)
break;
}
if (i >= sk_SSL_CIPHER_num(prio))
{
return(-1);
}
{
return(-1);
}
/* peer_key->x509 has been set by ssl2_set_certificate. */
}
/* can't happen */
{
return(-1);
}
{
return -1;
}
return(1);
}
static int client_hello(SSL *s)
{
unsigned char *buf;
unsigned char *p,*d;
/* CIPHER **cipher;*/
int i,n,j;
if (s->state == SSL2_ST_SEND_CLIENT_HELLO_A)
{
{
if (!ssl_get_new_session(s,0))
{
return(-1);
}
}
/* else use the pre-loaded session */
p=buf; /* header */
d=p+9; /* data section */
*(p++)=SSL2_MT_CLIENT_HELLO; /* type */
n=j=0;
n=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),d);
d+=n;
if (n == 0)
{
return(-1);
}
s2n(n,p); /* cipher spec num bytes */
if ((s->session->session_id_length > 0) &&
(s->session->session_id_length <=
{
i=s->session->session_id_length;
s2n(i,p); /* session id length */
d+=i;
}
else
{
s2n(0,p);
}
/*challenge id data*/
s->init_off=0;
}
/* SSL2_ST_SEND_CLIENT_HELLO_B */
return(ssl2_do_write(s));
}
static int client_master_key(SSL *s)
{
unsigned char *buf;
unsigned char *p,*d;
const EVP_CIPHER *c;
if (s->state == SSL2_ST_SEND_CLIENT_MASTER_KEY_A)
{
{
return(-1);
}
p=buf;
d=p+10;
*(p++)=SSL2_MT_CLIENT_MASTER_KEY;/* type */
p+=i;
/* make key_arg data */
i=EVP_CIPHER_iv_length(c);
sess->key_arg_length=i;
if (i > SSL_MAX_KEY_ARG_LENGTH)
{
return -1;
}
/* make a master key */
i=EVP_CIPHER_key_length(c);
if (i > 0)
{
if (i > sizeof sess->master_key)
{
return -1;
}
{
return(-1);
}
}
enc=8;
enc=5;
else
enc=i;
if (i < enc)
{
return(-1);
}
d+=clear;
if (enc <= 0)
{
return(-1);
}
#ifdef PKCS1_CHECK
if (s->options & SSL_OP_PKCS1_CHECK_2)
#endif
d+=enc;
{
return -1;
}
d+=karg;
s->init_off=0;
}
/* SSL2_ST_SEND_CLIENT_MASTER_KEY_B */
return(ssl2_do_write(s));
}
static int client_finished(SSL *s)
{
unsigned char *p;
if (s->state == SSL2_ST_SEND_CLIENT_FINISHED_A)
{
*(p++)=SSL2_MT_CLIENT_FINISHED;
{
return -1;
}
s->init_off=0;
}
return(ssl2_do_write(s));
}
/* read the data and then respond */
static int client_certificate(SSL *s)
{
unsigned char *buf;
unsigned char *p,*d;
int i;
unsigned int n;
int cert_ch_len;
unsigned char *cert_ch;
/* We have a cert associated with the SSL, so attach it to
* the session if it does not have one */
if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_A)
{
return(ssl2_part_read(s,SSL_F_CLIENT_CERTIFICATE,i));
s->init_num += i;
if (s->msg_callback)
s->msg_callback(0, s->version, 0, buf, (size_t)s->init_num, s, s->msg_callback_arg); /* REQUEST-CERTIFICATE */
/* type=buf[0]; */
/* type eq x509 */
{
return(-1);
}
{
}
else
}
if (s->state == SSL2_ST_X509_GET_CLIENT_CERTIFICATE)
{
/* If we get an error we need to
* ssl->rwstate=SSL_X509_LOOKUP;
* return(error);
* We should then be retried when things are ok and we
* can get a cert or not */
i=0;
{
}
if (i < 0)
{
return(-1);
}
s->rwstate=SSL_NOTHING;
{
if ( !SSL_use_certificate(s,x509) ||
!SSL_use_PrivateKey(s,pkey))
{
i=0;
}
}
else if (i == 1)
{
i=0;
}
if (i == 0)
{
/* We have no client certificate to respond with
* so send the correct error message back */
p=buf;
*(p++)=SSL2_MT_ERROR;
s->init_off=0;
s->init_num=3;
/* Write is done at the end */
}
}
if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_B)
{
return(ssl2_do_write(s));
}
if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_C)
{
/* ok, now we calculate the checksum
* do it first so we can reuse buf :-) */
p=buf;
s->s2->key_material_length);
p=buf;
d=p+6;
*(p++)=SSL2_MT_CLIENT_CERTIFICATE;
*(p++)=SSL2_CT_X509_CERTIFICATE;
s2n(n,p);
{
/* this is not good. If things have failed it
* means there so something wrong with the key.
* We will continue with a 0 length signature
*/
}
s2n(n,p);
d+=n;
s->init_off=0;
}
/* if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_D) */
return(ssl2_do_write(s));
}
static int get_server_verify(SSL *s)
{
unsigned char *p;
int i, n, len;
if (s->state == SSL2_ST_GET_SERVER_VERIFY_A)
{
if (i < (1-s->init_num))
return(ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i));
s->init_num += i;
if (*p != SSL2_MT_SERVER_VERIFY)
{
if (p[0] != SSL2_MT_ERROR)
{
}
else
{
/* try to read the error message */
return ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i);
}
return(-1);
}
}
if (i < n)
return(ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i));
if (s->msg_callback)
p += 1;
{
return(-1);
}
return(1);
}
static int get_server_finished(SSL *s)
{
unsigned char *buf;
unsigned char *p;
int i, n, len;
p=buf;
if (s->state == SSL2_ST_GET_SERVER_FINISHED_A)
{
if (i < (1-s->init_num))
return(ssl2_part_read(s,SSL_F_GET_SERVER_FINISHED,i));
s->init_num += i;
if (*p == SSL2_MT_REQUEST_CERTIFICATE)
{
return(1);
}
else if (*p != SSL2_MT_SERVER_FINISHED)
{
if (p[0] != SSL2_MT_ERROR)
{
}
else
{
/* try to read the error message */
return ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i);
}
return(-1);
}
}
if (i < n) /* XXX could be shorter than SSL2_SSL_SESSION_ID_LENGTH, that's the maximum */
return(ssl2_part_read(s,SSL_F_GET_SERVER_FINISHED,i));
s->init_num += i;
if (s->msg_callback)
s->msg_callback(0, s->version, 0, buf, (size_t)s->init_num, s, s->msg_callback_arg); /* SERVER-FINISHED */
if (!s->hit) /* new session */
{
/* new session-id */
/* Make sure we were not trying to re-use an old SSL_SESSION
* or bad things can happen */
/* ZZZZZZZZZZZZZ */
}
else
{
if (!(s->options & SSL_OP_MICROSOFT_SESS_ID_BUG))
{
(unsigned int)s->session->session_id_length)))
{
return(-1);
}
}
}
return(1);
}
/* loads in the certificate from the server */
{
int i;
int ret=0;
{
goto err;
}
{
goto err;
}
i=ssl_verify_cert_chain(s,sk);
if ((s->verify_mode != SSL_VERIFY_NONE) && (!i))
{
goto err;
}
ERR_clear_error(); /* but we keep s->verify_result */
/* server's cert for this session */
{
ret= -1;
goto err;
}
{
goto err;
}
{
goto err;
}
goto err;
ret=1;
err:
return(ret);
}
{
int i= -1;
{
return(-1);
}
{
goto end;
}
/* we have the public key */
if (i < 0)
end:
return(i);
}
#else /* !OPENSSL_NO_SSL2 */
# if PEDANTIC
# endif
#endif