js-libp2p-noise/src/handshake-xx.ts

164 lines
5.8 KiB
TypeScript
Raw Normal View History

2019-11-27 08:39:06 +01:00
import { Buffer } from "buffer";
2020-01-05 19:09:59 +01:00
import { XX } from "./handshakes/xx";
2020-02-07 12:59:52 +01:00
import { KeyPair } from "./@types/libp2p";
2019-12-24 20:54:45 +01:00
import { bytes, bytes32 } from "./@types/basic";
import { NoiseSession } from "./@types/handshake";
2020-01-07 13:34:45 +01:00
import {IHandshake} from "./@types/handshake-interface";
2019-11-22 12:52:59 +01:00
import {
decodePayload,
2020-02-07 20:21:27 +01:00
getPeerIdFromPayload,
2019-12-03 13:39:33 +01:00
verifySignedPayload,
2019-11-22 12:52:59 +01:00
} from "./utils";
2020-04-17 09:48:55 +02:00
import {
logger,
logLocalStaticKeys,
logLocalEphemeralKeys,
logRemoteEphemeralKey,
logRemoteStaticKey,
logCipherState,
} from "./logger";
import {decode0, decode1, decode2, encode0, encode1, encode2} from "./encoder";
2019-11-27 08:39:06 +01:00
import { WrappedConnection } from "./noise";
2020-02-07 12:59:52 +01:00
import PeerId from "peer-id";
2019-11-20 13:23:36 +01:00
2020-01-07 13:34:45 +01:00
export class XXHandshake implements IHandshake {
2019-11-25 13:09:40 +01:00
public isInitiator: boolean;
2019-11-28 17:32:46 +01:00
public session: NoiseSession;
2020-02-07 20:21:27 +01:00
public remotePeer!: PeerId;
2019-11-25 13:09:40 +01:00
2020-01-11 20:20:57 +01:00
protected payload: bytes;
2020-01-05 19:00:16 +01:00
protected connection: WrappedConnection;
2020-01-05 19:09:59 +01:00
protected xx: XX;
2020-01-07 13:34:45 +01:00
protected staticKeypair: KeyPair;
2020-01-07 10:16:57 +01:00
private prologue: bytes32;
2019-11-20 13:23:36 +01:00
constructor(
2019-11-25 13:09:40 +01:00
isInitiator: boolean,
2020-01-07 16:59:41 +01:00
payload: bytes,
2019-11-11 21:58:04 +01:00
prologue: bytes32,
2020-01-07 13:34:45 +01:00
staticKeypair: KeyPair,
2019-11-22 12:52:59 +01:00
connection: WrappedConnection,
2020-02-07 20:21:27 +01:00
remotePeer?: PeerId,
2020-01-05 19:09:59 +01:00
handshake?: XX,
2019-11-20 13:23:36 +01:00
) {
2019-11-25 13:09:40 +01:00
this.isInitiator = isInitiator;
2020-01-07 16:59:41 +01:00
this.payload = payload;
2019-11-20 13:23:36 +01:00
this.prologue = prologue;
2020-01-07 13:34:45 +01:00
this.staticKeypair = staticKeypair;
2019-11-20 22:52:08 +01:00
this.connection = connection;
2020-02-07 20:21:27 +01:00
if(remotePeer) {
this.remotePeer = remotePeer;
}
2020-01-05 19:09:59 +01:00
this.xx = handshake || new XX();
2020-01-07 13:34:45 +01:00
this.session = this.xx.initSession(this.isInitiator, this.prologue, this.staticKeypair);
2019-11-20 13:23:36 +01:00
}
2019-11-20 22:52:08 +01:00
// stage 0
public async propose(): Promise<void> {
logLocalStaticKeys(this.session.hs.s)
2019-11-25 13:09:40 +01:00
if (this.isInitiator) {
2019-12-02 11:24:30 +01:00
logger("Stage 0 - Initiator starting to send first message.");
2019-12-03 15:12:55 +01:00
const messageBuffer = this.xx.sendMessage(this.session, Buffer.alloc(0));
2020-01-07 10:29:40 +01:00
this.connection.writeLP(encode0(messageBuffer));
logger("Stage 0 - Initiator finished sending first message.");
logLocalEphemeralKeys(this.session.hs.e)
2019-11-20 22:52:08 +01:00
} else {
2019-12-02 11:24:30 +01:00
logger("Stage 0 - Responder waiting to receive first message...");
2020-02-14 10:10:42 +01:00
const receivedMessageBuffer = decode0((await this.connection.readLP()).slice());
2020-03-01 19:05:53 +01:00
const {valid} = this.xx.recvMessage(this.session, receivedMessageBuffer);
if(!valid) {
throw new Error("xx handshake stage 0 validation fail");
}
2019-12-02 11:24:30 +01:00
logger("Stage 0 - Responder received first message.");
logRemoteEphemeralKey(this.session.hs.re)
2019-11-28 17:32:46 +01:00
}
2019-11-11 21:58:04 +01:00
}
2019-11-20 13:23:36 +01:00
2019-11-21 14:43:12 +01:00
// stage 1
public async exchange(): Promise<void> {
2019-11-25 13:09:40 +01:00
if (this.isInitiator) {
2019-12-02 11:24:30 +01:00
logger('Stage 1 - Initiator waiting to receive first message from responder...');
2020-02-14 10:10:42 +01:00
const receivedMessageBuffer = decode1((await this.connection.readLP()).slice());
2020-03-01 19:05:53 +01:00
const {plaintext, valid} = this.xx.recvMessage(this.session, receivedMessageBuffer);
if(!valid) {
throw new Error("xx handshake stage 1 validation fail");
}
logger('Stage 1 - Initiator received the message.');
logRemoteEphemeralKey(this.session.hs.re)
logRemoteStaticKey(this.session.hs.rs)
2019-12-02 12:53:00 +01:00
logger("Initiator going to check remote's signature...");
2019-12-03 13:39:33 +01:00
try {
const decodedPayload = await decodePayload(plaintext);
this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload);
this.remotePeer = await verifySignedPayload(receivedMessageBuffer.ns, decodedPayload, this.remotePeer);
2019-12-03 13:39:33 +01:00
} catch (e) {
throw new Error(`Error occurred while verifying signed payload: ${e.message}`);
}
logger("All good with the signature!");
2019-11-21 13:38:39 +01:00
} else {
2019-12-02 11:24:30 +01:00
logger('Stage 1 - Responder sending out first message with signed payload and static key.');
2020-01-07 16:59:41 +01:00
const messageBuffer = this.xx.sendMessage(this.session, this.payload);
2020-01-07 10:29:40 +01:00
this.connection.writeLP(encode1(messageBuffer));
logger('Stage 1 - Responder sent the second handshake message with signed payload.')
logLocalEphemeralKeys(this.session.hs.e)
2019-11-21 13:38:39 +01:00
}
2019-11-20 13:23:36 +01:00
}
2019-11-21 14:43:12 +01:00
// stage 2
public async finish(): Promise<void> {
2019-11-25 13:09:40 +01:00
if (this.isInitiator) {
2019-12-02 11:24:30 +01:00
logger('Stage 2 - Initiator sending third handshake message.');
2020-01-07 16:59:41 +01:00
const messageBuffer = this.xx.sendMessage(this.session, this.payload);
this.connection.writeLP(encode2(messageBuffer));
logger('Stage 2 - Initiator sent message with signed payload.');
2019-11-21 13:38:39 +01:00
} else {
2019-12-02 11:24:30 +01:00
logger('Stage 2 - Responder waiting for third handshake message...');
const receivedMessageBuffer = decode2((await this.connection.readLP()).slice());
2020-03-01 19:05:53 +01:00
const {plaintext, valid} = this.xx.recvMessage(this.session, receivedMessageBuffer);
if(!valid) {
throw new Error("xx handshake stage 2 validation fail");
}
logger('Stage 2 - Responder received the message, finished handshake.');
2019-12-03 13:39:33 +01:00
try {
const decodedPayload = await decodePayload(plaintext);
this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload);
2020-02-17 12:11:55 +01:00
await verifySignedPayload(this.session.hs.rs, decodedPayload, this.remotePeer);
2019-12-03 13:39:33 +01:00
} catch (e) {
throw new Error(`Error occurred while verifying signed payload: ${e.message}`);
}
2019-11-21 13:38:39 +01:00
}
logCipherState(this.session)
2019-11-20 13:23:36 +01:00
}
2019-11-25 13:09:40 +01:00
2019-12-02 10:48:19 +01:00
public encrypt(plaintext: bytes, session: NoiseSession): bytes {
2019-11-25 13:09:40 +01:00
const cs = this.getCS(session);
2019-12-02 10:48:19 +01:00
2019-11-25 13:09:40 +01:00
return this.xx.encryptWithAd(cs, Buffer.alloc(0), plaintext);
}
2020-03-01 19:05:53 +01:00
public decrypt(ciphertext: bytes, session: NoiseSession): {plaintext: bytes; valid: boolean} {
2019-11-25 13:09:40 +01:00
const cs = this.getCS(session, false);
return this.xx.decryptWithAd(cs, Buffer.alloc(0), ciphertext);
}
2020-01-13 16:33:58 +01:00
public getRemoteStaticKey(): bytes {
return this.session.hs.rs;
}
2019-11-25 13:09:40 +01:00
private getCS(session: NoiseSession, encryption = true) {
if (!session.cs1 || !session.cs2) {
throw new Error("Handshake not completed properly, cipher state does not exist.");
}
if (this.isInitiator) {
return encryption ? session.cs1 : session.cs2;
} else {
return encryption ? session.cs2 : session.cs1;
}
}
2019-11-11 21:58:04 +01:00
}