]> git.ipfire.org Git - thirdparty/openssl.git/blame - doc/man3/SSL_write.pod
In documentation, consistently refer to OpenSSL 3.0
[thirdparty/openssl.git] / doc / man3 / SSL_write.pod
CommitLineData
cc99526d
RL
1=pod
2
3=head1 NAME
4
7c3a7561 5SSL_write_ex, SSL_write, SSL_sendfile - write bytes to a TLS/SSL connection
cc99526d
RL
6
7=head1 SYNOPSIS
8
9 #include <openssl/ssl.h>
10
7c3a7561 11 ossl_ssize_t SSL_sendfile(SSL *s, int fd, off_t offset, size_t size, int flags);
7714dc5e 12 int SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *written);
e34cfcf7 13 int SSL_write(SSL *ssl, const void *buf, int num);
cc99526d
RL
14
15=head1 DESCRIPTION
16
7714dc5e
MC
17SSL_write_ex() and SSL_write() write B<num> bytes from the buffer B<buf> into
18the specified B<ssl> connection. On success SSL_write_ex() will store the number
19of bytes written in B<*written>.
c19b6c92 20
7c3a7561
BP
21SSL_sendfile() writes B<size> bytes from offset B<offset> in the file
22descriptor B<fd> to the specified SSL connection B<s>. This function provides
23efficient zero-copy semantics. SSL_sendfile() is available only when
24Kernel TLS is enabled, which can be checked by calling BIO_get_ktls_send().
25It is provided here to allow users to maintain the same interface.
26The meaning of B<flags> is platform dependent.
27Currently, under Linux it is ignored.
28
c19b6c92
RL
29=head1 NOTES
30
6782e5fd
MC
31In the paragraphs below a "write function" is defined as one of either
32SSL_write_ex(), or SSL_write().
33
34If necessary, a write function will negotiate a TLS/SSL session, if not already
35explicitly performed by L<SSL_connect(3)> or L<SSL_accept(3)>. If the peer
36requests a re-negotiation, it will be performed transparently during
27b138e9 37the write function operation. The behaviour of the write functions depends on the
6782e5fd 38underlying BIO.
cc99526d 39
b72ff470 40For the transparent negotiation to succeed, the B<ssl> must have been
7abe76e1 41initialized to client or server mode. This is being done by calling
9b86974e 42L<SSL_set_connect_state(3)> or SSL_set_accept_state()
6782e5fd 43before the first call to a write function.
b72ff470 44
6782e5fd 45If the underlying BIO is B<blocking>, the write functions will only return, once
57fd5170 46the write operation has been finished or an error occurred.
cc99526d 47
6782e5fd
MC
48If the underlying BIO is B<non-blocking> the write functions will also return
49when the underlying BIO could not satisfy the needs of the function to continue
50the operation. In this case a call to L<SSL_get_error(3)> with the
51return value of the write function will yield B<SSL_ERROR_WANT_READ>
7714dc5e 52or B<SSL_ERROR_WANT_WRITE>. As at any time a re-negotiation is possible, a
6782e5fd
MC
53call to a write function can also cause read operations! The calling process
54then must repeat the call after taking appropriate action to satisfy the needs
55of the write function. The action depends on the underlying BIO. When using a
56non-blocking socket, nothing is to be done, but select() can be used to check
57for the required condition. When using a buffering BIO, like a BIO pair, data
58must be written into or retrieved out of the BIO before being able to continue.
59
60The write functions will only return with success when the complete contents of
61B<buf> of length B<num> has been written. This default behaviour can be changed
62with the SSL_MODE_ENABLE_PARTIAL_WRITE option of L<SSL_CTX_set_mode(3)>. When
63this flag is set the write functions will also return with success when a
64partial write has been successfully completed. In this case the write function
65operation is considered completed. The bytes are sent and a new write call with
66a new buffer (with the already sent bytes removed) must be started. A partial
67write is performed with the size of a message block, which is 16kB.
4b3270f7 68
4aa4f333 69=head1 WARNING
c19b6c92 70
6782e5fd
MC
71When a write function call has to be repeated because L<SSL_get_error(3)>
72returned B<SSL_ERROR_WANT_READ> or B<SSL_ERROR_WANT_WRITE>, it must be repeated
c19b6c92 73with the same arguments.
57fd5170
KR
74The data that was passed might have been partially processed.
75When B<SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER> was set using L<SSL_CTX_set_mode(3)>
76the pointer can be different, but the data and length should still be the same.
c19b6c92 77
57fd5170
KR
78You should not call SSL_write() with num=0, it will return an error.
79SSL_write_ex() can be called with num=0, but will not send application data to
80the peer.
20adcfa0 81
cc99526d
RL
82=head1 RETURN VALUES
83
740bfeba
MC
84SSL_write_ex() will return 1 for success or 0 for failure. Success means that
85all requested application data bytes have been written to the SSL connection or,
86if SSL_MODE_ENABLE_PARTIAL_WRITE is in use, at least 1 application data byte has
87been written to the SSL connection. Failure means that not all the requested
88bytes have been written yet (if SSL_MODE_ENABLE_PARTIAL_WRITE is not in use) or
89no bytes could be written to the SSL connection (if
90SSL_MODE_ENABLE_PARTIAL_WRITE is in use). Failures can be retryable (e.g. the
91network write buffer has temporarily filled up) or non-retryable (e.g. a fatal
92network error). In the event of a failure call L<SSL_get_error(3)> to find out
ed9fa2c7 93the reason which indicates whether the call is retryable or not.
7714dc5e
MC
94
95For SSL_write() the following return values can occur:
cc99526d
RL
96
97=over 4
98
beacb0f0 99=item E<gt> 0
cc99526d 100
1e4e5492
UM
101The write operation was successful, the return value is the number of
102bytes actually written to the TLS/SSL connection.
cc99526d 103
beacb0f0 104=item Z<><= 0
cc99526d 105
beacb0f0
KR
106The write operation was not successful, because either the connection was
107closed, an error occurred or action must be taken by the calling process.
108Call SSL_get_error() with the return value B<ret> to find out the reason.
d93eb21c 109
beacb0f0
KR
110Old documentation indicated a difference between 0 and -1, and that -1 was
111retryable.
112You should instead call SSL_get_error() to find out if it's retryable.
cc99526d
RL
113
114=back
115
7c3a7561
BP
116For SSL_sendfile(), the following return values can occur:
117
118=over 4
119
120=item Z<>>= 0
121
122The write operation was successful, the return value is the number
123of bytes of the file written to the TLS/SSL connection.
124
125=item E<lt> 0
126
127The write operation was not successful, because either the connection was
c2969ff6 128closed, an error occurred or action must be taken by the calling process.
7c3a7561
BP
129Call SSL_get_error() with the return value to find out the reason.
130
131=back
132
cc99526d
RL
133=head1 SEE ALSO
134
7714dc5e 135L<SSL_get_error(3)>, L<SSL_read_ex(3)>, L<SSL_read(3)>
9b86974e
RS
136L<SSL_CTX_set_mode(3)>, L<SSL_CTX_new(3)>,
137L<SSL_connect(3)>, L<SSL_accept(3)>
7c3a7561 138L<SSL_set_connect_state(3)>, L<BIO_ctrl(3)>,
b97fdb57 139L<ssl(7)>, L<bio(7)>
cc99526d 140
b5c4bbbe
JL
141=head1 HISTORY
142
143The SSL_write_ex() function was added in OpenSSL 1.1.1.
4674aaf4 144The SSL_sendfile() function was added in OpenSSL 3.0.
b5c4bbbe 145
e2f92610
RS
146=head1 COPYRIGHT
147
b5c4bbbe 148Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
e2f92610 149
4746f25a 150Licensed under the Apache License 2.0 (the "License"). You may not use
e2f92610
RS
151this file except in compliance with the License. You can obtain a copy
152in the file LICENSE in the source distribution or at
153L<https://www.openssl.org/source/license.html>.
154
155=cut