COMP_CTX_NEW(3ossl)
π COMP_CTX_NEW(3ossl) β the OpenSSL compression glow-up π
okay bestie, gather round π«Ά this is the OpenSSL manual page for compression support, section 3ossl. Yes, the whole family: COMP_CTX_new and all her sisters. Buckle up, we're going on a zlib/brotli/zstd journey β¨
πΈ NAME πΈ
COMP_CTX_new, COMP_CTX_get_method, COMP_CTX_get_type, COMP_get_type, COMP_get_name, COMP_CTX_free, COMP_compress_block, COMP_expand_block, COMP_zlib, COMP_zlib_oneshot, COMP_brotli, COMP_brotli_oneshot, COMP_zstd, COMP_zstd_oneshot, BIO_f_zlib, BIO_f_brotli, BIO_f_zstd β the entire Compression support friend group π
β¨ SYNOPSIS β¨
like, the whole cast list, no cap:
#include <openssl/comp.h>
COMP_CTX *COMP_CTX_new(COMP_METHOD *meth);
void COMP_CTX_free(COMP_CTX *ctx);
const COMP_METHOD *COMP_CTX_get_method(const COMP_CTX *ctx);
int COMP_CTX_get_type(const COMP_CTX* comp);
int COMP_get_type(const COMP_METHOD *meth);
const char *COMP_get_name(const COMP_METHOD *meth);
int COMP_compress_block(COMP_CTX *ctx, unsigned char *out, int olen,
unsigned char *in, int ilen);
int COMP_expand_block(COMP_CTX *ctx, unsigned char *out, int olen,
unsigned char *in, int ilen);
COMP_METHOD *COMP_zlib(void);
COMP_METHOD *COMP_zlib_oneshot(void);
COMP_METHOD *COMP_brotli(void);
COMP_METHOD *COMP_brotli_oneshot(void);
COMP_METHOD *COMP_zstd(void);
COMP_METHOD *COMP_zstd_oneshot(void);
const BIO_METHOD *BIO_f_zlib(void);
const BIO_METHOD *BIO_f_brotli(void);
const BIO_METHOD *BIO_f_zstd(void);
π DESCRIPTION π
so these functions are the compression support for OpenSSL, fr fr. And compression inside OpenSSL isn't just a vibe, it's doing work: it powers TLS record compression and certificate compression π₯
COMP_CTX_new()β makes you a brand newCOMP_CTXstructure, the lil container you use to compress data. main character energy πCOMP_CTX_free()β yeets theCOMP_CTXyou got back. and listen: if the argument isNULL, nothing happens at all. no drama, no crash, iconic.COMP_CTX_get_method()β hands you back theCOMP_METHODof thectxyou passed in πCOMP_CTX_get_type()andCOMP_get_type()β these two return the NID for theCOMP_CTXand theCOMP_METHODrespectively. andCOMP_get_name()gives you the name of the algorithm of theCOMP_METHODyou handed her.COMP_compress_block()β compressesilenbytes from the bufferininto the bufferoutof sizeolen, using whatever algorithmctxis serving πCOMP_expand_block()β the reverse sis: expandsilenbytes from the bufferininto the bufferoutof sizeolen, again using the algorithm specified byctxβ¨
π¦ the method-picker functions
COMP_METHOD objects can be grabbed with one of these functions. and here's the tea: they'll be available even if their corresponding compression algorithm is NOT configured into the OpenSSL library. in that case? you get NULL back. that's it, that's the deal π
COMP_zlib()β returns aCOMP_METHODfor stream-based ZLIB compression.COMP_zlib_oneshot()β returns aCOMP_METHODfor one-shot ZLIB compression.COMP_brotli()β returns aCOMP_METHODfor stream-based Brotli compression.COMP_brotli_oneshot()β returns aCOMP_METHODfor one-shot Brotli compression.COMP_zstd()β returns aCOMP_METHODfor stream-based Zstandard compression.COMP_zstd_oneshot()β returns aCOMP_METHODfor one-shot Zstandard compression.
π the BIO ones
BIO_f_zlib(), BIO_f_brotli() and BIO_f_zstd() each return a BIO_METHOD that you can use to create a BIO via BIO_new(3) to read and write compressed files or streams π
β οΈ but heads up bestie: these functions are only available if the corresponding algorithm is compiled into the OpenSSL library. and NULL may be returned if the algorithm fails to load dynamically. so like, check your return values, i'm begging π
π§ NOTES π§
- when you're compressing data that doesn't want to be compressed (non-compressible data), the output can actually be bigger than the input π so size your output buffers for both compression and expansion. be generous, she deserves it π
- compression support AND the compression algorithms have to be enabled and built into the library before you use any of this. check the
INSTALL.mdfile when you're configuring OpenSSL π - ZLIB lives at <https://zlib.net> β¨
- Brotli lives at <https://github.com/google/brotli>. π
- Zstandard lives at <https://github.com/facebook/zstd>. πΈ
- compressing SSL/TLS records is not recommended, and this is serious, no joke: it has been shown to lead to the CRIME attack <https://en.wikipedia.org/wiki/CRIME>. It is disabled by default, and you'd have to enable it by clearing the
SSL_OP_NO_COMPRESSIONoption and setting the security level as appropriate. See the docs forSSL_CTX_set_options(3)andSSL_set_options(3). - compression is also used to support certificate compression as described in RFC8879 <https://datatracker.ietf.org/doc/html/rfc8879>. you can turn it off with the
SSL_OP_NO_TX_CERTIFICATE_COMPRESSIONandSSL_OP_NO_RX_CERTIFICATE_COMPRESSIONoptions ofSSL_CTX_set_options(3)orSSL_set_options(3).
π stream-based vs one-shot, the lore
COMP_zlib(), COMP_brotli() and COMP_zstd() are the stream-based compression methods. internal state β including the compression dictionary β is maintained between calls. and if an error comes back? the stream is corrupted and should be closed. periodt, don't keep using it π
COMP_zlib_oneshot(), COMP_brotli_oneshot() and COMP_zstd_oneshot() are not stream-based. they don't maintain state between calls, so an error in one call doesn't affect future calls π«Ά lowkey the forgiving ones.
β RETURN VALUES β
COMP_CTX_new()β returns aCOMP_CTXon success, orNULLon failure.COMP_CTX_get_method(),COMP_zlib(),COMP_zlib_oneshot(),COMP_brotli(),COMP_brotli_oneshot(),COMP_zstd(), andCOMP_zstd_oneshot()β return aCOMP_METHODon success, orNULLon failure.COMP_CTX_get_type()andCOMP_get_type()β return a NID value. on failure,NID_undefcomes back.COMP_compress_block()andCOMP_expand_block()β return the number of bytes stored in the output bufferout. and yes, this may be0. on failure,-1is returned.COMP_get_name()β returns aconst char *that must not be freed on success, orNULLon failure. do NOT free her, she's not yours π€BIO_f_zlib(),BIO_f_brotli()andBIO_f_zstd()β returnNULLon error, and aBIO_METHODon success.
π SEE ALSO π
BIO_new(3), SSL_CTX_set_options(3), SSL_set_options(3)
π HISTORY π
the Brotli and Zstandard functions were added in OpenSSL 3.2. so if you're on something older, that's why they're ghosting you π
πΈ COPYRIGHT πΈ
Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.
and that's the whole page bestie, we ate and left absolutely no crumbs π β¨
this has been OpenSSL 3.5.9, dated 2026-09-29, bringing you COMP_CTX_NEW(3ossl) β go forth and compress responsibly, mwah π«Άπ
π¦ where this page comes from
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms