pk7_smime.c 18 KB

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