← Back to DAGDA | Support | Source: https://github.com/openssl/openssl/commit/beacb0f0c1ae7b0542fe053b95307f515b578eb7
Skip to content

Commit beacb0f

Browse files
committed
Make SSL_read and SSL_write return the old behaviour and document it.
This reverts commit 4880672. Fixes: #1903 Reviewed-by: Matt Caswell <matt@openssl.org> GH: #1931
1 parent 52fe14e commit beacb0f

6 files changed

Lines changed: 83 additions & 74 deletions

File tree

‎doc/man3/SSL_get_error.pod‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ if and only if B<ret E<gt> 0>.
3838

3939
=item SSL_ERROR_ZERO_RETURN
4040

41-
The TLS/SSL connection has been closed. If the protocol version is SSL 3.0
42-
or TLS 1.0, this result code is returned only if a closure
43-
alert has occurred in the protocol, i.e. if the connection has been
44-
closed cleanly. Note that in this case B<SSL_ERROR_ZERO_RETURN>
45-
does not necessarily indicate that the underlying transport
46-
has been closed.
41+
The TLS/SSL connection has been closed.
42+
If the protocol version is SSL 3.0 or higher, this result code is returned only
43+
if a closure alert has occurred in the protocol, i.e. if the connection has been
44+
closed cleanly.
45+
Note that in this case B<SSL_ERROR_ZERO_RETURN> does not necessarily
46+
indicate that the underlying transport has been closed.
4747

4848
=item SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE
4949

@@ -112,12 +112,9 @@ thread has completed.
112112

113113
=item SSL_ERROR_SYSCALL
114114

115-
Some I/O error occurred. The OpenSSL error queue may contain more
116-
information on the error. If the error queue is empty
117-
(i.e. ERR_get_error() returns 0), B<ret> can be used to find out more
118-
about the error: If B<ret == 0>, an EOF was observed that violates
119-
the protocol. If B<ret == -1>, the underlying B<BIO> reported an
120-
I/O error (for socket I/O on Unix systems, consult B<errno> for details).
115+
Some non-recoverable I/O error occurred.
116+
The OpenSSL error queue may contain more information on the error.
117+
For socket I/O on Unix systems, consult B<errno> for details.
121118

122119
=item SSL_ERROR_SSL
123120

‎doc/man3/SSL_read.pod‎

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ SSL_read_ex, SSL_read, SSL_peek_ex, SSL_peek
99

1010
#include <openssl/ssl.h>
1111

12-
int SSL_read_ex(SSL *ssl, void *buf, size_t num, size_t *read);
12+
int SSL_read_ex(SSL *ssl, void *buf, size_t num, size_t *readbytes);
1313
int SSL_read(SSL *ssl, void *buf, int num);
1414

15-
int SSL_peek_ex(SSL *ssl, void *buf, size_t num, size_t *read);
15+
int SSL_peek_ex(SSL *ssl, void *buf, size_t num, size_t *readbytes);
1616
int SSL_peek(SSL *ssl, void *buf, int num);
1717

1818
=head1 DESCRIPTION
1919

2020
SSL_read_ex() and SSL_read() try to read B<num> bytes from the specified B<ssl>
2121
into the buffer B<buf>. On success SSL_read_ex() will store the number of bytes
22-
actually read in B<*read>.
22+
actually read in B<*readbytes>.
2323

2424
SSL_peek_ex() and SSL_peek() are identical to SSL_read_ex() and SSL_read()
2525
respectively except no bytes are actually removed from the underlying BIO during
@@ -90,38 +90,32 @@ with the same arguments.
9090

9191
SSL_read_ex() and SSL_peek_ex() will return 1 for success or 0 for failure.
9292
Success means that 1 or more application data bytes have been read from the SSL
93-
connection. Failure means that no bytes could be read from the SSL connection.
93+
connection.
94+
Failure means that no bytes could be read from the SSL connection.
9495
Failures can be retryable (e.g. we are waiting for more bytes to
95-
be delivered by the network) or non-retryable (e.g. a fatal network error). In
96-
the event of a failure call L<SSL_get_error(3)> to find out the reason which
96+
be delivered by the network) or non-retryable (e.g. a fatal network error).
97+
In the event of a failure call L<SSL_get_error(3)> to find out the reason which
9798
indicates whether the call is retryable or not.
9899

99100
For SSL_read() and SSL_peek() the following return values can occur:
100101

101102
=over 4
102103

103-
=item E<gt>0
104+
=item E<gt> 0
104105

105-
The read operation was successful; the return value is the number of
106-
bytes actually read from the TLS/SSL connection.
106+
The read operation was successful.
107+
The return value is the number of bytes actually read from the TLS/SSL
108+
connection.
107109

108-
=item Z<>0
110+
=item Z<><= 0
109111

110-
The read operation was not successful. The reason may either be a clean
111-
shutdown due to a "close notify" alert sent by the peer (in which case
112-
the SSL_RECEIVED_SHUTDOWN flag in the ssl shutdown state is set
113-
(see L<SSL_shutdown(3)>,
114-
L<SSL_set_shutdown(3)>). It is also possible, that
115-
the peer simply shut down the underlying transport and the shutdown is
116-
incomplete. Call SSL_get_error() with the return value B<ret> to find out,
117-
whether an error occurred or the connection was shut down cleanly
118-
(SSL_ERROR_ZERO_RETURN).
112+
The read operation was not successful, because either the connection was closed,
113+
an error occurred or action must be taken by the calling process.
114+
Call L<SSL_get_error(3)> with the return value B<ret> to find out the reason.
119115

120-
=item E<lt>0
121-
122-
The read operation was not successful, because either an error occurred
123-
or action must be taken by the calling process. Call SSL_get_error() with the
124-
return value B<ret> to find out the reason.
116+
Old documentation indicated a difference between 0 and -1, and that -1 was
117+
retryable.
118+
You should instead call SSL_get_error() to find out if it's retryable.
125119

126120
=back
127121

‎doc/man3/SSL_write.pod‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,20 @@ For SSL_write() the following return values can occur:
8686

8787
=over 4
8888

89-
=item E<gt>0
89+
=item E<gt> 0
9090

9191
The write operation was successful, the return value is the number of
9292
bytes actually written to the TLS/SSL connection.
9393

94-
=item Z<>0
94+
=item Z<><= 0
9595

96-
The write operation was not successful. Probably the underlying connection
97-
was closed. Call SSL_get_error() with the return value B<ret> to find out,
98-
whether an error occurred or the connection was shut down cleanly
99-
(SSL_ERROR_ZERO_RETURN).
96+
The write operation was not successful, because either the connection was
97+
closed, an error occurred or action must be taken by the calling process.
98+
Call SSL_get_error() with the return value B<ret> to find out the reason.
10099

101-
=item E<lt>0
102-
103-
The write operation was not successful, because either an error occurred
104-
or action must be taken by the calling process. Call SSL_get_error() with the
105-
return value B<ret> to find out the reason.
100+
Old documentation indicated a difference between 0 and -1, and that -1 was
101+
retryable.
102+
You should instead call SSL_get_error() to find out if it's retryable.
106103

107104
=back
108105

‎include/openssl/ssl.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,9 +1580,9 @@ __owur int SSL_get_changed_async_fds(SSL *s, OSSL_ASYNC_FD *addfd,
15801580
__owur int SSL_accept(SSL *ssl);
15811581
__owur int SSL_connect(SSL *ssl);
15821582
__owur int SSL_read(SSL *ssl, void *buf, int num);
1583-
__owur int SSL_read_ex(SSL *ssl, void *buf, size_t num, size_t *read);
1583+
__owur int SSL_read_ex(SSL *ssl, void *buf, size_t num, size_t *readbytes);
15841584
__owur int SSL_peek(SSL *ssl, void *buf, int num);
1585-
__owur int SSL_peek_ex(SSL *ssl, void *buf, size_t num, size_t *read);
1585+
__owur int SSL_peek_ex(SSL *ssl, void *buf, size_t num, size_t *readbytes);
15861586
__owur int SSL_write(SSL *ssl, const void *buf, int num);
15871587
__owur int SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *written);
15881588
long SSL_ctrl(SSL *ssl, int cmd, long larg, void *parg);

‎ssl/record/rec_layer_s3.c‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,7 @@ const char *SSL_rstate_string(const SSL *s)
178178
}
179179

180180
/*
181-
* Return values are as per SSL_read(), i.e.
182-
* 1 Success
183-
* 0 Failure (not retryable)
184-
* <0 Failure (may be retryable)
181+
* Return values are as per SSL_read()
185182
*/
186183
int ssl3_read_n(SSL *s, size_t n, size_t max, int extend, int clearold,
187184
size_t *readbytes)
@@ -319,7 +316,7 @@ int ssl3_read_n(SSL *s, size_t n, size_t max, int extend, int clearold,
319316
if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s))
320317
if (len + left == 0)
321318
ssl3_release_read_buffer(s);
322-
return -1;
319+
return ret;
323320
}
324321
left += bioread;
325322
/*
@@ -897,10 +894,7 @@ int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
897894

898895
/* if s->s3->wbuf.left != 0, we need to call this
899896
*
900-
* Return values are as per SSL_read(), i.e.
901-
* 1 Success
902-
* 0 Failure (not retryable)
903-
* <0 Failure (may be retryable)
897+
* Return values are as per SSL_write()
904898
*/
905899
int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,
906900
size_t *written)
@@ -955,7 +949,7 @@ int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,
955949
*/
956950
SSL3_BUFFER_set_left(&wb[currbuf], 0);
957951
}
958-
return -1;
952+
return (i);
959953
}
960954
SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);
961955
SSL3_BUFFER_sub_left(&wb[currbuf], tmpwrit);

‎test/asynciotest.c‎

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -297,32 +297,59 @@ int main(int argc, char *argv[])
297297
* we hit at least one async event in both reading and writing
298298
*/
299299
for (j = 0; j < 2; j++) {
300+
int len;
301+
300302
/*
301303
* Write some test data. It should never take more than 2 attempts
302-
* (the first one might be a retryable fail). A zero return from
303-
* SSL_write() is a non-retryable failure, so fail immediately if
304-
* we get that.
304+
* (the first one might be a retryable fail).
305305
*/
306-
for (ret = -1, i = 0; ret < 0 && i < 2 * sizeof(testdata); i++)
307-
ret = SSL_write(clientssl, testdata, sizeof(testdata));
308-
if (ret <= 0) {
309-
printf("Test %d failed: Failed to write app data\n", test);
306+
for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < 2;
307+
i++) {
308+
ret = SSL_write(clientssl, testdata + len,
309+
sizeof(testdata) - len);
310+
if (ret > 0) {
311+
len += ret;
312+
} else {
313+
int ssl_error = SSL_get_error(clientssl, ret);
314+
315+
if (ssl_error == SSL_ERROR_SYSCALL ||
316+
ssl_error == SSL_ERROR_SSL) {
317+
printf("Test %d failed: Failed to write app data\n", test);
318+
err = -1;
319+
goto end;
320+
}
321+
}
322+
}
323+
if (len != sizeof(testdata)) {
324+
err = -1;
325+
printf("Test %d failed: Failed to write all app data\n", test);
310326
goto end;
311327
}
312328
/*
313329
* Now read the test data. It may take more attemps here because
314330
* it could fail once for each byte read, including all overhead
315-
* bytes from the record header/padding etc. Fail immediately if we
316-
* get a zero return from SSL_read().
331+
* bytes from the record header/padding etc.
317332
*/
318-
for (ret = -1, i = 0; ret < 0 && i < MAX_ATTEMPTS; i++)
319-
ret = SSL_read(serverssl, buf, sizeof(buf));
320-
if (ret <= 0) {
321-
printf("Test %d failed: Failed to read app data\n", test);
322-
goto end;
333+
for (ret = -1, i = 0, len = 0; len != sizeof(testdata) &&
334+
i < MAX_ATTEMPTS; i++)
335+
{
336+
ret = SSL_read(serverssl, buf + len, sizeof(buf) - len);
337+
if (ret > 0) {
338+
len += ret;
339+
} else {
340+
int ssl_error = SSL_get_error(serverssl, ret);
341+
342+
if (ssl_error == SSL_ERROR_SYSCALL ||
343+
ssl_error == SSL_ERROR_SSL) {
344+
printf("Test %d failed: Failed to read app data\n", test);
345+
err = -1;
346+
goto end;
347+
}
348+
}
323349
}
324-
if (ret != sizeof(testdata)
350+
if (len != sizeof(testdata)
325351
|| memcmp(buf, testdata, sizeof(testdata)) != 0) {
352+
err = -1;
326353
printf("Test %d failed: Unexpected app data received\n", test);
327354
goto end;
328355
}

0 commit comments

Comments
 (0)