js-libp2p-noise/src/handshake-ik.ts

142 lines
5.3 KiB
TypeScript
Raw Normal View History

2020-01-03 17:28:13 +01:00
import {WrappedConnection} from "./noise";
2020-01-05 19:09:59 +01:00
import {IK} from "./handshakes/ik";
2020-01-03 14:53:14 +01:00
import {NoiseSession} from "./@types/handshake";
import {bytes, bytes32} from "./@types/basic";
2020-02-07 12:59:52 +01:00
import {KeyPair} from "./@types/libp2p";
2020-01-07 13:34:45 +01:00
import {IHandshake} from "./@types/handshake-interface";
2020-01-03 17:28:13 +01:00
import {Buffer} from "buffer";
2020-01-15 11:32:40 +01:00
import {decode0, decode1, encode0, encode1} from "./encoder";
import {decodePayload, getPeerIdFromPayload, verifySignedPayload} from "./utils";
2020-01-15 17:27:32 +01:00
import {FailedIKError} from "./errors";
import {logger} from "./logger";
2020-02-07 12:59:52 +01:00
import PeerId from "peer-id";
2020-01-03 14:53:14 +01:00
2020-01-07 13:34:45 +01:00
export class IKHandshake implements IHandshake {
2020-01-03 14:53:14 +01:00
public isInitiator: boolean;
public session: NoiseSession;
2020-02-07 20:21:27 +01:00
public remotePeer!: PeerId;
2020-04-17 11:04:50 +02:00
public remoteEarlyData: Buffer;
2020-01-03 14:53:14 +01:00
2020-01-11 20:20:57 +01:00
private payload: bytes;
2020-01-03 14:53:14 +01:00
private prologue: bytes32;
2020-01-07 13:34:45 +01:00
private staticKeypair: KeyPair;
2020-01-03 14:53:14 +01:00
private connection: WrappedConnection;
2020-01-05 19:09:59 +01:00
private ik: IK;
2020-01-03 14:53:14 +01:00
constructor(
isInitiator: boolean,
2020-01-11 20:20:57 +01:00
payload: bytes,
2020-01-03 14:53:14 +01:00
prologue: bytes32,
2020-01-07 13:34:45 +01:00
staticKeypair: KeyPair,
2020-01-03 14:53:14 +01:00
connection: WrappedConnection,
2020-01-13 16:33:58 +01:00
remoteStaticKey: bytes,
2020-02-07 20:21:27 +01:00
remotePeer?: PeerId,
2020-01-05 19:09:59 +01:00
handshake?: IK,
2020-01-03 14:53:14 +01:00
) {
this.isInitiator = isInitiator;
2020-01-18 16:31:05 +01:00
this.payload = Buffer.from(payload);
2020-01-03 14:53:14 +01:00
this.prologue = prologue;
2020-01-07 13:34:45 +01:00
this.staticKeypair = staticKeypair;
2020-01-03 14:53:14 +01:00
this.connection = connection;
2020-02-07 20:21:27 +01:00
if(remotePeer) {
this.remotePeer = remotePeer;
}
2020-01-05 19:09:59 +01:00
this.ik = handshake || new IK();
2020-01-13 16:33:58 +01:00
this.session = this.ik.initSession(this.isInitiator, this.prologue, this.staticKeypair, remoteStaticKey);
2020-04-17 11:04:50 +02:00
this.remoteEarlyData = Buffer.alloc(0)
2020-01-03 14:53:14 +01:00
}
2020-01-03 17:28:13 +01:00
2020-01-15 11:32:40 +01:00
public async stage0(): Promise<void> {
if (this.isInitiator) {
2020-01-17 23:50:41 +01:00
logger("IK Stage 0 - Initiator sending message...");
2020-01-15 11:32:40 +01:00
const messageBuffer = this.ik.sendMessage(this.session, this.payload);
this.connection.writeLP(encode1(messageBuffer));
2020-01-17 23:50:41 +01:00
logger("IK Stage 0 - Initiator sent message.");
2020-01-15 11:32:40 +01:00
} else {
2020-01-17 23:50:41 +01:00
logger("IK Stage 0 - Responder receiving message...");
2020-01-21 11:04:34 +01:00
const receivedMsg = await this.connection.readLP();
2020-01-15 11:32:40 +01:00
try {
2020-02-14 10:10:42 +01:00
const receivedMessageBuffer = decode1(receivedMsg.slice());
2020-03-01 19:05:53 +01:00
const {plaintext, valid} = this.ik.recvMessage(this.session, receivedMessageBuffer);
if(!valid) {
throw new Error("ik handshake stage 0 decryption validation fail");
}
2020-01-17 23:50:41 +01:00
logger("IK Stage 0 - Responder got message, going to verify payload.");
const decodedPayload = await decodePayload(plaintext);
this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload);
2020-02-17 12:11:55 +01:00
await verifySignedPayload(this.session.hs.rs, decodedPayload, this.remotePeer);
2020-04-17 11:09:46 +02:00
this.setRemoteEarlyData(decodedPayload.data);
2020-01-17 23:50:41 +01:00
logger("IK Stage 0 - Responder successfully verified payload!");
2020-01-15 11:32:40 +01:00
} catch (e) {
logger("Responder breaking up with IK handshake in stage 0.");
2020-02-07 20:21:27 +01:00
2020-01-15 17:27:32 +01:00
throw new FailedIKError(receivedMsg, `Error occurred while verifying initiator's signed payload: ${e.message}`);
2020-01-15 11:32:40 +01:00
}
}
}
public async stage1(): Promise<void> {
if (this.isInitiator) {
2020-01-17 23:50:41 +01:00
logger("IK Stage 1 - Initiator receiving message...");
const receivedMsg = (await this.connection.readLP()).slice();
const receivedMessageBuffer = decode0(Buffer.from(receivedMsg));
2020-03-01 19:05:53 +01:00
const {plaintext, valid} = this.ik.recvMessage(this.session, receivedMessageBuffer);
2020-01-17 23:50:41 +01:00
logger("IK Stage 1 - Initiator got message, going to verify payload.");
2020-01-15 11:32:40 +01:00
try {
2020-03-01 19:05:53 +01:00
if(!valid) {
throw new Error("ik stage 1 decryption validation fail");
}
const decodedPayload = await decodePayload(plaintext);
this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload);
2020-02-17 12:11:55 +01:00
await verifySignedPayload(receivedMessageBuffer.ns.slice(0, 32), decodedPayload, this.remotePeer);
2020-04-17 11:09:46 +02:00
this.setRemoteEarlyData(decodedPayload.data);
2020-01-17 23:50:41 +01:00
logger("IK Stage 1 - Initiator successfully verified payload!");
2020-01-15 11:32:40 +01:00
} catch (e) {
logger("Initiator breaking up with IK handshake in stage 1.");
2020-01-15 17:27:32 +01:00
throw new FailedIKError(receivedMsg, `Error occurred while verifying responder's signed payload: ${e.message}`);
2020-01-15 11:32:40 +01:00
}
} else {
2020-01-17 23:50:41 +01:00
logger("IK Stage 1 - Responder sending message...");
2020-01-15 11:32:40 +01:00
const messageBuffer = this.ik.sendMessage(this.session, this.payload);
this.connection.writeLP(encode0(messageBuffer));
2020-01-17 23:50:41 +01:00
logger("IK Stage 1 - Responder sent message...");
2020-01-15 11:32:40 +01:00
}
}
2020-03-01 19:06:28 +01:00
public decrypt(ciphertext: bytes, session: NoiseSession): {plaintext: bytes; valid: boolean} {
2020-01-03 17:28:13 +01:00
const cs = this.getCS(session, false);
return this.ik.decryptWithAd(cs, Buffer.alloc(0), ciphertext);
}
public encrypt(plaintext: Buffer, session: NoiseSession): Buffer {
const cs = this.getCS(session);
return this.ik.encryptWithAd(cs, Buffer.alloc(0), plaintext);
}
2020-02-08 11:21:51 +01:00
public getLocalEphemeralKeys(): KeyPair {
2020-01-05 19:00:16 +01:00
if (!this.session.hs.e) {
throw new Error("Ephemeral keys do not exist.");
}
2020-01-03 17:28:13 +01:00
return this.session.hs.e;
}
private getCS(session: NoiseSession, encryption = true) {
if (!session.cs1 || !session.cs2) {
throw new Error("Handshake not completed properly, cipher state does not exist.");
}
if (this.isInitiator) {
return encryption ? session.cs1 : session.cs2;
} else {
return encryption ? session.cs2 : session.cs1;
}
}
2020-04-17 10:35:37 +02:00
2020-04-17 11:09:46 +02:00
private setRemoteEarlyData(data: Uint8Array|null|undefined): void {
2020-04-17 10:35:37 +02:00
if(data){
2020-04-17 11:07:58 +02:00
this.remoteEarlyData = Buffer.from(data.buffer, data.byteOffset, data.length);
2020-04-17 10:35:37 +02:00
}
}
2020-01-03 14:53:14 +01:00
}