SSH-AGENT Touchup

1. Add function to request the channel ID for the last message received.
2. Changed the send channel functions to use the self channel ID rather
   than the peer's channel ID.
3. Modified client and echoserver to use the channel ID for the agent
   to send messages.
4. Modify client to receive the entire message from the agent before
   trying to relay it to the peer.
pull/269/head
John Safranek 2020-07-30 14:00:28 -07:00
parent 2be3f15106
commit bfa3d5717c
No known key found for this signature in database
GPG Key ID: 8CE817DE0D3CCB4A
5 changed files with 49 additions and 12 deletions

View File

@ -512,6 +512,14 @@ static THREAD_RET readInput(void* in)
}
#ifdef WOLFSSH_AGENT
static inline void ato32(const byte* c, word32* u32)
{
*u32 = (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3];
}
#endif
static THREAD_RET readPeer(void* in)
{
byte buf[80];
@ -555,18 +563,30 @@ static THREAD_RET readPeer(void* in)
if (ret == WS_CHAN_RXD) {
byte agentBuf[512];
int rxd, txd;
word32 channel = 0;
rxd = wolfSSH_ChannelIdRead(args->ssh, 1,
wolfSSH_GetLastRxId(args->ssh, &channel);
rxd = wolfSSH_ChannelIdRead(args->ssh, channel,
agentBuf, sizeof(agentBuf));
if (rxd > 0) {
if (rxd > 4) {
word32 msgSz = 0;
ato32(agentBuf, &msgSz);
if (msgSz > (word32)rxd - 4) {
rxd += wolfSSH_ChannelIdRead(args->ssh, channel,
agentBuf + rxd,
sizeof(agentBuf) - rxd);
}
txd = rxd;
rxd = sizeof(agentBuf);
ret = wolfSSH_AGENT_Relay(args->ssh,
agentBuf, (word32*)&txd,
agentBuf, (word32*)&rxd);
if (ret == WS_SUCCESS)
wolfSSH_ChannelIdSend(args->ssh, 1,
if (ret == WS_SUCCESS) {
ret = wolfSSH_ChannelIdSend(args->ssh, channel,
agentBuf, rxd);
}
}
WMEMSET(agentBuf, 0, sizeof(agentBuf));
continue;

View File

@ -562,6 +562,7 @@ static int shell_worker(thread_ctx_t* threadCtx)
BUF_T agent_buf;
int agentFd = -1;
int listenFd = threadCtx->agentCbCtx.listenFd;
word32 agentChannelId = 0;
#endif
#ifdef SHELL_DEBUG
@ -789,7 +790,8 @@ static int shell_worker(thread_ctx_t* threadCtx)
}
#ifdef WOLFSSH_AGENT
if (rc == WS_CHAN_RXD) {
cnt_r = wolfSSH_ChannelIdRead(ssh, 1,
wolfSSH_GetLastRxId(ssh, &agentChannelId);
cnt_r = wolfSSH_ChannelIdRead(ssh, agentChannelId,
(byte*)agent_buf.buf, cnt_r);
if (cnt_r <= 0)
break;
@ -839,7 +841,7 @@ static int shell_worker(thread_ctx_t* threadCtx)
#ifdef SHELL_DEBUG
buf_dump(agent_buf.buf+agent_buf.rdidx, cnt_r);
#endif
cnt_w = wolfSSH_ChannelIdSend(ssh, 1,
cnt_w = wolfSSH_ChannelIdSend(ssh, agentChannelId,
(byte*)agent_buf.buf + agent_buf.rdidx, cnt_r);
if (cnt_w > 0) {
agent_buf.rdidx += cnt_r;

View File

@ -8462,7 +8462,7 @@ int SendChannelClose(WOLFSSH* ssh, word32 peerChannelId)
}
int SendChannelData(WOLFSSH* ssh, word32 peerChannel,
int SendChannelData(WOLFSSH* ssh, word32 channelId,
byte* data, word32 dataSz)
{
byte* output;
@ -8486,9 +8486,9 @@ int SendChannelData(WOLFSSH* ssh, word32 peerChannel,
}
if (ret == WS_SUCCESS) {
channel = ChannelFind(ssh, peerChannel, WS_CHANNEL_ID_PEER);
channel = ChannelFind(ssh, channelId, WS_CHANNEL_ID_SELF);
if (channel == NULL) {
WLOG(WS_LOG_DEBUG, "Invalid peer channel");
WLOG(WS_LOG_DEBUG, "Invalid channel");
ret = WS_INVALID_CHANID;
}
}
@ -8552,7 +8552,7 @@ int SendChannelData(WOLFSSH* ssh, word32 peerChannel,
}
int SendChannelWindowAdjust(WOLFSSH* ssh, word32 peerChannel,
int SendChannelWindowAdjust(WOLFSSH* ssh, word32 channelId,
word32 bytesToAdd)
{
byte* output;
@ -8565,9 +8565,9 @@ int SendChannelWindowAdjust(WOLFSSH* ssh, word32 peerChannel,
if (ssh == NULL)
ret = WS_BAD_ARGUMENT;
channel = ChannelFind(ssh, peerChannel, WS_CHANNEL_ID_PEER);
channel = ChannelFind(ssh, channelId, WS_CHANNEL_ID_SELF);
if (channel == NULL) {
WLOG(WS_LOG_DEBUG, "Invalid peer channel");
WLOG(WS_LOG_DEBUG, "Invalid channel");
ret = WS_INVALID_CHANID;
}
if (ret == WS_SUCCESS)

View File

@ -1715,6 +1715,20 @@ int wolfSSH_worker(WOLFSSH* ssh, word32* channelId)
}
int wolfSSH_GetLastRxId(WOLFSSH* ssh, word32* channelId)
{
int ret = WS_SUCCESS;
if (ssh == NULL || channelId == NULL)
ret = WS_ERROR;
if (ret == WS_SUCCESS)
*channelId = ssh->lastRxId;
return ret;
}
#ifdef WOLFSSH_FWD
WOLFSSH_CHANNEL* wolfSSH_ChannelFwdNew(WOLFSSH* ssh,

View File

@ -63,6 +63,7 @@ WOLFSSH_API WOLFSSH* wolfSSH_new(WOLFSSH_CTX*);
WOLFSSH_API void wolfSSH_free(WOLFSSH*);
WOLFSSH_API int wolfSSH_worker(WOLFSSH*, word32*);
WOLFSSH_API int wolfSSH_GetLastRxId(WOLFSSH*, word32*);
WOLFSSH_API int wolfSSH_set_fd(WOLFSSH*, WS_SOCKET_T);
WOLFSSH_API WS_SOCKET_T wolfSSH_get_fd(const WOLFSSH*);