Skip to content

Commit ed2b8a6

Browse files
ejohnstownphilljj
authored andcommitted
internal: compare KEX guesses to our first choice
RFC 4253 section 7 counts a first_kex_packet_follows guess as right only when the client's and server's first KEX and host key algorithms are the same. DoKexInit() now compares the client's guesses with the server's own first choices, not only with the negotiated ids. - tests: first choices differ but the guess is negotiated: skip - tests: same first choices in longer lists: do not skip Issue: F-15530
1 parent d3fbc86 commit ed2b8a6

2 files changed

Lines changed: 36 additions & 9 deletions

File tree

‎src/internal.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6818,6 +6818,8 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
68186818
byte cannedList[WOLFSSH_KEXINIT_ID_LIST_MAX] = {ID_NONE};
68196819
byte kexIdGuess = ID_NONE;
68206820
byte pubKeyIdGuess = ID_NONE;
6821+
byte kexIdFirst = ID_NONE;
6822+
byte pubKeyIdFirst = ID_NONE;
68216823
byte kexPacketFollows = 0;
68226824
word32 listSz;
68236825
word32 cannedListSz;
@@ -6897,6 +6899,7 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
68976899
* the guess is never read. list[] is reused across the fields, so
68986900
* take ID_UNKNOWN over a stale ID if that ever changes. */
68996901
kexIdGuess = (listSz > 0) ? list[0] : ID_UNKNOWN;
6902+
kexIdFirst = (cannedListSz > 0) ? cannedList[0] : ID_UNKNOWN;
69006903
algoId = MatchIdLists(side, list, listSz,
69016904
cannedList, cannedListSz);
69026905
if (algoId == ID_UNKNOWN) {
@@ -6987,6 +6990,7 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
69876990
}
69886991
if (ret == WS_SUCCESS) {
69896992
pubKeyIdGuess = (listSz > 0) ? list[0] : ID_UNKNOWN;
6993+
pubKeyIdFirst = (cannedListSz > 0) ? cannedList[0] : ID_UNKNOWN;
69906994
algoId = MatchIdLists(side, list, listSz, cannedList, cannedListSz);
69916995
if (algoId == ID_UNKNOWN) {
69926996
WLOG(WS_LOG_DEBUG, "Unable to negotiate Server Host Key Algo");
@@ -7148,8 +7152,13 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
71487152
if (kexPacketFollows && side != WOLFSSH_ENDPOINT_SERVER) {
71497153
WLOG(WS_LOG_DEBUG, " ignoring server's packet follows flag");
71507154
}
7155+
/* RFC 4253 section 7: the guess is right only when both
7156+
* sides list the same algorithm first. The negotiated id
7157+
* check also rejects a first choice neither side knows. */
71517158
else if (kexPacketFollows
7152-
&& (kexIdGuess != ssh->handshake->kexId
7159+
&& (kexIdGuess != kexIdFirst
7160+
|| pubKeyIdGuess != pubKeyIdFirst
7161+
|| kexIdGuess != ssh->handshake->kexId
71537162
|| pubKeyIdGuess != ssh->handshake->pubKeyId)) {
71547163
ssh->handshake->ignoreNextKexMsg = 1;
71557164
}

‎tests/regress.c‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17582,27 +17582,43 @@ typedef struct {
1758217582
byte firstPacketFollows;
1758317583
byte expectIgnore;
1758417584
byte endpointType;
17585+
/* Our own lists, first choice first; NULL is the good id alone. */
17586+
const char* ourKexList;
17587+
const char* ourKeyList;
1758517588
} FirstPacketFollowsCase;
1758617589

1758717590
static const FirstPacketFollowsCase firstPacketFollowsCases[] = {
1758817591
{ "follows=0, guesses irrelevant: flag stays off",
1758917592
FPF_KEX_BAD "," FPF_KEX_GOOD, FPF_KEY_BAD "," FPF_KEY_GOOD, 0, 0,
17590-
WOLFSSH_ENDPOINT_SERVER },
17593+
WOLFSSH_ENDPOINT_SERVER, NULL, NULL },
1759117594
{ "follows=1, both guesses match: do not skip",
17592-
FPF_KEX_GOOD, FPF_KEY_GOOD, 1, 0, WOLFSSH_ENDPOINT_SERVER },
17595+
FPF_KEX_GOOD, FPF_KEY_GOOD, 1, 0, WOLFSSH_ENDPOINT_SERVER,
17596+
NULL, NULL },
1759317597
{ "follows=1, KEX guess wrong: skip",
1759417598
FPF_KEX_BAD "," FPF_KEX_GOOD, FPF_KEY_GOOD, 1, 1,
17595-
WOLFSSH_ENDPOINT_SERVER },
17599+
WOLFSSH_ENDPOINT_SERVER, NULL, NULL },
1759617600
{ "follows=1, host-key guess wrong: skip", /* regression case */
1759717601
FPF_KEX_GOOD, FPF_KEY_BAD "," FPF_KEY_GOOD, 1, 1,
17598-
WOLFSSH_ENDPOINT_SERVER },
17602+
WOLFSSH_ENDPOINT_SERVER, NULL, NULL },
1759917603
{ "follows=1, both guesses wrong: skip",
1760017604
FPF_KEX_BAD "," FPF_KEX_GOOD, FPF_KEY_BAD "," FPF_KEY_GOOD, 1, 1,
17601-
WOLFSSH_ENDPOINT_SERVER },
17605+
WOLFSSH_ENDPOINT_SERVER, NULL, NULL },
17606+
/* RFC 4253 section 7: a guess is right only when both sides list the
17607+
* same first choice, even if the client's first choice is negotiated. */
17608+
{ "follows=1, same first choices, longer lists: do not skip",
17609+
FPF_KEX_GOOD "," FPF_KEX_BAD, FPF_KEY_GOOD "," FPF_KEY_BAD, 1, 0,
17610+
WOLFSSH_ENDPOINT_SERVER,
17611+
FPF_KEX_GOOD "," FPF_KEX_BAD, FPF_KEY_GOOD "," FPF_KEY_BAD },
17612+
{ "follows=1, KEX first choices differ, guess negotiated: skip",
17613+
FPF_KEX_GOOD "," FPF_KEX_BAD, FPF_KEY_GOOD, 1, 1,
17614+
WOLFSSH_ENDPOINT_SERVER, FPF_KEX_BAD "," FPF_KEX_GOOD, NULL },
17615+
{ "follows=1, host-key first choices differ, guess negotiated: skip",
17616+
FPF_KEX_GOOD, FPF_KEY_GOOD "," FPF_KEY_BAD, 1, 1,
17617+
WOLFSSH_ENDPOINT_SERVER, NULL, FPF_KEY_BAD "," FPF_KEY_GOOD },
1760217618
/* A server never sends a guessed packet; the client ignores the flag. */
1760317619
{ "client, server follows=1, both guesses wrong: do not skip",
1760417620
FPF_KEX_BAD "," FPF_KEX_GOOD, FPF_KEY_BAD "," FPF_KEY_GOOD, 1, 0,
17605-
WOLFSSH_ENDPOINT_CLIENT },
17621+
WOLFSSH_ENDPOINT_CLIENT, NULL, NULL },
1760617622
};
1760717623

1760817624
static void RunFirstPacketFollowsCase(const FirstPacketFollowsCase* tc)
@@ -17619,8 +17635,10 @@ static void RunFirstPacketFollowsCase(const FirstPacketFollowsCase* tc)
1761917635
ssh = wolfSSH_new(ctx);
1762017636
AssertNotNull(ssh);
1762117637

17622-
AssertIntEQ(wolfSSH_SetAlgoListKex(ssh, FPF_KEX_GOOD), WS_SUCCESS);
17623-
AssertIntEQ(wolfSSH_SetAlgoListKey(ssh, FPF_KEY_GOOD), WS_SUCCESS);
17638+
AssertIntEQ(wolfSSH_SetAlgoListKex(ssh, tc->ourKexList != NULL ?
17639+
tc->ourKexList : FPF_KEX_GOOD), WS_SUCCESS);
17640+
AssertIntEQ(wolfSSH_SetAlgoListKey(ssh, tc->ourKeyList != NULL ?
17641+
tc->ourKeyList : FPF_KEY_GOOD), WS_SUCCESS);
1762417642

1762517643
payloadSz = BuildKexInitPayload(ssh, tc->kexList, tc->keyList,
1762617644
tc->firstPacketFollows, payload, sizeof(payload));

0 commit comments

Comments
 (0)