cms_lib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /* crypto/cms/cms_lib.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. */
  54. #include <openssl/asn1t.h>
  55. #include <openssl/x509v3.h>
  56. #include <openssl/err.h>
  57. #include <openssl/pem.h>
  58. #include <openssl/bio.h>
  59. #include <openssl/asn1.h>
  60. #include "cms.h"
  61. #include "cms_lcl.h"
  62. IMPLEMENT_ASN1_FUNCTIONS(CMS_ContentInfo)
  63. IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
  64. DECLARE_ASN1_ITEM(CMS_CertificateChoices)
  65. DECLARE_ASN1_ITEM(CMS_RevocationInfoChoice)
  66. DECLARE_STACK_OF(CMS_CertificateChoices)
  67. DECLARE_STACK_OF(CMS_RevocationInfoChoice)
  68. const ASN1_OBJECT *CMS_get0_type(CMS_ContentInfo *cms)
  69. {
  70. return cms->contentType;
  71. }
  72. CMS_ContentInfo *cms_Data_create(void)
  73. {
  74. CMS_ContentInfo *cms;
  75. cms = CMS_ContentInfo_new();
  76. if (cms) {
  77. cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
  78. /* Never detached */
  79. CMS_set_detached(cms, 0);
  80. }
  81. return cms;
  82. }
  83. BIO *cms_content_bio(CMS_ContentInfo *cms)
  84. {
  85. ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
  86. if (!pos)
  87. return NULL;
  88. /* If content detached data goes nowhere: create NULL BIO */
  89. if (!*pos)
  90. return BIO_new(BIO_s_null());
  91. /*
  92. * If content not detached and created return memory BIO
  93. */
  94. if (!*pos || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
  95. return BIO_new(BIO_s_mem());
  96. /* Else content was read in: return read only BIO for it */
  97. return BIO_new_mem_buf((*pos)->data, (*pos)->length);
  98. }
  99. BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
  100. {
  101. BIO *cmsbio, *cont;
  102. if (icont)
  103. cont = icont;
  104. else
  105. cont = cms_content_bio(cms);
  106. if (!cont) {
  107. CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT);
  108. return NULL;
  109. }
  110. switch (OBJ_obj2nid(cms->contentType)) {
  111. case NID_pkcs7_data:
  112. return cont;
  113. case NID_pkcs7_signed:
  114. cmsbio = cms_SignedData_init_bio(cms);
  115. break;
  116. case NID_pkcs7_digest:
  117. cmsbio = cms_DigestedData_init_bio(cms);
  118. break;
  119. #ifdef ZLIB
  120. case NID_id_smime_ct_compressedData:
  121. cmsbio = cms_CompressedData_init_bio(cms);
  122. break;
  123. #endif
  124. case NID_pkcs7_encrypted:
  125. cmsbio = cms_EncryptedData_init_bio(cms);
  126. break;
  127. case NID_pkcs7_enveloped:
  128. cmsbio = cms_EnvelopedData_init_bio(cms);
  129. break;
  130. default:
  131. CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE);
  132. return NULL;
  133. }
  134. if (cmsbio)
  135. return BIO_push(cmsbio, cont);
  136. if (!icont)
  137. BIO_free(cont);
  138. return NULL;
  139. }
  140. int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
  141. {
  142. ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
  143. if (!pos)
  144. return 0;
  145. /* If ebmedded content find memory BIO and set content */
  146. if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) {
  147. BIO *mbio;
  148. unsigned char *cont;
  149. long contlen;
  150. mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
  151. if (!mbio) {
  152. CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_CONTENT_NOT_FOUND);
  153. return 0;
  154. }
  155. contlen = BIO_get_mem_data(mbio, &cont);
  156. /* Set bio as read only so its content can't be clobbered */
  157. BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
  158. BIO_set_mem_eof_return(mbio, 0);
  159. ASN1_STRING_set0(*pos, cont, contlen);
  160. (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
  161. }
  162. switch (OBJ_obj2nid(cms->contentType)) {
  163. case NID_pkcs7_data:
  164. case NID_pkcs7_enveloped:
  165. case NID_pkcs7_encrypted:
  166. case NID_id_smime_ct_compressedData:
  167. /* Nothing to do */
  168. return 1;
  169. case NID_pkcs7_signed:
  170. return cms_SignedData_final(cms, cmsbio);
  171. case NID_pkcs7_digest:
  172. return cms_DigestedData_do_final(cms, cmsbio, 0);
  173. default:
  174. CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_UNSUPPORTED_TYPE);
  175. return 0;
  176. }
  177. }
  178. /*
  179. * Return an OCTET STRING pointer to content. This allows it to be accessed
  180. * or set later.
  181. */
  182. ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
  183. {
  184. switch (OBJ_obj2nid(cms->contentType)) {
  185. case NID_pkcs7_data:
  186. return &cms->d.data;
  187. case NID_pkcs7_signed:
  188. return &cms->d.signedData->encapContentInfo->eContent;
  189. case NID_pkcs7_enveloped:
  190. return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
  191. case NID_pkcs7_digest:
  192. return &cms->d.digestedData->encapContentInfo->eContent;
  193. case NID_pkcs7_encrypted:
  194. return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
  195. case NID_id_smime_ct_authData:
  196. return &cms->d.authenticatedData->encapContentInfo->eContent;
  197. case NID_id_smime_ct_compressedData:
  198. return &cms->d.compressedData->encapContentInfo->eContent;
  199. default:
  200. if (cms->d.other->type == V_ASN1_OCTET_STRING)
  201. return &cms->d.other->value.octet_string;
  202. CMSerr(CMS_F_CMS_GET0_CONTENT, CMS_R_UNSUPPORTED_CONTENT_TYPE);
  203. return NULL;
  204. }
  205. }
  206. /*
  207. * Return an ASN1_OBJECT pointer to content type. This allows it to be
  208. * accessed or set later.
  209. */
  210. static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
  211. {
  212. switch (OBJ_obj2nid(cms->contentType)) {
  213. case NID_pkcs7_signed:
  214. return &cms->d.signedData->encapContentInfo->eContentType;
  215. case NID_pkcs7_enveloped:
  216. return &cms->d.envelopedData->encryptedContentInfo->contentType;
  217. case NID_pkcs7_digest:
  218. return &cms->d.digestedData->encapContentInfo->eContentType;
  219. case NID_pkcs7_encrypted:
  220. return &cms->d.encryptedData->encryptedContentInfo->contentType;
  221. case NID_id_smime_ct_authData:
  222. return &cms->d.authenticatedData->encapContentInfo->eContentType;
  223. case NID_id_smime_ct_compressedData:
  224. return &cms->d.compressedData->encapContentInfo->eContentType;
  225. default:
  226. CMSerr(CMS_F_CMS_GET0_ECONTENT_TYPE, CMS_R_UNSUPPORTED_CONTENT_TYPE);
  227. return NULL;
  228. }
  229. }
  230. const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
  231. {
  232. ASN1_OBJECT **petype;
  233. petype = cms_get0_econtent_type(cms);
  234. if (petype)
  235. return *petype;
  236. return NULL;
  237. }
  238. int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
  239. {
  240. ASN1_OBJECT **petype, *etype;
  241. petype = cms_get0_econtent_type(cms);
  242. if (!petype)
  243. return 0;
  244. if (!oid)
  245. return 1;
  246. etype = OBJ_dup(oid);
  247. if (!etype)
  248. return 0;
  249. ASN1_OBJECT_free(*petype);
  250. *petype = etype;
  251. return 1;
  252. }
  253. int CMS_is_detached(CMS_ContentInfo *cms)
  254. {
  255. ASN1_OCTET_STRING **pos;
  256. pos = CMS_get0_content(cms);
  257. if (!pos)
  258. return -1;
  259. if (*pos)
  260. return 0;
  261. return 1;
  262. }
  263. int CMS_set_detached(CMS_ContentInfo *cms, int detached)
  264. {
  265. ASN1_OCTET_STRING **pos;
  266. pos = CMS_get0_content(cms);
  267. if (!pos)
  268. return 0;
  269. if (detached) {
  270. if (*pos) {
  271. ASN1_OCTET_STRING_free(*pos);
  272. *pos = NULL;
  273. }
  274. return 1;
  275. }
  276. if (!*pos)
  277. *pos = ASN1_OCTET_STRING_new();
  278. if (*pos) {
  279. /*
  280. * NB: special flag to show content is created and not read in.
  281. */
  282. (*pos)->flags |= ASN1_STRING_FLAG_CONT;
  283. return 1;
  284. }
  285. CMSerr(CMS_F_CMS_SET_DETACHED, ERR_R_MALLOC_FAILURE);
  286. return 0;
  287. }
  288. /* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */
  289. void cms_DigestAlgorithm_set(X509_ALGOR *alg, const EVP_MD *md)
  290. {
  291. int param_type;
  292. if (md->flags & EVP_MD_FLAG_DIGALGID_ABSENT)
  293. param_type = V_ASN1_UNDEF;
  294. else
  295. param_type = V_ASN1_NULL;
  296. X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_type(md)), param_type, NULL);
  297. }
  298. /* Create a digest BIO from an X509_ALGOR structure */
  299. BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm)
  300. {
  301. BIO *mdbio = NULL;
  302. ASN1_OBJECT *digestoid;
  303. const EVP_MD *digest;
  304. X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
  305. digest = EVP_get_digestbyobj(digestoid);
  306. if (!digest) {
  307. CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
  308. CMS_R_UNKNOWN_DIGEST_ALGORIHM);
  309. goto err;
  310. }
  311. mdbio = BIO_new(BIO_f_md());
  312. if (!mdbio || !BIO_set_md(mdbio, digest)) {
  313. CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO, CMS_R_MD_BIO_INIT_ERROR);
  314. goto err;
  315. }
  316. return mdbio;
  317. err:
  318. if (mdbio)
  319. BIO_free(mdbio);
  320. return NULL;
  321. }
  322. /* Locate a message digest content from a BIO chain based on SignerInfo */
  323. int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
  324. X509_ALGOR *mdalg)
  325. {
  326. int nid;
  327. ASN1_OBJECT *mdoid;
  328. X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
  329. nid = OBJ_obj2nid(mdoid);
  330. /* Look for digest type to match signature */
  331. for (;;) {
  332. EVP_MD_CTX *mtmp;
  333. chain = BIO_find_type(chain, BIO_TYPE_MD);
  334. if (chain == NULL) {
  335. CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
  336. CMS_R_NO_MATCHING_DIGEST);
  337. return 0;
  338. }
  339. BIO_get_md_ctx(chain, &mtmp);
  340. if (EVP_MD_CTX_type(mtmp) == nid
  341. /*
  342. * Workaround for broken implementations that use signature
  343. * algorithm OID instead of digest.
  344. */
  345. || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)
  346. return EVP_MD_CTX_copy_ex(mctx, mtmp);
  347. chain = BIO_next(chain);
  348. }
  349. }
  350. static STACK_OF(CMS_CertificateChoices)
  351. **cms_get0_certificate_choices(CMS_ContentInfo *cms)
  352. {
  353. switch (OBJ_obj2nid(cms->contentType)) {
  354. case NID_pkcs7_signed:
  355. return &cms->d.signedData->certificates;
  356. case NID_pkcs7_enveloped:
  357. if (cms->d.envelopedData->originatorInfo == NULL)
  358. return NULL;
  359. return &cms->d.envelopedData->originatorInfo->certificates;
  360. default:
  361. CMSerr(CMS_F_CMS_GET0_CERTIFICATE_CHOICES,
  362. CMS_R_UNSUPPORTED_CONTENT_TYPE);
  363. return NULL;
  364. }
  365. }
  366. CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
  367. {
  368. STACK_OF(CMS_CertificateChoices) **pcerts;
  369. CMS_CertificateChoices *cch;
  370. pcerts = cms_get0_certificate_choices(cms);
  371. if (!pcerts)
  372. return NULL;
  373. if (!*pcerts)
  374. *pcerts = sk_CMS_CertificateChoices_new_null();
  375. if (!*pcerts)
  376. return NULL;
  377. cch = M_ASN1_new_of(CMS_CertificateChoices);
  378. if (!cch)
  379. return NULL;
  380. if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
  381. M_ASN1_free_of(cch, CMS_CertificateChoices);
  382. return NULL;
  383. }
  384. return cch;
  385. }
  386. int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
  387. {
  388. CMS_CertificateChoices *cch;
  389. STACK_OF(CMS_CertificateChoices) **pcerts;
  390. int i;
  391. pcerts = cms_get0_certificate_choices(cms);
  392. if (!pcerts)
  393. return 0;
  394. for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
  395. cch = sk_CMS_CertificateChoices_value(*pcerts, i);
  396. if (cch->type == CMS_CERTCHOICE_CERT) {
  397. if (!X509_cmp(cch->d.certificate, cert)) {
  398. CMSerr(CMS_F_CMS_ADD0_CERT,
  399. CMS_R_CERTIFICATE_ALREADY_PRESENT);
  400. return 0;
  401. }
  402. }
  403. }
  404. cch = CMS_add0_CertificateChoices(cms);
  405. if (!cch)
  406. return 0;
  407. cch->type = CMS_CERTCHOICE_CERT;
  408. cch->d.certificate = cert;
  409. return 1;
  410. }
  411. int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
  412. {
  413. int r;
  414. r = CMS_add0_cert(cms, cert);
  415. if (r > 0)
  416. CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);
  417. return r;
  418. }
  419. static STACK_OF(CMS_RevocationInfoChoice)
  420. **cms_get0_revocation_choices(CMS_ContentInfo *cms)
  421. {
  422. switch (OBJ_obj2nid(cms->contentType)) {
  423. case NID_pkcs7_signed:
  424. return &cms->d.signedData->crls;
  425. case NID_pkcs7_enveloped:
  426. if (cms->d.envelopedData->originatorInfo == NULL)
  427. return NULL;
  428. return &cms->d.envelopedData->originatorInfo->crls;
  429. default:
  430. CMSerr(CMS_F_CMS_GET0_REVOCATION_CHOICES,
  431. CMS_R_UNSUPPORTED_CONTENT_TYPE);
  432. return NULL;
  433. }
  434. }
  435. CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
  436. {
  437. STACK_OF(CMS_RevocationInfoChoice) **pcrls;
  438. CMS_RevocationInfoChoice *rch;
  439. pcrls = cms_get0_revocation_choices(cms);
  440. if (!pcrls)
  441. return NULL;
  442. if (!*pcrls)
  443. *pcrls = sk_CMS_RevocationInfoChoice_new_null();
  444. if (!*pcrls)
  445. return NULL;
  446. rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
  447. if (!rch)
  448. return NULL;
  449. if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
  450. M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
  451. return NULL;
  452. }
  453. return rch;
  454. }
  455. int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
  456. {
  457. CMS_RevocationInfoChoice *rch;
  458. rch = CMS_add0_RevocationInfoChoice(cms);
  459. if (!rch)
  460. return 0;
  461. rch->type = CMS_REVCHOICE_CRL;
  462. rch->d.crl = crl;
  463. return 1;
  464. }
  465. int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
  466. {
  467. int r;
  468. r = CMS_add0_crl(cms, crl);
  469. if (r > 0)
  470. CRYPTO_add(&crl->references, 1, CRYPTO_LOCK_X509_CRL);
  471. return r;
  472. }
  473. STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
  474. {
  475. STACK_OF(X509) *certs = NULL;
  476. CMS_CertificateChoices *cch;
  477. STACK_OF(CMS_CertificateChoices) **pcerts;
  478. int i;
  479. pcerts = cms_get0_certificate_choices(cms);
  480. if (!pcerts)
  481. return NULL;
  482. for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
  483. cch = sk_CMS_CertificateChoices_value(*pcerts, i);
  484. if (cch->type == 0) {
  485. if (!certs) {
  486. certs = sk_X509_new_null();
  487. if (!certs)
  488. return NULL;
  489. }
  490. if (!sk_X509_push(certs, cch->d.certificate)) {
  491. sk_X509_pop_free(certs, X509_free);
  492. return NULL;
  493. }
  494. CRYPTO_add(&cch->d.certificate->references, 1, CRYPTO_LOCK_X509);
  495. }
  496. }
  497. return certs;
  498. }
  499. STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
  500. {
  501. STACK_OF(X509_CRL) *crls = NULL;
  502. STACK_OF(CMS_RevocationInfoChoice) **pcrls;
  503. CMS_RevocationInfoChoice *rch;
  504. int i;
  505. pcrls = cms_get0_revocation_choices(cms);
  506. if (!pcrls)
  507. return NULL;
  508. for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) {
  509. rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
  510. if (rch->type == 0) {
  511. if (!crls) {
  512. crls = sk_X509_CRL_new_null();
  513. if (!crls)
  514. return NULL;
  515. }
  516. if (!sk_X509_CRL_push(crls, rch->d.crl)) {
  517. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  518. return NULL;
  519. }
  520. CRYPTO_add(&rch->d.crl->references, 1, CRYPTO_LOCK_X509_CRL);
  521. }
  522. }
  523. return crls;
  524. }
  525. int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
  526. {
  527. int ret;
  528. ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
  529. if (ret)
  530. return ret;
  531. return ASN1_INTEGER_cmp(ias->serialNumber, X509_get_serialNumber(cert));
  532. }
  533. int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
  534. {
  535. X509_check_purpose(cert, -1, -1);
  536. if (!cert->skid)
  537. return -1;
  538. return ASN1_OCTET_STRING_cmp(keyid, cert->skid);
  539. }
  540. int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
  541. {
  542. CMS_IssuerAndSerialNumber *ias;
  543. ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
  544. if (!ias)
  545. goto err;
  546. if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
  547. goto err;
  548. if (!ASN1_STRING_copy(ias->serialNumber, X509_get_serialNumber(cert)))
  549. goto err;
  550. if (*pias)
  551. M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
  552. *pias = ias;
  553. return 1;
  554. err:
  555. if (ias)
  556. M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
  557. CMSerr(CMS_F_CMS_SET1_IAS, ERR_R_MALLOC_FAILURE);
  558. return 0;
  559. }
  560. int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
  561. {
  562. ASN1_OCTET_STRING *keyid = NULL;
  563. X509_check_purpose(cert, -1, -1);
  564. if (!cert->skid) {
  565. CMSerr(CMS_F_CMS_SET1_KEYID, CMS_R_CERTIFICATE_HAS_NO_KEYID);
  566. return 0;
  567. }
  568. keyid = ASN1_STRING_dup(cert->skid);
  569. if (!keyid) {
  570. CMSerr(CMS_F_CMS_SET1_KEYID, ERR_R_MALLOC_FAILURE);
  571. return 0;
  572. }
  573. if (*pkeyid)
  574. ASN1_OCTET_STRING_free(*pkeyid);
  575. *pkeyid = keyid;
  576. return 1;
  577. }