From 2bb63e3e91a030e13a0e172c73baa644ec64546f Mon Sep 17 00:00:00 2001 From: Belma Gutlic Date: Fri, 3 Jan 2020 14:53:14 +0100 Subject: [PATCH] Create handshake handler files --- src/crypto.ts | 2 +- src/handshake-ik.ts | 44 +++++++++++++++++++++++++++ src/{handshake.ts => handshake-xx.ts} | 0 src/noise.ts | 8 ++--- test/handshake.test.ts | 2 +- test/noise.test.ts | 2 +- 6 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 src/handshake-ik.ts rename src/{handshake.ts => handshake-xx.ts} (100%) diff --git a/src/crypto.ts b/src/crypto.ts index 34f8c06..7ae4733 100644 --- a/src/crypto.ts +++ b/src/crypto.ts @@ -1,5 +1,5 @@ import { Buffer } from "buffer"; -import { Handshake } from "./handshake"; +import { Handshake } from "./handshake-xx"; interface ReturnEncryptionWrapper { (source: Iterable): AsyncIterableIterator; diff --git a/src/handshake-ik.ts b/src/handshake-ik.ts new file mode 100644 index 0000000..b38a5ee --- /dev/null +++ b/src/handshake-ik.ts @@ -0,0 +1,44 @@ +import {NoiseSession} from "./@types/handshake"; +import {bytes, bytes32} from "./@types/basic"; +import {KeyPair, PeerId} from "./@types/libp2p"; +import {WrappedConnection} from "./noise"; +import {IKHandshake} from "./handshakes/ik"; + +export class Handshake { + public isInitiator: boolean; + public session: NoiseSession; + + private libp2pPrivateKey: bytes; + private libp2pPublicKey: bytes; + private prologue: bytes32; + private staticKeys: KeyPair; + private connection: WrappedConnection; + private remotePeer: PeerId; + private ik: IKHandshake; + + constructor( + isInitiator: boolean, + libp2pPrivateKey: bytes, + libp2pPublicKey: bytes, + prologue: bytes32, + staticKeys: KeyPair, + connection: WrappedConnection, + remotePeer: PeerId, + handshake?: IKHandshake, + ) { + this.isInitiator = isInitiator; + this.libp2pPrivateKey = libp2pPrivateKey; + this.libp2pPublicKey = libp2pPublicKey; + this.prologue = prologue; + this.staticKeys = staticKeys; + this.connection = connection; + this.remotePeer = remotePeer; + + this.ik = handshake || new IKHandshake(); + + // Dummy data + // TODO: Load remote static keys if found + const remoteStaticKeys = this.staticKeys; + this.session = this.ik.initSession(this.isInitiator, this.prologue, this.staticKeys, remoteStaticKeys.publicKey); + } +} diff --git a/src/handshake.ts b/src/handshake-xx.ts similarity index 100% rename from src/handshake.ts rename to src/handshake-xx.ts diff --git a/src/noise.ts b/src/noise.ts index 068ade3..8a5f30a 100644 --- a/src/noise.ts +++ b/src/noise.ts @@ -6,7 +6,7 @@ import ensureBuffer from 'it-buffer'; import pipe from 'it-pipe'; import lp from 'it-length-prefixed'; -import { Handshake } from "./handshake"; +import { Handshake } from "./handshake-xx"; import { generateKeypair } from "./utils"; import { uint16BEDecode, uint16BEEncode } from "./encoder"; import { decryptStream, encryptStream } from "./crypto"; @@ -48,7 +48,7 @@ export class Noise implements NoiseConnection { public async secureOutbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise { const wrappedConnection = Wrap(connection); const libp2pPublicKey = localPeer.marshalPubKey(); - const handshake = await this.performHandshake(wrappedConnection, true, libp2pPublicKey, remotePeer); + const handshake = await this.performXXHandshake(wrappedConnection, true, libp2pPublicKey, remotePeer); const conn = await this.createSecureConnection(wrappedConnection, handshake); return { @@ -67,7 +67,7 @@ export class Noise implements NoiseConnection { public async secureInbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise { const wrappedConnection = Wrap(connection); const libp2pPublicKey = localPeer.marshalPubKey(); - const handshake = await this.performHandshake(wrappedConnection, false, libp2pPublicKey, remotePeer); + const handshake = await this.performXXHandshake(wrappedConnection, false, libp2pPublicKey, remotePeer); const conn = await this.createSecureConnection(wrappedConnection, handshake); return { @@ -76,7 +76,7 @@ export class Noise implements NoiseConnection { }; } - private async performHandshake( + private async performXXHandshake( connection: WrappedConnection, isInitiator: boolean, libp2pPublicKey: bytes, diff --git a/test/handshake.test.ts b/test/handshake.test.ts index 6019711..1ed6fa7 100644 --- a/test/handshake.test.ts +++ b/test/handshake.test.ts @@ -3,7 +3,7 @@ import Duplex from 'it-pair/duplex'; import {Buffer} from "buffer"; import Wrap from "it-pb-rpc"; -import {Handshake} from "../src/handshake"; +import {Handshake} from "../src/handshake-xx"; import {generateKeypair} from "../src/utils"; import {createPeerIdsFromFixtures} from "./fixtures/peer"; import {getKeyPairFromPeerId} from "./utils"; diff --git a/test/noise.test.ts b/test/noise.test.ts index 6594473..9e8ded3 100644 --- a/test/noise.test.ts +++ b/test/noise.test.ts @@ -5,7 +5,7 @@ import { Noise } from "../src"; import {createPeerIdsFromFixtures} from "./fixtures/peer"; import Wrap from "it-pb-rpc"; import { random } from "bcrypto"; -import {Handshake} from "../src/handshake"; +import {Handshake} from "../src/handshake-xx"; import { createHandshakePayload, generateKeypair,