j=_exp ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ? cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl;
j=is_export ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ? cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl;
int tls1_change_cipher_state(SSL *s, int which)	{	static const unsigned char empty[]="";	unsigned char *p,*key_block,*mac_secret;	unsigned char *exp_label,buf[TLS_MD_MAX_CONST_SIZE+		SSL3_RANDOM_SIZE*2];	unsigned char tmp1[EVP_MAX_KEY_LENGTH];	unsigned char tmp2[EVP_MAX_KEY_LENGTH];	unsigned char iv1[EVP_MAX_IV_LENGTH*2];	unsigned char iv2[EVP_MAX_IV_LENGTH*2];	unsigned char *ms,*key,*iv,*er1,*er2;	int client_write;	EVP_CIPHER_CTX *dd;	const EVP_CIPHER *c;	const SSL_COMP *comp;	const EVP_MD *m;	int _exp,n,i,j,k,exp_label_len,cl;	_exp=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);	c=s->s3->tmp.new_sym_enc;	m=s->s3->tmp.new_hash;	comp=s->s3->tmp.new_compression;	key_block=s->s3->tmp.key_block;#ifdef KSSL_DEBUG	printf("tls1_change_cipher_state(which= %d) w/\n", which);	printf("\talg= %ld, comp= %p\n", s->s3->tmp.new_cipher->algorithms,                comp);	printf("\tevp_cipher == %p ==? &d_cbc_ede_cipher3\n", c);	printf("\tevp_cipher: nid, blksz= %d, %d, keylen=%d, ivlen=%d\n",                c->nid,c->block_size,c->key_len,c->iv_len);	printf("\tkey_block: len= %d, data= ", s->s3->tmp.key_block_length);	{        int i;        for (i=0; i<s->s3->tmp.key_block_length; i++)		printf("%02x", key_block[i]);  printf("\n");        }#endif	/* KSSL_DEBUG */	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;		if (s->expand != NULL)			{			COMP_CTX_free(s->expand);			s->expand=NULL;			}		if (comp != NULL)			{			s->expand=COMP_CTX_new(comp->method);			if (s->expand == NULL)				{				SSLerr(SSL_F_TLS1_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_ENCRYPTED_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;		if (s->compress != NULL)			{			COMP_CTX_free(s->compress);			s->compress=NULL;			}		if (comp != NULL)			{			s->compress=COMP_CTX_new(comp->method);			if (s->compress == NULL)				{				SSLerr(SSL_F_TLS1_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);	er1= &(s->s3->client_random[0]);	er2= &(s->s3->server_random[0]);	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;		exp_label=(unsigned char *)TLS_MD_CLIENT_WRITE_KEY_CONST;		exp_label_len=TLS_MD_CLIENT_WRITE_KEY_CONST_SIZE;		client_write=1;		}	else		{		n=i;		ms=  &(p[ n]); n+=i+j;		key= &(p[ n]); n+=j+k;		iv=  &(p[ n]); n+=k;		exp_label=(unsigned char *)TLS_MD_SERVER_WRITE_KEY_CONST;		exp_label_len=TLS_MD_SERVER_WRITE_KEY_CONST_SIZE;		client_write=0;		}	if (n > s->s3->tmp.key_block_length)		{		SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,ERR_R_INTERNAL_ERROR);		goto err2;		}	memcpy(mac_secret,ms,i);#ifdef TLS_DEBUGprintf("which = %04X\nmac key=",which);{ int z; for (z=0; z<i; z++) printf("%02X%c",ms[z],((z+1)%16)?' ':'\n'); }#endif	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 :-).		 */		p=buf;		memcpy(p,exp_label,exp_label_len);		p+=exp_label_len;		memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);		p+=SSL3_RANDOM_SIZE;		memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);		p+=SSL3_RANDOM_SIZE;		tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(p-buf),key,j,			 tmp1,tmp2,EVP_CIPHER_key_length(c));		key=tmp1;		if (k > 0)			{			p=buf;			memcpy(p,TLS_MD_IV_BLOCK_CONST,				TLS_MD_IV_BLOCK_CONST_SIZE);			p+=TLS_MD_IV_BLOCK_CONST_SIZE;			memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);			p+=SSL3_RANDOM_SIZE;			memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);			p+=SSL3_RANDOM_SIZE;			tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,p-buf,empty,0,				 iv1,iv2,k*2);			if (client_write)				iv=iv1;			else				iv= &(iv1[k]);			}		}	s->session->key_arg_length=0;#ifdef KSSL_DEBUG	{        int i;	printf("EVP_CipherInit_ex(dd,c,key=,iv=,which)\n");	printf("\tkey= "); for (i=0; i<c->key_len; i++) printf("%02x", key[i]);	printf("\n");	printf("\t iv= "); for (i=0; i<c->iv_len; i++) printf("%02x", iv[i]);	printf("\n");	}#endif	/* KSSL_DEBUG */	EVP_CipherInit_ex(dd,c,NULL,key,iv,(which & SSL3_CC_WRITE));#ifdef TLS_DEBUGprintf("which = %04X\nkey=",which);{ int z; for (z=0; z<EVP_CIPHER_key_length(c); z++) printf("%02X%c",key[z],((z+1)%16)?' ':'\n'); }printf("\niv=");{ int z; for (z=0; z<k; z++) printf("%02X%c",iv[z],((z+1)%16)?' ':'\n'); }printf("\n");#endif	memset(tmp1,0,sizeof(tmp1));	memset(tmp2,0,sizeof(tmp1));	memset(iv1,0,sizeof(iv1));	memset(iv2,0,sizeof(iv2));	return(1);err:	SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,ERR_R_MALLOC_FAILURE);err2:	return(0);	}