Skip to content

Commit 7a0d8c0

Browse files
authored
chore: clean up logic (#165)
- decodePayload is synchronous - remove isValidPeerId - only decode peer id once - don't use the return value of verifySignedPayload anywhere - remove duplicate length check, use isValidPublicKey
1 parent 1e3d207 commit 7a0d8c0

6 files changed

Lines changed: 15 additions & 21 deletions

File tree

src/handshake-ik.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class IKHandshake implements IHandshake {
7373
throw new Error('ik handshake stage 0 decryption validation fail')
7474
}
7575
logger('IK Stage 0 - Responder got message, going to verify payload.')
76-
const decodedPayload = await decodePayload(plaintext)
76+
const decodedPayload = decodePayload(plaintext)
7777
this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload)
7878
await verifySignedPayload(this.session.hs.rs, decodedPayload, this.remotePeer)
7979
this.setRemoteEarlyData(decodedPayload.data)
@@ -99,7 +99,7 @@ export class IKHandshake implements IHandshake {
9999
if (!valid) {
100100
throw new Error('ik stage 1 decryption validation fail')
101101
}
102-
const decodedPayload = await decodePayload(plaintext)
102+
const decodedPayload = decodePayload(plaintext)
103103
this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload)
104104
await verifySignedPayload(receivedMessageBuffer.ns.slice(0, 32), decodedPayload, this.remotePeer)
105105
this.setRemoteEarlyData(decodedPayload.data)

src/handshake-xx-fallback.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class XXFallbackHandshake extends XXHandshake {
6969

7070
logger("Initiator going to check remote's signature...")
7171
try {
72-
const decodedPayload = await decodePayload(plaintext)
72+
const decodedPayload = decodePayload(plaintext)
7373
this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload)
7474
await verifySignedPayload(this.session.hs.rs, decodedPayload, this.remotePeer)
7575
this.setRemoteEarlyData(decodedPayload.data)

src/handshake-xx.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ export class XXHandshake implements IHandshake {
9494

9595
logger("Initiator going to check remote's signature...")
9696
try {
97-
const decodedPayload = await decodePayload(plaintext)
97+
const decodedPayload = decodePayload(plaintext)
9898
this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload)
99-
this.remotePeer = await verifySignedPayload(this.session.hs.rs, decodedPayload, this.remotePeer)
99+
await verifySignedPayload(this.session.hs.rs, decodedPayload, this.remotePeer)
100100
this.setRemoteEarlyData(decodedPayload.data)
101101
} catch (e) {
102102
const err = e as Error
@@ -129,7 +129,7 @@ export class XXHandshake implements IHandshake {
129129
logger('Stage 2 - Responder received the message, finished handshake.')
130130

131131
try {
132-
const decodedPayload = await decodePayload(plaintext)
132+
const decodedPayload = decodePayload(plaintext)
133133
this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload)
134134
await verifySignedPayload(this.session.hs.rs, decodedPayload, this.remotePeer)
135135
this.setRemoteEarlyData(decodedPayload.data)

src/handshakes/ik.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class IK extends AbstractHandshake {
108108
this.mixHash(hs.ss, hs.re)
109109
this.mixKey(hs.ss, this.dh(hs.s.privateKey, hs.re))
110110
const { plaintext: ns, valid: valid1 } = this.decryptAndHash(hs.ss, message.ns)
111-
if (valid1 && ns.length === 32 && isValidPublicKey(ns)) {
111+
if (valid1 && isValidPublicKey(ns)) {
112112
hs.rs = ns
113113
}
114114
this.mixKey(hs.ss, this.dh(hs.s.privateKey, hs.rs))

src/handshakes/xx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class XX extends AbstractHandshake {
8787
}
8888
this.mixKey(hs.ss, this.dh(hs.e.privateKey, hs.re))
8989
const { plaintext: ns, valid: valid1 } = this.decryptAndHash(hs.ss, message.ns)
90-
if (valid1 && ns.length === 32 && isValidPublicKey(ns)) {
90+
if (valid1 && isValidPublicKey(ns)) {
9191
hs.rs = ns
9292
}
9393
this.mixKey(hs.ss, this.dh(hs.e.privateKey, hs.rs))
@@ -97,7 +97,7 @@ export class XX extends AbstractHandshake {
9797

9898
private readMessageC (hs: HandshakeState, message: MessageBuffer): {h: bytes, plaintext: bytes, valid: boolean, cs1: CipherState, cs2: CipherState} {
9999
const { plaintext: ns, valid: valid1 } = this.decryptAndHash(hs.ss, message.ns)
100-
if (valid1 && ns.length === 32 && isValidPublicKey(ns)) {
100+
if (valid1 && isValidPublicKey(ns)) {
101101
hs.rs = ns
102102
}
103103
if (!hs.e) {

src/utils.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ export function getHandshakePayload (publicKey: bytes): bytes {
6262
return uint8ArrayConcat([prefix, publicKey], prefix.length + publicKey.length)
6363
}
6464

65-
async function isValidPeerId (peerId: PeerId, publicKeyProtobuf: bytes): Promise<boolean> {
66-
const generatedPeerId = await peerIdFromKeys(publicKeyProtobuf)
67-
return generatedPeerId.equals(peerId)
68-
}
69-
7065
/**
7166
* Verifies signed payload, throws on any irregularities.
7267
*
@@ -80,31 +75,30 @@ export async function verifySignedPayload (
8075
payload: pb.NoiseHandshakePayload,
8176
remotePeer: PeerId
8277
): Promise<PeerId> {
83-
const identityKey = payload.identityKey
84-
if (!(await isValidPeerId(remotePeer, identityKey))) {
78+
// Unmarshaling from PublicKey protobuf
79+
const payloadPeerId = await peerIdFromKeys(payload.identityKey)
80+
if (!payloadPeerId.equals(remotePeer)) {
8581
throw new Error("Peer ID doesn't match libp2p public key.")
8682
}
8783
const generatedPayload = getHandshakePayload(noiseStaticKey)
88-
// Unmarshaling from PublicKey protobuf
89-
const peerId = await peerIdFromKeys(identityKey)
9084

91-
if (peerId.publicKey == null) {
85+
if (payloadPeerId.publicKey == null) {
9286
throw new Error('PublicKey was missing from PeerId')
9387
}
9488

9589
if (payload.identitySig == null) {
9690
throw new Error('Signature was missing from message')
9791
}
9892

99-
const publicKey = unmarshalPublicKey(peerId.publicKey)
93+
const publicKey = unmarshalPublicKey(payloadPeerId.publicKey)
10094

10195
const valid = await publicKey.verify(generatedPayload, payload.identitySig)
10296

10397
if (!valid) {
10498
throw new Error("Static key doesn't match to peer that signed payload!")
10599
}
106100

107-
return peerId
101+
return payloadPeerId
108102
}
109103

110104
export function isValidPublicKey (pk: bytes): boolean {

0 commit comments

Comments
 (0)