2 * Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
13 #include <openssl/objects.h>
14 #include "internal/comp.h"
15 #include <openssl/err.h>
16 #include "crypto/cryptlib.h"
17 #include "internal/bio.h"
18 #include "internal/thread_once.h"
19 #include "comp_local.h"
21 COMP_METHOD *COMP_zlib(void);
23 #ifdef OPENSSL_NO_ZLIB
29 static int zlib_stateful_init(COMP_CTX *ctx);
30 static void zlib_stateful_finish(COMP_CTX *ctx);
31 static ossl_ssize_t zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
32 size_t olen, unsigned char *in,
34 static ossl_ssize_t zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
35 size_t olen, unsigned char *in,
38 /* memory allocations functions for zlib initialisation */
39 static void *zlib_zalloc(void *opaque, unsigned int no, unsigned int size)
43 p = OPENSSL_zalloc(no * size);
47 static void zlib_zfree(void *opaque, void *address)
49 OPENSSL_free(address);
53 static COMP_METHOD zlib_stateful_method = {
58 zlib_stateful_compress_block,
59 zlib_stateful_expand_block
63 * When OpenSSL is built on Windows, we do not want to require that
64 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
65 * work. Therefore, all ZLIB routines are loaded at run time
66 * and we do not link to a .LIB file when ZLIB_SHARED is set.
68 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
70 # endif /* !(OPENSSL_SYS_WINDOWS ||
71 * OPENSSL_SYS_WIN32) */
74 # include "internal/dso.h"
76 /* Function pointers */
77 typedef int (*compress_ft) (Bytef *dest, uLongf *destLen,
78 const Bytef *source, uLong sourceLen);
79 typedef int (*uncompress_ft) (Bytef *dest, uLongf *destLen,
80 const Bytef *source, uLong sourceLen);
81 typedef int (*inflateEnd_ft) (z_streamp strm);
82 typedef int (*inflate_ft) (z_streamp strm, int flush);
83 typedef int (*inflateInit__ft) (z_streamp strm,
84 const char *version, int stream_size);
85 typedef int (*deflateEnd_ft) (z_streamp strm);
86 typedef int (*deflate_ft) (z_streamp strm, int flush);
87 typedef int (*deflateInit__ft) (z_streamp strm, int level,
88 const char *version, int stream_size);
89 typedef const char *(*zError__ft) (int err);
90 static compress_ft p_compress = NULL;
91 static uncompress_ft p_uncompress = NULL;
92 static inflateEnd_ft p_inflateEnd = NULL;
93 static inflate_ft p_inflate = NULL;
94 static inflateInit__ft p_inflateInit_ = NULL;
95 static deflateEnd_ft p_deflateEnd = NULL;
96 static deflate_ft p_deflate = NULL;
97 static deflateInit__ft p_deflateInit_ = NULL;
98 static zError__ft p_zError = NULL;
100 static DSO *zlib_dso = NULL;
102 # define compress p_compress
103 # define uncompress p_uncompress
104 # define inflateEnd p_inflateEnd
105 # define inflate p_inflate
106 # define inflateInit_ p_inflateInit_
107 # define deflateEnd p_deflateEnd
108 # define deflate p_deflate
109 # define deflateInit_ p_deflateInit_
110 # define zError p_zError
111 # endif /* ZLIB_SHARED */
118 static int zlib_stateful_init(COMP_CTX *ctx)
121 struct zlib_state *state = OPENSSL_zalloc(sizeof(*state));
126 state->istream.zalloc = zlib_zalloc;
127 state->istream.zfree = zlib_zfree;
128 state->istream.opaque = Z_NULL;
129 state->istream.next_in = Z_NULL;
130 state->istream.next_out = Z_NULL;
131 err = inflateInit_(&state->istream, ZLIB_VERSION, sizeof(z_stream));
135 state->ostream.zalloc = zlib_zalloc;
136 state->ostream.zfree = zlib_zfree;
137 state->ostream.opaque = Z_NULL;
138 state->ostream.next_in = Z_NULL;
139 state->ostream.next_out = Z_NULL;
140 err = deflateInit_(&state->ostream, Z_DEFAULT_COMPRESSION,
141 ZLIB_VERSION, sizeof(z_stream));
152 static void zlib_stateful_finish(COMP_CTX *ctx)
154 struct zlib_state *state = ctx->data;
155 inflateEnd(&state->istream);
156 deflateEnd(&state->ostream);
160 static ossl_ssize_t zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
161 size_t olen, unsigned char *in,
165 struct zlib_state *state = ctx->data;
170 state->ostream.next_in = in;
171 state->ostream.avail_in = ilen;
172 state->ostream.next_out = out;
173 state->ostream.avail_out = olen;
175 err = deflate(&state->ostream, Z_SYNC_FLUSH);
178 if (state->ostream.avail_out > olen)
180 return (ossl_ssize_t)(olen - state->ostream.avail_out);
183 static ossl_ssize_t zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
184 size_t olen, unsigned char *in,
188 struct zlib_state *state = ctx->data;
193 state->istream.next_in = in;
194 state->istream.avail_in = ilen;
195 state->istream.next_out = out;
196 state->istream.avail_out = olen;
198 err = inflate(&state->istream, Z_SYNC_FLUSH);
201 if (state->istream.avail_out > olen)
203 return (ossl_ssize_t)(olen - state->istream.avail_out);
206 /* ONESHOT COMPRESSION/DECOMPRESSION */
208 static int zlib_oneshot_init(COMP_CTX *ctx)
213 static void zlib_oneshot_finish(COMP_CTX *ctx)
217 static ossl_ssize_t zlib_oneshot_compress_block(COMP_CTX *ctx, unsigned char *out,
218 size_t olen, unsigned char *in,
226 /* zlib's uLongf defined as unsigned long FAR */
227 if (olen > ULONG_MAX)
229 out_size = (uLongf)olen;
231 if (compress(out, &out_size, in, ilen) != Z_OK)
234 if (out_size > OSSL_SSIZE_MAX)
236 return (ossl_ssize_t)out_size;
239 static ossl_ssize_t zlib_oneshot_expand_block(COMP_CTX *ctx, unsigned char *out,
240 size_t olen, unsigned char *in,
248 /* zlib's uLongf defined as unsigned long FAR */
249 if (olen > ULONG_MAX)
251 out_size = (uLongf)olen;
253 if (uncompress(out, &out_size, in, ilen) != Z_OK)
256 if (out_size > OSSL_SSIZE_MAX)
258 return (ossl_ssize_t)out_size;
261 static COMP_METHOD zlib_oneshot_method = {
262 NID_zlib_compression,
266 zlib_oneshot_compress_block,
267 zlib_oneshot_expand_block
270 static CRYPTO_ONCE zlib_once = CRYPTO_ONCE_STATIC_INIT;
271 DEFINE_RUN_ONCE_STATIC(ossl_comp_zlib_init)
274 /* LIBZ may be externally defined, and we should respect that value */
276 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
277 # define LIBZ "ZLIB1"
278 # elif defined(OPENSSL_SYS_VMS)
285 zlib_dso = DSO_load(NULL, LIBZ, NULL, 0);
286 if (zlib_dso != NULL) {
287 p_compress = (compress_ft) DSO_bind_func(zlib_dso, "compress");
288 p_uncompress = (compress_ft) DSO_bind_func(zlib_dso, "uncompress");
289 p_inflateEnd = (inflateEnd_ft) DSO_bind_func(zlib_dso, "inflateEnd");
290 p_inflate = (inflate_ft) DSO_bind_func(zlib_dso, "inflate");
291 p_inflateInit_ = (inflateInit__ft) DSO_bind_func(zlib_dso, "inflateInit_");
292 p_deflateEnd = (deflateEnd_ft) DSO_bind_func(zlib_dso, "deflateEnd");
293 p_deflate = (deflate_ft) DSO_bind_func(zlib_dso, "deflate");
294 p_deflateInit_ = (deflateInit__ft) DSO_bind_func(zlib_dso, "deflateInit_");
295 p_zError = (zError__ft) DSO_bind_func(zlib_dso, "zError");
297 if (p_compress == NULL || p_uncompress == NULL || p_inflateEnd == NULL
298 || p_inflate == NULL || p_inflateInit_ == NULL
299 || p_deflateEnd == NULL || p_deflate == NULL
300 || p_deflateInit_ == NULL || p_zError == NULL) {
301 ossl_comp_zlib_cleanup();
310 COMP_METHOD *COMP_zlib(void)
312 COMP_METHOD *meth = NULL;
314 #ifndef OPENSSL_NO_ZLIB
315 if (RUN_ONCE(&zlib_once, ossl_comp_zlib_init))
316 meth = &zlib_stateful_method;
322 COMP_METHOD *COMP_zlib_oneshot(void)
324 COMP_METHOD *meth = NULL;
326 #ifndef OPENSSL_NO_ZLIB
327 if (RUN_ONCE(&zlib_once, ossl_comp_zlib_init))
328 meth = &zlib_oneshot_method;
334 /* Also called from OPENSSL_cleanup() */
335 void ossl_comp_zlib_cleanup(void)
343 #ifndef OPENSSL_NO_ZLIB
345 /* Zlib based compression/decompression filter BIO */
348 unsigned char *ibuf; /* Input buffer */
349 int ibufsize; /* Buffer size */
350 z_stream zin; /* Input decompress context */
351 unsigned char *obuf; /* Output buffer */
352 int obufsize; /* Output buffer size */
353 unsigned char *optr; /* Position in output buffer */
354 int ocount; /* Amount of data in output buffer */
355 int odone; /* deflate EOF */
356 int comp_level; /* Compression level to use */
357 z_stream zout; /* Output compression context */
360 # define ZLIB_DEFAULT_BUFSIZE 1024
362 static int bio_zlib_new(BIO *bi);
363 static int bio_zlib_free(BIO *bi);
364 static int bio_zlib_read(BIO *b, char *out, int outl);
365 static int bio_zlib_write(BIO *b, const char *in, int inl);
366 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr);
367 static long bio_zlib_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp);
369 static const BIO_METHOD bio_meth_zlib = {
376 NULL, /* bio_zlib_puts, */
377 NULL, /* bio_zlib_gets, */
381 bio_zlib_callback_ctrl
385 const BIO_METHOD *BIO_f_zlib(void)
387 #ifndef OPENSSL_NO_ZLIB
388 if (RUN_ONCE(&zlib_once, ossl_comp_zlib_init))
389 return &bio_meth_zlib;
394 #ifndef OPENSSL_NO_ZLIB
395 static int bio_zlib_new(BIO *bi)
400 if (!RUN_ONCE(&zlib_once, ossl_comp_zlib_init)) {
401 ERR_raise(ERR_LIB_COMP, COMP_R_ZLIB_NOT_SUPPORTED);
405 ctx = OPENSSL_zalloc(sizeof(*ctx));
408 ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE;
409 ctx->obufsize = ZLIB_DEFAULT_BUFSIZE;
410 ctx->zin.zalloc = Z_NULL;
411 ctx->zin.zfree = Z_NULL;
412 ctx->zout.zalloc = Z_NULL;
413 ctx->zout.zfree = Z_NULL;
414 ctx->comp_level = Z_DEFAULT_COMPRESSION;
416 BIO_set_data(bi, ctx);
421 static int bio_zlib_free(BIO *bi)
427 ctx = BIO_get_data(bi);
429 /* Destroy decompress context */
430 inflateEnd(&ctx->zin);
431 OPENSSL_free(ctx->ibuf);
434 /* Destroy compress context */
435 deflateEnd(&ctx->zout);
436 OPENSSL_free(ctx->obuf);
439 BIO_set_data(bi, NULL);
445 static int bio_zlib_read(BIO *b, char *out, int outl)
450 BIO *next = BIO_next(b);
454 ctx = BIO_get_data(b);
456 BIO_clear_retry_flags(b);
458 ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
459 if (ctx->ibuf == NULL)
461 if ((ret = inflateInit(zin)) != Z_OK) {
462 ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_INFLATE_ERROR,
463 "zlib error: %s", zError(ret));
466 zin->next_in = ctx->ibuf;
470 /* Copy output data directly to supplied buffer */
471 zin->next_out = (unsigned char *)out;
472 zin->avail_out = (unsigned int)outl;
474 /* Decompress while data available */
475 while (zin->avail_in) {
476 ret = inflate(zin, 0);
477 if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
478 ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_INFLATE_ERROR,
479 "zlib error: %s", zError(ret));
482 /* If EOF or we've read everything then return */
483 if ((ret == Z_STREAM_END) || !zin->avail_out)
484 return outl - zin->avail_out;
488 * No data in input buffer try to read some in, if an error then
489 * return the total data read.
491 ret = BIO_read(next, ctx->ibuf, ctx->ibufsize);
493 /* Total data read */
494 int tot = outl - zin->avail_out;
495 BIO_copy_next_retry(b);
497 return (tot > 0) ? tot : ret;
501 zin->next_in = ctx->ibuf;
505 static int bio_zlib_write(BIO *b, const char *in, int inl)
510 BIO *next = BIO_next(b);
514 ctx = BIO_get_data(b);
518 BIO_clear_retry_flags(b);
520 ctx->obuf = OPENSSL_malloc(ctx->obufsize);
521 /* Need error here */
522 if (ctx->obuf == NULL)
524 ctx->optr = ctx->obuf;
526 if ((ret = deflateInit(zout, ctx->comp_level)) != Z_OK) {
527 ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_DEFLATE_ERROR,
528 "zlib error: %s", zError(ret));
531 zout->next_out = ctx->obuf;
532 zout->avail_out = ctx->obufsize;
534 /* Obtain input data directly from supplied buffer */
535 zout->next_in = (void *)in;
536 zout->avail_in = inl;
538 /* If data in output buffer write it first */
539 while (ctx->ocount) {
540 ret = BIO_write(next, ctx->optr, ctx->ocount);
542 /* Total data written */
543 int tot = inl - zout->avail_in;
544 BIO_copy_next_retry(b);
546 return (tot > 0) ? tot : ret;
553 /* Have we consumed all supplied data? */
557 /* Compress some more */
560 ctx->optr = ctx->obuf;
561 zout->next_out = ctx->obuf;
562 zout->avail_out = ctx->obufsize;
563 /* Compress some more */
564 ret = deflate(zout, 0);
566 ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_DEFLATE_ERROR,
567 "zlib error: %s", zError(ret));
570 ctx->ocount = ctx->obufsize - zout->avail_out;
574 static int bio_zlib_flush(BIO *b)
579 BIO *next = BIO_next(b);
581 ctx = BIO_get_data(b);
582 /* If no data written or already flush show success */
583 if (!ctx->obuf || (ctx->odone && !ctx->ocount))
586 BIO_clear_retry_flags(b);
587 /* No more input data */
588 zout->next_in = NULL;
591 /* If data in output buffer write it first */
592 while (ctx->ocount) {
593 ret = BIO_write(next, ctx->optr, ctx->ocount);
595 BIO_copy_next_retry(b);
604 /* Compress some more */
607 ctx->optr = ctx->obuf;
608 zout->next_out = ctx->obuf;
609 zout->avail_out = ctx->obufsize;
610 /* Compress some more */
611 ret = deflate(zout, Z_FINISH);
612 if (ret == Z_STREAM_END)
614 else if (ret != Z_OK) {
615 ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_DEFLATE_ERROR,
616 "zlib error: %s", zError(ret));
619 ctx->ocount = ctx->obufsize - zout->avail_out;
623 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
628 BIO *next = BIO_next(b);
632 ctx = BIO_get_data(b);
642 ret = bio_zlib_flush(b);
644 ret = BIO_flush(next);
647 case BIO_C_SET_BUFF_SIZE:
662 OPENSSL_free(ctx->ibuf);
668 OPENSSL_free(ctx->obuf);
675 case BIO_C_DO_STATE_MACHINE:
676 BIO_clear_retry_flags(b);
677 ret = BIO_ctrl(next, cmd, num, ptr);
678 BIO_copy_next_retry(b);
681 case BIO_CTRL_WPENDING:
682 if (ctx->obuf == NULL)
690 /* Unknown amount pending but we are not finished */
694 ret = BIO_ctrl(next, cmd, num, ptr);
697 case BIO_CTRL_PENDING:
698 ret = ctx->zin.avail_in;
700 ret = BIO_ctrl(next, cmd, num, ptr);
704 ret = BIO_ctrl(next, cmd, num, ptr);
712 static long bio_zlib_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
714 BIO *next = BIO_next(b);
718 return BIO_callback_ctrl(next, cmd, fp);