2019-11-27 14:19:35 +01:00
|
|
|
import { expect, assert } from "chai";
|
2019-11-12 14:07:25 +01:00
|
|
|
import DuplexPair from 'it-pair/duplex';
|
|
|
|
|
2019-11-11 15:39:09 +01:00
|
|
|
import { Noise } from "../src";
|
2019-12-02 15:28:59 +01:00
|
|
|
import {createPeerIdsFromFixtures} from "./fixtures/peer";
|
2019-11-26 10:52:30 +01:00
|
|
|
import Wrap from "it-pb-rpc";
|
2019-12-27 13:15:06 +01:00
|
|
|
import { random } from "bcrypto";
|
2020-01-16 17:49:41 +01:00
|
|
|
import sinon from "sinon";
|
2020-01-07 13:34:45 +01:00
|
|
|
import {XXHandshake} from "../src/handshake-xx";
|
2019-11-26 10:52:30 +01:00
|
|
|
import {
|
|
|
|
createHandshakePayload,
|
|
|
|
generateKeypair,
|
2020-01-07 16:59:41 +01:00
|
|
|
getHandshakePayload, getPayload,
|
2019-11-26 10:52:30 +01:00
|
|
|
signPayload
|
|
|
|
} from "../src/utils";
|
2020-01-07 10:29:40 +01:00
|
|
|
import {decode0, decode1, encode1} from "../src/encoder";
|
2020-01-05 19:09:59 +01:00
|
|
|
import {XX} from "../src/handshakes/xx";
|
2019-11-26 10:52:30 +01:00
|
|
|
import {Buffer} from "buffer";
|
2019-12-27 13:15:06 +01:00
|
|
|
import {getKeyPairFromPeerId} from "./utils";
|
2020-01-15 17:27:32 +01:00
|
|
|
import {KeyCache} from "../src/keycache";
|
2020-01-16 17:49:41 +01:00
|
|
|
import {XXFallbackHandshake} from "../src/handshake-xx-fallback";
|
2019-11-11 15:39:09 +01:00
|
|
|
|
|
|
|
describe("Noise", () => {
|
2019-11-26 10:52:30 +01:00
|
|
|
let remotePeer, localPeer;
|
2020-01-16 17:49:41 +01:00
|
|
|
let sandbox = sinon.createSandbox();
|
2019-11-26 10:52:30 +01:00
|
|
|
|
|
|
|
before(async () => {
|
2019-12-02 15:28:59 +01:00
|
|
|
[localPeer, remotePeer] = await createPeerIdsFromFixtures(2);
|
2019-11-26 10:52:30 +01:00
|
|
|
});
|
|
|
|
|
2020-01-16 17:49:41 +01:00
|
|
|
afterEach(function() {
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
2020-01-15 17:27:32 +01:00
|
|
|
it("should communicate through encrypted streams without noise pipes", async() => {
|
2019-12-02 15:24:49 +01:00
|
|
|
try {
|
2020-01-13 16:33:58 +01:00
|
|
|
const noiseInit = new Noise(undefined, undefined, false);
|
|
|
|
const noiseResp = new Noise(undefined, undefined, false);
|
2019-11-26 10:52:30 +01:00
|
|
|
|
2019-12-02 15:24:49 +01:00
|
|
|
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);
|
2019-11-26 14:14:10 +01:00
|
|
|
|
2019-12-02 15:24:49 +01:00
|
|
|
wrappedOutbound.writeLP(Buffer.from("test"));
|
|
|
|
const response = await wrappedInbound.readLP();
|
|
|
|
expect(response.toString()).equal("test");
|
|
|
|
} catch (e) {
|
|
|
|
assert(false, e.message);
|
|
|
|
}
|
2019-11-27 08:39:06 +01:00
|
|
|
});
|
2019-11-26 15:24:10 +01:00
|
|
|
|
2019-11-27 08:39:06 +01:00
|
|
|
it("should test that secureOutbound is spec compliant", async() => {
|
2020-01-13 16:33:58 +01:00
|
|
|
const noiseInit = new Noise(undefined, undefined, false);
|
2019-11-26 15:24:10 +01:00
|
|
|
const [inboundConnection, outboundConnection] = DuplexPair();
|
|
|
|
|
2019-11-28 17:32:46 +01:00
|
|
|
const [outbound, { wrapped, handshake }] = await Promise.all([
|
2019-11-27 08:39:06 +01:00
|
|
|
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
|
2019-11-26 15:24:10 +01:00
|
|
|
(async () => {
|
|
|
|
const wrapped = Wrap(inboundConnection);
|
|
|
|
const prologue = Buffer.from('/noise');
|
2019-11-28 17:32:46 +01:00
|
|
|
const staticKeys = generateKeypair();
|
2020-01-05 19:09:59 +01:00
|
|
|
const xx = new XX();
|
2019-12-03 13:52:44 +01:00
|
|
|
|
2020-01-07 16:59:41 +01:00
|
|
|
const payload = await getPayload(remotePeer, staticKeys.publicKey);
|
2020-01-11 20:20:57 +01:00
|
|
|
const handshake = new XXHandshake(false, payload, prologue, staticKeys, wrapped, localPeer, xx);
|
2019-11-26 15:24:10 +01:00
|
|
|
|
2020-01-07 10:29:40 +01:00
|
|
|
let receivedMessageBuffer = decode0((await wrapped.readLP()).slice());
|
2019-11-27 14:19:35 +01:00
|
|
|
// The first handshake message contains the initiator's ephemeral public key
|
|
|
|
expect(receivedMessageBuffer.ne.length).equal(32);
|
2019-12-03 15:12:55 +01:00
|
|
|
xx.recvMessage(handshake.session, receivedMessageBuffer);
|
2019-11-26 15:24:10 +01:00
|
|
|
|
2019-11-27 14:19:35 +01:00
|
|
|
// Stage 1
|
2020-01-11 20:20:57 +01:00
|
|
|
const { publicKey: libp2pPubKey } = getKeyPairFromPeerId(remotePeer);
|
2020-01-07 16:59:41 +01:00
|
|
|
const signedPayload = await signPayload(remotePeer, getHandshakePayload(staticKeys.publicKey));
|
2020-01-11 20:20:57 +01:00
|
|
|
const handshakePayload = await createHandshakePayload(libp2pPubKey, signedPayload);
|
2019-11-27 14:19:35 +01:00
|
|
|
|
2019-12-03 15:12:55 +01:00
|
|
|
const messageBuffer = xx.sendMessage(handshake.session, handshakePayload);
|
2020-01-07 10:29:40 +01:00
|
|
|
wrapped.writeLP(encode1(messageBuffer));
|
2019-11-27 14:19:35 +01:00
|
|
|
|
|
|
|
// Stage 2 - finish handshake
|
2020-01-07 10:29:40 +01:00
|
|
|
receivedMessageBuffer = decode1((await wrapped.readLP()).slice());
|
2019-12-03 15:12:55 +01:00
|
|
|
xx.recvMessage(handshake.session, receivedMessageBuffer);
|
2019-12-02 15:24:49 +01:00
|
|
|
return {wrapped, handshake};
|
2019-11-26 15:24:10 +01:00
|
|
|
})(),
|
|
|
|
]);
|
2019-11-27 14:19:35 +01:00
|
|
|
|
2019-12-02 15:24:49 +01:00
|
|
|
try {
|
|
|
|
const wrappedOutbound = Wrap(outbound.conn);
|
|
|
|
wrappedOutbound.write(Buffer.from("test"));
|
2019-11-27 14:19:35 +01:00
|
|
|
|
2019-12-02 15:24:49 +01:00
|
|
|
// Check that noise message is prefixed with 16-bit big-endian unsigned integer
|
|
|
|
const receivedEncryptedPayload = (await wrapped.read()).slice();
|
|
|
|
const dataLength = receivedEncryptedPayload.readInt16BE(0);
|
|
|
|
const data = receivedEncryptedPayload.slice(2, dataLength + 2);
|
|
|
|
const decrypted = handshake.decrypt(data, handshake.session);
|
|
|
|
// Decrypted data should match
|
|
|
|
assert(decrypted.equals(Buffer.from("test")));
|
|
|
|
} catch (e) {
|
|
|
|
assert(false, e.message);
|
|
|
|
}
|
2019-11-11 15:39:09 +01:00
|
|
|
})
|
2019-12-24 16:25:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
it("should test large payloads", async() => {
|
|
|
|
try {
|
2020-01-13 16:33:58 +01:00
|
|
|
const noiseInit = new Noise(undefined, undefined, false);
|
|
|
|
const noiseResp = new Noise(undefined, undefined, false);
|
2019-12-24 16:25:49 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-12-27 13:15:06 +01:00
|
|
|
const largePlaintext = random.randomBytes(100000);
|
2019-12-24 16:25:49 +01:00
|
|
|
wrappedOutbound.writeLP(largePlaintext);
|
|
|
|
const response = await wrappedInbound.readLP();
|
2019-12-24 20:36:16 +01:00
|
|
|
|
|
|
|
expect(response.length).equals(largePlaintext.length);
|
2019-12-24 16:25:49 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
assert(false, e.message);
|
|
|
|
}
|
|
|
|
});
|
2020-01-15 17:27:32 +01:00
|
|
|
|
|
|
|
it("should communicate through encrypted streams with noise pipes", async() => {
|
|
|
|
try {
|
|
|
|
const staticKeysInitiator = generateKeypair();
|
|
|
|
const noiseInit = new Noise(staticKeysInitiator.privateKey);
|
|
|
|
const staticKeysResponder = generateKeypair();
|
|
|
|
const noiseResp = new Noise(staticKeysResponder.privateKey);
|
|
|
|
|
|
|
|
// Prepare key cache for noise pipes
|
|
|
|
await KeyCache.store(localPeer, staticKeysInitiator.publicKey);
|
|
|
|
await KeyCache.store(remotePeer, staticKeysResponder.publicKey);
|
|
|
|
|
2020-01-16 17:49:41 +01:00
|
|
|
const xxSpy = sandbox.spy(noiseInit, "performXXHandshake");
|
|
|
|
const xxFallbackSpy = sandbox.spy(noiseInit, "performXXFallbackHandshake");
|
|
|
|
|
2020-01-15 17:27:32 +01:00
|
|
|
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 v2"));
|
|
|
|
const response = await wrappedInbound.readLP();
|
|
|
|
expect(response.toString()).equal("test v2");
|
2020-01-16 17:49:41 +01:00
|
|
|
|
|
|
|
assert(xxSpy.notCalled);
|
|
|
|
assert(xxFallbackSpy.notCalled);
|
2020-01-15 17:27:32 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
assert(false, e.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should switch to XX fallback because of invalid remote static key", async() => {
|
|
|
|
try {
|
|
|
|
const staticKeysInitiator = generateKeypair();
|
|
|
|
const noiseInit = new Noise(staticKeysInitiator.privateKey);
|
|
|
|
const noiseResp = new Noise();
|
|
|
|
|
|
|
|
// Prepare key cache for noise pipes
|
|
|
|
await KeyCache.store(localPeer, staticKeysInitiator.publicKey);
|
|
|
|
await KeyCache.store(remotePeer, generateKeypair().publicKey);
|
|
|
|
|
|
|
|
const [inboundConnection, outboundConnection] = DuplexPair();
|
|
|
|
const [outbound, inbound] = await Promise.all([
|
|
|
|
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
|
|
|
|
noiseResp.secureInbound(remotePeer, inboundConnection, localPeer),
|
|
|
|
]);
|
2020-01-16 17:49:41 +01:00
|
|
|
|
|
|
|
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");
|
2020-01-15 17:27:32 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
2020-01-16 17:49:41 +01:00
|
|
|
assert(false, e.message);
|
2020-01-15 17:27:32 +01:00
|
|
|
}
|
|
|
|
});
|
2019-11-11 15:39:09 +01:00
|
|
|
});
|