mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 17:12:33 +00:00
Add test and improve
This commit is contained in:
parent
15db3b53ce
commit
231c721d12
@ -6,6 +6,5 @@ export class FailedIKError extends Error {
|
|||||||
|
|
||||||
this.initialMsg = initialMsg;
|
this.initialMsg = initialMsg;
|
||||||
this.name = "FailedIKhandshake";
|
this.name = "FailedIKhandshake";
|
||||||
this.stack = new Error().stack;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
16
src/noise.ts
16
src/noise.ts
@ -108,23 +108,13 @@ export class Noise implements INoiseConnection {
|
|||||||
private async performHandshake(params: HandshakeParams): Promise<IHandshake> {
|
private async performHandshake(params: HandshakeParams): Promise<IHandshake> {
|
||||||
const payload = await getPayload(params.localPeer, this.staticKeys.publicKey, this.earlyData);
|
const payload = await getPayload(params.localPeer, this.staticKeys.publicKey, this.earlyData);
|
||||||
|
|
||||||
let tryIK = this.useNoisePipes;
|
const remoteStaticKey = KeyCache.load(params.remotePeer);
|
||||||
const foundRemoteStaticKey = KeyCache.load(params.remotePeer);
|
|
||||||
if (tryIK && params.isInitiator && !foundRemoteStaticKey) {
|
|
||||||
tryIK = false;
|
|
||||||
logger(`Static key not found.`)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try IK if acting as responder or initiator that has remote's static key.
|
// Try IK if acting as responder or initiator that has remote's static key.
|
||||||
if (tryIK) {
|
if (this.useNoisePipes && remoteStaticKey) {
|
||||||
// Try IK first
|
// Try IK first
|
||||||
const { remotePeer, connection, isInitiator } = params;
|
const { remotePeer, connection, isInitiator } = params;
|
||||||
if (!foundRemoteStaticKey) {
|
const IKhandshake = new IKHandshake(isInitiator, payload, this.prologue, this.staticKeys, connection, remotePeer, remoteStaticKey);
|
||||||
// TODO: Recheck. Possible that responder should not have it here.
|
|
||||||
throw new Error("Remote static key should be initialized.");
|
|
||||||
}
|
|
||||||
|
|
||||||
const IKhandshake = new IKHandshake(isInitiator, payload, this.prologue, this.staticKeys, connection, remotePeer, foundRemoteStaticKey);
|
|
||||||
try {
|
try {
|
||||||
return await this.performIKHandshake(IKhandshake);
|
return await this.performIKHandshake(IKhandshake);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -163,7 +163,7 @@ describe("Noise", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should switch to XX fallback because of invalid remote static key", async() => {
|
it("IK -> XX fallback: initiator has invalid remote static key", async() => {
|
||||||
try {
|
try {
|
||||||
const staticKeysInitiator = generateKeypair();
|
const staticKeysInitiator = generateKeypair();
|
||||||
const noiseInit = new Noise(staticKeysInitiator.privateKey);
|
const noiseInit = new Noise(staticKeysInitiator.privateKey);
|
||||||
@ -171,6 +171,7 @@ describe("Noise", () => {
|
|||||||
const xxSpy = sandbox.spy(noiseInit, "performXXFallbackHandshake");
|
const xxSpy = sandbox.spy(noiseInit, "performXXFallbackHandshake");
|
||||||
|
|
||||||
// Prepare key cache for noise pipes
|
// Prepare key cache for noise pipes
|
||||||
|
KeyCache.resetStorage();
|
||||||
KeyCache.store(localPeer, staticKeysInitiator.publicKey);
|
KeyCache.store(localPeer, staticKeysInitiator.publicKey);
|
||||||
KeyCache.store(remotePeer, generateKeypair().publicKey);
|
KeyCache.store(remotePeer, generateKeypair().publicKey);
|
||||||
|
|
||||||
@ -194,12 +195,12 @@ describe("Noise", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("XX fallback with XX as responder has noise pipes disabled", async() => {
|
it("IK -> XX fallback: responder has disabled noise pipes", async() => {
|
||||||
try {
|
try {
|
||||||
const staticKeysInitiator = generateKeypair();
|
const staticKeysInitiator = generateKeypair();
|
||||||
const noiseInit = new Noise(staticKeysInitiator.privateKey);
|
const noiseInit = new Noise(staticKeysInitiator.privateKey);
|
||||||
const staticKeysResponder = generateKeypair();
|
|
||||||
|
|
||||||
|
const staticKeysResponder = generateKeypair();
|
||||||
const noiseResp = new Noise(staticKeysResponder.privateKey, undefined, false);
|
const noiseResp = new Noise(staticKeysResponder.privateKey, undefined, false);
|
||||||
const xxSpy = sandbox.spy(noiseInit, "performXXFallbackHandshake");
|
const xxSpy = sandbox.spy(noiseInit, "performXXFallbackHandshake");
|
||||||
|
|
||||||
@ -208,7 +209,6 @@ describe("Noise", () => {
|
|||||||
KeyCache.store(remotePeer, staticKeysResponder.publicKey);
|
KeyCache.store(remotePeer, staticKeysResponder.publicKey);
|
||||||
|
|
||||||
const [inboundConnection, outboundConnection] = DuplexPair();
|
const [inboundConnection, outboundConnection] = DuplexPair();
|
||||||
|
|
||||||
const [outbound, inbound] = await Promise.all([
|
const [outbound, inbound] = await Promise.all([
|
||||||
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
|
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
|
||||||
noiseResp.secureInbound(remotePeer, inboundConnection, localPeer),
|
noiseResp.secureInbound(remotePeer, inboundConnection, localPeer),
|
||||||
@ -228,7 +228,7 @@ describe("Noise", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Initiator starts with XX (pipes disabled) responder has noise pipes", async() => {
|
it("Initiator starts with XX (pipes disabled), responder has enabled noise pipes", async() => {
|
||||||
try {
|
try {
|
||||||
const staticKeysInitiator = generateKeypair();
|
const staticKeysInitiator = generateKeypair();
|
||||||
const noiseInit = new Noise(staticKeysInitiator.privateKey, undefined, false);
|
const noiseInit = new Noise(staticKeysInitiator.privateKey, undefined, false);
|
||||||
@ -262,4 +262,40 @@ describe("Noise", () => {
|
|||||||
assert(false, e.message);
|
assert(false, e.message);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("IK -> XX Fallback: responder has no remote static key", async() => {
|
||||||
|
try {
|
||||||
|
const staticKeysInitiator = generateKeypair();
|
||||||
|
const noiseInit = new Noise(staticKeysInitiator.privateKey);
|
||||||
|
const staticKeysResponder = generateKeypair();
|
||||||
|
|
||||||
|
const noiseResp = new Noise(staticKeysResponder.privateKey);
|
||||||
|
const xxFallbackInitSpy = sandbox.spy(noiseInit, "performXXFallbackHandshake");
|
||||||
|
const xxRespSpy = sandbox.spy(noiseResp, "performXXHandshake");
|
||||||
|
|
||||||
|
// Prepare key cache for noise pipes
|
||||||
|
KeyCache.resetStorage();
|
||||||
|
KeyCache.store(remotePeer, staticKeysResponder.publicKey);
|
||||||
|
|
||||||
|
const [inboundConnection, outboundConnection] = DuplexPair();
|
||||||
|
|
||||||
|
const [outbound, inbound] = await Promise.all([
|
||||||
|
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
|
||||||
|
noiseResp.secureInbound(remotePeer, inboundConnection, localPeer),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const wrappedInbound = Wrap(inbound.conn);
|
||||||
|
const wrappedOutbound = Wrap(outbound.conn);
|
||||||
|
|
||||||
|
wrappedOutbound.writeLP(Buffer.from("test fallback"));
|
||||||
|
const response = await wrappedInbound.readLP();
|
||||||
|
expect(response.toString()).equal("test fallback");
|
||||||
|
|
||||||
|
assert(xxFallbackInitSpy.calledOnce, "XX Fallback method was not called.");
|
||||||
|
assert(xxRespSpy.calledOnce, "XX method was not called.");
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
assert(false, e.message);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user