1
0

cms_lib.c 20 KB

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