pk7_smime.c 15 KB

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