---
name: "libressl-SSL_read"
section: "3"
platform: "linux"
package: "libressl"
version: "4.4.0-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/libressl-SSL_read"
---

# libressl-SSL\_read(3)

read bytes from a TLS connection

## NAME

**SSL\_read\_ex**, **SSL\_read**, **SSL\_peek\_ex**, **SSL\_peek** – read bytes from a TLS connection

## SYNOPSIS

SSL Library (libssl, -lssl) libcrypto

**#include \<openssl/ssl.h\>**

*int*\

**SSL\_read\_ex**(*SSL \*ssl*, *void \*buf*, *size\_t num*, *size\_t \*readbytes*);

*int*\

**SSL\_read**(*SSL \*ssl*, *void \*buf*, *int num*);

*int*\

**SSL\_peek\_ex**(*SSL \*ssl*, *void \*buf*, *size\_t num*, *size\_t \*readbytes*);

*int*\

**SSL\_peek**(*SSL \*ssl*, *void \*buf*, *int num*);

## DESCRIPTION

**SSL\_read\_ex**() and **SSL\_read**() try to read *num* bytes from the specified *ssl* into the buffer *buf*. On success **SSL\_read\_ex**() stores the number of bytes actually read in \**readbytes*.

**SSL\_peek\_ex**() and **SSL\_peek**() are identical to **SSL\_read\_ex**() and **SSL\_read**(), respectively, except that no bytes are removed from the underlying BIO during the read, such that a subsequent call to **SSL\_read\_ex**() or **SSL\_read**() will yield at least the same bytes once again.

In the following, **SSL\_read\_ex**(), **SSL\_read**(), **SSL\_peek\_ex**(), and **SSL\_peek**() are called “read functions”.

If necessary, a read function will negotiate a TLS session, if not already explicitly performed by SSL\_connect(3) or SSL\_accept(3). If the peer requests a re-negotiation, it will be performed transparently during the read function operation. The behaviour of the read functions depends on the underlying *BIO*.

For the transparent negotiation to succeed, the *ssl* must have been initialized to client or server mode. This is done by calling SSL\_set\_connect\_state(3) or SSL\_set\_accept\_state(3) before the first call to a read function.

The read functions work based on the TLS records. The data are received in records (with a maximum record size of 16kB). Only when a record has been completely received, it can be processed (decrypted and checked for integrity). Therefore, data that was not retrieved at the last read call can still be buffered inside the TLS layer and will be retrieved on the next read call. If *num* is higher than the number of bytes buffered, the read functions will return with the bytes buffered. If no more bytes are in the buffer, the read functions will trigger the processing of the next record. Only when the record has been received and processed completely will the read functions return reporting success. At most the contents of the record will be returned. As the size of a TLS record may exceed the maximum packet size of the underlying transport (e.g., TCP), it may be necessary to read several packets from the transport layer before the record is complete and the read call can succeed.

If the underlying *BIO* is blocking, a read function will only return once the read operation has been finished or an error occurred, except when a renegotiation takes place, in which case an SSL\_ERROR\_WANT\_READ may occur. This behavior can be controlled with the SSL\_MODE\_AUTO\_RETRY flag of the SSL\_CTX\_set\_mode(3) call.

If the underlying *BIO* is non-blocking, a read function will also return when the underlying *BIO* could not satisfy the needs of the function to continue the operation. In this case a call to SSL\_get\_error(3) with the return value of the read function will yield SSL\_ERROR\_WANT\_READ or SSL\_ERROR\_WANT\_WRITE. As at any time a re-negotiation is possible, a read function may also cause write operations. The calling process must then repeat the call after taking appropriate action to satisfy the needs of the read function. The action depends on the underlying *BIO*. When using a non-blocking socket, nothing is to be done, but [select(2)](/select/2.md) can be used to check for the required condition. When using a buffering *BIO*, like a *BIO* pair, data must be written into or retrieved out of the *BIO* before being able to continue.

SSL\_pending(3) can be used to find out whether there are buffered bytes available for immediate retrieval. In this case a read function can be called without blocking or actually receiving new data from the underlying socket.

When a read function operation has to be repeated because of SSL\_ERROR\_WANT\_READ or SSL\_ERROR\_WANT\_WRITE, it must be repeated with the same arguments.

## RETURN VALUES

**SSL\_read\_ex**() and **SSL\_peek\_ex**() return 1 for success or 0 for failure. Success means that one or more application data bytes have been read from the SSL connection. Failure means that no bytes could be read from the SSL connection. Failures can be retryable (e.g. we are waiting for more bytes to be delivered by the network) or non-retryable (e.g. a fatal network error). In the event of a failure, call SSL\_get\_error(3) to find out the reason which indicates whether the call is retryable or not.

For **SSL\_read**() and **SSL\_peek**(), the following return values can occur:

- \>0

  The read operation was successful. The return value is the number of bytes actually read from the TLS connection.

- 0

  The read operation was not successful. The reason may either be a clean shutdown due to a “close notify” alert sent by the peer (in which case the SSL\_RECEIVED\_SHUTDOWN flag in the ssl shutdown state is set (see SSL\_shutdown(3) and SSL\_set\_shutdown(3)). It is also possible that the peer simply shut down the underlying transport and the shutdown is incomplete. Call SSL\_get\_error(3) with the return value to find out whether an error occurred or the connection was shut down cleanly (SSL\_ERROR\_ZERO\_RETURN).

- \<0

  The read operation was not successful, because either an error occurred or action must be taken by the calling process. Call SSL\_get\_error(3) with the return value to find out the reason.

## SEE ALSO

BIO\_new(3), [ssl(3)](/ssl/3.md), SSL\_accept(3), SSL\_connect(3), SSL\_CTX\_new(3), SSL\_CTX\_set\_mode(3), SSL\_get\_error(3), SSL\_pending(3), SSL\_set\_connect\_state(3), SSL\_set\_shutdown(3), SSL\_shutdown(3), SSL\_write(3)

## HISTORY

**SSL\_read**() appeared in SSLeay 0.4 or earlier. **SSL\_peek**() first appeared in SSLeay 0.6.6. Both functions have been available since OpenBSD 2.4.

**SSL\_read\_ex**() and **SSL\_peek\_ex**() first appeared in OpenSSL 1.1.1 and have been available since OpenBSD 7.1.
