Skip to content

Commit

Permalink
fixes from peer review: move OS-specific code from wolfSSL_BIO_read()…
Browse files Browse the repository at this point in the history
… and wolfSSL_BIO_write() to wolfIO_Recv(), wolfIO_Send(), wolfIO_RecvFrom(), and wolfIO_SendTo(); add SOCKET_ETIMEDOUT definitions to wolfio.h; misc cleanups.
  • Loading branch information
douzzer committed Jun 28, 2024
1 parent de4ce19 commit a6cd359
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 39 deletions.
42 changes: 12 additions & 30 deletions src/bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,9 @@ int wolfSSL_BIO_read(WOLFSSL_BIO* bio, void* buf, int len)
* (cannot be used with WOLFSSL_USER_IO) */
bio->flags &= ~WOLFSSL_BIO_FLAG_RETRY;
ret = wolfIO_Recv(bio->num, (char*)buf, len, 0);
if (ret < 0) {
#ifdef USE_WINDOWS_API
if (WSAGetLastError() == WSAEWOULDBLOCK)
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
#else
if (errno == EAGAIN)
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
#endif
if (ret == WC_NO_ERR_TRACE(SOCKET_NODATA)) {
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
ret = WOLFSSL_BIO_ERROR;
}
#else
ret = NOT_COMPILED_IN;
Expand All @@ -379,14 +374,9 @@ int wolfSSL_BIO_read(WOLFSSL_BIO* bio, void* buf, int len)
wolfSSL_BIO_ADDR_clear(&bio->peer_addr);
ret = wolfIO_RecvFrom(bio->num, &bio->peer_addr, (char*)buf, len, 0);
}
if ((ret < 0) && (ret != WC_NO_ERR_TRACE(MEMORY_E))) {
#ifdef USE_WINDOWS_API
if (WSAGetLastError() == WSAEWOULDBLOCK)
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
#else
if (errno == EAGAIN)
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
#endif
if (ret == WC_NO_ERR_TRACE(SOCKET_NODATA)) {
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
ret = WOLFSSL_BIO_ERROR;
}
#else
ret = NOT_COMPILED_IN;
Expand Down Expand Up @@ -782,13 +772,10 @@ int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len)
* (cannot be used with WOLFSSL_USER_IO) */
bio->flags &= ~WOLFSSL_BIO_FLAG_RETRY;
ret = wolfIO_Send(bio->num, (char*)data, len, 0);
#ifdef USE_WINDOWS_API
if (WSAGetLastError() == WSAEWOULDBLOCK)
if (ret == WC_NO_ERR_TRACE(SOCKET_NODATA)) {
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
#else
if (errno == EAGAIN)
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
#endif
ret = WOLFSSL_BIO_ERROR;
}
#else
ret = NOT_COMPILED_IN;
#endif
Expand All @@ -806,14 +793,9 @@ int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len)
ret = SOCKET_ERROR_E;
else
ret = wolfIO_SendTo(bio->num, &bio->peer_addr, (char*)data, len, 0);
if (ret < 0) {
#ifdef USE_WINDOWS_API
if (WSAGetLastError() == WSAEWOULDBLOCK)
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
#else
if (errno == EAGAIN)
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
#endif
if (ret == WC_NO_ERR_TRACE(SOCKET_NODATA)) {
bio->flags |= WOLFSSL_BIO_FLAG_RETRY;
ret = WOLFSSL_BIO_ERROR;
}
#else
ret = NOT_COMPILED_IN;
Expand Down
1 change: 0 additions & 1 deletion src/ssl_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -2886,7 +2886,6 @@ int wolfSSL_CTX_load_verify_locations(WOLFSSL_CTX* ctx, const char* file,
* @return 1 on success.
* @return 0 on failure.
*/
WOLFSSL_API
int wolfSSL_CTX_load_verify_locations_compat(WOLFSSL_CTX* ctx, const char* file,
const char* path)
{
Expand Down
70 changes: 67 additions & 3 deletions src/wolfio.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ static WC_INLINE int wolfSSL_LastError(int err)
#elif defined(EBSNET)
return xn_getlasterror();
#elif defined(WOLFSSL_LINUXKM) || defined(WOLFSSL_EMNET)
return err; /* Return provided error value */
return -err; /* Return provided error value */
#elif defined(FUSION_RTOS)
#include <fclerrno.h>
return FCL_GET_ERRNO;
Expand Down Expand Up @@ -989,6 +989,21 @@ int wolfIO_Recv(SOCKET_T sd, char *buf, int sz, int rdFlags)
recvd = (int)RECV_FUNCTION(sd, buf, (size_t)sz, rdFlags);
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;
}

Expand All @@ -999,6 +1014,21 @@ int wolfIO_Send(SOCKET_T sd, char *buf, int sz, int wrFlags)
sent = (int)SEND_FUNCTION(sd, buf, (size_t)sz, wrFlags);
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;
}

Expand All @@ -1009,19 +1039,53 @@ int wolfIO_RecvFrom(SOCKET_T sd, WOLFSSL_BIO_ADDR *addr, char *buf, int sz, int
int recvd;
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);

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;
}

int wolfIO_SendTo(SOCKET_T sd, WOLFSSL_BIO_ADDR *addr, char *buf, int sz, int wrFlags)
{
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);

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;
}

Expand Down
4 changes: 1 addition & 3 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -48761,9 +48761,7 @@ static int test_wolfSSL_BIO_datagram(void)
static const struct timeval timeout = { 0, 250000 };
#endif

#ifdef USE_WINDOWS_API
WSAStartup();
#endif
StartTCP();

if (EXPECT_SUCCESS()) {
fd1 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
Expand Down
2 changes: 1 addition & 1 deletion wolfcrypt/src/asn.c
Original file line number Diff line number Diff line change
Expand Up @@ -13774,7 +13774,7 @@ static int GetRDN(DecodedCert* cert, char* full, word32* idx, int* nid,
* @param [in, out] cert Decoded certificate object.
* @param [out] full Buffer to hold full name as a string.
* @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, out] inOutIdx On in, start of certificate name.
* On out, start of ASN.1 item after cert name.
Expand Down
2 changes: 1 addition & 1 deletion wolfssl/error-ssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ enum wolfSSL_ErrorCodes {
DTLS_CID_ERROR = -454, /* Wrong or missing CID */
DTLS_TOO_MANY_FRAGMENTS_E = -455, /* Received too many fragments */
QUIC_WRONG_ENC_LEVEL = -456, /* QUIC data received on wrong encryption level */

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 !!!!! */

/* begin negotiation parameter errors */
Expand Down
4 changes: 4 additions & 0 deletions wolfssl/wolfio.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
#endif
#define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK
#define SOCKET_EAGAIN WSAETIMEDOUT
#define SOCKET_ETIMEDOUT WSAETIMEDOUT
#define SOCKET_ECONNRESET WSAECONNRESET
#define SOCKET_EINTR WSAEINTR
#define SOCKET_EPIPE WSAEPIPE
Expand Down Expand Up @@ -301,6 +302,7 @@
#elif defined(WOLFSSL_LWIP_NATIVE)
#define SOCKET_EWOULDBLOCK ERR_WOULDBLOCK
#define SOCKET_EAGAIN ERR_WOULDBLOCK
#define SOCKET_TIMEDOUT ERR_TIMEOUT
#define SOCKET_ECONNRESET ERR_RST
#define SOCKET_EINTR ERR_CLSD
#define SOCKET_EPIPE ERR_CLSD
Expand All @@ -318,6 +320,7 @@
#else
#define SOCKET_EWOULDBLOCK EWOULDBLOCK
#define SOCKET_EAGAIN EAGAIN
#define SOCKET_ETIMEDOUT ETIMEDOUT
#define SOCKET_ECONNRESET ECONNRESET
#define SOCKET_EINTR EINTR
#define SOCKET_EPIPE EPIPE
Expand Down Expand Up @@ -495,6 +498,7 @@ WOLFSSL_API int wolfIO_RecvFrom(SOCKET_T sd, WOLFSSL_BIO_ADDR *addr, char *buf,
FNS_CLOSE(s, &err); \
} while(0)
#endif
#define StartTCP() WC_DO_NOTHING
#else
#ifndef CloseSocket
#define CloseSocket(s) close(s)
Expand Down

0 comments on commit a6cd359

Please sign in to comment.