Skip to content

Commit

Permalink
protocol/kex: always immediately update decipher
Browse files Browse the repository at this point in the history
Fixes: #1312
  • Loading branch information
mscdex committed Jun 19, 2023
1 parent 281c290 commit ac336ce
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions lib/protocol/kex.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ const createKeyExchange = (() => {
this._dhData = undefined;
this._sig = undefined;
}
finish() {
finish(scOnly) {
if (this._finished)
return false;
this._finished = true;
Expand Down Expand Up @@ -783,9 +783,26 @@ const createKeyExchange = (() => {
this._protocol._packetRW.write.finalize(packet, true)
);
}
trySendNEWKEYS(this);

const completeHandshake = () => {
if (isServer || !scOnly)
trySendNEWKEYS(this);

let hsCipherConfig;
let hsWrite;
const completeHandshake = (partial) => {
if (hsCipherConfig) {
trySendNEWKEYS(this);
hsCipherConfig.outbound.seqno = this._protocol._cipher.outSeqno;
this._protocol._cipher.free();
this._protocol._cipher = createCipher(hsCipherConfig);
this._protocol._packetRW.write = hsWrite;
hsCipherConfig = undefined;
hsWrite = undefined;
this._protocol._onHandshakeComplete(negotiated);

return false;
}

if (!this.sessionID)
this.sessionID = exchangeHash;

Expand Down Expand Up @@ -868,9 +885,8 @@ const createKeyExchange = (() => {
macKey: (isServer ? scMacKey : csMacKey),
},
};
this._protocol._cipher && this._protocol._cipher.free();
this._protocol._decipher && this._protocol._decipher.free();
this._protocol._cipher = createCipher(config);
this._protocol._decipher.free();
hsCipherConfig = config;
this._protocol._decipher = createDecipher(config);

const rw = {
Expand Down Expand Up @@ -937,7 +953,8 @@ const createKeyExchange = (() => {
}
this._protocol._packetRW.read.cleanup();
this._protocol._packetRW.write.cleanup();
this._protocol._packetRW = rw;
this._protocol._packetRW.read = rw.read;
hsWrite = rw.write;

// Cleanup/reset various state
this._public = null;
Expand All @@ -950,13 +967,16 @@ const createKeyExchange = (() => {
this._dhData = undefined;
this._sig = undefined;

this._protocol._onHandshakeComplete(negotiated);

if (!partial)
return completeHandshake();
return false;
};

if (isServer || scOnly)
this.finish = completeHandshake;

if (!isServer)
return completeHandshake();
this.finish = completeHandshake;
return completeHandshake(scOnly);
}

start() {
Expand Down Expand Up @@ -1217,12 +1237,8 @@ const createKeyExchange = (() => {
);
this._receivedNEWKEYS = true;
++this._step;
if (this._protocol._server || this._hostVerified)
return this.finish();

// Signal to current decipher that we need to change to a new decipher
// for the next packet
return false;
return this.finish(!this._protocol._server && !this._hostVerified);
default:
return doFatalError(
this._protocol,
Expand Down

0 comments on commit ac336ce

Please sign in to comment.