if (!(flags & PKCS7_NOVERIFY)) for (i = 0; i < sk_X509_num(signers); i++) { signer = sk_X509_value (signers, i);
if (!(flags & PKCS7_NOVERIFY)) for (k = 0; k < sk_X509_num(signers); k++) { signer = sk_X509_value (signers, k);
int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,					BIO *indata, BIO *out, int flags){	STACK_OF(X509) *signers;	X509 *signer;	STACK_OF(PKCS7_SIGNER_INFO) *sinfos;	PKCS7_SIGNER_INFO *si;	X509_STORE_CTX cert_ctx;	char buf[4096];	int i, j=0;	BIO *p7bio;	BIO *tmpout;	if(!p7) {		PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_INVALID_NULL_POINTER);		return 0;	}	if(!PKCS7_type_is_signed(p7)) {		PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_WRONG_CONTENT_TYPE);		return 0;	}	/* Check for no data and no content: no data to verify signature */	if(PKCS7_get_detached(p7) && !indata) {		PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_NO_CONTENT);		return 0;	}	/* Check for data and content: two sets of data */	if(!PKCS7_get_detached(p7) && indata) {				PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_CONTENT_AND_DATA_PRESENT);		return 0;	}	sinfos = PKCS7_get_signer_info(p7);	if(!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) {		PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_NO_SIGNATURES_ON_DATA);		return 0;	}	signers = PKCS7_get0_signers(p7, certs, flags);	if(!signers) return 0;	/* Now verify the certificates */	if (!(flags & PKCS7_NOVERIFY)) for (i = 0; i < sk_X509_num(signers); i++) {		signer = sk_X509_value (signers, i);		if (!(flags & PKCS7_NOCHAIN)) {			X509_STORE_CTX_init(&cert_ctx, store, signer,							p7->d.sign->cert);			X509_STORE_CTX_set_purpose(&cert_ctx,						X509_PURPOSE_SMIME_SIGN);		} else X509_STORE_CTX_init (&cert_ctx, store, signer, NULL);		i = X509_verify_cert(&cert_ctx);		if (i <= 0) j = X509_STORE_CTX_get_error(&cert_ctx);		X509_STORE_CTX_cleanup(&cert_ctx);		if (i <= 0) {			PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_CERTIFICATE_VERIFY_ERROR);			ERR_add_error_data(2, "Verify error:",					 X509_verify_cert_error_string(j));			sk_X509_free(signers);			return 0;		}		/* Check for revocation status here */	}	p7bio=PKCS7_dataInit(p7,indata);	if(flags & PKCS7_TEXT) {		if(!(tmpout = BIO_new(BIO_s_mem()))) {			PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_MALLOC_FAILURE);			goto err;		}	} else tmpout = out;	/* We now have to 'read' from p7bio to calculate digests etc. */	for (;;)	{		i=BIO_read(p7bio,buf,sizeof(buf));		if (i <= 0) break;		if (tmpout) BIO_write(tmpout, buf, i);	}	if(flags & PKCS7_TEXT) {		if(!SMIME_text(tmpout, out)) {			PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_SMIME_TEXT_ERROR);			BIO_free(tmpout);			goto err;		}		BIO_free(tmpout);	}	/* Now Verify All Signatures */	if (!(flags & PKCS7_NOSIGS))	    for (i=0; i<sk_PKCS7_SIGNER_INFO_num(sinfos); i++)		{		si=sk_PKCS7_SIGNER_INFO_value(sinfos,i);		signer = sk_X509_value (signers, i);		j=PKCS7_signatureVerify(p7bio,p7,si, signer);		if (j <= 0) {			PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_SIGNATURE_FAILURE);			goto err;		}	}	sk_X509_free(signers);	if(indata) BIO_pop(p7bio);	BIO_free_all(p7bio);	return 1;	err:	sk_X509_free(signers);	BIO_free(p7bio);	return 0;}