BIO_NEW(3)

πŸ“¦ version 4.4.0, in alpine 3.24, arch rolling

πŸ’… BIO_NEW(3) πŸ’…

okay so bestie, this one is from the Library Functions Manual β€” yes, that manual β€” and we're about to get into the I/O abstraction glow-up of the century. ✨

πŸŽ€ NAME πŸŽ€

BIO_new, BIO_up_ref, BIO_set, BIO_free, BIO_vfree, BIO_free_all β€” construct and destruct I/O abstraction objects. no cap. 🫢

πŸ“š SYNOPSIS πŸ“š

library β€œlibcrypto” #include <openssl/bio.h>

BIO *
BIO_new(const BIO_METHOD *type);

int
BIO_up_ref(BIO *a);

int
BIO_set(BIO *a, const BIO_METHOD *type);

int
BIO_free(BIO *a);

void
BIO_vfree(BIO *a);

void
BIO_free_all(BIO *a);

✨ DESCRIPTION ✨

okay so a BIO is an I/O abstraction object, and it is literally hiding a bunch of the underlying I/O details from your application. the way it works? if an application uses BIOs for its I/O, it can transparently handle SSL connections, unencrypted network connections, and file I/O. iconic. πŸ’–

BIO_new() constructs a new BIO using the method type and sets its reference count to 1. there are two groups of BIO types, bestie: source/sink BIOs and filter BIOs.

Source/sink BIOs provide input or consume output. examples include socket BIOs and file BIOs.

Filter BIOs take data from one BIO and pass it through to another, or to the application, forming a chain of BIOs. the data may be left unmodified (for example by a message digest BIO) or translated (for example by an encryption BIO). the effect of a filter BIO may change according to the I/O operation it is performing: for example an encryption BIO encrypts data if it is written to and decrypts data if it is read from. it's giving shape-shifter behavior. πŸ¦‹

some BIOs (such as memory BIOs) can be used immediately after calling BIO_new(). others (such as file BIOs) need some additional initialization, and utility functions exist to construct and initialize such BIOs.

normally the type argument is supplied by a function which returns a pointer to a BIO_METHOD. there is a naming convention for such functions: the methods for source/sink BIOs are called BIO_s_*() and those for filter BIOs BIO_f_*(). iykyk. 🌟

BIO_up_ref() increments the reference count of a by 1. simple as that, no notes. πŸ’…

BIO_set() is a deprecated function to initialize an unused BIO structure located in static memory or on the stack, to set its method to type, and to set its reference count to 1. it must not be called on BIO objects created with BIO_new(), nor on objects that were already used. seriously, bestie, this is legacy energy β€” do not use it on those. πŸ™ˆ

BIO_free() and BIO_vfree() decrement the reference count of a by 1, and if the reference count reaches 0, they destruct the single BIO a, which may also have some effect on the underlying I/O structure β€” for example it may close the file being referred to under certain circumstances. if a is a NULL pointer, no action occurs. if BIO_free() is called on a BIO chain, it destructs at most one BIO, resulting in a memory leak. memory leak, bestie! πŸ’€

BIO_free_all() calls BIO_free() on a and on all following BIO objects in the chain. as soon as the reference count of a BIO is still non-zero after calling BIO_free() on it, the function BIO_free_all() returns right away and refrains from freeing the remaining BIO objects in the chain. it does not halt if an error occurs destructing an individual BIO in the chain. if a is a NULL pointer, no action occurs. calling BIO_free_all() on a single BIO has the same effect as BIO_vfree(). periodt. πŸ‘‘

common I/O functions are documented in BIO_read(3). forming chains is explained in BIO_push(3); inspecting them is explained in BIO_find_type(3). for more details about the different kinds of BIOs, see the individual BIO_METHOD manual pages. go read those too, bestie. πŸ“–

πŸ¦‹ RETURN VALUES πŸ¦‹

BIO_new() returns a newly constructed BIO object or NULL on failure.

BIO_up_ref(), BIO_set(), and BIO_free() return 1 for success or 0 for failure. that's the whole vibe check. βœ…

πŸ“ EXAMPLES πŸ“

create a memory BIO:

BIO *mem = BIO_new(BIO_s_mem());

🌈 SEE ALSO 🌈

okay these are the friends of this page, all of them:

πŸ’œ HISTORY πŸ’œ

BIO_new(), BIO_set(), and BIO_free() first appeared in SSLeay 0.6.0. BIO_free_all() first appeared in SSLeay 0.6.6. all these functions have been available since OpenBSD 2.4.

BIO_vfree() first appeared in OpenSSL 0.9.6 and has been available since OpenBSD 2.9.

BIO_up_ref() first appeared in OpenSSL 1.1.0 and has been available since OpenBSD 6.3. history lesson, slay. πŸ“œ

mwah πŸ’‹ source/version: Debian; date: June 8, 2025; page: BIO_NEW(3). bye bestie! 🫢

πŸ“¦ where this page comes from