BIO_READ(3SSL)
π BIO_READ β the OpenSSL I/O page, bestie edition π
okay so this is BIO_read(3SSL) from the OpenSSL manual, and it's literally the page about talking to BIOs β reading from them, writing to them, getting lines out of them. It's giving "the group chat of every I/O operation you'll ever do". Sit down, hydrate, and let's get into it π«Ά
β¨ NAME β¨
BIO_read_ex, BIO_write_ex, BIO_read, BIO_write, BIO_gets, BIO_get_line, BIO_puts β the BIO I/O functions. That's the whole squad. π
πΈ SYNOPSIS πΈ
#include <openssl/bio.h>
int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes);
int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written);
int BIO_read(BIO *b, void *data, int dlen);
int BIO_gets(BIO *b, char *buf, int size);
int BIO_get_line(BIO *b, char *buf, int size);
int BIO_write(BIO *b, const void *data, int dlen);
int BIO_puts(BIO *b, const char *buf);
π DESCRIPTION π
Okay so, function by function, no skipping, we do not skip here:
BIO_read_ex()β tries to readdlenbytes out of BIOband drops the data intodata. If any bytes were successfully read, the number of bytes read gets stored in*readbytes. Iconic. β¨BIO_write_ex()β tries to writedlenbytes coming fromdatainto BIOb. If it succeeds, the number of bytes written gets stored in*writtenβ¦ unlesswrittenisNULL, in which case, no notes, it just doesn't. πBIO_read()β tries to readlenbytes from BIOband places the data inbuf. (Yes, the synopsis saysdlenand this description sayslenβ I don't make the rules, I just report them faithfully. π)BIO_gets()β does the BIO's "gets" operation and puts the data inbuf. Usually it'll try to read one line of data from the BIO with a maximum length ofsize-1. BUT β plot twist β there are exceptions. For example,BIO_gets()on a digest BIO will calculate and return the digest, and other BIOs may not supportBIO_gets()at all, periodt. The returned string is always NUL-terminated, and the'\n'is preserved if it was present in the input data. On binary input there may be NUL characters inside the string, and in that case the return value (if it's nonnegative) might give you an incorrect length. yes really. πBIO_get_line()β tries to read from BIOba line of data up to the next'\n', or until the maximum lengthsize-1is reached, then places the data inbuf. The returned string is always NUL-terminated, and the'\n'is preserved if it was present in the input data. On binary input there may be NUL characters inside the string β and in this case the return value (if nonnegative) gives the actual length read. To implement this, unfortunately the data needs to be read byte-by-byte. sobbing, but it's fine, it's fine. π§BIO_write()β tries to writelenbytes frombufto BIOb.BIO_puts()β tries to write a NUL-terminated stringbufto BIOb, without the terminating NUL byte and without appending'\n'. So it's similar tofputs(3)and notputs(3). remember that, you'll thank yourself later π
π₯ RETURN VALUES π₯
BIO_read_ex()β returns1if data was successfully read, and0otherwise.BIO_write_ex()β returns1if no error was encountered writing data,0otherwise. Requesting to write0bytes is not considered an error. zero is valid bestie!! πBIO_read()β returns the number of bytes read on success. A return value of0means end-of-file was reached, or that a read of zero bytes was requested. A negative return value means an error condition. πBIO_write()β returns-2if the "write" operation isn't implemented by the BIO, or-1on other errors. Otherwise it returns the number of bytes written. That may be0if the BIObisNULLordlen <= 0.BIO_gets()β returns-2if the "gets" operation isn't implemented by the BIO, or-1on other errors. Otherwise it typically returns the amount of data read, but depending on the implementation it may return only the length up to the first NUL character contained in the data read. In any case, the trailing NUL that gets added after the data read is not included in the length returned. sneaky little NUL π- All the other functions β return either the amount of data successfully read or written (if the return value is positive), or that no data was successfully read or written if the result is
0or-1. And if the return value is-2, the operation is not implemented in that specific BIO type.
π NOTES π
A 0 or -1 return is not necessarily an indication of an error. Especially when the source/sink is nonblocking or of a certain type, it may just mean "no data is currently available, try again later." vibe check, not a break-up text π
One technique sometimes used with blocking sockets is to use a system call (like select(), poll() or an equivalent) to figure out when data is available, and then call read() to read it. The equivalent with BIOs β i.e. calling select() on the underlying I/O structure and then calling BIO_read() to read the data β should not be used, because a single call to BIO_read() can cause several reads (and writes, in the case of SSL BIOs) on the underlying I/O structure and may block as a result. Instead, select() (or equivalent) should be combined with non-blocking I/O so successive reads request a retry instead of blocking. not me telling you not to do the thing you were about to do π
Go read BIO_should_retry(3) for the details on how to determine the cause of a retry and other I/O issues. genuine required reading, iykyk π
If the "gets" method isn't supported by a BIO, then BIO_get_line() can be used. It's also possible to make BIO_gets() usable even if the "gets" method isn't supported, by adding a buffering BIO BIO_f_buffer(3) to the chain. workaround slay β¨
π¦ SEE ALSO π¦
β HISTORY β
BIO_gets()on 1.1.0 and older, when called on aBIO_fd()based BIO, did not keep the'\n'at the end of the line in the buffer. ancient lore πΏBIO_get_line()was added in OpenSSL 3.0.BIO_write_ex()returns1if the size of the data to write is0, and thewrittenparameter of the function can beNULL, since OpenSSL 3.0.
π COPYRIGHT π
Copyright 2000-2026 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>.
okay that's the whole page, you made it, I'm so proud π₯Ή this was 4.0.3, dated 2026-09-29, from the OpenSSL manual β and it's called BIO_READ(3SSL). go forth and BIO responsibly ππΈπ
π¦ where this page comes from
- ubuntu stonking: libssl-doc 4.0.3-1ubuntu1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms