Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

Fix/handshake error #101

Merged
merged 3 commits into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/handshake/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ exports.generateKeys = (state, callback) => {
log('2.3. mac + cipher')

parallel([
(cb) => support.makeMacAndCipher(state.protocols.local, cb),
(cb) => support.makeMacAndCipher(state.protocols.remote, cb)
(_cb) => support.makeMacAndCipher(state.protocols.local, _cb),
(_cb) => support.makeMacAndCipher(state.protocols.remote, _cb)
], cb)
}
], callback)
Expand Down
6 changes: 3 additions & 3 deletions src/handshake/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ log.error = debug('libp2p:secio:error')

// step 2. Exchange
// -- exchange (signed) ephemeral keys. verify signatures.
module.exports = function exchange (state, cb) {
module.exports = function exchange (state, callback) {
log('2. exchange - start')

log('2. exchange - writing exchange')
Expand All @@ -27,9 +27,9 @@ module.exports = function exchange (state, cb) {
},
(cb) => crypto.generateKeys(state, cb)
], (err) => {
if (err) { return cb(err) }
if (err) { return callback(err) }

log('2. exchange - finish')
cb()
callback()
})
}
6 changes: 3 additions & 3 deletions src/handshake/finish.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const crypto = require('./crypto')

// step 3. Finish
// -- send expected message to verify encryption works (send local nonce)
module.exports = function finish (state, cb) {
module.exports = function finish (state, callback) {
log('3. finish - start')

const proto = state.protocols
Expand Down Expand Up @@ -40,7 +40,7 @@ module.exports = function finish (state, cb) {
sink (read) {
}
})
cb(err)
callback(err)
}

if (err) return fail(err)
Expand All @@ -55,6 +55,6 @@ module.exports = function finish (state, cb) {

// Awesome that's all folks.
state.secure.resolve(shake.handshake.rest())
cb()
callback()
})
}
2 changes: 1 addition & 1 deletion src/handshake/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = function handshake (state, callback) {
}

// signal when the handshake is finished so that plumbing can happen
callback()
callback(err)
})

return state.stream
Expand Down
6 changes: 3 additions & 3 deletions src/handshake/propose.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ log.error = debug('libp2p:secio:error')

// step 1. Propose
// -- propose cipher suite + send pubkeys + nonce
module.exports = function propose (state, cb) {
module.exports = function propose (state, callback) {
log('1. propose - start')

log('1. propose - writing proposal')
Expand All @@ -26,10 +26,10 @@ module.exports = function propose (state, cb) {
(cb) => crypto.selectProtocols(state, cb)
], (err) => {
if (err) {
return cb(err)
return callback(err)
}

log('1. propose - finish')
cb()
callback()
})
}
28 changes: 28 additions & 0 deletions test/secio.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const Listener = ms.Listener
const Dialer = ms.Dialer

const secio = require('../src')
const State = require('../src/state')
const handshake = require('../src/handshake')

describe('secio', () => {
let peerA
Expand Down Expand Up @@ -144,4 +146,30 @@ describe('secio', () => {
secio.encrypt(peerA, new Connection(p[0]), peerC, check)
secio.encrypt(peerB, new Connection(p[1]), peerA, check)
})

it('bubbles errors from handshake failures properly', (done) => {
const p = pair()
const timeout = 60 * 1000 * 5
const stateA = new State(peerA, peerC, timeout, () => { })
const stateB = new State(peerB, peerA, timeout, () => { })
const connA = new Connection(p[0])
const connB = new Connection(p[1])

function finish (err) {
expect(err).to.exist()
done()
}

pull(
connA,
handshake(stateA, finish),
connA
)

pull(
connB,
handshake(stateB, finish),
connB
)
})
})