2 * Copyright 2008-2016 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>
18 #include "internal/asn1_int.h"
19 #include "internal/evp_int.h"
21 /* CMS SignedData Utilities */
23 static CMS_SignedData *cms_get0_signed(CMS_ContentInfo *cms)
25 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_signed) {
26 CMSerr(CMS_F_CMS_GET0_SIGNED, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA);
29 return cms->d.signedData;
32 static CMS_SignedData *cms_signed_data_init(CMS_ContentInfo *cms)
34 if (cms->d.other == NULL) {
35 cms->d.signedData = M_ASN1_new_of(CMS_SignedData);
36 if (!cms->d.signedData) {
37 CMSerr(CMS_F_CMS_SIGNED_DATA_INIT, ERR_R_MALLOC_FAILURE);
40 cms->d.signedData->version = 1;
41 cms->d.signedData->encapContentInfo->eContentType =
42 OBJ_nid2obj(NID_pkcs7_data);
43 cms->d.signedData->encapContentInfo->partial = 1;
44 ASN1_OBJECT_free(cms->contentType);
45 cms->contentType = OBJ_nid2obj(NID_pkcs7_signed);
46 return cms->d.signedData;
48 return cms_get0_signed(cms);
51 /* Just initialise SignedData e.g. for certs only structure */
53 int CMS_SignedData_init(CMS_ContentInfo *cms)
55 if (cms_signed_data_init(cms))
61 /* Check structures and fixup version numbers (if necessary) */
63 static void cms_sd_set_version(CMS_SignedData *sd)
66 CMS_CertificateChoices *cch;
67 CMS_RevocationInfoChoice *rch;
70 for (i = 0; i < sk_CMS_CertificateChoices_num(sd->certificates); i++) {
71 cch = sk_CMS_CertificateChoices_value(sd->certificates, i);
72 if (cch->type == CMS_CERTCHOICE_OTHER) {
75 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
78 } else if (cch->type == CMS_CERTCHOICE_V1ACERT) {
84 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(sd->crls); i++) {
85 rch = sk_CMS_RevocationInfoChoice_value(sd->crls, i);
86 if (rch->type == CMS_REVCHOICE_OTHER) {
92 if ((OBJ_obj2nid(sd->encapContentInfo->eContentType) != NID_pkcs7_data)
96 for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
97 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
98 if (si->sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
103 } else if (si->version < 1)
113 * RFC 5652 Section 11.1 Content Type
114 * The content-type attribute within signed-data MUST
115 * 1) be present if there are signed attributes
116 * 2) match the content type in the signed-data,
117 * 3) be a signed attribute.
118 * 4) not have more than one copy of the attribute.
120 * Note that since the CMS_SignerInfo_sign() always adds the "signing time"
121 * attribute, the content type attribute MUST be added also.
122 * Assumptions: This assumes that the attribute does not already exist.
124 static int cms_set_si_contentType_attr(CMS_ContentInfo *cms, CMS_SignerInfo *si)
126 ASN1_OBJECT *ctype = cms->d.signedData->encapContentInfo->eContentType;
128 /* Add the contentType attribute */
129 return CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,
130 V_ASN1_OBJECT, ctype, -1) > 0;
133 /* Copy an existing messageDigest value */
135 static int cms_copy_messageDigest(CMS_ContentInfo *cms, CMS_SignerInfo *si)
137 STACK_OF(CMS_SignerInfo) *sinfos;
138 CMS_SignerInfo *sitmp;
140 sinfos = CMS_get0_SignerInfos(cms);
141 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
142 ASN1_OCTET_STRING *messageDigest;
143 sitmp = sk_CMS_SignerInfo_value(sinfos, i);
146 if (CMS_signed_get_attr_count(sitmp) < 0)
148 if (OBJ_cmp(si->digestAlgorithm->algorithm,
149 sitmp->digestAlgorithm->algorithm))
151 messageDigest = CMS_signed_get0_data_by_OBJ(sitmp,
153 (NID_pkcs9_messageDigest),
154 -3, V_ASN1_OCTET_STRING);
155 if (!messageDigest) {
156 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST,
157 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
161 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
168 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST, CMS_R_NO_MATCHING_DIGEST);
172 int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert, int type)
175 case CMS_SIGNERINFO_ISSUER_SERIAL:
176 if (!cms_set1_ias(&sid->d.issuerAndSerialNumber, cert))
180 case CMS_SIGNERINFO_KEYIDENTIFIER:
181 if (!cms_set1_keyid(&sid->d.subjectKeyIdentifier, cert))
186 CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, CMS_R_UNKNOWN_ID);
195 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
196 ASN1_OCTET_STRING **keyid,
200 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL) {
202 *issuer = sid->d.issuerAndSerialNumber->issuer;
204 *sno = sid->d.issuerAndSerialNumber->serialNumber;
205 } else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
207 *keyid = sid->d.subjectKeyIdentifier;
213 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert)
215 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
216 return cms_ias_cert_cmp(sid->d.issuerAndSerialNumber, cert);
217 else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
218 return cms_keyid_cert_cmp(sid->d.subjectKeyIdentifier, cert);
223 static int cms_sd_asn1_ctrl(CMS_SignerInfo *si, int cmd)
225 EVP_PKEY *pkey = si->pkey;
227 if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
229 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_SIGN, cmd, si);
231 CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
235 CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_CTRL_FAILURE);
241 CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
242 X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
246 CMS_SignerInfo *si = NULL;
249 if (!X509_check_private_key(signer, pk)) {
250 CMSerr(CMS_F_CMS_ADD1_SIGNER,
251 CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
254 sd = cms_signed_data_init(cms);
257 si = M_ASN1_new_of(CMS_SignerInfo);
260 /* Call for side-effect of computing hash and caching extensions */
261 X509_check_purpose(signer, -1, -1);
268 si->mctx = EVP_MD_CTX_new();
271 if (si->mctx == NULL) {
272 CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
276 if (flags & CMS_USE_KEYID) {
280 type = CMS_SIGNERINFO_KEYIDENTIFIER;
282 type = CMS_SIGNERINFO_ISSUER_SERIAL;
286 if (!cms_set1_SignerIdentifier(si->sid, signer, type))
291 if (EVP_PKEY_get_default_digest_nid(pk, &def_nid) <= 0)
293 md = EVP_get_digestbynid(def_nid);
295 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DEFAULT_DIGEST);
301 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DIGEST_SET);
305 X509_ALGOR_set_md(si->digestAlgorithm, md);
307 /* See if digest is present in digestAlgorithms */
308 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
309 const ASN1_OBJECT *aoid;
310 alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
311 X509_ALGOR_get0(&aoid, NULL, NULL, alg);
312 if (OBJ_obj2nid(aoid) == EVP_MD_type(md))
316 if (i == sk_X509_ALGOR_num(sd->digestAlgorithms)) {
317 alg = X509_ALGOR_new();
320 X509_ALGOR_set_md(alg, md);
321 if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg)) {
322 X509_ALGOR_free(alg);
327 if (!(flags & CMS_KEY_PARAM) && !cms_sd_asn1_ctrl(si, 0))
329 if (!(flags & CMS_NOATTR)) {
331 * Initialize signed attributes structure so other attributes
332 * such as signing time etc are added later even if we add none here.
334 if (!si->signedAttrs) {
335 si->signedAttrs = sk_X509_ATTRIBUTE_new_null();
336 if (!si->signedAttrs)
340 if (!(flags & CMS_NOSMIMECAP)) {
341 STACK_OF(X509_ALGOR) *smcap = NULL;
342 i = CMS_add_standard_smimecap(&smcap);
344 i = CMS_add_smimecap(si, smcap);
345 sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
349 if (flags & CMS_CADES) {
350 ESS_SIGNING_CERT *sc = NULL;
351 ESS_SIGNING_CERT_V2 *sc2 = NULL;
354 if (md == EVP_sha1() || md == NULL) {
355 if ((sc = ESS_SIGNING_CERT_new_init(signer,
358 add_sc = CMS_add1_signing_cert(si, sc);
359 ESS_SIGNING_CERT_free(sc);
361 if ((sc2 = ESS_SIGNING_CERT_V2_new_init(md, signer,
364 add_sc = CMS_add1_signing_cert_v2(si, sc2);
365 ESS_SIGNING_CERT_V2_free(sc2);
370 if (flags & CMS_REUSE_DIGEST) {
371 if (!cms_copy_messageDigest(cms, si))
373 if (!cms_set_si_contentType_attr(cms, si))
375 if (!(flags & (CMS_PARTIAL | CMS_KEY_PARAM)) &&
376 !CMS_SignerInfo_sign(si))
381 if (!(flags & CMS_NOCERTS)) {
382 /* NB ignore -1 return for duplicate cert */
383 if (!CMS_add1_cert(cms, signer))
387 if (flags & CMS_KEY_PARAM) {
388 if (flags & CMS_NOATTR) {
389 si->pctx = EVP_PKEY_CTX_new(si->pkey, NULL);
390 if (si->pctx == NULL)
392 if (EVP_PKEY_sign_init(si->pctx) <= 0)
394 if (EVP_PKEY_CTX_set_signature_md(si->pctx, md) <= 0)
396 } else if (EVP_DigestSignInit(si->mctx, &si->pctx, md, NULL, pk) <= 0)
400 if (!sd->signerInfos)
401 sd->signerInfos = sk_CMS_SignerInfo_new_null();
402 if (!sd->signerInfos || !sk_CMS_SignerInfo_push(sd->signerInfos, si))
408 CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
410 M_ASN1_free_of(si, CMS_SignerInfo);
415 static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
422 tt = X509_gmtime_adj(NULL, 0);
427 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
428 tt->type, tt, -1) <= 0)
439 CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME, ERR_R_MALLOC_FAILURE);
445 EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si)
450 EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si)
455 STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
458 sd = cms_get0_signed(cms);
461 return sd->signerInfos;
464 STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
466 STACK_OF(X509) *signers = NULL;
467 STACK_OF(CMS_SignerInfo) *sinfos;
470 sinfos = CMS_get0_SignerInfos(cms);
471 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
472 si = sk_CMS_SignerInfo_value(sinfos, i);
475 signers = sk_X509_new_null();
479 if (!sk_X509_push(signers, si->signer)) {
480 sk_X509_free(signers);
488 void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
492 EVP_PKEY_free(si->pkey);
493 si->pkey = X509_get_pubkey(signer);
495 X509_free(si->signer);
499 int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
500 ASN1_OCTET_STRING **keyid,
501 X509_NAME **issuer, ASN1_INTEGER **sno)
503 return cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
506 int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
508 return cms_SignerIdentifier_cert_cmp(si->sid, cert);
511 int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,
516 CMS_CertificateChoices *cch;
517 STACK_OF(CMS_CertificateChoices) *certs;
521 sd = cms_get0_signed(cms);
524 certs = sd->certificates;
525 for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
526 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
530 for (j = 0; j < sk_X509_num(scerts); j++) {
531 x = sk_X509_value(scerts, j);
532 if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
533 CMS_SignerInfo_set1_signer_cert(si, x);
539 if (si->signer || (flags & CMS_NOINTERN))
542 for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++) {
543 cch = sk_CMS_CertificateChoices_value(certs, j);
546 x = cch->d.certificate;
547 if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
548 CMS_SignerInfo_set1_signer_cert(si, x);
557 void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk,
558 X509 **signer, X509_ALGOR **pdig,
564 *signer = si->signer;
566 *pdig = si->digestAlgorithm;
568 *psig = si->signatureAlgorithm;
571 ASN1_OCTET_STRING *CMS_SignerInfo_get0_signature(CMS_SignerInfo *si)
573 return si->signature;
576 static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
577 CMS_SignerInfo *si, BIO *chain)
579 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
581 EVP_PKEY_CTX *pctx = NULL;
584 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
589 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);
593 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
595 /* Set SignerInfo algorithm details if we used custom parameter */
596 if (si->pctx && !cms_sd_asn1_ctrl(si, 0))
600 * If any signed attributes calculate and add messageDigest attribute
603 if (CMS_signed_get_attr_count(si) >= 0) {
604 unsigned char md[EVP_MAX_MD_SIZE];
606 if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
608 if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
609 V_ASN1_OCTET_STRING, md, mdlen))
611 /* Copy content type across */
612 if (!cms_set_si_contentType_attr(cms, si))
615 if (!CMS_SignerInfo_sign(si))
617 } else if (si->pctx) {
620 unsigned char md[EVP_MAX_MD_SIZE];
623 if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
625 siglen = EVP_PKEY_size(si->pkey);
626 sig = OPENSSL_malloc(siglen);
628 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
631 if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) {
635 ASN1_STRING_set0(si->signature, sig, siglen);
639 sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey));
641 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
644 if (!EVP_SignFinal(mctx, sig, &siglen, si->pkey)) {
645 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_SIGNFINAL_ERROR);
649 ASN1_STRING_set0(si->signature, sig, siglen);
655 EVP_MD_CTX_free(mctx);
656 EVP_PKEY_CTX_free(pctx);
661 int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain)
663 STACK_OF(CMS_SignerInfo) *sinfos;
666 sinfos = CMS_get0_SignerInfos(cms);
667 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
668 si = sk_CMS_SignerInfo_value(sinfos, i);
669 if (!cms_SignerInfo_content_sign(cms, si, chain))
672 cms->d.signedData->encapContentInfo->partial = 0;
676 int CMS_SignerInfo_sign(CMS_SignerInfo *si)
678 EVP_MD_CTX *mctx = si->mctx;
679 EVP_PKEY_CTX *pctx = NULL;
680 unsigned char *abuf = NULL;
683 const EVP_MD *md = NULL;
685 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
689 if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0) {
690 if (!cms_add1_signingTime(si, NULL))
694 if (!CMS_si_check_attributes(si))
700 EVP_MD_CTX_reset(mctx);
701 if (EVP_DigestSignInit(mctx, &pctx, md, NULL, si->pkey) <= 0)
706 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
707 EVP_PKEY_CTRL_CMS_SIGN, 0, si) <= 0) {
708 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
712 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
713 ASN1_ITEM_rptr(CMS_Attributes_Sign));
716 if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
718 if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
721 abuf = OPENSSL_malloc(siglen);
724 if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
727 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
728 EVP_PKEY_CTRL_CMS_SIGN, 1, si) <= 0) {
729 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
733 EVP_MD_CTX_reset(mctx);
735 ASN1_STRING_set0(si->signature, abuf, siglen);
741 EVP_MD_CTX_reset(mctx);
745 int CMS_SignerInfo_verify(CMS_SignerInfo *si)
747 EVP_MD_CTX *mctx = NULL;
748 unsigned char *abuf = NULL;
750 const EVP_MD *md = NULL;
753 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_NO_PUBLIC_KEY);
757 if (!CMS_si_check_attributes(si))
760 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
763 if (si->mctx == NULL && (si->mctx = EVP_MD_CTX_new()) == NULL) {
764 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, ERR_R_MALLOC_FAILURE);
768 if (EVP_DigestVerifyInit(mctx, &si->pctx, md, NULL, si->pkey) <= 0)
771 if (!cms_sd_asn1_ctrl(si, 1))
774 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
775 ASN1_ITEM_rptr(CMS_Attributes_Verify));
778 r = EVP_DigestVerifyUpdate(mctx, abuf, alen);
784 r = EVP_DigestVerifyFinal(mctx,
785 si->signature->data, si->signature->length);
787 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE);
789 EVP_MD_CTX_reset(mctx);
793 /* Create a chain of digest BIOs from a CMS ContentInfo */
795 BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms)
800 sd = cms_get0_signed(cms);
803 if (cms->d.signedData->encapContentInfo->partial)
804 cms_sd_set_version(sd);
805 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
806 X509_ALGOR *digestAlgorithm;
808 digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
809 mdbio = cms_DigestAlgorithm_init_bio(digestAlgorithm);
813 BIO_push(chain, mdbio);
823 int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
825 ASN1_OCTET_STRING *os = NULL;
826 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
827 EVP_PKEY_CTX *pkctx = NULL;
829 unsigned char mval[EVP_MAX_MD_SIZE];
833 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, ERR_R_MALLOC_FAILURE);
836 /* If we have any signed attributes look for messageDigest value */
837 if (CMS_signed_get_attr_count(si) >= 0) {
838 os = CMS_signed_get0_data_by_OBJ(si,
839 OBJ_nid2obj(NID_pkcs9_messageDigest),
840 -3, V_ASN1_OCTET_STRING);
842 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
843 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
848 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
851 if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {
852 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
853 CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
857 /* If messageDigest found compare it */
860 if (mlen != (unsigned int)os->length) {
861 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
862 CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
866 if (memcmp(mval, os->data, mlen)) {
867 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
868 CMS_R_VERIFICATION_FAILURE);
873 const EVP_MD *md = EVP_MD_CTX_md(mctx);
874 pkctx = EVP_PKEY_CTX_new(si->pkey, NULL);
877 if (EVP_PKEY_verify_init(pkctx) <= 0)
879 if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)
882 if (!cms_sd_asn1_ctrl(si, 1))
884 r = EVP_PKEY_verify(pkctx, si->signature->data,
885 si->signature->length, mval, mlen);
887 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
888 CMS_R_VERIFICATION_FAILURE);
894 EVP_PKEY_CTX_free(pkctx);
895 EVP_MD_CTX_free(mctx);
900 int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
902 unsigned char *smder = NULL;
904 smderlen = i2d_X509_ALGORS(algs, &smder);
907 r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
908 V_ASN1_SEQUENCE, smder, smderlen);
913 int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
914 int algnid, int keysize)
917 ASN1_INTEGER *key = NULL;
919 key = ASN1_INTEGER_new();
920 if (key == NULL || !ASN1_INTEGER_set(key, keysize))
923 alg = X509_ALGOR_new();
925 ASN1_INTEGER_free(key);
929 X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),
930 key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
932 *algs = sk_X509_ALGOR_new_null();
933 if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) {
934 X509_ALGOR_free(alg);
940 /* Check to see if a cipher exists and if so add S/MIME capabilities */
942 static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
944 if (EVP_get_cipherbynid(nid))
945 return CMS_add_simple_smimecap(sk, nid, arg);
949 static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
951 if (EVP_get_digestbynid(nid))
952 return CMS_add_simple_smimecap(sk, nid, arg);
956 int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
958 if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
959 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
960 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
961 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
962 || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
963 || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
964 || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
965 || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
966 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
967 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
968 || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
969 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))