js-libp2p-noise/src/handshake.ts

116 lines
3.8 KiB
TypeScript
Raw Normal View History

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";
import { Buffer } from "buffer";
2019-11-22 12:52:59 +01:00
import {
createHandshakePayload,
decodeMessageBuffer,
encodeMessageBuffer,
getHandshakePayload,
signPayload
} from "./utils";
2019-11-25 13:09:40 +01:00
import {Noise, WrappedConnection} from "./noise";
2019-11-20 13:23:36 +01:00
type handshakeType = "XX";
2019-11-11 21:58:04 +01:00
export class Handshake {
2019-11-25 13:09:40 +01:00
public isInitiator: boolean;
2019-11-20 13:23:36 +01:00
private type: handshakeType;
private remotePublicKey: bytes;
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(
type: handshakeType,
2019-11-25 13:09:40 +01:00
isInitiator: boolean,
2019-11-11 21:58:04 +01:00
remotePublicKey: bytes,
prologue: bytes32,
staticKeys: KeyPair,
2019-11-22 12:52:59 +01:00
connection: WrappedConnection,
2019-11-20 13:23:36 +01:00
) {
this.type = type;
2019-11-25 13:09:40 +01:00
this.isInitiator = isInitiator;
2019-11-20 13:23:36 +01:00
this.remotePublicKey = remotePublicKey;
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
this.xx = new XXHandshake();
2019-11-20 13:23:36 +01:00
}
2019-11-20 22:52:08 +01:00
// stage 0
2019-11-25 13:09:40 +01:00
async propose(earlyData?: bytes) : Promise<NoiseSession> {
const ns = await this.xx.initSession(this.isInitiator, this.prologue, this.staticKeys, this.remotePublicKey);
2019-11-20 22:52:08 +01:00
2019-11-25 13:09:40 +01:00
if (this.isInitiator) {
2019-11-21 13:38:39 +01:00
const signedPayload = signPayload(this.staticKeys.privateKey, getHandshakePayload(this.staticKeys.publicKey));
const handshakePayload = await createHandshakePayload(
this.staticKeys.publicKey,
signedPayload,
earlyData,
this.staticKeys.privateKey
);
const message = Buffer.concat([Buffer.alloc(0), handshakePayload]);
const messageBuffer = await this.xx.sendMessage(ns, message);
2019-11-22 12:52:59 +01:00
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
2019-11-20 22:52:08 +01:00
} else {
const receivedMessageBuffer = (await this.connection.readLP()).slice();
2019-11-22 12:52:59 +01:00
const plaintext = await this.xx.recvMessage(ns, decodeMessageBuffer(receivedMessageBuffer));
2019-11-20 21:38:14 +01:00
}
2019-11-20 22:52:08 +01:00
return ns;
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-25 13:09:40 +01:00
async exchange(session: NoiseSession) : Promise<void> {
if (this.isInitiator) {
2019-11-21 13:38:39 +01:00
const receivedMessageBuffer = (await this.connection.readLP()).slice();
2019-11-22 12:52:59 +01:00
const plaintext = await this.xx.recvMessage(session, decodeMessageBuffer(receivedMessageBuffer));
2019-11-21 13:38:39 +01:00
} else {
// create payload as responder
const signedPayload = signPayload(this.staticKeys.privateKey, getHandshakePayload(this.staticKeys.publicKey));
const handshakePayload = await createHandshakePayload(this.remotePublicKey, signedPayload);
2019-11-20 13:23:36 +01:00
2019-11-21 13:38:39 +01:00
const message = Buffer.concat([Buffer.alloc(0), handshakePayload]);
const messageBuffer = await this.xx.sendMessage(session, message);
2019-11-22 12:52:59 +01:00
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
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-25 13:09:40 +01:00
async finish(session: NoiseSession) : Promise<void> {
if (this.isInitiator) {
2019-11-21 13:38:39 +01:00
const messageBuffer = await this.xx.sendMessage(session, Buffer.alloc(0));
2019-11-22 12:52:59 +01:00
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
2019-11-21 13:38:39 +01:00
} else {
const receivedMessageBuffer = (await this.connection.readLP()).slice();
2019-11-22 12:52:59 +01:00
const plaintext = await this.xx.recvMessage(session, decodeMessageBuffer(receivedMessageBuffer));
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
}