Skip to content

Commit 5fa6c0f

Browse files
sanity checks on message types during rekey
1 parent 89e25c8 commit 5fa6c0f

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
@@ -595,6 +595,40 @@ static void HandshakeInfoFree(HandshakeInfo* hs, void* heap)
595595
}
596596

597597

598+
/* RFC 4253 section 7.1, Once having sent SSH_MSG_KEXINIT the only messages
599+
* that can be sent are 1-19 (except SSH_MSG_SERVICE_REQUEST and
600+
* SSH_MSG_SERVICE_ACCEPT), 20-29 (except SSH_MSG_KEXINIT again), and 30-49
601+
*/
602+
INLINE static int IsMessageAllowedKeying(WOLFSSH *ssh, byte msg)
603+
{
604+
if (ssh->isKeying == 0) {
605+
return 1;
606+
}
607+
608+
/* case of servie request or accept in 1-19 */
609+
if (msg == MSGID_SERVICE_REQUEST || msg == MSGID_SERVICE_ACCEPT) {
610+
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by during rekeying", msg);
611+
ssh->error = WS_REKEYING;
612+
return 0;
613+
}
614+
615+
/* case of resending SSH_MSG_KEXINIT */
616+
if (msg == MSGID_KEXINIT) {
617+
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by during rekeying", msg);
618+
ssh->error = WS_REKEYING;
619+
return 0;
620+
}
621+
622+
/* case where message id greater than 49 */
623+
if (msg >= MSGID_USERAUTH_REQUEST) {
624+
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by during rekeying", msg);
625+
ssh->error = WS_REKEYING;
626+
return 0;
627+
}
628+
return 1;
629+
}
630+
631+
598632
#ifndef NO_WOLFSSH_SERVER
599633
INLINE static int IsMessageAllowedServer(WOLFSSH *ssh, byte msg)
600634
{
@@ -673,8 +707,12 @@ INLINE static int IsMessageAllowedClient(WOLFSSH *ssh, byte msg)
673707
#endif /* NO_WOLFSSH_CLIENT */
674708

675709

676-
INLINE static int IsMessageAllowed(WOLFSSH *ssh, byte msg)
710+
INLINE static int IsMessageAllowed(WOLFSSH *ssh, byte msg, byte state)
677711
{
712+
if (state == WS_MSG_SEND && !IsMessageAllowedKeying(ssh, msg)) {
713+
return 0;
714+
}
715+
678716
#ifndef NO_WOLFSSH_SERVER
679717
if (ssh->ctx->side == WOLFSSH_ENDPOINT_SERVER) {
680718
return IsMessageAllowedServer(ssh, msg);
@@ -5905,7 +5943,6 @@ static int DoNewKeys(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
59055943
HandshakeInfoFree(ssh->handshake, ssh->ctx->heap);
59065944
ssh->handshake = NULL;
59075945
WLOG(WS_LOG_DEBUG, "Keying completed");
5908-
59095946
if (ssh->ctx->keyingCompletionCb)
59105947
ssh->ctx->keyingCompletionCb(ssh->keyingCompletionCtx);
59115948
}
@@ -9309,7 +9346,7 @@ static int DoPacket(WOLFSSH* ssh, byte* bufferConsumed)
93099346
return WS_OVERFLOW_E;
93109347
}
93119348

9312-
if (!IsMessageAllowed(ssh, msg)) {
9349+
if (!IsMessageAllowed(ssh, msg, WS_MSG_RECV)) {
93139350
return WS_MSGID_NOT_ALLOWED_E;
93149351
}
93159352

@@ -15649,6 +15686,12 @@ int SendChannelEof(WOLFSSH* ssh, word32 peerChannelId)
1564915686
if (ssh == NULL)
1565015687
ret = WS_BAD_ARGUMENT;
1565115688

15689+
if (ret == WS_SUCCESS) {
15690+
if (!IsMessageAllowed(ssh, MSGID_CHANNEL_EOF, WS_MSG_SEND)) {
15691+
ret = WS_MSGID_NOT_ALLOWED_E;
15692+
}
15693+
}
15694+
1565215695
if (ret == WS_SUCCESS) {
1565315696
channel = ChannelFind(ssh, peerChannelId, WS_CHANNEL_ID_PEER);
1565415697
if (channel == NULL)
@@ -16077,6 +16120,12 @@ int SendChannelWindowAdjust(WOLFSSH* ssh, word32 channelId,
1607716120
if (ssh == NULL)
1607816121
ret = WS_BAD_ARGUMENT;
1607916122

16123+
if (ret == WS_SUCCESS) {
16124+
if (!IsMessageAllowed(ssh, MSGID_CHANNEL_WINDOW_ADJUST, WS_MSG_SEND)) {
16125+
ret = WS_MSGID_NOT_ALLOWED_E;
16126+
}
16127+
}
16128+
1608016129
channel = ChannelFind(ssh, channelId, WS_CHANNEL_ID_SELF);
1608116130
if (channel == NULL) {
1608216131
WLOG(WS_LOG_DEBUG, "Invalid channel");

wolfssh/internal.h

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

12501250
#define CHANNEL_EXTENDED_DATA_STDERR WOLFSSH_EXT_DATA_STDERR
12511251

1252+
/* Used when checking IsMessageAllowed() to determine if createing and sending
1253+
* the message or receiving the message is allowed */
1254+
#define WS_MSG_SEND 1
1255+
#define WS_MSG_RECV 2
12521256

12531257
/* dynamic memory types */
12541258
enum WS_DynamicTypes {
@@ -1442,4 +1446,3 @@ enum TerminalModes {
14421446
#endif
14431447

14441448
#endif /* _WOLFSSH_INTERNAL_H_ */
1445-

0 commit comments

Comments
 (0)