Skip to content

Commit 8051ab2

Browse files
committed
Convert SSL BIO to use SSL_write_ex().
We also modify the SSL_get_error() function to handle the fact that with SSL_write_ex() the error return is 0 not -1, and fix some bugs in the SSL BIO reading. Reviewed-by: Rich Salz <rsalz@openssl.org>
1 parent 8b0e934 commit 8051ab2

2 files changed

Lines changed: 61 additions & 72 deletions

File tree

‎ssl/bio_ssl.c‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ typedef struct bio_ssl_st {
2828
/* re-negotiate every time the total number of bytes is this size */
2929
int num_renegotiates;
3030
unsigned long renegotiate_count;
31-
unsigned long byte_count;
31+
size_t byte_count;
3232
unsigned long renegotiate_timeout;
3333
unsigned long last_time;
3434
} BIO_SSL;
@@ -112,7 +112,7 @@ static int ssl_read(BIO *b, char *buf, size_t size, size_t *readbytes)
112112

113113
switch (SSL_get_error(ssl, ret)) {
114114
case SSL_ERROR_NONE:
115-
if (ret <= 0)
115+
if (*readbytes == 0)
116116
break;
117117
if (sb->renegotiate_count > 0) {
118118
sb->byte_count += *readbytes;
@@ -179,17 +179,14 @@ static int ssl_write(BIO *b, const char *buf, size_t size, size_t *written)
179179

180180
BIO_clear_retry_flags(b);
181181

182-
if (size > INT_MAX)
183-
size = INT_MAX;
184-
185-
ret = SSL_write(ssl, buf, size);
182+
ret = SSL_write_ex(ssl, buf, size, written);
186183

187184
switch (SSL_get_error(ssl, ret)) {
188185
case SSL_ERROR_NONE:
189-
if (ret <= 0)
186+
if (*written == 0)
190187
break;
191188
if (bs->renegotiate_count > 0) {
192-
bs->byte_count += ret;
189+
bs->byte_count += *written;
193190
if (bs->byte_count > bs->renegotiate_count) {
194191
bs->byte_count = 0;
195192
bs->num_renegotiates++;
@@ -229,11 +226,6 @@ static int ssl_write(BIO *b, const char *buf, size_t size, size_t *written)
229226

230227
BIO_set_retry_reason(b, retry_reason);
231228

232-
if (ret > 0) {
233-
*written = ret;
234-
ret = 1;
235-
}
236-
237229
return ret;
238230
}
239231

‎ssl/ssl_lib.c‎

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3002,72 +3002,69 @@ int SSL_get_error(const SSL *s, int i)
30023002
return (SSL_ERROR_SSL);
30033003
}
30043004

3005-
if (i < 0) {
3006-
if (SSL_want_read(s)) {
3007-
bio = SSL_get_rbio(s);
3008-
if (BIO_should_read(bio))
3009-
return (SSL_ERROR_WANT_READ);
3010-
else if (BIO_should_write(bio))
3011-
/*
3012-
* This one doesn't make too much sense ... We never try to write
3013-
* to the rbio, and an application program where rbio and wbio
3014-
* are separate couldn't even know what it should wait for.
3015-
* However if we ever set s->rwstate incorrectly (so that we have
3016-
* SSL_want_read(s) instead of SSL_want_write(s)) and rbio and
3017-
* wbio *are* the same, this test works around that bug; so it
3018-
* might be safer to keep it.
3019-
*/
3020-
return (SSL_ERROR_WANT_WRITE);
3021-
else if (BIO_should_io_special(bio)) {
3022-
reason = BIO_get_retry_reason(bio);
3023-
if (reason == BIO_RR_CONNECT)
3024-
return (SSL_ERROR_WANT_CONNECT);
3025-
else if (reason == BIO_RR_ACCEPT)
3026-
return (SSL_ERROR_WANT_ACCEPT);
3027-
else
3028-
return (SSL_ERROR_SYSCALL); /* unknown */
3029-
}
3005+
if (SSL_want_read(s)) {
3006+
bio = SSL_get_rbio(s);
3007+
if (BIO_should_read(bio))
3008+
return (SSL_ERROR_WANT_READ);
3009+
else if (BIO_should_write(bio))
3010+
/*
3011+
* This one doesn't make too much sense ... We never try to write
3012+
* to the rbio, and an application program where rbio and wbio
3013+
* are separate couldn't even know what it should wait for.
3014+
* However if we ever set s->rwstate incorrectly (so that we have
3015+
* SSL_want_read(s) instead of SSL_want_write(s)) and rbio and
3016+
* wbio *are* the same, this test works around that bug; so it
3017+
* might be safer to keep it.
3018+
*/
3019+
return (SSL_ERROR_WANT_WRITE);
3020+
else if (BIO_should_io_special(bio)) {
3021+
reason = BIO_get_retry_reason(bio);
3022+
if (reason == BIO_RR_CONNECT)
3023+
return (SSL_ERROR_WANT_CONNECT);
3024+
else if (reason == BIO_RR_ACCEPT)
3025+
return (SSL_ERROR_WANT_ACCEPT);
3026+
else
3027+
return (SSL_ERROR_SYSCALL); /* unknown */
30303028
}
3029+
}
30313030

3032-
if (SSL_want_write(s)) {
3031+
if (SSL_want_write(s)) {
3032+
/*
3033+
* Access wbio directly - in order to use the buffered bio if
3034+
* present
3035+
*/
3036+
bio = s->wbio;
3037+
if (BIO_should_write(bio))
3038+
return (SSL_ERROR_WANT_WRITE);
3039+
else if (BIO_should_read(bio))
30333040
/*
3034-
* Access wbio directly - in order to use the buffered bio if
3035-
* present
3041+
* See above (SSL_want_read(s) with BIO_should_write(bio))
30363042
*/
3037-
bio = s->wbio;
3038-
if (BIO_should_write(bio))
3039-
return (SSL_ERROR_WANT_WRITE);
3040-
else if (BIO_should_read(bio))
3041-
/*
3042-
* See above (SSL_want_read(s) with BIO_should_write(bio))
3043-
*/
3044-
return (SSL_ERROR_WANT_READ);
3045-
else if (BIO_should_io_special(bio)) {
3046-
reason = BIO_get_retry_reason(bio);
3047-
if (reason == BIO_RR_CONNECT)
3048-
return (SSL_ERROR_WANT_CONNECT);
3049-
else if (reason == BIO_RR_ACCEPT)
3050-
return (SSL_ERROR_WANT_ACCEPT);
3051-
else
3052-
return (SSL_ERROR_SYSCALL);
3053-
}
3054-
}
3055-
if (SSL_want_x509_lookup(s)) {
3056-
return (SSL_ERROR_WANT_X509_LOOKUP);
3057-
}
3058-
if (SSL_want_async(s)) {
3059-
return SSL_ERROR_WANT_ASYNC;
3060-
}
3061-
if (SSL_want_async_job(s)) {
3062-
return SSL_ERROR_WANT_ASYNC_JOB;
3043+
return (SSL_ERROR_WANT_READ);
3044+
else if (BIO_should_io_special(bio)) {
3045+
reason = BIO_get_retry_reason(bio);
3046+
if (reason == BIO_RR_CONNECT)
3047+
return (SSL_ERROR_WANT_CONNECT);
3048+
else if (reason == BIO_RR_ACCEPT)
3049+
return (SSL_ERROR_WANT_ACCEPT);
3050+
else
3051+
return (SSL_ERROR_SYSCALL);
30633052
}
30643053
}
3065-
3066-
if (i == 0) {
3067-
if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
3068-
(s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
3069-
return (SSL_ERROR_ZERO_RETURN);
3054+
if (SSL_want_x509_lookup(s)) {
3055+
return (SSL_ERROR_WANT_X509_LOOKUP);
30703056
}
3057+
if (SSL_want_async(s)) {
3058+
return SSL_ERROR_WANT_ASYNC;
3059+
}
3060+
if (SSL_want_async_job(s)) {
3061+
return SSL_ERROR_WANT_ASYNC_JOB;
3062+
}
3063+
3064+
if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
3065+
(s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
3066+
return (SSL_ERROR_ZERO_RETURN);
3067+
30713068
return (SSL_ERROR_SYSCALL);
30723069
}
30733070

0 commit comments

Comments
 (0)