cms_lib.c 17 KB

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