Skip to content

Commit ab17b01

Browse files
committed
Validate agent signature responses
Track the last agent message id, reject non-signature replies, reset the failure flag, and ensure the agent write completes before trusting the returned signature.
1 parent ec9ca4f commit ab17b01

2 files changed

Lines changed: 41 additions & 17 deletions

File tree

src/agent.c

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,8 @@ static int DoMessage(WOLFSSH_AGENT_CTX* agent,
13401340

13411341
if (agent == NULL)
13421342
ret = WS_SSH_NULL_E; /* WS_AGENT_NULL_E */
1343+
else
1344+
agent->lastMsgId = 0;
13431345

13441346
if (ret == WS_SUCCESS) {
13451347
if (buf == NULL || idx == NULL || len == 0)
@@ -1371,6 +1373,7 @@ static int DoMessage(WOLFSSH_AGENT_CTX* agent,
13711373

13721374
if (ret == WS_SUCCESS) {
13731375
msg = buf[begin++];
1376+
agent->lastMsgId = msg;
13741377
payloadIdx = 0;
13751378
switch (msg) {
13761379
case MSGID_AGENT_FAILURE:
@@ -1793,6 +1796,11 @@ int wolfSSH_AGENT_SignRequest(WOLFSSH* ssh,
17931796

17941797
if (ret == WS_SUCCESS) {
17951798
agent = ssh->agent;
1799+
agent->requestFailure = 0;
1800+
agent->requestSuccess = 0;
1801+
agent->msg = NULL;
1802+
agent->msgSz = 0;
1803+
agent->lastMsgId = 0;
17961804
if (ssh->ctx->agentCb)
17971805
ret = ssh->ctx->agentCb(WOLFSSH_AGENT_LOCAL_SETUP, ssh->agentCbCtx);
17981806
}
@@ -1801,11 +1809,16 @@ int wolfSSH_AGENT_SignRequest(WOLFSSH* ssh,
18011809
ret = SendSignRequest(agent, digest, digestSz,
18021810
keyBlob, keyBlobSz, flags);
18031811

1804-
if (ret == WS_SUCCESS)
1805-
ret = ssh->ctx->agentIoCb(WOLFSSH_AGENT_IO_WRITE,
1806-
agent->msg, agent->msgSz, ssh->agentCbCtx);
1812+
if (ret == WS_SUCCESS) {
1813+
int wrote;
18071814

1808-
if (ret > 0) ret = WS_SUCCESS;
1815+
wrote = ssh->ctx->agentIoCb(WOLFSSH_AGENT_IO_WRITE,
1816+
agent->msg, agent->msgSz, ssh->agentCbCtx);
1817+
if (wrote != (int)agent->msgSz) {
1818+
WLOG(WS_LOG_AGENT, "agent write incomplete");
1819+
ret = WS_AGENT_CXN_FAIL;
1820+
}
1821+
}
18091822

18101823
if (agent != NULL && agent->msg != NULL) {
18111824
WFREE(ssh->agent->msg, ssh->agent->heap, DYNTYPE_AGENT_BUFFER);
@@ -1818,21 +1831,32 @@ int wolfSSH_AGENT_SignRequest(WOLFSSH* ssh,
18181831
rxBuf, sizeof(rxBuf), ssh->agentCbCtx);
18191832
if (rxSz > 0) {
18201833
ret = DoMessage(ssh->agent, rxBuf, rxSz, &idx);
1821-
if (ssh->agent->requestFailure) {
1822-
ssh->agent->requestFailure = 0;
1823-
ret = WS_AGENT_NO_KEY_E;
1824-
}
1825-
else {
1826-
word32 maxSigSz = *sigSz;
1827-
1828-
if (ssh->agent->msgSz > maxSigSz) {
1834+
if (ret == WS_SUCCESS) {
1835+
if (ssh->agent->lastMsgId != MSGID_AGENT_SIGN_RESPONSE) {
18291836
WLOG(WS_LOG_AGENT,
1830-
"agent signature too large for caller buffer");
1831-
ret = WS_BUFFER_E;
1837+
"agent response was not a signature message");
1838+
ret = WS_AGENT_NO_KEY_E;
18321839
}
18331840
else {
1834-
WMEMCPY(sig, ssh->agent->msg, ssh->agent->msgSz);
1835-
*sigSz = ssh->agent->msgSz;
1841+
if (ssh->agent->requestFailure ||
1842+
ssh->agent->msg == NULL ||
1843+
ssh->agent->msgSz == 0) {
1844+
ssh->agent->requestFailure = 0;
1845+
ret = WS_AGENT_NO_KEY_E;
1846+
}
1847+
else {
1848+
word32 maxSigSz = *sigSz;
1849+
1850+
if (ssh->agent->msgSz > maxSigSz) {
1851+
WLOG(WS_LOG_AGENT,
1852+
"agent signature too large for caller buffer");
1853+
ret = WS_BUFFER_E;
1854+
}
1855+
else {
1856+
WMEMCPY(sig, ssh->agent->msg, ssh->agent->msgSz);
1857+
*sigSz = ssh->agent->msgSz;
1858+
}
1859+
}
18361860
}
18371861
}
18381862
}

wolfssh/agent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ struct WOLFSSH_AGENT_CTX {
140140
enum AgentStates state;
141141
int requestSuccess;
142142
int requestFailure;
143+
byte lastMsgId;
143144
};
144145
typedef struct WOLFSSH_AGENT_CTX WOLFSSH_AGENT_CTX;
145146

@@ -190,4 +191,3 @@ WOLFSSH_API int wolfSSH_AGENT_SignRequest(WOLFSSH*, const byte*, word32,
190191
#endif
191192

192193
#endif /* _WOLFSSH_AGENT_H_ */
193-

0 commit comments

Comments
 (0)