BIO_S_BIO(3)
β¨ BIO_s_bio(3) β¨
okay so bestie, welcome to the Library Functions Manual entry for BIO_s_bio(3) β itβs giving libcrypto realness, and weβre about to make BIO pairs make sense. no cap. π
πΈ NAME πΈ
this whole squad is the BIO pair BIO situation:
BIO_s_bioBIO_make_bio_pairBIO_destroy_bio_pairBIO_shutdown_wrBIO_set_write_buf_sizeBIO_get_write_buf_sizeBIO_new_bio_pairBIO_get_write_guaranteeBIO_ctrl_get_write_guaranteeBIO_get_read_requestBIO_ctrl_get_read_requestBIO_ctrl_reset_read_request
β BIO pair BIO π
β¨ SYNOPSIS β¨
library βlibcryptoβ #include <openssl/bio.h>
const BIO_METHOD *
BIO_s_bio(void);
int
BIO_make_bio_pair(BIO *b1, BIO *b2);
int
BIO_destroy_bio_pair(BIO *b);
int
BIO_shutdown_wr(BIO *b);
int
BIO_set_write_buf_size(BIO *b, long size);
size_t
BIO_get_write_buf_size(BIO *b, long size);
int
BIO_new_bio_pair(BIO **bio1, size_t writebuf1, BIO **bio2,
size_t writebuf2);
int
BIO_get_write_guarantee(BIO *b);
size_t
BIO_ctrl_get_write_guarantee(BIO *b);
int
BIO_get_read_request(BIO *b);
size_t
BIO_ctrl_get_read_request(BIO *b);
int
BIO_ctrl_reset_read_request(BIO *b);
π DESCRIPTION π
BIO_s_bio() returns the method for a BIO pair. a BIO pair is literally a pair of source/sink BIOs where data written to either half of the pair is buffered and can be read from the other half. both halves must usually be handled by the same application thread since no locking is done on the internal data structures. π«Ά
since BIO chains typically end in a source/sink BIO, it is possible to make this one half of a BIO pair and have all the data processed by the chain under application control. main character energy tbh.
one typical use of BIO pairs is to place TLS/SSL I/O under application control. this can be used when the application wishes to use a non-standard transport for TLS/SSL or the normal socket routines are inappropriate. slay. π₯
calls to BIO_read(3) will read data from the buffer or request a retry if no data is available.
calls to BIO_write(3) will place data in the buffer or request a retry if the buffer is full.
the standard calls BIO_ctrl_pending(3) and BIO_ctrl_wpending(3) can be used to determine the amount of pending data in the read or write buffer.
BIO_reset(3) clears any data in the write buffer.
BIO_make_bio_pair()β joins two separate BIOs into a connected pair. cute. πBIO_destroy_pair()β destroys the association between two connected BIOs. freeing up any half of the pair will automatically destroy the association. (yes, the page saysBIO_destroy_pair()right there β weβre keeping it exactly as the manual says, bestie.)BIO_shutdown_wr()β used to close down a BIOb. after this call no further writes on BIObare allowed; they will return an error. reads on the other half of the pair will return any pending data or EOF when all pending data has been read.BIO_set_write_buf_size()β sets the write buffer size of BIObtosize. if the size is not initialized, a default value is used. this is currently17K, sufficient for a maximum size TLS record. when a chain containing a BIO pair is copied withBIO_dup_chain(3), the write buffer size is automatically copied from the original BIO object to the new one.BIO_get_write_buf_size()β returns the size of the write buffer.BIO_new_bio_pair()β combines the calls toBIO_new(3),BIO_make_bio_pair()andBIO_set_write_buf_size()to create a connected pair of BIOsbio1andbio2with write buffer sizeswritebuf1andwritebuf2. if either size is zero, then the default size is used.BIO_new_bio_pair()does not check whetherbio1orbio2point to some other BIO; the values are overwritten andBIO_free(3)is not called. β οΈBIO_get_write_guarantee()andBIO_ctrl_get_write_guarantee()β return the maximum length of data that can be currently written to the BIO. writes larger than this value will return a value fromBIO_write(3)less than the amount requested or if the buffer is full request a retry.BIO_ctrl_get_write_guarantee()is a function whereasBIO_get_write_guarantee()is a macro.BIO_get_read_request()andBIO_ctrl_get_read_request()β return the amount of data requested, or the buffer size if it is less, if the last read attempt at the other half of the BIO pair failed due to an empty buffer. this can be used to determine how much data should be written to the BIO so the next read will succeed: this is most useful in TLS/SSL applications where the amount of data read is usually meaningful rather than just a buffer size. after a successful read this call will return zero. it also will return zero once new data has been written satisfying the read request or part of it. note thatBIO_get_read_request()never returns an amount larger than that returned byBIO_get_write_guarantee().BIO_ctrl_reset_read_request()β can also be used to reset the value returned byBIO_get_read_request()to zero.
both halves of a BIO pair should be freed. even if one half is implicitly freed due to a BIO_free_all(3) or SSL_free(3) call, the other half still needs to be freed. donβt leave your other half hanging, bestie. π
when used in bidirectional applications (such as TLS/SSL), care should be taken to flush any data in the write buffer. this can be done by calling BIO_pending(3) on the other half of the pair and, if any data is pending, reading it and sending it to the underlying transport. this must be done before any normal processing (such as calling select(2)) due to a request and BIO_should_read(3) being true. π₯
to see why this is important, consider a case where a request is sent using BIO_write(3) and a response read with BIO_read(3), this can occur during a TLS/SSL handshake for example. BIO_write(3) will succeed and place data in the write buffer. BIO_read(3) will initially fail and BIO_should_read(3) will be true. if the application then waits for data to become available on the underlying transport before flushing the write buffer, it will never succeed because the request was never sent. deadlock vibes. π
BIO_eof(3) is true if no data is in the peer BIO and the peer BIO has been shutdown.
BIO_ctrl(3) cmd arguments correspond to macros as follows:
BIO_C_DESTROY_BIO_PAIRβBIO_destroy_bio_pair()BIO_C_GET_READ_REQUESTβBIO_get_read_request()BIO_C_GET_WRITE_BUF_SIZEβBIO_get_write_buf_size()BIO_C_GET_WRITE_GUARANTEEβBIO_get_write_guarantee()BIO_C_MAKE_BIO_PAIRβBIO_make_bio_pair()BIO_C_RESET_READ_REQUESTβBIO_ctrl_reset_read_request()BIO_C_SET_WRITE_BUF_SIZEβBIO_set_write_buf_size()BIO_C_SHUTDOWN_WRβBIO_shutdown_wr()BIO_CTRL_EOFβBIO_eof(3)BIO_CTRL_GET_CLOSEβBIO_get_close(3)BIO_CTRL_PENDINGβBIO_pending(3)BIO_CTRL_RESETβBIO_reset(3)BIO_CTRL_SET_CLOSEβBIO_set_close(3)BIO_CTRL_WPENDINGβBIO_wpending(3)
π«Ά RETURN VALUES π«Ά
BIO_new_bio_pair() returns 1 on success, with the new BIOs available in bio1 and bio2, or 0 on failure, with NULL pointers stored into the locations for bio1 and bio2. check the error stack for more information. π
when called on a BIO pair BIO object, BIO_method_type(3) returns the constant BIO_TYPE_BIO and BIO_method_name(3) returns a pointer to the static string "BIO pair".
π EXAMPLES π
the BIO pair can be used to have full control over the network access of an application. the application can call select(2) on the socket as required without having to go through the SSL interface. iconic. π
BIO *internal_bio, *network_bio;
...
BIO_new_bio_pair(&internal_bio, 0, &network_bio, 0);
SSL_set_bio(ssl, internal_bio, internal_bio);
SSL_operations(); /* e.g. SSL_read() and SSL_write() */
...
application | TLS-engine
| |
+----------> SSL_operations()
| /\ ||
| || \/
| BIO-pair (internal_bio)
| BIO-pair (network_bio)
| || /\
| \/ ||
+-----------< BIO_operations()
| |
socket |
...
SSL_free(ssl); /* implicitly frees internal_bio */
BIO_free(network_bio);
...
as the BIO pair will only buffer the data and never directly access the connection, it behaves non-blocking and will return as soon as the write buffer is full or the read buffer is drained. then the application has to flush the write buffer and/or fill the read buffer. π¦
use BIO_ctrl_pending(3) to find out whether data is buffered in the BIO and must be transferred to the network. use BIO_ctrl_get_read_request() to find out how many bytes must be written into the buffer before the SSL operations can successfully be continued.
π¦ SEE ALSO π¦
BIO_new(3), BIO_read(3), BIO_should_retry(3), ssl(3), SSL_set_bio(3)
π HISTORY π
BIO_s_bio(),BIO_make_bio_pair(),BIO_destroy_bio_pair(),BIO_set_write_buf_size(),BIO_get_write_buf_size(),BIO_new_bio_pair(),BIO_get_write_guarantee(),BIO_ctrl_get_write_guarantee(),BIO_get_read_request(), andBIO_ctrl_reset_read_request()first appeared in OpenSSL 0.9.4 and have been available since OpenBSD 2.6.BIO_ctrl_reset_read_request()first appeared in OpenSSL 0.9.5 and has been available since OpenBSD 2.7.BIO_shutdown_wr()first appeared in OpenSSL 0.9.6 and has been available since OpenBSD 2.9.
π CAVEATS π
β οΈ as the data is buffered, SSL operations may return with an ERROR_SSL_WANT_READ condition, but there is still data in the write buffer. an application must not rely on the error value of the SSL operation but must assure that the write buffer is always flushed first. otherwise a deadlock may occur as the peer might be waiting for the data before being able to continue. no cap, flush the buffer first. π
and thatβs the whole page, bestie π«Άπ β this little masterpiece comes from Debian, dated June 8, 2025, and its page name is BIO_S_BIO(3). go forth and pair those BIOs π β¨
π¦ where this page comes from
- alpine 3.24: libressl-doc 4.3.1-r0 Β· custom
- arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
- Copyright (c) 2000, 2002, 2015, 2016 The OpenSSL Project
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms