Skip to content

Commit f2b21dc

Browse files
committed
WIP
1 parent 671fc52 commit f2b21dc

File tree

2 files changed

+99
-36
lines changed

2 files changed

+99
-36
lines changed

src/internal.c

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -598,34 +598,43 @@ static void HandshakeInfoFree(HandshakeInfo* hs, void* heap)
598598
#ifndef NO_WOLFSSH_SERVER
599599
INLINE static int IsMessageAllowedServer(WOLFSSH *ssh, byte msg)
600600
{
601+
/* Transport Layer Generic messages are always allowed. */
602+
if (MSGIDLIMIT_TRANS_GEN(msg)) {
603+
return 1;
604+
}
605+
601606
/* Has client userauth started? */
607+
/* Allows the server to receive up to KEXDH GEX Request during KEX. */
602608
if (ssh->acceptState < ACCEPT_KEYED) {
603-
if (msg > MSGID_KEXDH_LIMIT) {
609+
if (msg > MSGID_KEXDH_GEX_REQUEST) {
604610
return 0;
605611
}
606612
}
607613
/* Is server userauth complete? */
608614
if (ssh->acceptState < ACCEPT_SERVER_USERAUTH_SENT) {
615+
/* The server should only receive the user auth request message,
616+
* it should not accept the other user auth messages, it sends
617+
* them. (>50) */
609618
/* Explicitly check for messages not allowed before user
610619
* authentication has comleted. */
611-
if (msg >= MSGID_USERAUTH_LIMIT) {
612-
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by server "
613-
"before user authentication is complete", msg);
620+
if (MSGIDLIMIT_POST_USERAUTH(msg)) {
621+
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by %s %s",
622+
msg, "server", "before user authentication is complete");
614623
return 0;
615624
}
616625
/* Explicitly check for the user authentication messages that
617626
* only the server sends, it shouldn't receive them. */
618-
if ((msg > MSGID_USERAUTH_RESTRICT) &&
627+
if ((msg > MSGID_USERAUTH_REQUEST) &&
619628
(msg != MSGID_USERAUTH_INFO_RESPONSE)) {
620-
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by server "
621-
"during user authentication", msg);
629+
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by %s %s",
630+
msg, "server", "during user authentication");
622631
return 0;
623632
}
624633
}
625634
else {
626-
if (msg >= MSGID_USERAUTH_RESTRICT && msg < MSGID_USERAUTH_LIMIT) {
627-
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by server "
628-
"after user authentication", msg);
635+
if (msg >= MSGID_USERAUTH_REQUEST && msg < MSGID_GLOBAL_REQUEST) {
636+
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by %s %s",
637+
msg, "server", "after user authentication");
629638
return 0;
630639
}
631640
}
@@ -638,6 +647,19 @@ INLINE static int IsMessageAllowedServer(WOLFSSH *ssh, byte msg)
638647
#ifndef NO_WOLFSSH_CLIENT
639648
INLINE static int IsMessageAllowedClient(WOLFSSH *ssh, byte msg)
640649
{
650+
/* Transport Layer Generic messages are always allowed. */
651+
if (MSGIDLIMIT_TRANS_GEN(msg)) {
652+
return 1;
653+
}
654+
655+
/* The client should only send the user auth request message
656+
* (50), it should not accept it. The server should only receive
657+
* the user auth request message, it should not accept the other
658+
* user auth messages, it sends them. (>50) */
659+
if (msg == MSGID_USERAUTH_REQUEST) {
660+
return 0;
661+
}
662+
641663
/* Is KEX complete? */
642664
if (ssh->connectState < CONNECT_KEYED && ssh->handshake) {
643665
/* If expecting a specific message, and didn't receive it, error. */
@@ -648,35 +670,37 @@ INLINE static int IsMessageAllowedClient(WOLFSSH *ssh, byte msg)
648670
return 0;
649671
}
650672
ssh->handshake->expectMsgId = MSGID_NONE;
673+
return 1;
651674
}
652675
}
653676
/* Has client userauth started? */
654677
if (ssh->connectState < CONNECT_CLIENT_KEXDH_INIT_SENT) {
655-
if (msg >= MSGID_KEXDH_LIMIT) {
678+
if (msg >= MSGID_KEXDH_GEX_REQUEST) {
656679
return 0;
657680
}
658681
}
659682
/* Is client userauth complete? */
660683
if (ssh->connectState < CONNECT_SERVER_USERAUTH_ACCEPT_DONE) {
661-
/* Explicitly check for messages not allowed before user
662-
* authentication has comleted. */
663-
if (msg >= MSGID_USERAUTH_LIMIT) {
664-
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by client "
665-
"before user authentication is complete", msg);
684+
/* The endpoints should not allow message IDs greater than or
685+
* equal to msgid 80 before user authentication is complete.
686+
* Per RFC 4252 section 6. */
687+
if (MSGIDLIMIT_POST_USERAUTH(msg)) {
688+
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by %s %s",
689+
msg, "client", "before user authentication is complete");
666690
return 0;
667691
}
668-
/* Explicitly check for the user authentication message that
669-
* only the client sends, it shouldn't receive it. */
670-
if (msg == MSGID_USERAUTH_RESTRICT) {
671-
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by client "
672-
"during user authentication", msg);
692+
/* Explicitly check for the user authentication request message.
693+
* The client only sends the message, it shouldn't receive it. */
694+
if (msg == MSGID_USERAUTH_REQUEST) {
695+
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by %s %s",
696+
msg, "client", "during user authentication");
673697
return 0;
674698
}
675699
}
676700
else {
677-
if (msg >= MSGID_USERAUTH_RESTRICT && msg < MSGID_USERAUTH_LIMIT) {
678-
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by client "
679-
"after user authentication", msg);
701+
if (MSGIDLIMIT_AUTH(msg)) {
702+
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by %s %s",
703+
msg, "client", "after user authentication");
680704
return 0;
681705
}
682706
}

wolfssh/internal.h

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,7 @@ enum ProcessReplyStates {
11801180

11811181
enum WS_MessageIds {
11821182
MSGID_NONE = 0,
1183+
11831184
MSGID_DISCONNECT = 1,
11841185
MSGID_IGNORE = 2,
11851186
MSGID_UNIMPLEMENTED = 3,
@@ -1235,19 +1236,57 @@ enum WS_MessageIds {
12351236
};
12361237

12371238

1238-
/* Allows the server to receive up to KEXDH GEX Request during KEX. */
1239-
#define MSGID_KEXDH_LIMIT MSGID_KEXDH_GEX_REQUEST
1240-
1241-
/* The endpoints should not allow message IDs greater than or
1242-
* equal to msgid 80 before user authentication is complete.
1243-
* Per RFC 4252 section 6. */
1244-
#define MSGID_USERAUTH_LIMIT 80
1239+
/* The following message ID ranges are described in RFC 5251, section 7. */
1240+
enum WS_MessageIdLimits {
1241+
/* Transport Layer Protocol: */
1242+
MSGIDLIMIT_TRANS_MIN = 1,
1243+
MSGIDLIMIT_TRANS_GEN_MIN = 1,
1244+
MSGIDLIMIT_TRANS_GEN_MAX = 19,
1245+
MSGIDLIMIT_TRANS_ALGO_MIN = 20,
1246+
MSGIDLIMIT_TRANS_ALGO_MAX = 29,
1247+
MSGIDLIMIT_TRANS_KEX_MIN = 30,
1248+
MSGIDLIMIT_TRANS_KEX_MAX = 49,
1249+
MSGIDLIMIT_TRANS_MAX = 49,
1250+
/* User Authentication Protocol: */
1251+
MSGIDLIMIT_AUTH_MIN = 50,
1252+
MSGIDLIMIT_AUTH_GEN_MIN = 50,
1253+
MSGIDLIMIT_AUTH_GEN_MAX = 59,
1254+
MSGIDLIMIT_AUTH_METH_MIN = 60,
1255+
MSGIDLIMIT_AUTH_METH_MAX = 79,
1256+
MSGIDLIMIT_AUTH_MAX = 79,
1257+
/* Connection Protocol: */
1258+
MSGIDLIMIT_CONN_MIN = 80,
1259+
MSGIDLIMIT_CONN_GEN_MIN = 80,
1260+
MSGIDLIMIT_CONN_GEN_MAX = 89,
1261+
MSGIDLIMIT_CONN_CHAN_MIN = 90,
1262+
MSGIDLIMIT_CONN_CHAN_MAX = 127,
1263+
MSGIDLIMIT_CONN_MAX = 127,
1264+
/* Reserved For Client Protocols: */
1265+
MSGIDLIMIT_RESERVED_MIN = 128,
1266+
MSGIDLIMIT_RESERVED_MAX = 191,
1267+
/* Local Extensions: */
1268+
MSGIDLIMIT_EXTENDED_MIN = 192,
1269+
MSGIDLIMIT_EXTENDED_MAX = 255,
1270+
};
12451271

1246-
/* The client should only send the user auth request message
1247-
* (50), it should not accept it. The server should only receive
1248-
* the user auth request message, it should not accept the other
1249-
* user auth messages, it sends them. (>50) */
1250-
#define MSGID_USERAUTH_RESTRICT 50
1272+
/* Message ID bounds checking. */
1273+
#define MSGIDLIMIT_BOUND(x,y,z) ((x) >= (y) && (x) <= (z))
1274+
#define MSGIDLIMIT_COMP(x,name) \
1275+
MSGIDLIMIT_BOUND((x),MSGIDLIMIT_##name##_MIN,MSGIDLIMIT_##name##_MAX)
1276+
#define MSGIDLIMIT_TRANS(x) MSGIDLIMIT_COMP((x),TRANS)
1277+
#define MSGIDLIMIT_TRANS_GEN(x) MSGIDLIMIT_COMP((x),TRANS_GEN)
1278+
#define MSGIDLIMIT_TRANS_ALGO(x) MSGIDLIMIT_COMP((x),TRANS_ALGO)
1279+
#define MSGIDLIMIT_TRANS_KEX(x) MSGIDLIMIT_COMP((x),TRANS_KEX)
1280+
#define MSGIDLIMIT_AUTH(x) MSGIDLIMIT_COMP((x),AUTH)
1281+
#define MSGIDLIMIT_AUTH_GEN(x) MSGIDLIMIT_COMP((x),AUTH_GEN)
1282+
#define MSGIDLIMIT_AUTH_METH(x) MSGIDLIMIT_COMP((x),AUTH_METH)
1283+
#define MSGIDLIMIT_CONN(x) MSGIDLIMIT_COMP((x),CONN)
1284+
#define MSGIDLIMIT_CONN_GEN(x) MSGIDLIMIT_COMP((x),CONN_GEN)
1285+
#define MSGIDLIMIT_CONN_CHAN(x) MSGIDLIMIT_COMP((x),CONN_CHAN)
1286+
#define MSGIDLIMIT_RESERVED(x) MSGIDLIMIT_COMP((x),RESERVED)
1287+
#define MSGIDLIMIT_EXTENDED(x) MSGIDLIMIT_COMP((x),EXTENDED)
1288+
#define MSGIDLIMIT_POST_USERAUTH(x) \
1289+
MSGIDLIMIT_BOUND((x),MSGIDLIMIT_CONN_MIN,MSGIDLIMIT_EXTENDED_MAX)
12511290

12521291

12531292
#define CHANNEL_EXTENDED_DATA_STDERR WOLFSSH_EXT_DATA_STDERR

0 commit comments

Comments
 (0)