Written handshake exchange

This commit is contained in:
morrigan 2019-11-21 13:38:39 +01:00
parent f6bc40baf4
commit b2d058291c
3 changed files with 47 additions and 24 deletions

View File

@ -2,56 +2,79 @@ import { bytes, bytes32 } from "./@types/basic";
import { NoiseSession, XXHandshake } from "./xx"; import { NoiseSession, XXHandshake } from "./xx";
import { KeyPair } from "./@types/libp2p"; import { KeyPair } from "./@types/libp2p";
import { Buffer } from "buffer"; import { Buffer } from "buffer";
import {createHandshakePayload, getHandshakePayload, signPayload} from "./utils";
type handshakeType = "XX"; type handshakeType = "XX";
export class Handshake { export class Handshake {
private type: handshakeType; private type: handshakeType;
private remotePublicKey: bytes; private remotePublicKey: bytes;
private signedPayload: bytes;
private prologue: bytes32; private prologue: bytes32;
private staticKeys: KeyPair; private staticKeys: KeyPair;
private connection: any; private connection: any;
private xx: XXHandshake;
constructor( constructor(
type: handshakeType, type: handshakeType,
remotePublicKey: bytes, remotePublicKey: bytes,
prologue: bytes32, prologue: bytes32,
signedPayload: bytes,
staticKeys: KeyPair, staticKeys: KeyPair,
connection, connection,
) { ) {
this.type = type; this.type = type;
this.remotePublicKey = remotePublicKey; this.remotePublicKey = remotePublicKey;
this.signedPayload = signedPayload;
this.prologue = prologue; this.prologue = prologue;
this.staticKeys = staticKeys; this.staticKeys = staticKeys;
this.connection = connection; this.connection = connection;
this.xx = new XXHandshake();
} }
// stage 0 // stage 0
async propose(isInitiator: boolean) : Promise<NoiseSession> { async propose(isInitiator: boolean, earlyData?: bytes) : Promise<NoiseSession> {
const xx = new XXHandshake(); const ns = await this.xx.initSession(isInitiator, this.prologue, this.staticKeys, this.remotePublicKey);
const ns = await xx.initSession(isInitiator, this.prologue, this.staticKeys, this.remotePublicKey);
if (isInitiator) { if (isInitiator) {
const message = Buffer.concat([Buffer.alloc(0), this.signedPayload]); const signedPayload = signPayload(this.staticKeys.privateKey, getHandshakePayload(this.staticKeys.publicKey));
const messageBuffer = await xx.sendMessage(ns, message); 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);
this.connection.writeLP(messageBuffer); this.connection.writeLP(messageBuffer);
} else { } else {
const receivedMessageBuffer = (await this.connection.readLP()).slice(); const receivedMessageBuffer = (await this.connection.readLP()).slice();
const plaintext = await xx.recvMessage(ns, receivedMessageBuffer); const plaintext = await this.xx.recvMessage(ns, receivedMessageBuffer);
} }
return ns; return ns;
} }
async exchange() : Promise<NoiseSession> { async exchange(isInitiator: boolean, session: NoiseSession) : Promise<void> {
if (isInitiator) {
const receivedMessageBuffer = (await this.connection.readLP()).slice();
const plaintext = await this.xx.recvMessage(session, receivedMessageBuffer);
} else {
// create payload as responder
const signedPayload = signPayload(this.staticKeys.privateKey, getHandshakePayload(this.staticKeys.publicKey));
const handshakePayload = await createHandshakePayload(this.remotePublicKey, signedPayload);
const message = Buffer.concat([Buffer.alloc(0), handshakePayload]);
const messageBuffer = await this.xx.sendMessage(session, message);
this.connection.writeLP(messageBuffer);
}
} }
async finish() : Promise<NoiseSession> { async finish(isInitiator: boolean, session: NoiseSession) : Promise<void> {
if (isInitiator) {
const messageBuffer = await this.xx.sendMessage(session, Buffer.alloc(0));
this.connection.writeLP(messageBuffer);
} else {
const receivedMessageBuffer = (await this.connection.readLP()).slice();
const plaintext = await this.xx.recvMessage(session, receivedMessageBuffer);
}
} }
} }

View File

@ -68,13 +68,12 @@ export class Noise implements NoiseConnection {
this.staticKeys = await generateKeypair(); this.staticKeys = await generateKeypair();
} }
const payload = getHandshakePayload(this.staticKeys.publicKey);
const signedPayload = signPayload(this.staticKeys.privateKey, payload);
const handshakePayload = await createHandshakePayload(this.staticKeys, signedPayload);
const prologue = Buffer.from(this.protocol); const prologue = Buffer.from(this.protocol);
const handshake = new Handshake('XX', remotePublicKey, prologue, handshakePayload, this.staticKeys, connection); const handshake = new Handshake('XX', remotePublicKey, prologue, this.staticKeys, connection);
const session = await handshake.propose(isInitiator);
const session = await handshake.propose(isInitiator, this.earlyData);
await handshake.exchange(isInitiator, session);
await handshake.finish(isInitiator, session);
return await encryptStreams(connection, session); return await encryptStreams(connection, session);
} }

View File

@ -21,15 +21,16 @@ export async function generateKeypair() : Promise<KeyPair> {
} }
export async function createHandshakePayload( export async function createHandshakePayload(
libp2pKeys: KeyPair, libp2pPublicKey: bytes,
signedPayload: bytes, signedPayload: bytes,
earlyData?: bytes, earlyData?: bytes,
libp2pPrivateKey?: bytes,
) : Promise<bytes> { ) : Promise<bytes> {
const NoiseHandshakePayload = await loadPayloadProto(); const NoiseHandshakePayload = await loadPayloadProto();
const payloadInit = NoiseHandshakePayload.create({ const payloadInit = NoiseHandshakePayload.create({
libp2pKey: libp2pKeys.publicKey, libp2pKey: libp2pPublicKey,
noiseStaticKeySignature: signedPayload, noiseStaticKeySignature: signedPayload,
...resolveEarlyDataPayload(libp2pKeys.privateKey, earlyData), ...resolveEarlyDataPayload(libp2pPrivateKey, earlyData),
}); });
return Buffer.from(NoiseHandshakePayload.encode(payloadInit).finish()); return Buffer.from(NoiseHandshakePayload.encode(payloadInit).finish());
@ -44,8 +45,8 @@ export const getHandshakePayload = (publicKey: bytes ) => Buffer.concat([Buffer.
export const getEarlyDataPayload = (earlyData: bytes) => Buffer.concat([Buffer.from("noise-libp2p-early-data:"), earlyData]); export const getEarlyDataPayload = (earlyData: bytes) => Buffer.concat([Buffer.from("noise-libp2p-early-data:"), earlyData]);
function resolveEarlyDataPayload(privateKey: bytes, earlyData?: bytes) : Object { function resolveEarlyDataPayload(privateKey?: bytes, earlyData?: bytes) : Object {
if (!earlyData) { if (!earlyData || !privateKey) {
return {}; return {};
} }