js-libp2p-noise/src/handshake.ts

126 lines
4.3 KiB
TypeScript
Raw Normal View History

2019-11-27 08:39:06 +01:00
import { Buffer } from "buffer";
2019-11-20 13:23:36 +01:00
import { bytes, bytes32 } from "./@types/basic";
2019-11-12 14:07:25 +01:00
import { NoiseSession, XXHandshake } from "./xx";
2019-11-20 22:52:08 +01:00
import { KeyPair } from "./@types/libp2p";
2019-11-22 12:52:59 +01:00
import {
createHandshakePayload,
decodeMessageBuffer,
encodeMessageBuffer,
getHandshakePayload,
2019-11-28 17:32:46 +01:00
logger, signEarlyDataPayload,
2019-11-27 08:39:06 +01:00
signPayload,
2019-11-22 12:52:59 +01:00
} from "./utils";
2019-11-27 08:39:06 +01:00
import { WrappedConnection } from "./noise";
2019-11-20 13:23:36 +01:00
2019-11-11 21:58:04 +01:00
export class Handshake {
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-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-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-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
2019-11-28 17:32:46 +01:00
async propose(earlyData?: bytes) : Promise<void> {
2019-11-25 13:09:40 +01:00
if (this.isInitiator) {
2019-11-28 17:32:46 +01:00
const signedPayload = signPayload(this.libp2pPrivateKey, getHandshakePayload(this.staticKeys.publicKey));
const signedEarlyDataPayload = signEarlyDataPayload(this.libp2pPrivateKey, earlyData || Buffer.alloc(0));
2019-11-21 13:38:39 +01:00
const handshakePayload = await createHandshakePayload(
2019-11-28 17:32:46 +01:00
this.libp2pPublicKey,
this.libp2pPrivateKey,
2019-11-21 13:38:39 +01:00
signedPayload,
2019-11-28 17:32:46 +01:00
signedEarlyDataPayload
2019-11-21 13:38:39 +01:00
);
2019-11-28 17:32:46 +01:00
const messageBuffer = await this.xx.sendMessage(this.session, handshakePayload);
2019-11-22 12:52:59 +01:00
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
2019-11-27 08:39:06 +01:00
2019-11-28 17:32:46 +01:00
logger("Stage 0 - Initiator finished proposing, sent signed NoiseHandshake payload.");
2019-11-20 22:52:08 +01:00
} else {
2019-11-28 17:32:46 +01:00
const receivedMessageBuffer = decodeMessageBuffer((await this.connection.readLP()).slice());
2019-11-20 22:52:08 +01:00
2019-11-28 17:32:46 +01:00
const plaintext = await this.xx.recvMessage(this.session, receivedMessageBuffer);
logger("Stage 0 - Responder received proposed message and remote static public key.");
}
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
2019-11-28 17:32:46 +01:00
async exchange() : Promise<void> {
2019-11-25 13:09:40 +01:00
if (this.isInitiator) {
2019-11-28 17:32:46 +01:00
const receivedMessageBuffer = decodeMessageBuffer((await this.connection.readLP()).slice());
const plaintext = await this.xx.recvMessage(this.session, receivedMessageBuffer);
2019-11-27 08:39:06 +01:00
logger('Stage 1 - Initiator received the message.');
2019-11-21 13:38:39 +01:00
} else {
// create payload as responder
2019-11-28 17:32:46 +01:00
const signedPayload = signPayload(this.libp2pPrivateKey, getHandshakePayload(this.staticKeys.publicKey));
const handshakePayload = await createHandshakePayload(
this.libp2pPublicKey,
this.libp2pPrivateKey,
signedPayload,
);
2019-11-20 13:23:36 +01:00
2019-11-28 17:32:46 +01:00
const messageBuffer = await this.xx.sendMessage(this.session, handshakePayload);
2019-11-22 12:52:59 +01:00
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
2019-11-27 08:39:06 +01:00
logger('Stage 1 - Responder sent the message.')
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
2019-11-28 17:32:46 +01:00
async finish() : Promise<void> {
2019-11-25 13:09:40 +01:00
if (this.isInitiator) {
2019-11-28 17:32:46 +01:00
const messageBuffer = await this.xx.sendMessage(this.session, Buffer.alloc(0));
2019-11-22 12:52:59 +01:00
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
2019-11-27 08:39:06 +01:00
logger('Stage 2 - Initiator sent message.');
2019-11-21 13:38:39 +01:00
} else {
const receivedMessageBuffer = (await this.connection.readLP()).slice();
2019-11-28 17:32:46 +01:00
const plaintext = await this.xx.recvMessage(this.session, decodeMessageBuffer(receivedMessageBuffer));
2019-11-27 08:39:06 +01:00
logger('Stage 2 - Responder received the message, finished handshake.')
2019-11-21 13:38:39 +01:00
}
2019-11-20 13:23:36 +01:00
}
2019-11-25 13:09:40 +01:00
encrypt(plaintext: bytes, session: NoiseSession): bytes {
const cs = this.getCS(session);
return this.xx.encryptWithAd(cs, Buffer.alloc(0), plaintext);
}
decrypt(ciphertext: bytes, session: NoiseSession): bytes {
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
}