52 lines
1.5 KiB
JavaScript
52 lines
1.5 KiB
JavaScript
"use strict";
|
|
|
|
import { once } from 'events';
|
|
|
|
import l from 'log';
|
|
import { JPake } from 'jpake';
|
|
|
|
export default async function rekey(connection, secret_join) {
|
|
// 0: Setup jpake
|
|
let jpake = new JPake(secret_join);
|
|
|
|
// 1: Round 1
|
|
connection.send("rekey", { round: 1, content: jpake.GetRound1Message() });
|
|
|
|
// 2: Round 2
|
|
|
|
const their_round1 = (await once(connection, "message-rekey"))[1];
|
|
|
|
if(typeof their_round1 !== "object"
|
|
|| their_round1.round !== 1
|
|
|| typeof their_round1.content !== "string")
|
|
throw new Error(`Error: Received invalid round 1 from peer`);
|
|
|
|
const our_round2 = jpake.GetRound2Message(their_round1.content);
|
|
if(typeof our_round2 !== "string") throw new Error(`Error: Failed to compute rekey round 2`);
|
|
|
|
connection.send("rekey", { round: 2, content: our_round2 });
|
|
|
|
// 3: Compute new shared key
|
|
const their_round2 = (await once(connection, "message-rekey"))[1];
|
|
|
|
if(typeof their_round2 !== "object"
|
|
|| their_round2.round !== 1
|
|
|| typeof their_round2.content !== "string")
|
|
throw new Error(`Error: Received invalid round 2 from peer`);
|
|
|
|
const new_shared_key = jpake.ComputeSharedKey(their_round2.content);
|
|
if(typeof new_shared_key !== "string")
|
|
throw new Error(`Error: Failed to compute shared key`);
|
|
|
|
return Buffer.from(new_shared_key, "hex");
|
|
|
|
// let data_bytes = response[0].toString("base64");
|
|
//
|
|
// const cert_theirs = decrypt(secret_join, data_bytes.toString("base64"));
|
|
// if(cert_theirs === null) {
|
|
// socket.destroy();
|
|
// return null;
|
|
// }
|
|
// console.log(`STARTTLS cert_theirs`, cert_theirs);
|
|
}
|