@@ -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;
@@ -4238,6 +4239,9 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
42384239 byte algoId;
42394240 byte list[24] = {ID_NONE};
42404241 byte cannedList[24] = {ID_NONE};
4242+ byte kexIdGuess = ID_NONE;
4243+ byte pubKeyIdGuess = ID_NONE;
4244+ byte kexPacketFollows = 0;
42414245 word32 listSz;
42424246 word32 cannedListSz;
42434247 word32 cannedAlgoNamesSz;
@@ -4309,7 +4313,7 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
43094313 (const byte*)ssh->algoListKex, cannedAlgoNamesSz);
43104314 }
43114315 if (ret == WS_SUCCESS) {
4312- ssh->handshake-> kexIdGuess = list[0];
4316+ kexIdGuess = list[0];
43134317 algoId = MatchIdLists(side, list, listSz,
43144318 cannedList, cannedListSz);
43154319 if (algoId == ID_UNKNOWN) {
@@ -4354,6 +4358,7 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
43544358 }
43554359 }
43564360 if (ret == WS_SUCCESS) {
4361+ pubKeyIdGuess = list[0];
43574362 algoId = MatchIdLists(side, list, listSz, cannedList, cannedListSz);
43584363 if (algoId == ID_UNKNOWN) {
43594364 WLOG(WS_LOG_DEBUG, "Unable to negotiate Server Host Key Algo");
@@ -4511,10 +4516,15 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
45114516 /* First KEX Packet Follows */
45124517 if (ret == WS_SUCCESS) {
45134518 WLOG(WS_LOG_DEBUG, "DKI: KEX Packet Follows");
4514- ret = GetBoolean(&ssh->handshake-> kexPacketFollows, buf, len, &begin);
4519+ ret = GetBoolean(&kexPacketFollows, buf, len, &begin);
45154520 if (ret == WS_SUCCESS) {
45164521 WLOG(WS_LOG_DEBUG, " packet follows: %s",
4517- ssh->handshake->kexPacketFollows ? "yes" : "no");
4522+ kexPacketFollows ? "yes" : "no");
4523+ if (kexPacketFollows
4524+ && (kexIdGuess != ssh->handshake->kexId
4525+ || pubKeyIdGuess != ssh->handshake->pubKeyId)) {
4526+ ssh->handshake->ignoreNextKexMsg = 1;
4527+ }
45184528 }
45194529 }
45204530
@@ -4819,12 +4829,11 @@ static int DoKexDhInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
48194829 ret = WS_BAD_ARGUMENT;
48204830
48214831 if (ret == WS_SUCCESS) {
4822- if (ssh->handshake->kexPacketFollows
4823- && ssh->handshake->kexIdGuess != ssh->handshake->kexId) {
4824-
4832+ if (ssh->handshake->ignoreNextKexMsg) {
48254833 /* skip this message. */
4826- WLOG(WS_LOG_DEBUG, "Skipping the client's KEX init function.");
4827- ssh->handshake->kexPacketFollows = 0;
4834+ WLOG(WS_LOG_DEBUG, "Skipping client's KEXDH_INIT message due to "
4835+ "first_packet_follows guess mismatch.");
4836+ ssh->handshake->ignoreNextKexMsg = 0;
48284837 *idx += len;
48294838 return WS_SUCCESS;
48304839 }
0 commit comments