BIO_READ(3ossl)

πŸ“¦ version 3.5.0, in opensuse-leap 16.0

πŸ’… BIO_READ(3ossl) β€” the OpenSSL girlies' guide to BIO I/O ✨

okay so bestie, this is a page from the OpenSSL manual, section 3ossl β€” it's the whole BIO_read/BIO_write family and honestly? it's giving "the plumbing of everything you've ever loved." let's get into it 🫢


🌸 NAME 🌸

BIO_read_ex, BIO_write_ex, BIO_read, BIO_write, BIO_gets, BIO_get_line, BIO_puts β€” literally the BIO I/O functions, 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 each of these is a different little guy with a different job, let me introduce you πŸ’…

BIO_read_ex() tries to read dlen bytes from BIO b and drops the data into data. If any bytes got read successfully, the number of bytes actually read goes into *readbytes. tracking, slay ✨

BIO_write_ex() tries to write dlen bytes from data to BIO b. If it works, the number of bytes written lands in *written β€” unless, and this is the fun part, written is NULL. then it just doesn't πŸ€·β€β™€οΈ

BIO_read() attempts to read len bytes from BIO b and puts the data in buf. (yes the parameter in the synopsis is called dlen and the description calls it len, we don't judge, we just note it πŸ’€)

BIO_gets() does the BIOs "gets" operation and places the data in buf. usually this is gonna try to read one line of data from the BIO, max length size-1. BUT β€” and this is the tea β€” there are exceptions. for example, BIO_gets() on a digest BIO will calculate and return the digest, and other BIOs might not support BIO_gets() at all, no cap. the returned string is always NUL-terminated and the '\n' is preserved if it's present in the input data. on binary input there may be NUL characters hangin' out inside the string; in that case the return value (if nonnegative) might give you an incorrect length 😭 lowkey a trap.

BIO_get_line() tries to read from BIO b a line of data up to the next '\n', or until the maximum length size-1 is reached, and places the data in buf. the returned string is always NUL-terminated and the '\n' is preserved if it's present in the input data. on binary input there may be NUL characters inside the string; in that case the return value (if nonnegative) gives the actual length read β€” accurate this time, we love consistency πŸ’–. but for implementing this, unfortunately the data needs to be read byte-by-byte. yeah. byte. by. byte. πŸ™ˆ

BIO_write() tries to write len bytes from buf to BIO b.

BIO_puts() tries to write a NUL-terminated string buf to BIO b. short king behavior.


πŸŽ€ RETURN VALUES πŸŽ€

BIO_read_ex() returns 1 if data was successfully read, and 0 otherwise. binary. iconic. πŸ‘‘

BIO_write_ex() returns 1 if no error was encountered writing data, 0 otherwise. and listen β€” requesting to write 0 bytes is not considered an error. zero is valid, zero is fine, zero is beautiful πŸ’…

BIO_write() returns -2 if the "write" operation is not implemented by the BIO, or -1 on other errors. otherwise it returns the number of bytes written. this may be 0 if the BIO b is NULL or dlen <= 0.

BIO_gets() returns -2 if the "gets" operation is not implemented by the BIO, or -1 on 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. don't count the NUL, bestie.

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 0 or -1. if the return value is -2 then the operation is simply not implemented in that specific BIO type. it's not you, it's the BIO.


🌈 NOTES 🌈

a 0 or -1 return is not necessarily an indication of an error. huge. in particular, when the source/sink is nonblocking or of a certain type, it may merely be an indication that no data is currently available and that the application should retry the operation later. it's giving "not right now, ask again later" πŸ¦‹

one technique sometimes used with blocking sockets is to use a system call (such as select(), poll() or equivalent) to determine when data is available and then call read() to read the data. the equivalent with BIOs β€” that is, call select() on the underlying I/O structure and then call 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 will request a retry instead of blocking. be smart about it, periodt.

see BIO_should_retry(3) for the deets on how to determine the cause of a retry and other I/O issues.

if the "gets" method is not 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 is not supported by adding a buffering BIO BIO_f_buffer(3) to the chain. glow-up via buffering ✨


πŸ‘€ SEE ALSO πŸ‘€

BIO_should_retry(3)


πŸ’€ HISTORY πŸ’€

  • BIO_gets() on 1.1.0 and older, when called on BIO_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. new girl on the block.
  • BIO_write_ex() returns 1 if the size of the data to write is 0 and the written parameter of the function can be NULL since OpenSSL 3.0. glow-up confirmed.

Copyright 2000-2023 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>.


aaand that's the page bestie 🫢 this version is from OpenSSL 3.5.0, dated 2026-09-25, and it's the BIO_READ(3ossl) manual page β€” go read some bytes, you got this πŸ’œπŸ“

πŸ“¦ where this page comes from