| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "SSL_READ 3SSL" |
| 58 |
.TH SSL_READ 3SSL 2026-04-07 3.6.2 OpenSSL |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
SSL_read_ex, SSL_read, SSL_peek_ex, SSL_peek |
| 65 |
\&\- read bytes from a TLS/SSL connection |
| 66 |
.SH SYNOPSIS |
| 67 |
.IX Header "SYNOPSIS" |
| 68 |
.Vb 1 |
| 69 |
\& #include <openssl/ssl.h> |
| 70 |
\& |
| 71 |
\& int SSL_read_ex(SSL *ssl, void *buf, size_t num, size_t *readbytes); |
| 72 |
\& int SSL_read(SSL *ssl, void *buf, int num); |
| 73 |
\& |
| 74 |
\& int SSL_peek_ex(SSL *ssl, void *buf, size_t num, size_t *readbytes); |
| 75 |
\& int SSL_peek(SSL *ssl, void *buf, int num); |
| 76 |
.Ve |
| 77 |
.SH DESCRIPTION |
| 78 |
.IX Header "DESCRIPTION" |
| 79 |
\&\fBSSL_read_ex()\fR and \fBSSL_read()\fR try to read \fBnum\fR bytes from the specified \fBssl\fR |
| 80 |
into the buffer \fBbuf\fR. On success \fBSSL_read_ex()\fR will store the number of bytes |
| 81 |
actually read in \fB*readbytes\fR. |
| 82 |
.PP |
| 83 |
\&\fBSSL_peek_ex()\fR and \fBSSL_peek()\fR are identical to \fBSSL_read_ex()\fR and \fBSSL_read()\fR |
| 84 |
respectively except no bytes are actually removed from the underlying BIO during |
| 85 |
the read, so that a subsequent call to \fBSSL_read_ex()\fR or \fBSSL_read()\fR will yield |
| 86 |
at least the same bytes. |
| 87 |
.SH NOTES |
| 88 |
.IX Header "NOTES" |
| 89 |
In the paragraphs below a "read function" is defined as one of \fBSSL_read_ex()\fR, |
| 90 |
\&\fBSSL_read()\fR, \fBSSL_peek_ex()\fR or \fBSSL_peek()\fR. |
| 91 |
.PP |
| 92 |
If necessary, a read function will negotiate a TLS/SSL session, if not already |
| 93 |
explicitly performed by \fBSSL_connect\fR\|(3) or \fBSSL_accept\fR\|(3). If the |
| 94 |
peer requests a re-negotiation, it will be performed transparently during |
| 95 |
the read function operation. The behaviour of the read functions depends on the |
| 96 |
underlying BIO. |
| 97 |
.PP |
| 98 |
For the transparent negotiation to succeed, the \fBssl\fR must have been |
| 99 |
initialized to client or server mode. This is being done by calling |
| 100 |
\&\fBSSL_set_connect_state\fR\|(3) or \fBSSL_set_accept_state()\fR before the first |
| 101 |
invocation of a read function. |
| 102 |
.PP |
| 103 |
The read functions work based on the SSL/TLS records. The data are received in |
| 104 |
records (with a maximum record size of 16kB). Only when a record has been |
| 105 |
completely received, can it be processed (decryption and check of integrity). |
| 106 |
Therefore, data that was not retrieved at the last read call can still be |
| 107 |
buffered inside the SSL layer and will be retrieved on the next read |
| 108 |
call. If \fBnum\fR is higher than the number of bytes buffered then the read |
| 109 |
functions will return with the bytes buffered. If no more bytes are in the |
| 110 |
buffer, the read functions will trigger the processing of the next record. |
| 111 |
Only when the record has been received and processed completely will the read |
| 112 |
functions return reporting success. At most the contents of one record will |
| 113 |
be returned. As the size of an SSL/TLS record may exceed the maximum packet size |
| 114 |
of the underlying transport (e.g. TCP), it may be necessary to read several |
| 115 |
packets from the transport layer before the record is complete and the read call |
| 116 |
can succeed. |
| 117 |
.PP |
| 118 |
If \fBSSL_MODE_AUTO_RETRY\fR has been switched off and a non-application data |
| 119 |
record has been processed, the read function can return and set the error to |
| 120 |
\&\fBSSL_ERROR_WANT_READ\fR. |
| 121 |
In this case there might still be unprocessed data available in the \fBBIO\fR. |
| 122 |
If read ahead was set using \fBSSL_CTX_set_read_ahead\fR\|(3), there might also still |
| 123 |
be unprocessed data available in the \fBSSL\fR. |
| 124 |
This behaviour can be controlled using the \fBSSL_CTX_set_mode\fR\|(3) call. |
| 125 |
.PP |
| 126 |
If the underlying BIO is \fBblocking\fR, a read function will only return once the |
| 127 |
read operation has been finished or an error occurred, except when a |
| 128 |
non-application data record has been processed and \fBSSL_MODE_AUTO_RETRY\fR is |
| 129 |
not set. |
| 130 |
Note that if \fBSSL_MODE_AUTO_RETRY\fR is set and only non-application data is |
| 131 |
available the call will hang. |
| 132 |
.PP |
| 133 |
If the underlying BIO is \fBnonblocking\fR, a read function will also return when |
| 134 |
the underlying BIO could not satisfy the needs of the function to continue the |
| 135 |
operation. |
| 136 |
In this case a call to \fBSSL_get_error\fR\|(3) with the |
| 137 |
return value of the read function will yield \fBSSL_ERROR_WANT_READ\fR or |
| 138 |
\&\fBSSL_ERROR_WANT_WRITE\fR. |
| 139 |
As at any time it's possible that non-application data needs to be sent, |
| 140 |
a read function can also cause write operations. |
| 141 |
The calling process then must repeat the call after taking appropriate action |
| 142 |
to satisfy the needs of the read function. |
| 143 |
The action depends on the underlying BIO. |
| 144 |
When using a nonblocking socket, nothing is to be done, but \fBselect()\fR can be |
| 145 |
used to check for the required condition. |
| 146 |
When using a buffering BIO, like a BIO pair, data must be written into or |
| 147 |
retrieved out of the BIO before being able to continue. |
| 148 |
.PP |
| 149 |
\&\fBSSL_pending\fR\|(3) can be used to find out whether there |
| 150 |
are buffered bytes available for immediate retrieval. |
| 151 |
In this case the read function can be called without blocking or actually |
| 152 |
receiving new data from the underlying socket. |
| 153 |
.PP |
| 154 |
When used with a QUIC SSL object, calling an I/O function such as \fBSSL_read()\fR |
| 155 |
allows internal network event processing to be performed. It is important that |
| 156 |
this processing is performed regularly. If an application is not using thread |
| 157 |
assisted mode, an application should ensure that an I/O function such as |
| 158 |
\&\fBSSL_read()\fR is called regularly, or alternatively ensure that \fBSSL_handle_events()\fR |
| 159 |
is called regularly. See \fBopenssl\-quic\fR\|(7) and \fBSSL_handle_events\fR\|(3) for more |
| 160 |
information. |
| 161 |
.SH "RETURN VALUES" |
| 162 |
.IX Header "RETURN VALUES" |
| 163 |
\&\fBSSL_read_ex()\fR and \fBSSL_peek_ex()\fR will return 1 for success or 0 for failure. |
| 164 |
Success means that 1 or more application data bytes have been read from the SSL |
| 165 |
connection. |
| 166 |
Failure means that no bytes could be read from the SSL connection. |
| 167 |
Failures can be retryable (e.g. we are waiting for more bytes to |
| 168 |
be delivered by the network) or non-retryable (e.g. a fatal network error). |
| 169 |
In the event of a failure call \fBSSL_get_error\fR\|(3) to find out the reason which |
| 170 |
indicates whether the call is retryable or not. |
| 171 |
.PP |
| 172 |
For \fBSSL_read()\fR and \fBSSL_peek()\fR the following return values can occur: |
| 173 |
.IP "> 0" 4 |
| 174 |
.IX Item "> 0" |
| 175 |
The read operation was successful. |
| 176 |
The return value is the number of bytes actually read from the TLS/SSL |
| 177 |
connection. |
| 178 |
.IP "<= 0" 4 |
| 179 |
.IX Item "<= 0" |
| 180 |
The read operation was not successful, because either the connection was closed, |
| 181 |
an error occurred or action must be taken by the calling process. |
| 182 |
Call \fBSSL_get_error\fR\|(3) with the return value \fBret\fR to find out the reason. |
| 183 |
.Sp |
| 184 |
Old documentation indicated a difference between 0 and \-1, and that \-1 was |
| 185 |
retryable. |
| 186 |
You should instead call \fBSSL_get_error()\fR to find out if it's retryable. |
| 187 |
.SH "SEE ALSO" |
| 188 |
.IX Header "SEE ALSO" |
| 189 |
\&\fBSSL_get_error\fR\|(3), \fBSSL_write_ex\fR\|(3), |
| 190 |
\&\fBSSL_CTX_set_mode\fR\|(3), \fBSSL_CTX_new\fR\|(3), |
| 191 |
\&\fBSSL_connect\fR\|(3), \fBSSL_accept\fR\|(3) |
| 192 |
\&\fBSSL_set_connect_state\fR\|(3), |
| 193 |
\&\fBSSL_pending\fR\|(3), |
| 194 |
\&\fBSSL_shutdown\fR\|(3), \fBSSL_set_shutdown\fR\|(3), |
| 195 |
\&\fBssl\fR\|(7), \fBbio\fR\|(7) |
| 196 |
.SH HISTORY |
| 197 |
.IX Header "HISTORY" |
| 198 |
The \fBSSL_read_ex()\fR and \fBSSL_peek_ex()\fR functions were added in OpenSSL 1.1.1. |
| 199 |
.SH COPYRIGHT |
| 200 |
.IX Header "COPYRIGHT" |
| 201 |
Copyright 2000\-2023 The OpenSSL Project Authors. All Rights Reserved. |
| 202 |
.PP |
| 203 |
Licensed under the Apache License 2.0 (the "License"). You may not use |
| 204 |
this file except in compliance with the License. You can obtain a copy |
| 205 |
in the file LICENSE in the source distribution or at |
| 206 |
<https://www.openssl.org/source/license.html>. |