s3_lib.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-2002 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 <stdio.h>
#include "ssl_locl.h"
#include "kssl_lcl.h"
static long ssl3_default_timeout(void );
/* The RSA ciphers */
/* Cipher 01 */
{
1,
0,
0,
0,
},
/* Cipher 02 */
{
1,
0,
0,
0,
},
/* anon DH */
/* Cipher 17 */
{
1,
0,
40,
128,
},
/* Cipher 18 */
{
1,
0,
128,
128,
},
/* Cipher 19 */
{
1,
0,
40,
128,
},
/* Cipher 1A */
{
1,
0,
56,
56,
},
/* Cipher 1B */
{
1,
0,
168,
168,
},
/* RSA again */
/* Cipher 03 */
{
1,
0,
40,
128,
},
/* Cipher 04 */
{
1,
0,
128,
128,
},
/* Cipher 05 */
{
1,
0,
128,
128,
},
/* Cipher 06 */
{
1,
0,
40,
128,
},
/* Cipher 07 */
#ifndef OPENSSL_NO_IDEA
{
1,
0,
128,
128,
},
#endif
/* Cipher 08 */
{
1,
0,
40,
56,
},
/* Cipher 09 */
{
1,
0,
56,
56,
},
/* Cipher 0A */
{
1,
0,
168,
168,
},
/* The DH ciphers */
/* Cipher 0B */
{
0,
0,
40,
56,
},
/* Cipher 0C */
{
0,
0,
56,
56,
},
/* Cipher 0D */
{
0,
0,
168,
168,
},
/* Cipher 0E */
{
0,
0,
40,
56,
},
/* Cipher 0F */
{
0,
0,
56,
56,
},
/* Cipher 10 */
{
0,
0,
168,
168,
},
/* The Ephemeral DH ciphers */
/* Cipher 11 */
{
1,
0,
40,
56,
},
/* Cipher 12 */
{
1,
0,
56,
56,
},
/* Cipher 13 */
{
1,
0,
168,
168,
},
/* Cipher 14 */
{
1,
0,
40,
56,
},
/* Cipher 15 */
{
1,
0,
56,
56,
},
/* Cipher 16 */
{
1,
0,
168,
168,
},
/* Fortezza */
/* Cipher 1C */
{
0,
0,
0,
0,
},
/* Cipher 1D */
{
0,
0,
0,
0,
},
#if 0
/* Cipher 1E */
{
0,
0,
128,
128,
},
#endif
#ifndef OPENSSL_NO_KRB5
/* The Kerberos ciphers
** 20000107 VRS: And the first shall be last,
** in hopes of avoiding the lynx ssl renegotiation problem.
*/
/* Cipher 1E VRS */
{
1,
0,
56,
56,
},
/* Cipher 1F VRS */
{
1,
0,
112,
168,
},
/* Cipher 20 VRS */
{
1,
0,
128,
128,
},
/* Cipher 21 VRS */
{
1,
0,
128,
128,
},
/* Cipher 22 VRS */
{
1,
0,
56,
56,
},
/* Cipher 23 VRS */
{
1,
0,
112,
168,
},
/* Cipher 24 VRS */
{
1,
0,
128,
128,
},
/* Cipher 25 VRS */
{
1,
0,
128,
128,
},
/* Cipher 26 VRS */
{
1,
0,
40,
56,
},
/* Cipher 27 VRS */
{
1,
0,
40,
128,
},
/* Cipher 28 VRS */
{
1,
0,
128,
128,
},
/* Cipher 29 VRS */
{
1,
0,
40,
56,
},
/* Cipher 2A VRS */
{
1,
0,
40,
128,
},
/* Cipher 2B VRS */
{
1,
0,
128,
128,
},
#endif /* OPENSSL_NO_KRB5 */
/* New TLS Export CipherSuites */
/* Cipher 60 */
{
1,
0,
56,
128,
},
/* Cipher 61 */
{
1,
0,
56,
128,
},
/* Cipher 62 */
{
1,
0,
56,
56,
},
/* Cipher 63 */
{
1,
0,
56,
56,
},
/* Cipher 64 */
{
1,
0,
56,
128,
},
/* Cipher 65 */
{
1,
0,
56,
128,
},
/* Cipher 66 */
{
1,
0,
128,
128,
},
#endif
/* New AES ciphersuites */
/* Cipher 2F */
{
1,
0,
128,
128,
},
/* Cipher 30 */
{
0,
0,
128,
128,
},
/* Cipher 31 */
{
0,
0,
128,
128,
},
/* Cipher 32 */
{
1,
0,
128,
128,
},
/* Cipher 33 */
{
1,
0,
128,
128,
},
/* Cipher 34 */
{
1,
0,
128,
128,
},
/* Cipher 35 */
{
1,
0,
256,
256,
},
/* Cipher 36 */
{
0,
0,
256,
256,
},
/* Cipher 37 */
{
0,
0,
256,
256,
},
/* Cipher 38 */
{
1,
0,
256,
256,
},
/* Cipher 39 */
{
1,
0,
256,
256,
},
/* Cipher 3A */
{
1,
0,
256,
256,
},
/* end of list */
};
static SSL3_ENC_METHOD SSLv3_enc_data={
};
static SSL_METHOD SSLv3_data= {
};
static long ssl3_default_timeout(void)
{
/* 2 hours, the 24 hours mentioned in the SSLv3 spec
* is way too long for http, the cache would over fill */
return(60*60*2);
}
SSL_METHOD *sslv3_base_method(void)
{
return(&SSLv3_data);
}
int ssl3_num_ciphers(void)
{
return(SSL3_NUM_CIPHERS);
}
SSL_CIPHER *ssl3_get_cipher(unsigned int u)
{
if (u < SSL3_NUM_CIPHERS)
else
return(NULL);
}
int ssl3_pending(SSL *s)
{
if (s->rstate == SSL_ST_READ_BODY)
return 0;
}
{
SSL3_STATE *s3;
return(1);
err:
return(0);
}
{
if(s == NULL)
return;
#ifndef OPENSSL_NO_DH
#endif
OPENSSL_free(s->s3);
}
void ssl3_clear(SSL *s)
{
{
}
#ifndef OPENSSL_NO_DH
#endif
s->packet_length=0;
s->s3->renegotiate=0;
s->s3->total_renegotiations=0;
s->s3->num_renegotiations=0;
s->s3->in_read_app_data=0;
s->version=SSL3_VERSION;
}
{
int ret=0;
#if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_RSA)
if (
#ifndef OPENSSL_NO_RSA
cmd == SSL_CTRL_SET_TMP_RSA ||
cmd == SSL_CTRL_SET_TMP_RSA_CB ||
#endif
#ifndef OPENSSL_NO_DSA
cmd == SSL_CTRL_SET_TMP_DH ||
cmd == SSL_CTRL_SET_TMP_DH_CB ||
#endif
0)
{
if (!ssl_cert_inst(&s->cert))
{
return(0);
}
}
#endif
switch (cmd)
{
break;
break;
break;
s->s3->num_renegotiations=0;
break;
break;
case SSL_CTRL_GET_FLAGS:
break;
#ifndef OPENSSL_NO_RSA
case SSL_CTRL_NEED_TMP_RSA:
ret = 1;
break;
case SSL_CTRL_SET_TMP_RSA:
{
{
return(ret);
}
{
return(ret);
}
ret = 1;
}
break;
case SSL_CTRL_SET_TMP_RSA_CB:
{
return(ret);
}
break;
#endif
#ifndef OPENSSL_NO_DH
case SSL_CTRL_SET_TMP_DH:
{
{
return(ret);
}
{
return(ret);
}
if (!(s->options & SSL_OP_SINGLE_DH_USE))
{
if (!DH_generate_key(dh))
{
return(ret);
}
}
ret = 1;
}
break;
case SSL_CTRL_SET_TMP_DH_CB:
{
return(ret);
}
break;
#endif
default:
break;
}
return(ret);
}
{
int ret=0;
#if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_RSA)
if (
#ifndef OPENSSL_NO_RSA
cmd == SSL_CTRL_SET_TMP_RSA_CB ||
#endif
#ifndef OPENSSL_NO_DSA
cmd == SSL_CTRL_SET_TMP_DH_CB ||
#endif
0)
{
if (!ssl_cert_inst(&s->cert))
{
return(0);
}
}
#endif
switch (cmd)
{
#ifndef OPENSSL_NO_RSA
case SSL_CTRL_SET_TMP_RSA_CB:
{
}
break;
#endif
#ifndef OPENSSL_NO_DH
case SSL_CTRL_SET_TMP_DH_CB:
{
}
break;
#endif
default:
break;
}
return(ret);
}
{
switch (cmd)
{
#ifndef OPENSSL_NO_RSA
case SSL_CTRL_NEED_TMP_RSA:
)
return(1);
else
return(0);
/* break; */
case SSL_CTRL_SET_TMP_RSA:
{
int i;
i=1;
i=0;
else
{
i=0;
}
if (!i)
{
return(0);
}
else
{
return(1);
}
}
/* break; */
case SSL_CTRL_SET_TMP_RSA_CB:
{
return(0);
}
break;
#endif
#ifndef OPENSSL_NO_DH
case SSL_CTRL_SET_TMP_DH:
{
{
return 0;
}
{
if (!DH_generate_key(new))
{
return 0;
}
}
return 1;
}
/*break; */
case SSL_CTRL_SET_TMP_DH_CB:
{
return(0);
}
break;
#endif
/* A Thawte special :-) */
{
return(0);
}
break;
default:
return(0);
}
return(1);
}
{
switch (cmd)
{
#ifndef OPENSSL_NO_RSA
case SSL_CTRL_SET_TMP_RSA_CB:
{
}
break;
#endif
#ifndef OPENSSL_NO_DH
case SSL_CTRL_SET_TMP_DH_CB:
{
}
break;
#endif
default:
return(0);
}
return(1);
}
/* This function needs to check if the ciphers required are actually
* available */
SSL_CIPHER *ssl3_get_cipher_by_char(const unsigned char *p)
{
static int init=1;
unsigned long id;
int i;
if (init)
{
if (init)
{
for (i=0; i<SSL3_NUM_CIPHERS; i++)
sorted[i]= &(ssl3_ciphers[i]);
SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
init=0;
}
}
(char *)sorted,
SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
return(NULL);
else
return(*cpp);
}
int ssl3_put_cipher_by_char(const SSL_CIPHER *c, unsigned char *p)
{
long l;
if (p != NULL)
{
l=c->id;
if ((l & 0xff000000) != 0x03000000) return(0);
p[0]=((unsigned char)(l>> 8L))&0xFF;
p[1]=((unsigned char)(l ))&0xFF;
}
return(2);
}
{
int i,j,ok;
/* Let's see which ciphers we can support */
#if 0
/* Do not set the compare functions, because this may lead to a
* reordering by "id". We want to keep the original ordering.
* We may pay a price in performance during sk_SSL_CIPHER_find(),
* but would have to pay with the price of sk_SSL_CIPHER_dup().
*/
#endif
#ifdef CIPHER_DEBUG
for(i=0 ; i < sk_SSL_CIPHER_num(srvr) ; ++i)
{
c=sk_SSL_CIPHER_value(srvr,i);
}
for(i=0 ; i < sk_SSL_CIPHER_num(clnt) ; ++i)
{
c=sk_SSL_CIPHER_value(clnt,i);
}
#endif
if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
{
}
else
{
}
for (i=0; i<sk_SSL_CIPHER_num(prio); i++)
{
c=sk_SSL_CIPHER_value(prio,i);
#ifdef KSSL_DEBUG
#endif /* KSSL_DEBUG */
#ifndef OPENSSL_NO_KRB5
{
if ( !kssl_keytab_is_available(s->kssl_ctx) )
continue;
}
#endif /* OPENSSL_NO_KRB5 */
if (SSL_C_IS_EXPORT(c))
{
#ifdef CIPHER_DEBUG
c,c->name);
#endif
}
else
{
#ifdef CIPHER_DEBUG
c->name);
#endif
}
if (!ok) continue;
j=sk_SSL_CIPHER_find(allow,c);
if (j >= 0)
{
break;
}
}
return(ret);
}
int ssl3_get_req_cert_type(SSL *s, unsigned char *p)
{
int ret=0;
unsigned long alg;
#ifndef OPENSSL_NO_DH
{
# ifndef OPENSSL_NO_RSA
p[ret++]=SSL3_CT_RSA_FIXED_DH;
# endif
# ifndef OPENSSL_NO_DSA
p[ret++]=SSL3_CT_DSS_FIXED_DH;
# endif
}
if ((s->version == SSL3_VERSION) &&
{
# ifndef OPENSSL_NO_RSA
p[ret++]=SSL3_CT_RSA_EPHEMERAL_DH;
# endif
# ifndef OPENSSL_NO_DSA
p[ret++]=SSL3_CT_DSS_EPHEMERAL_DH;
# endif
}
#endif /* !OPENSSL_NO_DH */
#ifndef OPENSSL_NO_RSA
p[ret++]=SSL3_CT_RSA_SIGN;
#endif
#ifndef OPENSSL_NO_DSA
p[ret++]=SSL3_CT_DSS_SIGN;
#endif
return(ret);
}
int ssl3_shutdown(SSL *s)
{
/* Don't do anything much if we have not done the handshake or
* we don't want to send messages :-) */
{
return(1);
}
if (!(s->shutdown & SSL_SENT_SHUTDOWN))
{
#if 1
#endif
/* our shutdown alert has been sent now, and if it still needs
* to be written, s->s3->alert_dispatch will be true */
}
else if (s->s3->alert_dispatch)
{
/* resend it if not sent */
#if 1
#endif
}
else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN))
{
/* If we are waiting for a close from our peer, we are closed */
ssl3_read_bytes(s,0,NULL,0,0);
}
!s->s3->alert_dispatch)
return(1);
else
return(0);
}
{
int ret,n;
#if 0
if (s->shutdown & SSL_SEND_SHUTDOWN)
{
s->rwstate=SSL_NOTHING;
return(0);
}
#endif
/* This is an experimental flag that sends the
* last handshake message in the same packet as the first
* use data - used to see if it helps the TCP protocol during
* session-id reuse */
/* The second test is because the buffer may have been removed */
{
/* First time through, we write into the buffer */
if (s->s3->delay_buf_pop_ret == 0)
{
}
s->rwstate=SSL_WRITING;
if (n <= 0) return(n);
s->rwstate=SSL_NOTHING;
/* We have flushed the buffer, so remove it */
s->s3->delay_buf_pop_ret=0;
}
else
{
}
return(ret);
}
{
int ret;
{
/* ssl3_read_bytes decided to call s->handshake_func, which
* called ssl3_read_bytes to read handshake data.
* However, ssl3_read_bytes actually found application data
* and thinks that application data makes sense here; so disable
* handshake processing and try to read application data again. */
s->in_handshake++;
s->in_handshake--;
}
else
s->s3->in_read_app_data=0;
return(ret);
}
{
}
{
}
int ssl3_renegotiate(SSL *s)
{
if (s->handshake_func == NULL)
return(1);
return(0);
return(1);
}
int ssl3_renegotiate_check(SSL *s)
{
int ret=0;
if (s->s3->renegotiate)
{
!SSL_in_init(s))
{
/*
if we are the server, and we have sent a 'RENEGOTIATE' message, we
need to go to SSL_ST_ACCEPT.
*/
/* SSL_ST_ACCEPT */
s->s3->renegotiate=0;
s->s3->num_renegotiations++;
s->s3->total_renegotiations++;
ret=1;
}
}
return(ret);
}