diff --git a/src/internal.c b/src/internal.c index 13c02134..6aa8cc8c 100644 --- a/src/internal.c +++ b/src/internal.c @@ -2802,7 +2802,7 @@ static INLINE int VerifyMac(WOLFSSH* ssh, const uint8_t* in, uint32_t inSz, } -int ProcessReply(WOLFSSH* ssh) +int DoReceive(WOLFSSH* ssh) { int ret = WS_FATAL_ERROR; int verifyResult; @@ -2904,7 +2904,7 @@ int ProcessReply(WOLFSSH* ssh) int ProcessClientVersion(WOLFSSH* ssh) { int ret; - uint32_t clientIdSz; + uint32_t idSz; if ( (ret = GetInputText(ssh)) < 0) { WLOG(WS_LOG_DEBUG, "get input text failed"); @@ -2921,20 +2921,32 @@ int ProcessClientVersion(WOLFSSH* ssh) return WS_VERSION_E; } - clientIdSz = ssh->inputBuffer.length - SSH_PROTO_EOL_SZ; + idSz = ssh->inputBuffer.length - SSH_PROTO_EOL_SZ; - ssh->clientId = (uint8_t*)WMALLOC(clientIdSz + LENGTH_SZ, + ssh->clientId = (uint8_t*)WMALLOC(idSz + LENGTH_SZ, ssh->ctx->heap, DYNTYPE_STRING); if (ssh->clientId == NULL) ret = WS_MEMORY_E; else { - c32toa(clientIdSz, ssh->clientId); - WMEMCPY(ssh->clientId + LENGTH_SZ, ssh->inputBuffer.buffer, clientIdSz); - ssh->clientIdSz = clientIdSz + LENGTH_SZ; + c32toa(idSz, ssh->clientId); + WMEMCPY(ssh->clientId + LENGTH_SZ, ssh->inputBuffer.buffer, idSz); + ssh->clientIdSz = idSz + LENGTH_SZ; ret = wc_ShaUpdate(&ssh->handshake->hash, - ssh->clientId, clientIdSz + LENGTH_SZ); + ssh->clientId, idSz + LENGTH_SZ); } + if (ret == WS_SUCCESS) { + uint8_t idSzFlat[LENGTH_SZ]; + + idSz = (uint32_t)WSTRLEN(sshIdStr) - SSH_PROTO_EOL_SZ; + c32toa(idSz, idSzFlat); + ret = wc_ShaUpdate(&ssh->handshake->hash, idSzFlat, LENGTH_SZ); + } + + if (ret == WS_SUCCESS) + ret = wc_ShaUpdate(&ssh->handshake->hash, + (const uint8_t*)sshIdStr, idSz); + ssh->inputBuffer.idx += ssh->inputBuffer.length; return ret; @@ -2955,18 +2967,6 @@ int SendServerVersion(WOLFSSH* ssh) ret = SendText(ssh, sshIdStr, sshIdStrSz); } - if (ret == WS_SUCCESS) { - uint8_t sshIdStrSzFlat[LENGTH_SZ]; - - sshIdStrSz -= SSH_PROTO_EOL_SZ; - c32toa(sshIdStrSz, sshIdStrSzFlat); - ret = wc_ShaUpdate(&ssh->handshake->hash, sshIdStrSzFlat, LENGTH_SZ); - } - - if (ret == WS_SUCCESS) - ret = wc_ShaUpdate(&ssh->handshake->hash, - (const uint8_t*)sshIdStr, sshIdStrSz); - return ret; } diff --git a/src/ssh.c b/src/ssh.c index 183851bc..642a0cac 100644 --- a/src/ssh.c +++ b/src/ssh.c @@ -250,6 +250,15 @@ int wolfSSH_accept(WOLFSSH* ssh) switch (ssh->acceptState) { case ACCEPT_BEGIN: + if ( (ssh->error = SendServerVersion(ssh)) < WS_SUCCESS) { + WLOG(WS_LOG_DEBUG, acceptError, + "CLIENT_VERSION_DONE", ssh->error); + return WS_FATAL_ERROR; + } + ssh->acceptState = ACCEPT_SERVER_VERSION_SENT; + WLOG(WS_LOG_DEBUG, acceptState, "SERVER_VERSION_SENT"); + + case ACCEPT_SERVER_VERSION_SENT: while (ssh->clientState < CLIENT_VERSION_DONE) { if ( (ssh->error = ProcessClientVersion(ssh)) < WS_SUCCESS) { WLOG(WS_LOG_DEBUG, acceptError, "BEGIN", ssh->error); @@ -260,17 +269,8 @@ int wolfSSH_accept(WOLFSSH* ssh) WLOG(WS_LOG_DEBUG, acceptState, "CLIENT_VERSION_DONE"); case ACCEPT_CLIENT_VERSION_DONE: - if ( (ssh->error = SendServerVersion(ssh)) < WS_SUCCESS) { - WLOG(WS_LOG_DEBUG, acceptError, - "CLIENT_VERSION_DONE", ssh->error); - return WS_FATAL_ERROR; - } - ssh->acceptState = ACCEPT_SERVER_VERSION_SENT; - WLOG(WS_LOG_DEBUG, acceptState, "SERVER_VERSION_SENT"); - - case ACCEPT_SERVER_VERSION_SENT: while (ssh->keyingState < KEYING_KEYED) { - if ( (ssh->error = ProcessReply(ssh)) < WS_SUCCESS) { + if ( (ssh->error = DoReceive(ssh)) < WS_SUCCESS) { WLOG(WS_LOG_DEBUG, acceptError, "SERVER_VERSION_SENT", ssh->error); return WS_FATAL_ERROR; @@ -281,7 +281,7 @@ int wolfSSH_accept(WOLFSSH* ssh) case ACCEPT_KEYED: while (ssh->clientState < CLIENT_USERAUTH_REQUEST_DONE) { - if ( (ssh->error = ProcessReply(ssh)) < 0) { + if ( (ssh->error = DoReceive(ssh)) < 0) { WLOG(WS_LOG_DEBUG, acceptError, "KEYED", ssh->error); return WS_FATAL_ERROR; @@ -302,7 +302,7 @@ int wolfSSH_accept(WOLFSSH* ssh) case ACCEPT_SERVER_USERAUTH_ACCEPT_SENT: while (ssh->clientState < CLIENT_USERAUTH_DONE) { - if ( (ssh->error = ProcessReply(ssh)) < 0) { + if ( (ssh->error = DoReceive(ssh)) < 0) { WLOG(WS_LOG_DEBUG, acceptError, "SERVER_USERAUTH_ACCEPT_SENT", ssh->error); return WS_FATAL_ERROR; @@ -322,7 +322,7 @@ int wolfSSH_accept(WOLFSSH* ssh) case ACCEPT_SERVER_USERAUTH_SENT: while (ssh->clientState < CLIENT_DONE) { - if ( (ssh->error = ProcessReply(ssh)) < 0) { + if ( (ssh->error = DoReceive(ssh)) < 0) { WLOG(WS_LOG_DEBUG, acceptError, "SERVER_USERAUTH_SENT", ssh->error); return WS_FATAL_ERROR; @@ -373,7 +373,7 @@ int wolfSSH_stream_read(WOLFSSH* ssh, uint8_t* buf, uint32_t bufSz) inputBuffer = &ssh->channelList->inputBuffer; while (inputBuffer->length - inputBuffer->idx == 0) { - int ret = ProcessReply(ssh); + int ret = DoReceive(ssh); if (ret < 0) { WLOG(WS_LOG_DEBUG, "Leaving wolfSSH_stream_read(), ret = %d", ret); return ret; diff --git a/wolfssh/internal.h b/wolfssh/internal.h index d9e6155c..7d690173 100644 --- a/wolfssh/internal.h +++ b/wolfssh/internal.h @@ -297,7 +297,7 @@ WOLFSSH_LOCAL int wsEmbedSend(WOLFSSH*, void*, uint32_t, void*); #endif /* WOLFSSH_USER_IO */ -WOLFSSH_LOCAL int ProcessReply(WOLFSSH*); +WOLFSSH_LOCAL int DoReceive(WOLFSSH*); WOLFSSH_LOCAL int ProcessClientVersion(WOLFSSH*); WOLFSSH_LOCAL int SendServerVersion(WOLFSSH*); WOLFSSH_LOCAL int SendKexInit(WOLFSSH*);