1 /* crypto/cms/cms_sd.c */
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
56 #include <openssl/asn1t.h>
57 #include <openssl/pem.h>
58 #include <openssl/x509.h>
59 #include <openssl/x509v3.h>
60 #include <openssl/err.h>
61 #include <openssl/cms.h>
63 #include "asn1_locl.h"
65 /* CMS SignedData Utilities */
67 DECLARE_ASN1_ITEM(CMS_SignedData
)
69 static CMS_SignedData
*cms_get0_signed(CMS_ContentInfo
*cms
)
71 if (OBJ_obj2nid(cms
->contentType
) != NID_pkcs7_signed
) {
72 CMSerr(CMS_F_CMS_GET0_SIGNED
, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA
);
75 return cms
->d
.signedData
;
78 static CMS_SignedData
*cms_signed_data_init(CMS_ContentInfo
*cms
)
80 if (cms
->d
.other
== NULL
) {
81 cms
->d
.signedData
= M_ASN1_new_of(CMS_SignedData
);
82 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
++) {
117 cch
= sk_CMS_CertificateChoices_value(sd
->certificates
, i
);
118 if (cch
->type
== CMS_CERTCHOICE_OTHER
) {
121 } else if (cch
->type
== CMS_CERTCHOICE_V2ACERT
) {
124 } else if (cch
->type
== CMS_CERTCHOICE_V1ACERT
) {
130 for (i
= 0; i
< sk_CMS_RevocationInfoChoice_num(sd
->crls
); i
++) {
131 rch
= sk_CMS_RevocationInfoChoice_value(sd
->crls
, i
);
132 if (rch
->type
== CMS_REVCHOICE_OTHER
) {
138 if ((OBJ_obj2nid(sd
->encapContentInfo
->eContentType
) != NID_pkcs7_data
)
139 && (sd
->version
< 3))
142 for (i
= 0; i
< sk_CMS_SignerInfo_num(sd
->signerInfos
); i
++) {
143 si
= sk_CMS_SignerInfo_value(sd
->signerInfos
, i
);
144 if (si
->sid
->type
== CMS_SIGNERINFO_KEYIDENTIFIER
) {
149 } else if (si
->version
< 1)
158 /* Copy an existing messageDigest value */
160 static int cms_copy_messageDigest(CMS_ContentInfo
*cms
, CMS_SignerInfo
*si
)
162 STACK_OF(CMS_SignerInfo
) *sinfos
;
163 CMS_SignerInfo
*sitmp
;
165 sinfos
= CMS_get0_SignerInfos(cms
);
166 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
167 ASN1_OCTET_STRING
*messageDigest
;
168 sitmp
= sk_CMS_SignerInfo_value(sinfos
, i
);
171 if (CMS_signed_get_attr_count(sitmp
) < 0)
173 if (OBJ_cmp(si
->digestAlgorithm
->algorithm
,
174 sitmp
->digestAlgorithm
->algorithm
))
176 messageDigest
= CMS_signed_get0_data_by_OBJ(sitmp
,
178 (NID_pkcs9_messageDigest
),
179 -3, V_ASN1_OCTET_STRING
);
180 if (!messageDigest
) {
181 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST
,
182 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE
);
186 if (CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_messageDigest
,
193 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST
, CMS_R_NO_MATCHING_DIGEST
);
197 int cms_set1_SignerIdentifier(CMS_SignerIdentifier
*sid
, X509
*cert
, int type
)
200 case CMS_SIGNERINFO_ISSUER_SERIAL
:
201 if (!cms_set1_ias(&sid
->d
.issuerAndSerialNumber
, cert
))
205 case CMS_SIGNERINFO_KEYIDENTIFIER
:
206 if (!cms_set1_keyid(&sid
->d
.subjectKeyIdentifier
, cert
))
211 CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER
, CMS_R_UNKNOWN_ID
);
220 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier
*sid
,
221 ASN1_OCTET_STRING
**keyid
,
225 if (sid
->type
== CMS_SIGNERINFO_ISSUER_SERIAL
) {
227 *issuer
= sid
->d
.issuerAndSerialNumber
->issuer
;
229 *sno
= sid
->d
.issuerAndSerialNumber
->serialNumber
;
230 } else if (sid
->type
== CMS_SIGNERINFO_KEYIDENTIFIER
) {
232 *keyid
= sid
->d
.subjectKeyIdentifier
;
238 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier
*sid
, X509
*cert
)
240 if (sid
->type
== CMS_SIGNERINFO_ISSUER_SERIAL
)
241 return cms_ias_cert_cmp(sid
->d
.issuerAndSerialNumber
, cert
);
242 else if (sid
->type
== CMS_SIGNERINFO_KEYIDENTIFIER
)
243 return cms_keyid_cert_cmp(sid
->d
.subjectKeyIdentifier
, cert
);
248 static int cms_sd_asn1_ctrl(CMS_SignerInfo
*si
, int cmd
)
250 EVP_PKEY
*pkey
= si
->pkey
;
252 if (!pkey
->ameth
|| !pkey
->ameth
->pkey_ctrl
)
254 i
= pkey
->ameth
->pkey_ctrl(pkey
, ASN1_PKEY_CTRL_CMS_SIGN
, cmd
, si
);
256 CMSerr(CMS_F_CMS_SD_ASN1_CTRL
, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE
);
260 CMSerr(CMS_F_CMS_SD_ASN1_CTRL
, CMS_R_CTRL_FAILURE
);
266 CMS_SignerInfo
*CMS_add1_signer(CMS_ContentInfo
*cms
,
267 X509
*signer
, EVP_PKEY
*pk
, const EVP_MD
*md
,
271 CMS_SignerInfo
*si
= NULL
;
274 if (!X509_check_private_key(signer
, pk
)) {
275 CMSerr(CMS_F_CMS_ADD1_SIGNER
,
276 CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE
);
279 sd
= cms_signed_data_init(cms
);
282 si
= M_ASN1_new_of(CMS_SignerInfo
);
285 X509_check_purpose(signer
, -1, -1);
287 CRYPTO_add(&pk
->references
, 1, CRYPTO_LOCK_EVP_PKEY
);
288 CRYPTO_add(&signer
->references
, 1, CRYPTO_LOCK_X509
);
292 EVP_MD_CTX_init(&si
->mctx
);
295 if (flags
& CMS_USE_KEYID
) {
299 type
= CMS_SIGNERINFO_KEYIDENTIFIER
;
301 type
= CMS_SIGNERINFO_ISSUER_SERIAL
;
305 if (!cms_set1_SignerIdentifier(si
->sid
, signer
, type
))
310 if (EVP_PKEY_get_default_digest_nid(pk
, &def_nid
) <= 0)
312 md
= EVP_get_digestbynid(def_nid
);
314 CMSerr(CMS_F_CMS_ADD1_SIGNER
, CMS_R_NO_DEFAULT_DIGEST
);
320 CMSerr(CMS_F_CMS_ADD1_SIGNER
, CMS_R_NO_DIGEST_SET
);
324 X509_ALGOR_set_md(si
->digestAlgorithm
, md
);
326 /* See if digest is present in digestAlgorithms */
327 for (i
= 0; i
< sk_X509_ALGOR_num(sd
->digestAlgorithms
); i
++) {
329 alg
= sk_X509_ALGOR_value(sd
->digestAlgorithms
, i
);
330 X509_ALGOR_get0(&aoid
, NULL
, NULL
, alg
);
331 if (OBJ_obj2nid(aoid
) == EVP_MD_type(md
))
335 if (i
== sk_X509_ALGOR_num(sd
->digestAlgorithms
)) {
336 alg
= X509_ALGOR_new();
339 X509_ALGOR_set_md(alg
, md
);
340 if (!sk_X509_ALGOR_push(sd
->digestAlgorithms
, alg
)) {
341 X509_ALGOR_free(alg
);
346 if (!(flags
& CMS_KEY_PARAM
) && !cms_sd_asn1_ctrl(si
, 0))
348 if (!(flags
& CMS_NOATTR
)) {
350 * Initialialize signed attributes strutucture so other attributes
351 * such as signing time etc are added later even if we add none here.
353 if (!si
->signedAttrs
) {
354 si
->signedAttrs
= sk_X509_ATTRIBUTE_new_null();
355 if (!si
->signedAttrs
)
359 if (!(flags
& CMS_NOSMIMECAP
)) {
360 STACK_OF(X509_ALGOR
) *smcap
= NULL
;
361 i
= CMS_add_standard_smimecap(&smcap
);
363 i
= CMS_add_smimecap(si
, smcap
);
364 sk_X509_ALGOR_pop_free(smcap
, X509_ALGOR_free
);
368 if (flags
& CMS_REUSE_DIGEST
) {
369 if (!cms_copy_messageDigest(cms
, si
))
371 if (!(flags
& (CMS_PARTIAL
| CMS_KEY_PARAM
)) &&
372 !CMS_SignerInfo_sign(si
))
377 if (!(flags
& CMS_NOCERTS
)) {
378 /* NB ignore -1 return for duplicate cert */
379 if (!CMS_add1_cert(cms
, signer
))
383 if (flags
& CMS_KEY_PARAM
) {
384 if (flags
& CMS_NOATTR
) {
385 si
->pctx
= EVP_PKEY_CTX_new(si
->pkey
, NULL
);
388 if (EVP_PKEY_sign_init(si
->pctx
) <= 0)
390 if (EVP_PKEY_CTX_set_signature_md(si
->pctx
, md
) <= 0)
392 } else if (EVP_DigestSignInit(&si
->mctx
, &si
->pctx
, md
, NULL
, pk
) <=
397 if (!sd
->signerInfos
)
398 sd
->signerInfos
= sk_CMS_SignerInfo_new_null();
399 if (!sd
->signerInfos
|| !sk_CMS_SignerInfo_push(sd
->signerInfos
, si
))
405 CMSerr(CMS_F_CMS_ADD1_SIGNER
, ERR_R_MALLOC_FAILURE
);
408 M_ASN1_free_of(si
, CMS_SignerInfo
);
413 static int cms_add1_signingTime(CMS_SignerInfo
*si
, ASN1_TIME
*t
)
420 tt
= X509_gmtime_adj(NULL
, 0);
425 if (CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_signingTime
,
426 tt
->type
, tt
, -1) <= 0)
437 CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME
, ERR_R_MALLOC_FAILURE
);
443 EVP_PKEY_CTX
*CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo
*si
)
448 EVP_MD_CTX
*CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo
*si
)
453 STACK_OF(CMS_SignerInfo
) *CMS_get0_SignerInfos(CMS_ContentInfo
*cms
)
456 sd
= cms_get0_signed(cms
);
459 return sd
->signerInfos
;
462 STACK_OF(X509
) *CMS_get0_signers(CMS_ContentInfo
*cms
)
464 STACK_OF(X509
) *signers
= NULL
;
465 STACK_OF(CMS_SignerInfo
) *sinfos
;
468 sinfos
= CMS_get0_SignerInfos(cms
);
469 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
470 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
473 signers
= sk_X509_new_null();
477 if (!sk_X509_push(signers
, si
->signer
)) {
478 sk_X509_free(signers
);
486 void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo
*si
, X509
*signer
)
489 CRYPTO_add(&signer
->references
, 1, CRYPTO_LOCK_X509
);
491 EVP_PKEY_free(si
->pkey
);
492 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
)
581 EVP_PKEY_CTX
*pctx
= NULL
;
582 EVP_MD_CTX_init(&mctx
);
585 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN
, CMS_R_NO_PRIVATE_KEY
);
589 if (!cms_DigestAlgorithm_find_ctx(&mctx
, chain
, si
->digestAlgorithm
))
591 /* Set SignerInfo algortihm details if we used custom parametsr */
592 if (si
->pctx
&& !cms_sd_asn1_ctrl(si
, 0))
596 * If any signed attributes calculate and add messageDigest attribute
599 if (CMS_signed_get_attr_count(si
) >= 0) {
601 cms
->d
.signedData
->encapContentInfo
->eContentType
;
602 unsigned char md
[EVP_MAX_MD_SIZE
];
604 if (!EVP_DigestFinal_ex(&mctx
, md
, &mdlen
))
606 if (!CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_messageDigest
,
607 V_ASN1_OCTET_STRING
, md
, mdlen
))
609 /* Copy content type across */
610 if (CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_contentType
,
611 V_ASN1_OBJECT
, ctype
, -1) <= 0)
613 if (!CMS_SignerInfo_sign(si
))
615 } else if (si
->pctx
) {
618 unsigned char md
[EVP_MAX_MD_SIZE
];
621 if (!EVP_DigestFinal_ex(&mctx
, md
, &mdlen
))
623 siglen
= EVP_PKEY_size(si
->pkey
);
624 sig
= OPENSSL_malloc(siglen
);
626 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN
, ERR_R_MALLOC_FAILURE
);
629 if (EVP_PKEY_sign(pctx
, sig
, &siglen
, md
, mdlen
) <= 0) {
633 ASN1_STRING_set0(si
->signature
, sig
, siglen
);
637 sig
= OPENSSL_malloc(EVP_PKEY_size(si
->pkey
));
639 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN
, ERR_R_MALLOC_FAILURE
);
642 if (!EVP_SignFinal(&mctx
, sig
, &siglen
, si
->pkey
)) {
643 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN
, CMS_R_SIGNFINAL_ERROR
);
647 ASN1_STRING_set0(si
->signature
, sig
, siglen
);
653 EVP_MD_CTX_cleanup(&mctx
);
655 EVP_PKEY_CTX_free(pctx
);
660 int cms_SignedData_final(CMS_ContentInfo
*cms
, BIO
*chain
)
662 STACK_OF(CMS_SignerInfo
) *sinfos
;
665 sinfos
= CMS_get0_SignerInfos(cms
);
666 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
667 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
668 if (!cms_SignerInfo_content_sign(cms
, si
, chain
))
671 cms
->d
.signedData
->encapContentInfo
->partial
= 0;
675 int CMS_SignerInfo_sign(CMS_SignerInfo
*si
)
677 EVP_MD_CTX
*mctx
= &si
->mctx
;
679 unsigned char *abuf
= NULL
;
682 const EVP_MD
*md
= NULL
;
684 md
= EVP_get_digestbyobj(si
->digestAlgorithm
->algorithm
);
688 if (CMS_signed_get_attr_by_NID(si
, NID_pkcs9_signingTime
, -1) < 0) {
689 if (!cms_add1_signingTime(si
, NULL
))
696 EVP_MD_CTX_init(mctx
);
697 if (EVP_DigestSignInit(mctx
, &pctx
, md
, NULL
, si
->pkey
) <= 0)
701 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
702 EVP_PKEY_CTRL_CMS_SIGN
, 0, si
) <= 0) {
703 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN
, CMS_R_CTRL_ERROR
);
707 alen
= ASN1_item_i2d((ASN1_VALUE
*)si
->signedAttrs
, &abuf
,
708 ASN1_ITEM_rptr(CMS_Attributes_Sign
));
711 if (EVP_DigestSignUpdate(mctx
, abuf
, alen
) <= 0)
713 if (EVP_DigestSignFinal(mctx
, NULL
, &siglen
) <= 0)
716 abuf
= OPENSSL_malloc(siglen
);
719 if (EVP_DigestSignFinal(mctx
, abuf
, &siglen
) <= 0)
722 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
723 EVP_PKEY_CTRL_CMS_SIGN
, 1, si
) <= 0) {
724 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN
, CMS_R_CTRL_ERROR
);
728 EVP_MD_CTX_cleanup(mctx
);
730 ASN1_STRING_set0(si
->signature
, abuf
, siglen
);
737 EVP_MD_CTX_cleanup(mctx
);
742 int CMS_SignerInfo_verify(CMS_SignerInfo
*si
)
744 EVP_MD_CTX
*mctx
= &si
->mctx
;
745 unsigned char *abuf
= NULL
;
747 const EVP_MD
*md
= NULL
;
750 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY
, CMS_R_NO_PUBLIC_KEY
);
754 md
= EVP_get_digestbyobj(si
->digestAlgorithm
->algorithm
);
757 EVP_MD_CTX_init(mctx
);
758 if (EVP_DigestVerifyInit(mctx
, &si
->pctx
, md
, NULL
, si
->pkey
) <= 0)
761 if (!cms_sd_asn1_ctrl(si
, 1))
764 alen
= ASN1_item_i2d((ASN1_VALUE
*)si
->signedAttrs
, &abuf
,
765 ASN1_ITEM_rptr(CMS_Attributes_Verify
));
768 r
= EVP_DigestVerifyUpdate(mctx
, abuf
, alen
);
774 r
= EVP_DigestVerifyFinal(mctx
,
775 si
->signature
->data
, si
->signature
->length
);
777 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY
, CMS_R_VERIFICATION_FAILURE
);
779 EVP_MD_CTX_cleanup(mctx
);
783 /* Create a chain of digest BIOs from a CMS ContentInfo */
785 BIO
*cms_SignedData_init_bio(CMS_ContentInfo
*cms
)
790 sd
= cms_get0_signed(cms
);
793 if (cms
->d
.signedData
->encapContentInfo
->partial
)
794 cms_sd_set_version(sd
);
795 for (i
= 0; i
< sk_X509_ALGOR_num(sd
->digestAlgorithms
); i
++) {
796 X509_ALGOR
*digestAlgorithm
;
798 digestAlgorithm
= sk_X509_ALGOR_value(sd
->digestAlgorithms
, i
);
799 mdbio
= cms_DigestAlgorithm_init_bio(digestAlgorithm
);
803 BIO_push(chain
, mdbio
);
814 int CMS_SignerInfo_verify_content(CMS_SignerInfo
*si
, BIO
*chain
)
816 ASN1_OCTET_STRING
*os
= NULL
;
818 EVP_PKEY_CTX
*pkctx
= NULL
;
820 unsigned char mval
[EVP_MAX_MD_SIZE
];
822 EVP_MD_CTX_init(&mctx
);
823 /* If we have any signed attributes look for messageDigest value */
824 if (CMS_signed_get_attr_count(si
) >= 0) {
825 os
= CMS_signed_get0_data_by_OBJ(si
,
826 OBJ_nid2obj(NID_pkcs9_messageDigest
),
827 -3, V_ASN1_OCTET_STRING
);
829 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
830 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE
);
835 if (!cms_DigestAlgorithm_find_ctx(&mctx
, chain
, si
->digestAlgorithm
))
838 if (EVP_DigestFinal_ex(&mctx
, mval
, &mlen
) <= 0) {
839 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
840 CMS_R_UNABLE_TO_FINALIZE_CONTEXT
);
844 /* If messageDigest found compare it */
847 if (mlen
!= (unsigned int)os
->length
) {
848 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
849 CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH
);
853 if (memcmp(mval
, os
->data
, mlen
)) {
854 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
855 CMS_R_VERIFICATION_FAILURE
);
860 const EVP_MD
*md
= EVP_MD_CTX_md(&mctx
);
861 pkctx
= EVP_PKEY_CTX_new(si
->pkey
, NULL
);
862 if (EVP_PKEY_verify_init(pkctx
) <= 0)
864 if (EVP_PKEY_CTX_set_signature_md(pkctx
, md
) <= 0)
867 if (!cms_sd_asn1_ctrl(si
, 1))
869 r
= EVP_PKEY_verify(pkctx
, si
->signature
->data
,
870 si
->signature
->length
, mval
, mlen
);
872 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
873 CMS_R_VERIFICATION_FAILURE
);
880 EVP_PKEY_CTX_free(pkctx
);
881 EVP_MD_CTX_cleanup(&mctx
);
886 int CMS_add_smimecap(CMS_SignerInfo
*si
, STACK_OF(X509_ALGOR
) *algs
)
888 unsigned char *smder
= NULL
;
890 smderlen
= i2d_X509_ALGORS(algs
, &smder
);
893 r
= CMS_signed_add1_attr_by_NID(si
, NID_SMIMECapabilities
,
894 V_ASN1_SEQUENCE
, smder
, smderlen
);
899 int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR
) **algs
,
900 int algnid
, int keysize
)
903 ASN1_INTEGER
*key
= NULL
;
905 key
= ASN1_INTEGER_new();
906 if (!key
|| !ASN1_INTEGER_set(key
, keysize
))
909 alg
= X509_ALGOR_new();
912 ASN1_INTEGER_free(key
);
916 X509_ALGOR_set0(alg
, OBJ_nid2obj(algnid
),
917 key
? V_ASN1_INTEGER
: V_ASN1_UNDEF
, key
);
919 *algs
= sk_X509_ALGOR_new_null();
920 if (!*algs
|| !sk_X509_ALGOR_push(*algs
, alg
)) {
921 X509_ALGOR_free(alg
);
927 /* Check to see if a cipher exists and if so add S/MIME capabilities */
929 static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR
) **sk
, int nid
, int arg
)
931 if (EVP_get_cipherbynid(nid
))
932 return CMS_add_simple_smimecap(sk
, nid
, arg
);
936 static int cms_add_digest_smcap(STACK_OF(X509_ALGOR
) **sk
, int nid
, int arg
)
938 if (EVP_get_digestbynid(nid
))
939 return CMS_add_simple_smimecap(sk
, nid
, arg
);
943 int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR
) **smcap
)
945 if (!cms_add_cipher_smcap(smcap
, NID_aes_256_cbc
, -1)
946 || !cms_add_digest_smcap(smcap
, NID_id_GostR3411_94
, -1)
947 || !cms_add_cipher_smcap(smcap
, NID_id_Gost28147_89
, -1)
948 || !cms_add_cipher_smcap(smcap
, NID_aes_192_cbc
, -1)
949 || !cms_add_cipher_smcap(smcap
, NID_aes_128_cbc
, -1)
950 || !cms_add_cipher_smcap(smcap
, NID_des_ede3_cbc
, -1)
951 || !cms_add_cipher_smcap(smcap
, NID_rc2_cbc
, 128)
952 || !cms_add_cipher_smcap(smcap
, NID_rc2_cbc
, 64)
953 || !cms_add_cipher_smcap(smcap
, NID_des_cbc
, -1)
954 || !cms_add_cipher_smcap(smcap
, NID_rc2_cbc
, 40))