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>
17 #include <openssl/ess.h>
19 #include "internal/asn1_int.h"
20 #include "internal/evp_int.h"
21 #include "internal/cms_int.h"
22 #include "internal/ess_int.h"
24 /* CMS SignedData Utilities */
26 static CMS_SignedData *cms_get0_signed(CMS_ContentInfo *cms)
28 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_signed) {
29 CMSerr(CMS_F_CMS_GET0_SIGNED, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA);
32 return cms->d.signedData;
35 static CMS_SignedData *cms_signed_data_init(CMS_ContentInfo *cms)
37 if (cms->d.other == NULL) {
38 cms->d.signedData = M_ASN1_new_of(CMS_SignedData);
39 if (!cms->d.signedData) {
40 CMSerr(CMS_F_CMS_SIGNED_DATA_INIT, ERR_R_MALLOC_FAILURE);
43 cms->d.signedData->version = 1;
44 cms->d.signedData->encapContentInfo->eContentType =
45 OBJ_nid2obj(NID_pkcs7_data);
46 cms->d.signedData->encapContentInfo->partial = 1;
47 ASN1_OBJECT_free(cms->contentType);
48 cms->contentType = OBJ_nid2obj(NID_pkcs7_signed);
49 return cms->d.signedData;
51 return cms_get0_signed(cms);
54 /* Just initialise SignedData e.g. for certs only structure */
56 int CMS_SignedData_init(CMS_ContentInfo *cms)
58 if (cms_signed_data_init(cms))
64 /* Check structures and fixup version numbers (if necessary) */
66 static void cms_sd_set_version(CMS_SignedData *sd)
69 CMS_CertificateChoices *cch;
70 CMS_RevocationInfoChoice *rch;
73 for (i = 0; i < sk_CMS_CertificateChoices_num(sd->certificates); i++) {
74 cch = sk_CMS_CertificateChoices_value(sd->certificates, i);
75 if (cch->type == CMS_CERTCHOICE_OTHER) {
78 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
81 } else if (cch->type == CMS_CERTCHOICE_V1ACERT) {
87 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(sd->crls); i++) {
88 rch = sk_CMS_RevocationInfoChoice_value(sd->crls, i);
89 if (rch->type == CMS_REVCHOICE_OTHER) {
95 if ((OBJ_obj2nid(sd->encapContentInfo->eContentType) != NID_pkcs7_data)
99 for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
100 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
101 if (si->sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
106 } else if (si->version < 1)
116 * RFC 5652 Section 11.1 Content Type
117 * The content-type attribute within signed-data MUST
118 * 1) be present if there are signed attributes
119 * 2) match the content type in the signed-data,
120 * 3) be a signed attribute.
121 * 4) not have more than one copy of the attribute.
123 * Note that since the CMS_SignerInfo_sign() always adds the "signing time"
124 * attribute, the content type attribute MUST be added also.
125 * Assumptions: This assumes that the attribute does not already exist.
127 static int cms_set_si_contentType_attr(CMS_ContentInfo *cms, CMS_SignerInfo *si)
129 ASN1_OBJECT *ctype = cms->d.signedData->encapContentInfo->eContentType;
131 /* Add the contentType attribute */
132 return CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,
133 V_ASN1_OBJECT, ctype, -1) > 0;
136 /* Copy an existing messageDigest value */
138 static int cms_copy_messageDigest(CMS_ContentInfo *cms, CMS_SignerInfo *si)
140 STACK_OF(CMS_SignerInfo) *sinfos;
141 CMS_SignerInfo *sitmp;
143 sinfos = CMS_get0_SignerInfos(cms);
144 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
145 ASN1_OCTET_STRING *messageDigest;
146 sitmp = sk_CMS_SignerInfo_value(sinfos, i);
149 if (CMS_signed_get_attr_count(sitmp) < 0)
151 if (OBJ_cmp(si->digestAlgorithm->algorithm,
152 sitmp->digestAlgorithm->algorithm))
154 messageDigest = CMS_signed_get0_data_by_OBJ(sitmp,
156 (NID_pkcs9_messageDigest),
157 -3, V_ASN1_OCTET_STRING);
158 if (!messageDigest) {
159 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST,
160 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
164 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
171 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST, CMS_R_NO_MATCHING_DIGEST);
175 int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert, int type)
178 case CMS_SIGNERINFO_ISSUER_SERIAL:
179 if (!cms_set1_ias(&sid->d.issuerAndSerialNumber, cert))
183 case CMS_SIGNERINFO_KEYIDENTIFIER:
184 if (!cms_set1_keyid(&sid->d.subjectKeyIdentifier, cert))
189 CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, CMS_R_UNKNOWN_ID);
198 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
199 ASN1_OCTET_STRING **keyid,
203 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL) {
205 *issuer = sid->d.issuerAndSerialNumber->issuer;
207 *sno = sid->d.issuerAndSerialNumber->serialNumber;
208 } else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
210 *keyid = sid->d.subjectKeyIdentifier;
216 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert)
218 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
219 return cms_ias_cert_cmp(sid->d.issuerAndSerialNumber, cert);
220 else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
221 return cms_keyid_cert_cmp(sid->d.subjectKeyIdentifier, cert);
226 static int cms_sd_asn1_ctrl(CMS_SignerInfo *si, int cmd)
228 EVP_PKEY *pkey = si->pkey;
230 if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
232 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_SIGN, cmd, si);
234 CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
238 CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_CTRL_FAILURE);
244 CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
245 X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
249 CMS_SignerInfo *si = NULL;
252 if (!X509_check_private_key(signer, pk)) {
253 CMSerr(CMS_F_CMS_ADD1_SIGNER,
254 CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
257 sd = cms_signed_data_init(cms);
260 si = M_ASN1_new_of(CMS_SignerInfo);
263 /* Call for side-effect of computing hash and caching extensions */
264 X509_check_purpose(signer, -1, -1);
271 si->mctx = EVP_MD_CTX_new();
274 if (si->mctx == NULL) {
275 CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
279 if (flags & CMS_USE_KEYID) {
283 type = CMS_SIGNERINFO_KEYIDENTIFIER;
285 type = CMS_SIGNERINFO_ISSUER_SERIAL;
289 if (!cms_set1_SignerIdentifier(si->sid, signer, type))
294 if (EVP_PKEY_get_default_digest_nid(pk, &def_nid) <= 0)
296 md = EVP_get_digestbynid(def_nid);
298 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DEFAULT_DIGEST);
304 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DIGEST_SET);
308 X509_ALGOR_set_md(si->digestAlgorithm, md);
310 /* See if digest is present in digestAlgorithms */
311 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
312 const ASN1_OBJECT *aoid;
313 alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
314 X509_ALGOR_get0(&aoid, NULL, NULL, alg);
315 if (OBJ_obj2nid(aoid) == EVP_MD_type(md))
319 if (i == sk_X509_ALGOR_num(sd->digestAlgorithms)) {
320 alg = X509_ALGOR_new();
323 X509_ALGOR_set_md(alg, md);
324 if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg)) {
325 X509_ALGOR_free(alg);
330 if (!(flags & CMS_KEY_PARAM) && !cms_sd_asn1_ctrl(si, 0))
332 if (!(flags & CMS_NOATTR)) {
334 * Initialize signed attributes structure so other attributes
335 * such as signing time etc are added later even if we add none here.
337 if (!si->signedAttrs) {
338 si->signedAttrs = sk_X509_ATTRIBUTE_new_null();
339 if (!si->signedAttrs)
343 if (!(flags & CMS_NOSMIMECAP)) {
344 STACK_OF(X509_ALGOR) *smcap = NULL;
345 i = CMS_add_standard_smimecap(&smcap);
347 i = CMS_add_smimecap(si, smcap);
348 sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
352 if (flags & CMS_CADES) {
353 ESS_SIGNING_CERT *sc = NULL;
354 ESS_SIGNING_CERT_V2 *sc2 = NULL;
357 if (md == EVP_sha1() || md == NULL) {
358 if ((sc = ESS_SIGNING_CERT_new_init(signer,
361 add_sc = cms_add1_signing_cert(si, sc);
362 ESS_SIGNING_CERT_free(sc);
364 if ((sc2 = ESS_SIGNING_CERT_V2_new_init(md, signer,
367 add_sc = cms_add1_signing_cert_v2(si, sc2);
368 ESS_SIGNING_CERT_V2_free(sc2);
373 if (flags & CMS_REUSE_DIGEST) {
374 if (!cms_copy_messageDigest(cms, si))
376 if (!cms_set_si_contentType_attr(cms, si))
378 if (!(flags & (CMS_PARTIAL | CMS_KEY_PARAM)) &&
379 !CMS_SignerInfo_sign(si))
384 if (!(flags & CMS_NOCERTS)) {
385 /* NB ignore -1 return for duplicate cert */
386 if (!CMS_add1_cert(cms, signer))
390 if (flags & CMS_KEY_PARAM) {
391 if (flags & CMS_NOATTR) {
392 si->pctx = EVP_PKEY_CTX_new(si->pkey, NULL);
393 if (si->pctx == NULL)
395 if (EVP_PKEY_sign_init(si->pctx) <= 0)
397 if (EVP_PKEY_CTX_set_signature_md(si->pctx, md) <= 0)
399 } else if (EVP_DigestSignInit(si->mctx, &si->pctx, md, NULL, pk) <= 0)
403 if (!sd->signerInfos)
404 sd->signerInfos = sk_CMS_SignerInfo_new_null();
405 if (!sd->signerInfos || !sk_CMS_SignerInfo_push(sd->signerInfos, si))
411 CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
413 M_ASN1_free_of(si, CMS_SignerInfo);
418 static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
425 tt = X509_gmtime_adj(NULL, 0);
430 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
431 tt->type, tt, -1) <= 0)
442 CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME, ERR_R_MALLOC_FAILURE);
448 EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si)
453 EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si)
458 STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
461 sd = cms_get0_signed(cms);
464 return sd->signerInfos;
467 STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
469 STACK_OF(X509) *signers = NULL;
470 STACK_OF(CMS_SignerInfo) *sinfos;
473 sinfos = CMS_get0_SignerInfos(cms);
474 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
475 si = sk_CMS_SignerInfo_value(sinfos, i);
478 signers = sk_X509_new_null();
482 if (!sk_X509_push(signers, si->signer)) {
483 sk_X509_free(signers);
491 void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
495 EVP_PKEY_free(si->pkey);
496 si->pkey = X509_get_pubkey(signer);
498 X509_free(si->signer);
502 int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
503 ASN1_OCTET_STRING **keyid,
504 X509_NAME **issuer, ASN1_INTEGER **sno)
506 return cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
509 int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
511 return cms_SignerIdentifier_cert_cmp(si->sid, cert);
514 int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,
519 CMS_CertificateChoices *cch;
520 STACK_OF(CMS_CertificateChoices) *certs;
524 sd = cms_get0_signed(cms);
527 certs = sd->certificates;
528 for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
529 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
533 for (j = 0; j < sk_X509_num(scerts); j++) {
534 x = sk_X509_value(scerts, j);
535 if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
536 CMS_SignerInfo_set1_signer_cert(si, x);
542 if (si->signer || (flags & CMS_NOINTERN))
545 for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++) {
546 cch = sk_CMS_CertificateChoices_value(certs, j);
549 x = cch->d.certificate;
550 if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
551 CMS_SignerInfo_set1_signer_cert(si, x);
560 void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk,
561 X509 **signer, X509_ALGOR **pdig,
567 *signer = si->signer;
569 *pdig = si->digestAlgorithm;
571 *psig = si->signatureAlgorithm;
574 ASN1_OCTET_STRING *CMS_SignerInfo_get0_signature(CMS_SignerInfo *si)
576 return si->signature;
579 static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
580 CMS_SignerInfo *si, BIO *chain)
582 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
584 EVP_PKEY_CTX *pctx = NULL;
587 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
592 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);
596 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
598 /* Set SignerInfo algorithm details if we used custom parameter */
599 if (si->pctx && !cms_sd_asn1_ctrl(si, 0))
603 * If any signed attributes calculate and add messageDigest attribute
606 if (CMS_signed_get_attr_count(si) >= 0) {
607 unsigned char md[EVP_MAX_MD_SIZE];
609 if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
611 if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
612 V_ASN1_OCTET_STRING, md, mdlen))
614 /* Copy content type across */
615 if (!cms_set_si_contentType_attr(cms, si))
618 if (!CMS_SignerInfo_sign(si))
620 } else if (si->pctx) {
623 unsigned char md[EVP_MAX_MD_SIZE];
626 if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
628 siglen = EVP_PKEY_size(si->pkey);
629 sig = OPENSSL_malloc(siglen);
631 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
634 if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) {
638 ASN1_STRING_set0(si->signature, sig, siglen);
642 sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey));
644 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
647 if (!EVP_SignFinal(mctx, sig, &siglen, si->pkey)) {
648 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_SIGNFINAL_ERROR);
652 ASN1_STRING_set0(si->signature, sig, siglen);
658 EVP_MD_CTX_free(mctx);
659 EVP_PKEY_CTX_free(pctx);
664 int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain)
666 STACK_OF(CMS_SignerInfo) *sinfos;
669 sinfos = CMS_get0_SignerInfos(cms);
670 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
671 si = sk_CMS_SignerInfo_value(sinfos, i);
672 if (!cms_SignerInfo_content_sign(cms, si, chain))
675 cms->d.signedData->encapContentInfo->partial = 0;
679 int CMS_SignerInfo_sign(CMS_SignerInfo *si)
681 EVP_MD_CTX *mctx = si->mctx;
682 EVP_PKEY_CTX *pctx = NULL;
683 unsigned char *abuf = NULL;
686 const EVP_MD *md = NULL;
688 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
692 if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0) {
693 if (!cms_add1_signingTime(si, NULL))
697 if (!CMS_si_check_attributes(si))
703 EVP_MD_CTX_reset(mctx);
704 if (EVP_DigestSignInit(mctx, &pctx, md, NULL, si->pkey) <= 0)
709 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
710 EVP_PKEY_CTRL_CMS_SIGN, 0, si) <= 0) {
711 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
715 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
716 ASN1_ITEM_rptr(CMS_Attributes_Sign));
719 if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
721 if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
724 abuf = OPENSSL_malloc(siglen);
727 if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
730 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
731 EVP_PKEY_CTRL_CMS_SIGN, 1, si) <= 0) {
732 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
736 EVP_MD_CTX_reset(mctx);
738 ASN1_STRING_set0(si->signature, abuf, siglen);
744 EVP_MD_CTX_reset(mctx);
748 int CMS_SignerInfo_verify(CMS_SignerInfo *si)
750 EVP_MD_CTX *mctx = NULL;
751 unsigned char *abuf = NULL;
753 const EVP_MD *md = NULL;
756 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_NO_PUBLIC_KEY);
760 if (!CMS_si_check_attributes(si))
763 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
766 if (si->mctx == NULL && (si->mctx = EVP_MD_CTX_new()) == NULL) {
767 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, ERR_R_MALLOC_FAILURE);
771 if (EVP_DigestVerifyInit(mctx, &si->pctx, md, NULL, si->pkey) <= 0)
774 if (!cms_sd_asn1_ctrl(si, 1))
777 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
778 ASN1_ITEM_rptr(CMS_Attributes_Verify));
781 r = EVP_DigestVerifyUpdate(mctx, abuf, alen);
787 r = EVP_DigestVerifyFinal(mctx,
788 si->signature->data, si->signature->length);
790 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE);
792 EVP_MD_CTX_reset(mctx);
796 /* Create a chain of digest BIOs from a CMS ContentInfo */
798 BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms)
803 sd = cms_get0_signed(cms);
806 if (cms->d.signedData->encapContentInfo->partial)
807 cms_sd_set_version(sd);
808 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
809 X509_ALGOR *digestAlgorithm;
811 digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
812 mdbio = cms_DigestAlgorithm_init_bio(digestAlgorithm);
816 BIO_push(chain, mdbio);
826 int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
828 ASN1_OCTET_STRING *os = NULL;
829 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
830 EVP_PKEY_CTX *pkctx = NULL;
832 unsigned char mval[EVP_MAX_MD_SIZE];
836 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, ERR_R_MALLOC_FAILURE);
839 /* If we have any signed attributes look for messageDigest value */
840 if (CMS_signed_get_attr_count(si) >= 0) {
841 os = CMS_signed_get0_data_by_OBJ(si,
842 OBJ_nid2obj(NID_pkcs9_messageDigest),
843 -3, V_ASN1_OCTET_STRING);
845 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
846 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
851 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
854 if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {
855 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
856 CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
860 /* If messageDigest found compare it */
863 if (mlen != (unsigned int)os->length) {
864 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
865 CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
869 if (memcmp(mval, os->data, mlen)) {
870 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
871 CMS_R_VERIFICATION_FAILURE);
876 const EVP_MD *md = EVP_MD_CTX_md(mctx);
877 pkctx = EVP_PKEY_CTX_new(si->pkey, NULL);
880 if (EVP_PKEY_verify_init(pkctx) <= 0)
882 if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)
885 if (!cms_sd_asn1_ctrl(si, 1))
887 r = EVP_PKEY_verify(pkctx, si->signature->data,
888 si->signature->length, mval, mlen);
890 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
891 CMS_R_VERIFICATION_FAILURE);
897 EVP_PKEY_CTX_free(pkctx);
898 EVP_MD_CTX_free(mctx);
903 int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
905 unsigned char *smder = NULL;
907 smderlen = i2d_X509_ALGORS(algs, &smder);
910 r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
911 V_ASN1_SEQUENCE, smder, smderlen);
916 int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
917 int algnid, int keysize)
920 ASN1_INTEGER *key = NULL;
922 key = ASN1_INTEGER_new();
923 if (key == NULL || !ASN1_INTEGER_set(key, keysize))
926 alg = X509_ALGOR_new();
928 ASN1_INTEGER_free(key);
932 X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),
933 key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
935 *algs = sk_X509_ALGOR_new_null();
936 if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) {
937 X509_ALGOR_free(alg);
943 /* Check to see if a cipher exists and if so add S/MIME capabilities */
945 static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
947 if (EVP_get_cipherbynid(nid))
948 return CMS_add_simple_smimecap(sk, nid, arg);
952 static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
954 if (EVP_get_digestbynid(nid))
955 return CMS_add_simple_smimecap(sk, nid, arg);
959 int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
961 if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
962 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
963 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
964 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
965 || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
966 || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
967 || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
968 || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
969 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
970 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
971 || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
972 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))