Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .wolfssl_known_macro_extras
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ WOLFSSL_MONT_RED_CT
WOLFSSL_MP_COND_COPY
WOLFSSL_MP_INVMOD_CONSTANT_TIME
WOLFSSL_MULTICIRCULATE_ALTNAMELIST
WOLFSSL_NETX_DUO
WOLFSSL_NEW_PRIME_CHECK
WOLFSSL_NOSHA3_384
WOLFSSL_NOT_WINDOWS_API
Expand Down
39 changes: 37 additions & 2 deletions doc/dox_comments/header_files-ja/wolfio.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,12 @@ void wolfSSL_SetIOWriteFlags(WOLFSSL* ssl, int flags);
/*!
\ingroup IO

\brief この関数は、WOLFSSL構造体内のnxCtx構造体のnxSocketおよびnxWaitメンバーを設定します
\brief この関数は、WOLFSSL構造体内のnxCtx構造体のnxTcpSocketおよびnxWaitメンバーを設定します

\return none 戻り値なし。

\param ssl wolfSSL_new()を使用して作成されたWOLFSSL構造体へのポインタ。
\param nxSocket nxCTX構造体のnxSocketメンバーに設定されるNX_TCP_SOCKET型へのポインタ
\param nxSocket nxCtx構造体のnxTcpSocketメンバーに設定されるNX_TCP_SOCKET型へのポインタ
\param waitOption nxCtx構造体のnxWaitメンバーに設定されるULONG型。
Comment thread
dgarske marked this conversation as resolved.
Outdated

_Example_
Expand All @@ -432,6 +432,41 @@ void wolfSSL_SetIOWriteFlags(WOLFSSL* ssl, int flags);
void wolfSSL_SetIO_NetX(WOLFSSL* ssl, NX_TCP_SOCKET* nxsocket,
ULONG waitoption);

/*!
\ingroup IO

\brief この関数は、DTLSセッション用のNetX Duo UDPコンテキストを設定します。
UDPソケット、送信先IPアドレス(値渡し)、送信先ポート、待機オプションを
WOLFSSL nxCtx構造体に格納します。WOLFSSL_NETX_DUOの定義が必要です(ThreadX NetX Duo SDK)。

\return none 戻り値なし。

\param ssl wolfSSL_new()を使用して作成されたWOLFSSL構造体へのポインタ。
\param nxsocket 作成・バインド済みのNX_UDP_SOCKETへのポインタ。
\param nxdip 送信先NXD_ADDRESS(値渡し;IPv4またはIPv6)。
\param nxport 送信先UDPポート番号。
\param waitoption NetX待機オプション(例:NX_WAIT_FOREVERまたはティック数)。

_Example_
\code
WOLFSSL* ssl = wolfSSL_new(ctx);
NX_UDP_SOCKET udpSocket;
NXD_ADDRESS peerAddr;
USHORT peerPort = 4433;
ULONG wait = NX_WAIT_FOREVER;
// … udpSocketとpeerAddrを初期化 …
wolfSSL_SetIO_NetX_Dtls(ssl, &udpSocket, peerAddr, peerPort, wait);
\endcode

\sa wolfSSL_SetIO_NetX
\sa NetX_SendTo
\sa NetX_ReceiveFrom
*/
void wolfSSL_SetIO_NetX_Dtls(WOLFSSL* ssl, NX_UDP_SOCKET* nxsocket,
NXD_ADDRESS nxdip, USHORT nxport,
ULONG waitoption);


/*!
\brief この関数は、WOLFSSL_CTX構造体のCBIOCookieメンバーのコールバックを設定します。
CallbackGenCookie型は関数ポインタで、次のシグネチャを持ちます:
Expand Down
40 changes: 38 additions & 2 deletions doc/dox_comments/header_files/wolfio.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,14 +450,14 @@ void wolfSSL_SetIOWriteFlags(WOLFSSL* ssl, int flags);
/*!
\ingroup IO

\brief This function sets the nxSocket and nxWait members of the nxCtx
\brief This function sets the nxTcpSocket and nxWait members of the nxCtx
struct within the WOLFSSL structure.

\return none No returns.

\param ssl a pointer to a WOLFSSL structure, created using wolfSSL_new().
\param nxSocket a pointer to type NX_TCP_SOCKET that is set to the
nxSocket member of the nxCTX structure.
nxTcpSocket member of the nxCtx structure.
\param waitOption a ULONG type that is set to the nxWait member of
the nxCtx structure.
Comment thread
dgarske marked this conversation as resolved.

Expand All @@ -481,6 +481,42 @@ void wolfSSL_SetIOWriteFlags(WOLFSSL* ssl, int flags);
void wolfSSL_SetIO_NetX(WOLFSSL* ssl, NX_TCP_SOCKET* nxsocket,
ULONG waitoption);

/*!
\ingroup IO

\brief This function configures the NetX Duo UDP context for a DTLS
session. It stores the UDP socket, destination IP address (by value),
destination port, and wait option into the WOLFSSL nxCtx structure.
Requires WOLFSSL_NETX_DUO to be defined (ThreadX NetX Duo SDK).

\return none No returns.

\param ssl a pointer to a WOLFSSL structure, created using wolfSSL_new().
\param nxsocket a pointer to an NX_UDP_SOCKET already created and bound.
\param nxdip the destination NXD_ADDRESS (passed by value; IPv4 or IPv6).
\param nxport the destination UDP port number.
\param waitoption a ULONG NetX wait option (e.g. NX_WAIT_FOREVER or ticks).

_Example_
\code
WOLFSSL* ssl = wolfSSL_new(ctx);
NX_UDP_SOCKET udpSocket;
NXD_ADDRESS peerAddr;
USHORT peerPort = 4433;
ULONG wait = NX_WAIT_FOREVER;
// … initialise udpSocket and peerAddr …
wolfSSL_SetIO_NetX_Dtls(ssl, &udpSocket, peerAddr, peerPort, wait);
\endcode

\sa wolfSSL_SetIO_NetX
\sa NetX_SendTo
\sa NetX_ReceiveFrom
*/
void wolfSSL_SetIO_NetX_Dtls(WOLFSSL* ssl, NX_UDP_SOCKET* nxsocket,
NXD_ADDRESS nxdip, USHORT nxport,
ULONG waitoption);
Comment thread
dgarske marked this conversation as resolved.


/*!
\brief This function sets the callback for the CBIOCookie member of the
WOLFSSL_CTX structure. The CallbackGenCookie type is a function pointer
Expand Down
12 changes: 11 additions & 1 deletion src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2668,6 +2668,12 @@ int InitSSL_Ctx(WOLFSSL_CTX* ctx, WOLFSSL_METHOD* method, void* heap)
#ifdef HAVE_NETX
ctx->CBIORecv = NetX_Receive;
ctx->CBIOSend = NetX_Send;
#if defined(WOLFSSL_DTLS) && defined(WOLFSSL_NETX_DUO)
if (method->version.major == DTLS_MAJOR) {
ctx->CBIORecv = NetX_ReceiveFrom;
ctx->CBIOSend = NetX_SendTo;
}
#endif /* WOLFSSL_DTLS && WOLFSSL_NETX_DUO */
#elif defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP)
ctx->CBIORecv = Mynewt_Receive;
ctx->CBIOSend = Mynewt_Send;
Expand Down Expand Up @@ -7907,9 +7913,13 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
if (wc_InitRwLock(&ssl->buffers.dtlsCtx.peerLock) != 0)
return BAD_MUTEX_E;
#endif

#ifdef HAVE_NETX
Comment thread
dgarske marked this conversation as resolved.
ssl->IOCB_ReadCtx = &ssl->nxCtx; /* default NetX IO ctx, same for read */
ssl->IOCB_WriteCtx = &ssl->nxCtx; /* and write */
#else
ssl->IOCB_ReadCtx = &ssl->buffers.dtlsCtx; /* prevent invalid pointer access if not */
ssl->IOCB_WriteCtx = &ssl->buffers.dtlsCtx; /* correctly set */
#endif
#else
#ifdef HAVE_NETX
ssl->IOCB_ReadCtx = &ssl->nxCtx; /* default NetX IO ctx, same for read */
Expand Down
165 changes: 154 additions & 11 deletions src/wolfio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2530,7 +2530,7 @@ void wolfSSL_CTX_SetIOSetPeer(WOLFSSL_CTX* ctx, CallbackSetPeer cb)

#ifdef HAVE_NETX

/* The NetX receive callback
/* The NetX receive callback for TLS
* return : bytes read, or error
*/
int NetX_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx)
Expand All @@ -2543,13 +2543,13 @@ int NetX_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx)

(void)ssl;

if (nxCtx == NULL || nxCtx->nxSocket == NULL) {
if (nxCtx == NULL || nxCtx->nxTcpSocket == NULL) {
WOLFSSL_MSG("NetX Recv NULL parameters");
return WOLFSSL_CBIO_ERR_GENERAL;
}

if (nxCtx->nxPacket == NULL) {
status = nx_tcp_socket_receive(nxCtx->nxSocket, &nxCtx->nxPacket,
status = nx_tcp_socket_receive(nxCtx->nxTcpSocket, &nxCtx->nxPacket,
nxCtx->nxWait);
if (status != NX_SUCCESS) {
WOLFSSL_MSG("NetX Recv receive error");
Expand Down Expand Up @@ -2586,7 +2586,7 @@ int NetX_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx)
}


/* The NetX send callback
/* The NetX send callback for TLS
* return : bytes sent, or error
*/
int NetX_Send(WOLFSSL* ssl, char *buf, int sz, void *ctx)
Expand All @@ -2598,12 +2598,12 @@ int NetX_Send(WOLFSSL* ssl, char *buf, int sz, void *ctx)

(void)ssl;

if (nxCtx == NULL || nxCtx->nxSocket == NULL) {
if (nxCtx == NULL || nxCtx->nxTcpSocket == NULL) {
WOLFSSL_MSG("NetX Send NULL parameters");
return WOLFSSL_CBIO_ERR_GENERAL;
}

pool = nxCtx->nxSocket->nx_tcp_socket_ip_ptr->nx_ip_default_packet_pool;
pool = nxCtx->nxTcpSocket->nx_tcp_socket_ip_ptr->nx_ip_default_packet_pool;
status = nx_packet_allocate(pool, &packet, NX_TCP_PACKET,
nxCtx->nxWait);
if (status != NX_SUCCESS) {
Expand All @@ -2618,7 +2618,7 @@ int NetX_Send(WOLFSSL* ssl, char *buf, int sz, void *ctx)
return WOLFSSL_CBIO_ERR_GENERAL;
}

status = nx_tcp_socket_send(nxCtx->nxSocket, packet, nxCtx->nxWait);
status = nx_tcp_socket_send(nxCtx->nxTcpSocket, packet, nxCtx->nxWait);
if (status != NX_SUCCESS) {
nx_packet_release(packet);
WOLFSSL_MSG("NetX Send socket send error");
Expand All @@ -2628,15 +2628,158 @@ int NetX_Send(WOLFSSL* ssl, char *buf, int sz, void *ctx)
return sz;
}


/* like set_fd, but for default NetX context */
void wolfSSL_SetIO_NetX(WOLFSSL* ssl, NX_TCP_SOCKET* nxSocket, ULONG waitOption)
void wolfSSL_SetIO_NetX(WOLFSSL* ssl, NX_TCP_SOCKET* nxsocket, ULONG waitoption)
{
if (ssl) {
ssl->nxCtx.nxTcpSocket = nxsocket;
ssl->nxCtx.nxWait = waitoption;
}
}

/* WOLFSSL_NETX_DUO: requires ThreadX NetX Duo (NXD_ADDRESS, nxd_udp_socket_send) */
#if defined(WOLFSSL_DTLS) && defined(WOLFSSL_NETX_DUO)
/* The NetX receive callback for DTLS
* return : bytes read, or error
*/
int NetX_ReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *ctx)
Comment thread
dgarske marked this conversation as resolved.
Comment thread
dgarske marked this conversation as resolved.
Comment thread
dgarske marked this conversation as resolved.
Comment thread
dgarske marked this conversation as resolved.
{
NetX_Ctx* nxCtx = (NetX_Ctx*)ctx;
ULONG left;
ULONG total;
ULONG copied = 0;
UINT status;
ULONG waitOption;

if (nxCtx == NULL || nxCtx->nxUdpSocket == NULL) {
WOLFSSL_MSG("NetX Recv NULL parameters");
return WOLFSSL_CBIO_ERR_GENERAL;
}

if (nxCtx->nxPacket == NULL) {
Comment thread
dgarske marked this conversation as resolved.
Outdated
/* Compute wait ticks from the current DTLS retransmit timeout so the
* DTLS state machine can drive retransmission on packet loss. */
if (!wolfSSL_dtls_get_using_nonblock(ssl)) {
Comment thread
dgarske marked this conversation as resolved.
Outdated
Comment thread
dgarske marked this conversation as resolved.
Outdated
int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl);
#ifdef WOLFSSL_DTLS13
if (wolfSSL_dtls13_use_quick_timeout(ssl) &&
IsAtLeastTLSv1_3(ssl->version))
dtls_timeout = dtls_timeout / 4;
#endif
waitOption = (dtls_timeout > 0)
? (ULONG)dtls_timeout * NX_IP_PERIODIC_RATE
: nxCtx->nxWait;
}
else {
waitOption = NX_NO_WAIT; /* non-blocking */
}
Comment thread
dgarske marked this conversation as resolved.
Outdated

status = nx_udp_socket_receive(nxCtx->nxUdpSocket, &nxCtx->nxPacket,
waitOption);
if (status != NX_SUCCESS) {
if (status == NX_NO_PACKET) {
/* Normal receive timeout - allow DTLS to retransmit */
WOLFSSL_MSG("NetX Recv timeout");
return wolfSSL_dtls_get_using_nonblock(ssl)
? WOLFSSL_CBIO_ERR_WANT_READ
: WOLFSSL_CBIO_ERR_TIMEOUT;
}
WOLFSSL_MSG("NetX Recv receive error");
return WOLFSSL_CBIO_ERR_GENERAL;
}
}

if (nxCtx->nxPacket) {
Comment thread
dgarske marked this conversation as resolved.
Outdated
status = nx_packet_length_get(nxCtx->nxPacket, &total);
if (status != NX_SUCCESS) {
WOLFSSL_MSG("NetX Recv length get error");
return WOLFSSL_CBIO_ERR_GENERAL;
}

left = total - nxCtx->nxOffset;
status = nx_packet_data_extract_offset(nxCtx->nxPacket, nxCtx->nxOffset,
buf, sz, &copied);
if (status != NX_SUCCESS) {
WOLFSSL_MSG("NetX Recv data extract offset error");
return WOLFSSL_CBIO_ERR_GENERAL;
}
Comment thread
dgarske marked this conversation as resolved.
Outdated

nxCtx->nxOffset += copied;

if (copied == left) {
WOLFSSL_MSG("NetX Recv Drained packet");
nx_packet_release(nxCtx->nxPacket);
nxCtx->nxPacket = NULL;
nxCtx->nxOffset = 0;
}
}

return copied;
}

/* The NetX send callback for DTLS
* return : bytes sent, or error
*/
int NetX_SendTo(WOLFSSL* ssl, char *buf, int sz, void *ctx)
{
NetX_Ctx* nxCtx = (NetX_Ctx*)ctx;
NX_PACKET* packet;
NX_PACKET_POOL* pool; /* shorthand */
UINT status;

(void)ssl;

if (nxCtx == NULL || nxCtx->nxUdpSocket == NULL) {
WOLFSSL_MSG("NetX Send NULL parameters");
return WOLFSSL_CBIO_ERR_GENERAL;
}

pool = nxCtx->nxUdpSocket->nx_udp_socket_ip_ptr->nx_ip_default_packet_pool;
status = nx_packet_allocate(pool, &packet, NX_UDP_PACKET,
nxCtx->nxWait);
if (status != NX_SUCCESS) {
WOLFSSL_MSG("NetX Send packet alloc error");
return WOLFSSL_CBIO_ERR_GENERAL;
}

status = nx_packet_data_append(packet, buf, sz, pool, nxCtx->nxWait);
if (status != NX_SUCCESS) {
nx_packet_release(packet);
WOLFSSL_MSG("NetX Send data append error");
return WOLFSSL_CBIO_ERR_GENERAL;
}

if (nxCtx->nxdIp.nxd_ip_version == NX_IP_VERSION_V4) {
Comment thread
dgarske marked this conversation as resolved.
status = nx_udp_socket_send(nxCtx->nxUdpSocket, packet,
nxCtx->nxdIp.nxd_ip_address.v4,
(UINT)nxCtx->nxPort);
}
else {
status = nxd_udp_socket_send(nxCtx->nxUdpSocket, packet,
&nxCtx->nxdIp, (UINT)nxCtx->nxPort);
}
if (status != NX_SUCCESS) {
nx_packet_release(packet);
WOLFSSL_MSG("NetX Send socket send error");
return WOLFSSL_CBIO_ERR_GENERAL;
}

return sz;
}

/* like set_fd, but for default NetX Duo UDP context */
void wolfSSL_SetIO_NetX_Dtls(WOLFSSL* ssl, NX_UDP_SOCKET* nxsocket,
Comment thread
dgarske marked this conversation as resolved.
Comment thread
dgarske marked this conversation as resolved.
NXD_ADDRESS nxdip, USHORT nxport,
ULONG waitoption)
{
if (ssl) {
ssl->nxCtx.nxSocket = nxSocket;
ssl->nxCtx.nxWait = waitOption;
ssl->nxCtx.nxUdpSocket = nxsocket;
ssl->nxCtx.nxdIp = nxdip;
ssl->nxCtx.nxPort = nxport;
ssl->nxCtx.nxWait = waitoption;
}
}
#endif /* WOLFSSL_DTLS && WOLFSSL_NETX_DUO */

#endif /* HAVE_NETX */

Expand Down
8 changes: 7 additions & 1 deletion wolfssl/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5711,10 +5711,16 @@ typedef struct DtlsMsg {

/* NETX I/O Callback default */
typedef struct NetX_Ctx {
NX_TCP_SOCKET* nxSocket; /* send/recv socket handle */
NX_TCP_SOCKET* nxTcpSocket; /* send/recv tcp socket handle */
NX_PACKET* nxPacket; /* incoming packet handle for short reads */
ULONG nxOffset; /* offset already read from nxPacket */
ULONG nxWait; /* wait option flag */
/* WOLFSSL_NETX_DUO: requires ThreadX NetX Duo (NXD_ADDRESS, nxd_udp_socket_send) */
#if defined(WOLFSSL_DTLS) && defined(WOLFSSL_NETX_DUO)
Comment thread
dgarske marked this conversation as resolved.
NX_UDP_SOCKET* nxUdpSocket; /* send/recv udp socket handle */
Comment thread
dgarske marked this conversation as resolved.
NXD_ADDRESS nxdIp; /* destination IP address for udp send */
USHORT nxPort; /* destination port for udp send */
#endif /* WOLFSSL_DTLS && WOLFSSL_NETX_DUO */
} NetX_Ctx;

#endif
Expand Down
Loading
Loading