Fix for BIO with callbacks not called after PR #3824 (was always returning WANT_READ).

This commit is contained in:
David Garske
2021-03-22 10:36:39 -07:00
parent fd94d05b0a
commit 5c762afb94
3 changed files with 45 additions and 42 deletions

View File

@@ -213,46 +213,47 @@ int wolfSSL_BIO_read(WOLFSSL_BIO* bio, void* buf, int len)
if (bio->method && bio->method->readCb) { if (bio->method && bio->method->readCb) {
ret = bio->method->readCb(bio, (char*)buf, len); ret = bio->method->readCb(bio, (char*)buf, len);
} }
else {
/* formatting data */
if (bio->type == WOLFSSL_BIO_BASE64 && ret > 0 && sz > 0) {
ret = wolfSSL_BIO_BASE64_read(bio, buf, sz);
}
/* formatting data */ /* write BIOs */
if (bio->type == WOLFSSL_BIO_BASE64 && ret > 0 && sz > 0) { if (bio && bio->type == WOLFSSL_BIO_BIO) {
ret = wolfSSL_BIO_BASE64_read(bio, buf, sz); ret = wolfSSL_BIO_BIO_read(bio, buf, len);
} }
/* write BIOs */ if (bio && bio->type == WOLFSSL_BIO_MEMORY) {
if (bio && bio->type == WOLFSSL_BIO_BIO) { ret = wolfSSL_BIO_MEMORY_read(bio, buf, len);
ret = wolfSSL_BIO_BIO_read(bio, buf, len); }
}
if (bio && bio->type == WOLFSSL_BIO_MEMORY) { #ifndef NO_FILESYSTEM
ret = wolfSSL_BIO_MEMORY_read(bio, buf, len); if (bio && bio->type == WOLFSSL_BIO_FILE) {
} if (bio->ptr)
ret = (int)XFREAD(buf, 1, len, (XFILE)bio->ptr);
#ifndef NO_FILESYSTEM #if !defined(USE_WINDOWS_API) && !defined(NO_WOLFSSL_DIR)\
if (bio && bio->type == WOLFSSL_BIO_FILE) { && !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2)
if (bio->ptr) else
ret = (int)XFREAD(buf, 1, len, (XFILE)bio->ptr); ret = (int)XREAD(bio->num, buf, len);
#if !defined(USE_WINDOWS_API) && !defined(NO_WOLFSSL_DIR)\ #endif
&& !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2) }
else
ret = (int)XREAD(bio->num, buf, len);
#endif #endif
}
#endif
#ifndef WOLFCRYPT_ONLY #ifndef WOLFCRYPT_ONLY
if (bio && bio->type == WOLFSSL_BIO_SSL) { if (bio && bio->type == WOLFSSL_BIO_SSL) {
ret = wolfSSL_BIO_SSL_read(bio, buf, len, front); ret = wolfSSL_BIO_SSL_read(bio, buf, len, front);
} }
/* data passing through BIO MD wrapper */ /* data passing through BIO MD wrapper */
if (bio && bio->type == WOLFSSL_BIO_MD && ret > 0) { if (bio && bio->type == WOLFSSL_BIO_MD && ret > 0) {
ret = wolfSSL_BIO_MD_read(bio, buf, ret); ret = wolfSSL_BIO_MD_read(bio, buf, ret);
} }
#endif #endif
if (bio->type == WOLFSSL_BIO_SOCKET) { if (bio->type == WOLFSSL_BIO_SOCKET) {
ret = wolfIO_Recv(bio->num, (char*)buf, len, 0); ret = wolfIO_Recv(bio->num, (char*)buf, len, 0);
}
} }
/* case where front of list is done */ /* case where front of list is done */
@@ -263,6 +264,12 @@ int wolfSSL_BIO_read(WOLFSSL_BIO* bio, void* buf, int len)
if (ret > 0) { if (ret > 0) {
sz = ret; /* adjust size for formatting */ sz = ret; /* adjust size for formatting */
} }
else {
if (wolfSSL_BIO_supports_pending(bio) &&
wolfSSL_BIO_ctrl_pending(bio) == 0) {
ret = WOLFSSL_CBIO_ERR_WANT_READ;
}
}
/* previous WOLFSSL_BIO in list working towards head of list */ /* previous WOLFSSL_BIO in list working towards head of list */
bio = bio->prev; bio = bio->prev;
@@ -1144,7 +1151,7 @@ int wolfSSL_BIO_ctrl_reset_read_request(WOLFSSL_BIO *b)
WOLFSSL_ENTER("wolfSSL_BIO_ctrl_reset_read_request"); WOLFSSL_ENTER("wolfSSL_BIO_ctrl_reset_read_request");
if (b == NULL || b->type == WOLFSSL_BIO_MEMORY) { if (b == NULL || b->type == WOLFSSL_BIO_MEMORY) {
return SSL_FAILURE; return WOLFSSL_FAILURE;
} }
b->readRq = 0; b->readRq = 0;
@@ -1194,7 +1201,7 @@ int wolfSSL_BIO_nread(WOLFSSL_BIO *bio, char **buf, int num)
} }
if (bio->type == WOLFSSL_BIO_MEMORY) { if (bio->type == WOLFSSL_BIO_MEMORY) {
return SSL_FAILURE; return WOLFSSL_FAILURE;
} }
if (bio->pair != NULL) { if (bio->pair != NULL) {
@@ -1246,7 +1253,7 @@ int wolfSSL_BIO_nwrite(WOLFSSL_BIO *bio, char **buf, int num)
} }
if (bio->type != WOLFSSL_BIO_BIO) { if (bio->type != WOLFSSL_BIO_BIO) {
return SSL_FAILURE; return WOLFSSL_FAILURE;
} }
if (bio->pair != NULL) { if (bio->pair != NULL) {
@@ -1414,7 +1421,7 @@ long wolfSSL_BIO_get_fp(WOLFSSL_BIO *bio, XFILE* fp)
} }
if (bio->type != WOLFSSL_BIO_FILE) { if (bio->type != WOLFSSL_BIO_FILE) {
return SSL_FAILURE; return WOLFSSL_FAILURE;
} }
*fp = (XFILE)bio->ptr; *fp = (XFILE)bio->ptr;

View File

@@ -133,11 +133,6 @@ int BioReceive(WOLFSSL* ssl, char* buf, int sz, void* ctx)
return WOLFSSL_CBIO_ERR_GENERAL; return WOLFSSL_CBIO_ERR_GENERAL;
} }
if (wolfSSL_BIO_supports_pending(ssl->biord) &&
wolfSSL_BIO_ctrl_pending(ssl->biord) == 0) {
WOLFSSL_MSG("BIO want read");
return WOLFSSL_CBIO_ERR_WANT_READ;
}
recvd = wolfSSL_BIO_read(ssl->biord, buf, sz); recvd = wolfSSL_BIO_read(ssl->biord, buf, sz);
if (recvd <= 0) { if (recvd <= 0) {
if (ssl->biord->type == WOLFSSL_BIO_SOCKET) { if (ssl->biord->type == WOLFSSL_BIO_SOCKET) {

View File

@@ -3625,6 +3625,7 @@ WOLFSSL_API size_t wolfSSL_get_client_random(const WOLFSSL* ssl,
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
WOLFSSL_API size_t wolfSSL_BIO_wpending(const WOLFSSL_BIO *bio); WOLFSSL_API size_t wolfSSL_BIO_wpending(const WOLFSSL_BIO *bio);
/* non-standard API to determine if BIO supports "pending" */
WOLFSSL_API int wolfSSL_BIO_supports_pending(const WOLFSSL_BIO *bio); WOLFSSL_API int wolfSSL_BIO_supports_pending(const WOLFSSL_BIO *bio);
WOLFSSL_API size_t wolfSSL_BIO_ctrl_pending(WOLFSSL_BIO *b); WOLFSSL_API size_t wolfSSL_BIO_ctrl_pending(WOLFSSL_BIO *b);