@@ -571,6 +571,7 @@ static HandshakeInfo* HandshakeInfoNew(void* heap)
571571 heap, DYNTYPE_HS);
572572 if (newHs != NULL) {
573573 WMEMSET(newHs, 0, sizeof(HandshakeInfo));
574+ newHs->expectMsgId = MSGID_NONE;
574575 newHs->kexId = ID_NONE;
575576 newHs->kexHashId = WC_HASH_TYPE_NONE;
576577 newHs->pubKeyId = ID_NONE;
@@ -4248,6 +4249,9 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
42484249 byte algoId;
42494250 byte list[24] = {ID_NONE};
42504251 byte cannedList[24] = {ID_NONE};
4252+ byte kexIdGuess = ID_NONE;
4253+ byte pubKeyIdGuess = ID_NONE;
4254+ byte kexPacketFollows = 0;
42514255 word32 listSz;
42524256 word32 cannedListSz;
42534257 word32 cannedAlgoNamesSz;
@@ -4319,7 +4323,7 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
43194323 (const byte*)ssh->algoListKex, cannedAlgoNamesSz);
43204324 }
43214325 if (ret == WS_SUCCESS) {
4322- ssh->handshake-> kexIdGuess = list[0];
4326+ kexIdGuess = list[0];
43234327 algoId = MatchIdLists(side, list, listSz,
43244328 cannedList, cannedListSz);
43254329 if (algoId == ID_UNKNOWN) {
@@ -4364,6 +4368,7 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
43644368 }
43654369 }
43664370 if (ret == WS_SUCCESS) {
4371+ pubKeyIdGuess = list[0];
43674372 algoId = MatchIdLists(side, list, listSz, cannedList, cannedListSz);
43684373 if (algoId == ID_UNKNOWN) {
43694374 WLOG(WS_LOG_DEBUG, "Unable to negotiate Server Host Key Algo");
@@ -4521,10 +4526,15 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
45214526 /* First KEX Packet Follows */
45224527 if (ret == WS_SUCCESS) {
45234528 WLOG(WS_LOG_DEBUG, "DKI: KEX Packet Follows");
4524- ret = GetBoolean(&ssh->handshake-> kexPacketFollows, buf, len, &begin);
4529+ ret = GetBoolean(&kexPacketFollows, buf, len, &begin);
45254530 if (ret == WS_SUCCESS) {
45264531 WLOG(WS_LOG_DEBUG, " packet follows: %s",
4527- ssh->handshake->kexPacketFollows ? "yes" : "no");
4532+ kexPacketFollows ? "yes" : "no");
4533+ if (kexPacketFollows
4534+ && (kexIdGuess != ssh->handshake->kexId
4535+ || pubKeyIdGuess != ssh->handshake->pubKeyId)) {
4536+ ssh->handshake->ignoreNextKexMsg = 1;
4537+ }
45284538 }
45294539 }
45304540
@@ -4836,12 +4846,11 @@ static int DoKexDhInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
48364846 ret = WS_BAD_ARGUMENT;
48374847
48384848 if (ret == WS_SUCCESS) {
4839- if (ssh->handshake->kexPacketFollows
4840- && ssh->handshake->kexIdGuess != ssh->handshake->kexId) {
4841-
4849+ if (ssh->handshake->ignoreNextKexMsg) {
48424850 /* skip this message. */
4843- WLOG(WS_LOG_DEBUG, "Skipping the client's KEX init function.");
4844- ssh->handshake->kexPacketFollows = 0;
4851+ WLOG(WS_LOG_DEBUG, "Skipping client's KEXDH_INIT message due to "
4852+ "first_packet_follows guess mismatch.");
4853+ ssh->handshake->ignoreNextKexMsg = 0;
48454854 *idx += len;
48464855 return WS_SUCCESS;
48474856 }
0 commit comments