cms_lib.c 15 KB

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