/* ssl/s3_lib.c */
-/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
* This package is an SSL implementation written
#include "objects.h"
#include "ssl_locl.h"
-char *ssl3_version_str="SSLv3 part of SSLeay 0.8.1b 29-Jun-1998";
+char *ssl3_version_str="SSLv3 part of OpenSSL 0.9.2 31-Dec-1998";
#define SSL3_NUM_CIPHERS (sizeof(ssl3_ciphers)/sizeof(SSL_CIPHER))
1,
SSL3_TXT_ADH_RC4_40_MD5,
SSL3_CK_ADH_RC4_40_MD5,
- SSL_kEDH |SSL_aNULL|SSL_RC4 |SSL_MD5 |SSL_EXP|SSL_SSLV3,
+ SSL_kEDH |SSL_aNULL|SSL_RC4 |SSL_MD5 |SSL_EXP40|SSL_SSLV3,
0,
SSL_ALL_CIPHERS,
},
1,
SSL3_TXT_ADH_DES_40_CBC_SHA,
SSL3_CK_ADH_DES_40_CBC_SHA,
- SSL_kEDH |SSL_aNULL|SSL_DES|SSL_SHA1|SSL_EXP|SSL_SSLV3,
+ SSL_kEDH |SSL_aNULL|SSL_DES|SSL_SHA1|SSL_EXP40|SSL_SSLV3,
0,
SSL_ALL_CIPHERS,
},
/* Cipher 1B */
{
1,
- SSL3_TXT_ADH_DES_196_CBC_SHA,
- SSL3_CK_ADH_DES_196_CBC_SHA,
+ SSL3_TXT_ADH_DES_192_CBC_SHA,
+ SSL3_CK_ADH_DES_192_CBC_SHA,
SSL_kEDH |SSL_aNULL|SSL_3DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3,
0,
SSL_ALL_CIPHERS,
1,
SSL3_TXT_RSA_RC4_40_MD5,
SSL3_CK_RSA_RC4_40_MD5,
- SSL_kRSA|SSL_aRSA|SSL_RC4 |SSL_MD5 |SSL_EXP|SSL_SSLV3,
+ SSL_kRSA|SSL_aRSA|SSL_RC4 |SSL_MD5 |SSL_EXP40|SSL_SSLV3,
0,
SSL_ALL_CIP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_kEDH|SSL_aRSA|SSL_DES|SSL_SHA1|SSL_EXP|SSL_SSLV3,
+ SSL_kEDH|SSL_aRSA|SSL_DES|SSL_SHA1|SSL_EXP40|SSL_SSLV3,
0,
SSL_ALL_CIPHERS,
},
SSL_ALL_CIPHERS,
},
+ /* New TLS Export CipherSuites */
+ /* Cipher 60 */
+ {
+ 1,
+ TLS1_TXT_RSA_EXPORT56_WITH_RC4_56_MD5,
+ TLS1_CK_RSA_EXPORT56_WITH_RC4_56_MD5,
+ SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_EXP56|SSL_TLSV1,
+ 0,
+ SSL_ALL_CIPHERS
+ },
+ /* Cipher 61 */
+ {
+ 1,
+ TLS1_TXT_RSA_EXPORT56_WITH_RC2_CBC_56_MD5,
+ TLS1_CK_RSA_EXPORT56_WITH_RC2_CBC_56_MD5,
+ SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5|SSL_EXP56|SSL_TLSV1,
+ 0,
+ SSL_ALL_CIPHERS
+ },
+ /* Cipher 62 */
+ {
+ 1,
+ TLS1_TXT_RSA_EXPORT56_WITH_DES_CBC_SHA,
+ TLS1_CK_RSA_EXPORT56_WITH_DES_CBC_SHA,
+ SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA|SSL_EXP56|SSL_TLSV1,
+ 0,
+ SSL_ALL_CIPHERS
+ },
+
/* end of list */
};
+static SSL3_ENC_METHOD SSLv3_enc_data={
+ ssl3_enc,
+ ssl3_mac,
+ ssl3_setup_key_block,
+ ssl3_generate_master_secret,
+ ssl3_change_cipher_state,
+ ssl3_final_finish_mac,
+ MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
+ ssl3_cert_verify_mac,
+ SSL3_MD_CLIENT_FINISHED_CONST,4,
+ SSL3_MD_SERVER_FINISHED_CONST,4,
+ ssl3_alert_code,
+ };
+
static SSL_METHOD SSLv3_data= {
- 3,
+ SSL3_VERSION,
ssl3_new,
ssl3_clear,
ssl3_free,
ssl3_write,
ssl3_shutdown,
ssl3_renegotiate,
+ ssl3_renegotiate_check,
ssl3_ctrl,
ssl3_ctx_ctrl,
ssl3_get_cipher_by_char,
ssl3_get_cipher,
ssl_bad_method,
ssl3_default_timeout,
+ &SSLv3_enc_data,
};
static long ssl3_default_timeout()
SSL3_CTX *s3;
if ((s3=(SSL3_CTX *)Malloc(sizeof(SSL3_CTX))) == NULL) goto err;
+ memset(s3,0,sizeof(SSL3_CTX));
s->s3=s3;
+ /*
s->s3->tmp.ca_names=NULL;
s->s3->tmp.key_block=NULL;
+ s->s3->tmp.key_block_length=0;
s->s3->rbuf.buf=NULL;
s->s3->wbuf.buf=NULL;
+ */
- ssl3_clear(s);
+ s->method->ssl_clear(s);
return(1);
err:
return(0);
void ssl3_free(s)
SSL *s;
{
+ if(s == NULL)
+ return;
+
ssl3_cleanup_key_block(s);
if (s->s3->rbuf.buf != NULL)
Free(s->s3->rbuf.buf);
if (s->s3->wbuf.buf != NULL)
Free(s->s3->wbuf.buf);
+ if (s->s3->rrec.comp != NULL)
+ Free(s->s3->rrec.comp);
#ifndef NO_DH
if (s->s3->tmp.dh != NULL)
DH_free(s->s3->tmp.dh);
if (s->s3->tmp.ca_names != NULL)
sk_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
+ if (s->s3->rrec.comp != NULL)
+ {
+ Free(s->s3->rrec.comp);
+ s->s3->rrec.comp=NULL;
+ }
+
rp=s->s3->rbuf.buf;
wp=s->s3->wbuf.buf;
memset(s->s3,0,sizeof(SSL3_CTX));
- if (rp != NULL)
- {
- s->packet= &(s->s3->rbuf.buf[0]);
- s->s3->rbuf.buf=rp;
- s->s3->wbuf.buf=wp;
- }
- else
- s->packet=NULL;
+ if (rp != NULL) s->s3->rbuf.buf=rp;
+ if (wp != NULL) s->s3->wbuf.buf=wp;
+
+ ssl_free_wbio_buffer(s);
+
s->packet_length=0;
- s->version=3;
+ 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;
}
long ssl3_ctrl(s,cmd,larg,parg)
long larg;
char *parg;
{
- return(0);
+ int ret=0;
+
+#if !defined(NO_DSA) || !defined(NO_RSA)
+ if (
+#ifndef NO_RSA
+ cmd == SSL_CTRL_SET_TMP_RSA ||
+ cmd == SSL_CTRL_SET_TMP_RSA_CB ||
+#endif
+#ifndef NO_DSA
+ cmd == SSL_CTRL_SET_TMP_DH ||
+ cmd == SSL_CTRL_SET_TMP_DH_CB ||
+#endif
+ 0)
+ {
+ if (!ssl_cert_instantiate(&s->cert, s->ctx->default_cert))
+ {
+ SSLerr(SSL_F_SSL3_CTRL, ERR_R_MALLOC_FAILURE);
+ return(0);
+ }
+ }
+#endif
+
+ switch (cmd)
+ {
+ case SSL_CTRL_GET_SESSION_REUSED:
+ ret=s->hit;
+ break;
+ case SSL_CTRL_GET_CLIENT_CERT_REQUEST:
+ break;
+ case SSL_CTRL_GET_NUM_RENEGOTIATIONS:
+ ret=s->s3->num_renegotiations;
+ break;
+ case SSL_CTRL_CLEAR_NUM_RENEGOTIATIONS:
+ ret=s->s3->num_renegotiations;
+ s->s3->num_renegotiations=0;
+ break;
+ case SSL_CTRL_GET_TOTAL_RENEGOTIATIONS:
+ ret=s->s3->total_renegotiations;
+ break;
+ case SSL_CTRL_GET_FLAGS:
+ ret=(int)(s->s3->flags);
+ break;
+#ifndef NO_RSA
+ case SSL_CTRL_NEED_TMP_RSA:
+ if ((s->cert != NULL) && (s->cert->rsa_tmp == NULL) &&
+ ((s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
+ (EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8))))
+ ret = 1;
+ break;
+ case SSL_CTRL_SET_TMP_RSA:
+ {
+ RSA *rsa = (RSA *)parg;
+ if (rsa == NULL) {
+ SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
+ return(ret);
+ }
+ if ((rsa = RSAPrivateKey_dup(rsa)) == NULL) {
+ SSLerr(SSL_F_SSL3_CTRL, ERR_R_RSA_LIB);
+ return(ret);
+ }
+ if (s->cert->rsa_tmp != NULL)
+ RSA_free(s->cert->rsa_tmp);
+ s->cert->rsa_tmp = rsa;
+ ret = 1;
+ }
+ break;
+ case SSL_CTRL_SET_TMP_RSA_CB:
+#ifndef NOPROTO
+ s->cert->rsa_tmp_cb = (RSA *(*)(SSL *, int, int))parg;
+#else
+ s->cert->rsa_tmp_cb = (RSA *(*)())parg;
+#endif
+ break;
+#endif
+#ifndef NO_DH
+ case SSL_CTRL_SET_TMP_DH:
+ {
+ DH *dh = (DH *)parg;
+ if (dh == NULL) {
+ SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
+ return(ret);
+ }
+ if ((dh = DHparams_dup(dh)) == NULL) {
+ SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
+ return(ret);
+ }
+ if (!DH_generate_key(dh)) {
+ DH_free(dh);
+ SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
+ return(ret);
+ }
+ if (s->cert->dh_tmp != NULL)
+ DH_free(s->cert->dh_tmp);
+ s->cert->dh_tmp = dh;
+ ret = 1;
+ }
+ break;
+ case SSL_CTRL_SET_TMP_DH_CB:
+#ifndef NOPROTO
+ s->cert->dh_tmp_cb = (DH *(*)(SSL *, int, int))parg;
+#else
+ s->cert->dh_tmp_cb = (DH *(*)())parg;
+#endif
+ break;
+#endif
+ default:
+ break;
+ }
+ return(ret);
}
long ssl3_ctx_ctrl(ctx,cmd,larg,parg)
return(1);
else
return(0);
- break;
+ /* break; */
case SSL_CTRL_SET_TMP_RSA:
{
RSA *rsa;
return(1);
}
}
- break;
+ /* break; */
case SSL_CTRL_SET_TMP_RSA_CB:
+#ifndef NOPROTO
+ cert->rsa_tmp_cb=(RSA *(*)(SSL *, int, int))parg;
+#else
cert->rsa_tmp_cb=(RSA *(*)())parg;
+#endif
break;
#endif
#ifndef NO_DH
case SSL_CTRL_SET_TMP_DH:
{
DH *new=NULL,*dh;
+ int rret=0;
dh=(DH *)parg;
if ( ((new=DHparams_dup(dh)) == NULL) ||
{
SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_DH_LIB);
if (new != NULL) DH_free(new);
- return(0);
}
else
{
if (cert->dh_tmp != NULL)
DH_free(cert->dh_tmp);
cert->dh_tmp=new;
- return(1);
+ rret=1;
}
+ return(rret);
}
- break;
+ /*break; */
case SSL_CTRL_SET_TMP_DH_CB:
+#ifndef NOPROTO
+ cert->dh_tmp_cb=(DH *(*)(SSL *, int, int))parg;
+#else
cert->dh_tmp_cb=(DH *(*)())parg;
+#endif
break;
#endif
+ /* A Thawte special :-) */
+ case SSL_CTRL_EXTRA_CHAIN_CERT:
+ if (ctx->extra_certs == NULL)
+ {
+ if ((ctx->extra_certs=sk_new_null()) == NULL)
+ return(0);
+ }
+ sk_push(ctx->extra_certs,(char *)parg);
+ break;
+
default:
return(0);
}
/* This function needs to check if the ciphers required are actually
* available */
SSL_CIPHER *ssl3_get_cipher_by_char(p)
-unsigned char *p;
+const unsigned char *p;
{
static int init=1;
static SSL_CIPHER *sorted[SSL3_NUM_CIPHERS];
}
int ssl3_put_cipher_by_char(c,p)
-SSL_CIPHER *c;
+const SSL_CIPHER *c;
unsigned char *p;
{
long l;
return(2);
}
-void ssl3_generate_key_block(s,km,num)
-SSL *s;
-unsigned char *km;
-int num;
- {
- MD5_CTX m5;
- SHA_CTX s1;
- unsigned char buf[8],smd[SHA_DIGEST_LENGTH];
- unsigned char c='A';
- int i,j,k;
-
- k=0;
- for (i=0; i<num; i+=MD5_DIGEST_LENGTH)
- {
- k++;
- for (j=0; j<k; j++)
- buf[j]=c;
- c++;
- SHA1_Init( &s1);
- SHA1_Update(&s1,buf,k);
- SHA1_Update(&s1,s->session->master_key,
- s->session->master_key_length);
- SHA1_Update(&s1,s->s3->server_random,SSL3_RANDOM_SIZE);
- SHA1_Update(&s1,s->s3->client_random,SSL3_RANDOM_SIZE);
- SHA1_Final( smd,&s1);
-
- MD5_Init( &m5);
- MD5_Update(&m5,s->session->master_key,
- s->session->master_key_length);
- MD5_Update(&m5,smd,SHA_DIGEST_LENGTH);
- if ((i+MD5_DIGEST_LENGTH) > num)
- {
- MD5_Final(smd,&m5);
- memcpy(km,smd,(num-i));
- }
- else
- MD5_Final(km,&m5);
-
- km+=MD5_DIGEST_LENGTH;
- }
- memset(smd,0,SHA_DIGEST_LENGTH);
- }
-
int ssl3_part_read(s,i)
SSL *s;
int i;
else
cert=s->ctx->default_cert;
- ssl_set_cert_masks(cert);
- mask=cert->mask;
- emask=cert->export_mask;
-
sk_set_cmp_func(pref,ssl_cipher_ptr_id_cmp);
for (i=0; i<sk_num(have); i++)
{
c=(SSL_CIPHER *)sk_value(have,i);
+
+ ssl_set_cert_masks(cert,c);
+ mask=cert->mask;
+ emask=cert->export_mask;
+
alg=c->algorithms&(SSL_MKEY_MASK|SSL_AUTH_MASK);
- if (alg & SSL_EXPORT)
+ if (SSL_IS_EXPORT(c->algorithms))
{
ok=((alg & emask) == alg)?1:0;
#ifdef CIPHER_DEBUG
- printf("%d:[%08lX:%08lX]%s\n",ok,alg,mask,c->name);
+ printf("%d:[%08lX:%08lX]%s (export)\n",ok,alg,mask,c->name);
#endif
}
else
#ifndef NO_DH
if (alg & (SSL_kDHr|SSL_kEDH))
{
-#ifndef NO_RSA
+# ifndef NO_RSA
p[ret++]=SSL3_CT_RSA_FIXED_DH;
-#endif
-#ifndef NO_DSA
+# endif
+# ifndef NO_DSA
p[ret++]=SSL3_CT_DSS_FIXED_DH;
-#endif
+# endif
}
- if (alg & (SSL_kEDH|SSL_kDHd|SSL_kDHr))
+ if ((s->version == SSL3_VERSION) &&
+ (alg & (SSL_kEDH|SSL_kDHd|SSL_kDHr)))
{
-#ifndef NO_RSA
+# ifndef NO_RSA
p[ret++]=SSL3_CT_RSA_EPHEMERAL_DH;
-#endif
-#ifndef NO_DSA
+# endif
+# ifndef NO_DSA
p[ret++]=SSL3_CT_DSS_EPHEMERAL_DH;
-#endif
+# endif
}
#endif /* !NO_DH */
#ifndef NO_RSA
p[ret++]=SSL3_CT_RSA_SIGN;
#endif
-/* p[ret++]=SSL3_CT_DSS_SIGN; */
+#ifndef NO_DSA
+ p[ret++]=SSL3_CT_DSS_SIGN;
+#endif
return(ret);
}
{
s->shutdown|=SSL_SENT_SHUTDOWN;
#if 1
- ssl3_send_alert(s,SSL3_AL_WARNING,SSL3_AD_CLOSE_NOTIFY);
+ ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_CLOSE_NOTIFY);
#endif
/* our shutdown alert has been sent now, and if it still needs
* to be written, s->s3->alert_dispatch will be true */
int len;
{
int ret,n;
- BIO *under;
#if 0
if (s->shutdown & SSL_SEND_SHUTDOWN)
return(0);
}
#endif
- errno=0;
+ clear_sys_error();
+ if (s->s3->renegotiate) ssl3_renegotiate_check(s);
/* This is an experimental flag that sends the
* last handshake message in the same packet as the first
if (n <= 0) return(n);
s->rwstate=SSL_NOTHING;
- /* We have flushed the buffer */
- under=BIO_pop(s->wbio);
- s->wbio=under;
- BIO_free(s->bbio);
- s->bbio=NULL;
+ /* We have flushed the buffer, so remove it */
+ ssl_free_wbio_buffer(s);
+ s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
+
ret=s->s3->delay_buf_pop_ret;
s->s3->delay_buf_pop_ret=0;
-
- s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
}
else
{
(char *)buf,len);
if (ret <= 0) return(ret);
}
+
return(ret);
}
char *buf;
int len;
{
- errno=0;
- return(ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len));
+ int ret;
+
+ clear_sys_error();
+ if (s->s3->renegotiate) ssl3_renegotiate_check(s);
+ s->s3->in_read_app_data=1;
+ ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len);
+ if ((ret == -1) && (s->s3->in_read_app_data == 0))
+ {
+ ERR_get_error(); /* clear the error */
+ s->s3->in_read_app_data=0;
+ s->in_handshake++;
+ ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len);
+ s->in_handshake--;
+ }
+ else
+ s->s3->in_read_app_data=0;
+
+ return(ret);
}
int ssl3_peek(s,buf,len)
rr= &(s->s3->rrec);
if ((rr->length == 0) || (rr->type != SSL3_RT_APPLICATION_DATA))
- return(0);
+ {
+ n=ssl3_read(s,buf,1);
+ if (n <= 0) return(n);
+ rr->length++;
+ rr->off--;
+ }
if ((unsigned int)len > rr->length)
n=rr->length;
if (s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)
return(0);
- if (!SSL_in_init(s))
- s->state=SSL_ST_RENEGOTIATE;
+ s->s3->renegotiate=1;
return(1);
}
+int ssl3_renegotiate_check(s)
+SSL *s;
+ {
+ int ret=0;
+
+ if (s->s3->renegotiate)
+ {
+ if ( (s->s3->rbuf.left == 0) &&
+ (s->s3->wbuf.left == 0) &&
+ !SSL_in_init(s))
+ {
+/*
+if we are the server, and we have sent a 'RENEGOTIATE' message, we
+need to go to SSL_ST_ACCEPT.\1e
+*/
+ /* SSL_ST_ACCEPT */
+ s->state=SSL_ST_RENEGOTIATE;
+ s->s3->renegotiate=0;
+ s->s3->num_renegotiations++;
+ s->s3->total_renegotiations++;
+ ret=1;
+ }
+ }
+ return(ret);
+ }
+