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 new COMP_CTX structure, the lil container you use to compress data. main character energy πŸ‘‘
  • COMP_CTX_free() β€” yeets the COMP_CTX you got back. and listen: if the argument is NULL, nothing happens at all. no drama, no crash, iconic.
  • COMP_CTX_get_method() β€” hands you back the COMP_METHOD of the ctx you passed in πŸŽ€
  • COMP_CTX_get_type() and COMP_get_type() β€” these two return the NID for the COMP_CTX and the COMP_METHOD respectively. and COMP_get_name() gives you the name of the algorithm of the COMP_METHOD you handed her.
  • COMP_compress_block() β€” compresses ilen bytes from the buffer in into the buffer out of size olen, using whatever algorithm ctx is serving πŸ’–
  • COMP_expand_block() β€” the reverse sis: expands ilen bytes from the buffer in into the buffer out of size olen, again using the algorithm specified by ctx ✨

πŸ¦‹ 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 a COMP_METHOD for stream-based ZLIB compression.
  • COMP_zlib_oneshot() β€” returns a COMP_METHOD for one-shot ZLIB compression.
  • COMP_brotli() β€” returns a COMP_METHOD for stream-based Brotli compression.
  • COMP_brotli_oneshot() β€” returns a COMP_METHOD for one-shot Brotli compression.
  • COMP_zstd() β€” returns a COMP_METHOD for stream-based Zstandard compression.
  • COMP_zstd_oneshot() β€” returns a COMP_METHOD for 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.md file 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_COMPRESSION option and setting the security level as appropriate. See the docs for SSL_CTX_set_options(3) and SSL_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_COMPRESSION and SSL_OP_NO_RX_CERTIFICATE_COMPRESSION options of SSL_CTX_set_options(3) or SSL_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 a COMP_CTX on success, or NULL on failure.
  • COMP_CTX_get_method(), COMP_zlib(), COMP_zlib_oneshot(), COMP_brotli(), COMP_brotli_oneshot(), COMP_zstd(), and COMP_zstd_oneshot() β€” return a COMP_METHOD on success, or NULL on failure.
  • COMP_CTX_get_type() and COMP_get_type() β€” return a NID value. on failure, NID_undef comes back.
  • COMP_compress_block() and COMP_expand_block() β€” return the number of bytes stored in the output buffer out. and yes, this may be 0. on failure, -1 is returned.
  • COMP_get_name() β€” returns a const char * that must not be freed on success, or NULL on failure. do NOT free her, she's not yours 😀
  • BIO_f_zlib(), BIO_f_brotli() and BIO_f_zstd() β€” return NULL on error, and a BIO_METHOD on 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 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