cms_lib.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /*
  2. * Copyright 2008-2025 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 <openssl/asn1t.h>
  10. #include <openssl/x509v3.h>
  11. #include <openssl/err.h>
  12. #include <openssl/pem.h>
  13. #include <openssl/bio.h>
  14. #include <openssl/asn1.h>
  15. #include <openssl/cms.h>
  16. #include <openssl/core_names.h>
  17. #include "internal/sizes.h"
  18. #include "internal/cryptlib.h"
  19. #include "crypto/x509.h"
  20. #include "cms_local.h"
  21. #include "internal/cms.h"
  22. static STACK_OF(CMS_CertificateChoices)
  23. **cms_get0_certificate_choices(CMS_ContentInfo *cms);
  24. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(CMS_ContentInfo)
  25. IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
  26. CMS_ContentInfo *d2i_CMS_ContentInfo(CMS_ContentInfo **a,
  27. const unsigned char **in, long len)
  28. {
  29. CMS_ContentInfo *ci;
  30. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(a == NULL ? NULL : *a);
  31. ci = (CMS_ContentInfo *)ASN1_item_d2i_ex((ASN1_VALUE **)a, in, len,
  32. (CMS_ContentInfo_it()),
  33. ossl_cms_ctx_get0_libctx(ctx),
  34. ossl_cms_ctx_get0_propq(ctx));
  35. if (ci != NULL) {
  36. ERR_set_mark();
  37. ossl_cms_resolve_libctx(ci);
  38. ERR_pop_to_mark();
  39. }
  40. return ci;
  41. }
  42. int i2d_CMS_ContentInfo(const CMS_ContentInfo *a, unsigned char **out)
  43. {
  44. return ASN1_item_i2d((const ASN1_VALUE *)a, out, (CMS_ContentInfo_it()));
  45. }
  46. CMS_ContentInfo *CMS_ContentInfo_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
  47. {
  48. CMS_ContentInfo *ci;
  49. ci = (CMS_ContentInfo *)ASN1_item_new_ex(ASN1_ITEM_rptr(CMS_ContentInfo),
  50. libctx, propq);
  51. if (ci != NULL) {
  52. ci->ctx.libctx = libctx;
  53. ci->ctx.propq = NULL;
  54. if (propq != NULL) {
  55. ci->ctx.propq = OPENSSL_strdup(propq);
  56. if (ci->ctx.propq == NULL) {
  57. CMS_ContentInfo_free(ci);
  58. ci = NULL;
  59. }
  60. }
  61. }
  62. return ci;
  63. }
  64. const CMS_CTX *ossl_cms_get0_cmsctx(const CMS_ContentInfo *cms)
  65. {
  66. return cms != NULL ? &cms->ctx : NULL;
  67. }
  68. OSSL_LIB_CTX *ossl_cms_ctx_get0_libctx(const CMS_CTX *ctx)
  69. {
  70. return ctx != NULL ? ctx->libctx : NULL;
  71. }
  72. const char *ossl_cms_ctx_get0_propq(const CMS_CTX *ctx)
  73. {
  74. return ctx != NULL ? ctx->propq : NULL;
  75. }
  76. void ossl_cms_resolve_libctx(CMS_ContentInfo *ci)
  77. {
  78. int i;
  79. CMS_CertificateChoices *cch;
  80. STACK_OF(CMS_CertificateChoices) **pcerts;
  81. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(ci);
  82. OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
  83. const char *propq = ossl_cms_ctx_get0_propq(ctx);
  84. ossl_cms_SignerInfos_set_cmsctx(ci);
  85. ossl_cms_RecipientInfos_set_cmsctx(ci);
  86. pcerts = cms_get0_certificate_choices(ci);
  87. if (pcerts != NULL) {
  88. for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
  89. cch = sk_CMS_CertificateChoices_value(*pcerts, i);
  90. if (cch->type == CMS_CERTCHOICE_CERT)
  91. ossl_x509_set0_libctx(cch->d.certificate, libctx, propq);
  92. }
  93. }
  94. }
  95. const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms)
  96. {
  97. return cms->contentType;
  98. }
  99. CMS_ContentInfo *ossl_cms_Data_create(OSSL_LIB_CTX *libctx, const char *propq)
  100. {
  101. CMS_ContentInfo *cms = CMS_ContentInfo_new_ex(libctx, propq);
  102. if (cms != NULL) {
  103. cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
  104. /* Never detached */
  105. CMS_set_detached(cms, 0);
  106. }
  107. return cms;
  108. }
  109. BIO *ossl_cms_content_bio(CMS_ContentInfo *cms)
  110. {
  111. ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
  112. if (pos == NULL)
  113. return NULL;
  114. /* If content detached data goes nowhere: create NULL BIO */
  115. if (*pos == NULL)
  116. return BIO_new(BIO_s_null());
  117. /*
  118. * If content not detached and created return memory BIO
  119. */
  120. if (*pos == NULL || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
  121. return BIO_new(BIO_s_mem());
  122. /* Else content was read in: return read only BIO for it */
  123. return BIO_new_mem_buf((*pos)->data, (*pos)->length);
  124. }
  125. BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
  126. {
  127. BIO *cmsbio, *cont;
  128. if (icont)
  129. cont = icont;
  130. else
  131. cont = ossl_cms_content_bio(cms);
  132. if (!cont) {
  133. ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT);
  134. return NULL;
  135. }
  136. switch (OBJ_obj2nid(cms->contentType)) {
  137. case NID_pkcs7_data:
  138. return cont;
  139. case NID_pkcs7_signed:
  140. cmsbio = ossl_cms_SignedData_init_bio(cms);
  141. break;
  142. case NID_pkcs7_digest:
  143. cmsbio = ossl_cms_DigestedData_init_bio(cms);
  144. break;
  145. #ifndef OPENSSL_NO_ZLIB
  146. case NID_id_smime_ct_compressedData:
  147. cmsbio = ossl_cms_CompressedData_init_bio(cms);
  148. break;
  149. #endif
  150. case NID_pkcs7_encrypted:
  151. cmsbio = ossl_cms_EncryptedData_init_bio(cms);
  152. break;
  153. case NID_pkcs7_enveloped:
  154. cmsbio = ossl_cms_EnvelopedData_init_bio(cms);
  155. break;
  156. case NID_id_smime_ct_authEnvelopedData:
  157. cmsbio = ossl_cms_AuthEnvelopedData_init_bio(cms);
  158. break;
  159. default:
  160. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_TYPE);
  161. goto err;
  162. }
  163. if (cmsbio)
  164. return BIO_push(cmsbio, cont);
  165. err:
  166. if (!icont)
  167. BIO_free(cont);
  168. return NULL;
  169. }
  170. /* unfortunately cannot constify SMIME_write_ASN1() due to this function */
  171. int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
  172. {
  173. return ossl_cms_DataFinal(cms, cmsbio, NULL, 0);
  174. }
  175. int ossl_cms_DataFinal(CMS_ContentInfo *cms, BIO *cmsbio,
  176. const unsigned char *precomp_md,
  177. unsigned int precomp_mdlen)
  178. {
  179. ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
  180. if (pos == NULL)
  181. return 0;
  182. /* If embedded content find memory BIO and set content */
  183. if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) {
  184. BIO *mbio;
  185. unsigned char *cont;
  186. long contlen;
  187. mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
  188. if (!mbio) {
  189. ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND);
  190. return 0;
  191. }
  192. contlen = BIO_get_mem_data(mbio, &cont);
  193. /* Set bio as read only so its content can't be clobbered */
  194. BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
  195. BIO_set_mem_eof_return(mbio, 0);
  196. ASN1_STRING_set0(*pos, cont, contlen);
  197. (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
  198. }
  199. switch (OBJ_obj2nid(cms->contentType)) {
  200. case NID_pkcs7_data:
  201. case NID_pkcs7_encrypted:
  202. case NID_id_smime_ct_compressedData:
  203. /* Nothing to do */
  204. return 1;
  205. case NID_pkcs7_enveloped:
  206. return ossl_cms_EnvelopedData_final(cms, cmsbio);
  207. case NID_id_smime_ct_authEnvelopedData:
  208. return ossl_cms_AuthEnvelopedData_final(cms, cmsbio);
  209. case NID_pkcs7_signed:
  210. return ossl_cms_SignedData_final(cms, cmsbio, precomp_md, precomp_mdlen);
  211. case NID_pkcs7_digest:
  212. return ossl_cms_DigestedData_do_final(cms, cmsbio, 0);
  213. default:
  214. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_TYPE);
  215. return 0;
  216. }
  217. }
  218. /*
  219. * Return an OCTET STRING pointer to content. This allows it to be accessed
  220. * or set later.
  221. */
  222. ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
  223. {
  224. switch (OBJ_obj2nid(cms->contentType)) {
  225. case NID_pkcs7_data:
  226. return &cms->d.data;
  227. case NID_pkcs7_signed:
  228. return &cms->d.signedData->encapContentInfo->eContent;
  229. case NID_pkcs7_enveloped:
  230. return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
  231. case NID_pkcs7_digest:
  232. return &cms->d.digestedData->encapContentInfo->eContent;
  233. case NID_pkcs7_encrypted:
  234. return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
  235. case NID_id_smime_ct_authEnvelopedData:
  236. return &cms->d.authEnvelopedData->authEncryptedContentInfo
  237. ->encryptedContent;
  238. case NID_id_smime_ct_authData:
  239. return &cms->d.authenticatedData->encapContentInfo->eContent;
  240. case NID_id_smime_ct_compressedData:
  241. return &cms->d.compressedData->encapContentInfo->eContent;
  242. default:
  243. if (cms->d.other->type == V_ASN1_OCTET_STRING)
  244. return &cms->d.other->value.octet_string;
  245. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
  246. return NULL;
  247. }
  248. }
  249. /*
  250. * Return an ASN1_OBJECT pointer to content type. This allows it to be
  251. * accessed or set later.
  252. */
  253. static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
  254. {
  255. switch (OBJ_obj2nid(cms->contentType)) {
  256. case NID_pkcs7_signed:
  257. return &cms->d.signedData->encapContentInfo->eContentType;
  258. case NID_pkcs7_enveloped:
  259. return &cms->d.envelopedData->encryptedContentInfo->contentType;
  260. case NID_pkcs7_digest:
  261. return &cms->d.digestedData->encapContentInfo->eContentType;
  262. case NID_pkcs7_encrypted:
  263. return &cms->d.encryptedData->encryptedContentInfo->contentType;
  264. case NID_id_smime_ct_authEnvelopedData:
  265. return &cms->d.authEnvelopedData->authEncryptedContentInfo
  266. ->contentType;
  267. case NID_id_smime_ct_authData:
  268. return &cms->d.authenticatedData->encapContentInfo->eContentType;
  269. case NID_id_smime_ct_compressedData:
  270. return &cms->d.compressedData->encapContentInfo->eContentType;
  271. default:
  272. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
  273. return NULL;
  274. }
  275. }
  276. const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
  277. {
  278. ASN1_OBJECT **petype;
  279. petype = cms_get0_econtent_type(cms);
  280. if (petype)
  281. return *petype;
  282. return NULL;
  283. }
  284. int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
  285. {
  286. ASN1_OBJECT **petype, *etype;
  287. petype = cms_get0_econtent_type(cms);
  288. if (petype == NULL)
  289. return 0;
  290. if (oid == NULL)
  291. return 1;
  292. etype = OBJ_dup(oid);
  293. if (etype == NULL)
  294. return 0;
  295. ASN1_OBJECT_free(*petype);
  296. *petype = etype;
  297. return 1;
  298. }
  299. int CMS_is_detached(CMS_ContentInfo *cms)
  300. {
  301. ASN1_OCTET_STRING **pos;
  302. pos = CMS_get0_content(cms);
  303. if (pos == NULL)
  304. return -1;
  305. if (*pos != NULL)
  306. return 0;
  307. return 1;
  308. }
  309. int CMS_set_detached(CMS_ContentInfo *cms, int detached)
  310. {
  311. ASN1_OCTET_STRING **pos;
  312. pos = CMS_get0_content(cms);
  313. if (pos == NULL)
  314. return 0;
  315. if (detached) {
  316. ASN1_OCTET_STRING_free(*pos);
  317. *pos = NULL;
  318. return 1;
  319. }
  320. if (*pos == NULL)
  321. *pos = ASN1_OCTET_STRING_new();
  322. if (*pos != NULL) {
  323. /*
  324. * NB: special flag to show content is created and not read in.
  325. */
  326. (*pos)->flags |= ASN1_STRING_FLAG_CONT;
  327. return 1;
  328. }
  329. ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
  330. return 0;
  331. }
  332. /* Create a digest BIO from an X509_ALGOR structure */
  333. BIO *ossl_cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm,
  334. const CMS_CTX *ctx)
  335. {
  336. BIO *mdbio = NULL;
  337. const ASN1_OBJECT *digestoid;
  338. const EVP_MD *digest = NULL;
  339. EVP_MD *fetched_digest = NULL;
  340. char alg[OSSL_MAX_NAME_SIZE];
  341. size_t xof_len = 0;
  342. X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
  343. OBJ_obj2txt(alg, sizeof(alg), digestoid, 0);
  344. (void)ERR_set_mark();
  345. fetched_digest = EVP_MD_fetch(ossl_cms_ctx_get0_libctx(ctx), alg,
  346. ossl_cms_ctx_get0_propq(ctx));
  347. if (fetched_digest != NULL)
  348. digest = fetched_digest;
  349. else
  350. digest = EVP_get_digestbyobj(digestoid);
  351. if (digest == NULL) {
  352. (void)ERR_clear_last_mark();
  353. ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_DIGEST_ALGORITHM);
  354. goto err;
  355. }
  356. (void)ERR_pop_to_mark();
  357. mdbio = BIO_new(BIO_f_md());
  358. if (mdbio == NULL || BIO_set_md(mdbio, digest) <= 0) {
  359. ERR_raise(ERR_LIB_CMS, CMS_R_MD_BIO_INIT_ERROR);
  360. goto err;
  361. }
  362. if (EVP_MD_xof(digest)) {
  363. if (EVP_MD_is_a(digest, SN_shake128))
  364. xof_len = 32;
  365. else if (EVP_MD_is_a(digest, SN_shake256))
  366. xof_len = 64;
  367. if (xof_len > 0) {
  368. EVP_MD_CTX *mdctx;
  369. OSSL_PARAM params[2];
  370. if (BIO_get_md_ctx(mdbio, &mdctx) <= 0 || mdctx == NULL)
  371. goto err;
  372. params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN,
  373. &xof_len);
  374. params[1] = OSSL_PARAM_construct_end();
  375. if (!EVP_MD_CTX_set_params(mdctx, params))
  376. goto err;
  377. }
  378. }
  379. EVP_MD_free(fetched_digest);
  380. return mdbio;
  381. err:
  382. EVP_MD_free(fetched_digest);
  383. BIO_free(mdbio);
  384. return NULL;
  385. }
  386. /* Locate a message digest content from a BIO chain based on SignerInfo */
  387. int ossl_cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
  388. X509_ALGOR *mdalg)
  389. {
  390. int nid;
  391. const ASN1_OBJECT *mdoid;
  392. X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
  393. nid = OBJ_obj2nid(mdoid);
  394. /* Look for digest type to match signature */
  395. for (;;) {
  396. EVP_MD_CTX *mtmp;
  397. chain = BIO_find_type(chain, BIO_TYPE_MD);
  398. if (chain == NULL) {
  399. ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_DIGEST);
  400. return 0;
  401. }
  402. BIO_get_md_ctx(chain, &mtmp);
  403. if (EVP_MD_CTX_get_type(mtmp) == nid
  404. /*
  405. * Workaround for broken implementations that use signature
  406. * algorithm OID instead of digest.
  407. */
  408. || EVP_MD_get_pkey_type(EVP_MD_CTX_get0_md(mtmp)) == nid)
  409. return EVP_MD_CTX_copy_ex(mctx, mtmp);
  410. chain = BIO_next(chain);
  411. }
  412. }
  413. static STACK_OF(CMS_CertificateChoices)
  414. **cms_get0_certificate_choices(CMS_ContentInfo *cms)
  415. {
  416. switch (OBJ_obj2nid(cms->contentType)) {
  417. case NID_pkcs7_signed:
  418. return &cms->d.signedData->certificates;
  419. case NID_pkcs7_enveloped:
  420. if (cms->d.envelopedData->originatorInfo == NULL)
  421. return NULL;
  422. return &cms->d.envelopedData->originatorInfo->certificates;
  423. case NID_id_smime_ct_authEnvelopedData:
  424. if (cms->d.authEnvelopedData->originatorInfo == NULL)
  425. return NULL;
  426. return &cms->d.authEnvelopedData->originatorInfo->certificates;
  427. default:
  428. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
  429. return NULL;
  430. }
  431. }
  432. CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
  433. {
  434. STACK_OF(CMS_CertificateChoices) **pcerts;
  435. CMS_CertificateChoices *cch;
  436. pcerts = cms_get0_certificate_choices(cms);
  437. if (pcerts == NULL)
  438. return NULL;
  439. if (*pcerts == NULL)
  440. *pcerts = sk_CMS_CertificateChoices_new_null();
  441. if (*pcerts == NULL)
  442. return NULL;
  443. cch = M_ASN1_new_of(CMS_CertificateChoices);
  444. if (!cch)
  445. return NULL;
  446. if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
  447. M_ASN1_free_of(cch, CMS_CertificateChoices);
  448. return NULL;
  449. }
  450. return cch;
  451. }
  452. int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
  453. {
  454. CMS_CertificateChoices *cch;
  455. STACK_OF(CMS_CertificateChoices) **pcerts;
  456. int i;
  457. pcerts = cms_get0_certificate_choices(cms);
  458. if (pcerts == NULL)
  459. return 0;
  460. for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
  461. cch = sk_CMS_CertificateChoices_value(*pcerts, i);
  462. if (cch->type == CMS_CERTCHOICE_CERT) {
  463. if (X509_cmp(cch->d.certificate, cert) == 0) {
  464. X509_free(cert);
  465. return 1; /* cert already present */
  466. }
  467. }
  468. }
  469. cch = CMS_add0_CertificateChoices(cms);
  470. if (!cch)
  471. return 0;
  472. cch->type = CMS_CERTCHOICE_CERT;
  473. cch->d.certificate = cert;
  474. return 1;
  475. }
  476. int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
  477. {
  478. if (!X509_up_ref(cert))
  479. return 0;
  480. if (CMS_add0_cert(cms, cert))
  481. return 1;
  482. X509_free(cert);
  483. return 0;
  484. }
  485. static STACK_OF(CMS_RevocationInfoChoice)
  486. **cms_get0_revocation_choices(CMS_ContentInfo *cms)
  487. {
  488. switch (OBJ_obj2nid(cms->contentType)) {
  489. case NID_pkcs7_signed:
  490. return &cms->d.signedData->crls;
  491. case NID_pkcs7_enveloped:
  492. if (cms->d.envelopedData->originatorInfo == NULL)
  493. return NULL;
  494. return &cms->d.envelopedData->originatorInfo->crls;
  495. case NID_id_smime_ct_authEnvelopedData:
  496. if (cms->d.authEnvelopedData->originatorInfo == NULL)
  497. return NULL;
  498. return &cms->d.authEnvelopedData->originatorInfo->crls;
  499. default:
  500. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
  501. return NULL;
  502. }
  503. }
  504. CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
  505. {
  506. STACK_OF(CMS_RevocationInfoChoice) **pcrls;
  507. CMS_RevocationInfoChoice *rch;
  508. pcrls = cms_get0_revocation_choices(cms);
  509. if (pcrls == NULL)
  510. return NULL;
  511. if (*pcrls == NULL)
  512. *pcrls = sk_CMS_RevocationInfoChoice_new_null();
  513. if (*pcrls == NULL)
  514. return NULL;
  515. rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
  516. if (rch == NULL)
  517. return NULL;
  518. if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
  519. M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
  520. return NULL;
  521. }
  522. return rch;
  523. }
  524. int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
  525. {
  526. CMS_RevocationInfoChoice *rch = CMS_add0_RevocationInfoChoice(cms);
  527. if (rch == NULL)
  528. return 0;
  529. rch->type = CMS_REVCHOICE_CRL;
  530. rch->d.crl = crl;
  531. return 1;
  532. }
  533. int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
  534. {
  535. if (!X509_CRL_up_ref(crl))
  536. return 0;
  537. if (CMS_add0_crl(cms, crl))
  538. return 1;
  539. X509_CRL_free(crl);
  540. return 0;
  541. }
  542. STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
  543. {
  544. STACK_OF(X509) *certs = NULL;
  545. if (!ossl_cms_get1_certs_ex(cms, &certs))
  546. return NULL;
  547. if (sk_X509_num(certs) == 0) {
  548. sk_X509_free(certs);
  549. return NULL;
  550. }
  551. return certs;
  552. }
  553. int ossl_cms_get1_certs_ex(CMS_ContentInfo *cms, STACK_OF(X509) **certs)
  554. {
  555. CMS_CertificateChoices *cch;
  556. STACK_OF(CMS_CertificateChoices) **pcerts;
  557. int i, n;
  558. if (certs == NULL)
  559. return 0;
  560. *certs = NULL;
  561. pcerts = cms_get0_certificate_choices(cms);
  562. if (pcerts == NULL)
  563. return 0;
  564. /* make sure to return NULL *certs only on error */
  565. n = sk_CMS_CertificateChoices_num(*pcerts);
  566. if ((*certs = sk_X509_new_reserve(NULL, n)) == NULL)
  567. return 0;
  568. for (i = 0; i < n; i++) {
  569. cch = sk_CMS_CertificateChoices_value(*pcerts, i);
  570. if (cch->type == 0) {
  571. if (!X509_add_cert(*certs, cch->d.certificate,
  572. X509_ADD_FLAG_UP_REF)) {
  573. OSSL_STACK_OF_X509_free(*certs);
  574. *certs = NULL;
  575. return 0;
  576. }
  577. }
  578. }
  579. return 1;
  580. }
  581. STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
  582. {
  583. STACK_OF(X509_CRL) *crls = NULL;
  584. if (!ossl_cms_get1_crls_ex(cms, &crls))
  585. return NULL;
  586. if (sk_X509_CRL_num(crls) == 0) {
  587. sk_X509_CRL_free(crls);
  588. return NULL;
  589. }
  590. return crls;
  591. }
  592. int ossl_cms_get1_crls_ex(CMS_ContentInfo *cms, STACK_OF(X509_CRL) **crls)
  593. {
  594. STACK_OF(CMS_RevocationInfoChoice) **pcrls;
  595. CMS_RevocationInfoChoice *rch;
  596. int i, n;
  597. if (crls == NULL)
  598. return 0;
  599. *crls = NULL;
  600. pcrls = cms_get0_revocation_choices(cms);
  601. if (pcrls == NULL)
  602. return 0;
  603. /* make sure to return NULL *crls only on error */
  604. n = sk_CMS_RevocationInfoChoice_num(*pcrls);
  605. if ((*crls = sk_X509_CRL_new_reserve(NULL, n)) == NULL)
  606. return 0;
  607. for (i = 0; i < n; i++) {
  608. rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
  609. if (rch->type == 0) {
  610. if (!X509_CRL_up_ref(rch->d.crl)
  611. || !ossl_assert(sk_X509_CRL_push(*crls, rch->d.crl))) {
  612. /* push cannot fail on reserved stack */
  613. sk_X509_CRL_pop_free(*crls, X509_CRL_free);
  614. *crls = NULL;
  615. return 0;
  616. }
  617. }
  618. }
  619. return 1;
  620. }
  621. int ossl_cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
  622. {
  623. int ret;
  624. ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
  625. if (ret)
  626. return ret;
  627. return ASN1_INTEGER_cmp(ias->serialNumber, X509_get0_serialNumber(cert));
  628. }
  629. int ossl_cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
  630. {
  631. const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert);
  632. if (cert_keyid == NULL)
  633. return -1;
  634. return ASN1_OCTET_STRING_cmp(keyid, cert_keyid);
  635. }
  636. int ossl_cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
  637. {
  638. CMS_IssuerAndSerialNumber *ias;
  639. ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
  640. if (!ias) {
  641. ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
  642. goto err;
  643. }
  644. if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert))) {
  645. ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB);
  646. goto err;
  647. }
  648. if (!ASN1_STRING_copy(ias->serialNumber, X509_get0_serialNumber(cert))) {
  649. ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
  650. goto err;
  651. }
  652. M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
  653. *pias = ias;
  654. return 1;
  655. err:
  656. M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
  657. return 0;
  658. }
  659. int ossl_cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
  660. {
  661. ASN1_OCTET_STRING *keyid = NULL;
  662. const ASN1_OCTET_STRING *cert_keyid;
  663. cert_keyid = X509_get0_subject_key_id(cert);
  664. if (cert_keyid == NULL) {
  665. ERR_raise(ERR_LIB_CMS, CMS_R_CERTIFICATE_HAS_NO_KEYID);
  666. return 0;
  667. }
  668. keyid = ASN1_STRING_dup(cert_keyid);
  669. if (!keyid) {
  670. ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
  671. return 0;
  672. }
  673. ASN1_OCTET_STRING_free(*pkeyid);
  674. *pkeyid = keyid;
  675. return 1;
  676. }
  677. CMS_EnvelopedData *ossl_cms_sign_encrypt(BIO *data, X509 *sign_cert, STACK_OF(X509) *certs,
  678. EVP_PKEY *sign_key, unsigned int sign_flags,
  679. STACK_OF(X509) *enc_recip, const EVP_CIPHER *cipher,
  680. unsigned int enc_flags, OSSL_LIB_CTX *libctx,
  681. const char *propq)
  682. {
  683. CMS_EnvelopedData *evd = NULL;
  684. BIO *privbio = NULL, *signbio = NULL;
  685. CMS_ContentInfo *signcms = NULL, *evpcms = NULL;
  686. if (data == NULL || sign_key == NULL || sign_cert == NULL || enc_recip == NULL) {
  687. ERR_raise(ERR_LIB_CMS, ERR_R_PASSED_NULL_PARAMETER);
  688. return NULL;
  689. }
  690. signcms = CMS_sign_ex(sign_cert, sign_key, certs, data, sign_flags, libctx, propq);
  691. if (signcms == NULL)
  692. goto err;
  693. signbio = BIO_new(BIO_s_mem());
  694. if (signbio == NULL
  695. || ASN1_item_i2d_bio(ASN1_ITEM_rptr(CMS_SignedData), signbio, signcms->d.signedData) <= 0)
  696. goto err;
  697. evpcms = CMS_encrypt_ex(enc_recip, signbio, cipher, enc_flags, libctx, propq);
  698. if (evpcms == NULL)
  699. goto err;
  700. evd = CMS_EnvelopedData_dup(evpcms->d.envelopedData);
  701. err:
  702. BIO_free(privbio);
  703. BIO_free(signbio);
  704. CMS_ContentInfo_free(signcms);
  705. CMS_ContentInfo_free(evpcms);
  706. return evd;
  707. }