Skip to content

Commit 15d94ff

Browse files
sanity checks on message types during rekey
1 parent 85bc7ba commit 15d94ff

2 files changed

Lines changed: 56 additions & 4 deletions

File tree

src/internal.c

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,40 @@ static void HandshakeInfoFree(HandshakeInfo* hs, void* heap)
584584
}
585585

586586

587+
/* RFC 4253 section 7.1, Once having sent SSH_MSG_KEXINIT the only messages
588+
* that can be sent are 1-19 (except SSH_MSG_SERVICE_REQUEST and
589+
* SSH_MSG_SERVICE_ACCEPT), 20-29 (except SSH_MSG_KEXINIT again), and 30-49
590+
*/
591+
INLINE static int IsMessageAllowedKeying(WOLFSSH *ssh, byte msg)
592+
{
593+
if (ssh->isKeying == 0) {
594+
return 1;
595+
}
596+
597+
/* case of servie request or accept in 1-19 */
598+
if (msg == MSGID_SERVICE_REQUEST || msg == MSGID_SERVICE_ACCEPT) {
599+
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by during rekeying", msg);
600+
ssh->error = WS_REKEYING;
601+
return 0;
602+
}
603+
604+
/* case of resending SSH_MSG_KEXINIT */
605+
if (msg == MSGID_KEXINIT) {
606+
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by during rekeying", msg);
607+
ssh->error = WS_REKEYING;
608+
return 0;
609+
}
610+
611+
/* case where message id greater than 49 */
612+
if (msg >= MSGID_USERAUTH_REQUEST) {
613+
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by during rekeying", msg);
614+
ssh->error = WS_REKEYING;
615+
return 0;
616+
}
617+
return 1;
618+
}
619+
620+
587621
#ifndef NO_WOLFSSH_SERVER
588622
INLINE static int IsMessageAllowedServer(WOLFSSH *ssh, byte msg)
589623
{
@@ -662,8 +696,12 @@ INLINE static int IsMessageAllowedClient(WOLFSSH *ssh, byte msg)
662696
#endif /* NO_WOLFSSH_CLIENT */
663697

664698

665-
INLINE static int IsMessageAllowed(WOLFSSH *ssh, byte msg)
699+
INLINE static int IsMessageAllowed(WOLFSSH *ssh, byte msg, byte state)
666700
{
701+
if (state == WS_MSG_SEND && !IsMessageAllowedKeying(ssh, msg)) {
702+
return 0;
703+
}
704+
667705
#ifndef NO_WOLFSSH_SERVER
668706
if (ssh->ctx->side == WOLFSSH_ENDPOINT_SERVER) {
669707
return IsMessageAllowedServer(ssh, msg);
@@ -5808,7 +5846,6 @@ static int DoNewKeys(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
58085846
HandshakeInfoFree(ssh->handshake, ssh->ctx->heap);
58095847
ssh->handshake = NULL;
58105848
WLOG(WS_LOG_DEBUG, "Keying completed");
5811-
58125849
if (ssh->ctx->keyingCompletionCb)
58135850
ssh->ctx->keyingCompletionCb(ssh->keyingCompletionCtx);
58145851
}
@@ -9178,7 +9215,7 @@ static int DoPacket(WOLFSSH* ssh, byte* bufferConsumed)
91789215
return WS_OVERFLOW_E;
91799216
}
91809217

9181-
if (!IsMessageAllowed(ssh, msg)) {
9218+
if (!IsMessageAllowed(ssh, msg, WS_MSG_RECV)) {
91829219
return WS_MSGID_NOT_ALLOWED_E;
91839220
}
91849221

@@ -15425,6 +15462,12 @@ int SendChannelEof(WOLFSSH* ssh, word32 peerChannelId)
1542515462
if (ssh == NULL)
1542615463
ret = WS_BAD_ARGUMENT;
1542715464

15465+
if (ret == WS_SUCCESS) {
15466+
if (!IsMessageAllowed(ssh, MSGID_CHANNEL_EOF, WS_MSG_SEND)) {
15467+
ret = WS_MSGID_NOT_ALLOWED_E;
15468+
}
15469+
}
15470+
1542815471
if (ret == WS_SUCCESS) {
1542915472
channel = ChannelFind(ssh, peerChannelId, WS_CHANNEL_ID_PEER);
1543015473
if (channel == NULL)
@@ -15853,6 +15896,12 @@ int SendChannelWindowAdjust(WOLFSSH* ssh, word32 channelId,
1585315896
if (ssh == NULL)
1585415897
ret = WS_BAD_ARGUMENT;
1585515898

15899+
if (ret == WS_SUCCESS) {
15900+
if (!IsMessageAllowed(ssh, MSGID_CHANNEL_WINDOW_ADJUST, WS_MSG_SEND)) {
15901+
ret = WS_MSGID_NOT_ALLOWED_E;
15902+
}
15903+
}
15904+
1585615905
channel = ChannelFind(ssh, channelId, WS_CHANNEL_ID_SELF);
1585715906
if (channel == NULL) {
1585815907
WLOG(WS_LOG_DEBUG, "Invalid channel");

wolfssh/internal.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,10 @@ enum WS_MessageIds {
12051205

12061206
#define CHANNEL_EXTENDED_DATA_STDERR WOLFSSH_EXT_DATA_STDERR
12071207

1208+
/* Used when checking IsMessageAllowed() to determine if createing and sending
1209+
* the message or receiving the message is allowed */
1210+
#define WS_MSG_SEND 1
1211+
#define WS_MSG_RECV 2
12081212

12091213
/* dynamic memory types */
12101214
enum WS_DynamicTypes {
@@ -1398,4 +1402,3 @@ enum TerminalModes {
13981402
#endif
13991403

14001404
#endif /* _WOLFSSH_INTERNAL_H_ */
1401-

0 commit comments

Comments
 (0)