2 * Copyright 2008-2020 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>
18 #include "cms_local.h"
19 #include "crypto/asn1.h"
20 #include "crypto/evp.h"
21 #include "crypto/cms.h"
22 #include "crypto/ess.h"
23 #include "crypto/x509.h" /* for X509_add_cert_new() */
25 DEFINE_STACK_OF(CMS_RevocationInfoChoice
)
26 DEFINE_STACK_OF(CMS_SignerInfo
)
28 DEFINE_STACK_OF(X509_ALGOR
)
29 DEFINE_STACK_OF(X509_ATTRIBUTE
)
31 /* CMS SignedData Utilities */
33 static CMS_SignedData
*cms_get0_signed(CMS_ContentInfo
*cms
)
35 if (OBJ_obj2nid(cms
->contentType
) != NID_pkcs7_signed
) {
36 CMSerr(CMS_F_CMS_GET0_SIGNED
, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA
);
39 return cms
->d
.signedData
;
42 static CMS_SignedData
*cms_signed_data_init(CMS_ContentInfo
*cms
)
44 if (cms
->d
.other
== NULL
) {
45 cms
->d
.signedData
= M_ASN1_new_of(CMS_SignedData
);
46 if (!cms
->d
.signedData
) {
47 CMSerr(CMS_F_CMS_SIGNED_DATA_INIT
, ERR_R_MALLOC_FAILURE
);
50 cms
->d
.signedData
->version
= 1;
51 cms
->d
.signedData
->encapContentInfo
->eContentType
=
52 OBJ_nid2obj(NID_pkcs7_data
);
53 cms
->d
.signedData
->encapContentInfo
->partial
= 1;
54 ASN1_OBJECT_free(cms
->contentType
);
55 cms
->contentType
= OBJ_nid2obj(NID_pkcs7_signed
);
56 return cms
->d
.signedData
;
58 return cms_get0_signed(cms
);
61 /* Just initialise SignedData e.g. for certs only structure */
63 int CMS_SignedData_init(CMS_ContentInfo
*cms
)
65 if (cms_signed_data_init(cms
))
72 /* Check structures and fixup version numbers (if necessary) */
74 static void cms_sd_set_version(CMS_SignedData
*sd
)
77 CMS_CertificateChoices
*cch
;
78 CMS_RevocationInfoChoice
*rch
;
81 for (i
= 0; i
< sk_CMS_CertificateChoices_num(sd
->certificates
); i
++) {
82 cch
= sk_CMS_CertificateChoices_value(sd
->certificates
, i
);
83 if (cch
->type
== CMS_CERTCHOICE_OTHER
) {
86 } else if (cch
->type
== CMS_CERTCHOICE_V2ACERT
) {
89 } else if (cch
->type
== CMS_CERTCHOICE_V1ACERT
) {
95 for (i
= 0; i
< sk_CMS_RevocationInfoChoice_num(sd
->crls
); i
++) {
96 rch
= sk_CMS_RevocationInfoChoice_value(sd
->crls
, i
);
97 if (rch
->type
== CMS_REVCHOICE_OTHER
) {
103 if ((OBJ_obj2nid(sd
->encapContentInfo
->eContentType
) != NID_pkcs7_data
)
104 && (sd
->version
< 3))
107 for (i
= 0; i
< sk_CMS_SignerInfo_num(sd
->signerInfos
); i
++) {
108 si
= sk_CMS_SignerInfo_value(sd
->signerInfos
, i
);
109 if (si
->sid
->type
== CMS_SIGNERINFO_KEYIDENTIFIER
) {
114 } else if (si
->version
< 1)
124 * RFC 5652 Section 11.1 Content Type
125 * The content-type attribute within signed-data MUST
126 * 1) be present if there are signed attributes
127 * 2) match the content type in the signed-data,
128 * 3) be a signed attribute.
129 * 4) not have more than one copy of the attribute.
131 * Note that since the CMS_SignerInfo_sign() always adds the "signing time"
132 * attribute, the content type attribute MUST be added also.
133 * Assumptions: This assumes that the attribute does not already exist.
135 static int cms_set_si_contentType_attr(CMS_ContentInfo
*cms
, CMS_SignerInfo
*si
)
137 ASN1_OBJECT
*ctype
= cms
->d
.signedData
->encapContentInfo
->eContentType
;
139 /* Add the contentType attribute */
140 return CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_contentType
,
141 V_ASN1_OBJECT
, ctype
, -1) > 0;
144 /* Copy an existing messageDigest value */
146 static int cms_copy_messageDigest(CMS_ContentInfo
*cms
, CMS_SignerInfo
*si
)
148 STACK_OF(CMS_SignerInfo
) *sinfos
;
149 CMS_SignerInfo
*sitmp
;
152 sinfos
= CMS_get0_SignerInfos(cms
);
153 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
154 ASN1_OCTET_STRING
*messageDigest
;
156 sitmp
= sk_CMS_SignerInfo_value(sinfos
, i
);
159 if (CMS_signed_get_attr_count(sitmp
) < 0)
161 if (OBJ_cmp(si
->digestAlgorithm
->algorithm
,
162 sitmp
->digestAlgorithm
->algorithm
))
164 messageDigest
= CMS_signed_get0_data_by_OBJ(sitmp
,
166 (NID_pkcs9_messageDigest
),
167 -3, V_ASN1_OCTET_STRING
);
168 if (!messageDigest
) {
169 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST
,
170 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE
);
174 if (CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_messageDigest
,
181 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST
, CMS_R_NO_MATCHING_DIGEST
);
185 int cms_set1_SignerIdentifier(CMS_SignerIdentifier
*sid
, X509
*cert
, int type
,
189 case CMS_SIGNERINFO_ISSUER_SERIAL
:
190 if (!cms_set1_ias(&sid
->d
.issuerAndSerialNumber
, cert
))
194 case CMS_SIGNERINFO_KEYIDENTIFIER
:
195 if (!cms_set1_keyid(&sid
->d
.subjectKeyIdentifier
, cert
))
200 CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER
, CMS_R_UNKNOWN_ID
);
209 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier
*sid
,
210 ASN1_OCTET_STRING
**keyid
,
214 if (sid
->type
== CMS_SIGNERINFO_ISSUER_SERIAL
) {
216 *issuer
= sid
->d
.issuerAndSerialNumber
->issuer
;
218 *sno
= sid
->d
.issuerAndSerialNumber
->serialNumber
;
219 } else if (sid
->type
== CMS_SIGNERINFO_KEYIDENTIFIER
) {
221 *keyid
= sid
->d
.subjectKeyIdentifier
;
227 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier
*sid
, X509
*cert
)
229 if (sid
->type
== CMS_SIGNERINFO_ISSUER_SERIAL
)
230 return cms_ias_cert_cmp(sid
->d
.issuerAndSerialNumber
, cert
);
231 else if (sid
->type
== CMS_SIGNERINFO_KEYIDENTIFIER
)
232 return cms_keyid_cert_cmp(sid
->d
.subjectKeyIdentifier
, cert
);
237 static int cms_sd_asn1_ctrl(CMS_SignerInfo
*si
, int cmd
)
239 EVP_PKEY
*pkey
= si
->pkey
;
242 if (pkey
->ameth
== NULL
|| pkey
->ameth
->pkey_ctrl
== NULL
)
244 i
= pkey
->ameth
->pkey_ctrl(pkey
, ASN1_PKEY_CTRL_CMS_SIGN
, cmd
, si
);
246 CMSerr(CMS_F_CMS_SD_ASN1_CTRL
, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE
);
250 CMSerr(CMS_F_CMS_SD_ASN1_CTRL
, CMS_R_CTRL_FAILURE
);
256 CMS_SignerInfo
*CMS_add1_signer(CMS_ContentInfo
*cms
,
257 X509
*signer
, EVP_PKEY
*pk
, const EVP_MD
*md
,
261 CMS_SignerInfo
*si
= NULL
;
264 const CMS_CTX
*ctx
= cms_get0_cmsctx(cms
);
266 if (!X509_check_private_key(signer
, pk
)) {
267 CMSerr(CMS_F_CMS_ADD1_SIGNER
,
268 CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE
);
271 sd
= cms_signed_data_init(cms
);
274 si
= M_ASN1_new_of(CMS_SignerInfo
);
277 /* Call for side-effect of computing hash and caching extensions */
278 X509_check_purpose(signer
, -1, -1);
286 si
->mctx
= EVP_MD_CTX_new();
289 if (si
->mctx
== NULL
) {
290 CMSerr(CMS_F_CMS_ADD1_SIGNER
, ERR_R_MALLOC_FAILURE
);
294 if (flags
& CMS_USE_KEYID
) {
298 type
= CMS_SIGNERINFO_KEYIDENTIFIER
;
300 type
= CMS_SIGNERINFO_ISSUER_SERIAL
;
304 if (!cms_set1_SignerIdentifier(si
->sid
, signer
, type
, ctx
))
309 if (EVP_PKEY_get_default_digest_nid(pk
, &def_nid
) <= 0)
311 md
= EVP_get_digestbynid(def_nid
);
313 CMSerr(CMS_F_CMS_ADD1_SIGNER
, CMS_R_NO_DEFAULT_DIGEST
);
319 CMSerr(CMS_F_CMS_ADD1_SIGNER
, CMS_R_NO_DIGEST_SET
);
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
++) {
332 const ASN1_OBJECT
*aoid
;
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 * Initialize signed attributes structure 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_CADES
) {
373 ESS_SIGNING_CERT
*sc
= NULL
;
374 ESS_SIGNING_CERT_V2
*sc2
= NULL
;
377 if (md
== EVP_sha1() || md
== NULL
) {
378 if ((sc
= ESS_SIGNING_CERT_new_init(signer
,
381 add_sc
= cms_add1_signing_cert(si
, sc
);
382 ESS_SIGNING_CERT_free(sc
);
384 if ((sc2
= ESS_SIGNING_CERT_V2_new_init(md
, signer
,
387 add_sc
= cms_add1_signing_cert_v2(si
, sc2
);
388 ESS_SIGNING_CERT_V2_free(sc2
);
393 if (flags
& CMS_REUSE_DIGEST
) {
394 if (!cms_copy_messageDigest(cms
, si
))
396 if (!cms_set_si_contentType_attr(cms
, si
))
398 if (!(flags
& (CMS_PARTIAL
| CMS_KEY_PARAM
)) &&
399 !CMS_SignerInfo_sign(si
))
404 if (!(flags
& CMS_NOCERTS
)) {
405 /* NB ignore -1 return for duplicate cert */
406 if (!CMS_add1_cert(cms
, signer
))
410 if (flags
& CMS_KEY_PARAM
) {
411 if (flags
& CMS_NOATTR
) {
412 si
->pctx
= EVP_PKEY_CTX_new_from_pkey(ctx
->libctx
, si
->pkey
,
414 if (si
->pctx
== NULL
)
416 if (EVP_PKEY_sign_init(si
->pctx
) <= 0)
418 if (EVP_PKEY_CTX_set_signature_md(si
->pctx
, md
) <= 0)
420 } else if (EVP_DigestSignInit_with_libctx(si
->mctx
, &si
->pctx
,
422 ctx
->libctx
, ctx
->propq
,
428 if (!sd
->signerInfos
)
429 sd
->signerInfos
= sk_CMS_SignerInfo_new_null();
430 if (!sd
->signerInfos
|| !sk_CMS_SignerInfo_push(sd
->signerInfos
, si
))
436 CMSerr(CMS_F_CMS_ADD1_SIGNER
, ERR_R_MALLOC_FAILURE
);
438 M_ASN1_free_of(si
, CMS_SignerInfo
);
443 void cms_SignerInfos_set_cmsctx(CMS_ContentInfo
*cms
)
447 STACK_OF(CMS_SignerInfo
) *sinfos
= CMS_get0_SignerInfos(cms
);
448 const CMS_CTX
*ctx
= cms_get0_cmsctx(cms
);
450 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
451 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
457 static int cms_add1_signingTime(CMS_SignerInfo
*si
, ASN1_TIME
*t
)
465 tt
= X509_gmtime_adj(NULL
, 0);
470 if (CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_signingTime
,
471 tt
->type
, tt
, -1) <= 0)
480 CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME
, ERR_R_MALLOC_FAILURE
);
486 EVP_PKEY_CTX
*CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo
*si
)
491 EVP_MD_CTX
*CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo
*si
)
496 STACK_OF(CMS_SignerInfo
) *CMS_get0_SignerInfos(CMS_ContentInfo
*cms
)
498 CMS_SignedData
*sd
= cms_get0_signed(cms
);
500 return sd
!= NULL
? sd
->signerInfos
: NULL
;
503 STACK_OF(X509
) *CMS_get0_signers(CMS_ContentInfo
*cms
)
505 STACK_OF(X509
) *signers
= NULL
;
506 STACK_OF(CMS_SignerInfo
) *sinfos
;
510 sinfos
= CMS_get0_SignerInfos(cms
);
511 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
512 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
513 if (si
->signer
!= NULL
) {
514 if (!X509_add_cert_new(&signers
, si
->signer
,
515 X509_ADD_FLAG_DEFAULT
)) {
516 sk_X509_free(signers
);
524 void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo
*si
, X509
*signer
)
526 if (signer
!= NULL
) {
528 EVP_PKEY_free(si
->pkey
);
529 si
->pkey
= X509_get_pubkey(signer
);
531 X509_free(si
->signer
);
535 int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo
*si
,
536 ASN1_OCTET_STRING
**keyid
,
537 X509_NAME
**issuer
, ASN1_INTEGER
**sno
)
539 return cms_SignerIdentifier_get0_signer_id(si
->sid
, keyid
, issuer
, sno
);
542 int CMS_SignerInfo_cert_cmp(CMS_SignerInfo
*si
, X509
*cert
)
544 return cms_SignerIdentifier_cert_cmp(si
->sid
, cert
);
547 int CMS_set1_signers_certs(CMS_ContentInfo
*cms
, STACK_OF(X509
) *scerts
,
552 CMS_CertificateChoices
*cch
;
553 STACK_OF(CMS_CertificateChoices
) *certs
;
558 sd
= cms_get0_signed(cms
);
561 certs
= sd
->certificates
;
562 for (i
= 0; i
< sk_CMS_SignerInfo_num(sd
->signerInfos
); i
++) {
563 si
= sk_CMS_SignerInfo_value(sd
->signerInfos
, i
);
564 if (si
->signer
!= NULL
)
567 for (j
= 0; j
< sk_X509_num(scerts
); j
++) {
568 x
= sk_X509_value(scerts
, j
);
569 if (CMS_SignerInfo_cert_cmp(si
, x
) == 0) {
570 CMS_SignerInfo_set1_signer_cert(si
, x
);
576 if (si
->signer
!= NULL
|| (flags
& CMS_NOINTERN
))
579 for (j
= 0; j
< sk_CMS_CertificateChoices_num(certs
); j
++) {
580 cch
= sk_CMS_CertificateChoices_value(certs
, j
);
583 x
= cch
->d
.certificate
;
584 if (CMS_SignerInfo_cert_cmp(si
, x
) == 0) {
585 CMS_SignerInfo_set1_signer_cert(si
, x
);
594 void CMS_SignerInfo_get0_algs(CMS_SignerInfo
*si
, EVP_PKEY
**pk
,
595 X509
**signer
, X509_ALGOR
**pdig
,
601 *signer
= si
->signer
;
603 *pdig
= si
->digestAlgorithm
;
605 *psig
= si
->signatureAlgorithm
;
608 ASN1_OCTET_STRING
*CMS_SignerInfo_get0_signature(CMS_SignerInfo
*si
)
610 return si
->signature
;
613 static int cms_SignerInfo_content_sign(CMS_ContentInfo
*cms
,
614 CMS_SignerInfo
*si
, BIO
*chain
)
616 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
618 EVP_PKEY_CTX
*pctx
= NULL
;
619 const CMS_CTX
*ctx
= cms_get0_cmsctx(cms
);
622 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN
, ERR_R_MALLOC_FAILURE
);
626 if (si
->pkey
== NULL
) {
627 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN
, CMS_R_NO_PRIVATE_KEY
);
631 if (!cms_DigestAlgorithm_find_ctx(mctx
, chain
, si
->digestAlgorithm
))
633 /* Set SignerInfo algorithm details if we used custom parameter */
634 if (si
->pctx
&& !cms_sd_asn1_ctrl(si
, 0))
638 * If any signed attributes calculate and add messageDigest attribute
641 if (CMS_signed_get_attr_count(si
) >= 0) {
642 unsigned char md
[EVP_MAX_MD_SIZE
];
645 if (!EVP_DigestFinal_ex(mctx
, md
, &mdlen
))
647 if (!CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_messageDigest
,
648 V_ASN1_OCTET_STRING
, md
, mdlen
))
650 /* Copy content type across */
651 if (!cms_set_si_contentType_attr(cms
, si
))
654 if (!CMS_SignerInfo_sign(si
))
656 } else if (si
->pctx
) {
659 unsigned char md
[EVP_MAX_MD_SIZE
];
663 if (!EVP_DigestFinal_ex(mctx
, md
, &mdlen
))
665 siglen
= EVP_PKEY_size(si
->pkey
);
666 sig
= OPENSSL_malloc(siglen
);
668 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN
, ERR_R_MALLOC_FAILURE
);
671 if (EVP_PKEY_sign(pctx
, sig
, &siglen
, md
, mdlen
) <= 0) {
675 ASN1_STRING_set0(si
->signature
, sig
, siglen
);
680 sig
= OPENSSL_malloc(EVP_PKEY_size(si
->pkey
));
682 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN
, ERR_R_MALLOC_FAILURE
);
685 if (!EVP_SignFinal_with_libctx(mctx
, sig
, &siglen
, si
->pkey
,
686 ctx
->libctx
, ctx
->propq
)) {
687 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN
, CMS_R_SIGNFINAL_ERROR
);
691 ASN1_STRING_set0(si
->signature
, sig
, siglen
);
697 EVP_MD_CTX_free(mctx
);
698 EVP_PKEY_CTX_free(pctx
);
703 int cms_SignedData_final(CMS_ContentInfo
*cms
, BIO
*chain
)
705 STACK_OF(CMS_SignerInfo
) *sinfos
;
709 sinfos
= CMS_get0_SignerInfos(cms
);
710 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
711 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
712 if (!cms_SignerInfo_content_sign(cms
, si
, chain
))
715 cms
->d
.signedData
->encapContentInfo
->partial
= 0;
719 int CMS_SignerInfo_sign(CMS_SignerInfo
*si
)
721 EVP_MD_CTX
*mctx
= si
->mctx
;
722 EVP_PKEY_CTX
*pctx
= NULL
;
723 unsigned char *abuf
= NULL
;
726 const CMS_CTX
*ctx
= si
->cms_ctx
;
727 const char *md_name
= OBJ_nid2sn(OBJ_obj2nid(si
->digestAlgorithm
->algorithm
));
732 if (CMS_signed_get_attr_by_NID(si
, NID_pkcs9_signingTime
, -1) < 0) {
733 if (!cms_add1_signingTime(si
, NULL
))
737 if (!CMS_si_check_attributes(si
))
743 EVP_MD_CTX_reset(mctx
);
744 if (EVP_DigestSignInit_with_libctx(mctx
, &pctx
,
745 md_name
, ctx
->libctx
, ctx
->propq
,
752 * TODO(3.0): This causes problems when providers are in use, so disabled
753 * for now. Can we get rid of this completely? AFAICT this ctrl has been
754 * present since CMS was first put in - but has never been used to do
755 * anything. All internal implementations just return 1 and ignore this ctrl
756 * and have always done so by the looks of things. To fix this we could
757 * convert this ctrl into a param, which would require us to send all the
758 * signer info data as a set of params...but that is non-trivial and since
759 * this isn't used by anything it may be better just to remove it.
762 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
763 EVP_PKEY_CTRL_CMS_SIGN
, 0, si
) <= 0) {
764 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN
, CMS_R_CTRL_ERROR
);
769 alen
= ASN1_item_i2d((ASN1_VALUE
*)si
->signedAttrs
, &abuf
,
770 ASN1_ITEM_rptr(CMS_Attributes_Sign
));
773 if (EVP_DigestSignUpdate(mctx
, abuf
, alen
) <= 0)
775 if (EVP_DigestSignFinal(mctx
, NULL
, &siglen
) <= 0)
778 abuf
= OPENSSL_malloc(siglen
);
781 if (EVP_DigestSignFinal(mctx
, abuf
, &siglen
) <= 0)
785 * TODO(3.0): This causes problems when providers are in use, so disabled
786 * for now. Can we get rid of this completely? AFAICT this ctrl has been
787 * present since CMS was first put in - but has never been used to do
788 * anything. All internal implementations just return 1 and ignore this ctrl
789 * and have always done so by the looks of things. To fix this we could
790 * convert this ctrl into a param, which would require us to send all the
791 * signer info data as a set of params...but that is non-trivial and since
792 * this isn't used by anything it may be better just to remove it.
795 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
796 EVP_PKEY_CTRL_CMS_SIGN
, 1, si
) <= 0) {
797 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN
, CMS_R_CTRL_ERROR
);
802 EVP_MD_CTX_reset(mctx
);
804 ASN1_STRING_set0(si
->signature
, abuf
, siglen
);
810 EVP_MD_CTX_reset(mctx
);
814 int CMS_SignerInfo_verify(CMS_SignerInfo
*si
)
816 EVP_MD_CTX
*mctx
= NULL
;
817 unsigned char *abuf
= NULL
;
821 const CMS_CTX
*ctx
= si
->cms_ctx
;
823 if (si
->pkey
== NULL
) {
824 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY
, CMS_R_NO_PUBLIC_KEY
);
828 if (!CMS_si_check_attributes(si
))
831 name
= OBJ_nid2sn(OBJ_obj2nid(si
->digestAlgorithm
->algorithm
));
832 md
= EVP_MD_fetch(ctx
->libctx
, name
, ctx
->propq
);
835 if (si
->mctx
== NULL
&& (si
->mctx
= EVP_MD_CTX_new()) == NULL
) {
836 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY
, ERR_R_MALLOC_FAILURE
);
840 if (EVP_DigestVerifyInit_with_libctx(mctx
, &si
->pctx
,
841 EVP_MD_name(md
), ctx
->libctx
, NULL
,
845 if (!cms_sd_asn1_ctrl(si
, 1))
848 alen
= ASN1_item_i2d((ASN1_VALUE
*)si
->signedAttrs
, &abuf
,
849 ASN1_ITEM_rptr(CMS_Attributes_Verify
));
852 r
= EVP_DigestVerifyUpdate(mctx
, abuf
, alen
);
858 r
= EVP_DigestVerifyFinal(mctx
,
859 si
->signature
->data
, si
->signature
->length
);
861 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY
, CMS_R_VERIFICATION_FAILURE
);
864 EVP_MD_CTX_reset(mctx
);
868 /* Create a chain of digest BIOs from a CMS ContentInfo */
870 BIO
*cms_SignedData_init_bio(CMS_ContentInfo
*cms
)
876 sd
= cms_get0_signed(cms
);
879 if (cms
->d
.signedData
->encapContentInfo
->partial
)
880 cms_sd_set_version(sd
);
881 for (i
= 0; i
< sk_X509_ALGOR_num(sd
->digestAlgorithms
); i
++) {
882 X509_ALGOR
*digestAlgorithm
;
885 digestAlgorithm
= sk_X509_ALGOR_value(sd
->digestAlgorithms
, i
);
886 mdbio
= cms_DigestAlgorithm_init_bio(digestAlgorithm
, cms_get0_cmsctx(cms
));
890 BIO_push(chain
, mdbio
);
900 int CMS_SignerInfo_verify_content(CMS_SignerInfo
*si
, BIO
*chain
)
902 ASN1_OCTET_STRING
*os
= NULL
;
903 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
904 EVP_PKEY_CTX
*pkctx
= NULL
;
906 unsigned char mval
[EVP_MAX_MD_SIZE
];
910 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
, ERR_R_MALLOC_FAILURE
);
913 /* If we have any signed attributes look for messageDigest value */
914 if (CMS_signed_get_attr_count(si
) >= 0) {
915 os
= CMS_signed_get0_data_by_OBJ(si
,
916 OBJ_nid2obj(NID_pkcs9_messageDigest
),
917 -3, V_ASN1_OCTET_STRING
);
919 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
920 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE
);
925 if (!cms_DigestAlgorithm_find_ctx(mctx
, chain
, si
->digestAlgorithm
))
928 if (EVP_DigestFinal_ex(mctx
, mval
, &mlen
) <= 0) {
929 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
930 CMS_R_UNABLE_TO_FINALIZE_CONTEXT
);
934 /* If messageDigest found compare it */
937 if (mlen
!= (unsigned int)os
->length
) {
938 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
939 CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH
);
943 if (memcmp(mval
, os
->data
, mlen
)) {
944 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
945 CMS_R_VERIFICATION_FAILURE
);
950 const EVP_MD
*md
= EVP_MD_CTX_md(mctx
);
951 const CMS_CTX
*ctx
= si
->cms_ctx
;
953 pkctx
= EVP_PKEY_CTX_new_from_pkey(ctx
->libctx
, si
->pkey
, ctx
->propq
);
956 if (EVP_PKEY_verify_init(pkctx
) <= 0)
958 if (EVP_PKEY_CTX_set_signature_md(pkctx
, md
) <= 0)
961 if (!cms_sd_asn1_ctrl(si
, 1))
963 r
= EVP_PKEY_verify(pkctx
, si
->signature
->data
,
964 si
->signature
->length
, mval
, mlen
);
966 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT
,
967 CMS_R_VERIFICATION_FAILURE
);
973 EVP_PKEY_CTX_free(pkctx
);
974 EVP_MD_CTX_free(mctx
);
979 int CMS_add_smimecap(CMS_SignerInfo
*si
, STACK_OF(X509_ALGOR
) *algs
)
981 unsigned char *smder
= NULL
;
984 smderlen
= i2d_X509_ALGORS(algs
, &smder
);
987 r
= CMS_signed_add1_attr_by_NID(si
, NID_SMIMECapabilities
,
988 V_ASN1_SEQUENCE
, smder
, smderlen
);
993 int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR
) **algs
,
994 int algnid
, int keysize
)
997 ASN1_INTEGER
*key
= NULL
;
1000 key
= ASN1_INTEGER_new();
1001 if (key
== NULL
|| !ASN1_INTEGER_set(key
, keysize
)) {
1002 ASN1_INTEGER_free(key
);
1006 alg
= X509_ALGOR_new();
1008 ASN1_INTEGER_free(key
);
1012 X509_ALGOR_set0(alg
, OBJ_nid2obj(algnid
),
1013 key
? V_ASN1_INTEGER
: V_ASN1_UNDEF
, key
);
1015 *algs
= sk_X509_ALGOR_new_null();
1016 if (*algs
== NULL
|| !sk_X509_ALGOR_push(*algs
, alg
)) {
1017 X509_ALGOR_free(alg
);
1023 /* Check to see if a cipher exists and if so add S/MIME capabilities */
1025 static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR
) **sk
, int nid
, int arg
)
1027 if (EVP_get_cipherbynid(nid
))
1028 return CMS_add_simple_smimecap(sk
, nid
, arg
);
1032 static int cms_add_digest_smcap(STACK_OF(X509_ALGOR
) **sk
, int nid
, int arg
)
1034 if (EVP_get_digestbynid(nid
))
1035 return CMS_add_simple_smimecap(sk
, nid
, arg
);
1039 int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR
) **smcap
)
1041 if (!cms_add_cipher_smcap(smcap
, NID_aes_256_cbc
, -1)
1042 || !cms_add_digest_smcap(smcap
, NID_id_GostR3411_2012_256
, -1)
1043 || !cms_add_digest_smcap(smcap
, NID_id_GostR3411_2012_512
, -1)
1044 || !cms_add_digest_smcap(smcap
, NID_id_GostR3411_94
, -1)
1045 || !cms_add_cipher_smcap(smcap
, NID_id_Gost28147_89
, -1)
1046 || !cms_add_cipher_smcap(smcap
, NID_aes_192_cbc
, -1)
1047 || !cms_add_cipher_smcap(smcap
, NID_aes_128_cbc
, -1)
1048 || !cms_add_cipher_smcap(smcap
, NID_des_ede3_cbc
, -1)
1049 || !cms_add_cipher_smcap(smcap
, NID_rc2_cbc
, 128)
1050 || !cms_add_cipher_smcap(smcap
, NID_rc2_cbc
, 64)
1051 || !cms_add_cipher_smcap(smcap
, NID_des_cbc
, -1)
1052 || !cms_add_cipher_smcap(smcap
, NID_rc2_cbc
, 40))