xref: /openbsd-src/lib/libssl/man/SSL_write.3 (revision 076fb2587504a6b02d30b90a371aec08753a80e3)
1*076fb258Sschwarze.\" $OpenBSD: SSL_write.3,v 1.7 2021/10/24 15:10:13 schwarze Exp $
2*076fb258Sschwarze.\" full merge up to: OpenSSL b97fdb57 Nov 11 09:33:09 2016 +0100
3*076fb258Sschwarze.\" partial merge up to: OpenSSL 24a535ea Sep 22 13:14:20 2020 +0100
4f1a3c524Sschwarze.\"
5*076fb258Sschwarze.\" This file was written by Lutz Jaenicke <jaenicke@openssl.org>
6*076fb258Sschwarze.\" and Matt Caswell <matt@openssl.org>.
7*076fb258Sschwarze.\" Copyright (c) 2000, 2001, 2002, 2016 The OpenSSL Project.
8*076fb258Sschwarze.\" All rights reserved.
9f1a3c524Sschwarze.\"
10cb6a1c70Sschwarze.\" Redistribution and use in source and binary forms, with or without
11cb6a1c70Sschwarze.\" modification, are permitted provided that the following conditions
12cb6a1c70Sschwarze.\" are met:
13cb6a1c70Sschwarze.\"
14cb6a1c70Sschwarze.\" 1. Redistributions of source code must retain the above copyright
15cb6a1c70Sschwarze.\"    notice, this list of conditions and the following disclaimer.
16cb6a1c70Sschwarze.\"
17cb6a1c70Sschwarze.\" 2. Redistributions in binary form must reproduce the above copyright
18cb6a1c70Sschwarze.\"    notice, this list of conditions and the following disclaimer in
19cb6a1c70Sschwarze.\"    the documentation and/or other materials provided with the
20cb6a1c70Sschwarze.\"    distribution.
21cb6a1c70Sschwarze.\"
22cb6a1c70Sschwarze.\" 3. All advertising materials mentioning features or use of this
23cb6a1c70Sschwarze.\"    software must display the following acknowledgment:
24cb6a1c70Sschwarze.\"    "This product includes software developed by the OpenSSL Project
25cb6a1c70Sschwarze.\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
26cb6a1c70Sschwarze.\"
27cb6a1c70Sschwarze.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28cb6a1c70Sschwarze.\"    endorse or promote products derived from this software without
29cb6a1c70Sschwarze.\"    prior written permission. For written permission, please contact
30cb6a1c70Sschwarze.\"    openssl-core@openssl.org.
31cb6a1c70Sschwarze.\"
32cb6a1c70Sschwarze.\" 5. Products derived from this software may not be called "OpenSSL"
33cb6a1c70Sschwarze.\"    nor may "OpenSSL" appear in their names without prior written
34cb6a1c70Sschwarze.\"    permission of the OpenSSL Project.
35cb6a1c70Sschwarze.\"
36cb6a1c70Sschwarze.\" 6. Redistributions of any form whatsoever must retain the following
37cb6a1c70Sschwarze.\"    acknowledgment:
38cb6a1c70Sschwarze.\"    "This product includes software developed by the OpenSSL Project
39cb6a1c70Sschwarze.\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
40cb6a1c70Sschwarze.\"
41cb6a1c70Sschwarze.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42cb6a1c70Sschwarze.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43cb6a1c70Sschwarze.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44cb6a1c70Sschwarze.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
45cb6a1c70Sschwarze.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46cb6a1c70Sschwarze.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47cb6a1c70Sschwarze.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48cb6a1c70Sschwarze.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49cb6a1c70Sschwarze.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50cb6a1c70Sschwarze.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51cb6a1c70Sschwarze.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52cb6a1c70Sschwarze.\" OF THE POSSIBILITY OF SUCH DAMAGE.
53cb6a1c70Sschwarze.\"
54*076fb258Sschwarze.Dd $Mdocdate: October 24 2021 $
55f1a3c524Sschwarze.Dt SSL_WRITE 3
56f1a3c524Sschwarze.Os
57f1a3c524Sschwarze.Sh NAME
58*076fb258Sschwarze.Nm SSL_write_ex ,
59f1a3c524Sschwarze.Nm SSL_write
60*076fb258Sschwarze.Nd write bytes to a TLS connection
61f1a3c524Sschwarze.Sh SYNOPSIS
62f1a3c524Sschwarze.In openssl/ssl.h
63f1a3c524Sschwarze.Ft int
64*076fb258Sschwarze.Fn SSL_write_ex "SSL *ssl" "const void *buf" "size_t num" "size_t *written"
65*076fb258Sschwarze.Ft int
66f1a3c524Sschwarze.Fn SSL_write "SSL *ssl" "const void *buf" "int num"
67f1a3c524Sschwarze.Sh DESCRIPTION
68*076fb258Sschwarze.Fn SSL_write_ex
69*076fb258Sschwarzeand
70f1a3c524Sschwarze.Fn SSL_write
71*076fb258Sschwarzewrite
72f1a3c524Sschwarze.Fa num
73f1a3c524Sschwarzebytes from the buffer
74f1a3c524Sschwarze.Fa buf
75f1a3c524Sschwarzeinto the specified
76f1a3c524Sschwarze.Fa ssl
77f1a3c524Sschwarzeconnection.
78*076fb258SschwarzeOn success
79*076fb258Sschwarze.Fn SSL_write_ex
80*076fb258Sschwarzestores the number of bytes written in
81*076fb258Sschwarze.Pf * Fa written .
82cb6a1c70Sschwarze.Pp
83*076fb258SschwarzeIn the following,
84*076fb258Sschwarze.Fn SSL_write_ex
85*076fb258Sschwarzeand
86f1a3c524Sschwarze.Fn SSL_write
87*076fb258Sschwarzeare called
88*076fb258Sschwarze.Dq write functions .
89*076fb258Sschwarze.Pp
90*076fb258SschwarzeIf necessary, a write function negotiates a TLS session,
91*076fb258Sschwarzeif not already explicitly performed by
92f1a3c524Sschwarze.Xr SSL_connect 3
93f1a3c524Sschwarzeor
94f1a3c524Sschwarze.Xr SSL_accept 3 .
95f1a3c524SschwarzeIf the peer requests a re-negotiation,
96f1a3c524Sschwarzeit will be performed transparently during the
97*076fb258Sschwarzewrite function operation.
98*076fb258SschwarzeThe behaviour of the write functions depends on the underlying
99f1a3c524Sschwarze.Vt BIO .
100f1a3c524Sschwarze.Pp
101f1a3c524SschwarzeFor the transparent negotiation to succeed, the
102f1a3c524Sschwarze.Fa ssl
103f1a3c524Sschwarzemust have been initialized to client or server mode.
104*076fb258SschwarzeThis is done by calling
105f1a3c524Sschwarze.Xr SSL_set_connect_state 3
106f1a3c524Sschwarzeor
107f1a3c524Sschwarze.Xr SSL_set_accept_state 3
108*076fb258Sschwarzebefore the first call to a write function.
109f1a3c524Sschwarze.Pp
110f1a3c524SschwarzeIf the underlying
111f1a3c524Sschwarze.Vt BIO
112f1a3c524Sschwarzeis
113f1a3c524Sschwarze.Em blocking ,
114*076fb258Sschwarzethe write function
115f1a3c524Sschwarzewill only return once the write operation has been finished or an error
116ff59bafaStboccurred, except when a renegotiation takes place, in which case a
117f1a3c524Sschwarze.Dv SSL_ERROR_WANT_READ
118f1a3c524Sschwarzemay occur.
119f1a3c524SschwarzeThis behaviour can be controlled with the
120f1a3c524Sschwarze.Dv SSL_MODE_AUTO_RETRY
121f1a3c524Sschwarzeflag of the
122f1a3c524Sschwarze.Xr SSL_CTX_set_mode 3
123f1a3c524Sschwarzecall.
124f1a3c524Sschwarze.Pp
125f1a3c524SschwarzeIf the underlying
126f1a3c524Sschwarze.Vt BIO
127f1a3c524Sschwarzeis
128f1a3c524Sschwarze.Em non-blocking ,
129*076fb258Sschwarzethe write function will also return when the underlying
130f1a3c524Sschwarze.Vt BIO
131*076fb258Sschwarzecould not satisfy the needs of the function to continue the operation.
132f1a3c524SschwarzeIn this case a call to
133f1a3c524Sschwarze.Xr SSL_get_error 3
134*076fb258Sschwarzewith the return value of the write function will yield
135f1a3c524Sschwarze.Dv SSL_ERROR_WANT_READ
136f1a3c524Sschwarzeor
137f1a3c524Sschwarze.Dv SSL_ERROR_WANT_WRITE .
138f1a3c524SschwarzeAs at any time a re-negotiation is possible, a call to
139*076fb258Sschwarzea write function can also cause read operations.
140f1a3c524SschwarzeThe calling process then must repeat the call after taking appropriate action
141*076fb258Sschwarzeto satisfy the needs of the write function.
142f1a3c524SschwarzeThe action depends on the underlying
143f1a3c524Sschwarze.Vt BIO .
144f1a3c524SschwarzeWhen using a non-blocking socket, nothing is to be done, but
145f1a3c524Sschwarze.Xr select 2
146f1a3c524Sschwarzecan be used to check for the required condition.
147f1a3c524SschwarzeWhen using a buffering
148f1a3c524Sschwarze.Vt BIO ,
149f1a3c524Sschwarzelike a
150f1a3c524Sschwarze.Vt BIO
151f1a3c524Sschwarzepair, data must be written into or retrieved out of the BIO before being able
152f1a3c524Sschwarzeto continue.
153f1a3c524Sschwarze.Pp
154*076fb258SschwarzeThe write functions
155cb6a1c70Sschwarzewill only return with success when the complete contents of
156f1a3c524Sschwarze.Fa buf
157f1a3c524Sschwarzeof length
158f1a3c524Sschwarze.Fa num
159f1a3c524Sschwarzehave been written.
160f1a3c524SschwarzeThis default behaviour can be changed with the
161f1a3c524Sschwarze.Dv SSL_MODE_ENABLE_PARTIAL_WRITE
162f1a3c524Sschwarzeoption of
163f1a3c524Sschwarze.Xr SSL_CTX_set_mode 3 .
164*076fb258SschwarzeWhen this flag is set, the write functions will also return with
165*076fb258Sschwarzesuccess when a partial write has been successfully completed.
166*076fb258SschwarzeIn this case the write function operation is considered completed.
167*076fb258SschwarzeThe bytes are sent and a new write call with a new buffer (with the
168*076fb258Sschwarzealready sent bytes removed) must be started.
169cb6a1c70SschwarzeA partial write is performed with the size of a message block,
170cb6a1c70Sschwarzewhich is 16kB.
171cb6a1c70Sschwarze.Pp
172*076fb258SschwarzeWhen a write function call has to be repeated because
173cb6a1c70Sschwarze.Xr SSL_get_error 3
174cb6a1c70Sschwarzereturned
175f1a3c524Sschwarze.Dv SSL_ERROR_WANT_READ
176f1a3c524Sschwarzeor
177f1a3c524Sschwarze.Dv SSL_ERROR_WANT_WRITE ,
178f1a3c524Sschwarzeit must be repeated with the same arguments.
179f1a3c524Sschwarze.Pp
180f1a3c524SschwarzeWhen calling
181f1a3c524Sschwarze.Fn SSL_write
182f1a3c524Sschwarzewith
183cb6a1c70Sschwarze.Fa num Ns =0
184cb6a1c70Sschwarzebytes to be sent, the behaviour is undefined.
185*076fb258Sschwarze.Fn SSL_write_ex
186*076fb258Sschwarzecan be called with
187*076fb258Sschwarze.Fa num Ns =0 ,
188*076fb258Sschwarzebut will not send application data to the peer.
189f1a3c524Sschwarze.Sh RETURN VALUES
190*076fb258Sschwarze.Fn SSL_write_ex
191*076fb258Sschwarzereturns 1 for success or 0 for failure.
192*076fb258SschwarzeSuccess means that all requested application data bytes have been
193*076fb258Sschwarzewritten to the TLS connection or, if
194*076fb258Sschwarze.Dv SSL_MODE_ENABLE_PARTIAL_WRITE
195*076fb258Sschwarzeis in use, at least one application data byte has been written
196*076fb258Sschwarzeto the TLS connection.
197*076fb258SschwarzeFailure means that not all the requested bytes have been written yet (if
198*076fb258Sschwarze.Dv SSL_MODE_ENABLE_PARTIAL_WRITE
199*076fb258Sschwarzeis not in use) or no bytes could be written to the TLS connection (if
200*076fb258Sschwarze.Dv SSL_MODE_ENABLE_PARTIAL_WRITE
201*076fb258Sschwarzeis in use).
202*076fb258SschwarzeFailures can be retryable (e.g. the network write buffer has temporarily
203*076fb258Sschwarzefilled up) or non-retryable (e.g. a fatal network error).
204*076fb258SschwarzeIn the event of a failure, call
205*076fb258Sschwarze.Xr SSL_get_error 3
206*076fb258Sschwarzeto find out the reason
207*076fb258Sschwarzewhich indicates whether the call is retryable or not.
208*076fb258Sschwarze.Pp
209*076fb258SschwarzeFor
210*076fb258Sschwarze.Fn SSL_write ,
211*076fb258Sschwarzethe following return values can occur:
212f1a3c524Sschwarze.Bl -tag -width Ds
213f1a3c524Sschwarze.It >0
214f1a3c524SschwarzeThe write operation was successful.
215*076fb258SschwarzeThe return value is the number of bytes actually written to the TLS
216f1a3c524Sschwarzeconnection.
217f1a3c524Sschwarze.It 0
218f1a3c524SschwarzeThe write operation was not successful.
219f1a3c524SschwarzeProbably the underlying connection was closed.
220f1a3c524SschwarzeCall
221f1a3c524Sschwarze.Xr SSL_get_error 3
222f1a3c524Sschwarzewith the return value to find out whether an error occurred or the connection
223f1a3c524Sschwarzewas shut down cleanly
224f1a3c524Sschwarze.Pq Dv SSL_ERROR_ZERO_RETURN .
225f1a3c524Sschwarze.It <0
226f1a3c524SschwarzeThe write operation was not successful, because either an error occurred or
227f1a3c524Sschwarzeaction must be taken by the calling process.
228f1a3c524SschwarzeCall
229f1a3c524Sschwarze.Xr SSL_get_error 3
230f1a3c524Sschwarzewith the return value to find out the reason.
231f1a3c524Sschwarze.El
232f1a3c524Sschwarze.Sh SEE ALSO
2335bee811bSschwarze.Xr BIO_new 3 ,
234f1a3c524Sschwarze.Xr ssl 3 ,
235f1a3c524Sschwarze.Xr SSL_accept 3 ,
236f1a3c524Sschwarze.Xr SSL_connect 3 ,
237f1a3c524Sschwarze.Xr SSL_CTX_new 3 ,
238f1a3c524Sschwarze.Xr SSL_CTX_set_mode 3 ,
239f1a3c524Sschwarze.Xr SSL_get_error 3 ,
240f1a3c524Sschwarze.Xr SSL_read 3 ,
241f1a3c524Sschwarze.Xr SSL_set_connect_state 3
2428fba1ec8Sschwarze.Sh HISTORY
2438fba1ec8Sschwarze.Fn SSL_write
24410e00d17Sschwarzeappeared in SSLeay 0.4 or earlier and has been available since
2458fba1ec8Sschwarze.Ox 2.4 .
246*076fb258Sschwarze.Pp
247*076fb258Sschwarze.Fn SSL_write_ex
248*076fb258Sschwarzefirst appeared in OpenSSL 1.1.1 and has been available since
249*076fb258Sschwarze.Ox 7.1 .
250