57 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-05-22 01:03:53 +02:00
'use strict'
const pull = require('pull-stream')
const handshake = require('pull-handshake')
2016-05-22 01:03:53 +02:00
const debug = require('debug')
const log = debug('libp2p:secio')
log.error = debug('libp2p:secio:error')
2016-05-22 01:03:53 +02:00
2016-05-22 21:46:19 +02:00
const etm = require('../etm')
const crypto = require('./crypto')
2016-05-22 20:04:59 +02:00
2016-05-22 18:36:31 +02:00
// step 3. Finish
// -- send expected message to verify encryption works (send local nonce)
module.exports = function finish (state, cb) {
2016-05-22 01:03:53 +02:00
log('3. finish - start')
2016-05-22 01:31:00 +02:00
const proto = state.protocols
const stream = state.shake.rest()
const shake = handshake({timeout: state.timeout})
pull(
stream,
etm.createUnboxStream(proto.remote.cipher, proto.remote.mac),
shake,
etm.createBoxStream(proto.local.cipher, proto.local.mac),
stream
)
2016-05-22 01:31:00 +02:00
shake.handshake.write(state.proposal.in.rand)
shake.handshake.read(state.proposal.in.rand.length, (err, nonceBack) => {
const fail = (err) => {
2016-05-26 20:36:06 +02:00
log.error(err)
state.secure.resolve({
source: pull.error(err),
sink (read) {
}
})
cb(err)
}
if (err) return fail(err)
try {
crypto.verifyNonce(state, nonceBack)
} catch (err) {
return fail(err)
2016-05-22 20:04:59 +02:00
}
2016-05-22 01:31:00 +02:00
log('3. finish - finish')
2016-05-22 01:31:00 +02:00
2016-05-22 20:04:59 +02:00
// Awesome that's all folks.
state.secure.resolve(shake.handshake.rest())
2016-05-22 20:04:59 +02:00
cb()
})
2016-05-22 01:03:53 +02:00
}