Skip to content

Commit 88ca5ba

Browse files
committed
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 91cbb64 commit 88ca5ba

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;
@@ -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
}

wolfssh/internal.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,12 +628,10 @@ typedef struct Keys {
628628
typedef struct HandshakeInfo {
629629
byte expectMsgId;
630630
byte kexId;
631-
byte kexIdGuess;
632631
byte kexHashId;
633632
byte pubKeyId;
634633
byte encryptId;
635634
byte macId;
636-
byte kexPacketFollows;
637635
byte aeadMode;
638636

639637
byte blockSz;
@@ -660,6 +658,7 @@ typedef struct HandshakeInfo {
660658
word32 generatorSz;
661659
#endif
662660

661+
byte ignoreNextKexMsg:1;
663662
byte useDh:1;
664663
byte useEcc:1;
665664
byte useEccMlKem:1;

0 commit comments

Comments
 (0)