⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cms_sd.c

📁 OpenSSL 0.9.8k 最新版OpenSSL
💻 C
📖 第 1 页 / 共 2 页
字号:
	}STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)	{	STACK_OF(X509) *signers = NULL;	STACK_OF(CMS_SignerInfo) *sinfos;	CMS_SignerInfo *si;	int i;	sinfos = CMS_get0_SignerInfos(cms);	for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)		{		si = sk_CMS_SignerInfo_value(sinfos, i);		if (si->signer)			{			if (!signers)				{				signers = sk_X509_new_null();				if (!signers)					return NULL;				}			if (!sk_X509_push(signers, si->signer))				{				sk_X509_free(signers);				return NULL;				}			}		}	return signers;	}void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)	{	if (signer)		{		CRYPTO_add(&signer->references, 1, CRYPTO_LOCK_X509);		if (si->pkey)			EVP_PKEY_free(si->pkey);		si->pkey = X509_get_pubkey(signer);		}	if (si->signer)		X509_free(si->signer);	si->signer = signer;	}int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,					ASN1_OCTET_STRING **keyid,					X509_NAME **issuer, ASN1_INTEGER **sno)	{	return cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);	}int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)	{	return cms_SignerIdentifier_cert_cmp(si->sid, cert);	}int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,				unsigned int flags)	{	CMS_SignedData *sd;	CMS_SignerInfo *si;	CMS_CertificateChoices *cch;	STACK_OF(CMS_CertificateChoices) *certs;	X509 *x;	int i, j;	int ret = 0;	sd = cms_get0_signed(cms);	if (!sd)		return -1;	certs = sd->certificates;	for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++)		{		si = sk_CMS_SignerInfo_value(sd->signerInfos, i);		if (si->signer)			continue;		for (j = 0; j < sk_X509_num(scerts); j++)			{			x = sk_X509_value(scerts, j);			if (CMS_SignerInfo_cert_cmp(si, x) == 0)				{				CMS_SignerInfo_set1_signer_cert(si, x);				ret++;				break;				}			}		if (si->signer || (flags & CMS_NOINTERN))			continue;		for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++)			{			cch = sk_CMS_CertificateChoices_value(certs, j);			if (cch->type != 0)				continue;			x = cch->d.certificate;			if (CMS_SignerInfo_cert_cmp(si, x) == 0)				{				CMS_SignerInfo_set1_signer_cert(si, x);				ret++;				break;				}			}		}	return ret;	}void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk, X509 **signer,					X509_ALGOR **pdig, X509_ALGOR **psig)	{	if (pk)		*pk = si->pkey;	if (signer)		*signer = si->signer;	if (pdig)		*pdig = si->digestAlgorithm;	if (psig)		*psig = si->signatureAlgorithm;	}/* In OpenSSL 0.9.8 we have the link between digest types and public * key types so we need to fixup the digest type if the public key * type is not appropriate. */static void cms_fixup_mctx(EVP_MD_CTX *mctx, EVP_PKEY *pkey)	{	if (EVP_MD_CTX_type(mctx) != NID_sha1)		return;#ifndef OPENSSL_NO_DSA	if (pkey->type == EVP_PKEY_DSA)		mctx->digest = EVP_dss1();	#endif#ifndef OPENSSL_NO_ECDSA	if (pkey->type == EVP_PKEY_EC)		mctx->digest = EVP_ecdsa();	#endif	}static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,					CMS_SignerInfo *si, BIO *chain)	{	EVP_MD_CTX mctx;	int r = 0;	EVP_MD_CTX_init(&mctx);	if (!si->pkey)		{		CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);		return 0;		}	if (!cms_DigestAlgorithm_find_ctx(&mctx, chain, si->digestAlgorithm))		goto err;	/* If any signed attributes calculate and add messageDigest attribute */	if (CMS_signed_get_attr_count(si) >= 0)		{		ASN1_OBJECT *ctype =			cms->d.signedData->encapContentInfo->eContentType; 		unsigned char md[EVP_MAX_MD_SIZE];		unsigned int mdlen;		EVP_DigestFinal_ex(&mctx, md, &mdlen);		if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,						V_ASN1_OCTET_STRING,						md, mdlen))			goto err;		/* Copy content type across */		if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,					V_ASN1_OBJECT, ctype, -1) <= 0)			goto err;		if (!CMS_SignerInfo_sign(si))			goto err;		}	else		{		unsigned char *sig;		unsigned int siglen;		sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey));		if (!sig)			{			CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN,					ERR_R_MALLOC_FAILURE);			goto err;			}		cms_fixup_mctx(&mctx, si->pkey);		if (!EVP_SignFinal(&mctx, sig, &siglen, si->pkey))			{			CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN,					CMS_R_SIGNFINAL_ERROR);			OPENSSL_free(sig);			goto err;			}		ASN1_STRING_set0(si->signature, sig, siglen);		}	r = 1;	err:	EVP_MD_CTX_cleanup(&mctx);	return r;	}int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain)	{	STACK_OF(CMS_SignerInfo) *sinfos;	CMS_SignerInfo *si;	int i;	sinfos = CMS_get0_SignerInfos(cms);	for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)		{		si = sk_CMS_SignerInfo_value(sinfos, i);		if (!cms_SignerInfo_content_sign(cms, si, chain))			return 0;		}	cms->d.signedData->encapContentInfo->partial = 0;	return 1;	}int CMS_SignerInfo_sign(CMS_SignerInfo *si)	{	EVP_MD_CTX mctx;	unsigned char *abuf = NULL;	int alen;	unsigned int siglen;	const EVP_MD *md = NULL;	md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);	if (md == NULL)		return 0;	EVP_MD_CTX_init(&mctx);	if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0)		{		if (!cms_add1_signingTime(si, NULL))			goto err;		}	if (EVP_SignInit_ex(&mctx, md, NULL) <= 0)		goto err;#if 0	if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,				EVP_PKEY_CTRL_CMS_SIGN, 0, si) <= 0)		{		CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);		goto err;		}#endif	alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs,&abuf,				ASN1_ITEM_rptr(CMS_Attributes_Sign));	if(!abuf)		goto err;	if (EVP_SignUpdate(&mctx, abuf, alen) <= 0)		goto err;	siglen = EVP_PKEY_size(si->pkey);	OPENSSL_free(abuf);	abuf = OPENSSL_malloc(siglen);	if(!abuf)		goto err;	cms_fixup_mctx(&mctx, si->pkey);	if (EVP_SignFinal(&mctx, abuf, &siglen, si->pkey) <= 0)		goto err;#if 0	if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,				EVP_PKEY_CTRL_CMS_SIGN, 1, si) <= 0)		{		CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);		goto err;		}#endif	EVP_MD_CTX_cleanup(&mctx);	ASN1_STRING_set0(si->signature, abuf, siglen);	return 1;	err:	if (abuf)		OPENSSL_free(abuf);	EVP_MD_CTX_cleanup(&mctx);	return 0;	}int CMS_SignerInfo_verify(CMS_SignerInfo *si)	{	EVP_MD_CTX mctx;	unsigned char *abuf = NULL;	int alen, r = -1;	const EVP_MD *md = NULL;	if (!si->pkey)		{		CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_NO_PUBLIC_KEY);		return -1;		}	md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);	if (md == NULL)		return -1;	EVP_MD_CTX_init(&mctx);	if (EVP_VerifyInit_ex(&mctx, md, NULL) <= 0)		goto err;	alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs,&abuf,				ASN1_ITEM_rptr(CMS_Attributes_Verify));	if(!abuf)		goto err;	r = EVP_VerifyUpdate(&mctx, abuf, alen);	OPENSSL_free(abuf);	if (r <= 0)		{		r = -1;		goto err;		}	cms_fixup_mctx(&mctx, si->pkey);	r = EVP_VerifyFinal(&mctx,			si->signature->data, si->signature->length, si->pkey);	if (r <= 0)		CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE);	err:	EVP_MD_CTX_cleanup(&mctx);	return r;	}/* Create a chain of digest BIOs from a CMS ContentInfo */BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms)	{	int i;	CMS_SignedData *sd;	BIO *chain = NULL;	sd = cms_get0_signed(cms);	if (!sd)		return NULL;	if (cms->d.signedData->encapContentInfo->partial)		cms_sd_set_version(sd);	for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++)		{		X509_ALGOR *digestAlgorithm;		BIO *mdbio;		digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);		mdbio = cms_DigestAlgorithm_init_bio(digestAlgorithm);		if (!mdbio)			goto err;			if (chain)			 BIO_push(chain, mdbio);		else			chain = mdbio;		}	return chain;	err:	if (chain)		BIO_free_all(chain);	return NULL;	}int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)	{	ASN1_OCTET_STRING *os = NULL;	EVP_MD_CTX mctx;	int r = -1;	EVP_MD_CTX_init(&mctx);	/* If we have any signed attributes look for messageDigest value */	if (CMS_signed_get_attr_count(si) >= 0)		{		os = CMS_signed_get0_data_by_OBJ(si,					OBJ_nid2obj(NID_pkcs9_messageDigest),					-3, V_ASN1_OCTET_STRING);		if (!os)			{			CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,				CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);			goto err;			}		}	if (!cms_DigestAlgorithm_find_ctx(&mctx, chain, si->digestAlgorithm))		goto err;	/* If messageDigest found compare it */	if (os)		{		unsigned char mval[EVP_MAX_MD_SIZE];		unsigned int mlen;		if (EVP_DigestFinal_ex(&mctx, mval, &mlen) <= 0)			{			CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,				CMS_R_UNABLE_TO_FINALIZE_CONTEXT);			goto err;			}		if (mlen != (unsigned int)os->length)			{			CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,				CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);			goto err;			}		if (memcmp(mval, os->data, mlen))			{			CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,				CMS_R_VERIFICATION_FAILURE);			r = 0;			}		else			r = 1;		}	else		{		cms_fixup_mctx(&mctx, si->pkey);		r = EVP_VerifyFinal(&mctx, si->signature->data,					si->signature->length, si->pkey);		if (r <= 0)			{			CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,				CMS_R_VERIFICATION_FAILURE);			r = 0;			}		}	err:	EVP_MD_CTX_cleanup(&mctx);	return r;	}int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)	{	unsigned char *smder = NULL;	int smderlen, r;	smderlen = i2d_X509_ALGORS(algs, &smder);	if (smderlen <= 0)		return 0;	r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,					V_ASN1_SEQUENCE, smder, smderlen);	OPENSSL_free(smder);	return r;	}int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,				int algnid, int keysize)	{	X509_ALGOR *alg;	ASN1_INTEGER *key = NULL;	if (keysize > 0)		{		key = ASN1_INTEGER_new();		if (!key || !ASN1_INTEGER_set(key, keysize))			return 0;		}	alg = X509_ALGOR_new();	if (!alg)		{		if (key)			ASN1_INTEGER_free(key);		return 0;		}			X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),				key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);	if (!*algs)		*algs = sk_X509_ALGOR_new_null();	if (!*algs || !sk_X509_ALGOR_push(*algs, alg))		{		X509_ALGOR_free(alg);		return 0;		}	return 1;	}/* Check to see if a cipher exists and if so add S/MIME capabilities */static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)	{	if (EVP_get_cipherbynid(nid))		return CMS_add_simple_smimecap(sk, nid, arg);	return 1;	}#if 0static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)	{	if (EVP_get_digestbynid(nid))		return CMS_add_simple_smimecap(sk, nid, arg);	return 1;	}#endifint CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)	{	if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)		|| !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)		|| !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)		|| !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)		|| !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)		|| !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)		|| !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)		|| !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))		return 0;	return 1;	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -