1 /* crypto/cms/cms_sd.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
55 #include <openssl/asn1t.h>
56 #include <openssl/pem.h>
57 #include <openssl/x509v3.h>
58 #include <openssl/err.h>
59 #include <openssl/cms.h>
62 /* CMS SignedData Utilities */
64 DECLARE_ASN1_ITEM(CMS_SignedData)
66 static CMS_SignedData *cms_get0_signed(CMS_ContentInfo *cms)
68 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_signed)
70 CMSerr(CMS_F_CMS_GET0_SIGNED, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA);
73 return cms->d.signedData;
76 static CMS_SignedData *cms_signed_data_init(CMS_ContentInfo *cms)
78 if (cms->d.other == NULL)
80 cms->d.signedData = M_ASN1_new_of(CMS_SignedData);
81 if (!cms->d.signedData)
83 CMSerr(CMS_F_CMS_SIGNED_DATA_INIT, ERR_R_MALLOC_FAILURE);
86 cms->d.signedData->version = 1;
87 cms->d.signedData->encapContentInfo->eContentType =
88 OBJ_nid2obj(NID_pkcs7_data);
89 cms->d.signedData->encapContentInfo->partial = 1;
90 ASN1_OBJECT_free(cms->contentType);
91 cms->contentType = OBJ_nid2obj(NID_pkcs7_signed);
92 return cms->d.signedData;
94 return cms_get0_signed(cms);
97 /* Just initialize SignedData e.g. for certs only structure */
99 int CMS_SignedData_init(CMS_ContentInfo *cms)
101 if (cms_signed_data_init(cms))
107 /* Check structures and fixup version numbers (if necessary) */
109 static void cms_sd_set_version(CMS_SignedData *sd)
112 CMS_CertificateChoices *cch;
113 CMS_RevocationInfoChoice *rch;
116 for (i = 0; i < sk_CMS_CertificateChoices_num(sd->certificates); i++)
118 cch = sk_CMS_CertificateChoices_value(sd->certificates, i);
119 if (cch->type == CMS_CERTCHOICE_OTHER)
124 else if (cch->type == CMS_CERTCHOICE_V2ACERT)
129 else if (cch->type == CMS_CERTCHOICE_V1ACERT)
136 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(sd->crls); i++)
138 rch = sk_CMS_RevocationInfoChoice_value(sd->crls, i);
139 if (rch->type == CMS_REVCHOICE_OTHER)
146 if ((OBJ_obj2nid(sd->encapContentInfo->eContentType) != NID_pkcs7_data)
147 && (sd->version < 3))
150 for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++)
152 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
153 if (si->sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
169 /* Copy an existing messageDigest value */
171 static int cms_copy_messageDigest(CMS_ContentInfo *cms, CMS_SignerInfo *si)
173 STACK_OF(CMS_SignerInfo) *sinfos;
174 CMS_SignerInfo *sitmp;
176 sinfos = CMS_get0_SignerInfos(cms);
177 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
179 ASN1_OCTET_STRING *messageDigest;
180 sitmp = sk_CMS_SignerInfo_value(sinfos, i);
183 if (CMS_signed_get_attr_count(sitmp) < 0)
185 if (OBJ_cmp(si->digestAlgorithm->algorithm,
186 sitmp->digestAlgorithm->algorithm))
188 messageDigest = CMS_signed_get0_data_by_OBJ(sitmp,
189 OBJ_nid2obj(NID_pkcs9_messageDigest),
190 -3, V_ASN1_OCTET_STRING);
193 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST,
194 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
198 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
205 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST, CMS_R_NO_MATCHING_DIGEST);
209 int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert, int type)
213 case CMS_SIGNERINFO_ISSUER_SERIAL:
214 sid->d.issuerAndSerialNumber =
215 M_ASN1_new_of(CMS_IssuerAndSerialNumber);
216 if (!sid->d.issuerAndSerialNumber)
218 if (!X509_NAME_set(&sid->d.issuerAndSerialNumber->issuer,
219 X509_get_issuer_name(cert)))
221 ASN1_STRING_free(sid->d.issuerAndSerialNumber->serialNumber);
222 sid->d.issuerAndSerialNumber->serialNumber =
223 ASN1_STRING_dup(X509_get_serialNumber(cert));
224 if(!sid->d.issuerAndSerialNumber->serialNumber)
228 case CMS_SIGNERINFO_KEYIDENTIFIER:
231 CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER,
232 CMS_R_CERTIFICATE_HAS_NO_KEYID);
235 sid->d.subjectKeyIdentifier = ASN1_STRING_dup(cert->skid);
236 if (!sid->d.subjectKeyIdentifier)
241 CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, CMS_R_UNKNOWN_ID);
250 CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, ERR_R_MALLOC_FAILURE);
255 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
256 ASN1_OCTET_STRING **keyid,
257 X509_NAME **issuer, ASN1_INTEGER **sno)
259 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
262 *issuer = sid->d.issuerAndSerialNumber->issuer;
264 *sno = sid->d.issuerAndSerialNumber->serialNumber;
266 else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
269 *keyid = sid->d.subjectKeyIdentifier;
276 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert)
279 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
281 ret = X509_NAME_cmp(sid->d.issuerAndSerialNumber->issuer,
282 X509_get_issuer_name(cert));
285 return ASN1_INTEGER_cmp(sid->d.issuerAndSerialNumber->serialNumber,
286 X509_get_serialNumber(cert));
288 else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
290 X509_check_purpose(cert, -1, -1);
293 return ASN1_OCTET_STRING_cmp(sid->d.subjectKeyIdentifier,
300 CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
301 X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
305 CMS_SignerInfo *si = NULL;
308 if(!X509_check_private_key(signer, pk))
310 CMSerr(CMS_F_CMS_ADD1_SIGNER,
311 CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
314 sd = cms_signed_data_init(cms);
317 si = M_ASN1_new_of(CMS_SignerInfo);
320 X509_check_purpose(signer, -1, -1);
322 CRYPTO_add(&pk->references, 1, CRYPTO_LOCK_EVP_PKEY);
323 CRYPTO_add(&signer->references, 1, CRYPTO_LOCK_X509);
328 if (flags & CMS_USE_KEYID)
333 type = CMS_SIGNERINFO_KEYIDENTIFIER;
337 type = CMS_SIGNERINFO_ISSUER_SERIAL;
341 if (!cms_set1_SignerIdentifier(si->sid, signer, type))
344 /* Since no EVP_PKEY_METHOD in 0.9.8 hard code SHA1 as default */
348 /* OpenSSL 0.9.8 only supports SHA1 with non-RSA keys */
350 if ((pk->type != EVP_PKEY_RSA) && (EVP_MD_type(md) != NID_sha1))
352 CMSerr(CMS_F_CMS_ADD1_SIGNER,
353 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
357 cms_DigestAlgorithm_set(si->digestAlgorithm, md);
359 /* See if digest is present in digestAlgorithms */
360 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++)
363 alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
364 X509_ALGOR_get0(&aoid, NULL, NULL, alg);
365 if (OBJ_obj2nid(aoid) == EVP_MD_type(md))
369 if (i == sk_X509_ALGOR_num(sd->digestAlgorithms))
371 alg = X509_ALGOR_new();
374 cms_DigestAlgorithm_set(alg, md);
375 if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg))
377 X509_ALGOR_free(alg);
382 /* Since we have no EVP_PKEY_ASN1_METHOD in OpenSSL 0.9.8,
383 * hard code algorithm parameters.
390 X509_ALGOR_set0(si->signatureAlgorithm,
391 OBJ_nid2obj(NID_rsaEncryption),
396 X509_ALGOR_set0(si->signatureAlgorithm,
397 OBJ_nid2obj(NID_dsaWithSHA1),
403 X509_ALGOR_set0(si->signatureAlgorithm,
404 OBJ_nid2obj(NID_ecdsa_with_SHA1),
409 CMSerr(CMS_F_CMS_ADD1_SIGNER,
410 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
415 if (!(flags & CMS_NOATTR))
417 /* Initialialize signed attributes strutucture so other
418 * attributes such as signing time etc are added later
419 * even if we add none here.
421 if (!si->signedAttrs)
423 si->signedAttrs = sk_X509_ATTRIBUTE_new_null();
424 if (!si->signedAttrs)
428 if (!(flags & CMS_NOSMIMECAP))
430 STACK_OF(X509_ALGOR) *smcap = NULL;
431 i = CMS_add_standard_smimecap(&smcap);
433 i = CMS_add_smimecap(si, smcap);
434 sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
438 if (flags & CMS_REUSE_DIGEST)
440 if (!cms_copy_messageDigest(cms, si))
442 if (!(flags & CMS_PARTIAL) &&
443 !CMS_SignerInfo_sign(si))
448 if (!(flags & CMS_NOCERTS))
450 /* NB ignore -1 return for duplicate cert */
451 if (!CMS_add1_cert(cms, signer))
455 if (!sd->signerInfos)
456 sd->signerInfos = sk_CMS_SignerInfo_new_null();
457 if (!sd->signerInfos ||
458 !sk_CMS_SignerInfo_push(sd->signerInfos, si))
464 CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
467 M_ASN1_free_of(si, CMS_SignerInfo);
472 static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
479 tt = X509_gmtime_adj(NULL, 0);
484 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
485 tt->type, tt, -1) <= 0)
496 CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME, ERR_R_MALLOC_FAILURE);
502 STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
505 sd = cms_get0_signed(cms);
508 return sd->signerInfos;
511 STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
513 STACK_OF(X509) *signers = NULL;
514 STACK_OF(CMS_SignerInfo) *sinfos;
517 sinfos = CMS_get0_SignerInfos(cms);
518 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
520 si = sk_CMS_SignerInfo_value(sinfos, i);
525 signers = sk_X509_new_null();
529 if (!sk_X509_push(signers, si->signer))
531 sk_X509_free(signers);
539 void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
543 CRYPTO_add(&signer->references, 1, CRYPTO_LOCK_X509);
545 EVP_PKEY_free(si->pkey);
546 si->pkey = X509_get_pubkey(signer);
549 X509_free(si->signer);
553 int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
554 ASN1_OCTET_STRING **keyid,
555 X509_NAME **issuer, ASN1_INTEGER **sno)
557 return cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
560 int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
562 return cms_SignerIdentifier_cert_cmp(si->sid, cert);
565 int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,
570 CMS_CertificateChoices *cch;
571 STACK_OF(CMS_CertificateChoices) *certs;
575 sd = cms_get0_signed(cms);
578 certs = sd->certificates;
579 for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++)
581 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
585 for (j = 0; j < sk_X509_num(scerts); j++)
587 x = sk_X509_value(scerts, j);
588 if (CMS_SignerInfo_cert_cmp(si, x) == 0)
590 CMS_SignerInfo_set1_signer_cert(si, x);
596 if (si->signer || (flags & CMS_NOINTERN))
599 for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++)
601 cch = sk_CMS_CertificateChoices_value(certs, j);
604 x = cch->d.certificate;
605 if (CMS_SignerInfo_cert_cmp(si, x) == 0)
607 CMS_SignerInfo_set1_signer_cert(si, x);
616 void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk, X509 **signer,
617 X509_ALGOR **pdig, X509_ALGOR **psig)
622 *signer = si->signer;
624 *pdig = si->digestAlgorithm;
626 *psig = si->signatureAlgorithm;
629 /* In OpenSSL 0.9.8 we have the link between digest types and public
630 * key types so we need to fixup the digest type if the public key
631 * type is not appropriate.
634 static void cms_fixup_mctx(EVP_MD_CTX *mctx, EVP_PKEY *pkey)
636 if (EVP_MD_CTX_type(mctx) != NID_sha1)
638 #ifndef OPENSSL_NO_DSA
639 if (pkey->type == EVP_PKEY_DSA)
640 mctx->digest = EVP_dss1();
642 #ifndef OPENSSL_NO_ECDSA
643 if (pkey->type == EVP_PKEY_EC)
644 mctx->digest = EVP_ecdsa();
648 static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
649 CMS_SignerInfo *si, BIO *chain)
653 EVP_MD_CTX_init(&mctx);
658 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);
662 if (!cms_DigestAlgorithm_find_ctx(&mctx, chain, si->digestAlgorithm))
665 /* If any signed attributes calculate and add messageDigest attribute */
667 if (CMS_signed_get_attr_count(si) >= 0)
670 cms->d.signedData->encapContentInfo->eContentType;
671 unsigned char md[EVP_MAX_MD_SIZE];
673 EVP_DigestFinal_ex(&mctx, md, &mdlen);
674 if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
678 /* Copy content type across */
679 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,
680 V_ASN1_OBJECT, ctype, -1) <= 0)
682 if (!CMS_SignerInfo_sign(si))
689 sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey));
692 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN,
693 ERR_R_MALLOC_FAILURE);
696 cms_fixup_mctx(&mctx, si->pkey);
697 if (!EVP_SignFinal(&mctx, sig, &siglen, si->pkey))
699 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN,
700 CMS_R_SIGNFINAL_ERROR);
704 ASN1_STRING_set0(si->signature, sig, siglen);
710 EVP_MD_CTX_cleanup(&mctx);
715 int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain)
717 STACK_OF(CMS_SignerInfo) *sinfos;
720 sinfos = CMS_get0_SignerInfos(cms);
721 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
723 si = sk_CMS_SignerInfo_value(sinfos, i);
724 if (!cms_SignerInfo_content_sign(cms, si, chain))
727 cms->d.signedData->encapContentInfo->partial = 0;
731 int CMS_SignerInfo_sign(CMS_SignerInfo *si)
734 unsigned char *abuf = NULL;
737 const EVP_MD *md = NULL;
739 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
743 EVP_MD_CTX_init(&mctx);
745 if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0)
747 if (!cms_add1_signingTime(si, NULL))
751 if (EVP_SignInit_ex(&mctx, md, NULL) <= 0)
755 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
756 EVP_PKEY_CTRL_CMS_SIGN, 0, si) <= 0)
758 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
763 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs,&abuf,
764 ASN1_ITEM_rptr(CMS_Attributes_Sign));
767 if (EVP_SignUpdate(&mctx, abuf, alen) <= 0)
769 siglen = EVP_PKEY_size(si->pkey);
771 abuf = OPENSSL_malloc(siglen);
774 cms_fixup_mctx(&mctx, si->pkey);
775 if (EVP_SignFinal(&mctx, abuf, &siglen, si->pkey) <= 0)
778 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
779 EVP_PKEY_CTRL_CMS_SIGN, 1, si) <= 0)
781 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
785 EVP_MD_CTX_cleanup(&mctx);
787 ASN1_STRING_set0(si->signature, abuf, siglen);
794 EVP_MD_CTX_cleanup(&mctx);
799 int CMS_SignerInfo_verify(CMS_SignerInfo *si)
802 unsigned char *abuf = NULL;
804 const EVP_MD *md = NULL;
808 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_NO_PUBLIC_KEY);
812 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
815 EVP_MD_CTX_init(&mctx);
816 if (EVP_VerifyInit_ex(&mctx, md, NULL) <= 0)
819 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs,&abuf,
820 ASN1_ITEM_rptr(CMS_Attributes_Verify));
823 r = EVP_VerifyUpdate(&mctx, abuf, alen);
830 cms_fixup_mctx(&mctx, si->pkey);
831 r = EVP_VerifyFinal(&mctx,
832 si->signature->data, si->signature->length, si->pkey);
834 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE);
836 EVP_MD_CTX_cleanup(&mctx);
840 /* Create a chain of digest BIOs from a CMS ContentInfo */
842 BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms)
847 sd = cms_get0_signed(cms);
850 if (cms->d.signedData->encapContentInfo->partial)
851 cms_sd_set_version(sd);
852 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++)
854 X509_ALGOR *digestAlgorithm;
856 digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
857 mdbio = cms_DigestAlgorithm_init_bio(digestAlgorithm);
861 BIO_push(chain, mdbio);
872 int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
874 ASN1_OCTET_STRING *os = NULL;
877 EVP_MD_CTX_init(&mctx);
878 /* If we have any signed attributes look for messageDigest value */
879 if (CMS_signed_get_attr_count(si) >= 0)
881 os = CMS_signed_get0_data_by_OBJ(si,
882 OBJ_nid2obj(NID_pkcs9_messageDigest),
883 -3, V_ASN1_OCTET_STRING);
886 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
887 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
892 if (!cms_DigestAlgorithm_find_ctx(&mctx, chain, si->digestAlgorithm))
895 /* If messageDigest found compare it */
899 unsigned char mval[EVP_MAX_MD_SIZE];
901 if (EVP_DigestFinal_ex(&mctx, mval, &mlen) <= 0)
903 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
904 CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
907 if (mlen != (unsigned int)os->length)
909 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
910 CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
914 if (memcmp(mval, os->data, mlen))
916 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
917 CMS_R_VERIFICATION_FAILURE);
925 cms_fixup_mctx(&mctx, si->pkey);
926 r = EVP_VerifyFinal(&mctx, si->signature->data,
927 si->signature->length, si->pkey);
930 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
931 CMS_R_VERIFICATION_FAILURE);
937 EVP_MD_CTX_cleanup(&mctx);
942 int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
944 unsigned char *smder = NULL;
946 smderlen = i2d_X509_ALGORS(algs, &smder);
949 r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
950 V_ASN1_SEQUENCE, smder, smderlen);
955 int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
956 int algnid, int keysize)
959 ASN1_INTEGER *key = NULL;
962 key = ASN1_INTEGER_new();
963 if (!key || !ASN1_INTEGER_set(key, keysize))
966 alg = X509_ALGOR_new();
970 ASN1_INTEGER_free(key);
974 X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),
975 key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
977 *algs = sk_X509_ALGOR_new_null();
978 if (!*algs || !sk_X509_ALGOR_push(*algs, alg))
980 X509_ALGOR_free(alg);
986 /* Check to see if a cipher exists and if so add S/MIME capabilities */
988 static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
990 if (EVP_get_cipherbynid(nid))
991 return CMS_add_simple_smimecap(sk, nid, arg);
995 static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
997 if (EVP_get_digestbynid(nid))
998 return CMS_add_simple_smimecap(sk, nid, arg);
1002 int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
1004 if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
1005 || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
1006 || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
1007 || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
1008 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
1009 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
1010 || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
1011 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))