mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-06-21 09:11:46 +00:00
@ -5,6 +5,7 @@ import PeerId from "peer-id";
|
||||
export interface IHandshake {
|
||||
session: NoiseSession;
|
||||
remotePeer: PeerId;
|
||||
remoteEarlyData: Buffer;
|
||||
encrypt(plaintext: bytes, session: NoiseSession): bytes;
|
||||
decrypt(ciphertext: bytes, session: NoiseSession): {plaintext: bytes; valid: boolean};
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ export interface INoiseConnection {
|
||||
|
||||
export type SecureOutbound = {
|
||||
conn: any;
|
||||
remoteEarlyData: Buffer;
|
||||
remotePeer: PeerId;
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,7 @@ export class IKHandshake implements IHandshake {
|
||||
public isInitiator: boolean;
|
||||
public session: NoiseSession;
|
||||
public remotePeer!: PeerId;
|
||||
public remoteEarlyData: Buffer;
|
||||
|
||||
private payload: bytes;
|
||||
private prologue: bytes32;
|
||||
@ -49,6 +50,7 @@ export class IKHandshake implements IHandshake {
|
||||
}
|
||||
this.ik = handshake || new IK();
|
||||
this.session = this.ik.initSession(this.isInitiator, this.prologue, this.staticKeypair, remoteStaticKey);
|
||||
this.remoteEarlyData = Buffer.alloc(0)
|
||||
}
|
||||
|
||||
public async stage0(): Promise<void> {
|
||||
@ -73,6 +75,7 @@ export class IKHandshake implements IHandshake {
|
||||
const decodedPayload = await decodePayload(plaintext);
|
||||
this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload);
|
||||
await verifySignedPayload(this.session.hs.rs, decodedPayload, this.remotePeer);
|
||||
this.setRemoteEarlyData(decodedPayload.data);
|
||||
logger("IK Stage 0 - Responder successfully verified payload!");
|
||||
logRemoteEphemeralKey(this.session.hs.re)
|
||||
} catch (e) {
|
||||
@ -97,6 +100,7 @@ export class IKHandshake implements IHandshake {
|
||||
const decodedPayload = await decodePayload(plaintext);
|
||||
this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload);
|
||||
await verifySignedPayload(receivedMessageBuffer.ns.slice(0, 32), decodedPayload, this.remotePeer);
|
||||
this.setRemoteEarlyData(decodedPayload.data);
|
||||
logger("IK Stage 1 - Initiator successfully verified payload!");
|
||||
logRemoteEphemeralKey(this.session.hs.re)
|
||||
} catch (e) {
|
||||
@ -142,4 +146,10 @@ export class IKHandshake implements IHandshake {
|
||||
return encryption ? session.cs2 : session.cs1;
|
||||
}
|
||||
}
|
||||
|
||||
private setRemoteEarlyData(data: Uint8Array|null|undefined): void {
|
||||
if(data){
|
||||
this.remoteEarlyData = Buffer.from(data.buffer, data.byteOffset, data.length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -70,6 +70,7 @@ export class XXFallbackHandshake extends XXHandshake {
|
||||
const decodedPayload = await decodePayload(plaintext);
|
||||
this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload);
|
||||
await verifySignedPayload(this.session.hs.rs, decodedPayload, this.remotePeer);
|
||||
this.setRemoteEarlyData(decodedPayload.data)
|
||||
} catch (e) {
|
||||
throw new Error(`Error occurred while verifying signed payload from responder: ${e.message}`);
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ export class XXHandshake implements IHandshake {
|
||||
public isInitiator: boolean;
|
||||
public session: NoiseSession;
|
||||
public remotePeer!: PeerId;
|
||||
public remoteEarlyData: Buffer;
|
||||
|
||||
protected payload: bytes;
|
||||
protected connection: WrappedConnection;
|
||||
@ -53,6 +54,7 @@ export class XXHandshake implements IHandshake {
|
||||
}
|
||||
this.xx = handshake || new XX();
|
||||
this.session = this.xx.initSession(this.isInitiator, this.prologue, this.staticKeypair);
|
||||
this.remoteEarlyData = Buffer.alloc(0)
|
||||
}
|
||||
|
||||
// stage 0
|
||||
@ -94,6 +96,7 @@ export class XXHandshake implements IHandshake {
|
||||
const decodedPayload = await decodePayload(plaintext);
|
||||
this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload);
|
||||
this.remotePeer = await verifySignedPayload(receivedMessageBuffer.ns, decodedPayload, this.remotePeer);
|
||||
this.setRemoteEarlyData(decodedPayload.data)
|
||||
} catch (e) {
|
||||
throw new Error(`Error occurred while verifying signed payload: ${e.message}`);
|
||||
}
|
||||
@ -127,6 +130,7 @@ export class XXHandshake implements IHandshake {
|
||||
const decodedPayload = await decodePayload(plaintext);
|
||||
this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload);
|
||||
await verifySignedPayload(this.session.hs.rs, decodedPayload, this.remotePeer);
|
||||
this.setRemoteEarlyData(decodedPayload.data)
|
||||
} catch (e) {
|
||||
throw new Error(`Error occurred while verifying signed payload: ${e.message}`);
|
||||
}
|
||||
@ -160,4 +164,10 @@ export class XXHandshake implements IHandshake {
|
||||
return encryption ? session.cs2 : session.cs1;
|
||||
}
|
||||
}
|
||||
|
||||
protected setRemoteEarlyData(data: Uint8Array|null|undefined): void {
|
||||
if(data){
|
||||
this.remoteEarlyData = Buffer.from(data.buffer, data.byteOffset, data.length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -85,6 +85,7 @@ export class Noise implements INoiseConnection {
|
||||
|
||||
return {
|
||||
conn,
|
||||
remoteEarlyData: handshake.remoteEarlyData,
|
||||
remotePeer: handshake.remotePeer,
|
||||
}
|
||||
}
|
||||
@ -115,6 +116,7 @@ export class Noise implements INoiseConnection {
|
||||
|
||||
return {
|
||||
conn,
|
||||
remoteEarlyData: handshake.remoteEarlyData,
|
||||
remotePeer: handshake.remotePeer
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user