SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
BIO buffers and custom transports

A BIO is an input/output object that lets the TLS adapter use memory buffers or your application's transport callbacks. Start with a memory BIO to learn the ownership and retry rules without opening a socket. For a connected POSIX or Winsock socket, use the socket transport guide.

The memory BIO example demonstrates a complete write/read cycle and cleanup. Select the bio component; it needs no native SharkSSL implementation.

Supported interfaces

Area Interfaces
Objects BIO_new, BIO_up_ref, BIO_free
I/O BIO_read, BIO_write, BIO_read_ex, BIO_write_ex, BIO_puts, BIO_ctrl, BIO_ctrl_pending, BIO_ctrl_wpending
Custom methods BIO_get_new_index, BIO_meth_new/free, setters for read, write, read_ex, write_ex, puts, ctrl, create, destroy
Accounting BIO_number_read, BIO_number_written return cumulative 64-bit byte counts
Method identification BIO_method_name, BIO_method_type
Method state BIO_set/get_data, BIO_set/get_init, BIO_set/get_shutdown
Retry state Flag setters/test/clear and read/write retry macros
Memory BIO_s_mem, BIO_new_mem_buf, default reset, EOF, flush, pending, close policy, get_mem_data, get_mem_ptr, nonpositive empty-read policy
Buffer ownership BUF_MEM_new, BUF_MEM_free
Application buffers in <openssl/crypto.h> OPENSSL_malloc, OPENSSL_zalloc, OPENSSL_realloc, OPENSSL_memdup, OPENSSL_clear_free, OPENSSL_free
Dispatcher extension SharkSslOssl_BIO_new_sodisp, SharkSslOssl_BIO_sodisp_result

Application buffers

Select the BIO component for the application buffer calls. Sizes are in bytes and have type size_t. The allocation functions return an owned void*, or NULL for an empty request or allocation failure. OPENSSL_zalloc initializes all bytes to zero. OPENSSL_memdup copies binary data and accepts sizes below INT_MAX. A failed nonzero resize preserves the original allocation. Resizing to zero releases it. Use OPENSSL_clear_free(ptr, length) to erase the first length bytes before release. Supply a length within the allocation. Both free functions accept NULL and use the configured allocator.

Ownership and custom methods

Methods must outlive every BIO created with them. Configure a method before creating its BIOs. A method without a create callback starts initialized. When a method supplies a create callback, the callback can call BIO_set_init(bio, 1) as soon as the BIO is ready for I/O. Each BIO's operations and mutable state require caller serialization. Reference-count updates and custom type allocation use atomic operations. Other BIO operations require caller serialization. The adapter uses the C runtime allocator by default. SHARKSSL_OSSL_PORT_HEADER can supply SO_MALLOC, SO_REALLOC, SO_FREE, and SO_ATOMIC_ADD together with their declarations. The private SharkSslOsslPort.h shares these hooks across BIO, TLS and EVP; custom allocation requires all three allocator hooks. Embedded integration must supply working atomic operations; unsupported compilers fail compilation instead of selecting a non-atomic mode.

BIO_read and BIO_write return the number of bytes transferred on success. After a nonpositive result, check BIO_should_retry and the retry direction. An empty writable memory BIO returns -1 with read retry by default. BIO_set_mem_eof_return accepts a negative int to select the retry result or zero to report EOF. An exhausted read-only memory BIO reports EOF by default.

Memory BIOs

Memory BIOs retain at most INT_MAX bytes in this adapter. Partial reads advance through a writable buffer. BIO_get_mem_data borrows its unread bytes. Reacquire this pointer after another BIO operation. A write can reuse space at the front when the tail is too short. A failed growth preserves unread data. Read-only BIOs borrow the original input, which must remain unchanged and alive. With BIO_CLOSE, owned memory is cleared and freed. With BIO_NOCLOSE, obtain the BUF_MEM using BIO_get_mem_ptr and release it after the BIO is freed. For a writable BIO, the returned BUF_MEM holds the unread bytes at the allocation base. For a read-only borrowed buffer, set its data field to NULL before calling BUF_MEM_free; the original bytes remain owned by the application.

An integer method callback receives at most INT_MAX bytes per call and returns a byte count, zero or a negative result. The last setter for each direction selects the active callback. A zero-byte BIO_write_ex on an existing BIO reports success with a count of zero. A method without a control callback returns -2 from BIO_ctrl. The memory BIO returns zero for unknown control commands.

BIO chains, file BIOs, secure-heap memory, datagram BIOs, nonclearing memory reset and buffer adoption are outside the subset. Socket BIO constructors require the optional hosted socket option; see socket ownership.

Dispatcher attachment

Create the connection and select nonblocking mode before attachment. Hold its dispatcher mutex for construction and I/O. The adapter installs no dispatcher callback and leaves readiness scheduling to the application.

BIO_NOCLOSE borrows the socket. BIO_CLOSE closes and unregisters it on final BIO release, acquiring its dispatcher mutex when necessary. Neither mode frees the caller's SoDispCon storage. Keep the connection and dispatcher alive until the last BIO reference is released; do not move or rebind the connection. Failed construction never takes ownership.

Successful attachment selects SoDispCon_setStreamIo(connection, TRUE) so an epoll callback can drain queued input and handle EOF before the application closes the connection. This policy persists after BIO_NOCLOSE release. Before reusing that connection with the dispatcher's legacy I/O, disable its events and restore SoDispCon_setStreamIo(connection, FALSE) while holding the dispatcher mutex. Fresh connection objects retain the legacy default. Other current ports need no extra dispatcher state.

Would-block and interruption set the appropriate read/write retry flags. EOF is latched on the read side while writes remain possible. Terminal failures are retained and cannot become EOF on later calls. The extension result getter returns the last native transport result and error; it is not an OpenSSL error queue. TCP EOF remains distinct from a TLS close_notify alert.

The dispatcher adapter supports Windows, Linux/POSIX and ThreadX/NetX Duo stream hooks. NetX requires BA_HAVE_SOCKET_WRITE_EVENTS=1 and its existing native notification options. Its blocking-only configuration leaves stream I/O unsupported. POSIX requires MSG_NOSIGNAL; Linux supports select, poll and epoll dispatchers. For a worked client, see ThreadX-NetX/README.md under the release's compat/openssl/examples directory.