@@ -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,18 +1831,41 @@ 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- WMEMCPY (sig , ssh -> agent -> msg , ssh -> agent -> msgSz );
1827- * sigSz = ssh -> agent -> msgSz ;
1834+ if (ret == WS_SUCCESS ) {
1835+ if (ssh -> agent -> lastMsgId != MSGID_AGENT_SIGN_RESPONSE ) {
1836+ WLOG (WS_LOG_AGENT ,
1837+ "agent response was not a signature message" );
1838+ ret = WS_AGENT_NO_KEY_E ;
1839+ }
1840+ else {
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+ }
1860+ }
18281861 }
18291862 }
18301863 else ret = WS_AGENT_NO_KEY_E ;
18311864 }
18321865
1866+ if (ret != WS_SUCCESS && sigSz != NULL )
1867+ * sigSz = 0 ;
1868+
18331869 if (agent != NULL ) {
18341870 agent -> msg = NULL ;
18351871 agent -> msgSz = 0 ;
0 commit comments