Add and use OPENSSL_zalloc
[openssl.git] / crypto / comp / c_zlib.c
1 /* ====================================================================
2  * Copyright (c) 1999-2015 The OpenSSL Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * 3. All advertising materials mentioning features or use of this
17  *    software must display the following acknowledgment:
18  *    "This product includes software developed by the OpenSSL Project
19  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
20  *
21  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22  *    endorse or promote products derived from this software without
23  *    prior written permission. For written permission, please contact
24  *    openssl-core@OpenSSL.org.
25  *
26  * 5. Products derived from this software may not be called "OpenSSL"
27  *    nor may "OpenSSL" appear in their names without prior written
28  *    permission of the OpenSSL Project.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the OpenSSL Project
33  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46  * OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This product includes cryptographic software written by Eric Young
50  * (eay@cryptsoft.com).  This product includes software written by Tim
51  * Hudson (tjh@cryptsoft.com).
52  *
53  */
54
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <openssl/objects.h>
59 #include <openssl/comp.h>
60 #include <openssl/err.h>
61 #include "comp_lcl.h"
62
63 COMP_METHOD *COMP_zlib(void);
64
65 static COMP_METHOD zlib_method_nozlib = {
66     NID_undef,
67     "(undef)",
68     NULL,
69     NULL,
70     NULL,
71     NULL,
72 };
73
74 #ifndef ZLIB
75 # undef ZLIB_SHARED
76 #else
77
78 # include <zlib.h>
79
80 static int zlib_stateful_init(COMP_CTX *ctx);
81 static void zlib_stateful_finish(COMP_CTX *ctx);
82 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
83                                         unsigned int olen, unsigned char *in,
84                                         unsigned int ilen);
85 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
86                                       unsigned int olen, unsigned char *in,
87                                       unsigned int ilen);
88
89 /* memory allocations functions for zlib intialization */
90 static void *zlib_zalloc(void *opaque, unsigned int no, unsigned int size)
91 {
92     void *p;
93
94     p = OPENSSL_zalloc(no * size);
95     return p;
96 }
97
98 static void zlib_zfree(void *opaque, void *address)
99 {
100     OPENSSL_free(address);
101 }
102
103
104 static COMP_METHOD zlib_stateful_method = {
105     NID_zlib_compression,
106     LN_zlib_compression,
107     zlib_stateful_init,
108     zlib_stateful_finish,
109     zlib_stateful_compress_block,
110     zlib_stateful_expand_block
111 };
112
113 /*
114  * When OpenSSL is built on Windows, we do not want to require that
115  * the ZLIB.DLL be available in order for the OpenSSL DLLs to
116  * work.  Therefore, all ZLIB routines are loaded at run time
117  * and we do not link to a .LIB file when ZLIB_SHARED is set.
118  */
119 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
120 #  include <windows.h>
121 # endif                         /* !(OPENSSL_SYS_WINDOWS ||
122                                  * OPENSSL_SYS_WIN32) */
123
124 # ifdef ZLIB_SHARED
125 #  include <openssl/dso.h>
126
127 /* Function pointers */
128 typedef int (*compress_ft) (Bytef *dest, uLongf * destLen,
129                             const Bytef *source, uLong sourceLen);
130 typedef int (*inflateEnd_ft) (z_streamp strm);
131 typedef int (*inflate_ft) (z_streamp strm, int flush);
132 typedef int (*inflateInit__ft) (z_streamp strm,
133                                 const char *version, int stream_size);
134 typedef int (*deflateEnd_ft) (z_streamp strm);
135 typedef int (*deflate_ft) (z_streamp strm, int flush);
136 typedef int (*deflateInit__ft) (z_streamp strm, int level,
137                                 const char *version, int stream_size);
138 typedef const char *(*zError__ft) (int err);
139 static compress_ft p_compress = NULL;
140 static inflateEnd_ft p_inflateEnd = NULL;
141 static inflate_ft p_inflate = NULL;
142 static inflateInit__ft p_inflateInit_ = NULL;
143 static deflateEnd_ft p_deflateEnd = NULL;
144 static deflate_ft p_deflate = NULL;
145 static deflateInit__ft p_deflateInit_ = NULL;
146 static zError__ft p_zError = NULL;
147
148 static int zlib_loaded = 0;     /* only attempt to init func pts once */
149 static DSO *zlib_dso = NULL;
150
151 #  define compress                p_compress
152 #  define inflateEnd              p_inflateEnd
153 #  define inflate                 p_inflate
154 #  define inflateInit_            p_inflateInit_
155 #  define deflateEnd              p_deflateEnd
156 #  define deflate                 p_deflate
157 #  define deflateInit_            p_deflateInit_
158 #  define zError                  p_zError
159 # endif                         /* ZLIB_SHARED */
160
161 struct zlib_state {
162     z_stream istream;
163     z_stream ostream;
164 };
165
166 static int zlib_stateful_init(COMP_CTX *ctx)
167 {
168     int err;
169     struct zlib_state *state = OPENSSL_malloc(sizeof(*state));
170
171     if (state == NULL)
172         goto err;
173
174     state->istream.zalloc = zlib_zalloc;
175     state->istream.zfree = zlib_zfree;
176     state->istream.opaque = Z_NULL;
177     state->istream.next_in = Z_NULL;
178     state->istream.next_out = Z_NULL;
179     state->istream.avail_in = 0;
180     state->istream.avail_out = 0;
181     err = inflateInit_(&state->istream, ZLIB_VERSION, sizeof(z_stream));
182     if (err != Z_OK)
183         goto err;
184
185     state->ostream.zalloc = zlib_zalloc;
186     state->ostream.zfree = zlib_zfree;
187     state->ostream.opaque = Z_NULL;
188     state->ostream.next_in = Z_NULL;
189     state->ostream.next_out = Z_NULL;
190     state->ostream.avail_in = 0;
191     state->ostream.avail_out = 0;
192     err = deflateInit_(&state->ostream, Z_DEFAULT_COMPRESSION,
193                        ZLIB_VERSION, sizeof(z_stream));
194     if (err != Z_OK)
195         goto err;
196
197     ctx->data = state;
198     return 1;
199  err:
200     OPENSSL_free(state);
201     return 0;
202 }
203
204 static void zlib_stateful_finish(COMP_CTX *ctx)
205 {
206     struct zlib_state *state = ctx->data;
207     inflateEnd(&state->istream);
208     deflateEnd(&state->ostream);
209     OPENSSL_free(state);
210 }
211
212 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
213                                         unsigned int olen, unsigned char *in,
214                                         unsigned int ilen)
215 {
216     int err = Z_OK;
217     struct zlib_state *state = ctx->data;
218
219     if (state == NULL)
220         return -1;
221
222     state->ostream.next_in = in;
223     state->ostream.avail_in = ilen;
224     state->ostream.next_out = out;
225     state->ostream.avail_out = olen;
226     if (ilen > 0)
227         err = deflate(&state->ostream, Z_SYNC_FLUSH);
228     if (err != Z_OK)
229         return -1;
230 # ifdef DEBUG_ZLIB
231     fprintf(stderr, "compress(%4d)->%4d %s\n",
232             ilen, olen - state->ostream.avail_out,
233             (ilen != olen - state->ostream.avail_out) ? "zlib" : "clear");
234 # endif
235     return olen - state->ostream.avail_out;
236 }
237
238 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
239                                       unsigned int olen, unsigned char *in,
240                                       unsigned int ilen)
241 {
242     int err = Z_OK;
243     struct zlib_state *state = ctx->data;
244
245     if (state == NULL)
246         return 0;
247
248     state->istream.next_in = in;
249     state->istream.avail_in = ilen;
250     state->istream.next_out = out;
251     state->istream.avail_out = olen;
252     if (ilen > 0)
253         err = inflate(&state->istream, Z_SYNC_FLUSH);
254     if (err != Z_OK)
255         return -1;
256 # ifdef DEBUG_ZLIB
257     fprintf(stderr, "expand(%4d)->%4d %s\n",
258             ilen, olen - state->istream.avail_out,
259             (ilen != olen - state->istream.avail_out) ? "zlib" : "clear");
260 # endif
261     return olen - state->istream.avail_out;
262 }
263
264 #endif
265
266 COMP_METHOD *COMP_zlib(void)
267 {
268     COMP_METHOD *meth = &zlib_method_nozlib;
269
270 #ifdef ZLIB_SHARED
271     if (!zlib_loaded) {
272 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
273         zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
274 # else
275         zlib_dso = DSO_load(NULL, "z", NULL, 0);
276 # endif
277         if (zlib_dso != NULL) {
278             p_compress = (compress_ft) DSO_bind_func(zlib_dso, "compress");
279             p_inflateEnd
280                 = (inflateEnd_ft) DSO_bind_func(zlib_dso, "inflateEnd");
281             p_inflate = (inflate_ft) DSO_bind_func(zlib_dso, "inflate");
282             p_inflateInit_
283                 = (inflateInit__ft) DSO_bind_func(zlib_dso, "inflateInit_");
284             p_deflateEnd
285                 = (deflateEnd_ft) DSO_bind_func(zlib_dso, "deflateEnd");
286             p_deflate = (deflate_ft) DSO_bind_func(zlib_dso, "deflate");
287             p_deflateInit_
288                 = (deflateInit__ft) DSO_bind_func(zlib_dso, "deflateInit_");
289             p_zError = (zError__ft) DSO_bind_func(zlib_dso, "zError");
290
291             if (p_compress && p_inflateEnd && p_inflate
292                 && p_inflateInit_ && p_deflateEnd
293                 && p_deflate && p_deflateInit_ && p_zError)
294                 zlib_loaded++;
295             if (zlib_loaded)
296                 meth = &zlib_stateful_method;
297         }
298     }
299 #endif
300 #if defined(ZLIB)
301     meth = &zlib_stateful_method;
302 #endif
303
304     return (meth);
305 }
306
307 void COMP_zlib_cleanup(void)
308 {
309 #ifdef ZLIB_SHARED
310     if (zlib_dso != NULL)
311         DSO_free(zlib_dso);
312     zlib_dso = NULL;
313 #endif
314 }
315
316 #ifdef ZLIB
317
318 /* Zlib based compression/decompression filter BIO */
319
320 typedef struct {
321     unsigned char *ibuf;        /* Input buffer */
322     int ibufsize;               /* Buffer size */
323     z_stream zin;               /* Input decompress context */
324     unsigned char *obuf;        /* Output buffer */
325     int obufsize;               /* Output buffer size */
326     unsigned char *optr;        /* Position in output buffer */
327     int ocount;                 /* Amount of data in output buffer */
328     int odone;                  /* deflate EOF */
329     int comp_level;             /* Compression level to use */
330     z_stream zout;              /* Output compression context */
331 } BIO_ZLIB_CTX;
332
333 # define ZLIB_DEFAULT_BUFSIZE 1024
334
335 static int bio_zlib_new(BIO *bi);
336 static int bio_zlib_free(BIO *bi);
337 static int bio_zlib_read(BIO *b, char *out, int outl);
338 static int bio_zlib_write(BIO *b, const char *in, int inl);
339 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr);
340 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp);
341
342 static BIO_METHOD bio_meth_zlib = {
343     BIO_TYPE_COMP,
344     "zlib",
345     bio_zlib_write,
346     bio_zlib_read,
347     NULL,
348     NULL,
349     bio_zlib_ctrl,
350     bio_zlib_new,
351     bio_zlib_free,
352     bio_zlib_callback_ctrl
353 };
354
355 BIO_METHOD *BIO_f_zlib(void)
356 {
357     return &bio_meth_zlib;
358 }
359
360 static int bio_zlib_new(BIO *bi)
361 {
362     BIO_ZLIB_CTX *ctx;
363 # ifdef ZLIB_SHARED
364     (void)COMP_zlib();
365     if (!zlib_loaded) {
366         COMPerr(COMP_F_BIO_ZLIB_NEW, COMP_R_ZLIB_NOT_SUPPORTED);
367         return 0;
368     }
369 # endif
370     ctx = OPENSSL_malloc(sizeof(*ctx));
371     if (!ctx) {
372         COMPerr(COMP_F_BIO_ZLIB_NEW, ERR_R_MALLOC_FAILURE);
373         return 0;
374     }
375     ctx->ibuf = NULL;
376     ctx->obuf = NULL;
377     ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE;
378     ctx->obufsize = ZLIB_DEFAULT_BUFSIZE;
379     ctx->zin.zalloc = Z_NULL;
380     ctx->zin.zfree = Z_NULL;
381     ctx->zin.next_in = NULL;
382     ctx->zin.avail_in = 0;
383     ctx->zin.next_out = NULL;
384     ctx->zin.avail_out = 0;
385     ctx->zout.zalloc = Z_NULL;
386     ctx->zout.zfree = Z_NULL;
387     ctx->zout.next_in = NULL;
388     ctx->zout.avail_in = 0;
389     ctx->zout.next_out = NULL;
390     ctx->zout.avail_out = 0;
391     ctx->odone = 0;
392     ctx->comp_level = Z_DEFAULT_COMPRESSION;
393     bi->init = 1;
394     bi->ptr = (char *)ctx;
395     bi->flags = 0;
396     return 1;
397 }
398
399 static int bio_zlib_free(BIO *bi)
400 {
401     BIO_ZLIB_CTX *ctx;
402     if (!bi)
403         return 0;
404     ctx = (BIO_ZLIB_CTX *) bi->ptr;
405     if (ctx->ibuf) {
406         /* Destroy decompress context */
407         inflateEnd(&ctx->zin);
408         OPENSSL_free(ctx->ibuf);
409     }
410     if (ctx->obuf) {
411         /* Destroy compress context */
412         deflateEnd(&ctx->zout);
413         OPENSSL_free(ctx->obuf);
414     }
415     OPENSSL_free(ctx);
416     bi->ptr = NULL;
417     bi->init = 0;
418     bi->flags = 0;
419     return 1;
420 }
421
422 static int bio_zlib_read(BIO *b, char *out, int outl)
423 {
424     BIO_ZLIB_CTX *ctx;
425     int ret;
426     z_stream *zin;
427     if (!out || !outl)
428         return 0;
429     ctx = (BIO_ZLIB_CTX *) b->ptr;
430     zin = &ctx->zin;
431     BIO_clear_retry_flags(b);
432     if (!ctx->ibuf) {
433         ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
434         if (!ctx->ibuf) {
435             COMPerr(COMP_F_BIO_ZLIB_READ, ERR_R_MALLOC_FAILURE);
436             return 0;
437         }
438         inflateInit(zin);
439         zin->next_in = ctx->ibuf;
440         zin->avail_in = 0;
441     }
442
443     /* Copy output data directly to supplied buffer */
444     zin->next_out = (unsigned char *)out;
445     zin->avail_out = (unsigned int)outl;
446     for (;;) {
447         /* Decompress while data available */
448         while (zin->avail_in) {
449             ret = inflate(zin, 0);
450             if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
451                 COMPerr(COMP_F_BIO_ZLIB_READ, COMP_R_ZLIB_INFLATE_ERROR);
452                 ERR_add_error_data(2, "zlib error:", zError(ret));
453                 return 0;
454             }
455             /* If EOF or we've read everything then return */
456             if ((ret == Z_STREAM_END) || !zin->avail_out)
457                 return outl - zin->avail_out;
458         }
459
460         /*
461          * No data in input buffer try to read some in, if an error then
462          * return the total data read.
463          */
464         ret = BIO_read(b->next_bio, ctx->ibuf, ctx->ibufsize);
465         if (ret <= 0) {
466             /* Total data read */
467             int tot = outl - zin->avail_out;
468             BIO_copy_next_retry(b);
469             if (ret < 0)
470                 return (tot > 0) ? tot : ret;
471             return tot;
472         }
473         zin->avail_in = ret;
474         zin->next_in = ctx->ibuf;
475     }
476 }
477
478 static int bio_zlib_write(BIO *b, const char *in, int inl)
479 {
480     BIO_ZLIB_CTX *ctx;
481     int ret;
482     z_stream *zout;
483     if (!in || !inl)
484         return 0;
485     ctx = (BIO_ZLIB_CTX *) b->ptr;
486     if (ctx->odone)
487         return 0;
488     zout = &ctx->zout;
489     BIO_clear_retry_flags(b);
490     if (!ctx->obuf) {
491         ctx->obuf = OPENSSL_malloc(ctx->obufsize);
492         /* Need error here */
493         if (!ctx->obuf) {
494             COMPerr(COMP_F_BIO_ZLIB_WRITE, ERR_R_MALLOC_FAILURE);
495             return 0;
496         }
497         ctx->optr = ctx->obuf;
498         ctx->ocount = 0;
499         deflateInit(zout, ctx->comp_level);
500         zout->next_out = ctx->obuf;
501         zout->avail_out = ctx->obufsize;
502     }
503     /* Obtain input data directly from supplied buffer */
504     zout->next_in = (void *)in;
505     zout->avail_in = inl;
506     for (;;) {
507         /* If data in output buffer write it first */
508         while (ctx->ocount) {
509             ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount);
510             if (ret <= 0) {
511                 /* Total data written */
512                 int tot = inl - zout->avail_in;
513                 BIO_copy_next_retry(b);
514                 if (ret < 0)
515                     return (tot > 0) ? tot : ret;
516                 return tot;
517             }
518             ctx->optr += ret;
519             ctx->ocount -= ret;
520         }
521
522         /* Have we consumed all supplied data? */
523         if (!zout->avail_in)
524             return inl;
525
526         /* Compress some more */
527
528         /* Reset buffer */
529         ctx->optr = ctx->obuf;
530         zout->next_out = ctx->obuf;
531         zout->avail_out = ctx->obufsize;
532         /* Compress some more */
533         ret = deflate(zout, 0);
534         if (ret != Z_OK) {
535             COMPerr(COMP_F_BIO_ZLIB_WRITE, COMP_R_ZLIB_DEFLATE_ERROR);
536             ERR_add_error_data(2, "zlib error:", zError(ret));
537             return 0;
538         }
539         ctx->ocount = ctx->obufsize - zout->avail_out;
540     }
541 }
542
543 static int bio_zlib_flush(BIO *b)
544 {
545     BIO_ZLIB_CTX *ctx;
546     int ret;
547     z_stream *zout;
548     ctx = (BIO_ZLIB_CTX *) b->ptr;
549     /* If no data written or already flush show success */
550     if (!ctx->obuf || (ctx->odone && !ctx->ocount))
551         return 1;
552     zout = &ctx->zout;
553     BIO_clear_retry_flags(b);
554     /* No more input data */
555     zout->next_in = NULL;
556     zout->avail_in = 0;
557     for (;;) {
558         /* If data in output buffer write it first */
559         while (ctx->ocount) {
560             ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount);
561             if (ret <= 0) {
562                 BIO_copy_next_retry(b);
563                 return ret;
564             }
565             ctx->optr += ret;
566             ctx->ocount -= ret;
567         }
568         if (ctx->odone)
569             return 1;
570
571         /* Compress some more */
572
573         /* Reset buffer */
574         ctx->optr = ctx->obuf;
575         zout->next_out = ctx->obuf;
576         zout->avail_out = ctx->obufsize;
577         /* Compress some more */
578         ret = deflate(zout, Z_FINISH);
579         if (ret == Z_STREAM_END)
580             ctx->odone = 1;
581         else if (ret != Z_OK) {
582             COMPerr(COMP_F_BIO_ZLIB_FLUSH, COMP_R_ZLIB_DEFLATE_ERROR);
583             ERR_add_error_data(2, "zlib error:", zError(ret));
584             return 0;
585         }
586         ctx->ocount = ctx->obufsize - zout->avail_out;
587     }
588 }
589
590 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
591 {
592     BIO_ZLIB_CTX *ctx;
593     int ret, *ip;
594     int ibs, obs;
595     if (!b->next_bio)
596         return 0;
597     ctx = (BIO_ZLIB_CTX *) b->ptr;
598     switch (cmd) {
599
600     case BIO_CTRL_RESET:
601         ctx->ocount = 0;
602         ctx->odone = 0;
603         ret = 1;
604         break;
605
606     case BIO_CTRL_FLUSH:
607         ret = bio_zlib_flush(b);
608         if (ret > 0)
609             ret = BIO_flush(b->next_bio);
610         break;
611
612     case BIO_C_SET_BUFF_SIZE:
613         ibs = -1;
614         obs = -1;
615         if (ptr != NULL) {
616             ip = ptr;
617             if (*ip == 0)
618                 ibs = (int)num;
619             else
620                 obs = (int)num;
621         } else {
622             ibs = (int)num;
623             obs = ibs;
624         }
625
626         if (ibs != -1) {
627             OPENSSL_free(ctx->ibuf);
628             ctx->ibuf = NULL;
629             ctx->ibufsize = ibs;
630         }
631
632         if (obs != -1) {
633             OPENSSL_free(ctx->obuf);
634             ctx->obuf = NULL;
635             ctx->obufsize = obs;
636         }
637         ret = 1;
638         break;
639
640     case BIO_C_DO_STATE_MACHINE:
641         BIO_clear_retry_flags(b);
642         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
643         BIO_copy_next_retry(b);
644         break;
645
646     default:
647         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
648         break;
649
650     }
651
652     return ret;
653 }
654
655 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
656 {
657     if (!b->next_bio)
658         return 0;
659     return BIO_callback_ctrl(b->next_bio, cmd, fp);
660 }
661
662 #endif