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

Commit 840c217

Browse files
committed
fix: use fixed encoding/decoding everywhere
fix: exchange final nonce handshake over encryption
1 parent 72f05bf commit 840c217

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

src/handshake/finish.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ log.error = debug('libp2p:secio:error')
77
const DuplexPair = require('it-pair/duplex')
88
const pipe = require('it-pipe')
99
const lp = require('it-length-prefixed')
10+
const Wrap = require('it-pb-rpc')
1011
const { int32BEEncode, int32BEDecode } = lp
1112
const ensureBuffer = require('it-buffer')
1213

@@ -20,32 +21,29 @@ module.exports = async function finish (state, wrapped) {
2021

2122
const proto = state.protocols
2223

23-
wrapped.write(state.proposal.in.rand)
24-
const nonceBack = await wrapped.read(state.proposal.in.rand.length)
25-
26-
crypto.verifyNonce(state, nonceBack.slice())
27-
28-
log('3. finish - finish')
29-
30-
const network = wrapped.unwrap()
3124
const [secure, user] = DuplexPair()
25+
const network = wrapped.unwrap()
3226

3327
pipe(
34-
secure.source, // this is FROM the user
28+
secure, // this is FROM the user
3529
ensureBuffer,
3630
etm.createBoxStream(proto.local.cipher, proto.local.mac),
3731
lp.encode({ lengthEncoder: int32BEEncode }),
38-
network.sink // and gets piped INTO the network
39-
)
40-
41-
pipe(
42-
network.source, // this is FROM the network
32+
network, // and gets piped INTO and FROM the network
4333
lp.decode({ lengthDecoder: int32BEDecode }),
4434
ensureBuffer,
4535
etm.createUnboxStream(proto.remote.cipher, proto.remote.mac),
46-
secure.sink // and gets piped TO the user
36+
secure // and gets piped TO the user
4737
)
4838

39+
// Exchange nonces over the encrypted stream for final verification
40+
const shake = Wrap(user)
41+
shake.write(state.proposal.in.rand)
42+
const nonceBack = await shake.read(state.proposal.in.rand.length)
43+
crypto.verifyNonce(state, nonceBack.slice())
44+
45+
log('3. finish - finish')
46+
4947
// Awesome that's all folks.
50-
state.secure = user
48+
state.secure = shake.unwrap()
5149
}

src/handshake/propose.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict'
22

33
const crypto = require('./crypto')
4-
4+
const lp = require('it-length-prefixed')
5+
const { int32BEEncode } = lp
56
const debug = require('debug')
67
const log = debug('libp2p:secio')
78
log.error = debug('libp2p:secio:error')
@@ -11,8 +12,10 @@ log.error = debug('libp2p:secio:error')
1112
module.exports = async function propose (state, wrapped) {
1213
log('1. propose - start')
1314

14-
log('1. propose - writing proposal')
15-
await wrapped.writeLP(crypto.createProposal(state))
15+
const prop = crypto.createProposal(state)
16+
log('1. propose - writing proposal', prop)
17+
18+
await wrapped.write(lp.encode.single(prop, { lengthEncoder: int32BEEncode }))
1619

1720
log('1. propose - reading proposal')
1821
const msg = (await wrapped.readLP()).slice()

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ log.error = debug('libp2p:secio:error')
88
const handshake = require('./handshake')
99
const State = require('./state')
1010
const Wrap = require('it-pb-rpc')
11+
const { int32BEDecode, int32BEEncode } = require('it-length-prefixed')
1112

1213
async function secure (localPeer, duplex, remotePeer) { // returns duplex
1314
assert(localPeer, 'no local private key provided')
1415
assert(duplex, 'no connection for the handshake provided')
1516

1617
const state = new State(localPeer, remotePeer)
17-
const wrapped = Wrap(duplex)
18+
const wrapped = Wrap(duplex, { lengthDecoder: int32BEDecode, lengthEncoder: int32BEEncode })
1819
await handshake(state, wrapped)
1920

2021
return {

0 commit comments

Comments
 (0)