144 lines
4.3 KiB
TypeScript
144 lines
4.3 KiB
TypeScript
export interface PassphraseBackup {
|
|
wrapped_key: string;
|
|
salt: string;
|
|
iterations: number;
|
|
}
|
|
|
|
export interface GetKeyResponse {
|
|
user_id: string;
|
|
key_check: string | null;
|
|
passphrase_backup: PassphraseBackup | null;
|
|
}
|
|
|
|
const KEY_TEXT_PREFIX = "vces-key-v1:";
|
|
const KEY_CHECK_PLAINTEXT = new TextEncoder().encode("vces-key-check-v1");
|
|
const PBKDF2_ITERATIONS = 600_000;
|
|
const IV_LENGTH = 12;
|
|
const MASTER_KEY_USAGES: KeyUsage[] = ["encrypt", "decrypt", "wrapKey", "unwrapKey"];
|
|
|
|
function to_base64(bytes: Uint8Array) {
|
|
return btoa(String.fromCharCode(...bytes));
|
|
}
|
|
|
|
function from_base64(text: string) {
|
|
return Uint8Array.from(atob(text), (char) => char.charCodeAt(0));
|
|
}
|
|
|
|
function concat(a: Uint8Array, b: Uint8Array) {
|
|
const result = new Uint8Array(a.length + b.length);
|
|
result.set(a);
|
|
result.set(b, a.length);
|
|
return result;
|
|
}
|
|
|
|
export function generate_master_key() {
|
|
return crypto.subtle.generateKey({ name: "AES-GCM", length: 256 }, true, MASTER_KEY_USAGES);
|
|
}
|
|
|
|
export async function export_key_text(key: CryptoKey) {
|
|
const raw = new Uint8Array(await crypto.subtle.exportKey("raw", key));
|
|
const base64url = to_base64(raw).replaceAll("+", "-").replaceAll("/", "_").replaceAll("=", "");
|
|
raw.fill(0);
|
|
return KEY_TEXT_PREFIX + base64url;
|
|
}
|
|
|
|
// Throws if the text isn't a key, imports it as non-extractable
|
|
export async function import_key_text(text: string) {
|
|
const not_a_key = new Error("That doesn't look like a VCES key");
|
|
const trimmed = text.trim();
|
|
if (!trimmed.startsWith(KEY_TEXT_PREFIX)) {
|
|
throw not_a_key;
|
|
}
|
|
|
|
let raw: Uint8Array<ArrayBuffer>;
|
|
try {
|
|
raw = from_base64(trimmed.slice(KEY_TEXT_PREFIX.length).replaceAll("-", "+").replaceAll("_", "/"));
|
|
} catch {
|
|
throw not_a_key;
|
|
}
|
|
if (raw.length !== 32) {
|
|
throw not_a_key;
|
|
}
|
|
|
|
const key = await crypto.subtle.importKey("raw", raw, "AES-GCM", false, MASTER_KEY_USAGES);
|
|
raw.fill(0);
|
|
return key;
|
|
}
|
|
|
|
export async function to_non_extractable(key: CryptoKey) {
|
|
const raw = new Uint8Array(await crypto.subtle.exportKey("raw", key));
|
|
const locked = await crypto.subtle.importKey("raw", raw, "AES-GCM", false, MASTER_KEY_USAGES);
|
|
raw.fill(0);
|
|
return locked;
|
|
}
|
|
|
|
// Stored on the server so a key can be checked before it's used, IV goes in front of the ciphertext
|
|
export async function make_key_check(key: CryptoKey) {
|
|
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
|
|
const ciphertext = await crypto.subtle.encrypt({ name: "AES-GCM", iv }, key, KEY_CHECK_PLAINTEXT);
|
|
return to_base64(concat(iv, new Uint8Array(ciphertext)));
|
|
}
|
|
|
|
export async function verify_key_check(key: CryptoKey, key_check: string) {
|
|
const bytes = from_base64(key_check);
|
|
try {
|
|
await crypto.subtle.decrypt(
|
|
{ name: "AES-GCM", iv: bytes.slice(0, IV_LENGTH) },
|
|
key,
|
|
bytes.slice(IV_LENGTH),
|
|
);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function derive_wrapping_key(passphrase: string, salt: Uint8Array<ArrayBuffer>, iterations: number) {
|
|
const base = await crypto.subtle.importKey(
|
|
"raw",
|
|
new TextEncoder().encode(passphrase.normalize("NFC")),
|
|
"PBKDF2",
|
|
false,
|
|
["deriveKey"],
|
|
);
|
|
return crypto.subtle.deriveKey(
|
|
{ name: "PBKDF2", hash: "SHA-256", salt, iterations },
|
|
base,
|
|
{ name: "AES-GCM", length: 256 },
|
|
false,
|
|
["wrapKey", "unwrapKey"],
|
|
);
|
|
}
|
|
|
|
// The key must be extractable to be wrapped
|
|
export async function wrap_with_passphrase(key: CryptoKey, passphrase: string): Promise<PassphraseBackup> {
|
|
const salt = crypto.getRandomValues(new Uint8Array(16));
|
|
const wrapping_key = await derive_wrapping_key(passphrase, salt, PBKDF2_ITERATIONS);
|
|
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
|
|
const wrapped = await crypto.subtle.wrapKey("raw", key, wrapping_key, { name: "AES-GCM", iv });
|
|
return {
|
|
wrapped_key: to_base64(concat(iv, new Uint8Array(wrapped))),
|
|
salt: to_base64(salt),
|
|
iterations: PBKDF2_ITERATIONS,
|
|
};
|
|
}
|
|
|
|
// Throws if the passphrase is wrong, the unwrapped key is non-extractable
|
|
export async function unwrap_with_passphrase(backup: PassphraseBackup, passphrase: string) {
|
|
const wrapping_key = await derive_wrapping_key(passphrase, from_base64(backup.salt), backup.iterations);
|
|
const wrapped = from_base64(backup.wrapped_key);
|
|
try {
|
|
return await crypto.subtle.unwrapKey(
|
|
"raw",
|
|
wrapped.slice(IV_LENGTH),
|
|
wrapping_key,
|
|
{ name: "AES-GCM", iv: wrapped.slice(0, IV_LENGTH) },
|
|
"AES-GCM",
|
|
false,
|
|
MASTER_KEY_USAGES,
|
|
);
|
|
} catch {
|
|
throw new Error("Wrong passphrase");
|
|
}
|
|
}
|