1*58d32726Sschwarze.\" $OpenBSD: tls_read.3,v 1.8 2023/09/18 17:25:15 schwarze Exp $ 2e690d60fSschwarze.\" 34801fc0bSschwarze.\" Copyright (c) 2014, 2015 Ted Unangst <tedu@openbsd.org> 44801fc0bSschwarze.\" Copyright (c) 2015 Doug Hogan <doug@openbsd.org> 54801fc0bSschwarze.\" Copyright (c) 2015 Joel Sing <jsing@openbsd.org> 64801fc0bSschwarze.\" Copyright (c) 2015 Bob Beck <beck@openbsd.org> 78990a602Sschwarze.\" Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org> 8e690d60fSschwarze.\" 9e690d60fSschwarze.\" Permission to use, copy, modify, and distribute this software for any 10e690d60fSschwarze.\" purpose with or without fee is hereby granted, provided that the above 11e690d60fSschwarze.\" copyright notice and this permission notice appear in all copies. 12e690d60fSschwarze.\" 13e690d60fSschwarze.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14e690d60fSschwarze.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15e690d60fSschwarze.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16e690d60fSschwarze.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17e690d60fSschwarze.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18e690d60fSschwarze.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19e690d60fSschwarze.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20e690d60fSschwarze.\" 21*58d32726Sschwarze.Dd $Mdocdate: September 18 2023 $ 22e690d60fSschwarze.Dt TLS_READ 3 23e690d60fSschwarze.Os 24e690d60fSschwarze.Sh NAME 25e690d60fSschwarze.Nm tls_read , 26e690d60fSschwarze.Nm tls_write , 27e690d60fSschwarze.Nm tls_handshake , 28e690d60fSschwarze.Nm tls_error , 29*58d32726Sschwarze.Nm tls_close 30e690d60fSschwarze.Nd use a TLS connection 31e690d60fSschwarze.Sh SYNOPSIS 32e690d60fSschwarze.In tls.h 33e690d60fSschwarze.Ft ssize_t 34e690d60fSschwarze.Fo tls_read 35e690d60fSschwarze.Fa "struct tls *ctx" 36e690d60fSschwarze.Fa "void *buf" 37e690d60fSschwarze.Fa "size_t buflen" 38e690d60fSschwarze.Fc 39e690d60fSschwarze.Ft ssize_t 40e690d60fSschwarze.Fo tls_write 41e690d60fSschwarze.Fa "struct tls *ctx" 42e690d60fSschwarze.Fa "const void *buf" 43e690d60fSschwarze.Fa "size_t buflen" 44e690d60fSschwarze.Fc 45e690d60fSschwarze.Ft int 46e690d60fSschwarze.Fn tls_handshake "struct tls *ctx" 47e690d60fSschwarze.Ft const char * 48e690d60fSschwarze.Fn tls_error "struct tls *ctx" 49e690d60fSschwarze.Ft int 50e690d60fSschwarze.Fn tls_close "struct tls *ctx" 51e690d60fSschwarze.Sh DESCRIPTION 52e690d60fSschwarze.Fn tls_read 53e690d60fSschwarzereads 54e690d60fSschwarze.Fa buflen 55e690d60fSschwarzebytes of data from the socket into 56e690d60fSschwarze.Fa buf . 57e690d60fSschwarzeIt returns the amount of data read. 58e690d60fSschwarze.Pp 59e690d60fSschwarze.Fn tls_write 60e690d60fSschwarzewrites 61e690d60fSschwarze.Fa buflen 62e690d60fSschwarzebytes of data from 63e690d60fSschwarze.Fa buf 64e690d60fSschwarzeto the socket. 65e690d60fSschwarzeIt returns the amount of data written. 66e690d60fSschwarze.Pp 67e690d60fSschwarze.Fn tls_handshake 68e690d60fSschwarzeexplicitly performs the TLS handshake. 69e690d60fSschwarzeIt is only necessary to call this function if you need to guarantee that the 70e690d60fSschwarzehandshake has completed, as both 71e690d60fSschwarze.Fn tls_read 72e690d60fSschwarzeand 73e690d60fSschwarze.Fn tls_write 74e690d60fSschwarzeautomatically perform the TLS handshake when necessary. 75e690d60fSschwarze.Pp 76e690d60fSschwarzeThe 77e690d60fSschwarze.Fn tls_error 78e690d60fSschwarzefunction may be used to retrieve a string containing more information 79e690d60fSschwarzeabout the most recent error relating to a context. 80e690d60fSschwarze.Pp 81e690d60fSschwarze.Fn tls_close 82e690d60fSschwarzecloses a connection after use. 83e690d60fSschwarzeOnly the TLS layer will be shut down and the caller is responsible for closing 84e690d60fSschwarzethe file descriptors, unless the connection was established using 85e690d60fSschwarze.Xr tls_connect 3 86e690d60fSschwarzeor 87e690d60fSschwarze.Xr tls_connect_servername 3 . 88e690d60fSschwarzeAfter closing the connection, 89e690d60fSschwarze.Fa ctx 90e690d60fSschwarzecan be passed to 91e690d60fSschwarze.Xr tls_free 3 . 92e690d60fSschwarze.Sh RETURN VALUES 93e690d60fSschwarze.Fn tls_read 94e690d60fSschwarzeand 95e690d60fSschwarze.Fn tls_write 96e690d60fSschwarzereturn a size on success or -1 on error. 97e690d60fSschwarze.Pp 98e690d60fSschwarze.Fn tls_handshake 99e690d60fSschwarzeand 100e690d60fSschwarze.Fn tls_close 101e690d60fSschwarzereturn 0 on success or -1 on error. 102e690d60fSschwarze.Pp 103e690d60fSschwarzeThe 104e690d60fSschwarze.Fn tls_read , 105e690d60fSschwarze.Fn tls_write , 106e690d60fSschwarze.Fn tls_handshake , 107e690d60fSschwarzeand 108e690d60fSschwarze.Fn tls_close 1095719c9cbSjsingfunctions also have two special return values: 110e690d60fSschwarze.Pp 111e690d60fSschwarze.Bl -tag -width "TLS_WANT_POLLOUT" -offset indent -compact 112e690d60fSschwarze.It Dv TLS_WANT_POLLIN 113e690d60fSschwarzeThe underlying read file descriptor needs to be readable in order to continue. 114e690d60fSschwarze.It Dv TLS_WANT_POLLOUT 115e690d60fSschwarzeThe underlying write file descriptor needs to be writeable in order to continue. 116e690d60fSschwarze.El 117e690d60fSschwarze.Pp 118e690d60fSschwarzeIn the case of blocking file descriptors, the same function call should be 119e690d60fSschwarzerepeated immediately. 120e690d60fSschwarzeIn the case of non-blocking file descriptors, the same function call should be 121e690d60fSschwarzerepeated when the required condition has been met. 122e690d60fSschwarze.Pp 123e690d60fSschwarzeCallers of these functions cannot rely on the value of the global 124e690d60fSschwarze.Ar errno . 125e690d60fSschwarzeTo prevent mishandling of error conditions, 126e690d60fSschwarze.Fn tls_read , 127e690d60fSschwarze.Fn tls_write , 128e690d60fSschwarze.Fn tls_handshake , 129e690d60fSschwarzeand 130e690d60fSschwarze.Fn tls_close 131e690d60fSschwarzeall explicitly clear 132e690d60fSschwarze.Ar errno . 1335719c9cbSjsing.Pp 1345719c9cbSjsing.Fn tls_error 1355719c9cbSjsingreturns 1365719c9cbSjsing.Dv NULL 1375719c9cbSjsingif no error occurred with 1385719c9cbSjsing.Fa ctx 1395719c9cbSjsingduring or since the last call to 1405719c9cbSjsing.Fn tls_handshake , 1415719c9cbSjsing.Fn tls_read , 1425719c9cbSjsing.Fn tls_write , 1435719c9cbSjsing.Fn tls_close , 1445719c9cbSjsingor 145*58d32726Sschwarze.Xr tls_reset 3 1465719c9cbSjsinginvolving 1475719c9cbSjsing.Fa ctx , 1485719c9cbSjsingor if memory allocation failed while trying to assemble the string 1495719c9cbSjsingdescribing the most recent error related to 1505719c9cbSjsing.Fa ctx . 151e690d60fSschwarze.Sh EXAMPLES 152e690d60fSschwarzeThe following example demonstrates how to handle TLS writes on a blocking 153e690d60fSschwarzefile descriptor: 154e690d60fSschwarze.Bd -literal -offset indent 155e690d60fSschwarze\&... 156e690d60fSschwarzewhile (len > 0) { 157e690d60fSschwarze ssize_t ret; 158e690d60fSschwarze 159e690d60fSschwarze ret = tls_write(ctx, buf, len); 160e690d60fSschwarze if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) 161e690d60fSschwarze continue; 162fe1f7dfaSderaadt if (ret == -1) 1635030b68bSespie errx(1, "tls_write: %s", tls_error(ctx)); 164e690d60fSschwarze buf += ret; 165e690d60fSschwarze len -= ret; 166e690d60fSschwarze} 167e690d60fSschwarze\&... 168e690d60fSschwarze.Ed 169e690d60fSschwarze.Pp 170e690d60fSschwarzeThe following example demonstrates how to handle TLS writes on a 171e690d60fSschwarzenon-blocking file descriptor using 172e690d60fSschwarze.Xr poll 2 : 173e690d60fSschwarze.Bd -literal -offset indent 174e690d60fSschwarze\&... 175e690d60fSschwarzepfd[0].fd = fd; 176e690d60fSschwarzepfd[0].events = POLLIN|POLLOUT; 177e690d60fSschwarzewhile (len > 0) { 178e690d60fSschwarze nready = poll(pfd, 1, 0); 179e690d60fSschwarze if (nready == -1) 180e690d60fSschwarze err(1, "poll"); 181e690d60fSschwarze if ((pfd[0].revents & (POLLERR|POLLNVAL))) 182e690d60fSschwarze errx(1, "bad fd %d", pfd[0].fd); 183e690d60fSschwarze if ((pfd[0].revents & (pfd[0].events|POLLHUP))) { 184e690d60fSschwarze ssize_t ret; 185e690d60fSschwarze 186e690d60fSschwarze ret = tls_write(ctx, buf, len); 187e690d60fSschwarze if (ret == TLS_WANT_POLLIN) 188e690d60fSschwarze pfd[0].events = POLLIN; 189e690d60fSschwarze else if (ret == TLS_WANT_POLLOUT) 190e690d60fSschwarze pfd[0].events = POLLOUT; 191fe1f7dfaSderaadt else if (ret == -1) 1925030b68bSespie errx(1, "tls_write: %s", tls_error(ctx)); 193e690d60fSschwarze else { 194e690d60fSschwarze buf += ret; 195e690d60fSschwarze len -= ret; 196e690d60fSschwarze } 197e690d60fSschwarze } 198e690d60fSschwarze} 199e690d60fSschwarze\&... 200e690d60fSschwarze.Ed 201e690d60fSschwarze.Sh SEE ALSO 202e690d60fSschwarze.Xr tls_accept_socket 3 , 203e690d60fSschwarze.Xr tls_configure 3 , 204e690d60fSschwarze.Xr tls_conn_version 3 , 205e690d60fSschwarze.Xr tls_connect 3 , 206e690d60fSschwarze.Xr tls_init 3 , 207e690d60fSschwarze.Xr tls_ocsp_process_response 3 2088b02f64eSschwarze.Sh HISTORY 2098b02f64eSschwarze.Fn tls_read , 2108b02f64eSschwarze.Fn tls_write , 2118b02f64eSschwarze.Fn tls_error , 2128b02f64eSschwarzeand 213*58d32726Sschwarze.Fn tls_close 2148b02f64eSschwarzeappeared in 2158b02f64eSschwarze.Ox 5.6 2168b02f64eSschwarzeand got their final names in 2178b02f64eSschwarze.Ox 5.7 . 2188b02f64eSschwarze.Pp 2198b02f64eSschwarze.Fn tls_handshake 2208b02f64eSschwarzeappeared in 2218b02f64eSschwarze.Ox 5.9 . 2228b02f64eSschwarze.Sh AUTHORS 2238b02f64eSschwarze.An Joel Sing Aq Mt jsing@openbsd.org 2248b02f64eSschwarzewith contributions from 2258b02f64eSschwarze.An Bob Beck Aq Mt beck@openbsd.org 2268990a602Sschwarze.Sh CAVEATS 2278990a602SschwarzeThe function 2288990a602Sschwarze.Fn tls_error 2298990a602Sschwarzereturns an internal pointer. 2308990a602SschwarzeIt must not be freed by the application, or a double free error 2318990a602Sschwarzewill occur. 2328990a602SschwarzeThe pointer will become invalid when the next error occurs with 2338990a602Sschwarze.Fa ctx . 2348990a602SschwarzeConsequently, if the application may need the message at a later 2358990a602Sschwarzetime, it has to copy the string before calling the next 2368990a602Sschwarze.Sy libtls 2378990a602Sschwarzefunction involving 2388990a602Sschwarze.Fa ctx , 2398990a602Sschwarzeor a segmentation fault or read access to unintended data is the 2408990a602Sschwarzelikely result. 241