42 lines
1.1 KiB
JavaScript
42 lines
1.1 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("jpake-rekey-r1", jpake.GetRound1Message());
|
||
|
|
||
|
// 2: Round 2
|
||
|
const their_round1 = (await once(connection, "message"))[0];
|
||
|
if(typeof their_round1 !== "string") return null;
|
||
|
|
||
|
const our_round2 = jpake.GetRound2Message(their_round1);
|
||
|
if(typeof our_round2 !== "string") return null;
|
||
|
|
||
|
connection.send("jpake-rekey-r2", our_round2);
|
||
|
|
||
|
|
||
|
// 3: Compute new shared key
|
||
|
const their_round2 = (await once(connection, "message"))[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);
|
||
|
}
|