From bf00317e95cd91b3bfa15ce84088e0e0c19a3bf4 Mon Sep 17 00:00:00 2001 From: paulaboks Date: Thu, 29 Oct 2026 17:29:25 -0300 Subject: [PATCH] Encryption keys --- islands/AskCode.tsx | 2 +- islands/KeysCreate.tsx | 157 +++++++++++++++++++++++++++++++++++++++ islands/KeysNew.tsx | 74 ++++++++++++++++++ islands/KeysUnlock.tsx | 107 ++++++++++++++++++++++++++ islands/MainIsland.tsx | 7 +- routes/api/create_key.ts | 49 ++++++++++++ routes/api/get_key.ts | 32 ++++++++ src/db.ts | 3 + src/key_store.ts | 37 +++++++++ src/keys.ts | 143 +++++++++++++++++++++++++++++++++++ src/server_constants.ts | 1 + 11 files changed, 609 insertions(+), 3 deletions(-) create mode 100644 islands/KeysCreate.tsx create mode 100644 islands/KeysNew.tsx create mode 100644 islands/KeysUnlock.tsx create mode 100644 routes/api/create_key.ts create mode 100644 routes/api/get_key.ts create mode 100644 src/key_store.ts create mode 100644 src/keys.ts diff --git a/islands/AskCode.tsx b/islands/AskCode.tsx index 4240f18..ebe64ad 100644 --- a/islands/AskCode.tsx +++ b/islands/AskCode.tsx @@ -12,7 +12,7 @@ export default function AskCode({ page }: AskCodeProps) { const on_success = (data: { code: string }) => { localStorage.setItem("code", data.code); - page.value = "keys"; + page.value = "keysnew"; }; useSignalEffect(() => { diff --git a/islands/KeysCreate.tsx b/islands/KeysCreate.tsx new file mode 100644 index 0000000..1ac8388 --- /dev/null +++ b/islands/KeysCreate.tsx @@ -0,0 +1,157 @@ +import { useComputed, useSignal } from "@preact/signals"; + +import { post_json } from "@/src/client.ts"; +import { save_master_key } from "@/src/key_store.ts"; +import { + export_key_text, + generate_master_key, + make_key_check, + to_non_extractable, + wrap_with_passphrase, +} from "@/src/keys.ts"; + +const MIN_PASSPHRASE_LENGTH = 14; + +interface KeysCreateProps { + user_id: string; + on_done: () => void; + // The account got a key some other way, it has to be unlocked instead + on_needs_unlock: (message: string) => void; +} + +export default function KeysCreate(props: KeysCreateProps) { + const step = useSignal<"create" | "backup" | "saving">("create"); + const new_key = useSignal(null); + const key_text = useSignal(""); + const saved_file = useSignal(false); + const passphrase = useSignal(""); + const passphrase_confirm = useSignal(""); + const error = useSignal(""); + + const passphrase_error = useComputed(() => { + if (!passphrase.value && !passphrase_confirm.value) { + return ""; + } + if (passphrase.value.length < MIN_PASSPHRASE_LENGTH) { + return `The passphrase must be at least ${MIN_PASSPHRASE_LENGTH} characters`; + } + if (passphrase.value !== passphrase_confirm.value) { + return "The passphrases don't match"; + } + return ""; + }); + const can_continue = useComputed(() => !passphrase_error.value && (saved_file.value || passphrase.value !== "")); + + async function create() { + const key = await generate_master_key(); + new_key.value = key; + key_text.value = await export_key_text(key); + step.value = "backup"; + } + + function download_key_file() { + const url = URL.createObjectURL(new Blob([key_text.value + "\n"], { type: "text/plain" })); + const link = document.createElement("a"); + link.href = url; + link.download = "vces-key.txt"; + link.click(); + // Revoking right away can cancel the download in some browsers + setTimeout(() => URL.revokeObjectURL(url), 60_000); + } + + async function finish(event: SubmitEvent) { + event.preventDefault(); + const key = new_key.value; + const code = localStorage.getItem("code"); + if (!key || !code || !can_continue.value) { + return; + } + + step.value = "saving"; + error.value = ""; + try { + const key_check = await make_key_check(key); + const passphrase_backup = passphrase.value ? await wrap_with_passphrase(key, passphrase.value) : undefined; + + const result = await post_json("/api/create_key", { code, key_check, passphrase_backup }); + if (!result.ok) { + if (result.status === 409) { + props.on_needs_unlock(result.error); + return; + } + error.value = result.error; + step.value = "backup"; + return; + } + } catch (err) { + error.value = err instanceof Error ? err.message : String(err); + step.value = "backup"; + return; + } + + try { + await save_master_key(props.user_id, await to_non_extractable(key)); + } catch { + props.on_needs_unlock( + "Your key was registered but couldn't be saved in this browser. Unlock it with your backup.", + ); + return; + } + props.on_done(); + } + + if (step.value === "create") { + return ( +
+

Your files are encrypted in this browser with a key the server never sees.

+ +
+ ); + } + + return ( +
+
+

Back up your key. If you lose it, your files can't be recovered, not even by the server.

+ +

Key file

+