cms_smime.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /* crypto/cms/cms_smime.c */
  2. /*
  3. * Written by Dr Stephen N Henson ([email protected]) 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. * [email protected].
  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 && 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 ((flags & CMS_STREAM) || 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. CMS_set_detached(cms, 0);
  183. if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
  184. return cms;
  185. CMS_ContentInfo_free(cms);
  186. return NULL;
  187. }
  188. int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
  189. const unsigned char *key, size_t keylen,
  190. BIO *dcont, BIO *out, unsigned int flags)
  191. {
  192. BIO *cont;
  193. int r;
  194. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) {
  195. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
  196. CMS_R_TYPE_NOT_ENCRYPTED_DATA);
  197. return 0;
  198. }
  199. if (!dcont && !check_content(cms))
  200. return 0;
  201. if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
  202. return 0;
  203. cont = CMS_dataInit(cms, dcont);
  204. if (!cont)
  205. return 0;
  206. r = cms_copy_content(out, cont, flags);
  207. do_free_upto(cont, dcont);
  208. return r;
  209. }
  210. CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
  211. const unsigned char *key,
  212. size_t keylen, unsigned int flags)
  213. {
  214. CMS_ContentInfo *cms;
  215. if (!cipher) {
  216. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT, CMS_R_NO_CIPHER);
  217. return NULL;
  218. }
  219. cms = CMS_ContentInfo_new();
  220. if (!cms)
  221. return NULL;
  222. if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
  223. return NULL;
  224. if (!(flags & CMS_DETACHED))
  225. CMS_set_detached(cms, 0);
  226. if ((flags & (CMS_STREAM | CMS_PARTIAL))
  227. || CMS_final(cms, in, NULL, flags))
  228. return cms;
  229. CMS_ContentInfo_free(cms);
  230. return NULL;
  231. }
  232. static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
  233. X509_STORE *store,
  234. STACK_OF(X509) *certs,
  235. STACK_OF(X509_CRL) *crls,
  236. unsigned int flags)
  237. {
  238. X509_STORE_CTX ctx;
  239. X509 *signer;
  240. int i, j, r = 0;
  241. CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
  242. if (!X509_STORE_CTX_init(&ctx, store, signer, certs)) {
  243. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, CMS_R_STORE_INIT_ERROR);
  244. goto err;
  245. }
  246. X509_STORE_CTX_set_default(&ctx, "smime_sign");
  247. if (crls)
  248. X509_STORE_CTX_set0_crls(&ctx, crls);
  249. i = X509_verify_cert(&ctx);
  250. if (i <= 0) {
  251. j = X509_STORE_CTX_get_error(&ctx);
  252. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
  253. CMS_R_CERTIFICATE_VERIFY_ERROR);
  254. ERR_add_error_data(2, "Verify error:",
  255. X509_verify_cert_error_string(j));
  256. goto err;
  257. }
  258. r = 1;
  259. err:
  260. X509_STORE_CTX_cleanup(&ctx);
  261. return r;
  262. }
  263. int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
  264. X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
  265. {
  266. CMS_SignerInfo *si;
  267. STACK_OF(CMS_SignerInfo) *sinfos;
  268. STACK_OF(X509) *cms_certs = NULL;
  269. STACK_OF(X509_CRL) *crls = NULL;
  270. X509 *signer;
  271. int i, scount = 0, ret = 0;
  272. BIO *cmsbio = NULL, *tmpin = NULL;
  273. if (!dcont && !check_content(cms))
  274. return 0;
  275. /* Attempt to find all signer certificates */
  276. sinfos = CMS_get0_SignerInfos(cms);
  277. if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
  278. CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
  279. goto err;
  280. }
  281. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  282. si = sk_CMS_SignerInfo_value(sinfos, i);
  283. CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
  284. if (signer)
  285. scount++;
  286. }
  287. if (scount != sk_CMS_SignerInfo_num(sinfos))
  288. scount += CMS_set1_signers_certs(cms, certs, flags);
  289. if (scount != sk_CMS_SignerInfo_num(sinfos)) {
  290. CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
  291. goto err;
  292. }
  293. /* Attempt to verify all signers certs */
  294. if (!(flags & CMS_NO_SIGNER_CERT_VERIFY)) {
  295. cms_certs = CMS_get1_certs(cms);
  296. if (!(flags & CMS_NOCRL))
  297. crls = CMS_get1_crls(cms);
  298. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  299. si = sk_CMS_SignerInfo_value(sinfos, i);
  300. if (!cms_signerinfo_verify_cert(si, store,
  301. cms_certs, crls, flags))
  302. goto err;
  303. }
  304. }
  305. /* Attempt to verify all SignerInfo signed attribute signatures */
  306. if (!(flags & CMS_NO_ATTR_VERIFY)) {
  307. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  308. si = sk_CMS_SignerInfo_value(sinfos, i);
  309. if (CMS_signed_get_attr_count(si) < 0)
  310. continue;
  311. if (CMS_SignerInfo_verify(si) <= 0)
  312. goto err;
  313. }
  314. }
  315. /*
  316. * Performance optimization: if the content is a memory BIO then store
  317. * its contents in a temporary read only memory BIO. This avoids
  318. * potentially large numbers of slow copies of data which will occur when
  319. * reading from a read write memory BIO when signatures are calculated.
  320. */
  321. if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
  322. char *ptr;
  323. long len;
  324. len = BIO_get_mem_data(dcont, &ptr);
  325. tmpin = BIO_new_mem_buf(ptr, len);
  326. if (tmpin == NULL) {
  327. CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
  328. return 0;
  329. }
  330. } else
  331. tmpin = dcont;
  332. cmsbio = CMS_dataInit(cms, tmpin);
  333. if (!cmsbio)
  334. goto err;
  335. if (!cms_copy_content(out, cmsbio, flags))
  336. goto err;
  337. if (!(flags & CMS_NO_CONTENT_VERIFY)) {
  338. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  339. si = sk_CMS_SignerInfo_value(sinfos, i);
  340. if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
  341. CMSerr(CMS_F_CMS_VERIFY, CMS_R_CONTENT_VERIFY_ERROR);
  342. goto err;
  343. }
  344. }
  345. }
  346. ret = 1;
  347. err:
  348. if (dcont && (tmpin == dcont))
  349. do_free_upto(cmsbio, dcont);
  350. else
  351. BIO_free_all(cmsbio);
  352. if (cms_certs)
  353. sk_X509_pop_free(cms_certs, X509_free);
  354. if (crls)
  355. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  356. return ret;
  357. }
  358. int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
  359. STACK_OF(X509) *certs,
  360. X509_STORE *store, unsigned int flags)
  361. {
  362. int r;
  363. flags &= ~(CMS_DETACHED | CMS_TEXT);
  364. r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
  365. if (r <= 0)
  366. return r;
  367. return cms_Receipt_verify(rcms, ocms);
  368. }
  369. CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey,
  370. STACK_OF(X509) *certs, BIO *data,
  371. unsigned int flags)
  372. {
  373. CMS_ContentInfo *cms;
  374. int i;
  375. cms = CMS_ContentInfo_new();
  376. if (!cms || !CMS_SignedData_init(cms))
  377. goto merr;
  378. if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
  379. CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
  380. goto err;
  381. }
  382. for (i = 0; i < sk_X509_num(certs); i++) {
  383. X509 *x = sk_X509_value(certs, i);
  384. if (!CMS_add1_cert(cms, x))
  385. goto merr;
  386. }
  387. if (!(flags & CMS_DETACHED))
  388. CMS_set_detached(cms, 0);
  389. if ((flags & (CMS_STREAM | CMS_PARTIAL))
  390. || CMS_final(cms, data, NULL, flags))
  391. return cms;
  392. else
  393. goto err;
  394. merr:
  395. CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE);
  396. err:
  397. if (cms)
  398. CMS_ContentInfo_free(cms);
  399. return NULL;
  400. }
  401. CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
  402. X509 *signcert, EVP_PKEY *pkey,
  403. STACK_OF(X509) *certs, unsigned int flags)
  404. {
  405. CMS_SignerInfo *rct_si;
  406. CMS_ContentInfo *cms = NULL;
  407. ASN1_OCTET_STRING **pos, *os;
  408. BIO *rct_cont = NULL;
  409. int r = 0;
  410. flags &= ~(CMS_STREAM | CMS_TEXT);
  411. /* Not really detached but avoids content being allocated */
  412. flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
  413. if (!pkey || !signcert) {
  414. CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT);
  415. return NULL;
  416. }
  417. /* Initialize signed data */
  418. cms = CMS_sign(NULL, NULL, certs, NULL, flags);
  419. if (!cms)
  420. goto err;
  421. /* Set inner content type to signed receipt */
  422. if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
  423. goto err;
  424. rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
  425. if (!rct_si) {
  426. CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR);
  427. goto err;
  428. }
  429. os = cms_encode_Receipt(si);
  430. if (!os)
  431. goto err;
  432. /* Set content to digest */
  433. rct_cont = BIO_new_mem_buf(os->data, os->length);
  434. if (!rct_cont)
  435. goto err;
  436. /* Add msgSigDigest attribute */
  437. if (!cms_msgSigDigest_add1(rct_si, si))
  438. goto err;
  439. /* Finalize structure */
  440. if (!CMS_final(cms, rct_cont, NULL, flags))
  441. goto err;
  442. /* Set embedded content */
  443. pos = CMS_get0_content(cms);
  444. *pos = os;
  445. r = 1;
  446. err:
  447. if (rct_cont)
  448. BIO_free(rct_cont);
  449. if (r)
  450. return cms;
  451. CMS_ContentInfo_free(cms);
  452. return NULL;
  453. }
  454. CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
  455. const EVP_CIPHER *cipher, unsigned int flags)
  456. {
  457. CMS_ContentInfo *cms;
  458. int i;
  459. X509 *recip;
  460. cms = CMS_EnvelopedData_create(cipher);
  461. if (!cms)
  462. goto merr;
  463. for (i = 0; i < sk_X509_num(certs); i++) {
  464. recip = sk_X509_value(certs, i);
  465. if (!CMS_add1_recipient_cert(cms, recip, flags)) {
  466. CMSerr(CMS_F_CMS_ENCRYPT, CMS_R_RECIPIENT_ERROR);
  467. goto err;
  468. }
  469. }
  470. if (!(flags & CMS_DETACHED))
  471. CMS_set_detached(cms, 0);
  472. if ((flags & (CMS_STREAM | CMS_PARTIAL))
  473. || CMS_final(cms, data, NULL, flags))
  474. return cms;
  475. else
  476. goto err;
  477. merr:
  478. CMSerr(CMS_F_CMS_ENCRYPT, ERR_R_MALLOC_FAILURE);
  479. err:
  480. if (cms)
  481. CMS_ContentInfo_free(cms);
  482. return NULL;
  483. }
  484. int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
  485. {
  486. STACK_OF(CMS_RecipientInfo) *ris;
  487. CMS_RecipientInfo *ri;
  488. int i, r;
  489. int debug = 0, ri_match = 0;
  490. ris = CMS_get0_RecipientInfos(cms);
  491. if (ris)
  492. debug = cms->d.envelopedData->encryptedContentInfo->debug;
  493. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  494. ri = sk_CMS_RecipientInfo_value(ris, i);
  495. if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_TRANS)
  496. continue;
  497. ri_match = 1;
  498. /*
  499. * If we have a cert try matching RecipientInfo otherwise try them
  500. * all.
  501. */
  502. if (!cert || (CMS_RecipientInfo_ktri_cert_cmp(ri, cert) == 0)) {
  503. CMS_RecipientInfo_set0_pkey(ri, pk);
  504. r = CMS_RecipientInfo_decrypt(cms, ri);
  505. CMS_RecipientInfo_set0_pkey(ri, NULL);
  506. if (cert) {
  507. /*
  508. * If not debugging clear any error and return success to
  509. * avoid leaking of information useful to MMA
  510. */
  511. if (!debug) {
  512. ERR_clear_error();
  513. return 1;
  514. }
  515. if (r > 0)
  516. return 1;
  517. CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_DECRYPT_ERROR);
  518. return 0;
  519. }
  520. /*
  521. * If no cert and not debugging don't leave loop after first
  522. * successful decrypt. Always attempt to decrypt all recipients
  523. * to avoid leaking timing of a successful decrypt.
  524. */
  525. else if (r > 0 && debug)
  526. return 1;
  527. }
  528. }
  529. /* If no cert and not debugging always return success */
  530. if (ri_match && !cert && !debug) {
  531. ERR_clear_error();
  532. return 1;
  533. }
  534. CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_NO_MATCHING_RECIPIENT);
  535. return 0;
  536. }
  537. int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
  538. unsigned char *key, size_t keylen,
  539. unsigned char *id, size_t idlen)
  540. {
  541. STACK_OF(CMS_RecipientInfo) *ris;
  542. CMS_RecipientInfo *ri;
  543. int i, r;
  544. ris = CMS_get0_RecipientInfos(cms);
  545. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  546. ri = sk_CMS_RecipientInfo_value(ris, i);
  547. if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
  548. continue;
  549. /*
  550. * If we have an id try matching RecipientInfo otherwise try them
  551. * all.
  552. */
  553. if (!id || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
  554. CMS_RecipientInfo_set0_key(ri, key, keylen);
  555. r = CMS_RecipientInfo_decrypt(cms, ri);
  556. CMS_RecipientInfo_set0_key(ri, NULL, 0);
  557. if (r > 0)
  558. return 1;
  559. if (id) {
  560. CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_DECRYPT_ERROR);
  561. return 0;
  562. }
  563. ERR_clear_error();
  564. }
  565. }
  566. CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_NO_MATCHING_RECIPIENT);
  567. return 0;
  568. }
  569. int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
  570. unsigned char *pass, ossl_ssize_t passlen)
  571. {
  572. STACK_OF(CMS_RecipientInfo) *ris;
  573. CMS_RecipientInfo *ri;
  574. int i, r;
  575. ris = CMS_get0_RecipientInfos(cms);
  576. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  577. ri = sk_CMS_RecipientInfo_value(ris, i);
  578. if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
  579. continue;
  580. CMS_RecipientInfo_set0_password(ri, pass, passlen);
  581. r = CMS_RecipientInfo_decrypt(cms, ri);
  582. CMS_RecipientInfo_set0_password(ri, NULL, 0);
  583. if (r > 0)
  584. return 1;
  585. }
  586. CMSerr(CMS_F_CMS_DECRYPT_SET1_PASSWORD, CMS_R_NO_MATCHING_RECIPIENT);
  587. return 0;
  588. }
  589. int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
  590. BIO *dcont, BIO *out, unsigned int flags)
  591. {
  592. int r;
  593. BIO *cont;
  594. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped) {
  595. CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);
  596. return 0;
  597. }
  598. if (!dcont && !check_content(cms))
  599. return 0;
  600. if (flags & CMS_DEBUG_DECRYPT)
  601. cms->d.envelopedData->encryptedContentInfo->debug = 1;
  602. else
  603. cms->d.envelopedData->encryptedContentInfo->debug = 0;
  604. if (!pk && !cert && !dcont && !out)
  605. return 1;
  606. if (pk && !CMS_decrypt_set1_pkey(cms, pk, cert))
  607. return 0;
  608. cont = CMS_dataInit(cms, dcont);
  609. if (!cont)
  610. return 0;
  611. r = cms_copy_content(out, cont, flags);
  612. do_free_upto(cont, dcont);
  613. return r;
  614. }
  615. int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
  616. {
  617. BIO *cmsbio;
  618. int ret = 0;
  619. if (!(cmsbio = CMS_dataInit(cms, dcont))) {
  620. CMSerr(CMS_F_CMS_FINAL, ERR_R_MALLOC_FAILURE);
  621. return 0;
  622. }
  623. SMIME_crlf_copy(data, cmsbio, flags);
  624. (void)BIO_flush(cmsbio);
  625. if (!CMS_dataFinal(cms, cmsbio)) {
  626. CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_DATAFINAL_ERROR);
  627. goto err;
  628. }
  629. ret = 1;
  630. err:
  631. do_free_upto(cmsbio, dcont);
  632. return ret;
  633. }
  634. #ifdef ZLIB
  635. int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
  636. unsigned int flags)
  637. {
  638. BIO *cont;
  639. int r;
  640. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
  641. CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
  642. return 0;
  643. }
  644. if (!dcont && !check_content(cms))
  645. return 0;
  646. cont = CMS_dataInit(cms, dcont);
  647. if (!cont)
  648. return 0;
  649. r = cms_copy_content(out, cont, flags);
  650. do_free_upto(cont, dcont);
  651. return r;
  652. }
  653. CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
  654. {
  655. CMS_ContentInfo *cms;
  656. if (comp_nid <= 0)
  657. comp_nid = NID_zlib_compression;
  658. cms = cms_CompressedData_create(comp_nid);
  659. if (!cms)
  660. return NULL;
  661. if (!(flags & CMS_DETACHED))
  662. CMS_set_detached(cms, 0);
  663. if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
  664. return cms;
  665. CMS_ContentInfo_free(cms);
  666. return NULL;
  667. }
  668. #else
  669. int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
  670. unsigned int flags)
  671. {
  672. CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  673. return 0;
  674. }
  675. CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
  676. {
  677. CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  678. return NULL;
  679. }
  680. #endif