/** * Guardian-assisted recovery (R-08). On this device a random recovery secret wraps the vault key and is * split into cards — one per guardian. The server keeps the wrap, a verifier derived from the secret and * who holds which card; it never sees the secret, a card, or the vault key. Getting back in: enough * cards rebuild the secret here, the verifier proves it to the server, and the vault key is re-wrapped * under a new password. HKDF gives the wrap key and the verifier independent lives: knowing the verifier * says nothing about the wrap key. */ import * as mb from './crypto'; import { combine, decodeShare, encodeShare, split } from './shamir'; import type { CipherBlobDto } from './types'; export const CARD_PREFIX = 'cbrec1-'; const SALT = new TextEncoder().encode('CapsuleBridge guardian recovery v1'); async function derive(secret: Uint8Array): Promise<{ wrapKey: CryptoKey; verifier: string }> { const ikm = await crypto.subtle.importKey('raw', new Uint8Array(secret), 'HKDF', false, ['deriveBits']); const bits = async (info: string) => new Uint8Array(await crypto.subtle.deriveBits({ name: 'HKDF', hash: 'SHA-256', salt: SALT, info: new TextEncoder().encode(info) }, ikm, 256)); const wrapKey = await mb.importAesKey(await bits('wrap')); const verifier = Array.from(await bits('verify')).map((b) => b.toString(16).padStart(2, '0')).join(''); return { wrapKey, verifier }; } export interface RecoveryCard { index: number; guardianId: string; guardianName: string; text: string } export function cardText(opts: { owner: string; guardian: string; index: number; total: number; threshold: number; share: string }): string { const { owner, guardian, index, total, threshold, share } = opts; return ( `Guardian recovery card ${index} of ${total} — for ${owner}\nHeld by: ${guardian}\n\n${share}\n\n` + `${owner} needs ${threshold} of these ${total} cards to get back into CapsuleBridge if the master password is ever forgotten. ` + `Keep this card with your important papers. Hand it back only to ${owner} — in person, or on a call you started — never to anyone who contacts you asking for it. ` + `This card alone opens nothing.` ); } /** Generate the secret, wrap the vault key under it, split it into one card per guardian. */ export async function setupGuardianRecovery(vaultKey: CryptoKey, guardians: { id: string; name: string }[], threshold: number, ownerName: string) { const secret = crypto.getRandomValues(new Uint8Array(32)); const { wrapKey, verifier } = await derive(secret); const wrap = await mb.aesEncrypt(wrapKey, await mb.exportRawKey(vaultKey)); const shares = split(secret, threshold, guardians.length); const cards: RecoveryCard[] = guardians.map((g, i) => ({ index: i + 1, guardianId: g.id, guardianName: g.name, text: cardText({ owner: ownerName, guardian: g.name, index: i + 1, total: guardians.length, threshold, share: encodeShare(shares[i], CARD_PREFIX) }), })); return { cards, request: { threshold, total: guardians.length, wrap, verifier, holders: cards.map((c) => ({ index: c.index, guardianId: c.guardianId })) }, }; } export function findCard(text: string): string | null { const m = text.match(/cbrec1-[a-z2-7-]+/i); return m ? m[0] : null; } /** Rebuild the secret from pasted cards and open the wrap. Throws a plain-English error when the cards are wrong. */ export async function recoverVaultKey(cardTexts: string[], wrap: CipherBlobDto): Promise<{ rawVaultKey: Uint8Array; verifier: string; used: number }> { const shares = cardTexts.map((t) => t.trim()).filter(Boolean).map((t) => decodeShare(findCard(t) ?? t, CARD_PREFIX)); const secret = combine(shares); const { wrapKey, verifier } = await derive(secret); let rawVaultKey: Uint8Array; try { rawVaultKey = await mb.aesDecrypt(wrapKey, wrap); } catch { throw new Error('These cards do not rebuild the recovery secret for this account. Check each card against its printout.'); } return { rawVaultKey, verifier, used: shares.length }; }