Debian Codegraph @

Cryptlib/OpenSSL/crypto/pkcs7/pk7_smime.c

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