@@ -595,42 +595,6 @@ static void HandshakeInfoFree(HandshakeInfo* hs, void* heap)
595595}
596596
597597
598- #if 0
599- /* RFC 4253 section 7.1, Once having sent SSH_MSG_KEXINIT the only messages
600- * that can be sent are 1-19 (except SSH_MSG_SERVICE_REQUEST and
601- * SSH_MSG_SERVICE_ACCEPT), 20-29 (except SSH_MSG_KEXINIT again), and 30-49
602- */
603- INLINE static int IsMessageAllowedKeying(WOLFSSH *ssh, byte msg)
604- {
605- if (ssh->isKeying == 0) {
606- return 1;
607- }
608-
609- /* case of service request or accept in 1-19 */
610- if (msg == MSGID_SERVICE_REQUEST || msg == MSGID_SERVICE_ACCEPT) {
611- WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by during rekeying", msg);
612- ssh->error = WS_REKEYING;
613- return 0;
614- }
615-
616- /* case of peer resending SSH_MSG_KEXINIT */
617- if ((ssh->isKeying & WOLFSSH_PEER_IS_KEYING) && msg == MSGID_KEXINIT) {
618- WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by during rekeying", msg);
619- ssh->error = WS_REKEYING;
620- return 0;
621- }
622-
623- /* case where message id greater than 49 */
624- if (msg >= MSGID_USERAUTH_REQUEST) {
625- WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by during rekeying", msg);
626- ssh->error = WS_REKEYING;
627- return 0;
628- }
629- return 1;
630- }
631- #endif
632-
633-
634598#ifndef NO_WOLFSSH_SERVER
635599INLINE static int IsMessageAllowedServer(WOLFSSH *ssh, byte msg)
636600{
@@ -694,6 +658,7 @@ INLINE static int IsMessageAllowedClient(WOLFSSH *ssh, byte msg)
694658 if (msg == MSGID_SERVICE_REQUEST || msg == MSGID_USERAUTH_REQUEST) {
695659 WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by %s %s",
696660 msg, "client", "ever");
661+ ssh->error = WS_MSGID_NOT_ALLOWED_E;
697662 return 0;
698663 }
699664
@@ -720,6 +685,7 @@ INLINE static int IsMessageAllowedClient(WOLFSSH *ssh, byte msg)
720685 if (msg == MSGID_KEXINIT) {
721686 WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by %s %s",
722687 msg, "client", "when keying");
688+ ssh->error = WS_REKEYING;
723689 return 0;
724690 }
725691
@@ -729,6 +695,7 @@ INLINE static int IsMessageAllowedClient(WOLFSSH *ssh, byte msg)
729695 WLOG(WS_LOG_DEBUG,
730696 "Message ID %u not the expected message %u",
731697 msg, ssh->handshake->expectMsgId);
698+ ssh->error = WS_REKEYING;
732699 return 0;
733700 }
734701 else {
@@ -748,6 +715,7 @@ INLINE static int IsMessageAllowedClient(WOLFSSH *ssh, byte msg)
748715 * when not keying. */
749716 WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by %s %s",
750717 msg, "client", "when not keying");
718+ ssh->error = WS_MSGID_NOT_ALLOWED_E;
751719 return 0;
752720 }
753721 }
@@ -761,9 +729,17 @@ INLINE static int IsMessageAllowedClient(WOLFSSH *ssh, byte msg)
761729 if (MSGIDLIMIT_POST_USERAUTH(msg)) {
762730 WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by %s %s",
763731 msg, "client", "before user authentication is complete");
732+ ssh->error = WS_MSGID_NOT_ALLOWED_E;
764733 return 0;
765734 }
766735 else if (MSGIDLIMIT_AUTH(msg)) {
736+ /* Do not accept any userauth messages until we've asked for auth. */
737+ if (ssh->connectState < CONNECT_CLIENT_USERAUTH_REQUEST_SENT) {
738+ WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by %s %s",
739+ msg, "client", "before sending userauth request");
740+ ssh->error = WS_MSGID_NOT_ALLOWED_E;
741+ return 0;
742+ }
767743 return 1;
768744 }
769745 }
@@ -774,6 +750,7 @@ INLINE static int IsMessageAllowedClient(WOLFSSH *ssh, byte msg)
774750 else if (MSGIDLIMIT_AUTH(msg)) {
775751 WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by %s %s",
776752 msg, "client", "after user authentication");
753+ ssh->error = WS_MSGID_NOT_ALLOWED_E;
777754 return 0;
778755 }
779756 }
@@ -787,12 +764,6 @@ INLINE static int IsMessageAllowedClient(WOLFSSH *ssh, byte msg)
787764 * Returns 1 if allowed 0 if not allowed. */
788765INLINE static int IsMessageAllowed(WOLFSSH *ssh, byte msg, byte state)
789766{
790- #if 0
791- if (!IsMessageAllowedKeying(ssh, msg)) {
792- return 0;
793- }
794- #endif
795-
796767#ifndef NO_WOLFSSH_SERVER
797768 if (ssh->ctx->side == WOLFSSH_ENDPOINT_SERVER) {
798769 return IsMessageAllowedServer(ssh, msg);
@@ -807,6 +778,13 @@ INLINE static int IsMessageAllowed(WOLFSSH *ssh, byte msg, byte state)
807778 return 0;
808779}
809780
781+ #ifdef WOLFSSH_TEST_INTERNAL
782+ int wolfSSH_TestIsMessageAllowed(WOLFSSH* ssh, byte msg, byte state)
783+ {
784+ return IsMessageAllowed(ssh, msg, state);
785+ }
786+ #endif
787+
810788
811789static const char cannedKexAlgoNames[] =
812790#if !defined(WOLFSSH_NO_NISTP256_MLKEM768_SHA256)
0 commit comments