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 /* CMS SignedData Utilities */
27 static CMS_SignedData
*cms_get0_signed(CMS_ContentInfo
*cms
)
29 if (OBJ_obj2nid(cms
->contentType
) != NID_pkcs7_signed
) {
30 ERR_raise(ERR_LIB_CMS
, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA
);
33 return cms
->d
.signedData
;
36 static CMS_SignedData
*cms_signed_data_init(CMS_ContentInfo
*cms
)
38 if (cms
->d
.other
== NULL
) {
39 cms
->d
.signedData
= M_ASN1_new_of(CMS_SignedData
);
40 if (!cms
->d
.signedData
) {
41 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
44 cms
->d
.signedData
->version
= 1;
45 cms
->d
.signedData
->encapContentInfo
->eContentType
=
46 OBJ_nid2obj(NID_pkcs7_data
);
47 cms
->d
.signedData
->encapContentInfo
->partial
= 1;
48 ASN1_OBJECT_free(cms
->contentType
);
49 cms
->contentType
= OBJ_nid2obj(NID_pkcs7_signed
);
50 return cms
->d
.signedData
;
52 return cms_get0_signed(cms
);
55 /* Just initialise SignedData e.g. for certs only structure */
57 int CMS_SignedData_init(CMS_ContentInfo
*cms
)
59 if (cms_signed_data_init(cms
))
66 /* Check structures and fixup version numbers (if necessary) */
68 static void cms_sd_set_version(CMS_SignedData
*sd
)
71 CMS_CertificateChoices
*cch
;
72 CMS_RevocationInfoChoice
*rch
;
75 for (i
= 0; i
< sk_CMS_CertificateChoices_num(sd
->certificates
); i
++) {
76 cch
= sk_CMS_CertificateChoices_value(sd
->certificates
, i
);
77 if (cch
->type
== CMS_CERTCHOICE_OTHER
) {
80 } else if (cch
->type
== CMS_CERTCHOICE_V2ACERT
) {
83 } else if (cch
->type
== CMS_CERTCHOICE_V1ACERT
) {
89 for (i
= 0; i
< sk_CMS_RevocationInfoChoice_num(sd
->crls
); i
++) {
90 rch
= sk_CMS_RevocationInfoChoice_value(sd
->crls
, i
);
91 if (rch
->type
== CMS_REVCHOICE_OTHER
) {
97 if ((OBJ_obj2nid(sd
->encapContentInfo
->eContentType
) != NID_pkcs7_data
)
101 for (i
= 0; i
< sk_CMS_SignerInfo_num(sd
->signerInfos
); i
++) {
102 si
= sk_CMS_SignerInfo_value(sd
->signerInfos
, i
);
103 if (si
->sid
->type
== CMS_SIGNERINFO_KEYIDENTIFIER
) {
108 } else if (si
->version
< 1)
118 * RFC 5652 Section 11.1 Content Type
119 * The content-type attribute within signed-data MUST
120 * 1) be present if there are signed attributes
121 * 2) match the content type in the signed-data,
122 * 3) be a signed attribute.
123 * 4) not have more than one copy of the attribute.
125 * Note that since the CMS_SignerInfo_sign() always adds the "signing time"
126 * attribute, the content type attribute MUST be added also.
127 * Assumptions: This assumes that the attribute does not already exist.
129 static int cms_set_si_contentType_attr(CMS_ContentInfo
*cms
, CMS_SignerInfo
*si
)
131 ASN1_OBJECT
*ctype
= cms
->d
.signedData
->encapContentInfo
->eContentType
;
133 /* Add the contentType attribute */
134 return CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_contentType
,
135 V_ASN1_OBJECT
, ctype
, -1) > 0;
138 /* Copy an existing messageDigest value */
140 static int cms_copy_messageDigest(CMS_ContentInfo
*cms
, CMS_SignerInfo
*si
)
142 STACK_OF(CMS_SignerInfo
) *sinfos
;
143 CMS_SignerInfo
*sitmp
;
146 sinfos
= CMS_get0_SignerInfos(cms
);
147 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
148 ASN1_OCTET_STRING
*messageDigest
;
150 sitmp
= sk_CMS_SignerInfo_value(sinfos
, i
);
153 if (CMS_signed_get_attr_count(sitmp
) < 0)
155 if (OBJ_cmp(si
->digestAlgorithm
->algorithm
,
156 sitmp
->digestAlgorithm
->algorithm
))
158 messageDigest
= CMS_signed_get0_data_by_OBJ(sitmp
,
160 (NID_pkcs9_messageDigest
),
161 -3, V_ASN1_OCTET_STRING
);
162 if (!messageDigest
) {
163 ERR_raise(ERR_LIB_CMS
, CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE
);
167 if (CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_messageDigest
,
174 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_MATCHING_DIGEST
);
178 int cms_set1_SignerIdentifier(CMS_SignerIdentifier
*sid
, X509
*cert
, int type
,
182 case CMS_SIGNERINFO_ISSUER_SERIAL
:
183 if (!cms_set1_ias(&sid
->d
.issuerAndSerialNumber
, cert
))
187 case CMS_SIGNERINFO_KEYIDENTIFIER
:
188 if (!cms_set1_keyid(&sid
->d
.subjectKeyIdentifier
, cert
))
193 ERR_raise(ERR_LIB_CMS
, CMS_R_UNKNOWN_ID
);
202 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier
*sid
,
203 ASN1_OCTET_STRING
**keyid
,
207 if (sid
->type
== CMS_SIGNERINFO_ISSUER_SERIAL
) {
209 *issuer
= sid
->d
.issuerAndSerialNumber
->issuer
;
211 *sno
= sid
->d
.issuerAndSerialNumber
->serialNumber
;
212 } else if (sid
->type
== CMS_SIGNERINFO_KEYIDENTIFIER
) {
214 *keyid
= sid
->d
.subjectKeyIdentifier
;
220 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier
*sid
, X509
*cert
)
222 if (sid
->type
== CMS_SIGNERINFO_ISSUER_SERIAL
)
223 return cms_ias_cert_cmp(sid
->d
.issuerAndSerialNumber
, cert
);
224 else if (sid
->type
== CMS_SIGNERINFO_KEYIDENTIFIER
)
225 return cms_keyid_cert_cmp(sid
->d
.subjectKeyIdentifier
, cert
);
230 static int cms_sd_asn1_ctrl(CMS_SignerInfo
*si
, int cmd
)
232 EVP_PKEY
*pkey
= si
->pkey
;
235 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
236 if (EVP_PKEY_is_a(pkey
, "DSA") || EVP_PKEY_is_a(pkey
, "EC"))
237 return cms_ecdsa_dsa_sign(si
, cmd
);
240 if (EVP_PKEY_is_a(pkey
, "RSA") || EVP_PKEY_is_a(pkey
, "RSA-PSS"))
241 return cms_rsa_sign(si
, cmd
);
243 /* Something else? We'll give engines etc a chance to handle this */
244 if (pkey
->ameth
== NULL
|| pkey
->ameth
->pkey_ctrl
== NULL
)
246 i
= pkey
->ameth
->pkey_ctrl(pkey
, ASN1_PKEY_CTRL_CMS_SIGN
, cmd
, si
);
248 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE
);
252 ERR_raise(ERR_LIB_CMS
, CMS_R_CTRL_FAILURE
);
258 CMS_SignerInfo
*CMS_add1_signer(CMS_ContentInfo
*cms
,
259 X509
*signer
, EVP_PKEY
*pk
, const EVP_MD
*md
,
263 CMS_SignerInfo
*si
= NULL
;
266 const CMS_CTX
*ctx
= cms_get0_cmsctx(cms
);
268 if (!X509_check_private_key(signer
, pk
)) {
269 ERR_raise(ERR_LIB_CMS
, CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE
);
272 sd
= cms_signed_data_init(cms
);
275 si
= M_ASN1_new_of(CMS_SignerInfo
);
278 /* Call for side-effect of computing hash and caching extensions */
279 X509_check_purpose(signer
, -1, -1);
287 si
->mctx
= EVP_MD_CTX_new();
290 if (si
->mctx
== NULL
) {
291 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
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
, ctx
))
310 if (EVP_PKEY_get_default_digest_nid(pk
, &def_nid
) <= 0)
312 md
= EVP_get_digestbynid(def_nid
);
314 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_DEFAULT_DIGEST
);
320 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_DIGEST_SET
);
325 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_DIGEST_SET
);
329 X509_ALGOR_set_md(si
->digestAlgorithm
, md
);
331 /* See if digest is present in digestAlgorithms */
332 for (i
= 0; i
< sk_X509_ALGOR_num(sd
->digestAlgorithms
); i
++) {
333 const ASN1_OBJECT
*aoid
;
334 alg
= sk_X509_ALGOR_value(sd
->digestAlgorithms
, i
);
335 X509_ALGOR_get0(&aoid
, NULL
, NULL
, alg
);
336 if (OBJ_obj2nid(aoid
) == EVP_MD_type(md
))
340 if (i
== sk_X509_ALGOR_num(sd
->digestAlgorithms
)) {
341 alg
= X509_ALGOR_new();
344 X509_ALGOR_set_md(alg
, md
);
345 if (!sk_X509_ALGOR_push(sd
->digestAlgorithms
, alg
)) {
346 X509_ALGOR_free(alg
);
351 if (!(flags
& CMS_KEY_PARAM
) && !cms_sd_asn1_ctrl(si
, 0))
353 if (!(flags
& CMS_NOATTR
)) {
355 * Initialize signed attributes structure so other attributes
356 * such as signing time etc are added later even if we add none here.
358 if (!si
->signedAttrs
) {
359 si
->signedAttrs
= sk_X509_ATTRIBUTE_new_null();
360 if (!si
->signedAttrs
)
364 if (!(flags
& CMS_NOSMIMECAP
)) {
365 STACK_OF(X509_ALGOR
) *smcap
= NULL
;
366 i
= CMS_add_standard_smimecap(&smcap
);
368 i
= CMS_add_smimecap(si
, smcap
);
369 sk_X509_ALGOR_pop_free(smcap
, X509_ALGOR_free
);
373 if (flags
& CMS_CADES
) {
374 ESS_SIGNING_CERT
*sc
= NULL
;
375 ESS_SIGNING_CERT_V2
*sc2
= NULL
;
378 if (md
== EVP_sha1() || md
== NULL
) {
379 if ((sc
= ESS_SIGNING_CERT_new_init(signer
,
382 add_sc
= cms_add1_signing_cert(si
, sc
);
383 ESS_SIGNING_CERT_free(sc
);
385 if ((sc2
= ESS_SIGNING_CERT_V2_new_init(md
, signer
,
388 add_sc
= cms_add1_signing_cert_v2(si
, sc2
);
389 ESS_SIGNING_CERT_V2_free(sc2
);
394 if (flags
& CMS_REUSE_DIGEST
) {
395 if (!cms_copy_messageDigest(cms
, si
))
397 if (!cms_set_si_contentType_attr(cms
, si
))
399 if (!(flags
& (CMS_PARTIAL
| CMS_KEY_PARAM
)) &&
400 !CMS_SignerInfo_sign(si
))
405 if (!(flags
& CMS_NOCERTS
)) {
406 /* NB ignore -1 return for duplicate cert */
407 if (!CMS_add1_cert(cms
, signer
))
411 if (flags
& CMS_KEY_PARAM
) {
412 if (flags
& CMS_NOATTR
) {
413 si
->pctx
= EVP_PKEY_CTX_new_from_pkey(ctx
->libctx
, si
->pkey
,
415 if (si
->pctx
== NULL
)
417 if (EVP_PKEY_sign_init(si
->pctx
) <= 0)
419 if (EVP_PKEY_CTX_set_signature_md(si
->pctx
, md
) <= 0)
421 } else if (EVP_DigestSignInit_ex(si
->mctx
, &si
->pctx
, EVP_MD_name(md
),
422 ctx
->libctx
, ctx
->propq
, pk
) <= 0) {
427 if (!sd
->signerInfos
)
428 sd
->signerInfos
= sk_CMS_SignerInfo_new_null();
429 if (!sd
->signerInfos
|| !sk_CMS_SignerInfo_push(sd
->signerInfos
, si
))
435 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
437 M_ASN1_free_of(si
, CMS_SignerInfo
);
442 void cms_SignerInfos_set_cmsctx(CMS_ContentInfo
*cms
)
446 STACK_OF(CMS_SignerInfo
) *sinfos
= CMS_get0_SignerInfos(cms
);
447 const CMS_CTX
*ctx
= cms_get0_cmsctx(cms
);
449 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
450 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
456 static int cms_add1_signingTime(CMS_SignerInfo
*si
, ASN1_TIME
*t
)
464 tt
= X509_gmtime_adj(NULL
, 0);
469 if (CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_signingTime
,
470 tt
->type
, tt
, -1) <= 0)
479 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
485 EVP_PKEY_CTX
*CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo
*si
)
490 EVP_MD_CTX
*CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo
*si
)
495 STACK_OF(CMS_SignerInfo
) *CMS_get0_SignerInfos(CMS_ContentInfo
*cms
)
497 CMS_SignedData
*sd
= cms_get0_signed(cms
);
499 return sd
!= NULL
? sd
->signerInfos
: NULL
;
502 STACK_OF(X509
) *CMS_get0_signers(CMS_ContentInfo
*cms
)
504 STACK_OF(X509
) *signers
= NULL
;
505 STACK_OF(CMS_SignerInfo
) *sinfos
;
509 sinfos
= CMS_get0_SignerInfos(cms
);
510 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
511 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
512 if (si
->signer
!= NULL
) {
513 if (!X509_add_cert_new(&signers
, si
->signer
,
514 X509_ADD_FLAG_DEFAULT
)) {
515 sk_X509_free(signers
);
523 void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo
*si
, X509
*signer
)
525 if (signer
!= NULL
) {
527 EVP_PKEY_free(si
->pkey
);
528 si
->pkey
= X509_get_pubkey(signer
);
530 X509_free(si
->signer
);
534 int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo
*si
,
535 ASN1_OCTET_STRING
**keyid
,
536 X509_NAME
**issuer
, ASN1_INTEGER
**sno
)
538 return cms_SignerIdentifier_get0_signer_id(si
->sid
, keyid
, issuer
, sno
);
541 int CMS_SignerInfo_cert_cmp(CMS_SignerInfo
*si
, X509
*cert
)
543 return cms_SignerIdentifier_cert_cmp(si
->sid
, cert
);
546 int CMS_set1_signers_certs(CMS_ContentInfo
*cms
, STACK_OF(X509
) *scerts
,
551 CMS_CertificateChoices
*cch
;
552 STACK_OF(CMS_CertificateChoices
) *certs
;
557 sd
= cms_get0_signed(cms
);
560 certs
= sd
->certificates
;
561 for (i
= 0; i
< sk_CMS_SignerInfo_num(sd
->signerInfos
); i
++) {
562 si
= sk_CMS_SignerInfo_value(sd
->signerInfos
, i
);
563 if (si
->signer
!= NULL
)
566 for (j
= 0; j
< sk_X509_num(scerts
); j
++) {
567 x
= sk_X509_value(scerts
, j
);
568 if (CMS_SignerInfo_cert_cmp(si
, x
) == 0) {
569 CMS_SignerInfo_set1_signer_cert(si
, x
);
575 if (si
->signer
!= NULL
|| (flags
& CMS_NOINTERN
))
578 for (j
= 0; j
< sk_CMS_CertificateChoices_num(certs
); j
++) {
579 cch
= sk_CMS_CertificateChoices_value(certs
, j
);
582 x
= cch
->d
.certificate
;
583 if (CMS_SignerInfo_cert_cmp(si
, x
) == 0) {
584 CMS_SignerInfo_set1_signer_cert(si
, x
);
593 void CMS_SignerInfo_get0_algs(CMS_SignerInfo
*si
, EVP_PKEY
**pk
,
594 X509
**signer
, X509_ALGOR
**pdig
,
600 *signer
= si
->signer
;
602 *pdig
= si
->digestAlgorithm
;
604 *psig
= si
->signatureAlgorithm
;
607 ASN1_OCTET_STRING
*CMS_SignerInfo_get0_signature(CMS_SignerInfo
*si
)
609 return si
->signature
;
612 static int cms_SignerInfo_content_sign(CMS_ContentInfo
*cms
,
613 CMS_SignerInfo
*si
, BIO
*chain
)
615 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
617 EVP_PKEY_CTX
*pctx
= NULL
;
618 const CMS_CTX
*ctx
= cms_get0_cmsctx(cms
);
621 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
625 if (si
->pkey
== NULL
) {
626 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_PRIVATE_KEY
);
630 if (!cms_DigestAlgorithm_find_ctx(mctx
, chain
, si
->digestAlgorithm
))
632 /* Set SignerInfo algorithm details if we used custom parameter */
633 if (si
->pctx
&& !cms_sd_asn1_ctrl(si
, 0))
637 * If any signed attributes calculate and add messageDigest attribute
640 if (CMS_signed_get_attr_count(si
) >= 0) {
641 unsigned char md
[EVP_MAX_MD_SIZE
];
644 if (!EVP_DigestFinal_ex(mctx
, md
, &mdlen
))
646 if (!CMS_signed_add1_attr_by_NID(si
, NID_pkcs9_messageDigest
,
647 V_ASN1_OCTET_STRING
, md
, mdlen
))
649 /* Copy content type across */
650 if (!cms_set_si_contentType_attr(cms
, si
))
653 if (!CMS_SignerInfo_sign(si
))
655 } else if (si
->pctx
) {
658 unsigned char md
[EVP_MAX_MD_SIZE
];
662 if (!EVP_DigestFinal_ex(mctx
, md
, &mdlen
))
664 siglen
= EVP_PKEY_size(si
->pkey
);
665 sig
= OPENSSL_malloc(siglen
);
667 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
670 if (EVP_PKEY_sign(pctx
, sig
, &siglen
, md
, mdlen
) <= 0) {
674 ASN1_STRING_set0(si
->signature
, sig
, siglen
);
679 sig
= OPENSSL_malloc(EVP_PKEY_size(si
->pkey
));
681 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
684 if (!EVP_SignFinal_ex(mctx
, sig
, &siglen
, si
->pkey
, ctx
->libctx
,
686 ERR_raise(ERR_LIB_CMS
, CMS_R_SIGNFINAL_ERROR
);
690 ASN1_STRING_set0(si
->signature
, sig
, siglen
);
696 EVP_MD_CTX_free(mctx
);
697 EVP_PKEY_CTX_free(pctx
);
702 int cms_SignedData_final(CMS_ContentInfo
*cms
, BIO
*chain
)
704 STACK_OF(CMS_SignerInfo
) *sinfos
;
708 sinfos
= CMS_get0_SignerInfos(cms
);
709 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++) {
710 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
711 if (!cms_SignerInfo_content_sign(cms
, si
, chain
))
714 cms
->d
.signedData
->encapContentInfo
->partial
= 0;
718 int CMS_SignerInfo_sign(CMS_SignerInfo
*si
)
720 EVP_MD_CTX
*mctx
= si
->mctx
;
721 EVP_PKEY_CTX
*pctx
= NULL
;
722 unsigned char *abuf
= NULL
;
725 const CMS_CTX
*ctx
= si
->cms_ctx
;
726 const char *md_name
= OBJ_nid2sn(OBJ_obj2nid(si
->digestAlgorithm
->algorithm
));
731 if (CMS_signed_get_attr_by_NID(si
, NID_pkcs9_signingTime
, -1) < 0) {
732 if (!cms_add1_signingTime(si
, NULL
))
736 if (!CMS_si_check_attributes(si
))
742 EVP_MD_CTX_reset(mctx
);
743 if (EVP_DigestSignInit_ex(mctx
, &pctx
, md_name
, ctx
->libctx
, ctx
->propq
,
750 * TODO(3.0): This causes problems when providers are in use, so disabled
751 * for now. Can we get rid of this completely? AFAICT this ctrl has been
752 * present since CMS was first put in - but has never been used to do
753 * anything. All internal implementations just return 1 and ignore this ctrl
754 * and have always done so by the looks of things. To fix this we could
755 * convert this ctrl into a param, which would require us to send all the
756 * signer info data as a set of params...but that is non-trivial and since
757 * this isn't used by anything it may be better just to remove it.
760 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
761 EVP_PKEY_CTRL_CMS_SIGN
, 0, si
) <= 0) {
762 ERR_raise(ERR_LIB_CMS
, CMS_R_CTRL_ERROR
);
767 alen
= ASN1_item_i2d((ASN1_VALUE
*)si
->signedAttrs
, &abuf
,
768 ASN1_ITEM_rptr(CMS_Attributes_Sign
));
771 if (EVP_DigestSignUpdate(mctx
, abuf
, alen
) <= 0)
773 if (EVP_DigestSignFinal(mctx
, NULL
, &siglen
) <= 0)
776 abuf
= OPENSSL_malloc(siglen
);
779 if (EVP_DigestSignFinal(mctx
, abuf
, &siglen
) <= 0)
783 * TODO(3.0): This causes problems when providers are in use, so disabled
784 * for now. Can we get rid of this completely? AFAICT this ctrl has been
785 * present since CMS was first put in - but has never been used to do
786 * anything. All internal implementations just return 1 and ignore this ctrl
787 * and have always done so by the looks of things. To fix this we could
788 * convert this ctrl into a param, which would require us to send all the
789 * signer info data as a set of params...but that is non-trivial and since
790 * this isn't used by anything it may be better just to remove it.
793 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
794 EVP_PKEY_CTRL_CMS_SIGN
, 1, si
) <= 0) {
795 ERR_raise(ERR_LIB_CMS
, CMS_R_CTRL_ERROR
);
800 EVP_MD_CTX_reset(mctx
);
802 ASN1_STRING_set0(si
->signature
, abuf
, siglen
);
808 EVP_MD_CTX_reset(mctx
);
812 int CMS_SignerInfo_verify(CMS_SignerInfo
*si
)
814 EVP_MD_CTX
*mctx
= NULL
;
815 unsigned char *abuf
= NULL
;
819 EVP_MD
*fetched_md
= NULL
;
820 const CMS_CTX
*ctx
= si
->cms_ctx
;
822 if (si
->pkey
== NULL
) {
823 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_PUBLIC_KEY
);
827 if (!CMS_si_check_attributes(si
))
830 name
= OBJ_nid2sn(OBJ_obj2nid(si
->digestAlgorithm
->algorithm
));
832 (void)ERR_set_mark();
833 fetched_md
= EVP_MD_fetch(ctx
->libctx
, name
, ctx
->propq
);
835 if (fetched_md
!= NULL
)
838 md
= EVP_get_digestbyobj(si
->digestAlgorithm
->algorithm
);
840 (void)ERR_clear_last_mark();
841 ERR_raise(ERR_LIB_CMS
, CMS_R_UNKNOWN_DIGEST_ALGORITHM
);
844 (void)ERR_pop_to_mark();
846 if (si
->mctx
== NULL
&& (si
->mctx
= EVP_MD_CTX_new()) == NULL
) {
847 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
851 if (EVP_DigestVerifyInit_ex(mctx
, &si
->pctx
, EVP_MD_name(md
), ctx
->libctx
,
852 NULL
, si
->pkey
) <= 0)
855 if (!cms_sd_asn1_ctrl(si
, 1))
858 alen
= ASN1_item_i2d((ASN1_VALUE
*)si
->signedAttrs
, &abuf
,
859 ASN1_ITEM_rptr(CMS_Attributes_Verify
));
860 if (abuf
== NULL
|| alen
< 0)
862 r
= EVP_DigestVerifyUpdate(mctx
, abuf
, alen
);
868 r
= EVP_DigestVerifyFinal(mctx
,
869 si
->signature
->data
, si
->signature
->length
);
871 ERR_raise(ERR_LIB_CMS
, CMS_R_VERIFICATION_FAILURE
);
873 EVP_MD_free(fetched_md
);
874 EVP_MD_CTX_reset(mctx
);
878 /* Create a chain of digest BIOs from a CMS ContentInfo */
880 BIO
*cms_SignedData_init_bio(CMS_ContentInfo
*cms
)
886 sd
= cms_get0_signed(cms
);
889 if (cms
->d
.signedData
->encapContentInfo
->partial
)
890 cms_sd_set_version(sd
);
891 for (i
= 0; i
< sk_X509_ALGOR_num(sd
->digestAlgorithms
); i
++) {
892 X509_ALGOR
*digestAlgorithm
;
895 digestAlgorithm
= sk_X509_ALGOR_value(sd
->digestAlgorithms
, i
);
896 mdbio
= cms_DigestAlgorithm_init_bio(digestAlgorithm
, cms_get0_cmsctx(cms
));
900 BIO_push(chain
, mdbio
);
910 int CMS_SignerInfo_verify_content(CMS_SignerInfo
*si
, BIO
*chain
)
912 ASN1_OCTET_STRING
*os
= NULL
;
913 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
914 EVP_PKEY_CTX
*pkctx
= NULL
;
916 unsigned char mval
[EVP_MAX_MD_SIZE
];
920 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
923 /* If we have any signed attributes look for messageDigest value */
924 if (CMS_signed_get_attr_count(si
) >= 0) {
925 os
= CMS_signed_get0_data_by_OBJ(si
,
926 OBJ_nid2obj(NID_pkcs9_messageDigest
),
927 -3, V_ASN1_OCTET_STRING
);
929 ERR_raise(ERR_LIB_CMS
, CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE
);
934 if (!cms_DigestAlgorithm_find_ctx(mctx
, chain
, si
->digestAlgorithm
))
937 if (EVP_DigestFinal_ex(mctx
, mval
, &mlen
) <= 0) {
938 ERR_raise(ERR_LIB_CMS
, CMS_R_UNABLE_TO_FINALIZE_CONTEXT
);
942 /* If messageDigest found compare it */
945 if (mlen
!= (unsigned int)os
->length
) {
946 ERR_raise(ERR_LIB_CMS
, CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH
);
950 if (memcmp(mval
, os
->data
, mlen
)) {
951 ERR_raise(ERR_LIB_CMS
, CMS_R_VERIFICATION_FAILURE
);
956 const EVP_MD
*md
= EVP_MD_CTX_md(mctx
);
957 const CMS_CTX
*ctx
= si
->cms_ctx
;
959 pkctx
= EVP_PKEY_CTX_new_from_pkey(ctx
->libctx
, si
->pkey
, ctx
->propq
);
962 if (EVP_PKEY_verify_init(pkctx
) <= 0)
964 if (EVP_PKEY_CTX_set_signature_md(pkctx
, md
) <= 0)
967 if (!cms_sd_asn1_ctrl(si
, 1))
969 r
= EVP_PKEY_verify(pkctx
, si
->signature
->data
,
970 si
->signature
->length
, mval
, mlen
);
972 ERR_raise(ERR_LIB_CMS
, CMS_R_VERIFICATION_FAILURE
);
978 EVP_PKEY_CTX_free(pkctx
);
979 EVP_MD_CTX_free(mctx
);
984 int CMS_add_smimecap(CMS_SignerInfo
*si
, STACK_OF(X509_ALGOR
) *algs
)
986 unsigned char *smder
= NULL
;
989 smderlen
= i2d_X509_ALGORS(algs
, &smder
);
992 r
= CMS_signed_add1_attr_by_NID(si
, NID_SMIMECapabilities
,
993 V_ASN1_SEQUENCE
, smder
, smderlen
);
998 int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR
) **algs
,
999 int algnid
, int keysize
)
1002 ASN1_INTEGER
*key
= NULL
;
1005 key
= ASN1_INTEGER_new();
1006 if (key
== NULL
|| !ASN1_INTEGER_set(key
, keysize
)) {
1007 ASN1_INTEGER_free(key
);
1011 alg
= X509_ALGOR_new();
1013 ASN1_INTEGER_free(key
);
1017 X509_ALGOR_set0(alg
, OBJ_nid2obj(algnid
),
1018 key
? V_ASN1_INTEGER
: V_ASN1_UNDEF
, key
);
1020 *algs
= sk_X509_ALGOR_new_null();
1021 if (*algs
== NULL
|| !sk_X509_ALGOR_push(*algs
, alg
)) {
1022 X509_ALGOR_free(alg
);
1028 /* Check to see if a cipher exists and if so add S/MIME capabilities */
1030 static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR
) **sk
, int nid
, int arg
)
1032 if (EVP_get_cipherbynid(nid
))
1033 return CMS_add_simple_smimecap(sk
, nid
, arg
);
1037 static int cms_add_digest_smcap(STACK_OF(X509_ALGOR
) **sk
, int nid
, int arg
)
1039 if (EVP_get_digestbynid(nid
))
1040 return CMS_add_simple_smimecap(sk
, nid
, arg
);
1044 int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR
) **smcap
)
1046 if (!cms_add_cipher_smcap(smcap
, NID_aes_256_cbc
, -1)
1047 || !cms_add_digest_smcap(smcap
, NID_id_GostR3411_2012_256
, -1)
1048 || !cms_add_digest_smcap(smcap
, NID_id_GostR3411_2012_512
, -1)
1049 || !cms_add_digest_smcap(smcap
, NID_id_GostR3411_94
, -1)
1050 || !cms_add_cipher_smcap(smcap
, NID_id_Gost28147_89
, -1)
1051 || !cms_add_cipher_smcap(smcap
, NID_aes_192_cbc
, -1)
1052 || !cms_add_cipher_smcap(smcap
, NID_aes_128_cbc
, -1)
1053 || !cms_add_cipher_smcap(smcap
, NID_des_ede3_cbc
, -1)
1054 || !cms_add_cipher_smcap(smcap
, NID_rc2_cbc
, 128)
1055 || !cms_add_cipher_smcap(smcap
, NID_rc2_cbc
, 64)
1056 || !cms_add_cipher_smcap(smcap
, NID_des_cbc
, -1)
1057 || !cms_add_cipher_smcap(smcap
, NID_rc2_cbc
, 40))