1 .\" $OpenBSD: tls_read.3,v 1.4 2017/02/20 13:09:15 schwarze Exp $
3 .\" Copyright (c) 2014, 2015 Ted Unangst <tedu@openbsd.org>
4 .\" Copyright (c) 2015 Doug Hogan <doug@openbsd.org>
5 .\" Copyright (c) 2015 Joel Sing <jsing@openbsd.org>
6 .\" Copyright (c) 2015 Bob Beck <beck@openbsd.org>
7 .\" Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
9 .\" Permission to use, copy, modify, and distribute this software for any
10 .\" purpose with or without fee is hereby granted, provided that the above
11 .\" copyright notice and this permission notice appear in all copies.
13 .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 .Dd $Mdocdate: February 20 2017 $
31 .Nd use a TLS connection
47 .Fn tls_handshake "struct tls *ctx"
49 .Fn tls_error "struct tls *ctx"
51 .Fn tls_close "struct tls *ctx"
53 .Fn tls_reset "struct tls *ctx"
58 bytes of data from the socket into
60 It returns the amount of data read.
68 It returns the amount of data written.
71 explicitly performs the TLS handshake.
72 It is only necessary to call this function if you need to guarantee that the
73 handshake has completed, as both
77 automatically perform the TLS handshake when necessary.
81 function may be used to retrieve a string containing more information
82 about the most recent error relating to a context.
85 closes a connection after use.
86 Only the TLS layer will be shut down and the caller is responsible for closing
87 the file descriptors, unless the connection was established using
90 .Xr tls_connect_servername 3 .
91 After closing the connection,
95 .\" XXX Fn tls_reset does what?
100 return a size on success or -1 on error.
105 return 0 on success or -1 on error.
110 if no error occurred with
112 during or since the last call to
121 or if memory allocation failed while trying to assemble the string
122 describing the most recent error related to
131 functions have two special return values:
133 .Bl -tag -width "TLS_WANT_POLLOUT" -offset indent -compact
134 .It Dv TLS_WANT_POLLIN
135 The underlying read file descriptor needs to be readable in order to continue.
136 .It Dv TLS_WANT_POLLOUT
137 The underlying write file descriptor needs to be writeable in order to continue.
140 In the case of blocking file descriptors, the same function call should be
141 repeated immediately.
142 In the case of non-blocking file descriptors, the same function call should be
143 repeated when the required condition has been met.
145 Callers of these functions cannot rely on the value of the global
147 To prevent mishandling of error conditions,
156 The following example demonstrates how to handle TLS writes on a blocking
158 .Bd -literal -offset indent
163 ret = tls_write(ctx, buf, len);
164 if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT)
167 err(1, "tls_write: %s", tls_error(ctx));
174 The following example demonstrates how to handle TLS writes on a
175 non-blocking file descriptor using
177 .Bd -literal -offset indent
180 pfd[0].events = POLLIN|POLLOUT;
182 nready = poll(pfd, 1, 0);
185 if ((pfd[0].revents & (POLLERR|POLLNVAL)))
186 errx(1, "bad fd %d", pfd[0].fd);
187 if ((pfd[0].revents & (pfd[0].events|POLLHUP))) {
190 ret = tls_write(ctx, buf, len);
191 if (ret == TLS_WANT_POLLIN)
192 pfd[0].events = POLLIN;
193 else if (ret == TLS_WANT_POLLOUT)
194 pfd[0].events = POLLOUT;
196 err(1, "tls_write: %s", tls_error(ctx));
206 .Xr tls_accept_socket 3 ,
207 .Xr tls_configure 3 ,
208 .Xr tls_conn_version 3 ,
211 .Xr tls_ocsp_process_response 3
221 and got their final names in
228 .An Joel Sing Aq Mt jsing@openbsd.org
229 with contributions from
230 .An Bob Beck Aq Mt beck@openbsd.org
234 returns an internal pointer.
235 It must not be freed by the application, or a double free error
237 The pointer will become invalid when the next error occurs with
239 Consequently, if the application may need the message at a later
240 time, it has to copy the string before calling the next
244 or a segmentation fault or read access to unintended data is the