SSL_READ(3ossl)
π SSL_READ(3ossl) π
okay bestie, sit DOWN, because today we're reading bytes off a TLS/SSL connection and it's honestly giving main character energy. this is the OpenSSL manual page for SSL_read_ex, SSL_read, SSL_peek_ex and SSL_peek, and by the end of it you're gonna know exactly how to pull data out of an SSL object without crashing out. no cap. β¨
β¨ NAME β¨
SSL_read_ex, SSL_read, SSL_peek_ex, SSL_peek β read bytes from a TLS/SSL 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 π
so SSL_read_ex() and SSL_read() are out here trying to read num bytes from the ssl you handed them, and they drop those bytes into the buffer buf. and when SSL_read_ex() succeeds? it stashes the actual number of bytes it read into *readbytes. so cute of it. π
SSL_peek_ex() and SSL_peek() are literally the same as SSL_read_ex() and SSL_read() respectively, EXCEPT β and this is the iconic part β no bytes are actually removed from the underlying BIO during the read. so a subsequent call to SSL_read_ex() or SSL_read() will yield at least the same bytes. it's giving window shopping. you're looking, not buying. ποΈ
π NOTES π
ok so for all the paragraphs below, when we say "read function" we mean one of SSL_read_ex(), SSL_read(), SSL_peek_ex() or SSL_peek(). one of the four. the whole squad. π
if it needs to, a read function will negotiate a TLS/SSL session β like, if that hasn't already been explicitly done by SSL_connect(3) or SSL_accept(3). and if the peer requests a re-negotiation, that gets performed transparently during the read function operation, mid-call, no drama. also, the behaviour of the read functions depends on the underlying BIO, so like, know your BIO bestie.
for that transparent negotiation to succeed, the ssl must have been initialized to client or server mode. how? by calling SSL_set_connect_state(3) or SSL_set_accept_state() before the first invocation of a read function. before!! not during!! π
the read functions work based on the SSL/TLS records. the data are received in records, with a maximum record size of 16kB. and only when a record has been completely received can it be processed β that means decryption AND check of integrity. so therefore, data that was not retrieved at the last read call can still be buffered inside the SSL layer, and it will be retrieved on the next read call. if num is higher than the number of bytes buffered, then 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 one record will be returned. and because the size of an SSL/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. yes really. patience is a virtue. π§
if SSL_MODE_AUTO_RETRY has been switched off and a non-application data record has been processed, the read function can return and set the error to SSL_ERROR_WANT_READ. in this case there might still be unprocessed data available in the BIO. and if read ahead was set using SSL_CTX_set_read_ahead(3), there might also still be unprocessed data available in the SSL. this behaviour can be controlled using the SSL_CTX_set_mode(3) call. okay? okay. π
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 non-application data record has been processed and SSL_MODE_AUTO_RETRY is not set.
β οΈ note that if SSL_MODE_AUTO_RETRY is set and only non-application data is available, the call will hang. like, hang hang. be aware. π
if the underlying BIO is nonblocking, 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. and because at any time it's possible that non-application data needs to be sent, a read function can also cause write operations. wild, right? a read function doing writes. multi-talented. β¨ the calling process then must 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 nonblocking socket, nothing is to be done, but
select()can be used to check for the required condition. easy. π«Ά - 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. okay so that one's more work. π
SSL_pending(3) can be used to find out whether there are buffered bytes available for immediate retrieval. in this case the read function can be called without blocking or actually receiving new data from the underlying socket. instant gratification, we love to see it. π
when used with a QUIC SSL object, calling an I/O function such as SSL_read() allows internal network event processing to be performed. it is important that this processing is performed regularly. if an application is not using thread assisted mode, an application should ensure that an I/O function such as SSL_read() is called regularly, or alternatively ensure that SSL_handle_events() is called regularly. see openssl-quic(7) and SSL_handle_events(3) for more information. don't ghost your event processing. iykyk. β
π₯ RETURN VALUES π₯
SSL_read_ex() and SSL_peek_ex() will return 1 for success or 0 for failure.
- success means that
1or more application data bytes have been read from the SSL connection. slay. π - failure means that no bytes could be read from the SSL connection. and 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. don't guess, bestie. check. π
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/SSL connection. π<= 0β the read operation was not successful, because either the connection was closed, an error occurred or action must be taken by the calling process. callSSL_get_error(3)with the return valueretto find out the reason.- old documentation indicated a difference between
0and-1, and that-1was retryable. you should instead callSSL_get_error()to find out if it's retryable. that's the glow-up. periodt. π
- old documentation indicated a difference between
π SEE ALSO π
SSL_get_error(3), SSL_write_ex(3), SSL_CTX_set_mode(3), SSL_CTX_new(3), SSL_connect(3), SSL_accept(3) SSL_set_connect_state(3), SSL_pending(3), SSL_shutdown(3), SSL_set_shutdown(3), ssl(7), bio(7)
π¦ HISTORY π¦
the SSL_read_ex() and SSL_peek_ex() functions were added in OpenSSL 1.1.1. babies, but iconic babies. πΈ
π COPYRIGHT π
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>.
and that's the whole page, bestie!! π
this was OpenSSL version 3.5.9, dated 2026-09-29, page SSL_READ(3ossl). go forth and read those bytes responsibly. mwah. ππβ¨
π¦ where this page comes from
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms