if (p7->detached) ASN1_OCTET_STRING_set(os,(unsigned char *)"",0); else
if (!p7->detached)
int PKCS7_dataFinal(p7,bio)PKCS7 *p7;BIO *bio;	{	int ret=0;	int i,j;	BIO *btmp;	BUF_MEM *buf_mem=NULL;	BUF_MEM *buf=NULL;	PKCS7_SIGNER_INFO *si;	EVP_MD_CTX *mdc,ctx_tmp;	STACK *sk,*si_sk=NULL;	unsigned char *p,*pp=NULL;	int x;	ASN1_OCTET_STRING *os=NULL;	i=OBJ_obj2nid(p7->type);	p7->state=PKCS7_S_HEADER;	switch (i)		{	case NID_pkcs7_signedAndEnveloped:		/* XXXXXXXXXXXXXXXX */		si_sk=p7->d.signed_and_enveloped->signer_info;		os=ASN1_OCTET_STRING_new();		p7->d.signed_and_enveloped->enc_data->enc_data=os;		break;	case NID_pkcs7_enveloped:		/* XXXXXXXXXXXXXXXX */		os=ASN1_OCTET_STRING_new();		p7->d.enveloped->enc_data->enc_data=os;		break;	case NID_pkcs7_signed:		si_sk=p7->d.sign->signer_info;		os=p7->d.sign->contents->d.data;		break;		}	if (si_sk != NULL)		{		if ((buf=BUF_MEM_new()) == NULL)			{			PKCS7err(PKCS7_F_PKCS7_DATASIGN,ERR_R_BIO_LIB);			goto err;			}		for (i=0; i<sk_num(si_sk); i++)			{			si=(PKCS7_SIGNER_INFO *)				sk_value(si_sk,i);			if (si->pkey == NULL) continue;			j=OBJ_obj2nid(si->digest_alg->algorithm);			btmp=bio;			for (;;)				{				if ((btmp=BIO_find_type(btmp,BIO_TYPE_MD)) 					== NULL)					{					PKCS7err(PKCS7_F_PKCS7_DATASIGN,PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);					goto err;					}				BIO_get_md_ctx(btmp,&mdc);				if (mdc == NULL)					{					PKCS7err(PKCS7_F_PKCS7_DATASIGN,PKCS7_R_INTERNAL_ERROR);					goto err;					}				if (EVP_MD_type(EVP_MD_CTX_type(mdc)) == j)					break;				else					btmp=btmp->next_bio;				}						/* We now have the EVP_MD_CTX, lets do the			 * signing. */			memcpy(&ctx_tmp,mdc,sizeof(ctx_tmp));			if (!BUF_MEM_grow(buf,EVP_PKEY_size(si->pkey)))				{				PKCS7err(PKCS7_F_PKCS7_DATASIGN,ERR_R_BIO_LIB);				goto err;				}			sk=si->auth_attr;			/* If there are attributes, we add the digest			 * attribute and only sign the attributes */			if ((sk != NULL) && (sk_num(sk) != 0))				{				unsigned char md_data[EVP_MAX_MD_SIZE];				unsigned int md_len;				ASN1_OCTET_STRING *digest;				ASN1_UTCTIME *sign_time;				EVP_MD *md_tmp;				/* Add signing time */				sign_time=X509_gmtime_adj(NULL,0);				PKCS7_add_signed_attribute(si,					NID_pkcs9_signingTime,					V_ASN1_UTCTIME,(char *)sign_time);				/* Add digest */				md_tmp=EVP_MD_CTX_type(&ctx_tmp);				EVP_DigestFinal(&ctx_tmp,md_data,&md_len);				digest=ASN1_OCTET_STRING_new();				ASN1_OCTET_STRING_set(digest,md_data,md_len);				PKCS7_add_signed_attribute(si,NID_pkcs9_messageDigest,					V_ASN1_OCTET_STRING,(char *)digest);				/* Now sign the mess */				EVP_SignInit(&ctx_tmp,md_tmp);				x=i2d_ASN1_SET(sk,NULL,i2d_X509_ATTRIBUTE,					V_ASN1_SET,V_ASN1_UNIVERSAL, IS_SET);				pp=(unsigned char *)Malloc(x);				p=pp;				i2d_ASN1_SET(sk,&p,i2d_X509_ATTRIBUTE,					V_ASN1_SET,V_ASN1_UNIVERSAL, IS_SET);				EVP_SignUpdate(&ctx_tmp,pp,x);				Free(pp);				pp=NULL;				}			if (si->pkey->type == EVP_PKEY_DSA)				ctx_tmp.digest=EVP_dss1();			if (!EVP_SignFinal(&ctx_tmp,(unsigned char *)buf->data,				(unsigned int *)&buf->length,si->pkey))				{				PKCS7err(PKCS7_F_PKCS7_DATASIGN,ERR_R_EVP_LIB);				goto err;				}			if (!ASN1_STRING_set(si->enc_digest,				(unsigned char *)buf->data,buf->length))				{				PKCS7err(PKCS7_F_PKCS7_DATASIGN,ERR_R_ASN1_LIB);				goto err;				}			}		}	if (p7->detached)		ASN1_OCTET_STRING_set(os,(unsigned char *)"",0);	else		{		btmp=BIO_find_type(bio,BIO_TYPE_MEM);		if (btmp == NULL)			{			PKCS7err(PKCS7_F_PKCS7_DATASIGN,PKCS7_R_UNABLE_TO_FIND_MEM_BIO);			goto err;			}		BIO_get_mem_ptr(btmp,&buf_mem);		ASN1_OCTET_STRING_set(os,			(unsigned char *)buf_mem->data,buf_mem->length);		}	if (pp != NULL) Free(pp);	pp=NULL;	ret=1;err:	if (buf != NULL) BUF_MEM_free(buf);	return(ret);	}