Skip to content

Commit f7bf230

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 93ddf8f commit f7bf230

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

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) {
@@ -12526,6 +12538,11 @@ int SendKexDhGexRequest(WOLFSSH* ssh)
1252612538
if (ret == WS_SUCCESS)
1252712539
ret = wolfSSH_SendPacket(ssh);
1252812540

12541+
if (ret == WS_SUCCESS) {
12542+
WLOG_EXPECT_MSGID(MSGID_KEXDH_GEX_GROUP);
12543+
ssh->handshake->expectMsgId = MSGID_KEXDH_GEX_GROUP;
12544+
}
12545+
1252912546
WLOG(WS_LOG_DEBUG, "Leaving SendKexDhGexRequest(), ret = %d", ret);
1253012547
return ret;
1253112548
}
@@ -12614,6 +12631,7 @@ int SendKexDhInit(WOLFSSH* ssh)
1261412631
#endif
1261512632
int ret = WS_SUCCESS;
1261612633
byte msgId = MSGID_KEXDH_INIT;
12634+
byte expectMsgId = MSGID_KEXDH_REPLY;
1261712635
byte e[MAX_KEX_KEY_SZ+1]; /* plus 1 in case of padding. */
1261812636
word32 eSz = (word32)sizeof(e);
1261912637
byte ePad = 0;
@@ -12665,6 +12683,7 @@ int SendKexDhInit(WOLFSSH* ssh)
1266512683
generator = ssh->handshake->generator;
1266612684
generatorSz = ssh->handshake->generatorSz;
1266712685
msgId = MSGID_KEXDH_GEX_INIT;
12686+
expectMsgId = MSGID_KEXDH_GEX_REPLY;
1266812687
break;
1266912688
#endif
1267012689
#ifndef WOLFSSH_NO_ECDH_SHA2_NISTP256
@@ -12876,6 +12895,11 @@ int SendKexDhInit(WOLFSSH* ssh)
1287612895
if (ret == WS_SUCCESS)
1287712896
ret = wolfSSH_SendPacket(ssh);
1287812897

12898+
if (ret == WS_SUCCESS) {
12899+
WLOG_EXPECT_MSGID(expectMsgId);
12900+
ssh->handshake->expectMsgId = expectMsgId;
12901+
}
12902+
1287912903
WLOG(WS_LOG_DEBUG, "Leaving SendKexDhInit(), ret = %d", ret);
1288012904
return ret;
1288112905
}

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)