NAME
SSL_write_ex
,
SSL_write
—
write bytes to a TLS
connection
SYNOPSIS
#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) 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_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.