1
0

cms_sd.c 36 KB

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