2 * Copyright 2008-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/cryptlib.h"
11 #include <openssl/asn1t.h>
12 #include <openssl/pem.h>
13 #include <openssl/x509.h>
14 #include <openssl/x509v3.h>
15 #include <openssl/err.h>
16 #include <openssl/cms.h>
17 #include <openssl/ess.h>
18 #include "cms_local.h"
19 #include "crypto/asn1.h"
20 #include "crypto/evp.h"
21 #include "crypto/cms.h"
22 #include "crypto/ess.h"
24 DEFINE_STACK_OF(CMS_RevocationInfoChoice)
25 DEFINE_STACK_OF(CMS_SignerInfo)
27 DEFINE_STACK_OF(X509_ALGOR)
28 DEFINE_STACK_OF(X509_ATTRIBUTE)
30 /* CMS SignedData Utilities */
32 static CMS_SignedData *cms_get0_signed(CMS_ContentInfo *cms)
34 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_signed) {
35 CMSerr(CMS_F_CMS_GET0_SIGNED, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA);
38 return cms->d.signedData;
41 static CMS_SignedData *cms_signed_data_init(CMS_ContentInfo *cms)
43 if (cms->d.other == NULL) {
44 cms->d.signedData = M_ASN1_new_of(CMS_SignedData);
45 if (!cms->d.signedData) {
46 CMSerr(CMS_F_CMS_SIGNED_DATA_INIT, ERR_R_MALLOC_FAILURE);
49 cms->d.signedData->version = 1;
50 cms->d.signedData->encapContentInfo->eContentType =
51 OBJ_nid2obj(NID_pkcs7_data);
52 cms->d.signedData->encapContentInfo->partial = 1;
53 ASN1_OBJECT_free(cms->contentType);
54 cms->contentType = OBJ_nid2obj(NID_pkcs7_signed);
55 return cms->d.signedData;
57 return cms_get0_signed(cms);
60 /* Just initialise SignedData e.g. for certs only structure */
62 int CMS_SignedData_init(CMS_ContentInfo *cms)
64 if (cms_signed_data_init(cms))
71 /* Check structures and fixup version numbers (if necessary) */
73 static void cms_sd_set_version(CMS_SignedData *sd)
76 CMS_CertificateChoices *cch;
77 CMS_RevocationInfoChoice *rch;
80 for (i = 0; i < sk_CMS_CertificateChoices_num(sd->certificates); i++) {
81 cch = sk_CMS_CertificateChoices_value(sd->certificates, i);
82 if (cch->type == CMS_CERTCHOICE_OTHER) {
85 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
88 } else if (cch->type == CMS_CERTCHOICE_V1ACERT) {
94 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(sd->crls); i++) {
95 rch = sk_CMS_RevocationInfoChoice_value(sd->crls, i);
96 if (rch->type == CMS_REVCHOICE_OTHER) {
102 if ((OBJ_obj2nid(sd->encapContentInfo->eContentType) != NID_pkcs7_data)
103 && (sd->version < 3))
106 for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
107 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
108 if (si->sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
113 } else if (si->version < 1)
123 * RFC 5652 Section 11.1 Content Type
124 * The content-type attribute within signed-data MUST
125 * 1) be present if there are signed attributes
126 * 2) match the content type in the signed-data,
127 * 3) be a signed attribute.
128 * 4) not have more than one copy of the attribute.
130 * Note that since the CMS_SignerInfo_sign() always adds the "signing time"
131 * attribute, the content type attribute MUST be added also.
132 * Assumptions: This assumes that the attribute does not already exist.
134 static int cms_set_si_contentType_attr(CMS_ContentInfo *cms, CMS_SignerInfo *si)
136 ASN1_OBJECT *ctype = cms->d.signedData->encapContentInfo->eContentType;
138 /* Add the contentType attribute */
139 return CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,
140 V_ASN1_OBJECT, ctype, -1) > 0;
143 /* Copy an existing messageDigest value */
145 static int cms_copy_messageDigest(CMS_ContentInfo *cms, CMS_SignerInfo *si)
147 STACK_OF(CMS_SignerInfo) *sinfos;
148 CMS_SignerInfo *sitmp;
151 sinfos = CMS_get0_SignerInfos(cms);
152 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
153 ASN1_OCTET_STRING *messageDigest;
155 sitmp = sk_CMS_SignerInfo_value(sinfos, i);
158 if (CMS_signed_get_attr_count(sitmp) < 0)
160 if (OBJ_cmp(si->digestAlgorithm->algorithm,
161 sitmp->digestAlgorithm->algorithm))
163 messageDigest = CMS_signed_get0_data_by_OBJ(sitmp,
165 (NID_pkcs9_messageDigest),
166 -3, V_ASN1_OCTET_STRING);
167 if (!messageDigest) {
168 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST,
169 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
173 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
180 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST, CMS_R_NO_MATCHING_DIGEST);
184 int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert, int type,
188 case CMS_SIGNERINFO_ISSUER_SERIAL:
189 if (!cms_set1_ias(&sid->d.issuerAndSerialNumber, cert))
193 case CMS_SIGNERINFO_KEYIDENTIFIER:
194 if (!cms_set1_keyid(&sid->d.subjectKeyIdentifier, cert))
199 CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, CMS_R_UNKNOWN_ID);
208 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
209 ASN1_OCTET_STRING **keyid,
213 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL) {
215 *issuer = sid->d.issuerAndSerialNumber->issuer;
217 *sno = sid->d.issuerAndSerialNumber->serialNumber;
218 } else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
220 *keyid = sid->d.subjectKeyIdentifier;
226 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert)
228 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
229 return cms_ias_cert_cmp(sid->d.issuerAndSerialNumber, cert);
230 else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
231 return cms_keyid_cert_cmp(sid->d.subjectKeyIdentifier, cert);
236 static int cms_sd_asn1_ctrl(CMS_SignerInfo *si, int cmd)
238 EVP_PKEY *pkey = si->pkey;
241 if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
243 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_SIGN, cmd, si);
245 CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
249 CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_CTRL_FAILURE);
255 CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
256 X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
260 CMS_SignerInfo *si = NULL;
263 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
265 if (!X509_check_private_key(signer, pk)) {
266 CMSerr(CMS_F_CMS_ADD1_SIGNER,
267 CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
270 sd = cms_signed_data_init(cms);
273 si = M_ASN1_new_of(CMS_SignerInfo);
276 /* Call for side-effect of computing hash and caching extensions */
277 X509_check_purpose(signer, -1, -1);
285 si->mctx = EVP_MD_CTX_new();
288 if (si->mctx == NULL) {
289 CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
293 if (flags & CMS_USE_KEYID) {
297 type = CMS_SIGNERINFO_KEYIDENTIFIER;
299 type = CMS_SIGNERINFO_ISSUER_SERIAL;
303 if (!cms_set1_SignerIdentifier(si->sid, signer, type, ctx))
308 if (EVP_PKEY_get_default_digest_nid(pk, &def_nid) <= 0)
310 md = EVP_get_digestbynid(def_nid);
312 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DEFAULT_DIGEST);
318 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DIGEST_SET);
323 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DIGEST_SET);
327 X509_ALGOR_set_md(si->digestAlgorithm, md);
329 /* See if digest is present in digestAlgorithms */
330 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
331 const ASN1_OBJECT *aoid;
332 alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
333 X509_ALGOR_get0(&aoid, NULL, NULL, alg);
334 if (OBJ_obj2nid(aoid) == EVP_MD_type(md))
338 if (i == sk_X509_ALGOR_num(sd->digestAlgorithms)) {
339 alg = X509_ALGOR_new();
342 X509_ALGOR_set_md(alg, md);
343 if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg)) {
344 X509_ALGOR_free(alg);
349 if (!(flags & CMS_KEY_PARAM) && !cms_sd_asn1_ctrl(si, 0))
351 if (!(flags & CMS_NOATTR)) {
353 * Initialize signed attributes structure so other attributes
354 * such as signing time etc are added later even if we add none here.
356 if (!si->signedAttrs) {
357 si->signedAttrs = sk_X509_ATTRIBUTE_new_null();
358 if (!si->signedAttrs)
362 if (!(flags & CMS_NOSMIMECAP)) {
363 STACK_OF(X509_ALGOR) *smcap = NULL;
364 i = CMS_add_standard_smimecap(&smcap);
366 i = CMS_add_smimecap(si, smcap);
367 sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
371 if (flags & CMS_CADES) {
372 ESS_SIGNING_CERT *sc = NULL;
373 ESS_SIGNING_CERT_V2 *sc2 = NULL;
376 if (md == EVP_sha1() || md == NULL) {
377 if ((sc = ESS_SIGNING_CERT_new_init(signer,
380 add_sc = cms_add1_signing_cert(si, sc);
381 ESS_SIGNING_CERT_free(sc);
383 if ((sc2 = ESS_SIGNING_CERT_V2_new_init(md, signer,
386 add_sc = cms_add1_signing_cert_v2(si, sc2);
387 ESS_SIGNING_CERT_V2_free(sc2);
392 if (flags & CMS_REUSE_DIGEST) {
393 if (!cms_copy_messageDigest(cms, si))
395 if (!cms_set_si_contentType_attr(cms, si))
397 if (!(flags & (CMS_PARTIAL | CMS_KEY_PARAM)) &&
398 !CMS_SignerInfo_sign(si))
403 if (!(flags & CMS_NOCERTS)) {
404 /* NB ignore -1 return for duplicate cert */
405 if (!CMS_add1_cert(cms, signer))
409 if (flags & CMS_KEY_PARAM) {
410 if (flags & CMS_NOATTR) {
411 si->pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, si->pkey,
413 if (si->pctx == NULL)
415 if (EVP_PKEY_sign_init(si->pctx) <= 0)
417 if (EVP_PKEY_CTX_set_signature_md(si->pctx, md) <= 0)
419 } else if (EVP_DigestSignInit_with_libctx(si->mctx, &si->pctx,
421 ctx->libctx, ctx->propq,
427 if (!sd->signerInfos)
428 sd->signerInfos = sk_CMS_SignerInfo_new_null();
429 if (!sd->signerInfos || !sk_CMS_SignerInfo_push(sd->signerInfos, si))
435 CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
437 M_ASN1_free_of(si, CMS_SignerInfo);
442 void cms_SignerInfos_set_cmsctx(CMS_ContentInfo *cms)
446 STACK_OF(CMS_SignerInfo) *sinfos = CMS_get0_SignerInfos(cms);
447 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
449 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
450 si = sk_CMS_SignerInfo_value(sinfos, i);
456 static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
464 tt = X509_gmtime_adj(NULL, 0);
469 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
470 tt->type, tt, -1) <= 0)
479 CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME, ERR_R_MALLOC_FAILURE);
485 EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si)
490 EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si)
495 STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
497 CMS_SignedData *sd = cms_get0_signed(cms);
499 return sd != NULL ? sd->signerInfos : NULL;
502 STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
504 STACK_OF(X509) *signers = NULL;
505 STACK_OF(CMS_SignerInfo) *sinfos;
509 sinfos = CMS_get0_SignerInfos(cms);
510 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
511 si = sk_CMS_SignerInfo_value(sinfos, i);
512 if (si->signer != NULL) {
513 if (signers == NULL) {
514 signers = sk_X509_new_null();
518 if (!sk_X509_push(signers, si->signer)) {
519 sk_X509_free(signers);
527 void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
529 if (signer != NULL) {
531 EVP_PKEY_free(si->pkey);
532 si->pkey = X509_get_pubkey(signer);
534 X509_free(si->signer);
538 int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
539 ASN1_OCTET_STRING **keyid,
540 X509_NAME **issuer, ASN1_INTEGER **sno)
542 return cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
545 int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
547 return cms_SignerIdentifier_cert_cmp(si->sid, cert);
550 int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,
555 CMS_CertificateChoices *cch;
556 STACK_OF(CMS_CertificateChoices) *certs;
561 sd = cms_get0_signed(cms);
564 certs = sd->certificates;
565 for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
566 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
567 if (si->signer != NULL)
570 for (j = 0; j < sk_X509_num(scerts); j++) {
571 x = sk_X509_value(scerts, j);
572 if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
573 CMS_SignerInfo_set1_signer_cert(si, x);
579 if (si->signer != NULL || (flags & CMS_NOINTERN))
582 for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++) {
583 cch = sk_CMS_CertificateChoices_value(certs, j);
586 x = cch->d.certificate;
587 if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
588 CMS_SignerInfo_set1_signer_cert(si, x);
597 void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk,
598 X509 **signer, X509_ALGOR **pdig,
604 *signer = si->signer;
606 *pdig = si->digestAlgorithm;
608 *psig = si->signatureAlgorithm;
611 ASN1_OCTET_STRING *CMS_SignerInfo_get0_signature(CMS_SignerInfo *si)
613 return si->signature;
616 static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
617 CMS_SignerInfo *si, BIO *chain)
619 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
621 EVP_PKEY_CTX *pctx = NULL;
622 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
625 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
629 if (si->pkey == NULL) {
630 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);
634 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
636 /* Set SignerInfo algorithm details if we used custom parameter */
637 if (si->pctx && !cms_sd_asn1_ctrl(si, 0))
641 * If any signed attributes calculate and add messageDigest attribute
644 if (CMS_signed_get_attr_count(si) >= 0) {
645 unsigned char md[EVP_MAX_MD_SIZE];
648 if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
650 if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
651 V_ASN1_OCTET_STRING, md, mdlen))
653 /* Copy content type across */
654 if (!cms_set_si_contentType_attr(cms, si))
657 if (!CMS_SignerInfo_sign(si))
659 } else if (si->pctx) {
662 unsigned char md[EVP_MAX_MD_SIZE];
666 if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
668 siglen = EVP_PKEY_size(si->pkey);
669 sig = OPENSSL_malloc(siglen);
671 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
674 if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) {
678 ASN1_STRING_set0(si->signature, sig, siglen);
683 sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey));
685 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
688 if (!EVP_SignFinal_with_libctx(mctx, sig, &siglen, si->pkey,
689 ctx->libctx, ctx->propq)) {
690 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_SIGNFINAL_ERROR);
694 ASN1_STRING_set0(si->signature, sig, siglen);
700 EVP_MD_CTX_free(mctx);
701 EVP_PKEY_CTX_free(pctx);
706 int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain)
708 STACK_OF(CMS_SignerInfo) *sinfos;
712 sinfos = CMS_get0_SignerInfos(cms);
713 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
714 si = sk_CMS_SignerInfo_value(sinfos, i);
715 if (!cms_SignerInfo_content_sign(cms, si, chain))
718 cms->d.signedData->encapContentInfo->partial = 0;
722 int CMS_SignerInfo_sign(CMS_SignerInfo *si)
724 EVP_MD_CTX *mctx = si->mctx;
725 EVP_PKEY_CTX *pctx = NULL;
726 unsigned char *abuf = NULL;
729 const CMS_CTX *ctx = si->cms_ctx;
730 const char *md_name = OBJ_nid2sn(OBJ_obj2nid(si->digestAlgorithm->algorithm));
735 if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0) {
736 if (!cms_add1_signingTime(si, NULL))
740 if (!CMS_si_check_attributes(si))
746 EVP_MD_CTX_reset(mctx);
747 if (EVP_DigestSignInit_with_libctx(mctx, &pctx,
748 md_name, ctx->libctx, ctx->propq,
755 * TODO(3.0): This causes problems when providers are in use, so disabled
756 * for now. Can we get rid of this completely? AFAICT this ctrl has been
757 * present since CMS was first put in - but has never been used to do
758 * anything. All internal implementations just return 1 and ignore this ctrl
759 * and have always done so by the looks of things. To fix this we could
760 * convert this ctrl into a param, which would require us to send all the
761 * signer info data as a set of params...but that is non-trivial and since
762 * this isn't used by anything it may be better just to remove it.
765 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
766 EVP_PKEY_CTRL_CMS_SIGN, 0, si) <= 0) {
767 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
772 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
773 ASN1_ITEM_rptr(CMS_Attributes_Sign));
776 if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
778 if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
781 abuf = OPENSSL_malloc(siglen);
784 if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
788 * TODO(3.0): This causes problems when providers are in use, so disabled
789 * for now. Can we get rid of this completely? AFAICT this ctrl has been
790 * present since CMS was first put in - but has never been used to do
791 * anything. All internal implementations just return 1 and ignore this ctrl
792 * and have always done so by the looks of things. To fix this we could
793 * convert this ctrl into a param, which would require us to send all the
794 * signer info data as a set of params...but that is non-trivial and since
795 * this isn't used by anything it may be better just to remove it.
798 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
799 EVP_PKEY_CTRL_CMS_SIGN, 1, si) <= 0) {
800 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
805 EVP_MD_CTX_reset(mctx);
807 ASN1_STRING_set0(si->signature, abuf, siglen);
813 EVP_MD_CTX_reset(mctx);
817 int CMS_SignerInfo_verify(CMS_SignerInfo *si)
819 EVP_MD_CTX *mctx = NULL;
820 unsigned char *abuf = NULL;
824 const CMS_CTX *ctx = si->cms_ctx;
826 if (si->pkey == NULL) {
827 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_NO_PUBLIC_KEY);
831 if (!CMS_si_check_attributes(si))
834 name = OBJ_nid2sn(OBJ_obj2nid(si->digestAlgorithm->algorithm));
835 md = EVP_MD_fetch(ctx->libctx, name, ctx->propq);
838 if (si->mctx == NULL && (si->mctx = EVP_MD_CTX_new()) == NULL) {
839 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, ERR_R_MALLOC_FAILURE);
843 if (EVP_DigestVerifyInit_with_libctx(mctx, &si->pctx,
844 EVP_MD_name(md), ctx->libctx, NULL,
848 if (!cms_sd_asn1_ctrl(si, 1))
851 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
852 ASN1_ITEM_rptr(CMS_Attributes_Verify));
855 r = EVP_DigestVerifyUpdate(mctx, abuf, alen);
861 r = EVP_DigestVerifyFinal(mctx,
862 si->signature->data, si->signature->length);
864 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE);
867 EVP_MD_CTX_reset(mctx);
871 /* Create a chain of digest BIOs from a CMS ContentInfo */
873 BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms)
879 sd = cms_get0_signed(cms);
882 if (cms->d.signedData->encapContentInfo->partial)
883 cms_sd_set_version(sd);
884 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
885 X509_ALGOR *digestAlgorithm;
888 digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
889 mdbio = cms_DigestAlgorithm_init_bio(digestAlgorithm, cms_get0_cmsctx(cms));
893 BIO_push(chain, mdbio);
903 int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
905 ASN1_OCTET_STRING *os = NULL;
906 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
907 EVP_PKEY_CTX *pkctx = NULL;
909 unsigned char mval[EVP_MAX_MD_SIZE];
913 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, ERR_R_MALLOC_FAILURE);
916 /* If we have any signed attributes look for messageDigest value */
917 if (CMS_signed_get_attr_count(si) >= 0) {
918 os = CMS_signed_get0_data_by_OBJ(si,
919 OBJ_nid2obj(NID_pkcs9_messageDigest),
920 -3, V_ASN1_OCTET_STRING);
922 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
923 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
928 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
931 if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {
932 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
933 CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
937 /* If messageDigest found compare it */
940 if (mlen != (unsigned int)os->length) {
941 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
942 CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
946 if (memcmp(mval, os->data, mlen)) {
947 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
948 CMS_R_VERIFICATION_FAILURE);
953 const EVP_MD *md = EVP_MD_CTX_md(mctx);
954 const CMS_CTX *ctx = si->cms_ctx;
956 pkctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, si->pkey, ctx->propq);
959 if (EVP_PKEY_verify_init(pkctx) <= 0)
961 if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)
964 if (!cms_sd_asn1_ctrl(si, 1))
966 r = EVP_PKEY_verify(pkctx, si->signature->data,
967 si->signature->length, mval, mlen);
969 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
970 CMS_R_VERIFICATION_FAILURE);
976 EVP_PKEY_CTX_free(pkctx);
977 EVP_MD_CTX_free(mctx);
982 int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
984 unsigned char *smder = NULL;
987 smderlen = i2d_X509_ALGORS(algs, &smder);
990 r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
991 V_ASN1_SEQUENCE, smder, smderlen);
996 int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
997 int algnid, int keysize)
1000 ASN1_INTEGER *key = NULL;
1003 key = ASN1_INTEGER_new();
1004 if (key == NULL || !ASN1_INTEGER_set(key, keysize)) {
1005 ASN1_INTEGER_free(key);
1009 alg = X509_ALGOR_new();
1011 ASN1_INTEGER_free(key);
1015 X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),
1016 key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
1018 *algs = sk_X509_ALGOR_new_null();
1019 if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) {
1020 X509_ALGOR_free(alg);
1026 /* Check to see if a cipher exists and if so add S/MIME capabilities */
1028 static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
1030 if (EVP_get_cipherbynid(nid))
1031 return CMS_add_simple_smimecap(sk, nid, arg);
1035 static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
1037 if (EVP_get_digestbynid(nid))
1038 return CMS_add_simple_smimecap(sk, nid, arg);
1042 int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
1044 if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
1045 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
1046 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
1047 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
1048 || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
1049 || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
1050 || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
1051 || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
1052 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
1053 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
1054 || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
1055 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))