cms_smime.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /* crypto/cms/cms_smime.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 "cryptlib.h"
  55. #include <openssl/asn1t.h>
  56. #include <openssl/x509.h>
  57. #include <openssl/x509v3.h>
  58. #include <openssl/err.h>
  59. #include <openssl/cms.h>
  60. #include "cms_lcl.h"
  61. static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
  62. {
  63. unsigned char buf[4096];
  64. int r = 0, i;
  65. BIO *tmpout = NULL;
  66. if (out == NULL)
  67. tmpout = BIO_new(BIO_s_null());
  68. else if (flags & CMS_TEXT) {
  69. tmpout = BIO_new(BIO_s_mem());
  70. BIO_set_mem_eof_return(tmpout, 0);
  71. } else
  72. tmpout = out;
  73. if (!tmpout) {
  74. CMSerr(CMS_F_CMS_COPY_CONTENT, ERR_R_MALLOC_FAILURE);
  75. goto err;
  76. }
  77. /* Read all content through chain to process digest, decrypt etc */
  78. for (;;) {
  79. i = BIO_read(in, buf, sizeof(buf));
  80. if (i <= 0) {
  81. if (BIO_method_type(in) == BIO_TYPE_CIPHER) {
  82. if (!BIO_get_cipher_status(in))
  83. goto err;
  84. }
  85. if (i < 0)
  86. goto err;
  87. break;
  88. }
  89. if (tmpout && (BIO_write(tmpout, buf, i) != i))
  90. goto err;
  91. }
  92. if (flags & CMS_TEXT) {
  93. if (!SMIME_text(tmpout, out)) {
  94. CMSerr(CMS_F_CMS_COPY_CONTENT, CMS_R_SMIME_TEXT_ERROR);
  95. goto err;
  96. }
  97. }
  98. r = 1;
  99. err:
  100. if (tmpout && (tmpout != out))
  101. BIO_free(tmpout);
  102. return r;
  103. }
  104. static int check_content(CMS_ContentInfo *cms)
  105. {
  106. ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
  107. if (!pos || !*pos) {
  108. CMSerr(CMS_F_CHECK_CONTENT, CMS_R_NO_CONTENT);
  109. return 0;
  110. }
  111. return 1;
  112. }
  113. static void do_free_upto(BIO *f, BIO *upto)
  114. {
  115. if (upto) {
  116. BIO *tbio;
  117. do {
  118. tbio = BIO_pop(f);
  119. BIO_free(f);
  120. f = tbio;
  121. }
  122. while (f != upto);
  123. } else
  124. BIO_free_all(f);
  125. }
  126. int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
  127. {
  128. BIO *cont;
  129. int r;
  130. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) {
  131. CMSerr(CMS_F_CMS_DATA, CMS_R_TYPE_NOT_DATA);
  132. return 0;
  133. }
  134. cont = CMS_dataInit(cms, NULL);
  135. if (!cont)
  136. return 0;
  137. r = cms_copy_content(out, cont, flags);
  138. BIO_free_all(cont);
  139. return r;
  140. }
  141. CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
  142. {
  143. CMS_ContentInfo *cms;
  144. cms = cms_Data_create();
  145. if (!cms)
  146. return NULL;
  147. if (CMS_final(cms, in, NULL, flags))
  148. return cms;
  149. CMS_ContentInfo_free(cms);
  150. return NULL;
  151. }
  152. int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
  153. unsigned int flags)
  154. {
  155. BIO *cont;
  156. int r;
  157. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) {
  158. CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_TYPE_NOT_DIGESTED_DATA);
  159. return 0;
  160. }
  161. if (!dcont && !check_content(cms))
  162. return 0;
  163. cont = CMS_dataInit(cms, dcont);
  164. if (!cont)
  165. return 0;
  166. r = cms_copy_content(out, cont, flags);
  167. if (r)
  168. r = cms_DigestedData_do_final(cms, cont, 1);
  169. do_free_upto(cont, dcont);
  170. return r;
  171. }
  172. CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
  173. unsigned int flags)
  174. {
  175. CMS_ContentInfo *cms;
  176. if (!md)
  177. md = EVP_sha1();
  178. cms = cms_DigestedData_create(md);
  179. if (!cms)
  180. return NULL;
  181. if (!(flags & CMS_DETACHED)) {
  182. flags &= ~CMS_STREAM;
  183. CMS_set_detached(cms, 0);
  184. }
  185. if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
  186. return cms;
  187. CMS_ContentInfo_free(cms);
  188. return NULL;
  189. }
  190. int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
  191. const unsigned char *key, size_t keylen,
  192. BIO *dcont, BIO *out, unsigned int flags)
  193. {
  194. BIO *cont;
  195. int r;
  196. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) {
  197. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
  198. CMS_R_TYPE_NOT_ENCRYPTED_DATA);
  199. return 0;
  200. }
  201. if (!dcont && !check_content(cms))
  202. return 0;
  203. if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
  204. return 0;
  205. cont = CMS_dataInit(cms, dcont);
  206. if (!cont)
  207. return 0;
  208. r = cms_copy_content(out, cont, flags);
  209. do_free_upto(cont, dcont);
  210. return r;
  211. }
  212. CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
  213. const unsigned char *key,
  214. size_t keylen, unsigned int flags)
  215. {
  216. CMS_ContentInfo *cms;
  217. if (!cipher) {
  218. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT, CMS_R_NO_CIPHER);
  219. return NULL;
  220. }
  221. cms = CMS_ContentInfo_new();
  222. if (!cms)
  223. return NULL;
  224. if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
  225. return NULL;
  226. if (!(flags & CMS_DETACHED)) {
  227. flags &= ~CMS_STREAM;
  228. CMS_set_detached(cms, 0);
  229. }
  230. if ((flags & (CMS_STREAM | CMS_PARTIAL))
  231. || CMS_final(cms, in, NULL, flags))
  232. return cms;
  233. CMS_ContentInfo_free(cms);
  234. return NULL;
  235. }
  236. static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
  237. X509_STORE *store,
  238. STACK_OF(X509) *certs,
  239. STACK_OF(X509_CRL) *crls,
  240. unsigned int flags)
  241. {
  242. X509_STORE_CTX ctx;
  243. X509 *signer;
  244. int i, j, r = 0;
  245. CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
  246. if (!X509_STORE_CTX_init(&ctx, store, signer, certs)) {
  247. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, CMS_R_STORE_INIT_ERROR);
  248. goto err;
  249. }
  250. X509_STORE_CTX_set_default(&ctx, "smime_sign");
  251. if (crls)
  252. X509_STORE_CTX_set0_crls(&ctx, crls);
  253. i = X509_verify_cert(&ctx);
  254. if (i <= 0) {
  255. j = X509_STORE_CTX_get_error(&ctx);
  256. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
  257. CMS_R_CERTIFICATE_VERIFY_ERROR);
  258. ERR_add_error_data(2, "Verify error:",
  259. X509_verify_cert_error_string(j));
  260. goto err;
  261. }
  262. r = 1;
  263. err:
  264. X509_STORE_CTX_cleanup(&ctx);
  265. return r;
  266. }
  267. int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
  268. X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
  269. {
  270. CMS_SignerInfo *si;
  271. STACK_OF(CMS_SignerInfo) *sinfos;
  272. STACK_OF(X509) *cms_certs = NULL;
  273. STACK_OF(X509_CRL) *crls = NULL;
  274. X509 *signer;
  275. int i, scount = 0, ret = 0;
  276. BIO *cmsbio = NULL, *tmpin = NULL;
  277. if (!dcont && !check_content(cms))
  278. return 0;
  279. /* Attempt to find all signer certificates */
  280. sinfos = CMS_get0_SignerInfos(cms);
  281. if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
  282. CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
  283. goto err;
  284. }
  285. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  286. si = sk_CMS_SignerInfo_value(sinfos, i);
  287. CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
  288. if (signer)
  289. scount++;
  290. }
  291. if (scount != sk_CMS_SignerInfo_num(sinfos))
  292. scount += CMS_set1_signers_certs(cms, certs, flags);
  293. if (scount != sk_CMS_SignerInfo_num(sinfos)) {
  294. CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
  295. goto err;
  296. }
  297. /* Attempt to verify all signers certs */
  298. if (!(flags & CMS_NO_SIGNER_CERT_VERIFY)) {
  299. cms_certs = CMS_get1_certs(cms);
  300. if (!(flags & CMS_NOCRL))
  301. crls = CMS_get1_crls(cms);
  302. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  303. si = sk_CMS_SignerInfo_value(sinfos, i);
  304. if (!cms_signerinfo_verify_cert(si, store,
  305. cms_certs, crls, flags))
  306. goto err;
  307. }
  308. }
  309. /* Attempt to verify all SignerInfo signed attribute signatures */
  310. if (!(flags & CMS_NO_ATTR_VERIFY)) {
  311. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  312. si = sk_CMS_SignerInfo_value(sinfos, i);
  313. if (CMS_signed_get_attr_count(si) < 0)
  314. continue;
  315. if (CMS_SignerInfo_verify(si) <= 0)
  316. goto err;
  317. }
  318. }
  319. /*
  320. * Performance optimization: if the content is a memory BIO then store
  321. * its contents in a temporary read only memory BIO. This avoids
  322. * potentially large numbers of slow copies of data which will occur when
  323. * reading from a read write memory BIO when signatures are calculated.
  324. */
  325. if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
  326. char *ptr;
  327. long len;
  328. len = BIO_get_mem_data(dcont, &ptr);
  329. tmpin = BIO_new_mem_buf(ptr, len);
  330. if (tmpin == NULL) {
  331. CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
  332. return 0;
  333. }
  334. } else
  335. tmpin = dcont;
  336. cmsbio = CMS_dataInit(cms, tmpin);
  337. if (!cmsbio)
  338. goto err;
  339. if (!cms_copy_content(out, cmsbio, flags))
  340. goto err;
  341. if (!(flags & CMS_NO_CONTENT_VERIFY)) {
  342. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  343. si = sk_CMS_SignerInfo_value(sinfos, i);
  344. if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
  345. CMSerr(CMS_F_CMS_VERIFY, CMS_R_CONTENT_VERIFY_ERROR);
  346. goto err;
  347. }
  348. }
  349. }
  350. ret = 1;
  351. err:
  352. if (dcont && (tmpin == dcont))
  353. do_free_upto(cmsbio, dcont);
  354. else
  355. BIO_free_all(cmsbio);
  356. if (cms_certs)
  357. sk_X509_pop_free(cms_certs, X509_free);
  358. if (crls)
  359. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  360. return ret;
  361. }
  362. int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
  363. STACK_OF(X509) *certs,
  364. X509_STORE *store, unsigned int flags)
  365. {
  366. int r;
  367. r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
  368. if (r <= 0)
  369. return r;
  370. return cms_Receipt_verify(rcms, ocms);
  371. }
  372. CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey,
  373. STACK_OF(X509) *certs, BIO *data,
  374. unsigned int flags)
  375. {
  376. CMS_ContentInfo *cms;
  377. int i;
  378. cms = CMS_ContentInfo_new();
  379. if (!cms || !CMS_SignedData_init(cms))
  380. goto merr;
  381. if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
  382. CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
  383. goto err;
  384. }
  385. for (i = 0; i < sk_X509_num(certs); i++) {
  386. X509 *x = sk_X509_value(certs, i);
  387. if (!CMS_add1_cert(cms, x))
  388. goto merr;
  389. }
  390. if (!(flags & CMS_DETACHED)) {
  391. flags &= ~CMS_STREAM;
  392. CMS_set_detached(cms, 0);
  393. }
  394. if ((flags & (CMS_STREAM | CMS_PARTIAL))
  395. || CMS_final(cms, data, NULL, flags))
  396. return cms;
  397. else
  398. goto err;
  399. merr:
  400. CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE);
  401. err:
  402. if (cms)
  403. CMS_ContentInfo_free(cms);
  404. return NULL;
  405. }
  406. CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
  407. X509 *signcert, EVP_PKEY *pkey,
  408. STACK_OF(X509) *certs, unsigned int flags)
  409. {
  410. CMS_SignerInfo *rct_si;
  411. CMS_ContentInfo *cms = NULL;
  412. ASN1_OCTET_STRING **pos, *os;
  413. BIO *rct_cont = NULL;
  414. int r = 0;
  415. flags &= ~CMS_STREAM;
  416. /* Not really detached but avoids content being allocated */
  417. flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
  418. if (!pkey || !signcert) {
  419. CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT);
  420. return NULL;
  421. }
  422. /* Initialize signed data */
  423. cms = CMS_sign(NULL, NULL, certs, NULL, flags);
  424. if (!cms)
  425. goto err;
  426. /* Set inner content type to signed receipt */
  427. if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
  428. goto err;
  429. rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
  430. if (!rct_si) {
  431. CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR);
  432. goto err;
  433. }
  434. os = cms_encode_Receipt(si);
  435. if (!os)
  436. goto err;
  437. /* Set content to digest */
  438. rct_cont = BIO_new_mem_buf(os->data, os->length);
  439. if (!rct_cont)
  440. goto err;
  441. /* Add msgSigDigest attribute */
  442. if (!cms_msgSigDigest_add1(rct_si, si))
  443. goto err;
  444. /* Finalize structure */
  445. if (!CMS_final(cms, rct_cont, NULL, flags))
  446. goto err;
  447. /* Set embedded content */
  448. pos = CMS_get0_content(cms);
  449. *pos = os;
  450. r = 1;
  451. err:
  452. if (rct_cont)
  453. BIO_free(rct_cont);
  454. if (r)
  455. return cms;
  456. CMS_ContentInfo_free(cms);
  457. return NULL;
  458. }
  459. CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
  460. const EVP_CIPHER *cipher, unsigned int flags)
  461. {
  462. CMS_ContentInfo *cms;
  463. int i;
  464. X509 *recip;
  465. cms = CMS_EnvelopedData_create(cipher);
  466. if (!cms)
  467. goto merr;
  468. for (i = 0; i < sk_X509_num(certs); i++) {
  469. recip = sk_X509_value(certs, i);
  470. if (!CMS_add1_recipient_cert(cms, recip, flags)) {
  471. CMSerr(CMS_F_CMS_ENCRYPT, CMS_R_RECIPIENT_ERROR);
  472. goto err;
  473. }
  474. }
  475. if (!(flags & CMS_DETACHED)) {
  476. flags &= ~CMS_STREAM;
  477. CMS_set_detached(cms, 0);
  478. }
  479. if ((flags & (CMS_STREAM | CMS_PARTIAL))
  480. || CMS_final(cms, data, NULL, flags))
  481. return cms;
  482. else
  483. goto err;
  484. merr:
  485. CMSerr(CMS_F_CMS_ENCRYPT, ERR_R_MALLOC_FAILURE);
  486. err:
  487. if (cms)
  488. CMS_ContentInfo_free(cms);
  489. return NULL;
  490. }
  491. int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
  492. {
  493. STACK_OF(CMS_RecipientInfo) *ris;
  494. CMS_RecipientInfo *ri;
  495. int i, r;
  496. int debug = 0, ri_match = 0;
  497. ris = CMS_get0_RecipientInfos(cms);
  498. if (ris)
  499. debug = cms->d.envelopedData->encryptedContentInfo->debug;
  500. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  501. ri = sk_CMS_RecipientInfo_value(ris, i);
  502. if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_TRANS)
  503. continue;
  504. ri_match = 1;
  505. /*
  506. * If we have a cert try matching RecipientInfo otherwise try them
  507. * all.
  508. */
  509. if (!cert || (CMS_RecipientInfo_ktri_cert_cmp(ri, cert) == 0)) {
  510. CMS_RecipientInfo_set0_pkey(ri, pk);
  511. r = CMS_RecipientInfo_decrypt(cms, ri);
  512. CMS_RecipientInfo_set0_pkey(ri, NULL);
  513. if (cert) {
  514. /*
  515. * If not debugging clear any error and return success to
  516. * avoid leaking of information useful to MMA
  517. */
  518. if (!debug) {
  519. ERR_clear_error();
  520. return 1;
  521. }
  522. if (r > 0)
  523. return 1;
  524. CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_DECRYPT_ERROR);
  525. return 0;
  526. }
  527. /*
  528. * If no cert and not debugging don't leave loop after first
  529. * successful decrypt. Always attempt to decrypt all recipients
  530. * to avoid leaking timing of a successful decrypt.
  531. */
  532. else if (r > 0 && debug)
  533. return 1;
  534. }
  535. }
  536. /* If no cert and not debugging always return success */
  537. if (ri_match && !cert && !debug) {
  538. ERR_clear_error();
  539. return 1;
  540. }
  541. CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_NO_MATCHING_RECIPIENT);
  542. return 0;
  543. }
  544. int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
  545. unsigned char *key, size_t keylen,
  546. unsigned char *id, size_t idlen)
  547. {
  548. STACK_OF(CMS_RecipientInfo) *ris;
  549. CMS_RecipientInfo *ri;
  550. int i, r;
  551. ris = CMS_get0_RecipientInfos(cms);
  552. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  553. ri = sk_CMS_RecipientInfo_value(ris, i);
  554. if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
  555. continue;
  556. /*
  557. * If we have an id try matching RecipientInfo otherwise try them
  558. * all.
  559. */
  560. if (!id || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
  561. CMS_RecipientInfo_set0_key(ri, key, keylen);
  562. r = CMS_RecipientInfo_decrypt(cms, ri);
  563. CMS_RecipientInfo_set0_key(ri, NULL, 0);
  564. if (r > 0)
  565. return 1;
  566. if (id) {
  567. CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_DECRYPT_ERROR);
  568. return 0;
  569. }
  570. ERR_clear_error();
  571. }
  572. }
  573. CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_NO_MATCHING_RECIPIENT);
  574. return 0;
  575. }
  576. int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
  577. BIO *dcont, BIO *out, unsigned int flags)
  578. {
  579. int r;
  580. BIO *cont;
  581. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped) {
  582. CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);
  583. return 0;
  584. }
  585. if (!dcont && !check_content(cms))
  586. return 0;
  587. if (flags & CMS_DEBUG_DECRYPT)
  588. cms->d.envelopedData->encryptedContentInfo->debug = 1;
  589. else
  590. cms->d.envelopedData->encryptedContentInfo->debug = 0;
  591. if (!pk && !cert && !dcont && !out)
  592. return 1;
  593. if (pk && !CMS_decrypt_set1_pkey(cms, pk, cert))
  594. return 0;
  595. cont = CMS_dataInit(cms, dcont);
  596. if (!cont)
  597. return 0;
  598. r = cms_copy_content(out, cont, flags);
  599. do_free_upto(cont, dcont);
  600. return r;
  601. }
  602. int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
  603. {
  604. BIO *cmsbio;
  605. int ret = 0;
  606. if (!(cmsbio = CMS_dataInit(cms, dcont))) {
  607. CMSerr(CMS_F_CMS_FINAL, ERR_R_MALLOC_FAILURE);
  608. return 0;
  609. }
  610. SMIME_crlf_copy(data, cmsbio, flags);
  611. (void)BIO_flush(cmsbio);
  612. if (!CMS_dataFinal(cms, cmsbio)) {
  613. CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_DATAFINAL_ERROR);
  614. goto err;
  615. }
  616. ret = 1;
  617. err:
  618. do_free_upto(cmsbio, dcont);
  619. return ret;
  620. }
  621. #ifdef ZLIB
  622. int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
  623. unsigned int flags)
  624. {
  625. BIO *cont;
  626. int r;
  627. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
  628. CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
  629. return 0;
  630. }
  631. if (!dcont && !check_content(cms))
  632. return 0;
  633. cont = CMS_dataInit(cms, dcont);
  634. if (!cont)
  635. return 0;
  636. r = cms_copy_content(out, cont, flags);
  637. do_free_upto(cont, dcont);
  638. return r;
  639. }
  640. CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
  641. {
  642. CMS_ContentInfo *cms;
  643. if (comp_nid <= 0)
  644. comp_nid = NID_zlib_compression;
  645. cms = cms_CompressedData_create(comp_nid);
  646. if (!cms)
  647. return NULL;
  648. if (!(flags & CMS_DETACHED)) {
  649. flags &= ~CMS_STREAM;
  650. CMS_set_detached(cms, 0);
  651. }
  652. if (CMS_final(cms, in, NULL, flags))
  653. return cms;
  654. CMS_ContentInfo_free(cms);
  655. return NULL;
  656. }
  657. #else
  658. int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
  659. unsigned int flags)
  660. {
  661. CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  662. return 0;
  663. }
  664. CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
  665. {
  666. CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  667. return NULL;
  668. }
  669. #endif