Create handshake handler files

This commit is contained in:
Belma Gutlic 2020-01-03 14:53:14 +01:00
parent b084207c52
commit 2bb63e3e91
6 changed files with 51 additions and 7 deletions

View File

@ -1,5 +1,5 @@
import { Buffer } from "buffer"; import { Buffer } from "buffer";
import { Handshake } from "./handshake"; import { Handshake } from "./handshake-xx";
interface ReturnEncryptionWrapper { interface ReturnEncryptionWrapper {
(source: Iterable<Uint8Array>): AsyncIterableIterator<Uint8Array>; (source: Iterable<Uint8Array>): AsyncIterableIterator<Uint8Array>;

44
src/handshake-ik.ts Normal file
View File

@ -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);
}
}

View File

@ -6,7 +6,7 @@ import ensureBuffer from 'it-buffer';
import pipe from 'it-pipe'; import pipe from 'it-pipe';
import lp from 'it-length-prefixed'; import lp from 'it-length-prefixed';
import { Handshake } from "./handshake"; import { Handshake } from "./handshake-xx";
import { generateKeypair } from "./utils"; import { generateKeypair } from "./utils";
import { uint16BEDecode, uint16BEEncode } from "./encoder"; import { uint16BEDecode, uint16BEEncode } from "./encoder";
import { decryptStream, encryptStream } from "./crypto"; import { decryptStream, encryptStream } from "./crypto";
@ -48,7 +48,7 @@ export class Noise implements NoiseConnection {
public async secureOutbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecureOutbound> { public async secureOutbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecureOutbound> {
const wrappedConnection = Wrap(connection); const wrappedConnection = Wrap(connection);
const libp2pPublicKey = localPeer.marshalPubKey(); 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); const conn = await this.createSecureConnection(wrappedConnection, handshake);
return { return {
@ -67,7 +67,7 @@ export class Noise implements NoiseConnection {
public async secureInbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecureOutbound> { public async secureInbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecureOutbound> {
const wrappedConnection = Wrap(connection); const wrappedConnection = Wrap(connection);
const libp2pPublicKey = localPeer.marshalPubKey(); 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); const conn = await this.createSecureConnection(wrappedConnection, handshake);
return { return {
@ -76,7 +76,7 @@ export class Noise implements NoiseConnection {
}; };
} }
private async performHandshake( private async performXXHandshake(
connection: WrappedConnection, connection: WrappedConnection,
isInitiator: boolean, isInitiator: boolean,
libp2pPublicKey: bytes, libp2pPublicKey: bytes,

View File

@ -3,7 +3,7 @@ import Duplex from 'it-pair/duplex';
import {Buffer} from "buffer"; import {Buffer} from "buffer";
import Wrap from "it-pb-rpc"; import Wrap from "it-pb-rpc";
import {Handshake} from "../src/handshake"; import {Handshake} from "../src/handshake-xx";
import {generateKeypair} from "../src/utils"; import {generateKeypair} from "../src/utils";
import {createPeerIdsFromFixtures} from "./fixtures/peer"; import {createPeerIdsFromFixtures} from "./fixtures/peer";
import {getKeyPairFromPeerId} from "./utils"; import {getKeyPairFromPeerId} from "./utils";

View File

@ -5,7 +5,7 @@ import { Noise } from "../src";
import {createPeerIdsFromFixtures} from "./fixtures/peer"; import {createPeerIdsFromFixtures} from "./fixtures/peer";
import Wrap from "it-pb-rpc"; import Wrap from "it-pb-rpc";
import { random } from "bcrypto"; import { random } from "bcrypto";
import {Handshake} from "../src/handshake"; import {Handshake} from "../src/handshake-xx";
import { import {
createHandshakePayload, createHandshakePayload,
generateKeypair, generateKeypair,