fixes from peer review: move OS-specific code from wolfSSL_BIO_read() and wolfSSL_BIO_write() to wolfIO_Recv(), wolfIO_Send(), wolfIO_RecvFrom(), and wolfIO_SendTo(); add SOCKET_ETIMEDOUT definitions to wolfio.h; misc cleanups.

This commit is contained in:
Daniel Pouzzner
2024-06-26 02:04:37 -05:00
parent 0c1163f01f
commit 5298039d09
7 changed files with 86 additions and 39 deletions

View File

@@ -353,14 +353,9 @@ int wolfSSL_BIO_read(WOLFSSL_BIO* bio, void* buf, int len)
* (cannot be used with WOLFSSL_USER_IO) */ * (cannot be used with WOLFSSL_USER_IO) */
bio->flags &= ~WOLFSSL_BIO_FLAG_RETRY; bio->flags &= ~WOLFSSL_BIO_FLAG_RETRY;
ret = wolfIO_Recv(bio->num, (char*)buf, len, 0); ret = wolfIO_Recv(bio->num, (char*)buf, len, 0);
if (ret < 0) { if (ret == WC_NO_ERR_TRACE(SOCKET_NODATA)) {
#ifdef USE_WINDOWS_API bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
if (WSAGetLastError() == WSAEWOULDBLOCK) ret = WOLFSSL_BIO_ERROR;
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
#else
if (errno == EAGAIN)
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
#endif
} }
#else #else
ret = NOT_COMPILED_IN; ret = NOT_COMPILED_IN;
@@ -379,14 +374,9 @@ int wolfSSL_BIO_read(WOLFSSL_BIO* bio, void* buf, int len)
wolfSSL_BIO_ADDR_clear(&bio->peer_addr); wolfSSL_BIO_ADDR_clear(&bio->peer_addr);
ret = wolfIO_RecvFrom(bio->num, &bio->peer_addr, (char*)buf, len, 0); ret = wolfIO_RecvFrom(bio->num, &bio->peer_addr, (char*)buf, len, 0);
} }
if ((ret < 0) && (ret != WC_NO_ERR_TRACE(MEMORY_E))) { if (ret == WC_NO_ERR_TRACE(SOCKET_NODATA)) {
#ifdef USE_WINDOWS_API bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
if (WSAGetLastError() == WSAEWOULDBLOCK) ret = WOLFSSL_BIO_ERROR;
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
#else
if (errno == EAGAIN)
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
#endif
} }
#else #else
ret = NOT_COMPILED_IN; ret = NOT_COMPILED_IN;
@@ -782,13 +772,10 @@ int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len)
* (cannot be used with WOLFSSL_USER_IO) */ * (cannot be used with WOLFSSL_USER_IO) */
bio->flags &= ~WOLFSSL_BIO_FLAG_RETRY; bio->flags &= ~WOLFSSL_BIO_FLAG_RETRY;
ret = wolfIO_Send(bio->num, (char*)data, len, 0); ret = wolfIO_Send(bio->num, (char*)data, len, 0);
#ifdef USE_WINDOWS_API if (ret == WC_NO_ERR_TRACE(SOCKET_NODATA)) {
if (WSAGetLastError() == WSAEWOULDBLOCK)
bio->flags |= WOLFSSL_BIO_FLAG_RETRY; bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
#else ret = WOLFSSL_BIO_ERROR;
if (errno == EAGAIN) }
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
#endif
#else #else
ret = NOT_COMPILED_IN; ret = NOT_COMPILED_IN;
#endif #endif
@@ -806,14 +793,9 @@ int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len)
ret = SOCKET_ERROR_E; ret = SOCKET_ERROR_E;
else else
ret = wolfIO_SendTo(bio->num, &bio->peer_addr, (char*)data, len, 0); ret = wolfIO_SendTo(bio->num, &bio->peer_addr, (char*)data, len, 0);
if (ret < 0) { if (ret == WC_NO_ERR_TRACE(SOCKET_NODATA)) {
#ifdef USE_WINDOWS_API bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
if (WSAGetLastError() == WSAEWOULDBLOCK) ret = WOLFSSL_BIO_ERROR;
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
#else
if (errno == EAGAIN)
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
#endif
} }
#else #else
ret = NOT_COMPILED_IN; ret = NOT_COMPILED_IN;

View File

@@ -2891,7 +2891,6 @@ int wolfSSL_CTX_load_verify_locations(WOLFSSL_CTX* ctx, const char* file,
* @return 1 on success. * @return 1 on success.
* @return 0 on failure. * @return 0 on failure.
*/ */
WOLFSSL_API
int wolfSSL_CTX_load_verify_locations_compat(WOLFSSL_CTX* ctx, const char* file, int wolfSSL_CTX_load_verify_locations_compat(WOLFSSL_CTX* ctx, const char* file,
const char* path) const char* path)
{ {

View File

@@ -177,7 +177,7 @@ static WC_INLINE int wolfSSL_LastError(int err)
#elif defined(EBSNET) #elif defined(EBSNET)
return xn_getlasterror(); return xn_getlasterror();
#elif defined(WOLFSSL_LINUXKM) || defined(WOLFSSL_EMNET) #elif defined(WOLFSSL_LINUXKM) || defined(WOLFSSL_EMNET)
return err; /* Return provided error value */ return -err; /* Return provided error value */
#elif defined(FUSION_RTOS) #elif defined(FUSION_RTOS)
#include <fclerrno.h> #include <fclerrno.h>
return FCL_GET_ERRNO; return FCL_GET_ERRNO;
@@ -1100,6 +1100,21 @@ int wolfIO_Recv(SOCKET_T sd, char *buf, int sz, int rdFlags)
recvd = (int)RECV_FUNCTION(sd, buf, (size_t)sz, rdFlags); recvd = (int)RECV_FUNCTION(sd, buf, (size_t)sz, rdFlags);
recvd = TranslateReturnCode(recvd, (int)sd); recvd = TranslateReturnCode(recvd, (int)sd);
if (recvd < 0) {
int last_err = wolfSSL_LastError(recvd);
if ((last_err == SOCKET_EWOULDBLOCK)
#if SOCKET_EWOULDBLOCK != SOCKET_EAGAIN
|| (last_err == SOCKET_EAGAIN)
#endif
#ifdef SOCKET_ETIMEDOUT
|| (last_err == SOCKET_ETIMEDOUT)
#endif
)
{
return SOCKET_NODATA;
}
}
return recvd; return recvd;
} }
@@ -1110,6 +1125,21 @@ int wolfIO_Send(SOCKET_T sd, char *buf, int sz, int wrFlags)
sent = (int)SEND_FUNCTION(sd, buf, (size_t)sz, wrFlags); sent = (int)SEND_FUNCTION(sd, buf, (size_t)sz, wrFlags);
sent = TranslateReturnCode(sent, (int)sd); sent = TranslateReturnCode(sent, (int)sd);
if (sent < 0) {
int last_err = wolfSSL_LastError(sent);
if ((last_err == SOCKET_EWOULDBLOCK)
#if SOCKET_EWOULDBLOCK != SOCKET_EAGAIN
|| (last_err == SOCKET_EAGAIN)
#endif
#ifdef SOCKET_ETIMEDOUT
|| (last_err == SOCKET_ETIMEDOUT)
#endif
)
{
return SOCKET_NODATA;
}
}
return sent; return sent;
} }
@@ -1120,9 +1150,26 @@ int wolfIO_RecvFrom(SOCKET_T sd, WOLFSSL_BIO_ADDR *addr, char *buf, int sz, int
int recvd; int recvd;
socklen_t addr_len = (socklen_t)sizeof(*addr); socklen_t addr_len = (socklen_t)sizeof(*addr);
recvd = (int)DTLS_RECVFROM_FUNCTION(sd, buf, (size_t)sz, rdFlags, addr ? &addr->sa : NULL, addr ? &addr_len : 0); recvd = (int)DTLS_RECVFROM_FUNCTION(sd, buf, (size_t)sz, rdFlags,
addr ? &addr->sa : NULL,
addr ? &addr_len : 0);
recvd = TranslateReturnCode(recvd, (int)sd); recvd = TranslateReturnCode(recvd, (int)sd);
if (recvd < 0) {
int last_err = wolfSSL_LastError(recvd);
if ((last_err == SOCKET_EWOULDBLOCK)
#if SOCKET_EWOULDBLOCK != SOCKET_EAGAIN
|| (last_err == SOCKET_EAGAIN)
#endif
#ifdef SOCKET_ETIMEDOUT
|| (last_err == SOCKET_ETIMEDOUT)
#endif
)
{
return SOCKET_NODATA;
}
}
return recvd; return recvd;
} }
@@ -1130,9 +1177,26 @@ int wolfIO_SendTo(SOCKET_T sd, WOLFSSL_BIO_ADDR *addr, char *buf, int sz, int wr
{ {
int sent; int sent;
sent = (int)DTLS_SENDTO_FUNCTION(sd, buf, (size_t)sz, wrFlags, addr ? &addr->sa : NULL, addr ? wolfSSL_BIO_ADDR_size(addr) : 0); sent = (int)DTLS_SENDTO_FUNCTION(sd, buf, (size_t)sz, wrFlags,
addr ? &addr->sa : NULL,
addr ? wolfSSL_BIO_ADDR_size(addr) : 0);
sent = TranslateReturnCode(sent, (int)sd); sent = TranslateReturnCode(sent, (int)sd);
if (sent < 0) {
int last_err = wolfSSL_LastError(sent);
if ((last_err == SOCKET_EWOULDBLOCK)
#if SOCKET_EWOULDBLOCK != SOCKET_EAGAIN
|| (last_err == SOCKET_EAGAIN)
#endif
#ifdef SOCKET_ETIMEDOUT
|| (last_err == SOCKET_ETIMEDOUT)
#endif
)
{
return SOCKET_NODATA;
}
}
return sent; return sent;
} }

View File

@@ -56986,9 +56986,7 @@ static int test_wolfSSL_BIO_datagram(void)
static const struct timeval timeout = { 0, 250000 }; static const struct timeval timeout = { 0, 250000 };
#endif #endif
#ifdef USE_WINDOWS_API StartTCP();
WSAStartup();
#endif
if (EXPECT_SUCCESS()) { if (EXPECT_SUCCESS()) {
fd1 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); fd1 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

View File

@@ -13775,7 +13775,7 @@ static int GetRDN(DecodedCert* cert, char* full, word32* idx, int* nid,
* @param [in, out] cert Decoded certificate object. * @param [in, out] cert Decoded certificate object.
* @param [out] full Buffer to hold full name as a string. * @param [out] full Buffer to hold full name as a string.
* @param [out] hash Buffer to hold hash of name. * @param [out] hash Buffer to hold hash of name.
* @param [in] nameType ISSUER or SUBJECT. * @param [in] nameType ASN_ISSUER or ASN_SUBJECT.
* @param [in] input Buffer holding certificate name. * @param [in] input Buffer holding certificate name.
* @param [in, out] inOutIdx On in, start of certificate name. * @param [in, out] inOutIdx On in, start of certificate name.
* On out, start of ASN.1 item after cert name. * On out, start of ASN.1 item after cert name.

View File

@@ -185,8 +185,8 @@ enum wolfSSL_ErrorCodes {
DTLS_CID_ERROR = -454, /* Wrong or missing CID */ DTLS_CID_ERROR = -454, /* Wrong or missing CID */
DTLS_TOO_MANY_FRAGMENTS_E = -455, /* Received too many fragments */ DTLS_TOO_MANY_FRAGMENTS_E = -455, /* Received too many fragments */
QUIC_WRONG_ENC_LEVEL = -456, /* QUIC data received on wrong encryption level */ QUIC_WRONG_ENC_LEVEL = -456, /* QUIC data received on wrong encryption level */
DUPLICATE_TLS_EXT_E = -457, /* Duplicate TLS extension in msg. */ DUPLICATE_TLS_EXT_E = -457, /* Duplicate TLS extension in msg. */
SOCKET_NOT_CONNECTED_E = -458, /* Socket has no associated peer. */
/* add strings to wolfSSL_ERR_reason_error_string in internal.c !!!!! */ /* add strings to wolfSSL_ERR_reason_error_string in internal.c !!!!! */
/* begin negotiation parameter errors */ /* begin negotiation parameter errors */

View File

@@ -208,6 +208,7 @@
#endif #endif
#define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK
#define SOCKET_EAGAIN WSAETIMEDOUT #define SOCKET_EAGAIN WSAETIMEDOUT
#define SOCKET_ETIMEDOUT WSAETIMEDOUT
#define SOCKET_ECONNRESET WSAECONNRESET #define SOCKET_ECONNRESET WSAECONNRESET
#define SOCKET_EINTR WSAEINTR #define SOCKET_EINTR WSAEINTR
#define SOCKET_EPIPE WSAEPIPE #define SOCKET_EPIPE WSAEPIPE
@@ -312,6 +313,7 @@
#elif defined(WOLFSSL_LWIP_NATIVE) #elif defined(WOLFSSL_LWIP_NATIVE)
#define SOCKET_EWOULDBLOCK ERR_WOULDBLOCK #define SOCKET_EWOULDBLOCK ERR_WOULDBLOCK
#define SOCKET_EAGAIN ERR_WOULDBLOCK #define SOCKET_EAGAIN ERR_WOULDBLOCK
#define SOCKET_TIMEDOUT ERR_TIMEOUT
#define SOCKET_ECONNRESET ERR_RST #define SOCKET_ECONNRESET ERR_RST
#define SOCKET_EINTR ERR_CLSD #define SOCKET_EINTR ERR_CLSD
#define SOCKET_EPIPE ERR_CLSD #define SOCKET_EPIPE ERR_CLSD
@@ -329,6 +331,7 @@
#else #else
#define SOCKET_EWOULDBLOCK EWOULDBLOCK #define SOCKET_EWOULDBLOCK EWOULDBLOCK
#define SOCKET_EAGAIN EAGAIN #define SOCKET_EAGAIN EAGAIN
#define SOCKET_ETIMEDOUT ETIMEDOUT
#define SOCKET_ECONNRESET ECONNRESET #define SOCKET_ECONNRESET ECONNRESET
#define SOCKET_EINTR EINTR #define SOCKET_EINTR EINTR
#define SOCKET_EPIPE EPIPE #define SOCKET_EPIPE EPIPE
@@ -514,6 +517,7 @@ WOLFSSL_API int wolfIO_RecvFrom(SOCKET_T sd, WOLFSSL_BIO_ADDR *addr, char *buf,
FNS_CLOSE(s, &err); \ FNS_CLOSE(s, &err); \
} while(0) } while(0)
#endif #endif
#define StartTCP() WC_DO_NOTHING
#else #else
#ifndef CloseSocket #ifndef CloseSocket
#define CloseSocket(s) close(s) #define CloseSocket(s) close(s)