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

153 lines
5.8 KiB
TypeScript
Raw Normal View History

2019-11-27 08:39:06 +01:00
import { Buffer } from "buffer";
2019-12-24 20:54:45 +01:00
import { XXHandshake } from "./handshakes/xx";
2019-12-03 13:39:33 +01:00
import { KeyPair, PeerId } from "./@types/libp2p";
2019-12-24 20:54:45 +01:00
import { bytes, bytes32 } from "./@types/basic";
import { NoiseSession } from "./@types/handshake";
import {HandshakeInterface} from "./@types/handshake-interface";
2019-11-22 12:52:59 +01:00
import {
createHandshakePayload,
getHandshakePayload,
2019-12-03 13:39:33 +01:00
signEarlyDataPayload,
signPayload,
verifySignedPayload,
2019-11-22 12:52:59 +01:00
} from "./utils";
2019-12-03 15:19:08 +01:00
import { logger } from "./logger";
2019-12-03 15:15:46 +01:00
import { decodeMessageBuffer, encodeMessageBuffer } from "./encoder";
2019-11-27 08:39:06 +01:00
import { WrappedConnection } from "./noise";
2019-11-20 13:23:36 +01:00
export class Handshake implements HandshakeInterface {
2019-11-25 13:09:40 +01:00
public isInitiator: boolean;
2019-11-28 17:32:46 +01:00
public session: NoiseSession;
2019-11-25 13:09:40 +01:00
2019-11-28 17:32:46 +01:00
private libp2pPrivateKey: bytes;
private libp2pPublicKey: bytes;
2019-11-20 13:23:36 +01:00
private prologue: bytes32;
private staticKeys: KeyPair;
2019-11-22 12:52:59 +01:00
private connection: WrappedConnection;
2019-12-03 13:39:33 +01:00
private remotePeer: PeerId;
2019-11-21 13:38:39 +01:00
private xx: XXHandshake;
2019-11-20 13:23:36 +01:00
constructor(
2019-11-25 13:09:40 +01:00
isInitiator: boolean,
2019-11-28 17:32:46 +01:00
libp2pPrivateKey: bytes,
libp2pPublicKey: bytes,
2019-11-11 21:58:04 +01:00
prologue: bytes32,
staticKeys: KeyPair,
2019-11-22 12:52:59 +01:00
connection: WrappedConnection,
2019-12-03 13:39:33 +01:00
remotePeer: PeerId,
2020-01-03 17:28:13 +01:00
ephemeralKeys?: KeyPair,
2019-11-27 14:19:35 +01:00
handshake?: XXHandshake,
2019-11-20 13:23:36 +01:00
) {
2019-11-25 13:09:40 +01:00
this.isInitiator = isInitiator;
2019-11-28 17:32:46 +01:00
this.libp2pPrivateKey = libp2pPrivateKey;
this.libp2pPublicKey = libp2pPublicKey;
2019-11-20 13:23:36 +01:00
this.prologue = prologue;
this.staticKeys = staticKeys;
2019-11-20 22:52:08 +01:00
this.connection = connection;
2019-12-03 13:39:33 +01:00
this.remotePeer = remotePeer;
2019-11-21 13:38:39 +01:00
2019-11-27 14:19:35 +01:00
this.xx = handshake || new XXHandshake();
2019-11-28 17:32:46 +01:00
this.session = this.xx.initSession(this.isInitiator, this.prologue, this.staticKeys);
2019-11-20 13:23:36 +01:00
}
2019-11-20 22:52:08 +01:00
// stage 0
public async propose(): Promise<void> {
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));
2019-11-22 12:52:59 +01:00
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
logger("Stage 0 - Initiator finished sending first message.");
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...");
2019-11-28 17:32:46 +01:00
const receivedMessageBuffer = decodeMessageBuffer((await this.connection.readLP()).slice());
2019-12-03 15:12:55 +01:00
this.xx.recvMessage(this.session, receivedMessageBuffer);
2019-12-02 11:24:30 +01:00
logger("Stage 0 - Responder received first message.");
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...');
2019-11-28 17:32:46 +01:00
const receivedMessageBuffer = decodeMessageBuffer((await this.connection.readLP()).slice());
2019-12-03 15:12:55 +01:00
const plaintext = this.xx.recvMessage(this.session, receivedMessageBuffer);
2019-12-02 10:48:19 +01:00
logger('Stage 1 - Initiator received the message. Got remote\'s static key.');
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 {
await verifySignedPayload(receivedMessageBuffer.ns, plaintext, this.remotePeer.id);
} 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.');
2019-11-28 17:32:46 +01:00
const signedPayload = signPayload(this.libp2pPrivateKey, getHandshakePayload(this.staticKeys.publicKey));
const signedEarlyDataPayload = signEarlyDataPayload(this.libp2pPrivateKey, Buffer.alloc(0));
2019-11-28 17:32:46 +01:00
const handshakePayload = await createHandshakePayload(
this.libp2pPublicKey,
this.libp2pPrivateKey,
signedPayload,
signedEarlyDataPayload,
2019-11-28 17:32:46 +01:00
);
2019-11-20 13:23:36 +01:00
2019-12-03 15:12:55 +01:00
const messageBuffer = this.xx.sendMessage(this.session, handshakePayload);
2019-11-22 12:52:59 +01:00
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
logger('Stage 1 - Responder sent the second handshake message with signed payload.')
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(earlyData?: bytes): 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.');
const signedPayload = signPayload(this.libp2pPrivateKey, getHandshakePayload(this.staticKeys.publicKey));
const signedEarlyDataPayload = signEarlyDataPayload(this.libp2pPrivateKey, earlyData || Buffer.alloc(0));
const handshakePayload = await createHandshakePayload(
this.libp2pPublicKey,
this.libp2pPrivateKey,
signedPayload,
signedEarlyDataPayload
);
2019-12-03 15:12:55 +01:00
const messageBuffer = this.xx.sendMessage(this.session, handshakePayload);
2019-11-22 12:52:59 +01:00
this.connection.writeLP(encodeMessageBuffer(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 = decodeMessageBuffer((await this.connection.readLP()).slice());
2019-12-03 15:12:55 +01:00
const plaintext = this.xx.recvMessage(this.session, receivedMessageBuffer);
2019-12-02 11:24:30 +01:00
logger('Stage 2 - Responder received the message, finished handshake. Got remote\'s static key.');
2019-12-03 13:39:33 +01:00
try {
await verifySignedPayload(receivedMessageBuffer.ns, plaintext, this.remotePeer.id);
} catch (e) {
throw new Error(`Error occurred while verifying signed payload: ${e.message}`);
}
2019-11-21 13:38:39 +01:00
}
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);
}
2019-12-02 10:48:19 +01:00
public decrypt(ciphertext: bytes, session: NoiseSession): bytes {
2019-11-25 13:09:40 +01:00
const cs = this.getCS(session, false);
return this.xx.decryptWithAd(cs, Buffer.alloc(0), ciphertext);
}
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
}