mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 12:52:15 +00:00
Update setting remote id
This commit is contained in:
parent
a5c45bfc53
commit
06d5034998
@ -56,9 +56,8 @@ export class IKHandshake implements IHandshake {
|
||||
try {
|
||||
const receivedMessageBuffer = decode1(receivedMsg);
|
||||
const plaintext = this.ik.recvMessage(this.session, receivedMessageBuffer);
|
||||
this.remotePeer = await getPeerIdFromPayload(plaintext);
|
||||
logger("IK Stage 0 - Responder got message, going to verify payload.");
|
||||
await verifySignedPayload(receivedMessageBuffer.ns, plaintext, this.remotePeer.id);
|
||||
this.remotePeer = await verifySignedPayload(receivedMessageBuffer.ns, plaintext, this.remotePeer);
|
||||
logger("IK Stage 0 - Responder successfully verified payload!");
|
||||
} catch (e) {
|
||||
logger("Responder breaking up with IK handshake in stage 0.");
|
||||
@ -77,7 +76,7 @@ export class IKHandshake implements IHandshake {
|
||||
logger("IK Stage 1 - Initiator got message, going to verify payload.");
|
||||
|
||||
try {
|
||||
await verifySignedPayload(receivedMessageBuffer.ns, plaintext, this.remotePeer.id);
|
||||
await verifySignedPayload(receivedMessageBuffer.ns, plaintext, this.remotePeer);
|
||||
logger("IK Stage 1 - Initiator successfully verified payload!");
|
||||
} catch (e) {
|
||||
logger("Initiator breaking up with IK handshake in stage 1.");
|
||||
|
@ -57,8 +57,7 @@ export class XXFallbackHandshake extends XXHandshake {
|
||||
|
||||
logger("Initiator going to check remote's signature...");
|
||||
try {
|
||||
this.remotePeer = await getPeerIdFromPayload(plaintext);
|
||||
await verifySignedPayload(receivedMessageBuffer.ns, plaintext, this.remotePeer.id);
|
||||
this.remotePeer = await verifySignedPayload(receivedMessageBuffer.ns, plaintext, this.remotePeer);
|
||||
} catch (e) {
|
||||
throw new Error(`Error occurred while verifying signed payload from responder: ${e.message}`);
|
||||
}
|
||||
|
@ -72,8 +72,7 @@ export class XXHandshake implements IHandshake {
|
||||
|
||||
logger("Initiator going to check remote's signature...");
|
||||
try {
|
||||
this.remotePeer = await getPeerIdFromPayload(plaintext);
|
||||
await verifySignedPayload(receivedMessageBuffer.ns, plaintext, this.remotePeer.id);
|
||||
this.remotePeer = await verifySignedPayload(receivedMessageBuffer.ns, plaintext, this.remotePeer);
|
||||
} catch (e) {
|
||||
throw new Error(`Error occurred while verifying signed payload: ${e.message}`);
|
||||
}
|
||||
@ -97,11 +96,10 @@ export class XXHandshake implements IHandshake {
|
||||
logger('Stage 2 - Responder waiting for third handshake message...');
|
||||
const receivedMessageBuffer = decode1(await this.connection.readLP());
|
||||
const plaintext = this.xx.recvMessage(this.session, receivedMessageBuffer);
|
||||
this.remotePeer = await getPeerIdFromPayload(plaintext);
|
||||
logger('Stage 2 - Responder received the message, finished handshake. Got remote\'s static key.');
|
||||
|
||||
try {
|
||||
await verifySignedPayload(receivedMessageBuffer.ns, plaintext, this.remotePeer.id);
|
||||
this.remotePeer = await verifySignedPayload(receivedMessageBuffer.ns, plaintext, this.remotePeer);
|
||||
} catch (e) {
|
||||
throw new Error(`Error occurred while verifying signed payload: ${e.message}`);
|
||||
}
|
||||
|
19
src/utils.ts
19
src/utils.ts
@ -82,7 +82,18 @@ async function isValidPeerId(peerId: bytes, publicKeyProtobuf: bytes) {
|
||||
return generatedPeerId.id.equals(peerId);
|
||||
}
|
||||
|
||||
export async function verifySignedPayload(noiseStaticKey: bytes, plaintext: bytes, peerId: bytes) {
|
||||
/**
|
||||
* Verifies signed payload and returns peer id that has sent the payload.
|
||||
* @param {bytes} noiseStaticKey - owner's noise static key
|
||||
* @param {bytes} plaintext - encoded payload
|
||||
* @param {PeerId} remotePeer - (optional) owner's libp2p peer ID
|
||||
* @returns {Promise<PeerId>} - peer ID of payload owner
|
||||
*/
|
||||
export async function verifySignedPayload(
|
||||
noiseStaticKey: bytes,
|
||||
plaintext: bytes,
|
||||
remotePeer?: PeerId
|
||||
): Promise<PeerId> {
|
||||
let receivedPayload;
|
||||
try {
|
||||
const NoiseHandshakePayload = await loadPayloadProto();
|
||||
@ -97,7 +108,9 @@ export async function verifySignedPayload(noiseStaticKey: bytes, plaintext: byte
|
||||
throw new Error("Failed to decode received payload. Reason: " + e.message);
|
||||
}
|
||||
|
||||
if (!(await isValidPeerId(peerId, receivedPayload.identityKey)) ) {
|
||||
remotePeer = remotePeer || await getPeerIdFromPayload(plaintext);
|
||||
|
||||
if (!(await isValidPeerId(remotePeer.id, receivedPayload.identityKey)) ) {
|
||||
throw new Error("Peer ID doesn't match libp2p public key.");
|
||||
}
|
||||
|
||||
@ -108,6 +121,8 @@ export async function verifySignedPayload(noiseStaticKey: bytes, plaintext: byte
|
||||
if (!publicKey.verify(generatedPayload, receivedPayload.identitySig)) {
|
||||
throw new Error("Static key doesn't match to peer that signed payload!");
|
||||
}
|
||||
|
||||
return remotePeer;
|
||||
}
|
||||
|
||||
export function getHkdf(ck: bytes32, ikm: bytes): Hkdf {
|
||||
|
@ -300,4 +300,35 @@ describe("Noise", () => {
|
||||
assert(false, e.message);
|
||||
}
|
||||
});
|
||||
|
||||
it("should working without remote peer provided in incoming connection", async() => {
|
||||
try {
|
||||
const staticKeysInitiator = generateKeypair();
|
||||
const noiseInit = new Noise(staticKeysInitiator.privateKey);
|
||||
const staticKeysResponder = generateKeypair();
|
||||
const noiseResp = new Noise(staticKeysResponder.privateKey);
|
||||
|
||||
// Prepare key cache for noise pipes
|
||||
KeyCache.store(localPeer, staticKeysInitiator.publicKey);
|
||||
KeyCache.store(remotePeer, staticKeysResponder.publicKey);
|
||||
|
||||
const [inboundConnection, outboundConnection] = DuplexPair();
|
||||
const [outbound, inbound] = await Promise.all([
|
||||
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
|
||||
noiseResp.secureInbound(remotePeer, inboundConnection),
|
||||
]);
|
||||
const wrappedInbound = Wrap(inbound.conn);
|
||||
const wrappedOutbound = Wrap(outbound.conn);
|
||||
|
||||
wrappedOutbound.writeLP(Buffer.from("test v2"));
|
||||
const response = await wrappedInbound.readLP();
|
||||
expect(response.toString()).equal("test v2");
|
||||
|
||||
assert(inbound.remotePeer.marshalPubKey().equals(localPeer.marshalPubKey()));
|
||||
assert(outbound.remotePeer.marshalPubKey().equals(remotePeer.marshalPubKey()));
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
assert(false, e.message);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user