RAND_bytes(key,keylen);
BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio)	{	int i,j;	BIO *out=NULL,*btmp=NULL;	X509_ALGOR *xa;	const EVP_MD *evp_md;	const EVP_CIPHER *evp_cipher=NULL;	STACK *md_sk=NULL,*rsk=NULL;	X509_ALGOR *xalg=NULL;	PKCS7_RECIP_INFO *ri=NULL;	EVP_PKEY *pkey;	i=OBJ_obj2nid(p7->type);	p7->state=PKCS7_S_HEADER;	switch (i)		{	case NID_pkcs7_signed:		md_sk=p7->d.sign->md_algs;		break;	case NID_pkcs7_signedAndEnveloped:		rsk=p7->d.signed_and_enveloped->recipientinfo;		md_sk=p7->d.signed_and_enveloped->md_algs;		evp_cipher=EVP_get_cipherbyname(OBJ_nid2sn(OBJ_obj2nid(p7->d.signed_and_enveloped->enc_data->algorithm->algorithm)));		if (evp_cipher == NULL)			{			PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNSUPPORTED_CIPHER_TYPE);			goto err;			}		xalg=p7->d.signed_and_enveloped->enc_data->algorithm;		break;	case NID_pkcs7_enveloped:		rsk=p7->d.enveloped->recipientinfo;		evp_cipher=EVP_get_cipherbyname(OBJ_nid2sn(OBJ_obj2nid(p7->d.enveloped->enc_data->algorithm->algorithm)));		if (evp_cipher == NULL)			{			PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNSUPPORTED_CIPHER_TYPE);			goto err;			}		xalg=p7->d.enveloped->enc_data->algorithm;		break;	default:		PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNSUPPORTED_CONTENT_TYPE);	        goto err;		}	if (md_sk != NULL)		{		for (i=0; i<sk_num(md_sk); i++)			{			xa=(X509_ALGOR *)sk_value(md_sk,i);			if ((btmp=BIO_new(BIO_f_md())) == NULL)				{				PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_BIO_LIB);				goto err;				}			j=OBJ_obj2nid(xa->algorithm);			evp_md=EVP_get_digestbyname(OBJ_nid2sn(j));			if (evp_md == NULL)				{				PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNKNOWN_DIGEST_TYPE);				goto err;				}			BIO_set_md(btmp,evp_md);			if (out == NULL)				out=btmp;			else				BIO_push(out,btmp);			btmp=NULL;			}		}	if (evp_cipher != NULL)		{		unsigned char key[EVP_MAX_KEY_LENGTH];		unsigned char iv[EVP_MAX_IV_LENGTH];		int keylen,ivlen;		int jj,max;		unsigned char *tmp;		if ((btmp=BIO_new(BIO_f_cipher())) == NULL)			{			PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_BIO_LIB);			goto err;			}		keylen=EVP_CIPHER_key_length(evp_cipher);		ivlen=EVP_CIPHER_iv_length(evp_cipher);		if (ivlen > 0) {			EVP_CIPHER_CTX *ctx;			BIO_get_cipher_ctx(btmp, &ctx);			if (xalg->parameter == NULL) 						xalg->parameter=ASN1_TYPE_new();			if(EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) < 0)								       goto err;		}		RAND_bytes(key,keylen);		/* Lets do the pub key stuff :-) */		max=0;		for (i=0; i<sk_num(rsk); i++)			{			ri=(PKCS7_RECIP_INFO *)sk_value(rsk,i);			if (ri->cert == NULL)				{				PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_MISSING_CERIPEND_INFO);				goto err;				}			pkey=X509_get_pubkey(ri->cert);			jj=EVP_PKEY_size(pkey);			EVP_PKEY_free(pkey);			if (max < jj) max=jj;			}		if ((tmp=(unsigned char *)Malloc(max)) == NULL)			{			PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_MALLOC_FAILURE);			goto err;			}		for (i=0; i<sk_num(rsk); i++)			{			ri=(PKCS7_RECIP_INFO *)sk_value(rsk,i);			pkey=X509_get_pubkey(ri->cert);			jj=EVP_PKEY_encrypt(tmp,key,keylen,pkey);			EVP_PKEY_free(pkey);			if (jj <= 0)				{				PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_EVP_LIB);				Free(tmp);				goto err;				}			ASN1_OCTET_STRING_set(ri->enc_key,tmp,jj);			}		Free(tmp);		BIO_set_cipher(btmp,evp_cipher,key,iv,1);		if (out == NULL)			out=btmp;		else			BIO_push(out,btmp);		btmp=NULL;		}	if (bio == NULL) /* ??????????? */		{		if (p7->detached)			bio=BIO_new(BIO_s_null());		else			{			bio=BIO_new(BIO_s_mem());			/* We need to set this so that when we have read all			 * the data, the encrypt BIO, if present, will read			 * EOF and encode the last few bytes */			BIO_set_mem_eof_return(bio,0);			if (PKCS7_type_is_signed(p7) &&				PKCS7_type_is_data(p7->d.sign->contents))				{				ASN1_OCTET_STRING *os;				os=p7->d.sign->contents->d.data;				if (os->length > 0)					BIO_write(bio,(char *)os->data,						os->length);				}			}		}	BIO_push(out,bio);	bio=NULL;	if (0)		{err:		if (out != NULL)			BIO_free_all(out);		if (btmp != NULL)			BIO_free_all(btmp);		out=NULL;		}	return(out);	}