if ((s->enc_read_ctx == NULL) && ((s->enc_read_ctx=(EVP_CIPHER_CTX *) OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL))
if (s->enc_read_ctx != NULL) reuse_dd = 1; else if ((s->enc_read_ctx=OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL)
int ssl3_change_cipher_state(SSL *s, int which)	{	unsigned char *p,*key_block,*mac_secret;	unsigned char exp_key[EVP_MAX_KEY_LENGTH];	unsigned char exp_iv[EVP_MAX_KEY_LENGTH];	unsigned char *ms,*key,*iv,*er1,*er2;	EVP_CIPHER_CTX *dd;	const EVP_CIPHER *c;	COMP_METHOD *comp;	const EVP_MD *m;	EVP_MD_CTX md;	int exp,n,i,j,k,cl;	exp=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);	c=s->s3->tmp.new_sym_enc;	m=s->s3->tmp.new_hash;	if (s->s3->tmp.new_compression == NULL)		comp=NULL;	else		comp=s->s3->tmp.new_compression->method;	key_block=s->s3->tmp.key_block;	if (which & SSL3_CC_READ)		{		if ((s->enc_read_ctx == NULL) &&			((s->enc_read_ctx=(EVP_CIPHER_CTX *)			OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL))			goto err;		dd= s->enc_read_ctx;		s->read_hash=m;		/* COMPRESS */		if (s->expand != NULL)			{			COMP_CTX_free(s->expand);			s->expand=NULL;			}		if (comp != NULL)			{			s->expand=COMP_CTX_new(comp);			if (s->expand == NULL)				{				SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);				goto err2;				}			if (s->s3->rrec.comp == NULL)				s->s3->rrec.comp=(unsigned char *)					OPENSSL_malloc(SSL3_RT_MAX_PLAIN_LENGTH);			if (s->s3->rrec.comp == NULL)				goto err;			}		memset(&(s->s3->read_sequence[0]),0,8);		mac_secret= &(s->s3->read_mac_secret[0]);		}	else		{		if ((s->enc_write_ctx == NULL) &&			((s->enc_write_ctx=(EVP_CIPHER_CTX *)			OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL))			goto err;		dd= s->enc_write_ctx;		s->write_hash=m;		/* COMPRESS */		if (s->compress != NULL)			{			COMP_CTX_free(s->compress);			s->compress=NULL;			}		if (comp != NULL)			{			s->compress=COMP_CTX_new(comp);			if (s->compress == NULL)				{				SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);				goto err2;				}			}		memset(&(s->s3->write_sequence[0]),0,8);		mac_secret= &(s->s3->write_mac_secret[0]);		}	EVP_CIPHER_CTX_init(dd);	p=s->s3->tmp.key_block;	i=EVP_MD_size(m);	cl=EVP_CIPHER_key_length(c);	j=exp ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ?		 cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl;	/* Was j=(exp)?5:EVP_CIPHER_key_length(c); */	k=EVP_CIPHER_iv_length(c);	if (	(which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||		(which == SSL3_CHANGE_CIPHER_SERVER_READ))		{		ms=  &(p[ 0]); n=i+i;		key= &(p[ n]); n+=j+j;		iv=  &(p[ n]); n+=k+k;		er1= &(s->s3->client_random[0]);		er2= &(s->s3->server_random[0]);		}	else		{		n=i;		ms=  &(p[ n]); n+=i+j;		key= &(p[ n]); n+=j+k;		iv=  &(p[ n]); n+=k;		er1= &(s->s3->server_random[0]);		er2= &(s->s3->client_random[0]);		}	if (n > s->s3->tmp.key_block_length)		{		SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE,ERR_R_INTERNAL_ERROR);		goto err2;		}	EVP_MD_CTX_init(&md);	memcpy(mac_secret,ms,i);	if (exp)		{		/* In here I set both the read and write key/iv to the		 * same value since only the correct one will be used :-).		 */		EVP_DigestInit_ex(&md,EVP_md5(), NULL);		EVP_DigestUpdate(&md,key,j);		EVP_DigestUpdate(&md,er1,SSL3_RANDOM_SIZE);		EVP_DigestUpdate(&md,er2,SSL3_RANDOM_SIZE);		EVP_DigestFinal_ex(&md,&(exp_key[0]),NULL);		key= &(exp_key[0]);		if (k > 0)			{			EVP_DigestInit_ex(&md,EVP_md5(), NULL);			EVP_DigestUpdate(&md,er1,SSL3_RANDOM_SIZE);			EVP_DigestUpdate(&md,er2,SSL3_RANDOM_SIZE);			EVP_DigestFinal_ex(&md,&(exp_iv[0]),NULL);			iv= &(exp_iv[0]);			}		}	s->session->key_arg_length=0;	EVP_CipherInit_ex(dd,c,NULL,key,iv,(which & SSL3_CC_WRITE));	memset(&(exp_key[0]),0,sizeof(exp_key));	memset(&(exp_iv[0]),0,sizeof(exp_iv));	EVP_MD_CTX_cleanup(&md);	return(1);err:	SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE,ERR_R_MALLOC_FAILURE);err2:	return(0);	}