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 * ====================================================================
55 #include "internal/cryptlib.h"
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 "internal/asn1_int.h"
64 #include "internal/evp_int.h"
66 /* CMS SignedData Utilities */
68 static CMS_SignedData
*cms_get0_signed(CMS_ContentInfo
*cms
)
70 if (OBJ_obj2nid(cms
->contentType
) != NID_pkcs7_signed
) {
71 CMSerr(CMS_F_CMS_GET0_SIGNED
, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA
);
74 return cms
->d
.signedData
;
77 static CMS_SignedData
*cms_signed_data_init(CMS_ContentInfo
*cms
)
79 if (cms
->d
.other
== NULL
) {
80 cms
->d
.signedData
= M_ASN1_new_of(CMS_SignedData
);
81 if (!cms
->d
.signedData
) {
82 CMSerr(CMS_F_CMS_SIGNED_DATA_INIT
, ERR_R_MALLOC_FAILURE
);
85 cms
->d
.signedData
->version
= 1;
86 cms
->d
.signedData
->encapContentInfo
->eContentType
=
87 OBJ_nid2obj(NID_pkcs7_data
);
88 cms
->d
.signedData
->encapContentInfo
->partial
= 1;
89 ASN1_OBJECT_free(cms
->contentType
);
90 cms
->contentType
= OBJ_nid2obj(NID_pkcs7_signed
);
91 return cms
->d
.signedData
;
93 return cms_get0_signed(cms
);
96 /* Just initialize SignedData e.g. for certs only structure */
98 int CMS_SignedData_init(CMS_ContentInfo
*cms
)
100 if (cms_signed_data_init(cms
))
106 /* Check structures and fixup version numbers (if necessary) */
108 static void cms_sd_set_version(CMS_SignedData
*sd
)
111 CMS_CertificateChoices
*cch
;
112 CMS_RevocationInfoChoice
*rch
;
115 for (i
= 0; i
< sk_CMS_CertificateChoices_num(sd
->certificates
); i
++) {
116 cch
= sk_CMS_CertificateChoices_value(sd
->certificates
, i
);
117 if (cch
->type
== CMS_CERTCHOICE_OTHER
) {
120 } else if (cch
->type
== CMS_CERTCHOICE_V2ACERT
) {
123 } else if (cch
->type
== CMS_CERTCHOICE_V1ACERT
) {
129 for (i
= 0; i
< sk_CMS_RevocationInfoChoice_num(sd
->crls
); i
++) {
130 rch
= sk_CMS_RevocationInfoChoice_value(sd
->crls
, i
);
131 if (rch
->type
== CMS_REVCHOICE_OTHER
) {
137 if ((OBJ_obj2nid(sd
->encapContentInfo
->eContentType
) != NID_pkcs7_data
)
138 && (sd
->version
< 3))
141 for (i
= 0; i
< sk_CMS_SignerInfo_num(sd
->signerInfos
); i
++) {
142 si
= sk_CMS_SignerInfo_value(sd
->signerInfos
, i
);
143 if (si
->sid
->type
== CMS_SIGNERINFO_KEYIDENTIFIER
) {
148 } else if (si
->version
< 1)
157 /* Copy an existing messageDigest value */
159 static int cms_copy_messageDigest(CMS_ContentInfo
*cms
, CMS_SignerInfo
*si
)
161 STACK_OF(CMS_SignerInfo
) *sinfos
;
162 CMS_SignerInfo
*sitmp
;
164 sinfos
= CMS_get0_SignerInfos(cms
);
165 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
166 ASN1_OCTET_STRING
*messageDigest
;
167 sitmp
= sk_CMS_SignerInfo_value(sinfos
, i
);
170 if (CMS_signed_get_attr_count(sitmp
) < 0)
172 if (OBJ_cmp(si
->digestAlgorithm
->algorithm
,
173 sitmp
->digestAlgorithm
->algorithm
))
175 messageDigest
= CMS_signed_get0_data_by_OBJ(sitmp
,
177 (NID_pkcs9_messageDigest
),
178 -3, V_ASN1_OCTET_STRING
);
179 if (!messageDigest
) {
180 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST
,
181 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE
);
185 if (CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_messageDigest
,
192 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST
, CMS_R_NO_MATCHING_DIGEST
);
196 int cms_set1_SignerIdentifier(CMS_SignerIdentifier
*sid
, X509
*cert
, int type
)
199 case CMS_SIGNERINFO_ISSUER_SERIAL
:
200 if (!cms_set1_ias(&sid
->d
.issuerAndSerialNumber
, cert
))
204 case CMS_SIGNERINFO_KEYIDENTIFIER
:
205 if (!cms_set1_keyid(&sid
->d
.subjectKeyIdentifier
, cert
))
210 CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER
, CMS_R_UNKNOWN_ID
);
219 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier
*sid
,
220 ASN1_OCTET_STRING
**keyid
,
224 if (sid
->type
== CMS_SIGNERINFO_ISSUER_SERIAL
) {
226 *issuer
= sid
->d
.issuerAndSerialNumber
->issuer
;
228 *sno
= sid
->d
.issuerAndSerialNumber
->serialNumber
;
229 } else if (sid
->type
== CMS_SIGNERINFO_KEYIDENTIFIER
) {
231 *keyid
= sid
->d
.subjectKeyIdentifier
;
237 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier
*sid
, X509
*cert
)
239 if (sid
->type
== CMS_SIGNERINFO_ISSUER_SERIAL
)
240 return cms_ias_cert_cmp(sid
->d
.issuerAndSerialNumber
, cert
);
241 else if (sid
->type
== CMS_SIGNERINFO_KEYIDENTIFIER
)
242 return cms_keyid_cert_cmp(sid
->d
.subjectKeyIdentifier
, cert
);
247 static int cms_sd_asn1_ctrl(CMS_SignerInfo
*si
, int cmd
)
249 EVP_PKEY
*pkey
= si
->pkey
;
251 if (!pkey
->ameth
|| !pkey
->ameth
->pkey_ctrl
)
253 i
= pkey
->ameth
->pkey_ctrl(pkey
, ASN1_PKEY_CTRL_CMS_SIGN
, cmd
, si
);
255 CMSerr(CMS_F_CMS_SD_ASN1_CTRL
, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE
);
259 CMSerr(CMS_F_CMS_SD_ASN1_CTRL
, CMS_R_CTRL_FAILURE
);
265 CMS_SignerInfo
*CMS_add1_signer(CMS_ContentInfo
*cms
,
266 X509
*signer
, EVP_PKEY
*pk
, const EVP_MD
*md
,
270 CMS_SignerInfo
*si
= NULL
;
273 if (!X509_check_private_key(signer
, pk
)) {
274 CMSerr(CMS_F_CMS_ADD1_SIGNER
,
275 CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE
);
278 sd
= cms_signed_data_init(cms
);
281 si
= M_ASN1_new_of(CMS_SignerInfo
);
284 X509_check_purpose(signer
, -1, -1);
286 CRYPTO_add(&pk
->references
, 1, CRYPTO_LOCK_EVP_PKEY
);
291 si
->mctx
= EVP_MD_CTX_new();
294 if (si
->mctx
== NULL
) {
295 CMSerr(CMS_F_CMS_ADD1_SIGNER
, ERR_R_MALLOC_FAILURE
);
299 if (flags
& CMS_USE_KEYID
) {
303 type
= CMS_SIGNERINFO_KEYIDENTIFIER
;
305 type
= CMS_SIGNERINFO_ISSUER_SERIAL
;
309 if (!cms_set1_SignerIdentifier(si
->sid
, signer
, type
))
314 if (EVP_PKEY_get_default_digest_nid(pk
, &def_nid
) <= 0)
316 md
= EVP_get_digestbynid(def_nid
);
318 CMSerr(CMS_F_CMS_ADD1_SIGNER
, CMS_R_NO_DEFAULT_DIGEST
);
324 CMSerr(CMS_F_CMS_ADD1_SIGNER
, CMS_R_NO_DIGEST_SET
);
328 X509_ALGOR_set_md(si
->digestAlgorithm
, md
);
330 /* See if digest is present in digestAlgorithms */
331 for (i
= 0; i
< sk_X509_ALGOR_num(sd
->digestAlgorithms
); i
++) {
333 alg
= sk_X509_ALGOR_value(sd
->digestAlgorithms
, i
);
334 X509_ALGOR_get0(&aoid
, NULL
, NULL
, alg
);
335 if (OBJ_obj2nid(aoid
) == EVP_MD_type(md
))
339 if (i
== sk_X509_ALGOR_num(sd
->digestAlgorithms
)) {
340 alg
= X509_ALGOR_new();
343 X509_ALGOR_set_md(alg
, md
);
344 if (!sk_X509_ALGOR_push(sd
->digestAlgorithms
, alg
)) {
345 X509_ALGOR_free(alg
);
350 if (!(flags
& CMS_KEY_PARAM
) && !cms_sd_asn1_ctrl(si
, 0))
352 if (!(flags
& CMS_NOATTR
)) {
354 * Initialialize signed attributes strutucture so other attributes
355 * such as signing time etc are added later even if we add none here.
357 if (!si
->signedAttrs
) {
358 si
->signedAttrs
= sk_X509_ATTRIBUTE_new_null();
359 if (!si
->signedAttrs
)
363 if (!(flags
& CMS_NOSMIMECAP
)) {
364 STACK_OF(X509_ALGOR
) *smcap
= NULL
;
365 i
= CMS_add_standard_smimecap(&smcap
);
367 i
= CMS_add_smimecap(si
, smcap
);
368 sk_X509_ALGOR_pop_free(smcap
, X509_ALGOR_free
);
372 if (flags
& CMS_REUSE_DIGEST
) {
373 if (!cms_copy_messageDigest(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
) <=
401 if (!sd
->signerInfos
)
402 sd
->signerInfos
= sk_CMS_SignerInfo_new_null();
403 if (!sd
->signerInfos
|| !sk_CMS_SignerInfo_push(sd
->signerInfos
, si
))
409 CMSerr(CMS_F_CMS_ADD1_SIGNER
, ERR_R_MALLOC_FAILURE
);
411 M_ASN1_free_of(si
, CMS_SignerInfo
);
416 static int cms_add1_signingTime(CMS_SignerInfo
*si
, ASN1_TIME
*t
)
423 tt
= X509_gmtime_adj(NULL
, 0);
428 if (CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_signingTime
,
429 tt
->type
, tt
, -1) <= 0)
440 CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME
, ERR_R_MALLOC_FAILURE
);
446 EVP_PKEY_CTX
*CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo
*si
)
451 EVP_MD_CTX
*CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo
*si
)
456 STACK_OF(CMS_SignerInfo
) *CMS_get0_SignerInfos(CMS_ContentInfo
*cms
)
459 sd
= cms_get0_signed(cms
);
462 return sd
->signerInfos
;
465 STACK_OF(X509
) *CMS_get0_signers(CMS_ContentInfo
*cms
)
467 STACK_OF(X509
) *signers
= NULL
;
468 STACK_OF(CMS_SignerInfo
) *sinfos
;
471 sinfos
= CMS_get0_SignerInfos(cms
);
472 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
473 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
476 signers
= sk_X509_new_null();
480 if (!sk_X509_push(signers
, si
->signer
)) {
481 sk_X509_free(signers
);
489 void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo
*si
, X509
*signer
)
493 EVP_PKEY_free(si
->pkey
);
494 si
->pkey
= X509_get_pubkey(signer
);
496 X509_free(si
->signer
);
500 int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo
*si
,
501 ASN1_OCTET_STRING
**keyid
,
502 X509_NAME
**issuer
, ASN1_INTEGER
**sno
)
504 return cms_SignerIdentifier_get0_signer_id(si
->sid
, keyid
, issuer
, sno
);
507 int CMS_SignerInfo_cert_cmp(CMS_SignerInfo
*si
, X509
*cert
)
509 return cms_SignerIdentifier_cert_cmp(si
->sid
, cert
);
512 int CMS_set1_signers_certs(CMS_ContentInfo
*cms
, STACK_OF(X509
) *scerts
,
517 CMS_CertificateChoices
*cch
;
518 STACK_OF(CMS_CertificateChoices
) *certs
;
522 sd
= cms_get0_signed(cms
);
525 certs
= sd
->certificates
;
526 for (i
= 0; i
< sk_CMS_SignerInfo_num(sd
->signerInfos
); i
++) {
527 si
= sk_CMS_SignerInfo_value(sd
->signerInfos
, i
);
531 for (j
= 0; j
< sk_X509_num(scerts
); j
++) {
532 x
= sk_X509_value(scerts
, j
);
533 if (CMS_SignerInfo_cert_cmp(si
, x
) == 0) {
534 CMS_SignerInfo_set1_signer_cert(si
, x
);
540 if (si
->signer
|| (flags
& CMS_NOINTERN
))
543 for (j
= 0; j
< sk_CMS_CertificateChoices_num(certs
); j
++) {
544 cch
= sk_CMS_CertificateChoices_value(certs
, j
);
547 x
= cch
->d
.certificate
;
548 if (CMS_SignerInfo_cert_cmp(si
, x
) == 0) {
549 CMS_SignerInfo_set1_signer_cert(si
, x
);
558 void CMS_SignerInfo_get0_algs(CMS_SignerInfo
*si
, EVP_PKEY
**pk
,
559 X509
**signer
, X509_ALGOR
**pdig
,
565 *signer
= si
->signer
;
567 *pdig
= si
->digestAlgorithm
;
569 *psig
= si
->signatureAlgorithm
;
572 ASN1_OCTET_STRING
*CMS_SignerInfo_get0_signature(CMS_SignerInfo
*si
)
574 return si
->signature
;
577 static int cms_SignerInfo_content_sign(CMS_ContentInfo
*cms
,
578 CMS_SignerInfo
*si
, BIO
*chain
)
580 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
582 EVP_PKEY_CTX
*pctx
= NULL
;
585 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN
, ERR_R_MALLOC_FAILURE
);
590 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN
, CMS_R_NO_PRIVATE_KEY
);
594 if (!cms_DigestAlgorithm_find_ctx(mctx
, chain
, si
->digestAlgorithm
))
596 /* Set SignerInfo algortihm details if we used custom parametsr */
597 if (si
->pctx
&& !cms_sd_asn1_ctrl(si
, 0))
601 * If any signed attributes calculate and add messageDigest attribute
604 if (CMS_signed_get_attr_count(si
) >= 0) {
606 cms
->d
.signedData
->encapContentInfo
->eContentType
;
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_signed_add1_attr_by_NID(si
, NID_pkcs9_contentType
,
616 V_ASN1_OBJECT
, ctype
, -1) <= 0)
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
;
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
))
700 EVP_MD_CTX_reset(mctx
);
701 if (EVP_DigestSignInit(mctx
, &pctx
, md
, NULL
, si
->pkey
) <= 0)
705 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
706 EVP_PKEY_CTRL_CMS_SIGN
, 0, si
) <= 0) {
707 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN
, CMS_R_CTRL_ERROR
);
711 alen
= ASN1_item_i2d((ASN1_VALUE
*)si
->signedAttrs
, &abuf
,
712 ASN1_ITEM_rptr(CMS_Attributes_Sign
));
715 if (EVP_DigestSignUpdate(mctx
, abuf
, alen
) <= 0)
717 if (EVP_DigestSignFinal(mctx
, NULL
, &siglen
) <= 0)
720 abuf
= OPENSSL_malloc(siglen
);
723 if (EVP_DigestSignFinal(mctx
, abuf
, &siglen
) <= 0)
726 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
727 EVP_PKEY_CTRL_CMS_SIGN
, 1, si
) <= 0) {
728 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN
, CMS_R_CTRL_ERROR
);
732 EVP_MD_CTX_reset(mctx
);
734 ASN1_STRING_set0(si
->signature
, abuf
, siglen
);
740 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 md
= EVP_get_digestbyobj(si
->digestAlgorithm
->algorithm
);
760 if (si
->mctx
== NULL
)
761 si
->mctx
= EVP_MD_CTX_new();
763 if (EVP_DigestVerifyInit(mctx
, &si
->pctx
, md
, NULL
, si
->pkey
) <= 0)
766 if (!cms_sd_asn1_ctrl(si
, 1))
769 alen
= ASN1_item_i2d((ASN1_VALUE
*)si
->signedAttrs
, &abuf
,
770 ASN1_ITEM_rptr(CMS_Attributes_Verify
));
773 r
= EVP_DigestVerifyUpdate(mctx
, abuf
, alen
);
779 r
= EVP_DigestVerifyFinal(mctx
,
780 si
->signature
->data
, si
->signature
->length
);
782 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY
, CMS_R_VERIFICATION_FAILURE
);
784 EVP_MD_CTX_reset(mctx
);
788 /* Create a chain of digest BIOs from a CMS ContentInfo */
790 BIO
*cms_SignedData_init_bio(CMS_ContentInfo
*cms
)
795 sd
= cms_get0_signed(cms
);
798 if (cms
->d
.signedData
->encapContentInfo
->partial
)
799 cms_sd_set_version(sd
);
800 for (i
= 0; i
< sk_X509_ALGOR_num(sd
->digestAlgorithms
); i
++) {
801 X509_ALGOR
*digestAlgorithm
;
803 digestAlgorithm
= sk_X509_ALGOR_value(sd
->digestAlgorithms
, i
);
804 mdbio
= cms_DigestAlgorithm_init_bio(digestAlgorithm
);
808 BIO_push(chain
, mdbio
);
818 int CMS_SignerInfo_verify_content(CMS_SignerInfo
*si
, BIO
*chain
)
820 ASN1_OCTET_STRING
*os
= NULL
;
821 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
822 EVP_PKEY_CTX
*pkctx
= NULL
;
824 unsigned char mval
[EVP_MAX_MD_SIZE
];
828 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
, ERR_R_MALLOC_FAILURE
);
831 /* If we have any signed attributes look for messageDigest value */
832 if (CMS_signed_get_attr_count(si
) >= 0) {
833 os
= CMS_signed_get0_data_by_OBJ(si
,
834 OBJ_nid2obj(NID_pkcs9_messageDigest
),
835 -3, V_ASN1_OCTET_STRING
);
837 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
838 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE
);
843 if (!cms_DigestAlgorithm_find_ctx(mctx
, chain
, si
->digestAlgorithm
))
846 if (EVP_DigestFinal_ex(mctx
, mval
, &mlen
) <= 0) {
847 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
848 CMS_R_UNABLE_TO_FINALIZE_CONTEXT
);
852 /* If messageDigest found compare it */
855 if (mlen
!= (unsigned int)os
->length
) {
856 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
857 CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH
);
861 if (memcmp(mval
, os
->data
, mlen
)) {
862 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
863 CMS_R_VERIFICATION_FAILURE
);
868 const EVP_MD
*md
= EVP_MD_CTX_md(mctx
);
869 pkctx
= EVP_PKEY_CTX_new(si
->pkey
, NULL
);
872 if (EVP_PKEY_verify_init(pkctx
) <= 0)
874 if (EVP_PKEY_CTX_set_signature_md(pkctx
, md
) <= 0)
877 if (!cms_sd_asn1_ctrl(si
, 1))
879 r
= EVP_PKEY_verify(pkctx
, si
->signature
->data
,
880 si
->signature
->length
, mval
, mlen
);
882 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
883 CMS_R_VERIFICATION_FAILURE
);
889 EVP_PKEY_CTX_free(pkctx
);
890 EVP_MD_CTX_free(mctx
);
895 int CMS_add_smimecap(CMS_SignerInfo
*si
, STACK_OF(X509_ALGOR
) *algs
)
897 unsigned char *smder
= NULL
;
899 smderlen
= i2d_X509_ALGORS(algs
, &smder
);
902 r
= CMS_signed_add1_attr_by_NID(si
, NID_SMIMECapabilities
,
903 V_ASN1_SEQUENCE
, smder
, smderlen
);
908 int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR
) **algs
,
909 int algnid
, int keysize
)
912 ASN1_INTEGER
*key
= NULL
;
914 key
= ASN1_INTEGER_new();
915 if (key
== NULL
|| !ASN1_INTEGER_set(key
, keysize
))
918 alg
= X509_ALGOR_new();
920 ASN1_INTEGER_free(key
);
924 X509_ALGOR_set0(alg
, OBJ_nid2obj(algnid
),
925 key
? V_ASN1_INTEGER
: V_ASN1_UNDEF
, key
);
927 *algs
= sk_X509_ALGOR_new_null();
928 if (*algs
== NULL
|| !sk_X509_ALGOR_push(*algs
, alg
)) {
929 X509_ALGOR_free(alg
);
935 /* Check to see if a cipher exists and if so add S/MIME capabilities */
937 static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR
) **sk
, int nid
, int arg
)
939 if (EVP_get_cipherbynid(nid
))
940 return CMS_add_simple_smimecap(sk
, nid
, arg
);
944 static int cms_add_digest_smcap(STACK_OF(X509_ALGOR
) **sk
, int nid
, int arg
)
946 if (EVP_get_digestbynid(nid
))
947 return CMS_add_simple_smimecap(sk
, nid
, arg
);
951 int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR
) **smcap
)
953 if (!cms_add_cipher_smcap(smcap
, NID_aes_256_cbc
, -1)
954 || !cms_add_digest_smcap(smcap
, NID_id_GostR3411_2012_256
, -1)
955 || !cms_add_digest_smcap(smcap
, NID_id_GostR3411_2012_512
, -1)
956 || !cms_add_digest_smcap(smcap
, NID_id_GostR3411_94
, -1)
957 || !cms_add_cipher_smcap(smcap
, NID_id_Gost28147_89
, -1)
958 || !cms_add_cipher_smcap(smcap
, NID_aes_192_cbc
, -1)
959 || !cms_add_cipher_smcap(smcap
, NID_aes_128_cbc
, -1)
960 || !cms_add_cipher_smcap(smcap
, NID_des_ede3_cbc
, -1)
961 || !cms_add_cipher_smcap(smcap
, NID_rc2_cbc
, 128)
962 || !cms_add_cipher_smcap(smcap
, NID_rc2_cbc
, 64)
963 || !cms_add_cipher_smcap(smcap
, NID_des_cbc
, -1)
964 || !cms_add_cipher_smcap(smcap
, NID_rc2_cbc
, 40))