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))
70 /* Check structures and fixup version numbers (if necessary) */
72 static void cms_sd_set_version(CMS_SignedData *sd)
75 CMS_CertificateChoices *cch;
76 CMS_RevocationInfoChoice *rch;
79 for (i = 0; i < sk_CMS_CertificateChoices_num(sd->certificates); i++) {
80 cch = sk_CMS_CertificateChoices_value(sd->certificates, i);
81 if (cch->type == CMS_CERTCHOICE_OTHER) {
84 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
87 } else if (cch->type == CMS_CERTCHOICE_V1ACERT) {
93 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(sd->crls); i++) {
94 rch = sk_CMS_RevocationInfoChoice_value(sd->crls, i);
95 if (rch->type == CMS_REVCHOICE_OTHER) {
101 if ((OBJ_obj2nid(sd->encapContentInfo->eContentType) != NID_pkcs7_data)
102 && (sd->version < 3))
105 for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
106 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
107 if (si->sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
112 } else if (si->version < 1)
122 * RFC 5652 Section 11.1 Content Type
123 * The content-type attribute within signed-data MUST
124 * 1) be present if there are signed attributes
125 * 2) match the content type in the signed-data,
126 * 3) be a signed attribute.
127 * 4) not have more than one copy of the attribute.
129 * Note that since the CMS_SignerInfo_sign() always adds the "signing time"
130 * attribute, the content type attribute MUST be added also.
131 * Assumptions: This assumes that the attribute does not already exist.
133 static int cms_set_si_contentType_attr(CMS_ContentInfo *cms, CMS_SignerInfo *si)
135 ASN1_OBJECT *ctype = cms->d.signedData->encapContentInfo->eContentType;
137 /* Add the contentType attribute */
138 return CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,
139 V_ASN1_OBJECT, ctype, -1) > 0;
142 /* Copy an existing messageDigest value */
144 static int cms_copy_messageDigest(CMS_ContentInfo *cms, CMS_SignerInfo *si)
146 STACK_OF(CMS_SignerInfo) *sinfos;
147 CMS_SignerInfo *sitmp;
149 sinfos = CMS_get0_SignerInfos(cms);
150 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
151 ASN1_OCTET_STRING *messageDigest;
152 sitmp = sk_CMS_SignerInfo_value(sinfos, i);
155 if (CMS_signed_get_attr_count(sitmp) < 0)
157 if (OBJ_cmp(si->digestAlgorithm->algorithm,
158 sitmp->digestAlgorithm->algorithm))
160 messageDigest = CMS_signed_get0_data_by_OBJ(sitmp,
162 (NID_pkcs9_messageDigest),
163 -3, V_ASN1_OCTET_STRING);
164 if (!messageDigest) {
165 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST,
166 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
170 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
177 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST, CMS_R_NO_MATCHING_DIGEST);
181 int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert, int type)
184 case CMS_SIGNERINFO_ISSUER_SERIAL:
185 if (!cms_set1_ias(&sid->d.issuerAndSerialNumber, cert))
189 case CMS_SIGNERINFO_KEYIDENTIFIER:
190 if (!cms_set1_keyid(&sid->d.subjectKeyIdentifier, cert))
195 CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, CMS_R_UNKNOWN_ID);
204 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
205 ASN1_OCTET_STRING **keyid,
209 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL) {
211 *issuer = sid->d.issuerAndSerialNumber->issuer;
213 *sno = sid->d.issuerAndSerialNumber->serialNumber;
214 } else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
216 *keyid = sid->d.subjectKeyIdentifier;
222 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert)
224 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
225 return cms_ias_cert_cmp(sid->d.issuerAndSerialNumber, cert);
226 else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
227 return cms_keyid_cert_cmp(sid->d.subjectKeyIdentifier, cert);
232 static int cms_sd_asn1_ctrl(CMS_SignerInfo *si, int cmd)
234 EVP_PKEY *pkey = si->pkey;
236 if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
238 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_SIGN, cmd, si);
240 CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
244 CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_CTRL_FAILURE);
250 CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
251 X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
255 CMS_SignerInfo *si = NULL;
258 if (!X509_check_private_key(signer, pk)) {
259 CMSerr(CMS_F_CMS_ADD1_SIGNER,
260 CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
263 sd = cms_signed_data_init(cms);
266 si = M_ASN1_new_of(CMS_SignerInfo);
269 /* Call for side-effect of computing hash and caching extensions */
270 X509_check_purpose(signer, -1, -1);
277 si->mctx = EVP_MD_CTX_new();
280 if (si->mctx == NULL) {
281 CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
285 if (flags & CMS_USE_KEYID) {
289 type = CMS_SIGNERINFO_KEYIDENTIFIER;
291 type = CMS_SIGNERINFO_ISSUER_SERIAL;
295 if (!cms_set1_SignerIdentifier(si->sid, signer, type))
300 if (EVP_PKEY_get_default_digest_nid(pk, &def_nid) <= 0)
302 md = EVP_get_digestbynid(def_nid);
304 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DEFAULT_DIGEST);
310 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DIGEST_SET);
314 X509_ALGOR_set_md(si->digestAlgorithm, md);
316 /* See if digest is present in digestAlgorithms */
317 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
318 const ASN1_OBJECT *aoid;
319 alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
320 X509_ALGOR_get0(&aoid, NULL, NULL, alg);
321 if (OBJ_obj2nid(aoid) == EVP_MD_type(md))
325 if (i == sk_X509_ALGOR_num(sd->digestAlgorithms)) {
326 alg = X509_ALGOR_new();
329 X509_ALGOR_set_md(alg, md);
330 if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg)) {
331 X509_ALGOR_free(alg);
336 if (!(flags & CMS_KEY_PARAM) && !cms_sd_asn1_ctrl(si, 0))
338 if (!(flags & CMS_NOATTR)) {
340 * Initialize signed attributes structure so other attributes
341 * such as signing time etc are added later even if we add none here.
343 if (!si->signedAttrs) {
344 si->signedAttrs = sk_X509_ATTRIBUTE_new_null();
345 if (!si->signedAttrs)
349 if (!(flags & CMS_NOSMIMECAP)) {
350 STACK_OF(X509_ALGOR) *smcap = NULL;
351 i = CMS_add_standard_smimecap(&smcap);
353 i = CMS_add_smimecap(si, smcap);
354 sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
358 if (flags & CMS_CADES) {
359 ESS_SIGNING_CERT *sc = NULL;
360 ESS_SIGNING_CERT_V2 *sc2 = NULL;
363 if (md == EVP_sha1() || md == NULL) {
364 if ((sc = ESS_SIGNING_CERT_new_init(signer,
367 add_sc = cms_add1_signing_cert(si, sc);
368 ESS_SIGNING_CERT_free(sc);
370 if ((sc2 = ESS_SIGNING_CERT_V2_new_init(md, signer,
373 add_sc = cms_add1_signing_cert_v2(si, sc2);
374 ESS_SIGNING_CERT_V2_free(sc2);
379 if (flags & CMS_REUSE_DIGEST) {
380 if (!cms_copy_messageDigest(cms, si))
382 if (!cms_set_si_contentType_attr(cms, si))
384 if (!(flags & (CMS_PARTIAL | CMS_KEY_PARAM)) &&
385 !CMS_SignerInfo_sign(si))
390 if (!(flags & CMS_NOCERTS)) {
391 /* NB ignore -1 return for duplicate cert */
392 if (!CMS_add1_cert(cms, signer))
396 if (flags & CMS_KEY_PARAM) {
397 if (flags & CMS_NOATTR) {
398 si->pctx = EVP_PKEY_CTX_new(si->pkey, NULL);
399 if (si->pctx == NULL)
401 if (EVP_PKEY_sign_init(si->pctx) <= 0)
403 if (EVP_PKEY_CTX_set_signature_md(si->pctx, md) <= 0)
405 } else if (EVP_DigestSignInit(si->mctx, &si->pctx, md, NULL, pk) <= 0)
409 if (!sd->signerInfos)
410 sd->signerInfos = sk_CMS_SignerInfo_new_null();
411 if (!sd->signerInfos || !sk_CMS_SignerInfo_push(sd->signerInfos, si))
417 CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
419 M_ASN1_free_of(si, CMS_SignerInfo);
424 static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
431 tt = X509_gmtime_adj(NULL, 0);
436 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
437 tt->type, tt, -1) <= 0)
448 CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME, ERR_R_MALLOC_FAILURE);
454 EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si)
459 EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si)
464 STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
467 sd = cms_get0_signed(cms);
470 return sd->signerInfos;
473 STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
475 STACK_OF(X509) *signers = NULL;
476 STACK_OF(CMS_SignerInfo) *sinfos;
479 sinfos = CMS_get0_SignerInfos(cms);
480 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
481 si = sk_CMS_SignerInfo_value(sinfos, i);
484 signers = sk_X509_new_null();
488 if (!sk_X509_push(signers, si->signer)) {
489 sk_X509_free(signers);
497 void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
501 EVP_PKEY_free(si->pkey);
502 si->pkey = X509_get_pubkey(signer);
504 X509_free(si->signer);
508 int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
509 ASN1_OCTET_STRING **keyid,
510 X509_NAME **issuer, ASN1_INTEGER **sno)
512 return cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
515 int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
517 return cms_SignerIdentifier_cert_cmp(si->sid, cert);
520 int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,
525 CMS_CertificateChoices *cch;
526 STACK_OF(CMS_CertificateChoices) *certs;
530 sd = cms_get0_signed(cms);
533 certs = sd->certificates;
534 for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
535 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
539 for (j = 0; j < sk_X509_num(scerts); j++) {
540 x = sk_X509_value(scerts, j);
541 if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
542 CMS_SignerInfo_set1_signer_cert(si, x);
548 if (si->signer || (flags & CMS_NOINTERN))
551 for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++) {
552 cch = sk_CMS_CertificateChoices_value(certs, j);
555 x = cch->d.certificate;
556 if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
557 CMS_SignerInfo_set1_signer_cert(si, x);
566 void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk,
567 X509 **signer, X509_ALGOR **pdig,
573 *signer = si->signer;
575 *pdig = si->digestAlgorithm;
577 *psig = si->signatureAlgorithm;
580 ASN1_OCTET_STRING *CMS_SignerInfo_get0_signature(CMS_SignerInfo *si)
582 return si->signature;
585 static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
586 CMS_SignerInfo *si, BIO *chain)
588 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
590 EVP_PKEY_CTX *pctx = NULL;
593 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
598 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);
602 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
604 /* Set SignerInfo algorithm details if we used custom parameter */
605 if (si->pctx && !cms_sd_asn1_ctrl(si, 0))
609 * If any signed attributes calculate and add messageDigest attribute
612 if (CMS_signed_get_attr_count(si) >= 0) {
613 unsigned char md[EVP_MAX_MD_SIZE];
615 if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
617 if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
618 V_ASN1_OCTET_STRING, md, mdlen))
620 /* Copy content type across */
621 if (!cms_set_si_contentType_attr(cms, si))
624 if (!CMS_SignerInfo_sign(si))
626 } else if (si->pctx) {
629 unsigned char md[EVP_MAX_MD_SIZE];
632 if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
634 siglen = EVP_PKEY_size(si->pkey);
635 sig = OPENSSL_malloc(siglen);
637 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
640 if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) {
644 ASN1_STRING_set0(si->signature, sig, siglen);
648 sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey));
650 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
653 if (!EVP_SignFinal(mctx, sig, &siglen, si->pkey)) {
654 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_SIGNFINAL_ERROR);
658 ASN1_STRING_set0(si->signature, sig, siglen);
664 EVP_MD_CTX_free(mctx);
665 EVP_PKEY_CTX_free(pctx);
670 int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain)
672 STACK_OF(CMS_SignerInfo) *sinfos;
675 sinfos = CMS_get0_SignerInfos(cms);
676 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
677 si = sk_CMS_SignerInfo_value(sinfos, i);
678 if (!cms_SignerInfo_content_sign(cms, si, chain))
681 cms->d.signedData->encapContentInfo->partial = 0;
685 int CMS_SignerInfo_sign(CMS_SignerInfo *si)
687 EVP_MD_CTX *mctx = si->mctx;
688 EVP_PKEY_CTX *pctx = NULL;
689 unsigned char *abuf = NULL;
692 const EVP_MD *md = NULL;
694 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
698 if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0) {
699 if (!cms_add1_signingTime(si, NULL))
703 if (!CMS_si_check_attributes(si))
709 EVP_MD_CTX_reset(mctx);
710 if (EVP_DigestSignInit(mctx, &pctx, md, NULL, si->pkey) <= 0)
716 * TODO(3.0): This causes problems when providers are in use, so disabled
717 * for now. Can we get rid of this completely? AFAICT this ctrl has been
718 * present since CMS was first put in - but has never been used to do
719 * anything. All internal implementations just return 1 and ignore this ctrl
720 * and have always done so by the looks of things. To fix this we could
721 * convert this ctrl into a param, which would require us to send all the
722 * signer info data as a set of params...but that is non-trivial and since
723 * this isn't used by anything it may be better just to remove it.
726 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
727 EVP_PKEY_CTRL_CMS_SIGN, 0, si) <= 0) {
728 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
733 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
734 ASN1_ITEM_rptr(CMS_Attributes_Sign));
737 if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
739 if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
742 abuf = OPENSSL_malloc(siglen);
745 if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
749 * TODO(3.0): This causes problems when providers are in use, so disabled
750 * for now. Can we get rid of this completely? AFAICT this ctrl has been
751 * present since CMS was first put in - but has never been used to do
752 * anything. All internal implementations just return 1 and ignore this ctrl
753 * and have always done so by the looks of things. To fix this we could
754 * convert this ctrl into a param, which would require us to send all the
755 * signer info data as a set of params...but that is non-trivial and since
756 * this isn't used by anything it may be better just to remove it.
759 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
760 EVP_PKEY_CTRL_CMS_SIGN, 1, si) <= 0) {
761 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
766 EVP_MD_CTX_reset(mctx);
768 ASN1_STRING_set0(si->signature, abuf, siglen);
774 EVP_MD_CTX_reset(mctx);
778 int CMS_SignerInfo_verify(CMS_SignerInfo *si)
780 EVP_MD_CTX *mctx = NULL;
781 unsigned char *abuf = NULL;
783 const EVP_MD *md = NULL;
786 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_NO_PUBLIC_KEY);
790 if (!CMS_si_check_attributes(si))
793 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
796 if (si->mctx == NULL && (si->mctx = EVP_MD_CTX_new()) == NULL) {
797 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, ERR_R_MALLOC_FAILURE);
801 if (EVP_DigestVerifyInit(mctx, &si->pctx, md, NULL, si->pkey) <= 0)
804 if (!cms_sd_asn1_ctrl(si, 1))
807 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
808 ASN1_ITEM_rptr(CMS_Attributes_Verify));
811 r = EVP_DigestVerifyUpdate(mctx, abuf, alen);
817 r = EVP_DigestVerifyFinal(mctx,
818 si->signature->data, si->signature->length);
820 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE);
822 EVP_MD_CTX_reset(mctx);
826 /* Create a chain of digest BIOs from a CMS ContentInfo */
828 BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms)
833 sd = cms_get0_signed(cms);
836 if (cms->d.signedData->encapContentInfo->partial)
837 cms_sd_set_version(sd);
838 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
839 X509_ALGOR *digestAlgorithm;
841 digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
842 mdbio = cms_DigestAlgorithm_init_bio(digestAlgorithm);
846 BIO_push(chain, mdbio);
856 int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
858 ASN1_OCTET_STRING *os = NULL;
859 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
860 EVP_PKEY_CTX *pkctx = NULL;
862 unsigned char mval[EVP_MAX_MD_SIZE];
866 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, ERR_R_MALLOC_FAILURE);
869 /* If we have any signed attributes look for messageDigest value */
870 if (CMS_signed_get_attr_count(si) >= 0) {
871 os = CMS_signed_get0_data_by_OBJ(si,
872 OBJ_nid2obj(NID_pkcs9_messageDigest),
873 -3, V_ASN1_OCTET_STRING);
875 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
876 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
881 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
884 if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {
885 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
886 CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
890 /* If messageDigest found compare it */
893 if (mlen != (unsigned int)os->length) {
894 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
895 CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
899 if (memcmp(mval, os->data, mlen)) {
900 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
901 CMS_R_VERIFICATION_FAILURE);
906 const EVP_MD *md = EVP_MD_CTX_md(mctx);
907 pkctx = EVP_PKEY_CTX_new(si->pkey, NULL);
910 if (EVP_PKEY_verify_init(pkctx) <= 0)
912 if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)
915 if (!cms_sd_asn1_ctrl(si, 1))
917 r = EVP_PKEY_verify(pkctx, si->signature->data,
918 si->signature->length, mval, mlen);
920 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
921 CMS_R_VERIFICATION_FAILURE);
927 EVP_PKEY_CTX_free(pkctx);
928 EVP_MD_CTX_free(mctx);
933 int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
935 unsigned char *smder = NULL;
937 smderlen = i2d_X509_ALGORS(algs, &smder);
940 r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
941 V_ASN1_SEQUENCE, smder, smderlen);
946 int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
947 int algnid, int keysize)
950 ASN1_INTEGER *key = NULL;
952 key = ASN1_INTEGER_new();
953 if (key == NULL || !ASN1_INTEGER_set(key, keysize)) {
954 ASN1_INTEGER_free(key);
958 alg = X509_ALGOR_new();
960 ASN1_INTEGER_free(key);
964 X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),
965 key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
967 *algs = sk_X509_ALGOR_new_null();
968 if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) {
969 X509_ALGOR_free(alg);
975 /* Check to see if a cipher exists and if so add S/MIME capabilities */
977 static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
979 if (EVP_get_cipherbynid(nid))
980 return CMS_add_simple_smimecap(sk, nid, arg);
984 static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
986 if (EVP_get_digestbynid(nid))
987 return CMS_add_simple_smimecap(sk, nid, arg);
991 int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
993 if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
994 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
995 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
996 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
997 || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
998 || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
999 || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
1000 || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
1001 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
1002 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
1003 || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
1004 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))