cms_lib.c 19 KB

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