xref: /onnv-gate/usr/src/common/openssl/doc/ssl/SSL_read.pod (revision 2175:b0b2f052a486)
1*2175Sjp161948=pod
2*2175Sjp161948
3*2175Sjp161948=head1 NAME
4*2175Sjp161948
5*2175Sjp161948SSL_read - read bytes from a TLS/SSL connection.
6*2175Sjp161948
7*2175Sjp161948=head1 SYNOPSIS
8*2175Sjp161948
9*2175Sjp161948 #include <openssl/ssl.h>
10*2175Sjp161948
11*2175Sjp161948 int SSL_read(SSL *ssl, void *buf, int num);
12*2175Sjp161948
13*2175Sjp161948=head1 DESCRIPTION
14*2175Sjp161948
15*2175Sjp161948SSL_read() tries to read B<num> bytes from the specified B<ssl> into the
16*2175Sjp161948buffer B<buf>.
17*2175Sjp161948
18*2175Sjp161948=head1 NOTES
19*2175Sjp161948
20*2175Sjp161948If necessary, SSL_read() will negotiate a TLS/SSL session, if
21*2175Sjp161948not already explicitly performed by L<SSL_connect(3)|SSL_connect(3)> or
22*2175Sjp161948L<SSL_accept(3)|SSL_accept(3)>. If the
23*2175Sjp161948peer requests a re-negotiation, it will be performed transparently during
24*2175Sjp161948the SSL_read() operation. The behaviour of SSL_read() depends on the
25*2175Sjp161948underlying BIO.
26*2175Sjp161948
27*2175Sjp161948For the transparent negotiation to succeed, the B<ssl> must have been
28*2175Sjp161948initialized to client or server mode. This is being done by calling
29*2175Sjp161948L<SSL_set_connect_state(3)|SSL_set_connect_state(3)> or SSL_set_accept_state()
30*2175Sjp161948before the first call to an SSL_read() or L<SSL_write(3)|SSL_write(3)>
31*2175Sjp161948function.
32*2175Sjp161948
33*2175Sjp161948SSL_read() works based on the SSL/TLS records. The data are received in
34*2175Sjp161948records (with a maximum record size of 16kB for SSLv3/TLSv1). Only when a
35*2175Sjp161948record has been completely received, it can be processed (decryption and
36*2175Sjp161948check of integrity). Therefore data that was not retrieved at the last
37*2175Sjp161948call of SSL_read() can still be buffered inside the SSL layer and will be
38*2175Sjp161948retrieved on the next call to SSL_read(). If B<num> is higher than the
39*2175Sjp161948number of bytes buffered, SSL_read() will return with the bytes buffered.
40*2175Sjp161948If no more bytes are in the buffer, SSL_read() will trigger the processing
41*2175Sjp161948of the next record. Only when the record has been received and processed
42*2175Sjp161948completely, SSL_read() will return reporting success. At most the contents
43*2175Sjp161948of the record will be returned. As the size of an SSL/TLS record may exceed
44*2175Sjp161948the maximum packet size of the underlying transport (e.g. TCP), it may
45*2175Sjp161948be necessary to read several packets from the transport layer before the
46*2175Sjp161948record is complete and SSL_read() can succeed.
47*2175Sjp161948
48*2175Sjp161948If the underlying BIO is B<blocking>, SSL_read() will only return, once the
49*2175Sjp161948read operation has been finished or an error occurred, except when a
50*2175Sjp161948renegotiation take place, in which case a SSL_ERROR_WANT_READ may occur.
51*2175Sjp161948This behaviour can be controlled with the SSL_MODE_AUTO_RETRY flag of the
52*2175Sjp161948L<SSL_CTX_set_mode(3)|SSL_CTX_set_mode(3)> call.
53*2175Sjp161948
54*2175Sjp161948If the underlying BIO is B<non-blocking>, SSL_read() will also return
55*2175Sjp161948when the underlying BIO could not satisfy the needs of SSL_read()
56*2175Sjp161948to continue the operation. In this case a call to
57*2175Sjp161948L<SSL_get_error(3)|SSL_get_error(3)> with the
58*2175Sjp161948return value of SSL_read() will yield B<SSL_ERROR_WANT_READ> or
59*2175Sjp161948B<SSL_ERROR_WANT_WRITE>. As at any time a re-negotiation is possible, a
60*2175Sjp161948call to SSL_read() can also cause write operations! The calling process
61*2175Sjp161948then must repeat the call after taking appropriate action to satisfy the
62*2175Sjp161948needs of SSL_read(). The action depends on the underlying BIO. When using a
63*2175Sjp161948non-blocking socket, nothing is to be done, but select() can be used to check
64*2175Sjp161948for the required condition. When using a buffering BIO, like a BIO pair, data
65*2175Sjp161948must be written into or retrieved out of the BIO before being able to continue.
66*2175Sjp161948
67*2175Sjp161948=head1 WARNING
68*2175Sjp161948
69*2175Sjp161948When an SSL_read() operation has to be repeated because of
70*2175Sjp161948B<SSL_ERROR_WANT_READ> or B<SSL_ERROR_WANT_WRITE>, it must be repeated
71*2175Sjp161948with the same arguments.
72*2175Sjp161948
73*2175Sjp161948=head1 RETURN VALUES
74*2175Sjp161948
75*2175Sjp161948The following return values can occur:
76*2175Sjp161948
77*2175Sjp161948=over 4
78*2175Sjp161948
79*2175Sjp161948=item E<gt>0
80*2175Sjp161948
81*2175Sjp161948The read operation was successful; the return value is the number of
82*2175Sjp161948bytes actually read from the TLS/SSL connection.
83*2175Sjp161948
84*2175Sjp161948=item 0
85*2175Sjp161948
86*2175Sjp161948The read operation was not successful. The reason may either be a clean
87*2175Sjp161948shutdown due to a "close notify" alert sent by the peer (in which case
88*2175Sjp161948the SSL_RECEIVED_SHUTDOWN flag in the ssl shutdown state is set
89*2175Sjp161948(see L<SSL_shutdown(3)|SSL_shutdown(3)>,
90*2175Sjp161948L<SSL_set_shutdown(3)|SSL_set_shutdown(3)>). It is also possible, that
91*2175Sjp161948the peer simply shut down the underlying transport and the shutdown is
92*2175Sjp161948incomplete. Call SSL_get_error() with the return value B<ret> to find out,
93*2175Sjp161948whether an error occurred or the connection was shut down cleanly
94*2175Sjp161948(SSL_ERROR_ZERO_RETURN).
95*2175Sjp161948
96*2175Sjp161948SSLv2 (deprecated) does not support a shutdown alert protocol, so it can
97*2175Sjp161948only be detected, whether the underlying connection was closed. It cannot
98*2175Sjp161948be checked, whether the closure was initiated by the peer or by something
99*2175Sjp161948else.
100*2175Sjp161948
101*2175Sjp161948=item E<lt>0
102*2175Sjp161948
103*2175Sjp161948The read operation was not successful, because either an error occurred
104*2175Sjp161948or action must be taken by the calling process. Call SSL_get_error() with the
105*2175Sjp161948return value B<ret> to find out the reason.
106*2175Sjp161948
107*2175Sjp161948=back
108*2175Sjp161948
109*2175Sjp161948=head1 SEE ALSO
110*2175Sjp161948
111*2175Sjp161948L<SSL_get_error(3)|SSL_get_error(3)>, L<SSL_write(3)|SSL_write(3)>,
112*2175Sjp161948L<SSL_CTX_set_mode(3)|SSL_CTX_set_mode(3)>, L<SSL_CTX_new(3)|SSL_CTX_new(3)>,
113*2175Sjp161948L<SSL_connect(3)|SSL_connect(3)>, L<SSL_accept(3)|SSL_accept(3)>
114*2175Sjp161948L<SSL_set_connect_state(3)|SSL_set_connect_state(3)>,
115*2175Sjp161948L<SSL_shutdown(3)|SSL_shutdown(3)>, L<SSL_set_shutdown(3)|SSL_set_shutdown(3)>,
116*2175Sjp161948L<ssl(3)|ssl(3)>, L<bio(3)|bio(3)>
117*2175Sjp161948
118*2175Sjp161948=cut
119