2 * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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>
18 #include "internal/asn1_int.h"
19 #include "internal/evp_int.h"
21 /* CMS SignedData Utilities */
23 static CMS_SignedData *cms_get0_signed(CMS_ContentInfo *cms)
25 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_signed) {
26 CMSerr(CMS_F_CMS_GET0_SIGNED, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA);
29 return cms->d.signedData;
32 static CMS_SignedData *cms_signed_data_init(CMS_ContentInfo *cms)
34 if (cms->d.other == NULL) {
35 cms->d.signedData = M_ASN1_new_of(CMS_SignedData);
36 if (!cms->d.signedData) {
37 CMSerr(CMS_F_CMS_SIGNED_DATA_INIT, ERR_R_MALLOC_FAILURE);
40 cms->d.signedData->version = 1;
41 cms->d.signedData->encapContentInfo->eContentType =
42 OBJ_nid2obj(NID_pkcs7_data);
43 cms->d.signedData->encapContentInfo->partial = 1;
44 ASN1_OBJECT_free(cms->contentType);
45 cms->contentType = OBJ_nid2obj(NID_pkcs7_signed);
46 return cms->d.signedData;
48 return cms_get0_signed(cms);
51 /* Just initialise SignedData e.g. for certs only structure */
53 int CMS_SignedData_init(CMS_ContentInfo *cms)
55 if (cms_signed_data_init(cms))
61 /* Check structures and fixup version numbers (if necessary) */
63 static void cms_sd_set_version(CMS_SignedData *sd)
66 CMS_CertificateChoices *cch;
67 CMS_RevocationInfoChoice *rch;
70 for (i = 0; i < sk_CMS_CertificateChoices_num(sd->certificates); i++) {
71 cch = sk_CMS_CertificateChoices_value(sd->certificates, i);
72 if (cch->type == CMS_CERTCHOICE_OTHER) {
75 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
78 } else if (cch->type == CMS_CERTCHOICE_V1ACERT) {
84 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(sd->crls); i++) {
85 rch = sk_CMS_RevocationInfoChoice_value(sd->crls, i);
86 if (rch->type == CMS_REVCHOICE_OTHER) {
92 if ((OBJ_obj2nid(sd->encapContentInfo->eContentType) != NID_pkcs7_data)
96 for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
97 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
98 if (si->sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
103 } else if (si->version < 1)
112 /* Copy an existing messageDigest value */
114 static int cms_copy_messageDigest(CMS_ContentInfo *cms, CMS_SignerInfo *si)
116 STACK_OF(CMS_SignerInfo) *sinfos;
117 CMS_SignerInfo *sitmp;
119 sinfos = CMS_get0_SignerInfos(cms);
120 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
121 ASN1_OCTET_STRING *messageDigest;
122 sitmp = sk_CMS_SignerInfo_value(sinfos, i);
125 if (CMS_signed_get_attr_count(sitmp) < 0)
127 if (OBJ_cmp(si->digestAlgorithm->algorithm,
128 sitmp->digestAlgorithm->algorithm))
130 messageDigest = CMS_signed_get0_data_by_OBJ(sitmp,
132 (NID_pkcs9_messageDigest),
133 -3, V_ASN1_OCTET_STRING);
134 if (!messageDigest) {
135 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST,
136 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
140 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
147 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST, CMS_R_NO_MATCHING_DIGEST);
151 int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert, int type)
154 case CMS_SIGNERINFO_ISSUER_SERIAL:
155 if (!cms_set1_ias(&sid->d.issuerAndSerialNumber, cert))
159 case CMS_SIGNERINFO_KEYIDENTIFIER:
160 if (!cms_set1_keyid(&sid->d.subjectKeyIdentifier, cert))
165 CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, CMS_R_UNKNOWN_ID);
174 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
175 ASN1_OCTET_STRING **keyid,
179 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL) {
181 *issuer = sid->d.issuerAndSerialNumber->issuer;
183 *sno = sid->d.issuerAndSerialNumber->serialNumber;
184 } else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
186 *keyid = sid->d.subjectKeyIdentifier;
192 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert)
194 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
195 return cms_ias_cert_cmp(sid->d.issuerAndSerialNumber, cert);
196 else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
197 return cms_keyid_cert_cmp(sid->d.subjectKeyIdentifier, cert);
202 static int cms_sd_asn1_ctrl(CMS_SignerInfo *si, int cmd)
204 EVP_PKEY *pkey = si->pkey;
206 if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
208 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_SIGN, cmd, si);
210 CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
214 CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_CTRL_FAILURE);
220 CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
221 X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
225 CMS_SignerInfo *si = NULL;
228 if (!X509_check_private_key(signer, pk)) {
229 CMSerr(CMS_F_CMS_ADD1_SIGNER,
230 CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
233 sd = cms_signed_data_init(cms);
236 si = M_ASN1_new_of(CMS_SignerInfo);
239 /* Call for side-effect of computing hash and caching extensions */
240 X509_check_purpose(signer, -1, -1);
247 si->mctx = EVP_MD_CTX_new();
250 if (si->mctx == NULL) {
251 CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
255 if (flags & CMS_USE_KEYID) {
259 type = CMS_SIGNERINFO_KEYIDENTIFIER;
261 type = CMS_SIGNERINFO_ISSUER_SERIAL;
265 if (!cms_set1_SignerIdentifier(si->sid, signer, type))
270 if (EVP_PKEY_get_default_digest_nid(pk, &def_nid) <= 0)
272 md = EVP_get_digestbynid(def_nid);
274 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DEFAULT_DIGEST);
280 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DIGEST_SET);
284 X509_ALGOR_set_md(si->digestAlgorithm, md);
286 /* See if digest is present in digestAlgorithms */
287 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
288 const ASN1_OBJECT *aoid;
289 alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
290 X509_ALGOR_get0(&aoid, NULL, NULL, alg);
291 if (OBJ_obj2nid(aoid) == EVP_MD_type(md))
295 if (i == sk_X509_ALGOR_num(sd->digestAlgorithms)) {
296 alg = X509_ALGOR_new();
299 X509_ALGOR_set_md(alg, md);
300 if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg)) {
301 X509_ALGOR_free(alg);
306 if (!(flags & CMS_KEY_PARAM) && !cms_sd_asn1_ctrl(si, 0))
308 if (!(flags & CMS_NOATTR)) {
310 * Initialize signed attributes structure so other attributes
311 * such as signing time etc are added later even if we add none here.
313 if (!si->signedAttrs) {
314 si->signedAttrs = sk_X509_ATTRIBUTE_new_null();
315 if (!si->signedAttrs)
319 if (!(flags & CMS_NOSMIMECAP)) {
320 STACK_OF(X509_ALGOR) *smcap = NULL;
321 i = CMS_add_standard_smimecap(&smcap);
323 i = CMS_add_smimecap(si, smcap);
324 sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
328 if (flags & CMS_REUSE_DIGEST) {
329 if (!cms_copy_messageDigest(cms, si))
331 if (!(flags & (CMS_PARTIAL | CMS_KEY_PARAM)) &&
332 !CMS_SignerInfo_sign(si))
337 if (!(flags & CMS_NOCERTS)) {
338 /* NB ignore -1 return for duplicate cert */
339 if (!CMS_add1_cert(cms, signer))
343 if (flags & CMS_KEY_PARAM) {
344 if (flags & CMS_NOATTR) {
345 si->pctx = EVP_PKEY_CTX_new(si->pkey, NULL);
346 if (si->pctx == NULL)
348 if (EVP_PKEY_sign_init(si->pctx) <= 0)
350 if (EVP_PKEY_CTX_set_signature_md(si->pctx, md) <= 0)
352 } else if (EVP_DigestSignInit(si->mctx, &si->pctx, md, NULL, pk) <=
357 if (!sd->signerInfos)
358 sd->signerInfos = sk_CMS_SignerInfo_new_null();
359 if (!sd->signerInfos || !sk_CMS_SignerInfo_push(sd->signerInfos, si))
365 CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
367 M_ASN1_free_of(si, CMS_SignerInfo);
372 static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
379 tt = X509_gmtime_adj(NULL, 0);
384 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
385 tt->type, tt, -1) <= 0)
396 CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME, ERR_R_MALLOC_FAILURE);
402 EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si)
407 EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si)
412 STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
415 sd = cms_get0_signed(cms);
418 return sd->signerInfos;
421 STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
423 STACK_OF(X509) *signers = NULL;
424 STACK_OF(CMS_SignerInfo) *sinfos;
427 sinfos = CMS_get0_SignerInfos(cms);
428 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
429 si = sk_CMS_SignerInfo_value(sinfos, i);
432 signers = sk_X509_new_null();
436 if (!sk_X509_push(signers, si->signer)) {
437 sk_X509_free(signers);
445 void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
449 EVP_PKEY_free(si->pkey);
450 si->pkey = X509_get_pubkey(signer);
452 X509_free(si->signer);
456 int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
457 ASN1_OCTET_STRING **keyid,
458 X509_NAME **issuer, ASN1_INTEGER **sno)
460 return cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
463 int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
465 return cms_SignerIdentifier_cert_cmp(si->sid, cert);
468 int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,
473 CMS_CertificateChoices *cch;
474 STACK_OF(CMS_CertificateChoices) *certs;
478 sd = cms_get0_signed(cms);
481 certs = sd->certificates;
482 for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
483 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
487 for (j = 0; j < sk_X509_num(scerts); j++) {
488 x = sk_X509_value(scerts, j);
489 if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
490 CMS_SignerInfo_set1_signer_cert(si, x);
496 if (si->signer || (flags & CMS_NOINTERN))
499 for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++) {
500 cch = sk_CMS_CertificateChoices_value(certs, j);
503 x = cch->d.certificate;
504 if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
505 CMS_SignerInfo_set1_signer_cert(si, x);
514 void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk,
515 X509 **signer, X509_ALGOR **pdig,
521 *signer = si->signer;
523 *pdig = si->digestAlgorithm;
525 *psig = si->signatureAlgorithm;
528 ASN1_OCTET_STRING *CMS_SignerInfo_get0_signature(CMS_SignerInfo *si)
530 return si->signature;
533 static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
534 CMS_SignerInfo *si, BIO *chain)
536 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
538 EVP_PKEY_CTX *pctx = NULL;
541 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
546 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);
550 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
552 /* Set SignerInfo algorithm details if we used custom parameter */
553 if (si->pctx && !cms_sd_asn1_ctrl(si, 0))
557 * If any signed attributes calculate and add messageDigest attribute
560 if (CMS_signed_get_attr_count(si) >= 0) {
562 cms->d.signedData->encapContentInfo->eContentType;
563 unsigned char md[EVP_MAX_MD_SIZE];
565 if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
567 if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
568 V_ASN1_OCTET_STRING, md, mdlen))
570 /* Copy content type across */
571 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,
572 V_ASN1_OBJECT, ctype, -1) <= 0)
574 if (!CMS_SignerInfo_sign(si))
576 } else if (si->pctx) {
579 unsigned char md[EVP_MAX_MD_SIZE];
582 if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
584 siglen = EVP_PKEY_size(si->pkey);
585 sig = OPENSSL_malloc(siglen);
587 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
590 if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) {
594 ASN1_STRING_set0(si->signature, sig, siglen);
598 sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey));
600 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
603 if (!EVP_SignFinal(mctx, sig, &siglen, si->pkey)) {
604 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_SIGNFINAL_ERROR);
608 ASN1_STRING_set0(si->signature, sig, siglen);
614 EVP_MD_CTX_free(mctx);
615 EVP_PKEY_CTX_free(pctx);
620 int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain)
622 STACK_OF(CMS_SignerInfo) *sinfos;
625 sinfos = CMS_get0_SignerInfos(cms);
626 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
627 si = sk_CMS_SignerInfo_value(sinfos, i);
628 if (!cms_SignerInfo_content_sign(cms, si, chain))
631 cms->d.signedData->encapContentInfo->partial = 0;
635 int CMS_SignerInfo_sign(CMS_SignerInfo *si)
637 EVP_MD_CTX *mctx = si->mctx;
639 unsigned char *abuf = NULL;
642 const EVP_MD *md = NULL;
644 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
648 if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0) {
649 if (!cms_add1_signingTime(si, NULL))
656 EVP_MD_CTX_reset(mctx);
657 if (EVP_DigestSignInit(mctx, &pctx, md, NULL, si->pkey) <= 0)
662 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
663 EVP_PKEY_CTRL_CMS_SIGN, 0, si) <= 0) {
664 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
668 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
669 ASN1_ITEM_rptr(CMS_Attributes_Sign));
672 if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
674 if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
677 abuf = OPENSSL_malloc(siglen);
680 if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
683 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
684 EVP_PKEY_CTRL_CMS_SIGN, 1, si) <= 0) {
685 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
689 EVP_MD_CTX_reset(mctx);
691 ASN1_STRING_set0(si->signature, abuf, siglen);
697 EVP_MD_CTX_reset(mctx);
702 int CMS_SignerInfo_verify(CMS_SignerInfo *si)
704 EVP_MD_CTX *mctx = NULL;
705 unsigned char *abuf = NULL;
707 const EVP_MD *md = NULL;
710 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_NO_PUBLIC_KEY);
714 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
717 if (si->mctx == NULL)
718 si->mctx = EVP_MD_CTX_new();
720 if (EVP_DigestVerifyInit(mctx, &si->pctx, md, NULL, si->pkey) <= 0)
723 if (!cms_sd_asn1_ctrl(si, 1))
726 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
727 ASN1_ITEM_rptr(CMS_Attributes_Verify));
730 r = EVP_DigestVerifyUpdate(mctx, abuf, alen);
736 r = EVP_DigestVerifyFinal(mctx,
737 si->signature->data, si->signature->length);
739 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE);
741 EVP_MD_CTX_reset(mctx);
745 /* Create a chain of digest BIOs from a CMS ContentInfo */
747 BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms)
752 sd = cms_get0_signed(cms);
755 if (cms->d.signedData->encapContentInfo->partial)
756 cms_sd_set_version(sd);
757 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
758 X509_ALGOR *digestAlgorithm;
760 digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
761 mdbio = cms_DigestAlgorithm_init_bio(digestAlgorithm);
765 BIO_push(chain, mdbio);
775 int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
777 ASN1_OCTET_STRING *os = NULL;
778 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
779 EVP_PKEY_CTX *pkctx = NULL;
781 unsigned char mval[EVP_MAX_MD_SIZE];
785 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, ERR_R_MALLOC_FAILURE);
788 /* If we have any signed attributes look for messageDigest value */
789 if (CMS_signed_get_attr_count(si) >= 0) {
790 os = CMS_signed_get0_data_by_OBJ(si,
791 OBJ_nid2obj(NID_pkcs9_messageDigest),
792 -3, V_ASN1_OCTET_STRING);
794 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
795 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
800 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
803 if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {
804 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
805 CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
809 /* If messageDigest found compare it */
812 if (mlen != (unsigned int)os->length) {
813 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
814 CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
818 if (memcmp(mval, os->data, mlen)) {
819 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
820 CMS_R_VERIFICATION_FAILURE);
825 const EVP_MD *md = EVP_MD_CTX_md(mctx);
826 pkctx = EVP_PKEY_CTX_new(si->pkey, NULL);
829 if (EVP_PKEY_verify_init(pkctx) <= 0)
831 if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)
834 if (!cms_sd_asn1_ctrl(si, 1))
836 r = EVP_PKEY_verify(pkctx, si->signature->data,
837 si->signature->length, mval, mlen);
839 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
840 CMS_R_VERIFICATION_FAILURE);
846 EVP_PKEY_CTX_free(pkctx);
847 EVP_MD_CTX_free(mctx);
852 int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
854 unsigned char *smder = NULL;
856 smderlen = i2d_X509_ALGORS(algs, &smder);
859 r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
860 V_ASN1_SEQUENCE, smder, smderlen);
865 int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
866 int algnid, int keysize)
869 ASN1_INTEGER *key = NULL;
871 key = ASN1_INTEGER_new();
872 if (key == NULL || !ASN1_INTEGER_set(key, keysize))
875 alg = X509_ALGOR_new();
877 ASN1_INTEGER_free(key);
881 X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),
882 key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
884 *algs = sk_X509_ALGOR_new_null();
885 if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) {
886 X509_ALGOR_free(alg);
892 /* Check to see if a cipher exists and if so add S/MIME capabilities */
894 static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
896 if (EVP_get_cipherbynid(nid))
897 return CMS_add_simple_smimecap(sk, nid, arg);
901 static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
903 if (EVP_get_digestbynid(nid))
904 return CMS_add_simple_smimecap(sk, nid, arg);
908 int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
910 if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
911 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
912 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
913 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
914 || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
915 || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
916 || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
917 || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
918 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
919 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
920 || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
921 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))