cms_lib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * Copyright 2008-2020 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/cms.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. ci = (CMS_ContentInfo *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
  27. (CMS_ContentInfo_it()));
  28. if (ci != NULL && a != NULL)
  29. cms_resolve_libctx(ci);
  30. return ci;
  31. }
  32. int i2d_CMS_ContentInfo(const CMS_ContentInfo *a, unsigned char **out)
  33. {
  34. return ASN1_item_i2d((const ASN1_VALUE *)a, out, (CMS_ContentInfo_it()));
  35. }
  36. CMS_ContentInfo *CMS_ContentInfo_new_with_libctx(OPENSSL_CTX *libctx,
  37. const char *propq)
  38. {
  39. CMS_ContentInfo *ci;
  40. ci = (CMS_ContentInfo *)ASN1_item_new(ASN1_ITEM_rptr(CMS_ContentInfo));
  41. if (ci != NULL) {
  42. ci->ctx.libctx = libctx;
  43. ci->ctx.propq = NULL;
  44. if (propq != NULL) {
  45. ci->ctx.propq = OPENSSL_strdup(propq);
  46. if (ci->ctx.propq == NULL) {
  47. CMS_ContentInfo_free(ci);
  48. ci = NULL;
  49. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  50. }
  51. }
  52. }
  53. return ci;
  54. }
  55. CMS_ContentInfo *CMS_ContentInfo_new(void)
  56. {
  57. return CMS_ContentInfo_new_with_libctx(NULL, NULL);
  58. }
  59. void CMS_ContentInfo_free(CMS_ContentInfo *cms)
  60. {
  61. if (cms != NULL) {
  62. OPENSSL_free(cms->ctx.propq);
  63. ASN1_item_free((ASN1_VALUE *)cms, ASN1_ITEM_rptr(CMS_ContentInfo));
  64. }
  65. }
  66. const CMS_CTX *cms_get0_cmsctx(const CMS_ContentInfo *cms)
  67. {
  68. return cms != NULL ? &cms->ctx : NULL;
  69. }
  70. OPENSSL_CTX *cms_ctx_get0_libctx(const CMS_CTX *ctx)
  71. {
  72. return ctx->libctx;
  73. }
  74. const char *cms_ctx_get0_propq(const CMS_CTX *ctx)
  75. {
  76. return ctx->propq;
  77. }
  78. void cms_resolve_libctx(CMS_ContentInfo *ci)
  79. {
  80. int i;
  81. CMS_CertificateChoices *cch;
  82. STACK_OF(CMS_CertificateChoices) **pcerts;
  83. const CMS_CTX *ctx;
  84. if (ci == NULL)
  85. return;
  86. ctx = cms_get0_cmsctx(ci);
  87. cms_SignerInfos_set_cmsctx(ci);
  88. cms_RecipientInfos_set_cmsctx(ci);
  89. pcerts = cms_get0_certificate_choices(ci);
  90. if (pcerts != NULL) {
  91. for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
  92. cch = sk_CMS_CertificateChoices_value(*pcerts, i);
  93. if (cch->type == CMS_CERTCHOICE_CERT)
  94. x509_set0_libctx(cch->d.certificate, ctx->libctx, ctx->propq);
  95. }
  96. }
  97. }
  98. const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms)
  99. {
  100. return cms->contentType;
  101. }
  102. CMS_ContentInfo *cms_Data_create(OPENSSL_CTX *libctx, const char *propq)
  103. {
  104. CMS_ContentInfo *cms = CMS_ContentInfo_new_with_libctx(libctx, propq);
  105. if (cms != NULL) {
  106. cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
  107. /* Never detached */
  108. CMS_set_detached(cms, 0);
  109. }
  110. return cms;
  111. }
  112. BIO *cms_content_bio(CMS_ContentInfo *cms)
  113. {
  114. ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
  115. if (pos == NULL)
  116. return NULL;
  117. /* If content detached data goes nowhere: create NULL BIO */
  118. if (*pos == NULL)
  119. return BIO_new(BIO_s_null());
  120. /*
  121. * If content not detached and created return memory BIO
  122. */
  123. if (*pos == NULL || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
  124. return BIO_new(BIO_s_mem());
  125. /* Else content was read in: return read only BIO for it */
  126. return BIO_new_mem_buf((*pos)->data, (*pos)->length);
  127. }
  128. BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
  129. {
  130. BIO *cmsbio, *cont;
  131. if (icont)
  132. cont = icont;
  133. else
  134. cont = cms_content_bio(cms);
  135. if (!cont) {
  136. CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT);
  137. return NULL;
  138. }
  139. switch (OBJ_obj2nid(cms->contentType)) {
  140. case NID_pkcs7_data:
  141. return cont;
  142. case NID_pkcs7_signed:
  143. cmsbio = cms_SignedData_init_bio(cms);
  144. break;
  145. case NID_pkcs7_digest:
  146. cmsbio = cms_DigestedData_init_bio(cms);
  147. break;
  148. #ifdef ZLIB
  149. case NID_id_smime_ct_compressedData:
  150. cmsbio = cms_CompressedData_init_bio(cms);
  151. break;
  152. #endif
  153. case NID_pkcs7_encrypted:
  154. cmsbio = cms_EncryptedData_init_bio(cms);
  155. break;
  156. case NID_pkcs7_enveloped:
  157. cmsbio = cms_EnvelopedData_init_bio(cms);
  158. break;
  159. case NID_id_smime_ct_authEnvelopedData:
  160. cmsbio = cms_AuthEnvelopedData_init_bio(cms);
  161. break;
  162. default:
  163. CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE);
  164. goto err;
  165. }
  166. if (cmsbio)
  167. return BIO_push(cmsbio, cont);
  168. err:
  169. if (!icont)
  170. BIO_free(cont);
  171. return NULL;
  172. }
  173. /* unfortunately cannot constify SMIME_write_ASN1() due to this function */
  174. int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
  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. CMSerr(CMS_F_CMS_DATAFINAL, 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 cms_EnvelopedData_final(cms, cmsbio);
  204. case NID_id_smime_ct_authEnvelopedData:
  205. return cms_AuthEnvelopedData_final(cms, cmsbio);
  206. case NID_pkcs7_signed:
  207. return cms_SignedData_final(cms, cmsbio);
  208. case NID_pkcs7_digest:
  209. return cms_DigestedData_do_final(cms, cmsbio, 0);
  210. default:
  211. CMSerr(CMS_F_CMS_DATAFINAL, 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. CMSerr(CMS_F_CMS_GET0_CONTENT, 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. CMSerr(CMS_F_CMS_GET0_ECONTENT_TYPE, 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. CMSerr(CMS_F_CMS_SET_DETACHED, ERR_R_MALLOC_FAILURE);
  327. return 0;
  328. }
  329. /* Create a digest BIO from an X509_ALGOR structure */
  330. BIO *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. const char *alg;
  338. X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
  339. alg = OBJ_nid2sn(OBJ_obj2nid(digestoid));
  340. (void)ERR_set_mark();
  341. fetched_digest = EVP_MD_fetch(ctx->libctx, alg, ctx->propq);
  342. if (fetched_digest != NULL)
  343. digest = fetched_digest;
  344. else
  345. digest = EVP_get_digestbyobj(digestoid);
  346. if (digest == NULL) {
  347. (void)ERR_clear_last_mark();
  348. CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
  349. 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)) {
  355. CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO, 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 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. CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
  379. CMS_R_NO_MATCHING_DIGEST);
  380. return 0;
  381. }
  382. BIO_get_md_ctx(chain, &mtmp);
  383. if (EVP_MD_CTX_type(mtmp) == nid
  384. /*
  385. * Workaround for broken implementations that use signature
  386. * algorithm OID instead of digest.
  387. */
  388. || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)
  389. return EVP_MD_CTX_copy_ex(mctx, mtmp);
  390. chain = BIO_next(chain);
  391. }
  392. }
  393. static STACK_OF(CMS_CertificateChoices)
  394. **cms_get0_certificate_choices(CMS_ContentInfo *cms)
  395. {
  396. switch (OBJ_obj2nid(cms->contentType)) {
  397. case NID_pkcs7_signed:
  398. return &cms->d.signedData->certificates;
  399. case NID_pkcs7_enveloped:
  400. if (cms->d.envelopedData->originatorInfo == NULL)
  401. return NULL;
  402. return &cms->d.envelopedData->originatorInfo->certificates;
  403. case NID_id_smime_ct_authEnvelopedData:
  404. if (cms->d.authEnvelopedData->originatorInfo == NULL)
  405. return NULL;
  406. return &cms->d.authEnvelopedData->originatorInfo->certificates;
  407. default:
  408. CMSerr(CMS_F_CMS_GET0_CERTIFICATE_CHOICES,
  409. CMS_R_UNSUPPORTED_CONTENT_TYPE);
  410. return NULL;
  411. }
  412. }
  413. CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
  414. {
  415. STACK_OF(CMS_CertificateChoices) **pcerts;
  416. CMS_CertificateChoices *cch;
  417. pcerts = cms_get0_certificate_choices(cms);
  418. if (pcerts == NULL)
  419. return NULL;
  420. if (*pcerts == NULL)
  421. *pcerts = sk_CMS_CertificateChoices_new_null();
  422. if (*pcerts == NULL)
  423. return NULL;
  424. cch = M_ASN1_new_of(CMS_CertificateChoices);
  425. if (!cch)
  426. return NULL;
  427. if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
  428. M_ASN1_free_of(cch, CMS_CertificateChoices);
  429. return NULL;
  430. }
  431. return cch;
  432. }
  433. int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
  434. {
  435. CMS_CertificateChoices *cch;
  436. STACK_OF(CMS_CertificateChoices) **pcerts;
  437. int i;
  438. pcerts = cms_get0_certificate_choices(cms);
  439. if (pcerts == NULL)
  440. return 0;
  441. for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
  442. cch = sk_CMS_CertificateChoices_value(*pcerts, i);
  443. if (cch->type == CMS_CERTCHOICE_CERT) {
  444. if (!X509_cmp(cch->d.certificate, cert)) {
  445. CMSerr(CMS_F_CMS_ADD0_CERT,
  446. CMS_R_CERTIFICATE_ALREADY_PRESENT);
  447. return 0;
  448. }
  449. }
  450. }
  451. cch = CMS_add0_CertificateChoices(cms);
  452. if (!cch)
  453. return 0;
  454. cch->type = CMS_CERTCHOICE_CERT;
  455. cch->d.certificate = cert;
  456. return 1;
  457. }
  458. int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
  459. {
  460. int r;
  461. r = CMS_add0_cert(cms, cert);
  462. if (r > 0)
  463. X509_up_ref(cert);
  464. return r;
  465. }
  466. static STACK_OF(CMS_RevocationInfoChoice)
  467. **cms_get0_revocation_choices(CMS_ContentInfo *cms)
  468. {
  469. switch (OBJ_obj2nid(cms->contentType)) {
  470. case NID_pkcs7_signed:
  471. return &cms->d.signedData->crls;
  472. case NID_pkcs7_enveloped:
  473. if (cms->d.envelopedData->originatorInfo == NULL)
  474. return NULL;
  475. return &cms->d.envelopedData->originatorInfo->crls;
  476. case NID_id_smime_ct_authEnvelopedData:
  477. if (cms->d.authEnvelopedData->originatorInfo == NULL)
  478. return NULL;
  479. return &cms->d.authEnvelopedData->originatorInfo->crls;
  480. default:
  481. CMSerr(CMS_F_CMS_GET0_REVOCATION_CHOICES,
  482. CMS_R_UNSUPPORTED_CONTENT_TYPE);
  483. return NULL;
  484. }
  485. }
  486. CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
  487. {
  488. STACK_OF(CMS_RevocationInfoChoice) **pcrls;
  489. CMS_RevocationInfoChoice *rch;
  490. pcrls = cms_get0_revocation_choices(cms);
  491. if (pcrls == NULL)
  492. return NULL;
  493. if (*pcrls == NULL)
  494. *pcrls = sk_CMS_RevocationInfoChoice_new_null();
  495. if (*pcrls == NULL)
  496. return NULL;
  497. rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
  498. if (rch == NULL)
  499. return NULL;
  500. if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
  501. M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
  502. return NULL;
  503. }
  504. return rch;
  505. }
  506. int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
  507. {
  508. CMS_RevocationInfoChoice *rch;
  509. rch = CMS_add0_RevocationInfoChoice(cms);
  510. if (!rch)
  511. return 0;
  512. rch->type = CMS_REVCHOICE_CRL;
  513. rch->d.crl = crl;
  514. return 1;
  515. }
  516. int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
  517. {
  518. int r;
  519. r = CMS_add0_crl(cms, crl);
  520. if (r > 0)
  521. X509_CRL_up_ref(crl);
  522. return r;
  523. }
  524. STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
  525. {
  526. STACK_OF(X509) *certs = NULL;
  527. CMS_CertificateChoices *cch;
  528. STACK_OF(CMS_CertificateChoices) **pcerts;
  529. int i;
  530. pcerts = cms_get0_certificate_choices(cms);
  531. if (pcerts == NULL)
  532. return NULL;
  533. for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
  534. cch = sk_CMS_CertificateChoices_value(*pcerts, i);
  535. if (cch->type == 0) {
  536. if (!X509_add_cert_new(&certs, cch->d.certificate,
  537. X509_ADD_FLAG_UP_REF)) {
  538. sk_X509_pop_free(certs, X509_free);
  539. return NULL;
  540. }
  541. }
  542. }
  543. return certs;
  544. }
  545. STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
  546. {
  547. STACK_OF(X509_CRL) *crls = NULL;
  548. STACK_OF(CMS_RevocationInfoChoice) **pcrls;
  549. CMS_RevocationInfoChoice *rch;
  550. int i;
  551. pcrls = cms_get0_revocation_choices(cms);
  552. if (pcrls == NULL)
  553. return NULL;
  554. for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) {
  555. rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
  556. if (rch->type == 0) {
  557. if (!crls) {
  558. crls = sk_X509_CRL_new_null();
  559. if (!crls)
  560. return NULL;
  561. }
  562. if (!sk_X509_CRL_push(crls, rch->d.crl)) {
  563. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  564. return NULL;
  565. }
  566. X509_CRL_up_ref(rch->d.crl);
  567. }
  568. }
  569. return crls;
  570. }
  571. int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
  572. {
  573. int ret;
  574. ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
  575. if (ret)
  576. return ret;
  577. return ASN1_INTEGER_cmp(ias->serialNumber, X509_get0_serialNumber(cert));
  578. }
  579. int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
  580. {
  581. const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert);
  582. if (cert_keyid == NULL)
  583. return -1;
  584. return ASN1_OCTET_STRING_cmp(keyid, cert_keyid);
  585. }
  586. int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
  587. {
  588. CMS_IssuerAndSerialNumber *ias;
  589. ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
  590. if (!ias)
  591. goto err;
  592. if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
  593. goto err;
  594. if (!ASN1_STRING_copy(ias->serialNumber, X509_get0_serialNumber(cert)))
  595. goto err;
  596. M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
  597. *pias = ias;
  598. return 1;
  599. err:
  600. M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
  601. CMSerr(CMS_F_CMS_SET1_IAS, ERR_R_MALLOC_FAILURE);
  602. return 0;
  603. }
  604. int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
  605. {
  606. ASN1_OCTET_STRING *keyid = NULL;
  607. const ASN1_OCTET_STRING *cert_keyid;
  608. cert_keyid = X509_get0_subject_key_id(cert);
  609. if (cert_keyid == NULL) {
  610. CMSerr(CMS_F_CMS_SET1_KEYID, CMS_R_CERTIFICATE_HAS_NO_KEYID);
  611. return 0;
  612. }
  613. keyid = ASN1_STRING_dup(cert_keyid);
  614. if (!keyid) {
  615. CMSerr(CMS_F_CMS_SET1_KEYID, ERR_R_MALLOC_FAILURE);
  616. return 0;
  617. }
  618. ASN1_OCTET_STRING_free(*pkeyid);
  619. *pkeyid = keyid;
  620. return 1;
  621. }