Skip to content

Commit b7b4ccf

Browse files
committed
Client Out Of Order Messaging Checking
1. Add macro for logging an expected message. 2. Add an expected message ID to the HandshakeInfo. 3. Add a message ID for "none (0)". 4. Add a check in IsMessageAllowedClient() for the expected message ID. Clear it if successful. 5. The KEXDH messages sent to the server have expected responses. Set them if sending the message is successful.
1 parent 194cd05 commit b7b4ccf

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/internal.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,18 @@ INLINE static int IsMessageAllowedServer(WOLFSSH *ssh, byte msg)
638638
#ifndef NO_WOLFSSH_CLIENT
639639
INLINE static int IsMessageAllowedClient(WOLFSSH *ssh, byte msg)
640640
{
641+
/* Is KEX complete? */
642+
if (ssh->connectState < CONNECT_KEYED && ssh->handshake) {
643+
/* If expecting a specific message, and didn't receive it, error. */
644+
if (ssh->handshake->expectMsgId != MSGID_NONE) {
645+
if (msg != ssh->handshake->expectMsgId) {
646+
WLOG(WS_LOG_DEBUG, "Message ID %u not the expected message %u",
647+
msg, ssh->handshake->expectMsgId);
648+
return 0;
649+
}
650+
ssh->handshake->expectMsgId = MSGID_NONE;
651+
}
652+
}
641653
/* Has client userauth started? */
642654
if (ssh->connectState < CONNECT_CLIENT_KEXDH_INIT_SENT) {
643655
if (msg >= MSGID_KEXDH_LIMIT) {
@@ -12523,6 +12535,11 @@ int SendKexDhGexRequest(WOLFSSH* ssh)
1252312535
if (ret == WS_SUCCESS)
1252412536
ret = wolfSSH_SendPacket(ssh);
1252512537

12538+
if (ret == WS_SUCCESS) {
12539+
WLOG_EXPECT_MSGID(MSGID_KEXDH_GEX_GROUP);
12540+
ssh->handshake->expectMsgId = MSGID_KEXDH_GEX_GROUP;
12541+
}
12542+
1252612543
WLOG(WS_LOG_DEBUG, "Leaving SendKexDhGexRequest(), ret = %d", ret);
1252712544
return ret;
1252812545
}
@@ -12611,6 +12628,7 @@ int SendKexDhInit(WOLFSSH* ssh)
1261112628
#endif
1261212629
int ret = WS_SUCCESS;
1261312630
byte msgId = MSGID_KEXDH_INIT;
12631+
byte expectMsgId = MSGID_KEXDH_REPLY;
1261412632
byte e[MAX_KEX_KEY_SZ+1]; /* plus 1 in case of padding. */
1261512633
word32 eSz = (word32)sizeof(e);
1261612634
byte ePad = 0;
@@ -12662,6 +12680,7 @@ int SendKexDhInit(WOLFSSH* ssh)
1266212680
generator = ssh->handshake->generator;
1266312681
generatorSz = ssh->handshake->generatorSz;
1266412682
msgId = MSGID_KEXDH_GEX_INIT;
12683+
expectMsgId = MSGID_KEXDH_GEX_REPLY;
1266512684
break;
1266612685
#endif
1266712686
#ifndef WOLFSSH_NO_ECDH_SHA2_NISTP256
@@ -12873,6 +12892,11 @@ int SendKexDhInit(WOLFSSH* ssh)
1287312892
if (ret == WS_SUCCESS)
1287412893
ret = wolfSSH_SendPacket(ssh);
1287512894

12895+
if (ret == WS_SUCCESS) {
12896+
WLOG_EXPECT_MSGID(expectMsgId);
12897+
ssh->handshake->expectMsgId = expectMsgId;
12898+
}
12899+
1287612900
WLOG(WS_LOG_DEBUG, "Leaving SendKexDhInit(), ret = %d", ret);
1287712901
return ret;
1287812902
}

wolfssh/internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ typedef struct Keys {
605605

606606

607607
typedef struct HandshakeInfo {
608+
byte expectMsgId;
608609
byte kexId;
609610
byte kexIdGuess;
610611
byte kexHashId;
@@ -1178,6 +1179,7 @@ enum ProcessReplyStates {
11781179

11791180

11801181
enum WS_MessageIds {
1182+
MSGID_NONE = 0,
11811183
MSGID_DISCONNECT = 1,
11821184
MSGID_IGNORE = 2,
11831185
MSGID_UNIMPLEMENTED = 3,

wolfssh/log.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ WOLFSSH_API void wolfSSH_Log(enum wolfSSH_LogLevel,
7676
if (wolfSSH_LogEnabled()) \
7777
wolfSSH_Log(__VA_ARGS__); \
7878
} while (0)
79+
#define WLOG_EXPECT_MSGID(x) WLOG(WS_LOG_DEBUG, "Expecting message %d", (x))
7980

8081

8182
#ifdef __cplusplus

0 commit comments

Comments
 (0)