cms_sd.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. /*
  2. * Copyright 2008-2023 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) > 0;
  235. else if (EVP_PKEY_is_a(pkey, "RSA") || EVP_PKEY_is_a(pkey, "RSA-PSS"))
  236. return ossl_cms_rsa_sign(si, cmd) > 0;
  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) > 0;
  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. ERR_raise_data(ERR_LIB_CMS, CMS_R_NO_DEFAULT_DIGEST,
  344. "pkey nid=%d", EVP_PKEY_get_id(pk));
  345. goto err;
  346. }
  347. md = EVP_get_digestbynid(def_nid);
  348. if (md == NULL) {
  349. ERR_raise_data(ERR_LIB_CMS, CMS_R_NO_DEFAULT_DIGEST,
  350. "default md nid=%d", def_nid);
  351. goto err;
  352. }
  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. ERR_raise_data(ERR_LIB_CMS, CMS_R_UNSUPPORTED_SIGNATURE_ALGORITHM,
  379. "pkey nid=%d", EVP_PKEY_get_id(pk));
  380. goto err;
  381. }
  382. if (!(flags & CMS_NOATTR)) {
  383. /*
  384. * Initialize signed attributes structure so other attributes
  385. * such as signing time etc are added later even if we add none here.
  386. */
  387. if (!si->signedAttrs) {
  388. si->signedAttrs = sk_X509_ATTRIBUTE_new_null();
  389. if (!si->signedAttrs) {
  390. ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
  391. goto err;
  392. }
  393. }
  394. if (!(flags & CMS_NOSMIMECAP)) {
  395. STACK_OF(X509_ALGOR) *smcap = NULL;
  396. i = CMS_add_standard_smimecap(&smcap);
  397. if (i)
  398. i = CMS_add_smimecap(si, smcap);
  399. sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
  400. if (!i) {
  401. ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
  402. goto err;
  403. }
  404. }
  405. if (flags & CMS_CADES) {
  406. ESS_SIGNING_CERT *sc = NULL;
  407. ESS_SIGNING_CERT_V2 *sc2 = NULL;
  408. int add_sc;
  409. if (md == NULL || EVP_MD_is_a(md, SN_sha1)) {
  410. if ((sc = OSSL_ESS_signing_cert_new_init(signer,
  411. NULL, 1)) == NULL)
  412. goto err;
  413. add_sc = ossl_cms_add1_signing_cert(si, sc);
  414. ESS_SIGNING_CERT_free(sc);
  415. } else {
  416. if ((sc2 = OSSL_ESS_signing_cert_v2_new_init(md, signer,
  417. NULL, 1)) == NULL)
  418. goto err;
  419. add_sc = ossl_cms_add1_signing_cert_v2(si, sc2);
  420. ESS_SIGNING_CERT_V2_free(sc2);
  421. }
  422. if (!add_sc)
  423. goto err;
  424. }
  425. if (flags & CMS_REUSE_DIGEST) {
  426. if (!cms_copy_messageDigest(cms, si))
  427. goto err;
  428. if (!cms_set_si_contentType_attr(cms, si))
  429. goto err;
  430. if (!(flags & (CMS_PARTIAL | CMS_KEY_PARAM)) &&
  431. !CMS_SignerInfo_sign(si))
  432. goto err;
  433. }
  434. }
  435. if (!(flags & CMS_NOCERTS)) {
  436. /* NB ignore -1 return for duplicate cert */
  437. if (!CMS_add1_cert(cms, signer)) {
  438. ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
  439. goto err;
  440. }
  441. }
  442. if (flags & CMS_KEY_PARAM) {
  443. if (flags & CMS_NOATTR) {
  444. si->pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
  445. si->pkey,
  446. ossl_cms_ctx_get0_propq(ctx));
  447. if (si->pctx == NULL)
  448. goto err;
  449. if (EVP_PKEY_sign_init(si->pctx) <= 0)
  450. goto err;
  451. if (EVP_PKEY_CTX_set_signature_md(si->pctx, md) <= 0)
  452. goto err;
  453. } else if (EVP_DigestSignInit_ex(si->mctx, &si->pctx,
  454. EVP_MD_get0_name(md),
  455. ossl_cms_ctx_get0_libctx(ctx),
  456. ossl_cms_ctx_get0_propq(ctx),
  457. pk, NULL) <= 0) {
  458. goto err;
  459. }
  460. }
  461. if (sd->signerInfos == NULL)
  462. sd->signerInfos = sk_CMS_SignerInfo_new_null();
  463. if (sd->signerInfos == NULL || !sk_CMS_SignerInfo_push(sd->signerInfos, si)) {
  464. ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
  465. goto err;
  466. }
  467. return si;
  468. err:
  469. M_ASN1_free_of(si, CMS_SignerInfo);
  470. return NULL;
  471. }
  472. void ossl_cms_SignerInfos_set_cmsctx(CMS_ContentInfo *cms)
  473. {
  474. int i;
  475. CMS_SignerInfo *si;
  476. STACK_OF(CMS_SignerInfo) *sinfos;
  477. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
  478. ERR_set_mark();
  479. sinfos = CMS_get0_SignerInfos(cms);
  480. ERR_pop_to_mark(); /* removes error in case sinfos == NULL */
  481. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  482. si = sk_CMS_SignerInfo_value(sinfos, i);
  483. if (si != NULL)
  484. si->cms_ctx = ctx;
  485. }
  486. }
  487. static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
  488. {
  489. ASN1_TIME *tt;
  490. int r = 0;
  491. if (t != NULL)
  492. tt = t;
  493. else
  494. tt = X509_gmtime_adj(NULL, 0);
  495. if (tt == NULL) {
  496. ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB);
  497. goto err;
  498. }
  499. if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
  500. tt->type, tt, -1) <= 0) {
  501. ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
  502. goto err;
  503. }
  504. r = 1;
  505. err:
  506. if (t == NULL)
  507. ASN1_TIME_free(tt);
  508. return r;
  509. }
  510. EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si)
  511. {
  512. return si->pctx;
  513. }
  514. EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si)
  515. {
  516. return si->mctx;
  517. }
  518. STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
  519. {
  520. CMS_SignedData *sd = cms_get0_signed(cms);
  521. return sd != NULL ? sd->signerInfos : NULL;
  522. }
  523. STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
  524. {
  525. STACK_OF(X509) *signers = NULL;
  526. STACK_OF(CMS_SignerInfo) *sinfos;
  527. CMS_SignerInfo *si;
  528. int i;
  529. sinfos = CMS_get0_SignerInfos(cms);
  530. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  531. si = sk_CMS_SignerInfo_value(sinfos, i);
  532. if (si->signer != NULL) {
  533. if (!ossl_x509_add_cert_new(&signers, si->signer,
  534. X509_ADD_FLAG_DEFAULT)) {
  535. sk_X509_free(signers);
  536. return NULL;
  537. }
  538. }
  539. }
  540. return signers;
  541. }
  542. void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
  543. {
  544. if (signer != NULL) {
  545. X509_up_ref(signer);
  546. EVP_PKEY_free(si->pkey);
  547. si->pkey = X509_get_pubkey(signer);
  548. }
  549. X509_free(si->signer);
  550. si->signer = signer;
  551. }
  552. int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
  553. ASN1_OCTET_STRING **keyid,
  554. X509_NAME **issuer, ASN1_INTEGER **sno)
  555. {
  556. return ossl_cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
  557. }
  558. int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
  559. {
  560. return ossl_cms_SignerIdentifier_cert_cmp(si->sid, cert);
  561. }
  562. int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,
  563. unsigned int flags)
  564. {
  565. CMS_SignedData *sd;
  566. CMS_SignerInfo *si;
  567. CMS_CertificateChoices *cch;
  568. STACK_OF(CMS_CertificateChoices) *certs;
  569. X509 *x;
  570. int i, j;
  571. int ret = 0;
  572. sd = cms_get0_signed(cms);
  573. if (sd == NULL)
  574. return -1;
  575. certs = sd->certificates;
  576. for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
  577. si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
  578. if (si->signer != NULL)
  579. continue;
  580. for (j = 0; j < sk_X509_num(scerts); j++) {
  581. x = sk_X509_value(scerts, j);
  582. if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
  583. CMS_SignerInfo_set1_signer_cert(si, x);
  584. ret++;
  585. break;
  586. }
  587. }
  588. if (si->signer != NULL || (flags & CMS_NOINTERN))
  589. continue;
  590. for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++) {
  591. cch = sk_CMS_CertificateChoices_value(certs, j);
  592. if (cch->type != CMS_CERTCHOICE_CERT)
  593. continue;
  594. x = cch->d.certificate;
  595. if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
  596. CMS_SignerInfo_set1_signer_cert(si, x);
  597. ret++;
  598. break;
  599. }
  600. }
  601. }
  602. return ret;
  603. }
  604. void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk,
  605. X509 **signer, X509_ALGOR **pdig,
  606. X509_ALGOR **psig)
  607. {
  608. if (pk != NULL)
  609. *pk = si->pkey;
  610. if (signer != NULL)
  611. *signer = si->signer;
  612. if (pdig != NULL)
  613. *pdig = si->digestAlgorithm;
  614. if (psig != NULL)
  615. *psig = si->signatureAlgorithm;
  616. }
  617. ASN1_OCTET_STRING *CMS_SignerInfo_get0_signature(CMS_SignerInfo *si)
  618. {
  619. return si->signature;
  620. }
  621. static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
  622. CMS_SignerInfo *si, BIO *chain,
  623. const unsigned char *md,
  624. unsigned int mdlen)
  625. {
  626. EVP_MD_CTX *mctx = EVP_MD_CTX_new();
  627. int r = 0;
  628. EVP_PKEY_CTX *pctx = NULL;
  629. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
  630. if (mctx == NULL) {
  631. ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
  632. return 0;
  633. }
  634. if (si->pkey == NULL) {
  635. ERR_raise(ERR_LIB_CMS, CMS_R_NO_PRIVATE_KEY);
  636. goto err;
  637. }
  638. if (!ossl_cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
  639. goto err;
  640. /* Set SignerInfo algorithm details if we used custom parameter */
  641. if (si->pctx && !cms_sd_asn1_ctrl(si, 0))
  642. goto err;
  643. /*
  644. * If any signed attributes calculate and add messageDigest attribute
  645. */
  646. if (CMS_signed_get_attr_count(si) >= 0) {
  647. unsigned char computed_md[EVP_MAX_MD_SIZE];
  648. if (md == NULL) {
  649. if (!EVP_DigestFinal_ex(mctx, computed_md, &mdlen))
  650. goto err;
  651. md = computed_md;
  652. }
  653. if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
  654. V_ASN1_OCTET_STRING, md, mdlen))
  655. goto err;
  656. /* Copy content type across */
  657. if (!cms_set_si_contentType_attr(cms, si))
  658. goto err;
  659. if (!CMS_SignerInfo_sign(si))
  660. goto err;
  661. } else if (si->pctx) {
  662. unsigned char *sig;
  663. size_t siglen;
  664. unsigned char computed_md[EVP_MAX_MD_SIZE];
  665. pctx = si->pctx;
  666. if (md == NULL) {
  667. if (!EVP_DigestFinal_ex(mctx, computed_md, &mdlen))
  668. goto err;
  669. md = computed_md;
  670. }
  671. siglen = EVP_PKEY_get_size(si->pkey);
  672. if (siglen == 0 || (sig = OPENSSL_malloc(siglen)) == NULL)
  673. goto err;
  674. if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) {
  675. OPENSSL_free(sig);
  676. goto err;
  677. }
  678. ASN1_STRING_set0(si->signature, sig, siglen);
  679. } else {
  680. unsigned char *sig;
  681. unsigned int siglen;
  682. if (md != NULL) {
  683. ERR_raise(ERR_LIB_CMS, CMS_R_OPERATION_UNSUPPORTED);
  684. goto err;
  685. }
  686. siglen = EVP_PKEY_get_size(si->pkey);
  687. if (siglen == 0 || (sig = OPENSSL_malloc(siglen)) == NULL)
  688. goto err;
  689. if (!EVP_SignFinal_ex(mctx, sig, &siglen, si->pkey,
  690. ossl_cms_ctx_get0_libctx(ctx),
  691. ossl_cms_ctx_get0_propq(ctx))) {
  692. ERR_raise(ERR_LIB_CMS, CMS_R_SIGNFINAL_ERROR);
  693. OPENSSL_free(sig);
  694. goto err;
  695. }
  696. ASN1_STRING_set0(si->signature, sig, siglen);
  697. }
  698. r = 1;
  699. err:
  700. EVP_MD_CTX_free(mctx);
  701. EVP_PKEY_CTX_free(pctx);
  702. return r;
  703. }
  704. int ossl_cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain,
  705. const unsigned char *precomp_md,
  706. unsigned int precomp_mdlen)
  707. {
  708. STACK_OF(CMS_SignerInfo) *sinfos;
  709. CMS_SignerInfo *si;
  710. int i;
  711. sinfos = CMS_get0_SignerInfos(cms);
  712. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  713. si = sk_CMS_SignerInfo_value(sinfos, i);
  714. if (!cms_SignerInfo_content_sign(cms, si, chain,
  715. precomp_md, precomp_mdlen))
  716. return 0;
  717. }
  718. cms->d.signedData->encapContentInfo->partial = 0;
  719. return 1;
  720. }
  721. int CMS_SignerInfo_sign(CMS_SignerInfo *si)
  722. {
  723. EVP_MD_CTX *mctx = si->mctx;
  724. EVP_PKEY_CTX *pctx = NULL;
  725. unsigned char *abuf = NULL;
  726. int alen;
  727. size_t siglen;
  728. const CMS_CTX *ctx = si->cms_ctx;
  729. char md_name[OSSL_MAX_NAME_SIZE];
  730. if (OBJ_obj2txt(md_name, sizeof(md_name),
  731. si->digestAlgorithm->algorithm, 0) <= 0)
  732. return 0;
  733. if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0) {
  734. if (!cms_add1_signingTime(si, NULL))
  735. goto err;
  736. }
  737. if (!ossl_cms_si_check_attributes(si))
  738. goto err;
  739. if (si->pctx) {
  740. pctx = si->pctx;
  741. } else {
  742. EVP_MD_CTX_reset(mctx);
  743. if (EVP_DigestSignInit_ex(mctx, &pctx, md_name,
  744. ossl_cms_ctx_get0_libctx(ctx),
  745. ossl_cms_ctx_get0_propq(ctx), si->pkey,
  746. NULL) <= 0)
  747. goto err;
  748. si->pctx = pctx;
  749. }
  750. alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
  751. ASN1_ITEM_rptr(CMS_Attributes_Sign));
  752. if (!abuf)
  753. goto err;
  754. if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
  755. goto err;
  756. if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
  757. goto err;
  758. OPENSSL_free(abuf);
  759. abuf = OPENSSL_malloc(siglen);
  760. if (abuf == NULL)
  761. goto err;
  762. if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
  763. goto err;
  764. EVP_MD_CTX_reset(mctx);
  765. ASN1_STRING_set0(si->signature, abuf, siglen);
  766. return 1;
  767. err:
  768. OPENSSL_free(abuf);
  769. EVP_MD_CTX_reset(mctx);
  770. return 0;
  771. }
  772. int CMS_SignerInfo_verify(CMS_SignerInfo *si)
  773. {
  774. EVP_MD_CTX *mctx = NULL;
  775. unsigned char *abuf = NULL;
  776. int alen, r = -1;
  777. char name[OSSL_MAX_NAME_SIZE];
  778. const EVP_MD *md;
  779. EVP_MD *fetched_md = NULL;
  780. const CMS_CTX *ctx = si->cms_ctx;
  781. OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
  782. const char *propq = ossl_cms_ctx_get0_propq(ctx);
  783. if (si->pkey == NULL) {
  784. ERR_raise(ERR_LIB_CMS, CMS_R_NO_PUBLIC_KEY);
  785. return -1;
  786. }
  787. if (!ossl_cms_si_check_attributes(si))
  788. return -1;
  789. OBJ_obj2txt(name, sizeof(name), si->digestAlgorithm->algorithm, 0);
  790. (void)ERR_set_mark();
  791. fetched_md = EVP_MD_fetch(libctx, name, propq);
  792. if (fetched_md != NULL)
  793. md = fetched_md;
  794. else
  795. md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
  796. if (md == NULL) {
  797. (void)ERR_clear_last_mark();
  798. ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_DIGEST_ALGORITHM);
  799. return -1;
  800. }
  801. (void)ERR_pop_to_mark();
  802. if (si->mctx == NULL && (si->mctx = EVP_MD_CTX_new()) == NULL) {
  803. ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
  804. goto err;
  805. }
  806. mctx = si->mctx;
  807. if (EVP_DigestVerifyInit_ex(mctx, &si->pctx, EVP_MD_get0_name(md), libctx,
  808. propq, si->pkey, NULL) <= 0)
  809. goto err;
  810. if (!cms_sd_asn1_ctrl(si, 1))
  811. goto err;
  812. alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
  813. ASN1_ITEM_rptr(CMS_Attributes_Verify));
  814. if (abuf == NULL || alen < 0)
  815. goto err;
  816. r = EVP_DigestVerifyUpdate(mctx, abuf, alen);
  817. OPENSSL_free(abuf);
  818. if (r <= 0) {
  819. r = -1;
  820. goto err;
  821. }
  822. r = EVP_DigestVerifyFinal(mctx,
  823. si->signature->data, si->signature->length);
  824. if (r <= 0)
  825. ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
  826. err:
  827. EVP_MD_free(fetched_md);
  828. EVP_MD_CTX_reset(mctx);
  829. return r;
  830. }
  831. /* Create a chain of digest BIOs from a CMS ContentInfo */
  832. BIO *ossl_cms_SignedData_init_bio(CMS_ContentInfo *cms)
  833. {
  834. int i;
  835. CMS_SignedData *sd;
  836. BIO *chain = NULL;
  837. sd = cms_get0_signed(cms);
  838. if (sd == NULL)
  839. return NULL;
  840. if (cms->d.signedData->encapContentInfo->partial)
  841. cms_sd_set_version(sd);
  842. for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
  843. X509_ALGOR *digestAlgorithm;
  844. BIO *mdbio;
  845. digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
  846. mdbio = ossl_cms_DigestAlgorithm_init_bio(digestAlgorithm,
  847. ossl_cms_get0_cmsctx(cms));
  848. if (mdbio == NULL)
  849. goto err;
  850. if (chain != NULL)
  851. BIO_push(chain, mdbio);
  852. else
  853. chain = mdbio;
  854. }
  855. return chain;
  856. err:
  857. BIO_free_all(chain);
  858. return NULL;
  859. }
  860. int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
  861. {
  862. ASN1_OCTET_STRING *os = NULL;
  863. EVP_MD_CTX *mctx = EVP_MD_CTX_new();
  864. EVP_PKEY_CTX *pkctx = NULL;
  865. int r = -1;
  866. unsigned char mval[EVP_MAX_MD_SIZE];
  867. unsigned int mlen;
  868. if (mctx == NULL) {
  869. ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
  870. goto err;
  871. }
  872. /* If we have any signed attributes look for messageDigest value */
  873. if (CMS_signed_get_attr_count(si) >= 0) {
  874. os = CMS_signed_get0_data_by_OBJ(si,
  875. OBJ_nid2obj(NID_pkcs9_messageDigest),
  876. -3, V_ASN1_OCTET_STRING);
  877. if (os == NULL) {
  878. ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
  879. goto err;
  880. }
  881. }
  882. if (!ossl_cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
  883. goto err;
  884. if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {
  885. ERR_raise(ERR_LIB_CMS, CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
  886. goto err;
  887. }
  888. /* If messageDigest found compare it */
  889. if (os != NULL) {
  890. if (mlen != (unsigned int)os->length) {
  891. ERR_raise(ERR_LIB_CMS, CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
  892. goto err;
  893. }
  894. if (memcmp(mval, os->data, mlen)) {
  895. ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
  896. r = 0;
  897. } else {
  898. r = 1;
  899. }
  900. } else {
  901. const EVP_MD *md = EVP_MD_CTX_get0_md(mctx);
  902. const CMS_CTX *ctx = si->cms_ctx;
  903. pkctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
  904. si->pkey,
  905. ossl_cms_ctx_get0_propq(ctx));
  906. if (pkctx == NULL)
  907. goto err;
  908. if (EVP_PKEY_verify_init(pkctx) <= 0)
  909. goto err;
  910. if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)
  911. goto err;
  912. si->pctx = pkctx;
  913. if (!cms_sd_asn1_ctrl(si, 1))
  914. goto err;
  915. r = EVP_PKEY_verify(pkctx, si->signature->data,
  916. si->signature->length, mval, mlen);
  917. if (r <= 0) {
  918. ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
  919. r = 0;
  920. }
  921. }
  922. err:
  923. EVP_PKEY_CTX_free(pkctx);
  924. EVP_MD_CTX_free(mctx);
  925. return r;
  926. }
  927. BIO *CMS_SignedData_verify(CMS_SignedData *sd, BIO *detached_data,
  928. STACK_OF(X509) *scerts, X509_STORE *store,
  929. STACK_OF(X509) *extra, STACK_OF(X509_CRL) *crls,
  930. unsigned int flags,
  931. OSSL_LIB_CTX *libctx, const char *propq)
  932. {
  933. CMS_ContentInfo *ci;
  934. BIO *bio = NULL;
  935. int i, res = 0;
  936. if (sd == NULL) {
  937. ERR_raise(ERR_LIB_CMS, ERR_R_PASSED_NULL_PARAMETER);
  938. return NULL;
  939. }
  940. if ((ci = CMS_ContentInfo_new_ex(libctx, propq)) == NULL)
  941. return NULL;
  942. if ((bio = BIO_new(BIO_s_mem())) == NULL)
  943. goto end;
  944. ci->contentType = OBJ_nid2obj(NID_pkcs7_signed);
  945. ci->d.signedData = sd;
  946. for (i = 0; i < sk_X509_num(extra); i++)
  947. if (!CMS_add1_cert(ci, sk_X509_value(extra, i)))
  948. goto end;
  949. for (i = 0; i < sk_X509_CRL_num(crls); i++)
  950. if (!CMS_add1_crl(ci, sk_X509_CRL_value(crls, i)))
  951. goto end;
  952. res = CMS_verify(ci, scerts, store, detached_data, bio, flags);
  953. end:
  954. if (ci != NULL)
  955. ci->d.signedData = NULL; /* do not indirectly free |sd| */
  956. CMS_ContentInfo_free(ci);
  957. if (!res) {
  958. BIO_free(bio);
  959. bio = NULL;
  960. }
  961. return bio;
  962. }
  963. int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
  964. {
  965. unsigned char *smder = NULL;
  966. int smderlen, r;
  967. smderlen = i2d_X509_ALGORS(algs, &smder);
  968. if (smderlen <= 0)
  969. return 0;
  970. r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
  971. V_ASN1_SEQUENCE, smder, smderlen);
  972. OPENSSL_free(smder);
  973. return r;
  974. }
  975. int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
  976. int algnid, int keysize)
  977. {
  978. X509_ALGOR *alg;
  979. ASN1_INTEGER *key = NULL;
  980. if (keysize > 0) {
  981. key = ASN1_INTEGER_new();
  982. if (key == NULL || !ASN1_INTEGER_set(key, keysize)) {
  983. ASN1_INTEGER_free(key);
  984. return 0;
  985. }
  986. }
  987. alg = ossl_X509_ALGOR_from_nid(algnid, key != NULL ? V_ASN1_INTEGER :
  988. V_ASN1_UNDEF, key);
  989. if (alg == NULL) {
  990. ASN1_INTEGER_free(key);
  991. return 0;
  992. }
  993. if (*algs == NULL)
  994. *algs = sk_X509_ALGOR_new_null();
  995. if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) {
  996. X509_ALGOR_free(alg);
  997. return 0;
  998. }
  999. return 1;
  1000. }
  1001. /* Check to see if a cipher exists and if so add S/MIME capabilities */
  1002. static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
  1003. {
  1004. if (EVP_get_cipherbynid(nid))
  1005. return CMS_add_simple_smimecap(sk, nid, arg);
  1006. return 1;
  1007. }
  1008. static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
  1009. {
  1010. if (EVP_get_digestbynid(nid))
  1011. return CMS_add_simple_smimecap(sk, nid, arg);
  1012. return 1;
  1013. }
  1014. int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
  1015. {
  1016. if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
  1017. || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
  1018. || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
  1019. || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
  1020. || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
  1021. || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
  1022. || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
  1023. || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
  1024. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
  1025. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
  1026. || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
  1027. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))
  1028. return 0;
  1029. return 1;
  1030. }