mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 20:12:25 +00:00
Verify payload in stage 1
This commit is contained in:
parent
7b03a3df3b
commit
bf9ae90a5e
@ -80,7 +80,7 @@ export class Handshake {
|
|||||||
if (!libp2pRemotekey) {
|
if (!libp2pRemotekey) {
|
||||||
throw new Error("Missing remote's libp2p public key, can't verify signature.");
|
throw new Error("Missing remote's libp2p public key, can't verify signature.");
|
||||||
}
|
}
|
||||||
verifySignedPayload(receivedMessageBuffer.ns, plaintext, libp2pRemotekey);
|
await verifySignedPayload(receivedMessageBuffer.ns, plaintext, libp2pRemotekey);
|
||||||
} else {
|
} else {
|
||||||
logger('Stage 1 - Responder sending out first message with signed payload and static key.');
|
logger('Stage 1 - Responder sending out first message with signed payload and static key.');
|
||||||
const signedPayload = signPayload(this.libp2pPrivateKey, getHandshakePayload(this.staticKeys.publicKey));
|
const signedPayload = signPayload(this.libp2pPrivateKey, getHandshakePayload(this.staticKeys.publicKey));
|
||||||
|
@ -47,7 +47,7 @@ export class Noise implements NoiseConnection {
|
|||||||
public async secureOutbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecureOutbound> {
|
public async secureOutbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecureOutbound> {
|
||||||
const wrappedConnection = Wrap(connection);
|
const wrappedConnection = Wrap(connection);
|
||||||
const libp2pPublicKey = localPeer.pubKey.marshal();
|
const libp2pPublicKey = localPeer.pubKey.marshal();
|
||||||
const handshake = await this.performHandshake(wrappedConnection, true, libp2pPublicKey);
|
const handshake = await this.performHandshake(wrappedConnection, true, libp2pPublicKey, remotePeer);
|
||||||
const conn = await this.createSecureConnection(wrappedConnection, handshake);
|
const conn = await this.createSecureConnection(wrappedConnection, handshake);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -66,7 +66,7 @@ export class Noise implements NoiseConnection {
|
|||||||
public async secureInbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecureOutbound> {
|
public async secureInbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecureOutbound> {
|
||||||
const wrappedConnection = Wrap(connection);
|
const wrappedConnection = Wrap(connection);
|
||||||
const libp2pPublicKey = localPeer.pubKey.marshal();
|
const libp2pPublicKey = localPeer.pubKey.marshal();
|
||||||
const handshake = await this.performHandshake(wrappedConnection, false, libp2pPublicKey);
|
const handshake = await this.performHandshake(wrappedConnection, false, libp2pPublicKey, remotePeer);
|
||||||
const conn = await this.createSecureConnection(wrappedConnection, handshake);
|
const conn = await this.createSecureConnection(wrappedConnection, handshake);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -79,12 +79,13 @@ export class Noise implements NoiseConnection {
|
|||||||
connection: WrappedConnection,
|
connection: WrappedConnection,
|
||||||
isInitiator: boolean,
|
isInitiator: boolean,
|
||||||
libp2pPublicKey: bytes,
|
libp2pPublicKey: bytes,
|
||||||
|
remotePeer: PeerId,
|
||||||
): Promise<Handshake> {
|
): Promise<Handshake> {
|
||||||
const prologue = Buffer.from(this.protocol);
|
const prologue = Buffer.from(this.protocol);
|
||||||
const handshake = new Handshake(isInitiator, this.privateKey, libp2pPublicKey, prologue, this.staticKeys, connection);
|
const handshake = new Handshake(isInitiator, this.privateKey, libp2pPublicKey, prologue, this.staticKeys, connection);
|
||||||
|
|
||||||
await handshake.propose(this.earlyData);
|
await handshake.propose(this.earlyData);
|
||||||
await handshake.exchange();
|
await handshake.exchange(remotePeer.pubKey.marshal());
|
||||||
await handshake.finish();
|
await handshake.finish();
|
||||||
|
|
||||||
return handshake;
|
return handshake;
|
||||||
|
@ -90,11 +90,13 @@ export async function verifyPeerId(peerId: bytes, publicKey: bytes) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function verifySignedPayload(noiseStaticKey: bytes, plaintext: bytes, libp2pPublicKey: bytes) {
|
export async function verifySignedPayload(noiseStaticKey: bytes, plaintext: bytes, libp2pPublicKey: bytes) {
|
||||||
|
const NoiseHandshakePayload = await loadPayloadProto();
|
||||||
|
const receivedPayload = NoiseHandshakePayload.toObject(NoiseHandshakePayload.decode(plaintext));
|
||||||
const generatedPayload = getHandshakePayload(noiseStaticKey);
|
const generatedPayload = getHandshakePayload(noiseStaticKey);
|
||||||
|
|
||||||
if (!ed25519.verify(generatedPayload, signature, libp2pPublicKey)) {
|
if (!ed25519.verify(generatedPayload, receivedPayload.noiseStaticKeySignature, libp2pPublicKey)) {
|
||||||
throw new Error("Static key doesn't match to peer that signed payload!");
|
Promise.reject("Static key doesn't match to peer that signed payload!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ describe("Handshake", () => {
|
|||||||
await handshakeResponder.propose();
|
await handshakeResponder.propose();
|
||||||
|
|
||||||
await handshakeResponder.exchange();
|
await handshakeResponder.exchange();
|
||||||
await handshakeInitator.exchange();
|
await handshakeInitator.exchange(peerB.pubKey.marshal());
|
||||||
|
|
||||||
await handshakeInitator.finish();
|
await handshakeInitator.finish();
|
||||||
await handshakeResponder.finish();
|
await handshakeResponder.finish();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user