cms_lib.c 16 KB

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