---
name: "libressl-SSL_write"
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_write"
---

# libressl-SSL\_write(3)

write bytes to a TLS connection

## NAME

**SSL\_write\_ex**, **SSL\_write** – write bytes to a TLS connection

## SYNOPSIS

SSL Library (libssl, -lssl) libcrypto

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

*int*\

**SSL\_write\_ex**(*SSL \*ssl*, *const void \*buf*, *size\_t num*, *size\_t \*written*);

*int*\

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

## DESCRIPTION

**SSL\_write\_ex**() and **SSL\_write**() write *num* bytes from the buffer *buf* into the specified *ssl* connection. On success **SSL\_write\_ex**() stores the number of bytes written in \**written*.

In the following, **SSL\_write\_ex**() and **SSL\_write**() are called “write functions”.

If necessary, a write function negotiates 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 write function operation. The behaviour of the write 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 write function.

If the underlying *BIO* is *blocking*, the write function will only return once the write operation has been finished or an error occurred, except when a renegotiation takes place, in which case a SSL\_ERROR\_WANT\_READ may occur. This behaviour 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*, the write 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 write function will yield SSL\_ERROR\_WANT\_READ or SSL\_ERROR\_WANT\_WRITE. As at any time a re-negotiation is possible, a call to a write function can also cause read operations. The calling process then must repeat the call after taking appropriate action to satisfy the needs of the write 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.

The write functions will only return with success when the complete contents of *buf* of length *num* have been written. This default behaviour can be changed with the SSL\_MODE\_ENABLE\_PARTIAL\_WRITE option of SSL\_CTX\_set\_mode(3). When this flag is set, the write functions will also return with success when a partial write has been successfully completed. In this 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 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.

When calling **SSL\_write**() with *num*=0 bytes to be sent, the behaviour is undefined. **SSL\_write\_ex**() can be called with *num*=0, but will not send application data to the peer.

## RETURN VALUES

**SSL\_write\_ex**() returns 1 for success or 0 for failure. Success means that all requested application data bytes have been written to the TLS connection or, if SSL\_MODE\_ENABLE\_PARTIAL\_WRITE is in use, at least one application data byte has been written to the TLS connection. Failure means that 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 TLS 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**(), the following return values can occur:

- \>0

  The write operation was successful. The return value is the number of bytes actually written to the TLS connection.

- 0

  The write operation was not successful. Probably the underlying connection was closed. 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 write 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\_read(3), SSL\_set\_connect\_state(3)

## HISTORY

**SSL\_write**() appeared in SSLeay 0.4 or earlier and has been available since OpenBSD 2.4.

**SSL\_write\_ex**() first appeared in OpenSSL 1.1.1 and has been available since OpenBSD 7.1.
