xref: /netbsd-src/crypto/external/bsd/openssl/dist/doc/man3/SSL_write.pod (revision b0d1725196a7921d003d2c66a14f186abda4176b)
113d40330Schristos=pod
213d40330Schristos
313d40330Schristos=head1 NAME
413d40330Schristos
5*b0d17251SchristosSSL_write_ex, SSL_write, SSL_sendfile - write bytes to a TLS/SSL connection
613d40330Schristos
713d40330Schristos=head1 SYNOPSIS
813d40330Schristos
913d40330Schristos #include <openssl/ssl.h>
1013d40330Schristos
11*b0d17251Schristos ossl_ssize_t SSL_sendfile(SSL *s, int fd, off_t offset, size_t size, int flags);
1213d40330Schristos int SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *written);
1313d40330Schristos int SSL_write(SSL *ssl, const void *buf, int num);
1413d40330Schristos
1513d40330Schristos=head1 DESCRIPTION
1613d40330Schristos
1713d40330SchristosSSL_write_ex() and SSL_write() write B<num> bytes from the buffer B<buf> into
1813d40330Schristosthe specified B<ssl> connection. On success SSL_write_ex() will store the number
1913d40330Schristosof bytes written in B<*written>.
2013d40330Schristos
21*b0d17251SchristosSSL_sendfile() writes B<size> bytes from offset B<offset> in the file
22*b0d17251Schristosdescriptor B<fd> to the specified SSL connection B<s>. This function provides
23*b0d17251Schristosefficient zero-copy semantics. SSL_sendfile() is available only when
24*b0d17251SchristosKernel TLS is enabled, which can be checked by calling BIO_get_ktls_send().
25*b0d17251SchristosIt is provided here to allow users to maintain the same interface.
26*b0d17251SchristosThe meaning of B<flags> is platform dependent.
27*b0d17251SchristosCurrently, under Linux it is ignored.
28*b0d17251Schristos
2913d40330Schristos=head1 NOTES
3013d40330Schristos
3113d40330SchristosIn the paragraphs below a "write function" is defined as one of either
3213d40330SchristosSSL_write_ex(), or SSL_write().
3313d40330Schristos
3413d40330SchristosIf necessary, a write function will negotiate a TLS/SSL session, if not already
3513d40330Schristosexplicitly performed by L<SSL_connect(3)> or L<SSL_accept(3)>. If the peer
3613d40330Schristosrequests a re-negotiation, it will be performed transparently during
3713d40330Schristosthe write function operation. The behaviour of the write functions depends on the
3813d40330Schristosunderlying BIO.
3913d40330Schristos
4013d40330SchristosFor the transparent negotiation to succeed, the B<ssl> must have been
4113d40330Schristosinitialized to client or server mode. This is being done by calling
4213d40330SchristosL<SSL_set_connect_state(3)> or SSL_set_accept_state()
4313d40330Schristosbefore the first call to a write function.
4413d40330Schristos
4513d40330SchristosIf the underlying BIO is B<blocking>, the write functions will only return, once
4613d40330Schristosthe write operation has been finished or an error occurred.
4713d40330Schristos
48f30e0929SchristosIf the underlying BIO is B<nonblocking> the write functions will also return
4913d40330Schristoswhen the underlying BIO could not satisfy the needs of the function to continue
5013d40330Schristosthe operation. In this case a call to L<SSL_get_error(3)> with the
5113d40330Schristosreturn value of the write function will yield B<SSL_ERROR_WANT_READ>
5213d40330Schristosor B<SSL_ERROR_WANT_WRITE>. As at any time a re-negotiation is possible, a
5313d40330Schristoscall to a write function can also cause read operations! The calling process
5413d40330Schristosthen must repeat the call after taking appropriate action to satisfy the needs
5513d40330Schristosof the write function. The action depends on the underlying BIO. When using a
56f30e0929Schristosnonblocking socket, nothing is to be done, but select() can be used to check
5713d40330Schristosfor the required condition. When using a buffering BIO, like a BIO pair, data
5813d40330Schristosmust be written into or retrieved out of the BIO before being able to continue.
5913d40330Schristos
6013d40330SchristosThe write functions will only return with success when the complete contents of
6113d40330SchristosB<buf> of length B<num> has been written. This default behaviour can be changed
6213d40330Schristoswith the SSL_MODE_ENABLE_PARTIAL_WRITE option of L<SSL_CTX_set_mode(3)>. When
6313d40330Schristosthis flag is set the write functions will also return with success when a
6413d40330Schristospartial write has been successfully completed. In this case the write function
6513d40330Schristosoperation is considered completed. The bytes are sent and a new write call with
6613d40330Schristosa new buffer (with the already sent bytes removed) must be started. A partial
6713d40330Schristoswrite is performed with the size of a message block, which is 16kB.
6813d40330Schristos
694ce06407Schristos=head1 WARNINGS
7013d40330Schristos
7113d40330SchristosWhen a write function call has to be repeated because L<SSL_get_error(3)>
7213d40330Schristosreturned B<SSL_ERROR_WANT_READ> or B<SSL_ERROR_WANT_WRITE>, it must be repeated
7313d40330Schristoswith the same arguments.
7413d40330SchristosThe data that was passed might have been partially processed.
7513d40330SchristosWhen B<SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER> was set using L<SSL_CTX_set_mode(3)>
7613d40330Schristosthe pointer can be different, but the data and length should still be the same.
7713d40330Schristos
7813d40330SchristosYou should not call SSL_write() with num=0, it will return an error.
7913d40330SchristosSSL_write_ex() can be called with num=0, but will not send application data to
8013d40330Schristosthe peer.
8113d40330Schristos
8213d40330Schristos=head1 RETURN VALUES
8313d40330Schristos
8413d40330SchristosSSL_write_ex() will return 1 for success or 0 for failure. Success means that
8513d40330Schristosall requested application data bytes have been written to the SSL connection or,
8613d40330Schristosif SSL_MODE_ENABLE_PARTIAL_WRITE is in use, at least 1 application data byte has
8713d40330Schristosbeen written to the SSL connection. Failure means that not all the requested
8813d40330Schristosbytes have been written yet (if SSL_MODE_ENABLE_PARTIAL_WRITE is not in use) or
8913d40330Schristosno bytes could be written to the SSL connection (if
9013d40330SchristosSSL_MODE_ENABLE_PARTIAL_WRITE is in use). Failures can be retryable (e.g. the
9113d40330Schristosnetwork write buffer has temporarily filled up) or non-retryable (e.g. a fatal
9213d40330Schristosnetwork error). In the event of a failure call L<SSL_get_error(3)> to find out
9313d40330Schristosthe reason which indicates whether the call is retryable or not.
9413d40330Schristos
9513d40330SchristosFor SSL_write() the following return values can occur:
9613d40330Schristos
9713d40330Schristos=over 4
9813d40330Schristos
9913d40330Schristos=item E<gt> 0
10013d40330Schristos
10113d40330SchristosThe write operation was successful, the return value is the number of
10213d40330Schristosbytes actually written to the TLS/SSL connection.
10313d40330Schristos
10413d40330Schristos=item Z<><= 0
10513d40330Schristos
10613d40330SchristosThe write operation was not successful, because either the connection was
10713d40330Schristosclosed, an error occurred or action must be taken by the calling process.
10813d40330SchristosCall SSL_get_error() with the return value B<ret> to find out the reason.
10913d40330Schristos
11013d40330SchristosOld documentation indicated a difference between 0 and -1, and that -1 was
11113d40330Schristosretryable.
11213d40330SchristosYou should instead call SSL_get_error() to find out if it's retryable.
11313d40330Schristos
11413d40330Schristos=back
11513d40330Schristos
116*b0d17251SchristosFor SSL_sendfile(), the following return values can occur:
117*b0d17251Schristos
118*b0d17251Schristos=over 4
119*b0d17251Schristos
120*b0d17251Schristos=item Z<>>= 0
121*b0d17251Schristos
122*b0d17251SchristosThe write operation was successful, the return value is the number
123*b0d17251Schristosof bytes of the file written to the TLS/SSL connection. The return
124*b0d17251Schristosvalue can be less than B<size> for a partial write.
125*b0d17251Schristos
126*b0d17251Schristos=item E<lt> 0
127*b0d17251Schristos
128*b0d17251SchristosThe write operation was not successful, because either the connection was
129*b0d17251Schristosclosed, an error occurred or action must be taken by the calling process.
130*b0d17251SchristosCall SSL_get_error() with the return value to find out the reason.
131*b0d17251Schristos
132*b0d17251Schristos=back
133*b0d17251Schristos
13413d40330Schristos=head1 SEE ALSO
13513d40330Schristos
13613d40330SchristosL<SSL_get_error(3)>, L<SSL_read_ex(3)>, L<SSL_read(3)>
13713d40330SchristosL<SSL_CTX_set_mode(3)>, L<SSL_CTX_new(3)>,
13813d40330SchristosL<SSL_connect(3)>, L<SSL_accept(3)>
139*b0d17251SchristosL<SSL_set_connect_state(3)>, L<BIO_ctrl(3)>,
14013d40330SchristosL<ssl(7)>, L<bio(7)>
14113d40330Schristos
142a3b08d93Schristos=head1 HISTORY
143a3b08d93Schristos
144a3b08d93SchristosThe SSL_write_ex() function was added in OpenSSL 1.1.1.
145*b0d17251SchristosThe SSL_sendfile() function was added in OpenSSL 3.0.
146a3b08d93Schristos
14713d40330Schristos=head1 COPYRIGHT
14813d40330Schristos
149*b0d17251SchristosCopyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
15013d40330Schristos
151*b0d17251SchristosLicensed under the Apache License 2.0 (the "License").  You may not use
15213d40330Schristosthis file except in compliance with the License.  You can obtain a copy
15313d40330Schristosin the file LICENSE in the source distribution or at
15413d40330SchristosL<https://www.openssl.org/source/license.html>.
15513d40330Schristos
15613d40330Schristos=cut
157