Skip to content

Commit

Permalink
protocol/kex: do not wait to send NEWKEYS
Browse files Browse the repository at this point in the history
  • Loading branch information
mscdex committed Aug 12, 2021
1 parent 3b21709 commit 0af296d
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions lib/protocol/kex.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,18 +778,7 @@ const createKeyExchange = (() => {
this._protocol._packetRW.write.finalize(packet, true)
);
}
if (!this._sentNEWKEYS) {
this._protocol._debug && this._protocol._debug(
'Outbound: Sending NEWKEYS'
);
const p = this._protocol._packetRW.write.allocStartKEX;
const packet = this._protocol._packetRW.write.alloc(1, true);
packet[p] = MESSAGE.NEWKEYS;
this._protocol._cipher.encrypt(
this._protocol._packetRW.write.finalize(packet, true)
);
this._sentNEWKEYS = true;
}
trySendNEWKEYS(this);

const completeHandshake = () => {
if (!this.sessionID)
Expand Down Expand Up @@ -1180,6 +1169,8 @@ const createKeyExchange = (() => {
this._hostVerified = true;
if (this._receivedNEWKEYS)
this.finish();
else
trySendNEWKEYS(this);
});
}
if (ret === undefined) {
Expand All @@ -1203,6 +1194,7 @@ const createKeyExchange = (() => {
'Host accepted (verified)'
);
this._hostVerified = true;
trySendNEWKEYS(this);
}
++this._step;
break;
Expand Down Expand Up @@ -1798,6 +1790,21 @@ function dhEstimate(neg) {
return 8192;
}

function trySendNEWKEYS(kex) {
if (!kex._sentNEWKEYS) {
kex._protocol._debug && kex._protocol._debug(
'Outbound: Sending NEWKEYS'
);
const p = kex._protocol._packetRW.write.allocStartKEX;
const packet = kex._protocol._packetRW.write.alloc(1, true);
packet[p] = MESSAGE.NEWKEYS;
kex._protocol._cipher.encrypt(
kex._protocol._packetRW.write.finalize(packet, true)
);
kex._sentNEWKEYS = true;
}
}

module.exports = {
KexInit,
kexinit,
Expand Down

0 comments on commit 0af296d

Please sign in to comment.