pk7_smime.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /* pk7_smime.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2004 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. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. /* Simple PKCS#7 processing functions */
  60. #include <stdio.h>
  61. #include "internal/cryptlib.h"
  62. #include <openssl/x509.h>
  63. #include <openssl/x509v3.h>
  64. #define BUFFERSIZE 4096
  65. static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si);
  66. PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
  67. BIO *data, int flags)
  68. {
  69. PKCS7 *p7;
  70. int i;
  71. if ((p7 = PKCS7_new()) == NULL) {
  72. PKCS7err(PKCS7_F_PKCS7_SIGN, ERR_R_MALLOC_FAILURE);
  73. return NULL;
  74. }
  75. if (!PKCS7_set_type(p7, NID_pkcs7_signed))
  76. goto err;
  77. if (!PKCS7_content_new(p7, NID_pkcs7_data))
  78. goto err;
  79. if (pkey && !PKCS7_sign_add_signer(p7, signcert, pkey, NULL, flags)) {
  80. PKCS7err(PKCS7_F_PKCS7_SIGN, PKCS7_R_PKCS7_ADD_SIGNER_ERROR);
  81. goto err;
  82. }
  83. if (!(flags & PKCS7_NOCERTS)) {
  84. for (i = 0; i < sk_X509_num(certs); i++) {
  85. if (!PKCS7_add_certificate(p7, sk_X509_value(certs, i)))
  86. goto err;
  87. }
  88. }
  89. if (flags & PKCS7_DETACHED)
  90. PKCS7_set_detached(p7, 1);
  91. if (flags & (PKCS7_STREAM | PKCS7_PARTIAL))
  92. return p7;
  93. if (PKCS7_final(p7, data, flags))
  94. return p7;
  95. err:
  96. PKCS7_free(p7);
  97. return NULL;
  98. }
  99. int PKCS7_final(PKCS7 *p7, BIO *data, int flags)
  100. {
  101. BIO *p7bio;
  102. int ret = 0;
  103. if ((p7bio = PKCS7_dataInit(p7, NULL)) == NULL) {
  104. PKCS7err(PKCS7_F_PKCS7_FINAL, ERR_R_MALLOC_FAILURE);
  105. return 0;
  106. }
  107. SMIME_crlf_copy(data, p7bio, flags);
  108. (void)BIO_flush(p7bio);
  109. if (!PKCS7_dataFinal(p7, p7bio)) {
  110. PKCS7err(PKCS7_F_PKCS7_FINAL, PKCS7_R_PKCS7_DATASIGN);
  111. goto err;
  112. }
  113. ret = 1;
  114. err:
  115. BIO_free_all(p7bio);
  116. return ret;
  117. }
  118. /* Check to see if a cipher exists and if so add S/MIME capabilities */
  119. static int add_cipher_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
  120. {
  121. if (EVP_get_cipherbynid(nid))
  122. return PKCS7_simple_smimecap(sk, nid, arg);
  123. return 1;
  124. }
  125. static int add_digest_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
  126. {
  127. if (EVP_get_digestbynid(nid))
  128. return PKCS7_simple_smimecap(sk, nid, arg);
  129. return 1;
  130. }
  131. PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, X509 *signcert,
  132. EVP_PKEY *pkey, const EVP_MD *md,
  133. int flags)
  134. {
  135. PKCS7_SIGNER_INFO *si = NULL;
  136. STACK_OF(X509_ALGOR) *smcap = NULL;
  137. if (!X509_check_private_key(signcert, pkey)) {
  138. PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER,
  139. PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  140. return NULL;
  141. }
  142. if ((si = PKCS7_add_signature(p7, signcert, pkey, md)) == NULL) {
  143. PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER,
  144. PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR);
  145. return NULL;
  146. }
  147. if (!(flags & PKCS7_NOCERTS)) {
  148. if (!PKCS7_add_certificate(p7, signcert))
  149. goto err;
  150. }
  151. if (!(flags & PKCS7_NOATTR)) {
  152. if (!PKCS7_add_attrib_content_type(si, NULL))
  153. goto err;
  154. /* Add SMIMECapabilities */
  155. if (!(flags & PKCS7_NOSMIMECAP)) {
  156. if ((smcap = sk_X509_ALGOR_new_null()) == NULL) {
  157. PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER, ERR_R_MALLOC_FAILURE);
  158. goto err;
  159. }
  160. if (!add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
  161. || !add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
  162. || !add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
  163. || !add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
  164. || !add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
  165. || !add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
  166. || !add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
  167. || !add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
  168. || !add_cipher_smcap(smcap, NID_rc2_cbc, 128)
  169. || !add_cipher_smcap(smcap, NID_rc2_cbc, 64)
  170. || !add_cipher_smcap(smcap, NID_des_cbc, -1)
  171. || !add_cipher_smcap(smcap, NID_rc2_cbc, 40)
  172. || !PKCS7_add_attrib_smimecap(si, smcap))
  173. goto err;
  174. sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
  175. smcap = NULL;
  176. }
  177. if (flags & PKCS7_REUSE_DIGEST) {
  178. if (!pkcs7_copy_existing_digest(p7, si))
  179. goto err;
  180. if (!(flags & PKCS7_PARTIAL) && !PKCS7_SIGNER_INFO_sign(si))
  181. goto err;
  182. }
  183. }
  184. return si;
  185. err:
  186. sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
  187. return NULL;
  188. }
  189. /*
  190. * Search for a digest matching SignerInfo digest type and if found copy
  191. * across.
  192. */
  193. static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
  194. {
  195. int i;
  196. STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
  197. PKCS7_SIGNER_INFO *sitmp;
  198. ASN1_OCTET_STRING *osdig = NULL;
  199. sinfos = PKCS7_get_signer_info(p7);
  200. for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
  201. sitmp = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
  202. if (si == sitmp)
  203. break;
  204. if (sk_X509_ATTRIBUTE_num(sitmp->auth_attr) <= 0)
  205. continue;
  206. if (!OBJ_cmp(si->digest_alg->algorithm, sitmp->digest_alg->algorithm)) {
  207. osdig = PKCS7_digest_from_attributes(sitmp->auth_attr);
  208. break;
  209. }
  210. }
  211. if (osdig)
  212. return PKCS7_add1_attrib_digest(si, osdig->data, osdig->length);
  213. PKCS7err(PKCS7_F_PKCS7_COPY_EXISTING_DIGEST,
  214. PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND);
  215. return 0;
  216. }
  217. int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
  218. BIO *indata, BIO *out, int flags)
  219. {
  220. STACK_OF(X509) *signers;
  221. X509 *signer;
  222. STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
  223. PKCS7_SIGNER_INFO *si;
  224. X509_STORE_CTX cert_ctx;
  225. char *buf = NULL;
  226. int i, j = 0, k, ret = 0;
  227. BIO *p7bio = NULL;
  228. BIO *tmpin = NULL, *tmpout = NULL;
  229. if (!p7) {
  230. PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_INVALID_NULL_POINTER);
  231. return 0;
  232. }
  233. if (!PKCS7_type_is_signed(p7)) {
  234. PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_WRONG_CONTENT_TYPE);
  235. return 0;
  236. }
  237. /* Check for no data and no content: no data to verify signature */
  238. if (PKCS7_get_detached(p7) && !indata) {
  239. PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_NO_CONTENT);
  240. return 0;
  241. }
  242. /* Check for data and content: two sets of data */
  243. if (!PKCS7_get_detached(p7) && indata) {
  244. PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_CONTENT_AND_DATA_PRESENT);
  245. return 0;
  246. }
  247. sinfos = PKCS7_get_signer_info(p7);
  248. if (!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) {
  249. PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_NO_SIGNATURES_ON_DATA);
  250. return 0;
  251. }
  252. signers = PKCS7_get0_signers(p7, certs, flags);
  253. if (!signers)
  254. return 0;
  255. /* Now verify the certificates */
  256. if (!(flags & PKCS7_NOVERIFY))
  257. for (k = 0; k < sk_X509_num(signers); k++) {
  258. signer = sk_X509_value(signers, k);
  259. if (!(flags & PKCS7_NOCHAIN)) {
  260. if (!X509_STORE_CTX_init(&cert_ctx, store, signer,
  261. p7->d.sign->cert)) {
  262. PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_X509_LIB);
  263. goto err;
  264. }
  265. X509_STORE_CTX_set_default(&cert_ctx, "smime_sign");
  266. } else if (!X509_STORE_CTX_init(&cert_ctx, store, signer, NULL)) {
  267. PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_X509_LIB);
  268. goto err;
  269. }
  270. if (!(flags & PKCS7_NOCRL))
  271. X509_STORE_CTX_set0_crls(&cert_ctx, p7->d.sign->crl);
  272. i = X509_verify_cert(&cert_ctx);
  273. if (i <= 0)
  274. j = X509_STORE_CTX_get_error(&cert_ctx);
  275. X509_STORE_CTX_cleanup(&cert_ctx);
  276. if (i <= 0) {
  277. PKCS7err(PKCS7_F_PKCS7_VERIFY,
  278. PKCS7_R_CERTIFICATE_VERIFY_ERROR);
  279. ERR_add_error_data(2, "Verify error:",
  280. X509_verify_cert_error_string(j));
  281. goto err;
  282. }
  283. /* Check for revocation status here */
  284. }
  285. /*
  286. * Performance optimization: if the content is a memory BIO then store
  287. * its contents in a temporary read only memory BIO. This avoids
  288. * potentially large numbers of slow copies of data which will occur when
  289. * reading from a read write memory BIO when signatures are calculated.
  290. */
  291. if (indata && (BIO_method_type(indata) == BIO_TYPE_MEM)) {
  292. char *ptr;
  293. long len;
  294. len = BIO_get_mem_data(indata, &ptr);
  295. tmpin = BIO_new_mem_buf(ptr, len);
  296. if (tmpin == NULL) {
  297. PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_MALLOC_FAILURE);
  298. goto err;
  299. }
  300. } else
  301. tmpin = indata;
  302. if ((p7bio = PKCS7_dataInit(p7, tmpin)) == NULL)
  303. goto err;
  304. if (flags & PKCS7_TEXT) {
  305. if ((tmpout = BIO_new(BIO_s_mem())) == NULL) {
  306. PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_MALLOC_FAILURE);
  307. goto err;
  308. }
  309. BIO_set_mem_eof_return(tmpout, 0);
  310. } else
  311. tmpout = out;
  312. /* We now have to 'read' from p7bio to calculate digests etc. */
  313. if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL) {
  314. PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_MALLOC_FAILURE);
  315. goto err;
  316. }
  317. for (;;) {
  318. i = BIO_read(p7bio, buf, BUFFERSIZE);
  319. if (i <= 0)
  320. break;
  321. if (tmpout)
  322. BIO_write(tmpout, buf, i);
  323. }
  324. if (flags & PKCS7_TEXT) {
  325. if (!SMIME_text(tmpout, out)) {
  326. PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_SMIME_TEXT_ERROR);
  327. BIO_free(tmpout);
  328. goto err;
  329. }
  330. BIO_free(tmpout);
  331. }
  332. /* Now Verify All Signatures */
  333. if (!(flags & PKCS7_NOSIGS))
  334. for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
  335. si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
  336. signer = sk_X509_value(signers, i);
  337. j = PKCS7_signatureVerify(p7bio, p7, si, signer);
  338. if (j <= 0) {
  339. PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_SIGNATURE_FAILURE);
  340. goto err;
  341. }
  342. }
  343. ret = 1;
  344. err:
  345. OPENSSL_free(buf);
  346. if (tmpin == indata) {
  347. if (indata)
  348. BIO_pop(p7bio);
  349. }
  350. BIO_free_all(p7bio);
  351. sk_X509_free(signers);
  352. return ret;
  353. }
  354. STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs,
  355. int flags)
  356. {
  357. STACK_OF(X509) *signers;
  358. STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
  359. PKCS7_SIGNER_INFO *si;
  360. PKCS7_ISSUER_AND_SERIAL *ias;
  361. X509 *signer;
  362. int i;
  363. if (!p7) {
  364. PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_INVALID_NULL_POINTER);
  365. return NULL;
  366. }
  367. if (!PKCS7_type_is_signed(p7)) {
  368. PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_WRONG_CONTENT_TYPE);
  369. return NULL;
  370. }
  371. /* Collect all the signers together */
  372. sinfos = PKCS7_get_signer_info(p7);
  373. if (sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) {
  374. PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_NO_SIGNERS);
  375. return 0;
  376. }
  377. if ((signers = sk_X509_new_null()) == NULL) {
  378. PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, ERR_R_MALLOC_FAILURE);
  379. return NULL;
  380. }
  381. for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
  382. si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
  383. ias = si->issuer_and_serial;
  384. signer = NULL;
  385. /* If any certificates passed they take priority */
  386. if (certs)
  387. signer = X509_find_by_issuer_and_serial(certs,
  388. ias->issuer, ias->serial);
  389. if (!signer && !(flags & PKCS7_NOINTERN)
  390. && p7->d.sign->cert)
  391. signer =
  392. X509_find_by_issuer_and_serial(p7->d.sign->cert,
  393. ias->issuer, ias->serial);
  394. if (!signer) {
  395. PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,
  396. PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND);
  397. sk_X509_free(signers);
  398. return 0;
  399. }
  400. if (!sk_X509_push(signers, signer)) {
  401. sk_X509_free(signers);
  402. return NULL;
  403. }
  404. }
  405. return signers;
  406. }
  407. /* Build a complete PKCS#7 enveloped data */
  408. PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
  409. int flags)
  410. {
  411. PKCS7 *p7;
  412. BIO *p7bio = NULL;
  413. int i;
  414. X509 *x509;
  415. if ((p7 = PKCS7_new()) == NULL) {
  416. PKCS7err(PKCS7_F_PKCS7_ENCRYPT, ERR_R_MALLOC_FAILURE);
  417. return NULL;
  418. }
  419. if (!PKCS7_set_type(p7, NID_pkcs7_enveloped))
  420. goto err;
  421. if (!PKCS7_set_cipher(p7, cipher)) {
  422. PKCS7err(PKCS7_F_PKCS7_ENCRYPT, PKCS7_R_ERROR_SETTING_CIPHER);
  423. goto err;
  424. }
  425. for (i = 0; i < sk_X509_num(certs); i++) {
  426. x509 = sk_X509_value(certs, i);
  427. if (!PKCS7_add_recipient(p7, x509)) {
  428. PKCS7err(PKCS7_F_PKCS7_ENCRYPT, PKCS7_R_ERROR_ADDING_RECIPIENT);
  429. goto err;
  430. }
  431. }
  432. if (flags & PKCS7_STREAM)
  433. return p7;
  434. if (PKCS7_final(p7, in, flags))
  435. return p7;
  436. err:
  437. BIO_free_all(p7bio);
  438. PKCS7_free(p7);
  439. return NULL;
  440. }
  441. int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags)
  442. {
  443. BIO *tmpmem;
  444. int ret = 0, i;
  445. char *buf = NULL;
  446. if (!p7) {
  447. PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_INVALID_NULL_POINTER);
  448. return 0;
  449. }
  450. if (!PKCS7_type_is_enveloped(p7)) {
  451. PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_WRONG_CONTENT_TYPE);
  452. return 0;
  453. }
  454. if (cert && !X509_check_private_key(cert, pkey)) {
  455. PKCS7err(PKCS7_F_PKCS7_DECRYPT,
  456. PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  457. return 0;
  458. }
  459. if ((tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert)) == NULL) {
  460. PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_DECRYPT_ERROR);
  461. return 0;
  462. }
  463. if (flags & PKCS7_TEXT) {
  464. BIO *tmpbuf, *bread;
  465. /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */
  466. if ((tmpbuf = BIO_new(BIO_f_buffer())) == NULL) {
  467. PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
  468. BIO_free_all(tmpmem);
  469. return 0;
  470. }
  471. if ((bread = BIO_push(tmpbuf, tmpmem)) == NULL) {
  472. PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
  473. BIO_free_all(tmpbuf);
  474. BIO_free_all(tmpmem);
  475. return 0;
  476. }
  477. ret = SMIME_text(bread, data);
  478. if (ret > 0 && BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
  479. if (!BIO_get_cipher_status(tmpmem))
  480. ret = 0;
  481. }
  482. BIO_free_all(bread);
  483. return ret;
  484. }
  485. if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL) {
  486. PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
  487. goto err;
  488. }
  489. for (;;) {
  490. i = BIO_read(tmpmem, buf, BUFFERSIZE);
  491. if (i <= 0) {
  492. ret = 1;
  493. if (BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
  494. if (!BIO_get_cipher_status(tmpmem))
  495. ret = 0;
  496. }
  497. break;
  498. }
  499. if (BIO_write(data, buf, i) != i) {
  500. break;
  501. }
  502. }
  503. err:
  504. OPENSSL_free(buf);
  505. BIO_free_all(tmpmem);
  506. return ret;
  507. }