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"
65 /* CMS SignedData Utilities */
67 static CMS_SignedData
*cms_get0_signed(CMS_ContentInfo
*cms
)
69 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
) {
79 cms
->d
.signedData
= M_ASN1_new_of(CMS_SignedData
);
80 if (!cms
->d
.signedData
) {
81 CMSerr(CMS_F_CMS_SIGNED_DATA_INIT
, ERR_R_MALLOC_FAILURE
);
84 cms
->d
.signedData
->version
= 1;
85 cms
->d
.signedData
->encapContentInfo
->eContentType
=
86 OBJ_nid2obj(NID_pkcs7_data
);
87 cms
->d
.signedData
->encapContentInfo
->partial
= 1;
88 ASN1_OBJECT_free(cms
->contentType
);
89 cms
->contentType
= OBJ_nid2obj(NID_pkcs7_signed
);
90 return cms
->d
.signedData
;
92 return cms_get0_signed(cms
);
95 /* Just initialize SignedData e.g. for certs only structure */
97 int CMS_SignedData_init(CMS_ContentInfo
*cms
)
99 if (cms_signed_data_init(cms
))
105 /* Check structures and fixup version numbers (if necessary) */
107 static void cms_sd_set_version(CMS_SignedData
*sd
)
110 CMS_CertificateChoices
*cch
;
111 CMS_RevocationInfoChoice
*rch
;
114 for (i
= 0; i
< sk_CMS_CertificateChoices_num(sd
->certificates
); i
++) {
115 cch
= sk_CMS_CertificateChoices_value(sd
->certificates
, i
);
116 if (cch
->type
== CMS_CERTCHOICE_OTHER
) {
119 } else if (cch
->type
== CMS_CERTCHOICE_V2ACERT
) {
122 } else if (cch
->type
== CMS_CERTCHOICE_V1ACERT
) {
128 for (i
= 0; i
< sk_CMS_RevocationInfoChoice_num(sd
->crls
); i
++) {
129 rch
= sk_CMS_RevocationInfoChoice_value(sd
->crls
, i
);
130 if (rch
->type
== CMS_REVCHOICE_OTHER
) {
136 if ((OBJ_obj2nid(sd
->encapContentInfo
->eContentType
) != NID_pkcs7_data
)
137 && (sd
->version
< 3))
140 for (i
= 0; i
< sk_CMS_SignerInfo_num(sd
->signerInfos
); i
++) {
141 si
= sk_CMS_SignerInfo_value(sd
->signerInfos
, i
);
142 if (si
->sid
->type
== CMS_SIGNERINFO_KEYIDENTIFIER
) {
147 } else if (si
->version
< 1)
156 /* Copy an existing messageDigest value */
158 static int cms_copy_messageDigest(CMS_ContentInfo
*cms
, CMS_SignerInfo
*si
)
160 STACK_OF(CMS_SignerInfo
) *sinfos
;
161 CMS_SignerInfo
*sitmp
;
163 sinfos
= CMS_get0_SignerInfos(cms
);
164 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
165 ASN1_OCTET_STRING
*messageDigest
;
166 sitmp
= sk_CMS_SignerInfo_value(sinfos
, i
);
169 if (CMS_signed_get_attr_count(sitmp
) < 0)
171 if (OBJ_cmp(si
->digestAlgorithm
->algorithm
,
172 sitmp
->digestAlgorithm
->algorithm
))
174 messageDigest
= CMS_signed_get0_data_by_OBJ(sitmp
,
176 (NID_pkcs9_messageDigest
),
177 -3, V_ASN1_OCTET_STRING
);
178 if (!messageDigest
) {
179 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST
,
180 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE
);
184 if (CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_messageDigest
,
191 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST
, CMS_R_NO_MATCHING_DIGEST
);
195 int cms_set1_SignerIdentifier(CMS_SignerIdentifier
*sid
, X509
*cert
, int type
)
198 case CMS_SIGNERINFO_ISSUER_SERIAL
:
199 if (!cms_set1_ias(&sid
->d
.issuerAndSerialNumber
, cert
))
203 case CMS_SIGNERINFO_KEYIDENTIFIER
:
204 if (!cms_set1_keyid(&sid
->d
.subjectKeyIdentifier
, cert
))
209 CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER
, CMS_R_UNKNOWN_ID
);
218 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier
*sid
,
219 ASN1_OCTET_STRING
**keyid
,
223 if (sid
->type
== CMS_SIGNERINFO_ISSUER_SERIAL
) {
225 *issuer
= sid
->d
.issuerAndSerialNumber
->issuer
;
227 *sno
= sid
->d
.issuerAndSerialNumber
->serialNumber
;
228 } else if (sid
->type
== CMS_SIGNERINFO_KEYIDENTIFIER
) {
230 *keyid
= sid
->d
.subjectKeyIdentifier
;
236 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier
*sid
, X509
*cert
)
238 if (sid
->type
== CMS_SIGNERINFO_ISSUER_SERIAL
)
239 return cms_ias_cert_cmp(sid
->d
.issuerAndSerialNumber
, cert
);
240 else if (sid
->type
== CMS_SIGNERINFO_KEYIDENTIFIER
)
241 return cms_keyid_cert_cmp(sid
->d
.subjectKeyIdentifier
, cert
);
246 static int cms_sd_asn1_ctrl(CMS_SignerInfo
*si
, int cmd
)
248 EVP_PKEY
*pkey
= si
->pkey
;
250 if (!pkey
->ameth
|| !pkey
->ameth
->pkey_ctrl
)
252 i
= pkey
->ameth
->pkey_ctrl(pkey
, ASN1_PKEY_CTRL_CMS_SIGN
, cmd
, si
);
254 CMSerr(CMS_F_CMS_SD_ASN1_CTRL
, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE
);
258 CMSerr(CMS_F_CMS_SD_ASN1_CTRL
, CMS_R_CTRL_FAILURE
);
264 CMS_SignerInfo
*CMS_add1_signer(CMS_ContentInfo
*cms
,
265 X509
*signer
, EVP_PKEY
*pk
, const EVP_MD
*md
,
269 CMS_SignerInfo
*si
= NULL
;
272 if (!X509_check_private_key(signer
, pk
)) {
273 CMSerr(CMS_F_CMS_ADD1_SIGNER
,
274 CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE
);
277 sd
= cms_signed_data_init(cms
);
280 si
= M_ASN1_new_of(CMS_SignerInfo
);
283 X509_check_purpose(signer
, -1, -1);
285 CRYPTO_add(&pk
->references
, 1, CRYPTO_LOCK_EVP_PKEY
);
290 EVP_MD_CTX_init(&si
->mctx
);
293 if (flags
& CMS_USE_KEYID
) {
297 type
= CMS_SIGNERINFO_KEYIDENTIFIER
;
299 type
= CMS_SIGNERINFO_ISSUER_SERIAL
;
303 if (!cms_set1_SignerIdentifier(si
->sid
, signer
, type
))
308 if (EVP_PKEY_get_default_digest_nid(pk
, &def_nid
) <= 0)
310 md
= EVP_get_digestbynid(def_nid
);
312 CMSerr(CMS_F_CMS_ADD1_SIGNER
, CMS_R_NO_DEFAULT_DIGEST
);
318 CMSerr(CMS_F_CMS_ADD1_SIGNER
, CMS_R_NO_DIGEST_SET
);
322 X509_ALGOR_set_md(si
->digestAlgorithm
, md
);
324 /* See if digest is present in digestAlgorithms */
325 for (i
= 0; i
< sk_X509_ALGOR_num(sd
->digestAlgorithms
); i
++) {
327 alg
= sk_X509_ALGOR_value(sd
->digestAlgorithms
, i
);
328 X509_ALGOR_get0(&aoid
, NULL
, NULL
, alg
);
329 if (OBJ_obj2nid(aoid
) == EVP_MD_type(md
))
333 if (i
== sk_X509_ALGOR_num(sd
->digestAlgorithms
)) {
334 alg
= X509_ALGOR_new();
337 X509_ALGOR_set_md(alg
, md
);
338 if (!sk_X509_ALGOR_push(sd
->digestAlgorithms
, alg
)) {
339 X509_ALGOR_free(alg
);
344 if (!(flags
& CMS_KEY_PARAM
) && !cms_sd_asn1_ctrl(si
, 0))
346 if (!(flags
& CMS_NOATTR
)) {
348 * Initialialize signed attributes strutucture so other attributes
349 * such as signing time etc are added later even if we add none here.
351 if (!si
->signedAttrs
) {
352 si
->signedAttrs
= sk_X509_ATTRIBUTE_new_null();
353 if (!si
->signedAttrs
)
357 if (!(flags
& CMS_NOSMIMECAP
)) {
358 STACK_OF(X509_ALGOR
) *smcap
= NULL
;
359 i
= CMS_add_standard_smimecap(&smcap
);
361 i
= CMS_add_smimecap(si
, smcap
);
362 sk_X509_ALGOR_pop_free(smcap
, X509_ALGOR_free
);
366 if (flags
& CMS_REUSE_DIGEST
) {
367 if (!cms_copy_messageDigest(cms
, si
))
369 if (!(flags
& (CMS_PARTIAL
| CMS_KEY_PARAM
)) &&
370 !CMS_SignerInfo_sign(si
))
375 if (!(flags
& CMS_NOCERTS
)) {
376 /* NB ignore -1 return for duplicate cert */
377 if (!CMS_add1_cert(cms
, signer
))
381 if (flags
& CMS_KEY_PARAM
) {
382 if (flags
& CMS_NOATTR
) {
383 si
->pctx
= EVP_PKEY_CTX_new(si
->pkey
, NULL
);
384 if (si
->pctx
== NULL
)
386 if (EVP_PKEY_sign_init(si
->pctx
) <= 0)
388 if (EVP_PKEY_CTX_set_signature_md(si
->pctx
, md
) <= 0)
390 } else if (EVP_DigestSignInit(&si
->mctx
, &si
->pctx
, md
, NULL
, pk
) <=
395 if (!sd
->signerInfos
)
396 sd
->signerInfos
= sk_CMS_SignerInfo_new_null();
397 if (!sd
->signerInfos
|| !sk_CMS_SignerInfo_push(sd
->signerInfos
, si
))
403 CMSerr(CMS_F_CMS_ADD1_SIGNER
, ERR_R_MALLOC_FAILURE
);
405 M_ASN1_free_of(si
, CMS_SignerInfo
);
410 static int cms_add1_signingTime(CMS_SignerInfo
*si
, ASN1_TIME
*t
)
417 tt
= X509_gmtime_adj(NULL
, 0);
422 if (CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_signingTime
,
423 tt
->type
, tt
, -1) <= 0)
434 CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME
, ERR_R_MALLOC_FAILURE
);
440 EVP_PKEY_CTX
*CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo
*si
)
445 EVP_MD_CTX
*CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo
*si
)
450 STACK_OF(CMS_SignerInfo
) *CMS_get0_SignerInfos(CMS_ContentInfo
*cms
)
453 sd
= cms_get0_signed(cms
);
456 return sd
->signerInfos
;
459 STACK_OF(X509
) *CMS_get0_signers(CMS_ContentInfo
*cms
)
461 STACK_OF(X509
) *signers
= NULL
;
462 STACK_OF(CMS_SignerInfo
) *sinfos
;
465 sinfos
= CMS_get0_SignerInfos(cms
);
466 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
467 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
470 signers
= sk_X509_new_null();
474 if (!sk_X509_push(signers
, si
->signer
)) {
475 sk_X509_free(signers
);
483 void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo
*si
, X509
*signer
)
487 EVP_PKEY_free(si
->pkey
);
488 si
->pkey
= X509_get_pubkey(signer
);
490 X509_free(si
->signer
);
494 int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo
*si
,
495 ASN1_OCTET_STRING
**keyid
,
496 X509_NAME
**issuer
, ASN1_INTEGER
**sno
)
498 return cms_SignerIdentifier_get0_signer_id(si
->sid
, keyid
, issuer
, sno
);
501 int CMS_SignerInfo_cert_cmp(CMS_SignerInfo
*si
, X509
*cert
)
503 return cms_SignerIdentifier_cert_cmp(si
->sid
, cert
);
506 int CMS_set1_signers_certs(CMS_ContentInfo
*cms
, STACK_OF(X509
) *scerts
,
511 CMS_CertificateChoices
*cch
;
512 STACK_OF(CMS_CertificateChoices
) *certs
;
516 sd
= cms_get0_signed(cms
);
519 certs
= sd
->certificates
;
520 for (i
= 0; i
< sk_CMS_SignerInfo_num(sd
->signerInfos
); i
++) {
521 si
= sk_CMS_SignerInfo_value(sd
->signerInfos
, i
);
525 for (j
= 0; j
< sk_X509_num(scerts
); j
++) {
526 x
= sk_X509_value(scerts
, j
);
527 if (CMS_SignerInfo_cert_cmp(si
, x
) == 0) {
528 CMS_SignerInfo_set1_signer_cert(si
, x
);
534 if (si
->signer
|| (flags
& CMS_NOINTERN
))
537 for (j
= 0; j
< sk_CMS_CertificateChoices_num(certs
); j
++) {
538 cch
= sk_CMS_CertificateChoices_value(certs
, j
);
541 x
= cch
->d
.certificate
;
542 if (CMS_SignerInfo_cert_cmp(si
, x
) == 0) {
543 CMS_SignerInfo_set1_signer_cert(si
, x
);
552 void CMS_SignerInfo_get0_algs(CMS_SignerInfo
*si
, EVP_PKEY
**pk
,
553 X509
**signer
, X509_ALGOR
**pdig
,
559 *signer
= si
->signer
;
561 *pdig
= si
->digestAlgorithm
;
563 *psig
= si
->signatureAlgorithm
;
566 ASN1_OCTET_STRING
*CMS_SignerInfo_get0_signature(CMS_SignerInfo
*si
)
568 return si
->signature
;
571 static int cms_SignerInfo_content_sign(CMS_ContentInfo
*cms
,
572 CMS_SignerInfo
*si
, BIO
*chain
)
576 EVP_PKEY_CTX
*pctx
= NULL
;
577 EVP_MD_CTX_init(&mctx
);
580 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN
, CMS_R_NO_PRIVATE_KEY
);
584 if (!cms_DigestAlgorithm_find_ctx(&mctx
, chain
, si
->digestAlgorithm
))
586 /* Set SignerInfo algortihm details if we used custom parametsr */
587 if (si
->pctx
&& !cms_sd_asn1_ctrl(si
, 0))
591 * If any signed attributes calculate and add messageDigest attribute
594 if (CMS_signed_get_attr_count(si
) >= 0) {
596 cms
->d
.signedData
->encapContentInfo
->eContentType
;
597 unsigned char md
[EVP_MAX_MD_SIZE
];
599 if (!EVP_DigestFinal_ex(&mctx
, md
, &mdlen
))
601 if (!CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_messageDigest
,
602 V_ASN1_OCTET_STRING
, md
, mdlen
))
604 /* Copy content type across */
605 if (CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_contentType
,
606 V_ASN1_OBJECT
, ctype
, -1) <= 0)
608 if (!CMS_SignerInfo_sign(si
))
610 } else if (si
->pctx
) {
613 unsigned char md
[EVP_MAX_MD_SIZE
];
616 if (!EVP_DigestFinal_ex(&mctx
, md
, &mdlen
))
618 siglen
= EVP_PKEY_size(si
->pkey
);
619 sig
= OPENSSL_malloc(siglen
);
621 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN
, ERR_R_MALLOC_FAILURE
);
624 if (EVP_PKEY_sign(pctx
, sig
, &siglen
, md
, mdlen
) <= 0) {
628 ASN1_STRING_set0(si
->signature
, sig
, siglen
);
632 sig
= OPENSSL_malloc(EVP_PKEY_size(si
->pkey
));
634 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN
, ERR_R_MALLOC_FAILURE
);
637 if (!EVP_SignFinal(&mctx
, sig
, &siglen
, si
->pkey
)) {
638 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN
, CMS_R_SIGNFINAL_ERROR
);
642 ASN1_STRING_set0(si
->signature
, sig
, siglen
);
648 EVP_MD_CTX_cleanup(&mctx
);
649 EVP_PKEY_CTX_free(pctx
);
654 int cms_SignedData_final(CMS_ContentInfo
*cms
, BIO
*chain
)
656 STACK_OF(CMS_SignerInfo
) *sinfos
;
659 sinfos
= CMS_get0_SignerInfos(cms
);
660 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
661 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
662 if (!cms_SignerInfo_content_sign(cms
, si
, chain
))
665 cms
->d
.signedData
->encapContentInfo
->partial
= 0;
669 int CMS_SignerInfo_sign(CMS_SignerInfo
*si
)
671 EVP_MD_CTX
*mctx
= &si
->mctx
;
673 unsigned char *abuf
= NULL
;
676 const EVP_MD
*md
= NULL
;
678 md
= EVP_get_digestbyobj(si
->digestAlgorithm
->algorithm
);
682 if (CMS_signed_get_attr_by_NID(si
, NID_pkcs9_signingTime
, -1) < 0) {
683 if (!cms_add1_signingTime(si
, NULL
))
690 EVP_MD_CTX_init(mctx
);
691 if (EVP_DigestSignInit(mctx
, &pctx
, md
, NULL
, si
->pkey
) <= 0)
695 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
696 EVP_PKEY_CTRL_CMS_SIGN
, 0, si
) <= 0) {
697 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN
, CMS_R_CTRL_ERROR
);
701 alen
= ASN1_item_i2d((ASN1_VALUE
*)si
->signedAttrs
, &abuf
,
702 ASN1_ITEM_rptr(CMS_Attributes_Sign
));
705 if (EVP_DigestSignUpdate(mctx
, abuf
, alen
) <= 0)
707 if (EVP_DigestSignFinal(mctx
, NULL
, &siglen
) <= 0)
710 abuf
= OPENSSL_malloc(siglen
);
713 if (EVP_DigestSignFinal(mctx
, abuf
, &siglen
) <= 0)
716 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
717 EVP_PKEY_CTRL_CMS_SIGN
, 1, si
) <= 0) {
718 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN
, CMS_R_CTRL_ERROR
);
722 EVP_MD_CTX_cleanup(mctx
);
724 ASN1_STRING_set0(si
->signature
, abuf
, siglen
);
730 EVP_MD_CTX_cleanup(mctx
);
735 int CMS_SignerInfo_verify(CMS_SignerInfo
*si
)
737 EVP_MD_CTX
*mctx
= &si
->mctx
;
738 unsigned char *abuf
= NULL
;
740 const EVP_MD
*md
= NULL
;
743 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY
, CMS_R_NO_PUBLIC_KEY
);
747 md
= EVP_get_digestbyobj(si
->digestAlgorithm
->algorithm
);
750 EVP_MD_CTX_init(mctx
);
751 if (EVP_DigestVerifyInit(mctx
, &si
->pctx
, md
, NULL
, si
->pkey
) <= 0)
754 if (!cms_sd_asn1_ctrl(si
, 1))
757 alen
= ASN1_item_i2d((ASN1_VALUE
*)si
->signedAttrs
, &abuf
,
758 ASN1_ITEM_rptr(CMS_Attributes_Verify
));
761 r
= EVP_DigestVerifyUpdate(mctx
, abuf
, alen
);
767 r
= EVP_DigestVerifyFinal(mctx
,
768 si
->signature
->data
, si
->signature
->length
);
770 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY
, CMS_R_VERIFICATION_FAILURE
);
772 EVP_MD_CTX_cleanup(mctx
);
776 /* Create a chain of digest BIOs from a CMS ContentInfo */
778 BIO
*cms_SignedData_init_bio(CMS_ContentInfo
*cms
)
783 sd
= cms_get0_signed(cms
);
786 if (cms
->d
.signedData
->encapContentInfo
->partial
)
787 cms_sd_set_version(sd
);
788 for (i
= 0; i
< sk_X509_ALGOR_num(sd
->digestAlgorithms
); i
++) {
789 X509_ALGOR
*digestAlgorithm
;
791 digestAlgorithm
= sk_X509_ALGOR_value(sd
->digestAlgorithms
, i
);
792 mdbio
= cms_DigestAlgorithm_init_bio(digestAlgorithm
);
796 BIO_push(chain
, mdbio
);
806 int CMS_SignerInfo_verify_content(CMS_SignerInfo
*si
, BIO
*chain
)
808 ASN1_OCTET_STRING
*os
= NULL
;
810 EVP_PKEY_CTX
*pkctx
= NULL
;
812 unsigned char mval
[EVP_MAX_MD_SIZE
];
814 EVP_MD_CTX_init(&mctx
);
815 /* If we have any signed attributes look for messageDigest value */
816 if (CMS_signed_get_attr_count(si
) >= 0) {
817 os
= CMS_signed_get0_data_by_OBJ(si
,
818 OBJ_nid2obj(NID_pkcs9_messageDigest
),
819 -3, V_ASN1_OCTET_STRING
);
821 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
822 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE
);
827 if (!cms_DigestAlgorithm_find_ctx(&mctx
, chain
, si
->digestAlgorithm
))
830 if (EVP_DigestFinal_ex(&mctx
, mval
, &mlen
) <= 0) {
831 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
832 CMS_R_UNABLE_TO_FINALIZE_CONTEXT
);
836 /* If messageDigest found compare it */
839 if (mlen
!= (unsigned int)os
->length
) {
840 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
841 CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH
);
845 if (memcmp(mval
, os
->data
, mlen
)) {
846 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
847 CMS_R_VERIFICATION_FAILURE
);
852 const EVP_MD
*md
= EVP_MD_CTX_md(&mctx
);
853 pkctx
= EVP_PKEY_CTX_new(si
->pkey
, NULL
);
856 if (EVP_PKEY_verify_init(pkctx
) <= 0)
858 if (EVP_PKEY_CTX_set_signature_md(pkctx
, md
) <= 0)
861 if (!cms_sd_asn1_ctrl(si
, 1))
863 r
= EVP_PKEY_verify(pkctx
, si
->signature
->data
,
864 si
->signature
->length
, mval
, mlen
);
866 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
867 CMS_R_VERIFICATION_FAILURE
);
873 EVP_PKEY_CTX_free(pkctx
);
874 EVP_MD_CTX_cleanup(&mctx
);
879 int CMS_add_smimecap(CMS_SignerInfo
*si
, STACK_OF(X509_ALGOR
) *algs
)
881 unsigned char *smder
= NULL
;
883 smderlen
= i2d_X509_ALGORS(algs
, &smder
);
886 r
= CMS_signed_add1_attr_by_NID(si
, NID_SMIMECapabilities
,
887 V_ASN1_SEQUENCE
, smder
, smderlen
);
892 int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR
) **algs
,
893 int algnid
, int keysize
)
896 ASN1_INTEGER
*key
= NULL
;
898 key
= ASN1_INTEGER_new();
899 if (key
== NULL
|| !ASN1_INTEGER_set(key
, keysize
))
902 alg
= X509_ALGOR_new();
904 ASN1_INTEGER_free(key
);
908 X509_ALGOR_set0(alg
, OBJ_nid2obj(algnid
),
909 key
? V_ASN1_INTEGER
: V_ASN1_UNDEF
, key
);
911 *algs
= sk_X509_ALGOR_new_null();
912 if (*algs
== NULL
|| !sk_X509_ALGOR_push(*algs
, alg
)) {
913 X509_ALGOR_free(alg
);
919 /* Check to see if a cipher exists and if so add S/MIME capabilities */
921 static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR
) **sk
, int nid
, int arg
)
923 if (EVP_get_cipherbynid(nid
))
924 return CMS_add_simple_smimecap(sk
, nid
, arg
);
928 static int cms_add_digest_smcap(STACK_OF(X509_ALGOR
) **sk
, int nid
, int arg
)
930 if (EVP_get_digestbynid(nid
))
931 return CMS_add_simple_smimecap(sk
, nid
, arg
);
935 int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR
) **smcap
)
937 if (!cms_add_cipher_smcap(smcap
, NID_aes_256_cbc
, -1)
938 || !cms_add_digest_smcap(smcap
, NID_id_GostR3411_2012_256
, -1)
939 || !cms_add_digest_smcap(smcap
, NID_id_GostR3411_2012_512
, -1)
940 || !cms_add_digest_smcap(smcap
, NID_id_GostR3411_94
, -1)
941 || !cms_add_cipher_smcap(smcap
, NID_id_Gost28147_89
, -1)
942 || !cms_add_cipher_smcap(smcap
, NID_aes_192_cbc
, -1)
943 || !cms_add_cipher_smcap(smcap
, NID_aes_128_cbc
, -1)
944 || !cms_add_cipher_smcap(smcap
, NID_des_ede3_cbc
, -1)
945 || !cms_add_cipher_smcap(smcap
, NID_rc2_cbc
, 128)
946 || !cms_add_cipher_smcap(smcap
, NID_rc2_cbc
, 64)
947 || !cms_add_cipher_smcap(smcap
, NID_des_cbc
, -1)
948 || !cms_add_cipher_smcap(smcap
, NID_rc2_cbc
, 40))