1*076fb258Sschwarze.\" $OpenBSD: SSL_read.3,v 1.8 2021/10/24 15:10:13 schwarze Exp $ 2*076fb258Sschwarze.\" full merge up to: OpenSSL 5a2443ae Nov 14 11:37:36 2016 +0000 3*076fb258Sschwarze.\" partial merge up to: OpenSSL 24a535ea Sep 22 13:14:20 2020 +0100 4f1a3c524Sschwarze.\" 5545cba2cSschwarze.\" This file was written by Lutz Jaenicke <jaenicke@openssl.org> and 6545cba2cSschwarze.\" Matt Caswell <matt@openssl.org>. 7209714aeSschwarze.\" Copyright (c) 2000, 2001, 2008, 2016 The OpenSSL Project. 8209714aeSschwarze.\" All rights reserved. 9f1a3c524Sschwarze.\" 10545cba2cSschwarze.\" Redistribution and use in source and binary forms, with or without 11545cba2cSschwarze.\" modification, are permitted provided that the following conditions 12545cba2cSschwarze.\" are met: 13545cba2cSschwarze.\" 14545cba2cSschwarze.\" 1. Redistributions of source code must retain the above copyright 15545cba2cSschwarze.\" notice, this list of conditions and the following disclaimer. 16545cba2cSschwarze.\" 17545cba2cSschwarze.\" 2. Redistributions in binary form must reproduce the above copyright 18545cba2cSschwarze.\" notice, this list of conditions and the following disclaimer in 19545cba2cSschwarze.\" the documentation and/or other materials provided with the 20545cba2cSschwarze.\" distribution. 21545cba2cSschwarze.\" 22545cba2cSschwarze.\" 3. All advertising materials mentioning features or use of this 23545cba2cSschwarze.\" software must display the following acknowledgment: 24545cba2cSschwarze.\" "This product includes software developed by the OpenSSL Project 25545cba2cSschwarze.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 26545cba2cSschwarze.\" 27545cba2cSschwarze.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 28545cba2cSschwarze.\" endorse or promote products derived from this software without 29545cba2cSschwarze.\" prior written permission. For written permission, please contact 30545cba2cSschwarze.\" openssl-core@openssl.org. 31545cba2cSschwarze.\" 32545cba2cSschwarze.\" 5. Products derived from this software may not be called "OpenSSL" 33545cba2cSschwarze.\" nor may "OpenSSL" appear in their names without prior written 34545cba2cSschwarze.\" permission of the OpenSSL Project. 35545cba2cSschwarze.\" 36545cba2cSschwarze.\" 6. Redistributions of any form whatsoever must retain the following 37545cba2cSschwarze.\" acknowledgment: 38545cba2cSschwarze.\" "This product includes software developed by the OpenSSL Project 39545cba2cSschwarze.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 40545cba2cSschwarze.\" 41545cba2cSschwarze.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 42545cba2cSschwarze.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43545cba2cSschwarze.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 44545cba2cSschwarze.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 45545cba2cSschwarze.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46545cba2cSschwarze.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 47545cba2cSschwarze.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 48545cba2cSschwarze.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 49545cba2cSschwarze.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 50545cba2cSschwarze.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 51545cba2cSschwarze.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 52545cba2cSschwarze.\" OF THE POSSIBILITY OF SUCH DAMAGE. 53545cba2cSschwarze.\" 54*076fb258Sschwarze.Dd $Mdocdate: October 24 2021 $ 55f1a3c524Sschwarze.Dt SSL_READ 3 56f1a3c524Sschwarze.Os 57f1a3c524Sschwarze.Sh NAME 58*076fb258Sschwarze.Nm SSL_read_ex , 59bb16c35aSschwarze.Nm SSL_read , 60*076fb258Sschwarze.Nm SSL_peek_ex , 61bb16c35aSschwarze.Nm SSL_peek 62209714aeSschwarze.Nd read bytes from a TLS connection 63f1a3c524Sschwarze.Sh SYNOPSIS 64f1a3c524Sschwarze.In openssl/ssl.h 65f1a3c524Sschwarze.Ft int 66*076fb258Sschwarze.Fn SSL_read_ex "SSL *ssl" "void *buf" "size_t num" "size_t *readbytes" 67*076fb258Sschwarze.Ft int 68f1a3c524Sschwarze.Fn SSL_read "SSL *ssl" "void *buf" "int num" 69545cba2cSschwarze.Ft int 70*076fb258Sschwarze.Fn SSL_peek_ex "SSL *ssl" "void *buf" "size_t num" "size_t *readbytes" 71*076fb258Sschwarze.Ft int 72545cba2cSschwarze.Fn SSL_peek "SSL *ssl" "void *buf" "int num" 73f1a3c524Sschwarze.Sh DESCRIPTION 74*076fb258Sschwarze.Fn SSL_read_ex 75*076fb258Sschwarzeand 76f1a3c524Sschwarze.Fn SSL_read 77*076fb258Sschwarzetry to read 78f1a3c524Sschwarze.Fa num 79f1a3c524Sschwarzebytes from the specified 80f1a3c524Sschwarze.Fa ssl 81f1a3c524Sschwarzeinto the buffer 82f1a3c524Sschwarze.Fa buf . 83*076fb258SschwarzeOn success 84*076fb258Sschwarze.Fn SSL_read_ex 85*076fb258Sschwarzestores the number of bytes actually read in 86*076fb258Sschwarze.Pf * Fa readbytes . 87545cba2cSschwarze.Pp 88*076fb258Sschwarze.Fn SSL_peek_ex 89*076fb258Sschwarzeand 90545cba2cSschwarze.Fn SSL_peek 91*076fb258Sschwarzeare identical to 92*076fb258Sschwarze.Fn SSL_read_ex 93*076fb258Sschwarzeand 94*076fb258Sschwarze.Fn SSL_read , 95*076fb258Sschwarzerespectively, 96545cba2cSschwarzeexcept that no bytes are removed from the underlying BIO during 97545cba2cSschwarzethe read, such that a subsequent call to 98*076fb258Sschwarze.Fn SSL_read_ex 99*076fb258Sschwarzeor 100545cba2cSschwarze.Fn SSL_read 101545cba2cSschwarzewill yield at least the same bytes once again. 102545cba2cSschwarze.Pp 103545cba2cSschwarzeIn the following, 104*076fb258Sschwarze.Fn SSL_read_ex , 105*076fb258Sschwarze.Fn SSL_read , 106*076fb258Sschwarze.Fn SSL_peek_ex , 107545cba2cSschwarzeand 108545cba2cSschwarze.Fn SSL_peek 109545cba2cSschwarzeare called 110545cba2cSschwarze.Dq read functions . 111545cba2cSschwarze.Pp 112209714aeSschwarzeIf necessary, a read function will negotiate a TLS session, if 113545cba2cSschwarzenot already explicitly performed by 114f1a3c524Sschwarze.Xr SSL_connect 3 115f1a3c524Sschwarzeor 116f1a3c524Sschwarze.Xr SSL_accept 3 . 117545cba2cSschwarzeIf the peer requests a re-negotiation, it will be performed 118545cba2cSschwarzetransparently during the read function operation. 119545cba2cSschwarzeThe behaviour of the read functions depends on the underlying 120f1a3c524Sschwarze.Vt BIO . 121f1a3c524Sschwarze.Pp 122f1a3c524SschwarzeFor the transparent negotiation to succeed, the 123f1a3c524Sschwarze.Fa ssl 124f1a3c524Sschwarzemust have been initialized to client or server mode. 125545cba2cSschwarzeThis is done by calling 126f1a3c524Sschwarze.Xr SSL_set_connect_state 3 127f1a3c524Sschwarzeor 128f1a3c524Sschwarze.Xr SSL_set_accept_state 3 129545cba2cSschwarzebefore the first call to a read function. 130f1a3c524Sschwarze.Pp 131*076fb258SschwarzeThe read functions work based on the TLS records. 132545cba2cSschwarzeThe data are received in records (with a maximum record size of 16kB). 133545cba2cSschwarzeOnly when a record has been completely received, it can be processed 134f1a3c524Sschwarze(decrypted and checked for integrity). 135*076fb258SschwarzeTherefore, data that was not retrieved at the last read call can 136209714aeSschwarzestill be buffered inside the TLS layer and will be retrieved on the 137545cba2cSschwarzenext read call. 138f1a3c524SschwarzeIf 139f1a3c524Sschwarze.Fa num 140545cba2cSschwarzeis higher than the number of bytes buffered, the read functions 141f1a3c524Sschwarzewill return with the bytes buffered. 142545cba2cSschwarzeIf no more bytes are in the buffer, the read functions will trigger 143545cba2cSschwarzethe processing of the next record. 144545cba2cSschwarzeOnly when the record has been received and processed completely 145545cba2cSschwarzewill the read functions return reporting success. 146f1a3c524SschwarzeAt most the contents of the record will be returned. 147209714aeSschwarzeAs the size of a TLS record may exceed the maximum packet size 148545cba2cSschwarzeof the underlying transport (e.g., TCP), it may be necessary to 149545cba2cSschwarzeread several packets from the transport layer before the record is 150545cba2cSschwarzecomplete and the read call can succeed. 151f1a3c524Sschwarze.Pp 152f1a3c524SschwarzeIf the underlying 153f1a3c524Sschwarze.Vt BIO 154545cba2cSschwarzeis blocking, 155545cba2cSschwarzea read function will only return once the read operation has been 156545cba2cSschwarzefinished or an error occurred, except when a renegotiation takes 157545cba2cSschwarzeplace, in which case an 158f1a3c524Sschwarze.Dv SSL_ERROR_WANT_READ 159f1a3c524Sschwarzemay occur. 160f1a3c524SschwarzeThis behavior can be controlled with the 161f1a3c524Sschwarze.Dv SSL_MODE_AUTO_RETRY 162f1a3c524Sschwarzeflag of the 163f1a3c524Sschwarze.Xr SSL_CTX_set_mode 3 164f1a3c524Sschwarzecall. 165f1a3c524Sschwarze.Pp 166f1a3c524SschwarzeIf the underlying 167f1a3c524Sschwarze.Vt BIO 168545cba2cSschwarzeis non-blocking, a read function will also return when the underlying 169f1a3c524Sschwarze.Vt BIO 170545cba2cSschwarzecould not satisfy the needs of the function to continue the operation. 171f1a3c524SschwarzeIn this case a call to 172f1a3c524Sschwarze.Xr SSL_get_error 3 173545cba2cSschwarzewith the return value of the read function will yield 174f1a3c524Sschwarze.Dv SSL_ERROR_WANT_READ 175f1a3c524Sschwarzeor 176f1a3c524Sschwarze.Dv SSL_ERROR_WANT_WRITE . 177545cba2cSschwarzeAs at any time a re-negotiation is possible, a read function may 178545cba2cSschwarzealso cause write operations. 179545cba2cSschwarzeThe calling process must then repeat the call after taking appropriate 180545cba2cSschwarzeaction to satisfy the needs of the read function. 181f1a3c524SschwarzeThe action depends on the underlying 182f1a3c524Sschwarze.Vt BIO . 183f1a3c524SschwarzeWhen using a non-blocking socket, nothing is to be done, but 184f1a3c524Sschwarze.Xr select 2 185f1a3c524Sschwarzecan be used to check for the required condition. 186f1a3c524SschwarzeWhen using a buffering 187f1a3c524Sschwarze.Vt BIO , 188f1a3c524Sschwarzelike a 189f1a3c524Sschwarze.Vt BIO 190f1a3c524Sschwarzepair, data must be written into or retrieved out of the 191f1a3c524Sschwarze.Vt BIO 192f1a3c524Sschwarzebefore being able to continue. 193f1a3c524Sschwarze.Pp 194f1a3c524Sschwarze.Xr SSL_pending 3 195f1a3c524Sschwarzecan be used to find out whether there are buffered bytes available for 196f1a3c524Sschwarzeimmediate retrieval. 197545cba2cSschwarzeIn this case a read function can be called without blocking or 198545cba2cSschwarzeactually receiving new data from the underlying socket. 199545cba2cSschwarze.Pp 200545cba2cSschwarzeWhen a read function operation has to be repeated because of 201f1a3c524Sschwarze.Dv SSL_ERROR_WANT_READ 202f1a3c524Sschwarzeor 203f1a3c524Sschwarze.Dv SSL_ERROR_WANT_WRITE , 204f1a3c524Sschwarzeit must be repeated with the same arguments. 205f1a3c524Sschwarze.Sh RETURN VALUES 206*076fb258Sschwarze.Fn SSL_read_ex 207*076fb258Sschwarzeand 208*076fb258Sschwarze.Fn SSL_peek_ex 209*076fb258Sschwarzereturn 1 for success or 0 for failure. 210*076fb258SschwarzeSuccess means that one or more application data bytes 211*076fb258Sschwarzehave been read from the SSL connection. 212*076fb258SschwarzeFailure means that no bytes could be read from the SSL connection. 213*076fb258SschwarzeFailures can be retryable (e.g. we are waiting for more bytes to be 214*076fb258Sschwarzedelivered by the network) or non-retryable (e.g. a fatal network error). 215*076fb258SschwarzeIn the event of a failure, call 216*076fb258Sschwarze.Xr SSL_get_error 3 217*076fb258Sschwarzeto find out the reason which indicates whether the call is retryable or not. 218*076fb258Sschwarze.Pp 219*076fb258SschwarzeFor 220*076fb258Sschwarze.Fn SSL_read 221*076fb258Sschwarzeand 222*076fb258Sschwarze.Fn SSL_peek , 223*076fb258Sschwarzethe following return values can occur: 224f1a3c524Sschwarze.Bl -tag -width Ds 225f1a3c524Sschwarze.It >0 226545cba2cSschwarzeThe read operation was successful. 227545cba2cSschwarzeThe return value is the number of bytes actually read from the 228209714aeSschwarzeTLS connection. 229f1a3c524Sschwarze.It 0 230f1a3c524SschwarzeThe read operation was not successful. 231f1a3c524SschwarzeThe reason may either be a clean shutdown due to a 232f1a3c524Sschwarze.Dq close notify 233f1a3c524Sschwarzealert sent by the peer (in which case the 234f1a3c524Sschwarze.Dv SSL_RECEIVED_SHUTDOWN 235f1a3c524Sschwarzeflag in the ssl shutdown state is set (see 236f1a3c524Sschwarze.Xr SSL_shutdown 3 237f1a3c524Sschwarzeand 238f1a3c524Sschwarze.Xr SSL_set_shutdown 3 ) . 239f1a3c524SschwarzeIt is also possible that the peer simply shut down the underlying transport and 240f1a3c524Sschwarzethe shutdown is incomplete. 241f1a3c524SschwarzeCall 242209714aeSschwarze.Xr SSL_get_error 3 243f1a3c524Sschwarzewith the return value to find out whether an error occurred or the connection 244f1a3c524Sschwarzewas shut down cleanly 245f1a3c524Sschwarze.Pq Dv SSL_ERROR_ZERO_RETURN . 246f1a3c524Sschwarze.It <0 247f1a3c524SschwarzeThe read operation was not successful, because either an error occurred or 248f1a3c524Sschwarzeaction must be taken by the calling process. 249f1a3c524SschwarzeCall 250209714aeSschwarze.Xr SSL_get_error 3 251f1a3c524Sschwarzewith the return value to find out the reason. 252f1a3c524Sschwarze.El 253f1a3c524Sschwarze.Sh SEE ALSO 2545bee811bSschwarze.Xr BIO_new 3 , 255f1a3c524Sschwarze.Xr ssl 3 , 256f1a3c524Sschwarze.Xr SSL_accept 3 , 257f1a3c524Sschwarze.Xr SSL_connect 3 , 258f1a3c524Sschwarze.Xr SSL_CTX_new 3 , 259f1a3c524Sschwarze.Xr SSL_CTX_set_mode 3 , 260f1a3c524Sschwarze.Xr SSL_get_error 3 , 261f1a3c524Sschwarze.Xr SSL_pending 3 , 262f1a3c524Sschwarze.Xr SSL_set_connect_state 3 , 263f1a3c524Sschwarze.Xr SSL_set_shutdown 3 , 264f1a3c524Sschwarze.Xr SSL_shutdown 3 , 265f1a3c524Sschwarze.Xr SSL_write 3 2668fba1ec8Sschwarze.Sh HISTORY 2678fba1ec8Sschwarze.Fn SSL_read 26810e00d17Sschwarzeappeared in SSLeay 0.4 or earlier. 2698fba1ec8Sschwarze.Fn SSL_peek 27010e00d17Sschwarzefirst appeared in SSLeay 0.6.6. 27110e00d17SschwarzeBoth functions have been available since 2728fba1ec8Sschwarze.Ox 2.4 . 273*076fb258Sschwarze.Pp 274*076fb258Sschwarze.Fn SSL_read_ex 275*076fb258Sschwarzeand 276*076fb258Sschwarze.Fn SSL_peek_ex 277*076fb258Sschwarzefirst appeared in OpenSSL 1.1.1 and have been available since 278*076fb258Sschwarze.Ox 7.1 . 279