1 /* crypto/cms/cms_sd.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
55 #include <openssl/asn1t.h>
56 #include <openssl/pem.h>
57 #include <openssl/x509v3.h>
58 #include <openssl/err.h>
59 #include <openssl/cms.h>
61 #include "asn1_locl.h"
63 /* CMS SignedData Utilities */
65 DECLARE_ASN1_ITEM(CMS_SignedData)
67 static CMS_SignedData *cms_get0_signed(CMS_ContentInfo *cms)
69 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)
81 cms->d.signedData = M_ASN1_new_of(CMS_SignedData);
82 if (!cms->d.signedData)
84 CMSerr(CMS_F_CMS_SIGNED_DATA_INIT, ERR_R_MALLOC_FAILURE);
87 cms->d.signedData->version = 1;
88 cms->d.signedData->encapContentInfo->eContentType =
89 OBJ_nid2obj(NID_pkcs7_data);
90 cms->d.signedData->encapContentInfo->partial = 1;
91 ASN1_OBJECT_free(cms->contentType);
92 cms->contentType = OBJ_nid2obj(NID_pkcs7_signed);
93 return cms->d.signedData;
95 return cms_get0_signed(cms);
98 /* Just initialize SignedData e.g. for certs only structure */
100 int CMS_SignedData_init(CMS_ContentInfo *cms)
102 if (cms_signed_data_init(cms))
108 /* Check structures and fixup version numbers (if necessary) */
110 static void cms_sd_set_version(CMS_SignedData *sd)
113 CMS_CertificateChoices *cch;
114 CMS_RevocationInfoChoice *rch;
117 for (i = 0; i < sk_CMS_CertificateChoices_num(sd->certificates); i++)
119 cch = sk_CMS_CertificateChoices_value(sd->certificates, i);
120 if (cch->type == CMS_CERTCHOICE_OTHER)
125 else if (cch->type == CMS_CERTCHOICE_V2ACERT)
130 else if (cch->type == CMS_CERTCHOICE_V1ACERT)
137 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(sd->crls); i++)
139 rch = sk_CMS_RevocationInfoChoice_value(sd->crls, i);
140 if (rch->type == CMS_REVCHOICE_OTHER)
147 if ((OBJ_obj2nid(sd->encapContentInfo->eContentType) != NID_pkcs7_data)
148 && (sd->version < 3))
151 for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++)
153 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
154 if (si->sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
170 /* Copy an existing messageDigest value */
172 static int cms_copy_messageDigest(CMS_ContentInfo *cms, CMS_SignerInfo *si)
174 STACK_OF(CMS_SignerInfo) *sinfos;
175 CMS_SignerInfo *sitmp;
177 sinfos = CMS_get0_SignerInfos(cms);
178 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
180 ASN1_OCTET_STRING *messageDigest;
181 sitmp = sk_CMS_SignerInfo_value(sinfos, i);
184 if (CMS_signed_get_attr_count(sitmp) < 0)
186 if (OBJ_cmp(si->digestAlgorithm->algorithm,
187 sitmp->digestAlgorithm->algorithm))
189 messageDigest = CMS_signed_get0_data_by_OBJ(sitmp,
190 OBJ_nid2obj(NID_pkcs9_messageDigest),
191 -3, V_ASN1_OCTET_STRING);
194 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST,
195 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
199 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
206 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST, CMS_R_NO_MATCHING_DIGEST);
210 int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert, int type)
214 case CMS_SIGNERINFO_ISSUER_SERIAL:
215 sid->d.issuerAndSerialNumber =
216 M_ASN1_new_of(CMS_IssuerAndSerialNumber);
217 if (!sid->d.issuerAndSerialNumber)
219 if (!X509_NAME_set(&sid->d.issuerAndSerialNumber->issuer,
220 X509_get_issuer_name(cert)))
222 if (!ASN1_STRING_copy(
223 sid->d.issuerAndSerialNumber->serialNumber,
224 X509_get_serialNumber(cert)))
228 case CMS_SIGNERINFO_KEYIDENTIFIER:
229 sid->d.subjectKeyIdentifier = ASN1_STRING_dup(cert->skid);
230 if (!sid->d.subjectKeyIdentifier)
235 CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, CMS_R_UNKNOWN_ID);
244 CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, ERR_R_MALLOC_FAILURE);
249 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
250 ASN1_OCTET_STRING **keyid,
251 X509_NAME **issuer, ASN1_INTEGER **sno)
253 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
256 *issuer = sid->d.issuerAndSerialNumber->issuer;
258 *sno = sid->d.issuerAndSerialNumber->serialNumber;
260 else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
263 *keyid = sid->d.subjectKeyIdentifier;
270 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert)
273 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
275 ret = X509_NAME_cmp(sid->d.issuerAndSerialNumber->issuer,
276 X509_get_issuer_name(cert));
279 return ASN1_INTEGER_cmp(sid->d.issuerAndSerialNumber->serialNumber,
280 X509_get_serialNumber(cert));
282 else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
284 X509_check_purpose(cert, -1, -1);
287 return ASN1_OCTET_STRING_cmp(sid->d.subjectKeyIdentifier,
294 CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
295 X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
299 CMS_SignerInfo *si = NULL;
302 if(!X509_check_private_key(signer, pk))
304 CMSerr(CMS_F_CMS_ADD1_SIGNER,
305 CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
308 sd = cms_signed_data_init(cms);
311 si = M_ASN1_new_of(CMS_SignerInfo);
314 X509_check_purpose(signer, -1, -1);
316 CRYPTO_add(&pk->references, 1, CRYPTO_LOCK_EVP_PKEY);
317 CRYPTO_add(&signer->references, 1, CRYPTO_LOCK_X509);
322 if (flags & CMS_USE_KEYID)
327 type = CMS_SIGNERINFO_KEYIDENTIFIER;
331 type = CMS_SIGNERINFO_ISSUER_SERIAL;
335 if (!cms_set1_SignerIdentifier(si->sid, signer, type))
341 if (EVP_PKEY_get_default_digest_nid(pk, &def_nid) <= 0)
343 md = EVP_get_digestbynid(def_nid);
346 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DEFAULT_DIGEST);
353 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DIGEST_SET);
357 cms_DigestAlgorithm_set(si->digestAlgorithm, md);
359 /* See if digest is present in digestAlgorithms */
360 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++)
363 alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
364 X509_ALGOR_get0(&aoid, NULL, NULL, alg);
365 if (OBJ_obj2nid(aoid) == EVP_MD_type(md))
369 if (i == sk_X509_ALGOR_num(sd->digestAlgorithms))
371 alg = X509_ALGOR_new();
374 cms_DigestAlgorithm_set(alg, md);
375 if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg))
377 X509_ALGOR_free(alg);
382 if (pk->ameth && pk->ameth->pkey_ctrl)
384 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_SIGN,
388 CMSerr(CMS_F_CMS_ADD1_SIGNER,
389 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
394 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_CTRL_FAILURE);
399 if (!(flags & CMS_NOATTR))
401 /* Copy content type across */
403 OBJ_dup(sd->encapContentInfo->eContentType);
406 i = CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,
407 V_ASN1_OBJECT, ctype, -1);
408 ASN1_OBJECT_free(ctype);
411 if (!(flags & CMS_NOSMIMECAP))
413 STACK_OF(X509_ALGOR) *smcap = NULL;
414 i = CMS_add_standard_smimecap(&smcap);
416 i = CMS_add_smimecap(si, smcap);
417 sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
421 if (flags & CMS_REUSE_DIGEST)
423 if (!cms_copy_messageDigest(cms, si))
425 if (!(flags & CMS_PARTIAL) &&
426 !CMS_SignerInfo_sign(si))
431 if (!(flags & CMS_NOCERTS))
433 /* NB ignore -1 return for duplicate cert */
434 if (!CMS_add1_cert(cms, signer))
438 if (!sd->signerInfos)
439 sd->signerInfos = sk_CMS_SignerInfo_new_null();
440 if (!sd->signerInfos ||
441 !sk_CMS_SignerInfo_push(sd->signerInfos, si))
447 CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
450 M_ASN1_free_of(si, CMS_SignerInfo);
455 static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
462 tt = X509_gmtime_adj(NULL, 0);
467 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
468 tt->type, tt, -1) <= 0)
479 CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME, ERR_R_MALLOC_FAILURE);
485 STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
488 sd = cms_get0_signed(cms);
491 return sd->signerInfos;
494 STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
496 STACK_OF(X509) *signers = NULL;
497 STACK_OF(CMS_SignerInfo) *sinfos;
500 sinfos = CMS_get0_SignerInfos(cms);
501 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
503 si = sk_CMS_SignerInfo_value(sinfos, i);
508 signers = sk_X509_new_null();
512 if (!sk_X509_push(signers, si->signer))
514 sk_X509_free(signers);
522 void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
526 CRYPTO_add(&signer->references, 1, CRYPTO_LOCK_X509);
528 EVP_PKEY_free(si->pkey);
529 si->pkey = X509_get_pubkey(signer);
532 X509_free(si->signer);
536 int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
537 ASN1_OCTET_STRING **keyid,
538 X509_NAME **issuer, ASN1_INTEGER **sno)
540 return cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
543 int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
545 return cms_SignerIdentifier_cert_cmp(si->sid, cert);
548 int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,
553 CMS_CertificateChoices *cch;
554 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++)
564 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
568 for (j = 0; j < sk_X509_num(scerts); j++)
570 x = sk_X509_value(scerts, j);
571 if (CMS_SignerInfo_cert_cmp(si, x) == 0)
573 CMS_SignerInfo_set1_signer_cert(si, x);
579 if (si->signer || (flags & CMS_NOINTERN))
582 for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++)
584 cch = sk_CMS_CertificateChoices_value(certs, j);
587 x = cch->d.certificate;
588 if (CMS_SignerInfo_cert_cmp(si, x) == 0)
590 CMS_SignerInfo_set1_signer_cert(si, x);
599 void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk, X509 **signer,
600 X509_ALGOR **pdig, X509_ALGOR **psig)
605 *signer = si->signer;
607 *pdig = si->digestAlgorithm;
609 *psig = si->signatureAlgorithm;
612 static int cms_SignerInfo_content_sign(CMS_SignerInfo *si, BIO *chain)
616 EVP_MD_CTX_init(&mctx);
621 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);
625 if (!cms_DigestAlgorithm_find_ctx(&mctx, chain, si->digestAlgorithm))
628 /* If any signed attributes calculate and add messageDigest attribute */
630 if (CMS_signed_get_attr_count(si) >= 0)
632 unsigned char md[EVP_MAX_MD_SIZE];
634 EVP_DigestFinal_ex(&mctx, md, &mdlen);
635 if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
639 if (!CMS_SignerInfo_sign(si))
646 sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey));
649 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN,
650 ERR_R_MALLOC_FAILURE);
653 if (!EVP_SignFinal(&mctx, sig, &siglen, si->pkey))
655 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN,
656 CMS_R_SIGNFINAL_ERROR);
660 ASN1_STRING_set0(si->signature, sig, siglen);
666 EVP_MD_CTX_cleanup(&mctx);
671 int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain)
673 STACK_OF(CMS_SignerInfo) *sinfos;
676 sinfos = CMS_get0_SignerInfos(cms);
677 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
679 si = sk_CMS_SignerInfo_value(sinfos, i);
680 if (!cms_SignerInfo_content_sign(si, chain))
683 cms->d.signedData->encapContentInfo->partial = 0;
687 int CMS_SignerInfo_sign(CMS_SignerInfo *si)
691 unsigned char *abuf = NULL;
694 const EVP_MD *md = NULL;
696 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
700 EVP_MD_CTX_init(&mctx);
702 if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0)
704 if (!cms_add1_signingTime(si, NULL))
708 if (EVP_DigestSignInit(&mctx, &pctx, md, NULL, si->pkey) <= 0)
711 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
712 EVP_PKEY_CTRL_CMS_SIGN, 0, si) <= 0)
714 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
718 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs,&abuf,
719 ASN1_ITEM_rptr(CMS_Attributes_Sign));
722 if (EVP_DigestSignUpdate(&mctx, abuf, alen) <= 0)
724 if (EVP_DigestSignFinal(&mctx, NULL, &siglen) <= 0)
727 abuf = OPENSSL_malloc(siglen);
730 if (EVP_DigestSignFinal(&mctx, abuf, &siglen) <= 0)
733 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
734 EVP_PKEY_CTRL_CMS_SIGN, 1, si) <= 0)
736 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
740 EVP_MD_CTX_cleanup(&mctx);
742 ASN1_STRING_set0(si->signature, abuf, siglen);
749 EVP_MD_CTX_cleanup(&mctx);
754 int CMS_SignerInfo_verify(CMS_SignerInfo *si)
758 unsigned char *abuf = NULL;
760 const EVP_MD *md = NULL;
764 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_NO_PUBLIC_KEY);
768 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
771 EVP_MD_CTX_init(&mctx);
772 if (EVP_DigestVerifyInit(&mctx, &pctx, md, NULL, si->pkey) <= 0)
775 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs,&abuf,
776 ASN1_ITEM_rptr(CMS_Attributes_Verify));
779 r = EVP_DigestVerifyUpdate(&mctx, abuf, alen);
786 r = EVP_DigestVerifyFinal(&mctx,
787 si->signature->data, si->signature->length);
789 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE);
791 EVP_MD_CTX_cleanup(&mctx);
795 /* Create a chain of digest BIOs from a CMS ContentInfo */
797 BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms)
802 sd = cms_get0_signed(cms);
805 if (cms->d.signedData->encapContentInfo->partial)
806 cms_sd_set_version(sd);
807 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++)
809 X509_ALGOR *digestAlgorithm;
811 digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
812 mdbio = cms_DigestAlgorithm_init_bio(digestAlgorithm);
816 BIO_push(chain, mdbio);
827 int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
829 ASN1_OCTET_STRING *os = NULL;
832 EVP_MD_CTX_init(&mctx);
833 /* If we have any signed attributes look for messageDigest value */
834 if (CMS_signed_get_attr_count(si) >= 0)
836 os = CMS_signed_get0_data_by_OBJ(si,
837 OBJ_nid2obj(NID_pkcs9_messageDigest),
838 -3, V_ASN1_OCTET_STRING);
841 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
842 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
847 if (!cms_DigestAlgorithm_find_ctx(&mctx, chain, si->digestAlgorithm))
850 /* If messageDigest found compare it */
854 unsigned char mval[EVP_MAX_MD_SIZE];
856 if (EVP_DigestFinal_ex(&mctx, mval, &mlen) <= 0)
858 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
859 CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
862 if (mlen != (unsigned int)os->length)
864 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
865 CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
869 if (memcmp(mval, os->data, mlen))
871 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
872 CMS_R_VERIFICATION_FAILURE);
880 r = EVP_VerifyFinal(&mctx, si->signature->data,
881 si->signature->length, si->pkey);
884 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
885 CMS_R_VERIFICATION_FAILURE);
891 EVP_MD_CTX_cleanup(&mctx);
896 int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
898 unsigned char *smder = NULL;
900 smderlen = i2d_X509_ALGORS(algs, &smder);
903 r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
904 V_ASN1_SEQUENCE, smder, smderlen);
909 int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
910 int algnid, int keysize)
913 ASN1_INTEGER *key = NULL;
916 key = ASN1_INTEGER_new();
917 if (!key || !ASN1_INTEGER_set(key, keysize))
920 alg = X509_ALGOR_new();
924 ASN1_INTEGER_free(key);
928 X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),
929 key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
931 *algs = sk_X509_ALGOR_new_null();
932 if (!*algs || !sk_X509_ALGOR_push(*algs, alg))
934 X509_ALGOR_free(alg);
940 /* Check to see if a cipher exists and if so add S/MIME capabilities */
942 static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
944 if (EVP_get_cipherbynid(nid))
945 return CMS_add_simple_smimecap(sk, nid, arg);
949 static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
951 if (EVP_get_digestbynid(nid))
952 return CMS_add_simple_smimecap(sk, nid, arg);
956 int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
958 if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
959 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
960 || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
961 || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
962 || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
963 || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
964 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
965 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
966 || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
967 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))