Use static key caching

This commit is contained in:
Belma Gutlic
2020-01-13 16:33:58 +01:00
parent 90af03ab02
commit 7d22967197
6 changed files with 29 additions and 23 deletions

View File

@ -24,6 +24,7 @@ export class IKHandshake implements IHandshake {
staticKeypair: KeyPair, staticKeypair: KeyPair,
connection: WrappedConnection, connection: WrappedConnection,
remotePeer: PeerId, remotePeer: PeerId,
remoteStaticKey: bytes,
handshake?: IK, handshake?: IK,
) { ) {
this.isInitiator = isInitiator; this.isInitiator = isInitiator;
@ -34,11 +35,7 @@ export class IKHandshake implements IHandshake {
this.remotePeer = remotePeer; this.remotePeer = remotePeer;
this.ik = handshake || new IK(); this.ik = handshake || new IK();
this.session = this.ik.initSession(this.isInitiator, this.prologue, this.staticKeypair, remoteStaticKey);
// Dummy data
// TODO: Load remote static keys if found
const remoteStaticKeys = this.staticKeypair;
this.session = this.ik.initSession(this.isInitiator, this.prologue, this.staticKeypair, remoteStaticKeys.publicKey);
} }
public decrypt(ciphertext: Buffer, session: NoiseSession): Buffer { public decrypt(ciphertext: Buffer, session: NoiseSession): Buffer {

View File

@ -114,6 +114,10 @@ export class XXHandshake implements IHandshake {
return this.xx.decryptWithAd(cs, Buffer.alloc(0), ciphertext); return this.xx.decryptWithAd(cs, Buffer.alloc(0), ciphertext);
} }
public getRemoteStaticKey(): bytes {
return this.session.hs.rs;
}
private getCS(session: NoiseSession, encryption = true) { private getCS(session: NoiseSession, encryption = true) {
if (!session.cs1 || !session.cs2) { if (!session.cs1 || !session.cs2) {
throw new Error("Handshake not completed properly, cipher state does not exist."); throw new Error("Handshake not completed properly, cipher state does not exist.");

View File

@ -145,7 +145,6 @@ export class XX extends AbstractHandshake {
session.h = h; session.h = h;
session.cs1 = cs1; session.cs1 = cs1;
session.cs2 = cs2; session.cs2 = cs2;
delete session.hs;
} else if (session.mc.gtn(2)) { } else if (session.mc.gtn(2)) {
if (session.i) { if (session.i) {
if (!session.cs1) { if (!session.cs1) {

View File

@ -20,11 +20,11 @@ class Keycache {
} }
} }
public async load(peerId: PeerId): Promise<bytes32> { public async load(peerId: PeerId): Promise<bytes32|null> {
const release = await this.mutex.acquire(); const release = await this.mutex.acquire();
let key; let key;
try { try {
key = this.storage.get(peerId.id); key = this.storage.get(peerId.id) || null;
} finally { } finally {
release(); release();
} }

View File

@ -16,6 +16,8 @@ import { bytes } from "./@types/basic";
import { INoiseConnection, PeerId, KeyPair, SecureOutbound } from "./@types/libp2p"; import { INoiseConnection, PeerId, KeyPair, SecureOutbound } from "./@types/libp2p";
import { Duplex } from "./@types/it-pair"; import { Duplex } from "./@types/it-pair";
import {IHandshake} from "./@types/handshake-interface"; import {IHandshake} from "./@types/handshake-interface";
import {KeyCache} from "./keycache";
import {logger} from "./logger";
export type WrappedConnection = ReturnType<typeof Wrap>; export type WrappedConnection = ReturnType<typeof Wrap>;
@ -104,14 +106,21 @@ export class Noise implements INoiseConnection {
* @param remotePeer * @param remotePeer
*/ */
private async performHandshake(params: HandshakeParams): Promise<IHandshake> { private async performHandshake(params: HandshakeParams): Promise<IHandshake> {
// TODO: Implement noise pipes
const payload = await getPayload(params.localPeer, this.staticKeys.publicKey, this.earlyData); const payload = await getPayload(params.localPeer, this.staticKeys.publicKey, this.earlyData);
if (false) { let foundRemoteStaticKey: bytes|null = null;
let IKhandshake; if (this.useNoisePipes && params.isInitiator) {
logger("Initiator using noise pipes. Going to load cached static key...");
foundRemoteStaticKey = await KeyCache.load(params.remotePeer);
logger(`Static key has been found: ${!!foundRemoteStaticKey}`)
}
if (foundRemoteStaticKey) {
// Try IK first
const { remotePeer, connection, isInitiator } = params;
const IKhandshake = new IKHandshake(isInitiator, payload, this.prologue, this.staticKeys, connection, remotePeer, foundRemoteStaticKey);
try { try {
IKhandshake = await this.performIKHandshake(params, payload); return await this.performIKHandshake(IKhandshake, payload);
return IKhandshake;
} catch (e) { } catch (e) {
// XX fallback // XX fallback
const ephemeralKeys = IKhandshake.getRemoteEphemeralKeys(); const ephemeralKeys = IKhandshake.getRemoteEphemeralKeys();
@ -156,7 +165,7 @@ export class Noise implements INoiseConnection {
await handshake.finish(); await handshake.finish();
if (this.useNoisePipes) { if (this.useNoisePipes) {
await KeyCache.store(remotePeer, handshake.getRemoteStaticKey());
} }
} catch (e) { } catch (e) {
throw new Error(`Error occurred during XX handshake: ${e.message}`); throw new Error(`Error occurred during XX handshake: ${e.message}`);
@ -166,12 +175,9 @@ export class Noise implements INoiseConnection {
} }
private async performIKHandshake( private async performIKHandshake(
params: HandshakeParams, handshake: IKHandshake,
payload: bytes, payload: bytes,
): Promise<IKHandshake> { ): Promise<IKHandshake> {
const { isInitiator, remotePeer, connection } = params;
const handshake = new IKHandshake(isInitiator, payload, this.prologue, this.staticKeys, connection, remotePeer);
// TODO // TODO
return handshake; return handshake;

View File

@ -26,8 +26,8 @@ describe("Noise", () => {
it("should communicate through encrypted streams", async() => { it("should communicate through encrypted streams", async() => {
try { try {
const noiseInit = new Noise(); const noiseInit = new Noise(undefined, undefined, false);
const noiseResp = new Noise(); const noiseResp = new Noise(undefined, undefined, false);
const [inboundConnection, outboundConnection] = DuplexPair(); const [inboundConnection, outboundConnection] = DuplexPair();
const [outbound, inbound] = await Promise.all([ const [outbound, inbound] = await Promise.all([
@ -46,7 +46,7 @@ describe("Noise", () => {
}); });
it("should test that secureOutbound is spec compliant", async() => { it("should test that secureOutbound is spec compliant", async() => {
const noiseInit = new Noise(); const noiseInit = new Noise(undefined, undefined, false);
const [inboundConnection, outboundConnection] = DuplexPair(); const [inboundConnection, outboundConnection] = DuplexPair();
const [outbound, { wrapped, handshake }] = await Promise.all([ const [outbound, { wrapped, handshake }] = await Promise.all([
@ -99,8 +99,8 @@ describe("Noise", () => {
it("should test large payloads", async() => { it("should test large payloads", async() => {
try { try {
const noiseInit = new Noise(); const noiseInit = new Noise(undefined, undefined, false);
const noiseResp = new Noise(); const noiseResp = new Noise(undefined, undefined, false);
const [inboundConnection, outboundConnection] = DuplexPair(); const [inboundConnection, outboundConnection] = DuplexPair();
const [outbound, inbound] = await Promise.all([ const [outbound, inbound] = await Promise.all([