Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
108 changes: 85 additions & 23 deletions examples/portfwd/portfwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@
#define INVALID_FWD_PORT 0
/* wolfSSH_worker() calls to wait for a want-reply tcpip-forward answer. */
#define MAX_REPLY_TRIES 100

#ifndef USE_WINDOWS_API
#define SOCKET_ERRNO errno
#define SOCKET_EINTR EINTR
#else
#define SOCKET_ERRNO WSAGetLastError()
#define SOCKET_EINTR WSAEINTR
#endif
static const char defaultFwdFromHost[] = "0.0.0.0";


Expand Down Expand Up @@ -259,6 +267,30 @@ typedef struct PortfwdState {
} PortfwdState;


/* Send all of buf, retrying on EINTR. Returns bufSz or -1. */
static int app_write_all(SOCKET_T fd, const byte* buf, word32 bufSz)
{
word32 off = 0;
int cnt;

while (off < bufSz) {
cnt = (int)send(fd, (const char*)buf + off, (int)(bufSz - off), 0);

if (cnt > 0) {
off += (word32)cnt;
}
else if (cnt < 0 && SOCKET_ERRNO == SOCKET_EINTR) {
continue;
}
else {
return -1;
}
}

return (int)bufSz;
}


/* Open a TCP connection to the pre-resolved forward target. Returns -1 on
* failure. Unlike tcp_socket(), nothing here exits the process: this runs
* inside a channel-open callback, where the right answer is to refuse the one
Expand Down Expand Up @@ -475,6 +507,7 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args)
int appFdSet = 0;
int appFdHalfClosed = 0;
int appEof = 0;
int sessionLost = 0;
int reverse = 0;
int fwdFromPortSet = 0;
PortfwdState fwdState;
Expand Down Expand Up @@ -829,6 +862,24 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args)
break;
}

/* Past an idle WANT_READ/WRITE or a refused open, any other
* return means the session is gone; a dead peer keeps sshFd
* readable, so looping would spin. */
if (ret == WS_FATAL_ERROR) {
int err = wolfSSH_get_error(ssh);
if (err == WS_WANT_READ || err == WS_WANT_WRITE ||
err == WS_CHANOPEN_FAILED)
ret = err;
}
if (ret != WS_SUCCESS && ret != WS_CHAN_RXD &&
Comment thread
ejohnstown marked this conversation as resolved.
Resolved
ret != WS_EXTDATA && ret != WS_WANT_READ &&
ret != WS_WANT_WRITE && ret != WS_WINDOW_FULL &&
ret != WS_REKEYING && ret != WS_EOF &&
ret != WS_CHANOPEN_FAILED) {
sessionLost = 1;
break;
}
Comment thread
ejohnstown marked this conversation as resolved.
Resolved

/* A refused open frees the channel and reports a fatal
* error, not a close, so the guard above does not run.
* Re-resolve by id rather than trust the pointer. */
Expand Down Expand Up @@ -857,7 +908,8 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args)
drained = wolfSSH_ChannelRead(fwdChannel, sshBuffer,
sshBufferSz);
if (drained > 0) {
if ((int)send(appFd, sshBuffer, drained, 0) != drained)
if (app_write_all(appFd, sshBuffer,
(word32)drained) < 0)
break;
}
} while (drained > 0);
Expand Down Expand Up @@ -890,8 +942,8 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args)
if (ret > 0) {
sshBufferUsed = (word32)ret;
if (appFd != -1) {
ret = (int)send(appFd, sshBuffer, sshBufferUsed, 0);
if (ret != (int)sshBufferUsed)
ret = app_write_all(appFd, sshBuffer, sshBufferUsed);
if (ret < 0)
break;
}
}
Expand Down Expand Up @@ -934,29 +986,39 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args)
break;
}

if (reverse) {
/* Best effort: the session is going away regardless, but a failure
* here means the peer's listener may outlive us. */
ret = wolfSSH_FwdRemoteCancel(ssh, fwdFromHost, fwdFromPort, 0);
if (ret != WS_SUCCESS)
printf("Couldn't cancel the remote port forward, ret = %d\n", ret);
if (sessionLost) {
/* No peer to say goodbye to; just free. */
printf("Lost the SSH session, ret = %d, error = %d\n",
ret, wolfSSH_get_error(ssh));
((func_args*)args)->return_code = 1;
}
else {
if (reverse) {
/* Best effort: the session is going away regardless, but a failure
* here means the peer's listener may outlive us. */
ret = wolfSSH_FwdRemoteCancel(ssh, fwdFromHost, fwdFromPort, 0);
if (ret != WS_SUCCESS)
printf("Couldn't cancel the remote port forward, "
"ret = %d\n", ret);
}

ret = wolfSSH_shutdown(ssh);
/* A queued write and a retired channel are both done as far as this
* teardown is concerned: the disconnect carries the write out. */
if (ret != WS_SUCCESS && ret != WS_WANT_WRITE && ret != WS_CHANNEL_CLOSED)
err_sys("Closing port forward stream failed.");

/* peer already hung up, just close */
if (wolfSSH_get_error(ssh) != WS_SOCKET_ERROR_E) {
ret = SendDisconnectAndDrain(ssh, sshFd);
if (ret == WS_WANT_READ || ret == WS_WANT_WRITE) {
printf("Gave up on a graceful shutdown, closing the socket\n");
ret = WS_SUCCESS;
ret = wolfSSH_shutdown(ssh);
/* A queued write and a retired channel are both done as far as this
* teardown is concerned: the disconnect carries the write out. */
if (ret != WS_SUCCESS && ret != WS_WANT_WRITE &&
ret != WS_CHANNEL_CLOSED)
err_sys("Closing port forward stream failed.");

/* peer already hung up, just close */
if (wolfSSH_get_error(ssh) != WS_SOCKET_ERROR_E) {
ret = SendDisconnectAndDrain(ssh, sshFd);
if (ret == WS_WANT_READ || ret == WS_WANT_WRITE) {
printf("Gave up on a graceful shutdown, closing the socket\n");
ret = WS_SUCCESS;
}
else if (ret != WS_SUCCESS)
err_sys("Sending the disconnect failed.");
}
else if (ret != WS_SUCCESS)
err_sys("Sending the disconnect failed.");
}

WCLOSESOCKET(sshFd);
Expand Down
30 changes: 25 additions & 5 deletions examples/tpmcertserver/tpmcertclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ int main(int argc, char* argv[])
if (ret == 0) {
int txSz = (int)XSTRLEN((char*)txt);
int sent = 0;
int rxSz;
int rxd = 0;
int n;

/* stream_send may transmit fewer bytes than requested; loop until the
Expand All @@ -180,14 +180,34 @@ int main(int argc, char* argv[])
sent += n;
}

/* stream_read may also return short; read all txSz bytes. */
while (ret == 0 && rxd < txSz) {
n = wolfSSH_stream_read(ssh, rxBuf + rxd, (word32)(txSz - rxd));
if (n > 0) {
rxd += n;
}
else if (n == WS_FATAL_ERROR
&& wolfSSH_get_error(ssh) == WS_REKEYING) {
/* stream_read does not receive mid-rekey; drive it. */
n = wolfSSH_worker(ssh, NULL);
if (n < 0 && n != WS_CHAN_RXD && n != WS_REKEYING) {
fprintf(stderr, "rekey failed: %d\n", n);
ret = -1;
}
}
else {
fprintf(stderr, "stream read failed: %d\n", n);
ret = -1;
}
}

if (ret == 0) {
rxSz = wolfSSH_stream_read(ssh, rxBuf, sizeof(rxBuf) - 1);
if (rxSz <= 0) {
fprintf(stderr, "stream read failed: %d\n", rxSz);
rxBuf[rxd] = 0;
if (XMEMCMP(rxBuf, txt, (size_t)txSz) != 0) {
fprintf(stderr, "echo does not match what was sent\n");
ret = -1;
}
else {
rxBuf[rxSz] = 0;
printf("Echo from server: %s", (char*)rxBuf);
}
}
Expand Down
21 changes: 10 additions & 11 deletions examples/tpmcertserver/tpmcertserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,17 @@ static int TpmCsMakeKeyAndCert(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
rc = wolfTPM2_CSR_Generate_ex(dev, key, subject, keyUsage,
ENCODING_TYPE_ASN1, certDer, (int)*certDerSz, sigType,
1, devId);
}

if (rc >= 0) {
/* wolfTPM2_CSR_Generate_ex returns the generated DER length on
* success; sanity-check it against the output buffer capacity
* before trusting it as the certificate size. */
if (rc == 0 || (word32)rc > *certDerSz) {
rc = BUFFER_E;
}
else {
*certDerSz = (word32)rc;
rc = 0;
/* Only this call returns a DER length; a positive rc from an
* earlier step is a TPM_RC failure. */
if (rc >= 0) {
if (rc == 0 || (word32)rc > *certDerSz) {
rc = BUFFER_E;
}
else {
*certDerSz = (word32)rc;
rc = 0;
}
}
}

Expand Down
Loading
Loading