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

View File

@ -21,15 +21,16 @@ export async function generateKeypair() : Promise<KeyPair> {
}
export async function createHandshakePayload(
libp2pKeys: KeyPair,
libp2pPublicKey: bytes,
signedPayload: bytes,
earlyData?: bytes,
libp2pPrivateKey?: bytes,
) : Promise<bytes> {
const NoiseHandshakePayload = await loadPayloadProto();
const payloadInit = NoiseHandshakePayload.create({
libp2pKey: libp2pKeys.publicKey,
libp2pKey: libp2pPublicKey,
noiseStaticKeySignature: signedPayload,
...resolveEarlyDataPayload(libp2pKeys.privateKey, earlyData),
...resolveEarlyDataPayload(libp2pPrivateKey, earlyData),
});
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]);
function resolveEarlyDataPayload(privateKey: bytes, earlyData?: bytes) : Object {
if (!earlyData) {
function resolveEarlyDataPayload(privateKey?: bytes, earlyData?: bytes) : Object {
if (!earlyData || !privateKey) {
return {};
}