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

358 lines
14 KiB
TypeScript
Raw Normal View History

2020-06-19 12:49:40 +02:00
import { assert, expect } from 'chai'
import DuplexPair from 'it-pair/duplex'
import { createPeerIdsFromFixtures } from './fixtures/peer'
import Wrap from 'it-pb-rpc'
import sinon from 'sinon'
import BufferList from 'bl'
import { randomBytes } from 'libp2p-crypto'
import { Buffer } from 'buffer'
import { Noise } from '../src'
import { XXHandshake } from '../src/handshake-xx'
import { createHandshakePayload, generateKeypair, getHandshakePayload, getPayload, signPayload } from '../src/utils'
import { decode0, decode2, encode1, uint16BEDecode, uint16BEEncode } from '../src/encoder'
import { XX } from '../src/handshakes/xx'
import { getKeyPairFromPeerId } from './utils'
import { KeyCache } from '../src/keycache'
import { NOISE_MSG_MAX_LENGTH_BYTES } from '../src/constants'
describe('Noise', () => {
let remotePeer, localPeer
const sandbox = sinon.createSandbox()
2019-11-26 10:52:30 +01:00
before(async () => {
2020-06-19 12:49:40 +02:00
[localPeer, remotePeer] = await createPeerIdsFromFixtures(2)
})
2019-11-26 10:52:30 +01:00
2020-06-19 12:49:40 +02:00
afterEach(function () {
sandbox.restore()
})
2020-01-16 17:49:41 +01:00
2020-06-19 12:49:40 +02:00
it('should communicate through encrypted streams without noise pipes', async () => {
try {
2020-06-19 13:06:31 +02:00
const noiseInit = new Noise(undefined, undefined)
const noiseResp = new Noise(undefined, undefined)
2019-11-26 10:52:30 +01:00
2020-06-19 12:49:40 +02:00
const [inboundConnection, outboundConnection] = DuplexPair()
const [outbound, inbound] = await Promise.all([
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
2020-06-19 12:49:40 +02:00
noiseResp.secureInbound(remotePeer, inboundConnection, localPeer)
])
const wrappedInbound = Wrap(inbound.conn)
const wrappedOutbound = Wrap(outbound.conn)
wrappedOutbound.writeLP(Buffer.from('test'))
const response = await wrappedInbound.readLP()
expect(response.toString()).equal('test')
} catch (e) {
2020-06-19 12:49:40 +02:00
assert(false, e.message)
}
2020-06-19 12:49:40 +02:00
})
2019-11-26 15:24:10 +01:00
2020-06-19 12:49:40 +02:00
it('should test that secureOutbound is spec compliant', async () => {
2020-06-19 13:06:31 +02:00
const noiseInit = new Noise(undefined, undefined)
2020-06-19 12:49:40 +02:00
const [inboundConnection, outboundConnection] = DuplexPair()
2019-11-26 15:24:10 +01:00
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 () => {
2020-02-13 22:51:36 +01:00
const wrapped = Wrap(
inboundConnection,
{
lengthEncoder: uint16BEEncode,
lengthDecoder: uint16BEDecode,
maxDataLength: NOISE_MSG_MAX_LENGTH_BYTES
}
2020-06-19 12:49:40 +02:00
)
const prologue = Buffer.alloc(0)
const staticKeys = generateKeypair()
const xx = new XX()
2019-12-03 13:52:44 +01:00
2020-06-19 12:49:40 +02:00
const payload = await getPayload(remotePeer, staticKeys.publicKey)
const handshake = new XXHandshake(false, payload, prologue, staticKeys, wrapped, localPeer, xx)
2019-11-26 15:24:10 +01:00
2020-06-19 12:49:40 +02: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
2020-06-19 12:49:40 +02:00
expect(receivedMessageBuffer.ne.length).equal(32)
xx.recvMessage(handshake.session, receivedMessageBuffer)
2019-11-26 15:24:10 +01:00
2019-11-27 14:19:35 +01:00
// Stage 1
2020-06-19 12:49:40 +02:00
const { publicKey: libp2pPubKey } = getKeyPairFromPeerId(remotePeer)
const signedPayload = await signPayload(remotePeer, getHandshakePayload(staticKeys.publicKey))
const handshakePayload = await createHandshakePayload(libp2pPubKey, signedPayload)
2019-11-27 14:19:35 +01:00
2020-06-19 12:49:40 +02:00
const messageBuffer = xx.sendMessage(handshake.session, handshakePayload)
wrapped.writeLP(encode1(messageBuffer))
2019-11-27 14:19:35 +01:00
// Stage 2 - finish handshake
2020-06-19 12:49:40 +02:00
receivedMessageBuffer = decode2((await wrapped.readLP()).slice())
xx.recvMessage(handshake.session, receivedMessageBuffer)
return { wrapped, handshake }
})()
])
2019-11-27 14:19:35 +01:00
try {
2020-06-19 12:49:40 +02:00
const wrappedOutbound = Wrap(outbound.conn)
wrappedOutbound.write(new BufferList([Buffer.from('test')]))
2019-11-27 14:19:35 +01:00
// Check that noise message is prefixed with 16-bit big-endian unsigned integer
2020-06-19 12:49:40 +02:00
const receivedEncryptedPayload = (await wrapped.read()).slice()
const dataLength = receivedEncryptedPayload.readInt16BE(0)
const data = receivedEncryptedPayload.slice(2, dataLength + 2)
const { plaintext: decrypted, valid } = handshake.decrypt(data, handshake.session)
// Decrypted data should match
2020-06-19 12:49:40 +02:00
assert(decrypted.equals(Buffer.from('test')))
assert(valid)
} catch (e) {
2020-06-19 12:49:40 +02:00
assert(false, e.message)
}
2020-06-19 12:49:40 +02:00
})
2019-12-24 16:25:49 +01:00
2020-06-19 12:49:40 +02:00
it('should test large payloads', async function () {
this.timeout(10000)
2019-12-24 16:25:49 +01:00
try {
2020-06-19 13:06:31 +02:00
const noiseInit = new Noise(undefined, undefined)
const noiseResp = new Noise(undefined, undefined)
2019-12-24 16:25:49 +01:00
2020-06-19 12:49:40 +02:00
const [inboundConnection, outboundConnection] = DuplexPair()
2019-12-24 16:25:49 +01:00
const [outbound, inbound] = await Promise.all([
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
2020-06-19 12:49:40 +02:00
noiseResp.secureInbound(remotePeer, inboundConnection, localPeer)
])
const wrappedInbound = Wrap(inbound.conn)
const wrappedOutbound = Wrap(outbound.conn)
2019-12-24 16:25:49 +01:00
2020-06-19 12:49:40 +02:00
const largePlaintext = randomBytes(100000)
wrappedOutbound.writeLP(largePlaintext)
const response = await wrappedInbound.read(100000)
2019-12-24 20:36:16 +01:00
2020-06-19 12:49:40 +02:00
expect(response.length).equals(largePlaintext.length)
2019-12-24 16:25:49 +01:00
} catch (e) {
2020-06-19 12:49:40 +02:00
assert(false, e.message)
2019-12-24 16:25:49 +01:00
}
2020-06-19 12:49:40 +02:00
})
2020-01-15 17:27:32 +01:00
2020-06-19 12:49:40 +02:00
it.skip('should communicate through encrypted streams with noise pipes', async () => {
2020-01-15 17:27:32 +01:00
try {
2020-06-19 12:49:40 +02:00
const staticKeysInitiator = generateKeypair()
const noiseInit = new Noise(staticKeysInitiator.privateKey)
const staticKeysResponder = generateKeypair()
const noiseResp = new Noise(staticKeysResponder.privateKey)
2020-01-15 17:27:32 +01:00
// Prepare key cache for noise pipes
2020-06-19 12:49:40 +02:00
KeyCache.store(localPeer, staticKeysInitiator.publicKey)
KeyCache.store(remotePeer, staticKeysResponder.publicKey)
2020-01-15 17:27:32 +01:00
2020-06-19 12:49:40 +02:00
const xxSpy = sandbox.spy(noiseInit, 'performXXHandshake')
const xxFallbackSpy = sandbox.spy(noiseInit, 'performXXFallbackHandshake')
2020-01-16 17:49:41 +01:00
2020-06-19 12:49:40 +02:00
const [inboundConnection, outboundConnection] = DuplexPair()
2020-01-15 17:27:32 +01:00
const [outbound, inbound] = await Promise.all([
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
2020-06-19 12:49:40 +02:00
noiseResp.secureInbound(remotePeer, inboundConnection, localPeer)
])
const wrappedInbound = Wrap(inbound.conn)
const wrappedOutbound = Wrap(outbound.conn)
2020-01-15 17:27:32 +01:00
2020-06-19 12:49:40 +02:00
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
2020-06-19 12:49:40 +02:00
assert(xxSpy.notCalled)
assert(xxFallbackSpy.notCalled)
2020-01-15 17:27:32 +01:00
} catch (e) {
2020-06-19 12:49:40 +02:00
assert(false, e.message)
2020-01-15 17:27:32 +01:00
}
2020-06-19 12:49:40 +02:00
})
2020-01-15 17:27:32 +01:00
2020-06-19 12:49:40 +02:00
it.skip('IK -> XX fallback: initiator has invalid remote static key', async () => {
2020-01-15 17:27:32 +01:00
try {
2020-06-19 12:49:40 +02:00
const staticKeysInitiator = generateKeypair()
const noiseInit = new Noise(staticKeysInitiator.privateKey)
const noiseResp = new Noise()
const xxSpy = sandbox.spy(noiseInit, 'performXXFallbackHandshake')
2020-01-15 17:27:32 +01:00
// Prepare key cache for noise pipes
2020-06-19 12:49:40 +02:00
KeyCache.resetStorage()
KeyCache.store(localPeer, staticKeysInitiator.publicKey)
KeyCache.store(remotePeer, generateKeypair().publicKey)
2020-01-15 17:27:32 +01:00
2020-06-19 12:49:40 +02:00
const [inboundConnection, outboundConnection] = DuplexPair()
2020-01-15 17:27:32 +01:00
const [outbound, inbound] = await Promise.all([
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
2020-06-19 12:49:40 +02:00
noiseResp.secureInbound(remotePeer, inboundConnection, localPeer)
])
2020-01-16 17:49:41 +01:00
2020-06-19 12:49:40 +02:00
const wrappedInbound = Wrap(inbound.conn)
const wrappedOutbound = Wrap(outbound.conn)
2020-01-16 17:49:41 +01:00
2020-06-19 12:49:40 +02:00
wrappedOutbound.writeLP(Buffer.from('test fallback'))
const response = await wrappedInbound.readLP()
expect(response.toString()).equal('test fallback')
2020-06-19 12:49:40 +02:00
assert(xxSpy.calledOnce, 'XX Fallback method was never called.')
2020-01-15 17:27:32 +01:00
} catch (e) {
2020-06-19 12:49:40 +02:00
assert(false, e.message)
2020-01-15 17:27:32 +01:00
}
2020-06-19 12:49:40 +02:00
})
// this didn't work before but we didn't verify decryption
it.skip('IK -> XX fallback: responder has disabled noise pipes', async () => {
try {
const staticKeysInitiator = generateKeypair()
const noiseInit = new Noise(staticKeysInitiator.privateKey)
const staticKeysResponder = generateKeypair()
const noiseResp = new Noise(staticKeysResponder.privateKey, undefined, false)
const xxSpy = sandbox.spy(noiseInit, 'performXXFallbackHandshake')
// Prepare key cache for noise pipes
KeyCache.store(localPeer, staticKeysInitiator.publicKey)
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(xxSpy.calledOnce, 'XX Fallback method was never called.')
} catch (e) {
assert(false, e.message)
}
})
it.skip('Initiator starts with XX (pipes disabled), responder has enabled noise pipes', async () => {
2020-01-17 23:50:41 +01:00
try {
2020-06-19 12:49:40 +02:00
const staticKeysInitiator = generateKeypair()
const noiseInit = new Noise(staticKeysInitiator.privateKey, undefined, false)
const staticKeysResponder = generateKeypair()
2020-01-17 23:50:41 +01:00
2020-06-19 12:49:40 +02:00
const noiseResp = new Noise(staticKeysResponder.privateKey)
const xxInitSpy = sandbox.spy(noiseInit, 'performXXHandshake')
const xxRespSpy = sandbox.spy(noiseResp, 'performXXFallbackHandshake')
2020-01-17 23:50:41 +01:00
// Prepare key cache for noise pipes
2020-06-19 12:49:40 +02:00
KeyCache.store(localPeer, staticKeysInitiator.publicKey)
2020-01-17 23:50:41 +01:00
2020-06-19 12:49:40 +02:00
const [inboundConnection, outboundConnection] = DuplexPair()
2020-01-17 23:50:41 +01:00
const [outbound, inbound] = await Promise.all([
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
2020-06-19 12:49:40 +02:00
noiseResp.secureInbound(remotePeer, inboundConnection, localPeer)
])
2020-01-17 23:50:41 +01:00
2020-06-19 12:49:40 +02:00
const wrappedInbound = Wrap(inbound.conn)
const wrappedOutbound = Wrap(outbound.conn)
2020-01-17 23:50:41 +01:00
2020-06-19 12:49:40 +02:00
wrappedOutbound.writeLP(Buffer.from('test fallback'))
const response = await wrappedInbound.readLP()
expect(response.toString()).equal('test fallback')
2020-01-17 23:50:41 +01:00
2020-06-19 12:49:40 +02:00
assert(xxInitSpy.calledOnce, 'XX method was never called.')
assert(xxRespSpy.calledOnce, 'XX Fallback method was never called.')
2020-01-17 23:50:41 +01:00
} catch (e) {
2020-06-19 12:49:40 +02:00
assert(false, e.message)
2020-01-17 23:50:41 +01:00
}
2020-06-19 12:49:40 +02:00
})
2020-01-18 17:00:31 +01:00
2020-06-19 12:49:40 +02:00
it.skip('IK: responder has no remote static key', async () => {
2020-01-18 17:00:31 +01:00
try {
2020-06-19 12:49:40 +02:00
const staticKeysInitiator = generateKeypair()
const noiseInit = new Noise(staticKeysInitiator.privateKey)
const staticKeysResponder = generateKeypair()
2020-01-18 17:00:31 +01:00
2020-06-19 12:49:40 +02:00
const noiseResp = new Noise(staticKeysResponder.privateKey)
const ikInitSpy = sandbox.spy(noiseInit, 'performIKHandshake')
const xxFallbackInitSpy = sandbox.spy(noiseInit, 'performXXFallbackHandshake')
const ikRespSpy = sandbox.spy(noiseResp, 'performIKHandshake')
2020-01-18 17:00:31 +01:00
// Prepare key cache for noise pipes
2020-06-19 12:49:40 +02:00
KeyCache.resetStorage()
KeyCache.store(remotePeer, staticKeysResponder.publicKey)
2020-01-18 17:00:31 +01:00
2020-06-19 12:49:40 +02:00
const [inboundConnection, outboundConnection] = DuplexPair()
2020-01-18 17:00:31 +01:00
const [outbound, inbound] = await Promise.all([
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
2020-06-19 12:49:40 +02:00
noiseResp.secureInbound(remotePeer, inboundConnection, localPeer)
])
2020-01-18 17:00:31 +01:00
2020-06-19 12:49:40 +02:00
const wrappedInbound = Wrap(inbound.conn)
const wrappedOutbound = Wrap(outbound.conn)
2020-01-18 17:00:31 +01:00
2020-06-19 12:49:40 +02:00
wrappedOutbound.writeLP(Buffer.from('test fallback'))
const response = await wrappedInbound.readLP()
expect(response.toString()).equal('test fallback')
2020-01-18 17:00:31 +01:00
2020-06-19 12:49:40 +02:00
assert(ikInitSpy.calledOnce, 'IK handshake was not called.')
assert(ikRespSpy.calledOnce, 'IK handshake was not called.')
assert(xxFallbackInitSpy.notCalled, 'XX Fallback method was called.')
2020-01-18 17:00:31 +01:00
} catch (e) {
2020-06-19 12:49:40 +02:00
assert(false, e.message)
2020-01-18 17:00:31 +01:00
}
2020-06-19 12:49:40 +02:00
})
2020-02-08 12:23:35 +01:00
2020-06-19 12:49:40 +02:00
it('should working without remote peer provided in incoming connection', async () => {
2020-02-08 12:23:35 +01:00
try {
2020-06-19 12:49:40 +02:00
const staticKeysInitiator = generateKeypair()
const noiseInit = new Noise(staticKeysInitiator.privateKey)
const staticKeysResponder = generateKeypair()
const noiseResp = new Noise(staticKeysResponder.privateKey)
2020-02-08 12:23:35 +01:00
// Prepare key cache for noise pipes
2020-06-19 12:49:40 +02:00
KeyCache.store(localPeer, staticKeysInitiator.publicKey)
KeyCache.store(remotePeer, staticKeysResponder.publicKey)
2020-02-08 12:23:35 +01:00
2020-06-19 12:49:40 +02:00
const [inboundConnection, outboundConnection] = DuplexPair()
2020-02-08 12:23:35 +01:00
const [outbound, inbound] = await Promise.all([
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
2020-06-19 12:49:40 +02:00
noiseResp.secureInbound(remotePeer, inboundConnection)
])
const wrappedInbound = Wrap(inbound.conn)
const wrappedOutbound = Wrap(outbound.conn)
2020-02-08 12:23:35 +01:00
2020-06-19 12:49:40 +02:00
wrappedOutbound.writeLP(Buffer.from('test v2'))
const response = await wrappedInbound.readLP()
expect(response.toString()).equal('test v2')
2020-02-08 12:23:35 +01:00
2020-06-19 12:49:40 +02:00
assert(inbound.remotePeer.marshalPubKey().equals(localPeer.marshalPubKey()))
assert(outbound.remotePeer.marshalPubKey().equals(remotePeer.marshalPubKey()))
2020-02-08 12:23:35 +01:00
} catch (e) {
2020-06-19 12:49:40 +02:00
assert(false, e.message)
2020-02-08 12:23:35 +01:00
}
2020-06-19 12:49:40 +02:00
})
2020-04-17 10:56:38 +02:00
2020-06-19 12:49:40 +02:00
it('should accept and return early data from remote peer', async () => {
2020-04-17 10:56:38 +02:00
try {
const localPeerEarlyData = Buffer.from('early data')
2020-06-19 12:49:40 +02:00
const staticKeysInitiator = generateKeypair()
const noiseInit = new Noise(staticKeysInitiator.privateKey, localPeerEarlyData)
const staticKeysResponder = generateKeypair()
const noiseResp = new Noise(staticKeysResponder.privateKey)
2020-04-17 10:56:38 +02:00
// Prepare key cache for noise pipes
2020-06-19 12:49:40 +02:00
KeyCache.store(localPeer, staticKeysInitiator.publicKey)
KeyCache.store(remotePeer, staticKeysResponder.publicKey)
2020-04-17 10:56:38 +02:00
2020-06-19 12:49:40 +02:00
const [inboundConnection, outboundConnection] = DuplexPair()
2020-04-17 10:56:38 +02:00
const [outbound, inbound] = await Promise.all([
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
2020-06-19 12:49:40 +02:00
noiseResp.secureInbound(remotePeer, inboundConnection)
])
2020-04-17 10:56:38 +02:00
2020-04-17 11:07:58 +02:00
assert(inbound.remoteEarlyData.equals(localPeerEarlyData))
assert(outbound.remoteEarlyData.equals(Buffer.alloc(0)))
2020-04-17 10:56:38 +02:00
} catch (e) {
2020-06-19 12:49:40 +02:00
assert(false, e.message)
2020-04-17 10:56:38 +02:00
}
2020-06-19 12:49:40 +02:00
})
})