Skip to content

Commit 55d48e6

Browse files
ejohnstownpadelsbach
authored andcommitted
First packet follows check needs pubkey guess
When processing the KEX Init message, stash guesses for the peer's KEX and public key algorithms. When reading first_packet_follows, if set check the guesses and set the handshake info flag ignoreNextKexMsg. When processing the KexDhInit message, check that flag. Affected functions: DoKexInit, DoKexDhInit. Issue: F-1686
1 parent 6118655 commit 55d48e6

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

src/internal.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

wolfssh/internal.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,12 +631,10 @@ typedef struct Keys {
631631
typedef struct HandshakeInfo {
632632
byte expectMsgId;
633633
byte kexId;
634-
byte kexIdGuess;
635634
byte kexHashId;
636635
byte pubKeyId;
637636
byte encryptId;
638637
byte macId;
639-
byte kexPacketFollows;
640638
byte aeadMode;
641639

642640
byte blockSz;
@@ -663,6 +661,7 @@ typedef struct HandshakeInfo {
663661
word32 generatorSz;
664662
#endif
665663

664+
byte ignoreNextKexMsg:1;
666665
byte useDh:1;
667666
byte useEcc:1;
668667
byte useEccMlKem:1;

0 commit comments

Comments
 (0)