cms_lib.c 20 KB

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