SSL_WRITE(3SSL)

πŸ’– NAME πŸ’–

okay so bestie, say hi to the main characters: SSL_write_ex2, SSL_write_ex, SSL_write, SSL_sendfile, and the lil flag SSL_WRITE_FLAG_CONCLUDE πŸ‘‘ β€” they all exist for one iconic job: writing bytes to a TLS/SSL connection. That's the whole vibe. We're in the OpenSSL manual pages, section 3SSL, and it's giving "let me send your data, respectfully." ✨


πŸŽ€ SYNOPSIS πŸŽ€

#include <openssl/ssl.h>

#define SSL_WRITE_FLAG_CONCLUDE

ossl_ssize_t SSL_sendfile(SSL *s, int fd, off_t offset, size_t size, int flags);
int SSL_write_ex2(SSL *s, const void *buf, size_t num,
                  uint64_t flags,
                  size_t *written);
int SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *written);
int SSL_write(SSL *ssl, const void *buf, int num);

🌸 DESCRIPTION 🌸

So SSL_write_ex() and SSL_write() take num bytes out of the buffer buf and write them into the SSL connection you picked, ssl. And when SSL_write_ex() slays, it also drops the number of bytes it actually wrote into *written. 🫢

Then there's SSL_write_ex2(), which is basically SSL_write_ex()'s cooler sibling β€” it does the same thing but also accepts optional flags that change up its behaviour. And no cap: calling SSL_write_ex2() with a flags argument of 0 is exactly the same as calling SSL_write_ex(). Literally identical energy. πŸ’…

SSL_sendfile() writes size bytes starting at offset offset in the file descriptor fd over to the SSL connection s. The whole point of this one is efficient zero-copy semantics, which is lowkey gorgeous. But heads up β€” SSL_sendfile() is only available when Kernel TLS is enabled, and you can check that by calling BIO_get_ktls_send(). It's hanging out here so users can keep the same interface they're used to. The meaning of flags depends on your platform. Currently, under Linux, it's just ignored. πŸ™ˆ

The flags argument to SSL_write_ex2() accepts zero or more of the flags below. Note that which flags are actually supported depends on the kind of SSL object and the underlying protocol being used β€” so it's a vibe check every time. πŸ”

✨ SSL_WRITE_FLAG_CONCLUDE ✨

This flag is only supported on QUIC stream SSL objects (or QUIC connection SSL objects with a default stream attached). So if you're not in QUIC-land, it's not for you, bestie, sorry. 😭

If this flag is set, and the call to SSL_write_ex2() succeeds, and all of the data passed to the call is written β€” meaning "*written == num" β€” then the relevant QUIC stream's send part is concluded automatically, as though SSL_stream_conclude(3) was called. That causes transmission of a FIN for the stream. 🌈

Now, using this flag is semantically equivalent to calling SSL_stream_conclude(3) after a successful call to this function. But using the flag is more efficient than making those two API calls separately, because it lets the written stream data and the FIN flag (the one saying "end of stream, bye") get scheduled as part of the same QUIC STREAM frame and QUIC packet. Two birds, one packet, we love efficiency. πŸ’€βœ¨

Important: setting this flag does not cause a stream's send part to be concluded if not all of the data passed to the call was consumed. So partial vibes = no FIN.

Also, a call to SSL_write_ex2() fails if a flag is passed which is not supported or understood by the given SSL object. So an application should figure out whether a flag is supported before attempting to use it β€” for example, for SSL_WRITE_FLAG_CONCLUDE, checking that a QUIC stream SSL object is actually being used. Don't just yolo a flag in there. 🚫

πŸ“ NOTES πŸ“

In the paragraphs below, a "write function" means one of either SSL_write_ex() or SSL_write(). Those two, specifically. Keep up. πŸ“Œ

If necessary, a write function will negotiate a TLS/SSL session for you, if SSL_connect(3) or SSL_accept(3) hasn't already done it explicitly. And if the peer requests a re-negotiation, it gets performed transparently during the write function operation. The behaviour of the write functions depends on the underlying BIO. 🧚

For that 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 call to a write function. ⭐

If the underlying BIO is blocking, the write functions will only return once the write operation has finished or an error occurred.

If the underlying BIO is nonblocking, the write functions will also return when the underlying BIO couldn't satisfy the needs of the function to continue the operation. In that case, calling SSL_get_error(3) with the return value of the write function will give you SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE. And here's the wild part: since a re-negotiation is possible at any time, a call to a write function can also cause read operations! 😭 The calling process then has to repeat the call after taking appropriate action to satisfy the needs of the write function. What that action is depends on the underlying BIO:

  • Nonblocking socket? Nothing to do, but select() can be used to check for the required condition.
  • Buffering BIO, like a BIO pair? Data must be written into or retrieved out of the BIO before you can continue.

The write functions only return with success when the complete contents of buf of length num has been written. That default behaviour can be changed with the SSL_MODE_ENABLE_PARTIAL_WRITE option of SSL_CTX_set_mode(3). When that flag is set, the write functions will also return with success when a partial write has been successfully completed β€” in that case the write function operation is considered completed. The bytes are sent, and a new write call with a new buffer (with the already sent bytes removed) must be started. A partial write is performed with the size of a message block, which is 16kB. πŸ“

When used with a QUIC SSL object, calling an I/O function such as SSL_write() allows internal network event processing to be performed. And it's important that this processing happens regularly. If an application isn't using thread assisted mode, it should make sure an I/O function such as SSL_write() is called regularly, or alternatively make sure SSL_handle_events() is called regularly. See openssl-quic(7) and SSL_handle_events(3) for more info. πŸ¦‹


🚨 WARNINGS 🚨

When a write function call has to be repeated because SSL_get_error(3) returned SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE, it must be repeated with the same arguments. The data that was passed might have been partially processed. When SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER was set using SSL_CTX_set_mode(3), the pointer can be different β€” but the data and length should still be the same. πŸ‘€

You should not call SSL_write() with num=0, it will return an error. SSL_write_ex() can be called with num=0, but it will not send application data to the peer. Not a single byte. Zero. Nothing. πŸ’€

🫢 RETURN VALUES 🫢

SSL_write_ex() and SSL_write_ex2() return 1 for success or 0 for failure.

  • Success means all requested application data bytes have been written to the SSL connection or β€” if SSL_MODE_ENABLE_PARTIAL_WRITE is in use β€” at least 1 application data byte has been written to the SSL connection.
  • Failure means not all the requested bytes have been written yet (if SSL_MODE_ENABLE_PARTIAL_WRITE is not in use) or no bytes could be written to the SSL connection (if SSL_MODE_ENABLE_PARTIAL_WRITE is in use).

Failures can be retryable (e.g. the network write buffer has temporarily filled up) 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_write(), these are the return values that can occur:

  • > 0 β€” The write operation was successful, and the return value is the number of bytes actually written to the TLS/SSL connection. πŸŽ‰
  • <= 0 β€” The write operation was not successful, because either the connection was closed, an error occurred, or action must be taken by the calling process. Call SSL_get_error() with the return value ret to find out the reason.
Old documentation indicated a difference between 0 and -1, and that -1 was retryable. You should instead call SSL_get_error() to find out if it's retryable. That old lore is over, bestie. πŸ“œπŸš«

For SSL_sendfile(), these are the return values that can occur:

  • >= 0 β€” The write operation was successful, and the return value is the number of bytes of the file written to the TLS/SSL connection. The return value can be less than size for a partial write.
  • < 0 β€” The write operation was not successful, because either the connection was closed, an error occurred, or action must be taken by the calling process. Call SSL_get_error() with the return value to find out the reason.

πŸ‘€ SEE ALSO πŸ‘€

Go read those too, they're part of the lore. πŸ“š


πŸ•°οΈ HISTORY πŸ•°οΈ

  • SSL_write_ex() was added in OpenSSL 1.1.1.
  • SSL_sendfile() was added in OpenSSL 3.0.
  • SSL_write_ex2() was added in OpenSSL 3.3.

The glow-up is real and it's documented. ✨


Copyright 2000-2024 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>.


aaaand that's the whole page, bestie πŸŽ€πŸ’– this was SSL_WRITE(3SSL) from OpenSSL 4.0.3, dated 2026-09-29. go forth and write bytes, but like, correctly. mwah πŸ’…πŸ¦‹

πŸ“¦ where this page comes from