SSL_READ(3ossl)
π SSL_READ(3ossl) β read bytes from a TLS/SSL connection π
okay so bestie, this is from the 3ossl manual section (OpenSSL, obvi) and it's literally all about pulling bytes out of a TLS/SSL connection. it's giving "the plumbing that makes your secure chat work" energy. let's get into it π«Ά
β¨ 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);
four functions, all your besties, all doing the read thing π
πΈ DESCRIPTION πΈ
So SSL_read_ex() and SSL_read() try to read num bytes from the ssl you handed them into the buffer buf. cute. And on success, SSL_read_ex() is the overachiever that also stores the number of bytes actually read into *readbytes β¨
SSL_peek_ex() and SSL_peek() are literally identical to SSL_read_ex() and SSL_read() respectively β EXCEPT, and this is the main character detail, no bytes are actually removed from the underlying BIO during the read. so if you peek, then call SSL_read_ex() or SSL_read() after, you're guaranteed to get at least the same bytes. it's like reading a text over someone's shoulder without them knowing π no cap.
π¦ NOTES π¦
okay in the paragraphs below, when they say a "read function" they mean one of SSL_read_ex(), SSL_read(), SSL_peek_ex() or SSL_peek(). all four. the whole squad.
If necessary, a read function will negotiate a TLS/SSL session, if not already explicitly performed by SSL_connect(3) or SSL_accept(3). and if the peer requests a re-negotiation, it gets performed transparently during the read function operation. so it's just doing it behind the scenes, no drama. but heads up β the behaviour of the read functions depends on the underlying BIO. always.
For the transparent negotiation to succeed, the ssl must have been initialized to client or server mode. that happens by calling SSL_set_connect_state(3) or SSL_set_accept_state() before the first invocation of a read function. don't skip this step, bestie, it will not work π
The read functions work based on the SSL/TLS records. the data are received in records (with a maximum record size of 16kB). Only when a record has been completely received can it be processed (decryption and check of integrity). so like, it's chunked. Therefore, data that was not retrieved at the last read call can still be buffered inside the SSL layer and 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 here's the fun part: as 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. so yeah, multiple packets, one record, that's the vibe. π
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. 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. iykyk π
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. π hang! like not moving! be careful bestie.
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 since at any time it's possible that non-application data needs to be sent, a read function can also cause write operations. plot twist, a read that writes π
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. 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 the read function can be called without blocking or actually receiving new data from the underlying socket. obsessed, so efficient. β¨
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, fr fr π§
π RETURN VALUES π
SSL_read_ex() and SSL_peek_ex() will return 1 for success or 0 for failure. Success means that 1 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. always ask SSL_get_error(3), they know things π
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. slay π<= 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 between0and-1, and that-1was retryable. You should instead callSSL_get_error()to find out if it's retryable. that's the old lore, don't trust it, trustSSL_get_error(). periodt. π
π 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)
the whole friend group, go read them too π
π HISTORY π
The SSL_read_ex() and SSL_peek_ex() functions were added in OpenSSL 1.1.1. so they're not ancient, they're just iconic. β¨
π 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.0, dated 2026-09-25, and it's filed under SSL_READ(3ossl) β go read some bytes, you got this ππΈπ¦
π¦ where this page comes from
- opensuse-leap 16.0: openssl-3-doc 3.5.0-160000.11.1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms