e97d5ba05043308a38344a0906b1108ff312e75f
[openssl.git] / doc / man3 / SSL_read.pod
1 =pod
2
3 =head1 NAME
4
5 SSL_read_ex, SSL_read - read bytes from a TLS/SSL connection
6
7 =head1 SYNOPSIS
8
9  #include <openssl/ssl.h>
10
11  int SSL_read_ex(SSL *ssl, void *buf, size_t num, size_t *read);
12  int SSL_read(SSL *ssl, void *buf, int num);
13
14  int SSL_peek_ex(SSL *ssl, void *buf, size_t num, size_t *read);
15  int SSL_peek(SSL *ssl, void *buf, int num);
16
17 =head1 DESCRIPTION
18
19 SSL_read_ex() and SSL_read() try to read B<num> bytes from the specified B<ssl>
20 into the buffer B<buf>. On success SSL_read_ex() will store the number of bytes
21 actually read in B<*read>.
22
23 SSL_peek_ex() and SSL_peek() are identical to SSL_read_ex() and SSL_read()
24 respectively except no bytes are actually removed from the underlying BIO during
25 the read, so that a subsequent call to SSL_read_ex() or SSL_read() will yield
26 at least the same bytes.
27
28 =head1 NOTES
29
30 In the paragraphs below a "read function" is defined as one of SSL_read_ex(),
31 SSL_read(), SSL_peek_ex() or SSL_peek().
32
33 If necessary, a read function will negotiate a TLS/SSL session, if not already
34 explicitly performed by L<SSL_connect(3)> or L<SSL_accept(3)>. If the
35 peer requests a re-negotiation, it will be performed transparently during
36 the read function operation. The behaviour of the read functions depends on the
37 underlying BIO.
38
39 For the transparent negotiation to succeed, the B<ssl> must have been
40 initialized to client or server mode. This is being done by calling
41 L<SSL_set_connect_state(3)> or SSL_set_accept_state() before the first
42 invocation of a read function.
43
44 The read functions work based on the SSL/TLS records. The data are received in
45 records (with a maximum record size of 16kB). Only when a record has been
46 completely received, can it be processed (decryption and check of integrity).
47 Therefore data that was not retrieved at the last read call can still be
48 buffered inside the SSL layer and will be retrieved on the next read
49 call. If B<num> is higher than the number of bytes buffered then the read
50 functions will return with the bytes buffered. If no more bytes are in the
51 buffer, the read functions will trigger the processing of the next record.
52 Only when the record has been received and processed completely will the read
53 functions return reporting success. At most the contents of the record will
54 be returned. As the size of an SSL/TLS record may exceed the maximum packet size
55 of the underlying transport (e.g. TCP), it may be necessary to read several
56 packets from the transport layer before the record is complete and the read call
57 can succeed.
58
59 If the underlying BIO is B<blocking>, a read function will only return once the
60 read operation has been finished or an error occurred, except when a
61 renegotiation takes place, in which case a SSL_ERROR_WANT_READ may occur. This
62 behaviour can be controlled with the SSL_MODE_AUTO_RETRY flag of the
63 L<SSL_CTX_set_mode(3)> call.
64
65 If the underlying BIO is B<non-blocking>, a read function will also return when
66 the underlying BIO could not satisfy the needs of the function to continue the
67 operation. In this case a call to L<SSL_get_error(3)> with the
68 return value of the read function will yield B<SSL_ERROR_WANT_READ> or
69 B<SSL_ERROR_WANT_WRITE>. As at any time a re-negotiation is possible, a
70 a read function can also cause write operations! The calling process then must
71 repeat the call after taking appropriate action to satisfy the needs of the read
72 function. The action depends on the underlying BIO. When using a non-blocking
73 socket, nothing is to be done, but select() can be used to check for the
74 required condition. When using a buffering BIO, like a BIO pair, data must be
75 written into or retrieved out of the BIO before being able to continue.
76
77 L<SSL_pending(3)> can be used to find out whether there
78 are buffered bytes available for immediate retrieval. In this case
79 the read function can be called without blocking or actually receiving
80 new data from the underlying socket.
81
82 =head1 WARNING
83
84 When a read function operation has to be repeated because L<SSL_get_error(3)>
85 returned B<SSL_ERROR_WANT_READ> or B<SSL_ERROR_WANT_WRITE>, it must be repeated
86 with the same arguments.
87
88 =head1 RETURN VALUES
89
90 SSL_read_ex() will return 1 for success or 0 for failure. In the event of a
91 failure call SSL_get_error() to find out the reason.
92
93 For SSL_read() the following return values can occur:
94
95 =over 4
96
97 =item E<gt>0
98
99 The read operation was successful; the return value is the number of
100 bytes actually read from the TLS/SSL connection.
101
102 =item Z<>0
103
104 The read operation was not successful. The reason may either be a clean
105 shutdown due to a "close notify" alert sent by the peer (in which case
106 the SSL_RECEIVED_SHUTDOWN flag in the ssl shutdown state is set
107 (see L<SSL_shutdown(3)>,
108 L<SSL_set_shutdown(3)>). It is also possible, that
109 the peer simply shut down the underlying transport and the shutdown is
110 incomplete. Call SSL_get_error() with the return value B<ret> to find out,
111 whether an error occurred or the connection was shut down cleanly
112 (SSL_ERROR_ZERO_RETURN).
113
114 =item E<lt>0
115
116 The read operation was not successful, because either an error occurred
117 or action must be taken by the calling process. Call SSL_get_error() with the
118 return value B<ret> to find out the reason.
119
120 =back
121
122 =head1 SEE ALSO
123
124 L<SSL_get_error(3)>, L<SSL_write_ex(3)>,
125 L<SSL_CTX_set_mode(3)>, L<SSL_CTX_new(3)>,
126 L<SSL_connect(3)>, L<SSL_accept(3)>
127 L<SSL_set_connect_state(3)>,
128 L<SSL_pending(3)>,
129 L<SSL_shutdown(3)>, L<SSL_set_shutdown(3)>,
130 L<ssl(3)>, L<bio(3)>
131
132 =head1 COPYRIGHT
133
134 Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
135
136 Licensed under the OpenSSL license (the "License").  You may not use
137 this file except in compliance with the License.  You can obtain a copy
138 in the file LICENSE in the source distribution or at
139 L<https://www.openssl.org/source/license.html>.
140
141 =cut