Blame doc/man3/SSL_read.pod

Packit c4476c
=pod
Packit c4476c
Packit c4476c
=head1 NAME
Packit c4476c
Packit c4476c
SSL_read_ex, SSL_read, SSL_peek_ex, SSL_peek
Packit c4476c
- read bytes from a TLS/SSL connection
Packit c4476c
Packit c4476c
=head1 SYNOPSIS
Packit c4476c
Packit c4476c
 #include <openssl/ssl.h>
Packit c4476c
Packit c4476c
 int SSL_read_ex(SSL *ssl, void *buf, size_t num, size_t *readbytes);
Packit c4476c
 int SSL_read(SSL *ssl, void *buf, int num);
Packit c4476c
Packit c4476c
 int SSL_peek_ex(SSL *ssl, void *buf, size_t num, size_t *readbytes);
Packit c4476c
 int SSL_peek(SSL *ssl, void *buf, int num);
Packit c4476c
Packit c4476c
=head1 DESCRIPTION
Packit c4476c
Packit c4476c
SSL_read_ex() and SSL_read() try to read B<num> bytes from the specified B<ssl>
Packit c4476c
into the buffer B<buf>. On success SSL_read_ex() will store the number of bytes
Packit c4476c
actually read in B<*readbytes>.
Packit c4476c
Packit c4476c
SSL_peek_ex() and SSL_peek() are identical to SSL_read_ex() and SSL_read()
Packit c4476c
respectively except no bytes are actually removed from the underlying BIO during
Packit c4476c
the read, so that a subsequent call to SSL_read_ex() or SSL_read() will yield
Packit c4476c
at least the same bytes.
Packit c4476c
Packit c4476c
=head1 NOTES
Packit c4476c
Packit c4476c
In the paragraphs below a "read function" is defined as one of SSL_read_ex(),
Packit c4476c
SSL_read(), SSL_peek_ex() or SSL_peek().
Packit c4476c
Packit c4476c
If necessary, a read function will negotiate a TLS/SSL session, if not already
Packit c4476c
explicitly performed by L<SSL_connect(3)> or L<SSL_accept(3)>. If the
Packit c4476c
peer requests a re-negotiation, it will be performed transparently during
Packit c4476c
the read function operation. The behaviour of the read functions depends on the
Packit c4476c
underlying BIO.
Packit c4476c
Packit c4476c
For the transparent negotiation to succeed, the B<ssl> must have been
Packit c4476c
initialized to client or server mode. This is being done by calling
Packit c4476c
L<SSL_set_connect_state(3)> or SSL_set_accept_state() before the first
Packit c4476c
invocation of a read function.
Packit c4476c
Packit c4476c
The read functions work based on the SSL/TLS records. The data are received in
Packit c4476c
records (with a maximum record size of 16kB). Only when a record has been
Packit c4476c
completely received, can it be processed (decryption and check of integrity).
Packit c4476c
Therefore data that was not retrieved at the last read call can still be
Packit c4476c
buffered inside the SSL layer and will be retrieved on the next read
Packit c4476c
call. If B<num> is higher than the number of bytes buffered then the read
Packit c4476c
functions will return with the bytes buffered. If no more bytes are in the
Packit c4476c
buffer, the read functions will trigger the processing of the next record.
Packit c4476c
Only when the record has been received and processed completely will the read
Packit c4476c
functions return reporting success. At most the contents of one record will
Packit c4476c
be returned. As the size of an SSL/TLS record may exceed the maximum packet size
Packit c4476c
of the underlying transport (e.g. TCP), it may be necessary to read several
Packit c4476c
packets from the transport layer before the record is complete and the read call
Packit c4476c
can succeed.
Packit c4476c
Packit c4476c
If B<SSL_MODE_AUTO_RETRY> has been switched off and a non-application data
Packit c4476c
record has been processed, the read function can return and set the error to
Packit c4476c
B<SSL_ERROR_WANT_READ>.
Packit c4476c
In this case there might still be unprocessed data available in the B<BIO>.
Packit c4476c
If read ahead was set using L<SSL_CTX_set_read_ahead(3)>, there might also still
Packit c4476c
be unprocessed data available in the B<SSL>.
Packit c4476c
This behaviour can be controlled using the L<SSL_CTX_set_mode(3)> call.
Packit c4476c
Packit c4476c
If the underlying BIO is B<blocking>, a read function will only return once the
Packit c4476c
read operation has been finished or an error occurred, except when a
Packit c4476c
non-application data record has been processed and B<SSL_MODE_AUTO_RETRY> is
Packit c4476c
not set.
Packit c4476c
Note that if B<SSL_MODE_AUTO_RETRY> is set and only non-application data is
Packit c4476c
available the call will hang.
Packit c4476c
Packit c4476c
If the underlying BIO is B<non-blocking>, a read function will also return when
Packit c4476c
the underlying BIO could not satisfy the needs of the function to continue the
Packit c4476c
operation.
Packit c4476c
In this case a call to L<SSL_get_error(3)> with the
Packit c4476c
return value of the read function will yield B<SSL_ERROR_WANT_READ> or
Packit c4476c
B<SSL_ERROR_WANT_WRITE>.
Packit c4476c
As at any time it's possible that non-application data needs to be sent,
Packit c4476c
a read function can also cause write operations.
Packit c4476c
The calling process then must repeat the call after taking appropriate action
Packit c4476c
to satisfy the needs of the read function.
Packit c4476c
The action depends on the underlying BIO.
Packit c4476c
When using a non-blocking socket, nothing is to be done, but select() can be
Packit c4476c
used to check for the required condition.
Packit c4476c
When using a buffering BIO, like a BIO pair, data must be written into or
Packit c4476c
retrieved out of the BIO before being able to continue.
Packit c4476c
Packit c4476c
L<SSL_pending(3)> can be used to find out whether there
Packit c4476c
are buffered bytes available for immediate retrieval.
Packit c4476c
In this case the read function can be called without blocking or actually
Packit c4476c
receiving new data from the underlying socket.
Packit c4476c
Packit c4476c
=head1 RETURN VALUES
Packit c4476c
Packit c4476c
SSL_read_ex() and SSL_peek_ex() will return 1 for success or 0 for failure.
Packit c4476c
Success means that 1 or more application data bytes have been read from the SSL
Packit c4476c
connection.
Packit c4476c
Failure means that no bytes could be read from the SSL connection.
Packit c4476c
Failures can be retryable (e.g. we are waiting for more bytes to
Packit c4476c
be delivered by the network) or non-retryable (e.g. a fatal network error).
Packit c4476c
In the event of a failure call L<SSL_get_error(3)> to find out the reason which
Packit c4476c
indicates whether the call is retryable or not.
Packit c4476c
Packit c4476c
For SSL_read() and SSL_peek() the following return values can occur:
Packit c4476c
Packit c4476c
=over 4
Packit c4476c
Packit c4476c
=item E<gt> 0
Packit c4476c
Packit c4476c
The read operation was successful.
Packit c4476c
The return value is the number of bytes actually read from the TLS/SSL
Packit c4476c
connection.
Packit c4476c
Packit c4476c
=item Z<><= 0
Packit c4476c
Packit c4476c
The read operation was not successful, because either the connection was closed,
Packit c4476c
an error occurred or action must be taken by the calling process.
Packit c4476c
Call L<SSL_get_error(3)> with the return value B<ret> to find out the reason.
Packit c4476c
Packit c4476c
Old documentation indicated a difference between 0 and -1, and that -1 was
Packit c4476c
retryable.
Packit c4476c
You should instead call SSL_get_error() to find out if it's retryable.
Packit c4476c
Packit c4476c
=back
Packit c4476c
Packit c4476c
=head1 SEE ALSO
Packit c4476c
Packit c4476c
L<SSL_get_error(3)>, L<SSL_write_ex(3)>,
Packit c4476c
L<SSL_CTX_set_mode(3)>, L<SSL_CTX_new(3)>,
Packit c4476c
L<SSL_connect(3)>, L<SSL_accept(3)>
Packit c4476c
L<SSL_set_connect_state(3)>,
Packit c4476c
L<SSL_pending(3)>,
Packit c4476c
L<SSL_shutdown(3)>, L<SSL_set_shutdown(3)>,
Packit c4476c
L<ssl(7)>, L<bio(7)>
Packit c4476c
Packit c4476c
=head1 HISTORY
Packit c4476c
Packit c4476c
The SSL_read_ex() and SSL_peek_ex() functions were added in OpenSSL 1.1.1.
Packit c4476c
Packit c4476c
=head1 COPYRIGHT
Packit c4476c
Packit c4476c
Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
Packit c4476c
Packit c4476c
Licensed under the OpenSSL license (the "License").  You may not use
Packit c4476c
this file except in compliance with the License.  You can obtain a copy
Packit c4476c
in the file LICENSE in the source distribution or at
Packit c4476c
L<https://www.openssl.org/source/license.html>.
Packit c4476c
Packit c4476c
=cut