cms_sd.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. /*
  2. * Copyright 2008-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #include "internal/cryptlib.h"
  10. #include <openssl/asn1t.h>
  11. #include <openssl/pem.h>
  12. #include <openssl/x509.h>
  13. #include <openssl/x509v3.h>
  14. #include <openssl/err.h>
  15. #include <openssl/cms.h>
  16. #include <openssl/ess.h>
  17. #include "internal/sizes.h"
  18. #include "crypto/asn1.h"
  19. #include "crypto/evp.h"
  20. #include "crypto/ess.h"
  21. #include "crypto/x509.h" /* for ossl_x509_add_cert_new() */
  22. #include "cms_local.h"
  23. /* CMS SignedData Utilities */
  24. static CMS_SignedData *cms_get0_signed(CMS_ContentInfo *cms)
  25. {
  26. if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_signed) {
  27. ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA);
  28. return NULL;
  29. }
  30. return cms->d.signedData;
  31. }
  32. static CMS_SignedData *cms_signed_data_init(CMS_ContentInfo *cms)
  33. {
  34. if (cms->d.other == NULL) {
  35. cms->d.signedData = M_ASN1_new_of(CMS_SignedData);
  36. if (!cms->d.signedData) {
  37. ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
  38. return NULL;
  39. }
  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;
  47. }
  48. return cms_get0_signed(cms);
  49. }
  50. /* Just initialise SignedData e.g. for certs only structure */
  51. int CMS_SignedData_init(CMS_ContentInfo *cms)
  52. {
  53. if (cms_signed_data_init(cms))
  54. return 1;
  55. else
  56. return 0;
  57. }
  58. /* Check structures and fixup version numbers (if necessary) */
  59. static void cms_sd_set_version(CMS_SignedData *sd)
  60. {
  61. int i;
  62. CMS_CertificateChoices *cch;
  63. CMS_RevocationInfoChoice *rch;
  64. CMS_SignerInfo *si;
  65. for (i = 0; i < sk_CMS_CertificateChoices_num(sd->certificates); i++) {
  66. cch = sk_CMS_CertificateChoices_value(sd->certificates, i);
  67. if (cch->type == CMS_CERTCHOICE_OTHER) {
  68. if (sd->version < 5)
  69. sd->version = 5;
  70. } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
  71. if (sd->version < 4)
  72. sd->version = 4;
  73. } else if (cch->type == CMS_CERTCHOICE_V1ACERT) {
  74. if (sd->version < 3)
  75. sd->version = 3;
  76. }
  77. }
  78. for (i = 0; i < sk_CMS_RevocationInfoChoice_num(sd->crls); i++) {
  79. rch = sk_CMS_RevocationInfoChoice_value(sd->crls, i);
  80. if (rch->type == CMS_REVCHOICE_OTHER) {
  81. if (sd->version < 5)
  82. sd->version = 5;
  83. }
  84. }
  85. if ((OBJ_obj2nid(sd->encapContentInfo->eContentType) != NID_pkcs7_data)
  86. && (sd->version < 3))
  87. sd->version = 3;
  88. for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
  89. si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
  90. if (si->sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
  91. if (si->version < 3)
  92. si->version = 3;
  93. if (sd->version < 3)
  94. sd->version = 3;
  95. } else if (si->version < 1) {
  96. si->version = 1;
  97. }
  98. }
  99. if (sd->version < 1)
  100. sd->version = 1;
  101. }
  102. /*
  103. * RFC 5652 Section 11.1 Content Type
  104. * The content-type attribute within signed-data MUST
  105. * 1) be present if there are signed attributes
  106. * 2) match the content type in the signed-data,
  107. * 3) be a signed attribute.
  108. * 4) not have more than one copy of the attribute.
  109. *
  110. * Note that since the CMS_SignerInfo_sign() always adds the "signing time"
  111. * attribute, the content type attribute MUST be added also.
  112. * Assumptions: This assumes that the attribute does not already exist.
  113. */
  114. static int cms_set_si_contentType_attr(CMS_ContentInfo *cms, CMS_SignerInfo *si)
  115. {
  116. ASN1_OBJECT *ctype = cms->d.signedData->encapContentInfo->eContentType;
  117. /* Add the contentType attribute */
  118. return CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,
  119. V_ASN1_OBJECT, ctype, -1) > 0;
  120. }
  121. /* Copy an existing messageDigest value */
  122. static int cms_copy_messageDigest(CMS_ContentInfo *cms, CMS_SignerInfo *si)
  123. {
  124. STACK_OF(CMS_SignerInfo) *sinfos;
  125. CMS_SignerInfo *sitmp;
  126. int i;
  127. sinfos = CMS_get0_SignerInfos(cms);
  128. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  129. ASN1_OCTET_STRING *messageDigest;
  130. sitmp = sk_CMS_SignerInfo_value(sinfos, i);
  131. if (sitmp == si)
  132. continue;
  133. if (CMS_signed_get_attr_count(sitmp) < 0)
  134. continue;
  135. if (OBJ_cmp(si->digestAlgorithm->algorithm,
  136. sitmp->digestAlgorithm->algorithm))
  137. continue;
  138. messageDigest = CMS_signed_get0_data_by_OBJ(sitmp,
  139. OBJ_nid2obj
  140. (NID_pkcs9_messageDigest),
  141. -3, V_ASN1_OCTET_STRING);
  142. if (!messageDigest) {
  143. ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
  144. return 0;
  145. }
  146. if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
  147. V_ASN1_OCTET_STRING,
  148. messageDigest, -1))
  149. return 1;
  150. else
  151. return 0;
  152. }
  153. ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_DIGEST);
  154. return 0;
  155. }
  156. int ossl_cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert,
  157. int type, const CMS_CTX *ctx)
  158. {
  159. switch (type) {
  160. case CMS_SIGNERINFO_ISSUER_SERIAL:
  161. if (!ossl_cms_set1_ias(&sid->d.issuerAndSerialNumber, cert))
  162. return 0;
  163. break;
  164. case CMS_SIGNERINFO_KEYIDENTIFIER:
  165. if (!ossl_cms_set1_keyid(&sid->d.subjectKeyIdentifier, cert))
  166. return 0;
  167. break;
  168. default:
  169. ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_ID);
  170. return 0;
  171. }
  172. sid->type = type;
  173. return 1;
  174. }
  175. int ossl_cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
  176. ASN1_OCTET_STRING **keyid,
  177. X509_NAME **issuer,
  178. ASN1_INTEGER **sno)
  179. {
  180. if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL) {
  181. if (issuer)
  182. *issuer = sid->d.issuerAndSerialNumber->issuer;
  183. if (sno)
  184. *sno = sid->d.issuerAndSerialNumber->serialNumber;
  185. } else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
  186. if (keyid)
  187. *keyid = sid->d.subjectKeyIdentifier;
  188. } else {
  189. return 0;
  190. }
  191. return 1;
  192. }
  193. int ossl_cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert)
  194. {
  195. if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
  196. return ossl_cms_ias_cert_cmp(sid->d.issuerAndSerialNumber, cert);
  197. else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
  198. return ossl_cms_keyid_cert_cmp(sid->d.subjectKeyIdentifier, cert);
  199. else
  200. return -1;
  201. }
  202. /* Method to map any, incl. provider-implemented PKEY types to OIDs */
  203. /* (EC)DSA and all provider-delivered signatures implementation is the same */
  204. static int cms_generic_sign(CMS_SignerInfo *si, int verify)
  205. {
  206. if (!ossl_assert(verify == 0 || verify == 1))
  207. return -1;
  208. if (!verify) {
  209. EVP_PKEY *pkey = si->pkey;
  210. int snid, hnid, pknid = EVP_PKEY_get_id(pkey);
  211. X509_ALGOR *alg1, *alg2;
  212. CMS_SignerInfo_get0_algs(si, NULL, NULL, &alg1, &alg2);
  213. if (alg1 == NULL || alg1->algorithm == NULL)
  214. return -1;
  215. hnid = OBJ_obj2nid(alg1->algorithm);
  216. if (hnid == NID_undef)
  217. return -1;
  218. if (pknid <= 0) { /* check whether a provider registered a NID */
  219. const char *typename = EVP_PKEY_get0_type_name(pkey);
  220. if (typename != NULL)
  221. pknid = OBJ_txt2nid(typename);
  222. }
  223. if (!OBJ_find_sigid_by_algs(&snid, hnid, pknid))
  224. return -1;
  225. return X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, NULL);
  226. }
  227. return 1;
  228. }
  229. static int cms_sd_asn1_ctrl(CMS_SignerInfo *si, int cmd)
  230. {
  231. EVP_PKEY *pkey = si->pkey;
  232. int i;
  233. if (EVP_PKEY_is_a(pkey, "DSA") || EVP_PKEY_is_a(pkey, "EC"))
  234. return cms_generic_sign(si, cmd);
  235. else if (EVP_PKEY_is_a(pkey, "RSA") || EVP_PKEY_is_a(pkey, "RSA-PSS"))
  236. return ossl_cms_rsa_sign(si, cmd);
  237. /* Now give engines, providers, etc a chance to handle this */
  238. if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
  239. return cms_generic_sign(si, cmd);
  240. i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_SIGN, cmd, si);
  241. if (i == -2) {
  242. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  243. return 0;
  244. }
  245. if (i <= 0) {
  246. ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE);
  247. return 0;
  248. }
  249. return 1;
  250. }
  251. /* Add SigningCertificate signed attribute to the signer info. */
  252. static int ossl_cms_add1_signing_cert(CMS_SignerInfo *si,
  253. const ESS_SIGNING_CERT *sc)
  254. {
  255. ASN1_STRING *seq = NULL;
  256. unsigned char *p, *pp = NULL;
  257. int ret, len = i2d_ESS_SIGNING_CERT(sc, NULL);
  258. if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
  259. return 0;
  260. p = pp;
  261. i2d_ESS_SIGNING_CERT(sc, &p);
  262. if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len)) {
  263. ASN1_STRING_free(seq);
  264. OPENSSL_free(pp);
  265. return 0;
  266. }
  267. OPENSSL_free(pp);
  268. ret = CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificate,
  269. V_ASN1_SEQUENCE, seq, -1);
  270. ASN1_STRING_free(seq);
  271. return ret;
  272. }
  273. /* Add SigningCertificateV2 signed attribute to the signer info. */
  274. static int ossl_cms_add1_signing_cert_v2(CMS_SignerInfo *si,
  275. const ESS_SIGNING_CERT_V2 *sc)
  276. {
  277. ASN1_STRING *seq = NULL;
  278. unsigned char *p, *pp = NULL;
  279. int ret, len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
  280. if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
  281. return 0;
  282. p = pp;
  283. i2d_ESS_SIGNING_CERT_V2(sc, &p);
  284. if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len)) {
  285. ASN1_STRING_free(seq);
  286. OPENSSL_free(pp);
  287. return 0;
  288. }
  289. OPENSSL_free(pp);
  290. ret = CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificateV2,
  291. V_ASN1_SEQUENCE, seq, -1);
  292. ASN1_STRING_free(seq);
  293. return ret;
  294. }
  295. CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
  296. X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
  297. unsigned int flags)
  298. {
  299. CMS_SignedData *sd;
  300. CMS_SignerInfo *si = NULL;
  301. X509_ALGOR *alg;
  302. int i, type;
  303. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
  304. if (!X509_check_private_key(signer, pk)) {
  305. ERR_raise(ERR_LIB_CMS, CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  306. return NULL;
  307. }
  308. sd = cms_signed_data_init(cms);
  309. if (!sd)
  310. goto err;
  311. si = M_ASN1_new_of(CMS_SignerInfo);
  312. if (!si) {
  313. ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
  314. goto err;
  315. }
  316. /* Call for side-effect of computing hash and caching extensions */
  317. X509_check_purpose(signer, -1, -1);
  318. X509_up_ref(signer);
  319. EVP_PKEY_up_ref(pk);
  320. si->cms_ctx = ctx;
  321. si->pkey = pk;
  322. si->signer = signer;
  323. si->mctx = EVP_MD_CTX_new();
  324. si->pctx = NULL;
  325. if (si->mctx == NULL) {
  326. ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
  327. goto err;
  328. }
  329. if (flags & CMS_USE_KEYID) {
  330. si->version = 3;
  331. if (sd->version < 3)
  332. sd->version = 3;
  333. type = CMS_SIGNERINFO_KEYIDENTIFIER;
  334. } else {
  335. type = CMS_SIGNERINFO_ISSUER_SERIAL;
  336. si->version = 1;
  337. }
  338. if (!ossl_cms_set1_SignerIdentifier(si->sid, signer, type, ctx))
  339. goto err;
  340. if (md == NULL) {
  341. int def_nid;
  342. if (EVP_PKEY_get_default_digest_nid(pk, &def_nid) <= 0)
  343. goto err;
  344. md = EVP_get_digestbynid(def_nid);
  345. if (md == NULL) {
  346. ERR_raise(ERR_LIB_CMS, CMS_R_NO_DEFAULT_DIGEST);
  347. goto err;
  348. }
  349. }
  350. if (md == NULL) {
  351. ERR_raise(ERR_LIB_CMS, CMS_R_NO_DIGEST_SET);
  352. goto err;
  353. }
  354. X509_ALGOR_set_md(si->digestAlgorithm, md);
  355. /* See if digest is present in digestAlgorithms */
  356. for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
  357. const ASN1_OBJECT *aoid;
  358. char name[OSSL_MAX_NAME_SIZE];
  359. alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
  360. X509_ALGOR_get0(&aoid, NULL, NULL, alg);
  361. OBJ_obj2txt(name, sizeof(name), aoid, 0);
  362. if (EVP_MD_is_a(md, name))
  363. break;
  364. }
  365. if (i == sk_X509_ALGOR_num(sd->digestAlgorithms)) {
  366. if ((alg = X509_ALGOR_new()) == NULL) {
  367. ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
  368. goto err;
  369. }
  370. X509_ALGOR_set_md(alg, md);
  371. if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg)) {
  372. X509_ALGOR_free(alg);
  373. ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
  374. goto err;
  375. }
  376. }
  377. if (!(flags & CMS_KEY_PARAM) && !cms_sd_asn1_ctrl(si, 0))
  378. goto err;
  379. if (!(flags & CMS_NOATTR)) {
  380. /*
  381. * Initialize signed attributes structure so other attributes
  382. * such as signing time etc are added later even if we add none here.
  383. */
  384. if (!si->signedAttrs) {
  385. si->signedAttrs = sk_X509_ATTRIBUTE_new_null();
  386. if (!si->signedAttrs) {
  387. ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
  388. goto err;
  389. }
  390. }
  391. if (!(flags & CMS_NOSMIMECAP)) {
  392. STACK_OF(X509_ALGOR) *smcap = NULL;
  393. i = CMS_add_standard_smimecap(&smcap);
  394. if (i)
  395. i = CMS_add_smimecap(si, smcap);
  396. sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
  397. if (!i) {
  398. ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
  399. goto err;
  400. }
  401. }
  402. if (flags & CMS_CADES) {
  403. ESS_SIGNING_CERT *sc = NULL;
  404. ESS_SIGNING_CERT_V2 *sc2 = NULL;
  405. int add_sc;
  406. if (md == NULL || EVP_MD_is_a(md, SN_sha1)) {
  407. if ((sc = OSSL_ESS_signing_cert_new_init(signer,
  408. NULL, 1)) == NULL)
  409. goto err;
  410. add_sc = ossl_cms_add1_signing_cert(si, sc);
  411. ESS_SIGNING_CERT_free(sc);
  412. } else {
  413. if ((sc2 = OSSL_ESS_signing_cert_v2_new_init(md, signer,
  414. NULL, 1)) == NULL)
  415. goto err;
  416. add_sc = ossl_cms_add1_signing_cert_v2(si, sc2);
  417. ESS_SIGNING_CERT_V2_free(sc2);
  418. }
  419. if (!add_sc)
  420. goto err;
  421. }
  422. if (flags & CMS_REUSE_DIGEST) {
  423. if (!cms_copy_messageDigest(cms, si))
  424. goto err;
  425. if (!cms_set_si_contentType_attr(cms, si))
  426. goto err;
  427. if (!(flags & (CMS_PARTIAL | CMS_KEY_PARAM)) &&
  428. !CMS_SignerInfo_sign(si))
  429. goto err;
  430. }
  431. }
  432. if (!(flags & CMS_NOCERTS)) {
  433. /* NB ignore -1 return for duplicate cert */
  434. if (!CMS_add1_cert(cms, signer)) {
  435. ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
  436. goto err;
  437. }
  438. }
  439. if (flags & CMS_KEY_PARAM) {
  440. if (flags & CMS_NOATTR) {
  441. si->pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
  442. si->pkey,
  443. ossl_cms_ctx_get0_propq(ctx));
  444. if (si->pctx == NULL)
  445. goto err;
  446. if (EVP_PKEY_sign_init(si->pctx) <= 0)
  447. goto err;
  448. if (EVP_PKEY_CTX_set_signature_md(si->pctx, md) <= 0)
  449. goto err;
  450. } else if (EVP_DigestSignInit_ex(si->mctx, &si->pctx,
  451. EVP_MD_get0_name(md),
  452. ossl_cms_ctx_get0_libctx(ctx),
  453. ossl_cms_ctx_get0_propq(ctx),
  454. pk, NULL) <= 0) {
  455. goto err;
  456. }
  457. }
  458. if (sd->signerInfos == NULL)
  459. sd->signerInfos = sk_CMS_SignerInfo_new_null();
  460. if (sd->signerInfos == NULL || !sk_CMS_SignerInfo_push(sd->signerInfos, si)) {
  461. ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
  462. goto err;
  463. }
  464. return si;
  465. err:
  466. M_ASN1_free_of(si, CMS_SignerInfo);
  467. return NULL;
  468. }
  469. void ossl_cms_SignerInfos_set_cmsctx(CMS_ContentInfo *cms)
  470. {
  471. int i;
  472. CMS_SignerInfo *si;
  473. STACK_OF(CMS_SignerInfo) *sinfos;
  474. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
  475. ERR_set_mark();
  476. sinfos = CMS_get0_SignerInfos(cms);
  477. ERR_pop_to_mark(); /* removes error in case sinfos == NULL */
  478. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  479. si = sk_CMS_SignerInfo_value(sinfos, i);
  480. if (si != NULL)
  481. si->cms_ctx = ctx;
  482. }
  483. }
  484. static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
  485. {
  486. ASN1_TIME *tt;
  487. int r = 0;
  488. if (t != NULL)
  489. tt = t;
  490. else
  491. tt = X509_gmtime_adj(NULL, 0);
  492. if (tt == NULL) {
  493. ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB);
  494. goto err;
  495. }
  496. if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
  497. tt->type, tt, -1) <= 0) {
  498. ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
  499. goto err;
  500. }
  501. r = 1;
  502. err:
  503. if (t == NULL)
  504. ASN1_TIME_free(tt);
  505. return r;
  506. }
  507. EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si)
  508. {
  509. return si->pctx;
  510. }
  511. EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si)
  512. {
  513. return si->mctx;
  514. }
  515. STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
  516. {
  517. CMS_SignedData *sd = cms_get0_signed(cms);
  518. return sd != NULL ? sd->signerInfos : NULL;
  519. }
  520. STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
  521. {
  522. STACK_OF(X509) *signers = NULL;
  523. STACK_OF(CMS_SignerInfo) *sinfos;
  524. CMS_SignerInfo *si;
  525. int i;
  526. sinfos = CMS_get0_SignerInfos(cms);
  527. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  528. si = sk_CMS_SignerInfo_value(sinfos, i);
  529. if (si->signer != NULL) {
  530. if (!ossl_x509_add_cert_new(&signers, si->signer,
  531. X509_ADD_FLAG_DEFAULT)) {
  532. sk_X509_free(signers);
  533. return NULL;
  534. }
  535. }
  536. }
  537. return signers;
  538. }
  539. void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
  540. {
  541. if (signer != NULL) {
  542. X509_up_ref(signer);
  543. EVP_PKEY_free(si->pkey);
  544. si->pkey = X509_get_pubkey(signer);
  545. }
  546. X509_free(si->signer);
  547. si->signer = signer;
  548. }
  549. int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
  550. ASN1_OCTET_STRING **keyid,
  551. X509_NAME **issuer, ASN1_INTEGER **sno)
  552. {
  553. return ossl_cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
  554. }
  555. int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
  556. {
  557. return ossl_cms_SignerIdentifier_cert_cmp(si->sid, cert);
  558. }
  559. int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,
  560. unsigned int flags)
  561. {
  562. CMS_SignedData *sd;
  563. CMS_SignerInfo *si;
  564. CMS_CertificateChoices *cch;
  565. STACK_OF(CMS_CertificateChoices) *certs;
  566. X509 *x;
  567. int i, j;
  568. int ret = 0;
  569. sd = cms_get0_signed(cms);
  570. if (sd == NULL)
  571. return -1;
  572. certs = sd->certificates;
  573. for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
  574. si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
  575. if (si->signer != NULL)
  576. continue;
  577. for (j = 0; j < sk_X509_num(scerts); j++) {
  578. x = sk_X509_value(scerts, j);
  579. if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
  580. CMS_SignerInfo_set1_signer_cert(si, x);
  581. ret++;
  582. break;
  583. }
  584. }
  585. if (si->signer != NULL || (flags & CMS_NOINTERN))
  586. continue;
  587. for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++) {
  588. cch = sk_CMS_CertificateChoices_value(certs, j);
  589. if (cch->type != CMS_CERTCHOICE_CERT)
  590. continue;
  591. x = cch->d.certificate;
  592. if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
  593. CMS_SignerInfo_set1_signer_cert(si, x);
  594. ret++;
  595. break;
  596. }
  597. }
  598. }
  599. return ret;
  600. }
  601. void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk,
  602. X509 **signer, X509_ALGOR **pdig,
  603. X509_ALGOR **psig)
  604. {
  605. if (pk != NULL)
  606. *pk = si->pkey;
  607. if (signer != NULL)
  608. *signer = si->signer;
  609. if (pdig != NULL)
  610. *pdig = si->digestAlgorithm;
  611. if (psig != NULL)
  612. *psig = si->signatureAlgorithm;
  613. }
  614. ASN1_OCTET_STRING *CMS_SignerInfo_get0_signature(CMS_SignerInfo *si)
  615. {
  616. return si->signature;
  617. }
  618. static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
  619. CMS_SignerInfo *si, BIO *chain,
  620. const unsigned char *md,
  621. unsigned int mdlen)
  622. {
  623. EVP_MD_CTX *mctx = EVP_MD_CTX_new();
  624. int r = 0;
  625. EVP_PKEY_CTX *pctx = NULL;
  626. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
  627. if (mctx == NULL) {
  628. ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
  629. return 0;
  630. }
  631. if (si->pkey == NULL) {
  632. ERR_raise(ERR_LIB_CMS, CMS_R_NO_PRIVATE_KEY);
  633. goto err;
  634. }
  635. if (!ossl_cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
  636. goto err;
  637. /* Set SignerInfo algorithm details if we used custom parameter */
  638. if (si->pctx && !cms_sd_asn1_ctrl(si, 0))
  639. goto err;
  640. /*
  641. * If any signed attributes calculate and add messageDigest attribute
  642. */
  643. if (CMS_signed_get_attr_count(si) >= 0) {
  644. unsigned char computed_md[EVP_MAX_MD_SIZE];
  645. if (md == NULL) {
  646. if (!EVP_DigestFinal_ex(mctx, computed_md, &mdlen))
  647. goto err;
  648. md = computed_md;
  649. }
  650. if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
  651. V_ASN1_OCTET_STRING, md, mdlen))
  652. goto err;
  653. /* Copy content type across */
  654. if (!cms_set_si_contentType_attr(cms, si))
  655. goto err;
  656. if (!CMS_SignerInfo_sign(si))
  657. goto err;
  658. } else if (si->pctx) {
  659. unsigned char *sig;
  660. size_t siglen;
  661. unsigned char computed_md[EVP_MAX_MD_SIZE];
  662. pctx = si->pctx;
  663. if (md == NULL) {
  664. if (!EVP_DigestFinal_ex(mctx, computed_md, &mdlen))
  665. goto err;
  666. md = computed_md;
  667. }
  668. siglen = EVP_PKEY_get_size(si->pkey);
  669. sig = OPENSSL_malloc(siglen);
  670. if (sig == NULL)
  671. goto err;
  672. if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) {
  673. OPENSSL_free(sig);
  674. goto err;
  675. }
  676. ASN1_STRING_set0(si->signature, sig, siglen);
  677. } else {
  678. unsigned char *sig;
  679. unsigned int siglen;
  680. if (md != NULL) {
  681. ERR_raise(ERR_LIB_CMS, CMS_R_OPERATION_UNSUPPORTED);
  682. goto err;
  683. }
  684. sig = OPENSSL_malloc(EVP_PKEY_get_size(si->pkey));
  685. if (sig == NULL)
  686. goto err;
  687. if (!EVP_SignFinal_ex(mctx, sig, &siglen, si->pkey,
  688. ossl_cms_ctx_get0_libctx(ctx),
  689. ossl_cms_ctx_get0_propq(ctx))) {
  690. ERR_raise(ERR_LIB_CMS, CMS_R_SIGNFINAL_ERROR);
  691. OPENSSL_free(sig);
  692. goto err;
  693. }
  694. ASN1_STRING_set0(si->signature, sig, siglen);
  695. }
  696. r = 1;
  697. err:
  698. EVP_MD_CTX_free(mctx);
  699. EVP_PKEY_CTX_free(pctx);
  700. return r;
  701. }
  702. int ossl_cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain,
  703. const unsigned char *precomp_md,
  704. unsigned int precomp_mdlen)
  705. {
  706. STACK_OF(CMS_SignerInfo) *sinfos;
  707. CMS_SignerInfo *si;
  708. int i;
  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,
  713. precomp_md, precomp_mdlen))
  714. return 0;
  715. }
  716. cms->d.signedData->encapContentInfo->partial = 0;
  717. return 1;
  718. }
  719. int CMS_SignerInfo_sign(CMS_SignerInfo *si)
  720. {
  721. EVP_MD_CTX *mctx = si->mctx;
  722. EVP_PKEY_CTX *pctx = NULL;
  723. unsigned char *abuf = NULL;
  724. int alen;
  725. size_t siglen;
  726. const CMS_CTX *ctx = si->cms_ctx;
  727. char md_name[OSSL_MAX_NAME_SIZE];
  728. if (OBJ_obj2txt(md_name, sizeof(md_name),
  729. si->digestAlgorithm->algorithm, 0) <= 0)
  730. return 0;
  731. if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0) {
  732. if (!cms_add1_signingTime(si, NULL))
  733. goto err;
  734. }
  735. if (!ossl_cms_si_check_attributes(si))
  736. goto err;
  737. if (si->pctx) {
  738. pctx = si->pctx;
  739. } else {
  740. EVP_MD_CTX_reset(mctx);
  741. if (EVP_DigestSignInit_ex(mctx, &pctx, md_name,
  742. ossl_cms_ctx_get0_libctx(ctx),
  743. ossl_cms_ctx_get0_propq(ctx), si->pkey,
  744. NULL) <= 0)
  745. goto err;
  746. si->pctx = pctx;
  747. }
  748. alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
  749. ASN1_ITEM_rptr(CMS_Attributes_Sign));
  750. if (!abuf)
  751. goto err;
  752. if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
  753. goto err;
  754. if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
  755. goto err;
  756. OPENSSL_free(abuf);
  757. abuf = OPENSSL_malloc(siglen);
  758. if (abuf == NULL)
  759. goto err;
  760. if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
  761. goto err;
  762. EVP_MD_CTX_reset(mctx);
  763. ASN1_STRING_set0(si->signature, abuf, siglen);
  764. return 1;
  765. err:
  766. OPENSSL_free(abuf);
  767. EVP_MD_CTX_reset(mctx);
  768. return 0;
  769. }
  770. int CMS_SignerInfo_verify(CMS_SignerInfo *si)
  771. {
  772. EVP_MD_CTX *mctx = NULL;
  773. unsigned char *abuf = NULL;
  774. int alen, r = -1;
  775. char name[OSSL_MAX_NAME_SIZE];
  776. const EVP_MD *md;
  777. EVP_MD *fetched_md = NULL;
  778. const CMS_CTX *ctx = si->cms_ctx;
  779. OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
  780. const char *propq = ossl_cms_ctx_get0_propq(ctx);
  781. if (si->pkey == NULL) {
  782. ERR_raise(ERR_LIB_CMS, CMS_R_NO_PUBLIC_KEY);
  783. return -1;
  784. }
  785. if (!ossl_cms_si_check_attributes(si))
  786. return -1;
  787. OBJ_obj2txt(name, sizeof(name), si->digestAlgorithm->algorithm, 0);
  788. (void)ERR_set_mark();
  789. fetched_md = EVP_MD_fetch(libctx, name, propq);
  790. if (fetched_md != NULL)
  791. md = fetched_md;
  792. else
  793. md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
  794. if (md == NULL) {
  795. (void)ERR_clear_last_mark();
  796. ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_DIGEST_ALGORITHM);
  797. return -1;
  798. }
  799. (void)ERR_pop_to_mark();
  800. if (si->mctx == NULL && (si->mctx = EVP_MD_CTX_new()) == NULL) {
  801. ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
  802. goto err;
  803. }
  804. mctx = si->mctx;
  805. if (EVP_DigestVerifyInit_ex(mctx, &si->pctx, EVP_MD_get0_name(md), libctx,
  806. propq, si->pkey, NULL) <= 0)
  807. goto err;
  808. if (!cms_sd_asn1_ctrl(si, 1))
  809. goto err;
  810. alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
  811. ASN1_ITEM_rptr(CMS_Attributes_Verify));
  812. if (abuf == NULL || alen < 0)
  813. goto err;
  814. r = EVP_DigestVerifyUpdate(mctx, abuf, alen);
  815. OPENSSL_free(abuf);
  816. if (r <= 0) {
  817. r = -1;
  818. goto err;
  819. }
  820. r = EVP_DigestVerifyFinal(mctx,
  821. si->signature->data, si->signature->length);
  822. if (r <= 0)
  823. ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
  824. err:
  825. EVP_MD_free(fetched_md);
  826. EVP_MD_CTX_reset(mctx);
  827. return r;
  828. }
  829. /* Create a chain of digest BIOs from a CMS ContentInfo */
  830. BIO *ossl_cms_SignedData_init_bio(CMS_ContentInfo *cms)
  831. {
  832. int i;
  833. CMS_SignedData *sd;
  834. BIO *chain = NULL;
  835. sd = cms_get0_signed(cms);
  836. if (sd == NULL)
  837. return NULL;
  838. if (cms->d.signedData->encapContentInfo->partial)
  839. cms_sd_set_version(sd);
  840. for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
  841. X509_ALGOR *digestAlgorithm;
  842. BIO *mdbio;
  843. digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
  844. mdbio = ossl_cms_DigestAlgorithm_init_bio(digestAlgorithm,
  845. ossl_cms_get0_cmsctx(cms));
  846. if (mdbio == NULL)
  847. goto err;
  848. if (chain != NULL)
  849. BIO_push(chain, mdbio);
  850. else
  851. chain = mdbio;
  852. }
  853. return chain;
  854. err:
  855. BIO_free_all(chain);
  856. return NULL;
  857. }
  858. int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
  859. {
  860. ASN1_OCTET_STRING *os = NULL;
  861. EVP_MD_CTX *mctx = EVP_MD_CTX_new();
  862. EVP_PKEY_CTX *pkctx = NULL;
  863. int r = -1;
  864. unsigned char mval[EVP_MAX_MD_SIZE];
  865. unsigned int mlen;
  866. if (mctx == NULL) {
  867. ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
  868. goto err;
  869. }
  870. /* If we have any signed attributes look for messageDigest value */
  871. if (CMS_signed_get_attr_count(si) >= 0) {
  872. os = CMS_signed_get0_data_by_OBJ(si,
  873. OBJ_nid2obj(NID_pkcs9_messageDigest),
  874. -3, V_ASN1_OCTET_STRING);
  875. if (os == NULL) {
  876. ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
  877. goto err;
  878. }
  879. }
  880. if (!ossl_cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
  881. goto err;
  882. if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {
  883. ERR_raise(ERR_LIB_CMS, CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
  884. goto err;
  885. }
  886. /* If messageDigest found compare it */
  887. if (os != NULL) {
  888. if (mlen != (unsigned int)os->length) {
  889. ERR_raise(ERR_LIB_CMS, CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
  890. goto err;
  891. }
  892. if (memcmp(mval, os->data, mlen)) {
  893. ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
  894. r = 0;
  895. } else {
  896. r = 1;
  897. }
  898. } else {
  899. const EVP_MD *md = EVP_MD_CTX_get0_md(mctx);
  900. const CMS_CTX *ctx = si->cms_ctx;
  901. pkctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
  902. si->pkey,
  903. ossl_cms_ctx_get0_propq(ctx));
  904. if (pkctx == NULL)
  905. goto err;
  906. if (EVP_PKEY_verify_init(pkctx) <= 0)
  907. goto err;
  908. if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)
  909. goto err;
  910. si->pctx = pkctx;
  911. if (!cms_sd_asn1_ctrl(si, 1))
  912. goto err;
  913. r = EVP_PKEY_verify(pkctx, si->signature->data,
  914. si->signature->length, mval, mlen);
  915. if (r <= 0) {
  916. ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
  917. r = 0;
  918. }
  919. }
  920. err:
  921. EVP_PKEY_CTX_free(pkctx);
  922. EVP_MD_CTX_free(mctx);
  923. return r;
  924. }
  925. BIO *CMS_SignedData_verify(CMS_SignedData *sd, BIO *detached_data,
  926. STACK_OF(X509) *scerts, X509_STORE *store,
  927. STACK_OF(X509) *extra, STACK_OF(X509_CRL) *crls,
  928. unsigned int flags,
  929. OSSL_LIB_CTX *libctx, const char *propq)
  930. {
  931. CMS_ContentInfo *ci;
  932. BIO *bio = NULL;
  933. int i, res = 0;
  934. if (sd == NULL) {
  935. ERR_raise(ERR_LIB_CMS, ERR_R_PASSED_NULL_PARAMETER);
  936. return NULL;
  937. }
  938. if ((ci = CMS_ContentInfo_new_ex(libctx, propq)) == NULL)
  939. return NULL;
  940. if ((bio = BIO_new(BIO_s_mem())) == NULL)
  941. goto end;
  942. ci->contentType = OBJ_nid2obj(NID_pkcs7_signed);
  943. ci->d.signedData = sd;
  944. for (i = 0; i < sk_X509_num(extra); i++)
  945. if (!CMS_add1_cert(ci, sk_X509_value(extra, i)))
  946. goto end;
  947. for (i = 0; i < sk_X509_CRL_num(crls); i++)
  948. if (!CMS_add1_crl(ci, sk_X509_CRL_value(crls, i)))
  949. goto end;
  950. res = CMS_verify(ci, scerts, store, detached_data, bio, flags);
  951. end:
  952. if (ci != NULL)
  953. ci->d.signedData = NULL; /* do not indirectly free |sd| */
  954. CMS_ContentInfo_free(ci);
  955. if (!res) {
  956. BIO_free(bio);
  957. bio = NULL;
  958. }
  959. return bio;
  960. }
  961. int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
  962. {
  963. unsigned char *smder = NULL;
  964. int smderlen, r;
  965. smderlen = i2d_X509_ALGORS(algs, &smder);
  966. if (smderlen <= 0)
  967. return 0;
  968. r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
  969. V_ASN1_SEQUENCE, smder, smderlen);
  970. OPENSSL_free(smder);
  971. return r;
  972. }
  973. int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
  974. int algnid, int keysize)
  975. {
  976. X509_ALGOR *alg;
  977. ASN1_INTEGER *key = NULL;
  978. if (keysize > 0) {
  979. key = ASN1_INTEGER_new();
  980. if (key == NULL || !ASN1_INTEGER_set(key, keysize)) {
  981. ASN1_INTEGER_free(key);
  982. return 0;
  983. }
  984. }
  985. alg = ossl_X509_ALGOR_from_nid(algnid, key != NULL ? V_ASN1_INTEGER :
  986. V_ASN1_UNDEF, key);
  987. if (alg == NULL) {
  988. ASN1_INTEGER_free(key);
  989. return 0;
  990. }
  991. if (*algs == NULL)
  992. *algs = sk_X509_ALGOR_new_null();
  993. if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) {
  994. X509_ALGOR_free(alg);
  995. return 0;
  996. }
  997. return 1;
  998. }
  999. /* Check to see if a cipher exists and if so add S/MIME capabilities */
  1000. static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
  1001. {
  1002. if (EVP_get_cipherbynid(nid))
  1003. return CMS_add_simple_smimecap(sk, nid, arg);
  1004. return 1;
  1005. }
  1006. static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
  1007. {
  1008. if (EVP_get_digestbynid(nid))
  1009. return CMS_add_simple_smimecap(sk, nid, arg);
  1010. return 1;
  1011. }
  1012. int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
  1013. {
  1014. if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
  1015. || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
  1016. || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
  1017. || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
  1018. || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
  1019. || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
  1020. || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
  1021. || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
  1022. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
  1023. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
  1024. || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
  1025. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))
  1026. return 0;
  1027. return 1;
  1028. }