"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 let [ event, their_round1 ] = (await once(connection, "message-rekey")); if(typeof their_round1 !== "object" || event !== "rekey" || 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"))[0]; if(typeof their_round2 !== "string") return null; const new_shared_key = jpake.ComputeSharedKey(their_round2); if(typeof new_shared_key !== "string") return null; 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); }