NAME
SSL_read_ex
,
SSL_read
, SSL_peek_ex
,
SSL_peek
—
read bytes from a TLS
connection
SYNOPSIS
#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) 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_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.