js-libp2p-noise/test/noise.test.ts

128 lines
5.0 KiB
TypeScript
Raw Normal View History

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";
2019-11-26 10:52:30 +01:00
import {Handshake} from "../src/handshake";
import {
createHandshakePayload,
generateKeypair,
2019-12-02 15:28:59 +01:00
getHandshakePayload,
2019-11-26 10:52:30 +01:00
signPayload
} from "../src/utils";
2019-12-03 15:15:46 +01:00
import { decodeMessageBuffer, encodeMessageBuffer } from "../src/encoder";
2019-12-24 20:45:48 +01:00
import {XXHandshake} 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";
2019-11-11 15:39:09 +01:00
describe("Noise", () => {
2019-11-26 10:52:30 +01:00
let remotePeer, localPeer;
before(async () => {
2019-12-02 15:28:59 +01:00
[localPeer, remotePeer] = await createPeerIdsFromFixtures(2);
2019-11-26 10:52:30 +01:00
});
2019-11-26 15:24:10 +01:00
it("should communicate through encrypted streams", async() => {
try {
2019-12-03 13:52:44 +01:00
const { privateKey: libp2pInitPrivKey } = getKeyPairFromPeerId(localPeer);
const { privateKey: libp2pRespPrivKey } = getKeyPairFromPeerId(remotePeer);
const noiseInit = new Noise(libp2pInitPrivKey);
const noiseResp = new Noise(libp2pRespPrivKey);
2019-11-26 10:52:30 +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
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() => {
2019-12-03 13:52:44 +01:00
const { privateKey: libp2pInitPrivKey } = getKeyPairFromPeerId(localPeer);
const noiseInit = new Noise(libp2pInitPrivKey);
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();
2019-11-27 14:19:35 +01:00
const xx = new XXHandshake();
2019-12-03 13:52:44 +01:00
const { privateKey: libp2pPrivKey, publicKey: libp2pPubKey } = getKeyPairFromPeerId(remotePeer);
2019-12-03 13:39:33 +01:00
const handshake = new Handshake(false, libp2pPrivKey, libp2pPubKey, prologue, staticKeys, wrapped, localPeer, xx);
2019-11-26 15:24:10 +01:00
2019-11-27 14:19:35 +01:00
let receivedMessageBuffer = decodeMessageBuffer((await wrapped.readLP()).slice());
// 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
2019-11-28 17:32:46 +01:00
const signedPayload = signPayload(libp2pPrivKey, getHandshakePayload(staticKeys.publicKey));
const handshakePayload = await createHandshakePayload(libp2pPubKey, libp2pPrivKey, signedPayload);
2019-11-27 14:19:35 +01:00
2019-12-03 15:12:55 +01:00
const messageBuffer = xx.sendMessage(handshake.session, handshakePayload);
2019-11-27 14:19:35 +01:00
wrapped.writeLP(encodeMessageBuffer(messageBuffer));
// Stage 2 - finish handshake
receivedMessageBuffer = decodeMessageBuffer((await wrapped.readLP()).slice());
2019-12-03 15:12:55 +01:00
xx.recvMessage(handshake.session, receivedMessageBuffer);
return {wrapped, handshake};
2019-11-26 15:24:10 +01:00
})(),
]);
2019-11-27 14:19:35 +01:00
try {
const wrappedOutbound = Wrap(outbound.conn);
wrappedOutbound.write(Buffer.from("test"));
2019-11-27 14:19:35 +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 {
const { privateKey: libp2pInitPrivKey } = getKeyPairFromPeerId(localPeer);
const { privateKey: libp2pRespPrivKey } = getKeyPairFromPeerId(remotePeer);
const noiseInit = new Noise(libp2pInitPrivKey);
const noiseResp = new Noise(libp2pRespPrivKey);
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);
}
});
2019-11-11 15:39:09 +01:00
});