All files / crypto keys.ts

98.33% Statements 118/120
85% Branches 17/20
95.23% Functions 20/21
99.15% Lines 118/119

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371                                        17x 17x 17x 17x 17x                                                         54x                   135x           135x         88x 88x       133x 133x 133x 133x 133x               91x 91x 91x 91x 91x 91x 91x 91x   11x         304x           269x       55x       129x 129x       85x 85x 85x 85x                   54x 54x 54x     54x 54x   54x                             54x   54x 54x   54x   54x               73x 73x 73x 73x 73x 73x 73x   7x 7x                 18x 1x   17x 17x     17x 17x 17x 17x           4x 1x 1x 1x 1x           3x 3x                     6x 6x   6x 6x   6x 6x   6x   6x 6x                         14x 14x   14x 14x   14x   14x   14x 14x   14x                       5x 5x   5x 5x 1x     4x 4x   4x                 3x 3x 1x     2x 2x             6x 6x 6x               4x 4x   4x 4x   4x   4x   4x 4x   4x                       3x   3x 3x   3x 3x   3x   3x                      
/**
 * Key management for Stork encryption at rest.
 *
 * Two-tier vault key pattern:
 *   - Vault Key (MDK): random 256-bit key, encrypts the SQLite database
 *   - Password envelope: Argon2id KDF → KEK → AES-256-GCM wraps vault key
 *   - Recovery envelope: BIP39 mnemonic → 32-byte key → AES-256-GCM wraps vault key
 *
 * Credential rotation (password change, recovery key rotation) is O(1): only the
 * envelope blob is re-encrypted; the vault key and database are untouched.
 */
 
import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
import { existsSync, readFileSync, renameSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { argon2id } from "@noble/hashes/argon2.js";
import { generateMnemonic, mnemonicToEntropy, validateMnemonic } from "bip39";
 
// ── Argon2id parameters ────────────────────────────────────────────────────
// Use fast KDF in test mode to avoid timeouts under coverage instrumentation
const FAST_KDF = process.env.STORK_FAST_KDF === "1";
const ARGON2_MEMORY = FAST_KDF ? 1024 : 65536; // 1 MiB test / 64 MiB prod
const ARGON2_ITERATIONS = FAST_KDF ? 1 : 3;
const ARGON2_PARALLELISM = 1;
const KEY_BYTES = 32; // 256-bit keys throughout
 
// ── stork.keys file format ─────────────────────────────────────────────────
 
interface WrappedKey {
	iv: string; // base64, 12 bytes
	ciphertext: string; // base64, encrypted vault key
	tag: string; // base64, 16 bytes GCM auth tag
}
 
interface KeysFile {
	version: 1;
	kdf: {
		algorithm: "argon2id";
		salt: string; // base64, 32 bytes
		memoryCost: number;
		timeCost: number;
		parallelism: number;
	};
	wrappedMasterKey: {
		password: WrappedKey;
		recovery: WrappedKey;
		pendingRecovery?: WrappedKey; // present during two-phase rotation
	};
}
 
// ── Crypto primitives ──────────────────────────────────────────────────────
 
function generateVaultKey(): Buffer {
	return randomBytes(KEY_BYTES);
}
 
interface KdfParams {
	memoryCost: number;
	timeCost: number;
	parallelism: number;
}
 
function deriveKEK(password: string, salt: Buffer, params?: KdfParams): Buffer {
	const key = argon2id(password, salt, {
		m: params?.memoryCost ?? ARGON2_MEMORY,
		t: params?.timeCost ?? ARGON2_ITERATIONS,
		p: params?.parallelism ?? ARGON2_PARALLELISM,
		dkLen: KEY_BYTES,
	});
	return Buffer.from(key);
}
 
function recoveryMnemonicToKey(mnemonic: string): Buffer {
	// BIP39 entropy → 32 bytes (256-bit mnemonic → 32-byte raw entropy)
	const entropy = mnemonicToEntropy(mnemonic);
	return Buffer.from(entropy, "hex");
}
 
function wrapKey(vaultKey: Buffer, wrappingKey: Buffer): WrappedKey {
	const iv = randomBytes(12);
	const cipher = createCipheriv("aes-256-gcm", wrappingKey, iv);
	const ciphertext = Buffer.concat([cipher.update(vaultKey), cipher.final()]);
	const tag = cipher.getAuthTag();
	return {
		iv: iv.toString("base64"),
		ciphertext: ciphertext.toString("base64"),
		tag: tag.toString("base64"),
	};
}
 
function unwrapKey(envelope: WrappedKey, wrappingKey: Buffer): Buffer {
	const iv = Buffer.from(envelope.iv, "base64");
	const ciphertext = Buffer.from(envelope.ciphertext, "base64");
	const tag = Buffer.from(envelope.tag, "base64");
	const decipher = createDecipheriv("aes-256-gcm", wrappingKey, iv);
	decipher.setAuthTag(tag);
	try {
		const vaultKey = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
		return vaultKey;
	} catch {
		throw new Error("Decryption failed: wrong password or corrupted key file");
	}
}
 
function zeroBuffer(buf: Buffer): void {
	buf.fill(0);
}
 
// ── Key file I/O ───────────────────────────────────────────────────────────
 
function keysFilePath(dataDir: string): string {
	return join(dataDir, "stork.keys");
}
 
export function keysFileExists(dataDir: string): boolean {
	return existsSync(keysFilePath(dataDir));
}
 
function readKeysFile(dataDir: string): KeysFile {
	const raw = readFileSync(keysFilePath(dataDir), "utf8");
	return JSON.parse(raw) as KeysFile;
}
 
function writeKeysFile(dataDir: string, data: KeysFile): void {
	const path = keysFilePath(dataDir);
	const tmp = `${path}.tmp`;
	writeFileSync(tmp, JSON.stringify(data, null, 2), { mode: 0o600 });
	renameSync(tmp, path);
}
 
// ── Public API ─────────────────────────────────────────────────────────────
 
/**
 * First-boot setup: generate vault key, wrap with both password and recovery key.
 * Writes stork.keys and returns the 24-word BIP39 recovery mnemonic for display.
 */
export function initializeEncryption(dataDir: string, password: string): string {
	const vaultKey = generateVaultKey();
	const passwordSalt = randomBytes(KEY_BYTES);
	const kek = deriveKEK(password, passwordSalt);
 
	// Generate recovery mnemonic (24 words = 256 bits of entropy)
	const recoveryMnemonic = generateMnemonic(256);
	const recoveryKey = recoveryMnemonicToKey(recoveryMnemonic);
 
	const keysData: KeysFile = {
		version: 1,
		kdf: {
			algorithm: "argon2id",
			salt: passwordSalt.toString("base64"),
			memoryCost: ARGON2_MEMORY,
			timeCost: ARGON2_ITERATIONS,
			parallelism: ARGON2_PARALLELISM,
		},
		wrappedMasterKey: {
			password: wrapKey(vaultKey, kek),
			recovery: wrapKey(vaultKey, recoveryKey),
		},
	};
 
	writeKeysFile(dataDir, keysData);
 
	zeroBuffer(kek);
	zeroBuffer(recoveryKey);
	// vaultKey is returned conceptually — caller must zero it after opening the DB
	zeroBuffer(vaultKey);
 
	return recoveryMnemonic;
}
 
/**
 * Unlock with password. Returns the vault key buffer.
 * Caller MUST zero the returned buffer after passing it to SQLCipher.
 */
export function unlockWithPassword(dataDir: string, password: string): Buffer {
	const keysData = readKeysFile(dataDir);
	const passwordSalt = Buffer.from(keysData.kdf.salt, "base64");
	const kek = deriveKEK(password, passwordSalt, keysData.kdf);
	try {
		const vaultKey = unwrapKey(keysData.wrappedMasterKey.password, kek);
		zeroBuffer(kek);
		return vaultKey;
	} catch (e) {
		zeroBuffer(kek);
		throw e;
	}
}
 
/**
 * Unlock with BIP39 recovery mnemonic. Returns the vault key buffer.
 * Tries both the active recovery envelope and any pending rotation envelope.
 */
export function unlockWithRecovery(dataDir: string, mnemonic: string): Buffer {
	if (!validateMnemonic(mnemonic)) {
		throw new Error("Invalid recovery mnemonic");
	}
	const keysData = readKeysFile(dataDir);
	const recoveryKey = recoveryMnemonicToKey(mnemonic);
 
	// Try active recovery envelope first
	try {
		const vaultKey = unwrapKey(keysData.wrappedMasterKey.recovery, recoveryKey);
		zeroBuffer(recoveryKey);
		return vaultKey;
	} catch {
		// Fall through to try pending envelope
	}
 
	// Try pending recovery envelope (mid-rotation state)
	if (keysData.wrappedMasterKey.pendingRecovery) {
		try {
			const vaultKey = unwrapKey(keysData.wrappedMasterKey.pendingRecovery, recoveryKey);
			zeroBuffer(recoveryKey);
			return vaultKey;
		} catch {
			// Both failed
		}
	}
 
	zeroBuffer(recoveryKey);
	throw new Error("Decryption failed: wrong password or corrupted key file");
}
 
/**
 * Change password. Re-wraps vault key with new KEK. O(1) — database untouched.
 */
export function changePassword(
	dataDir: string,
	currentPassword: string,
	newPassword: string,
): void {
	const vaultKey = unlockWithPassword(dataDir, currentPassword);
	const keysData = readKeysFile(dataDir);
 
	const newSalt = randomBytes(KEY_BYTES);
	const newKek = deriveKEK(newPassword, newSalt);
 
	keysData.kdf.salt = newSalt.toString("base64");
	keysData.wrappedMasterKey.password = wrapKey(vaultKey, newKek);
 
	writeKeysFile(dataDir, keysData);
 
	zeroBuffer(newKek);
	zeroBuffer(vaultKey);
}
 
/**
 * Rotate recovery key — Phase 1 (prepare).
 *
 * Generates a new recovery envelope and stores it as `pendingRecovery` alongside
 * the existing `recovery` envelope. Both the old and new mnemonics will unlock
 * the vault until the rotation is confirmed or cancelled.
 *
 * Returns the new 24-word BIP39 mnemonic for the user to write down.
 */
export function prepareRecoveryKeyRotation(dataDir: string, currentPassword: string): string {
	const vaultKey = unlockWithPassword(dataDir, currentPassword);
	const keysData = readKeysFile(dataDir);
 
	const newMnemonic = generateMnemonic(256);
	const newRecoveryKey = recoveryMnemonicToKey(newMnemonic);
 
	keysData.wrappedMasterKey.pendingRecovery = wrapKey(vaultKey, newRecoveryKey);
 
	writeKeysFile(dataDir, keysData);
 
	zeroBuffer(newRecoveryKey);
	zeroBuffer(vaultKey);
 
	return newMnemonic;
}
 
/**
 * Rotate recovery key — Phase 2 (confirm).
 *
 * Promotes `pendingRecovery` to `recovery` and deletes the old envelope.
 * After this call, only the new mnemonic (from prepareRecoveryKeyRotation) works.
 * Requires password to authorize.
 */
export function confirmRecoveryKeyRotation(dataDir: string, currentPassword: string): void {
	// Verify password is correct
	const vaultKey = unlockWithPassword(dataDir, currentPassword);
	zeroBuffer(vaultKey);
 
	const keysData = readKeysFile(dataDir);
	if (!keysData.wrappedMasterKey.pendingRecovery) {
		throw new Error("No pending recovery key rotation to confirm");
	}
 
	keysData.wrappedMasterKey.recovery = keysData.wrappedMasterKey.pendingRecovery;
	keysData.wrappedMasterKey.pendingRecovery = undefined;
 
	writeKeysFile(dataDir, keysData);
}
 
/**
 * Cancel a pending recovery key rotation.
 *
 * Removes the `pendingRecovery` envelope, leaving the original recovery key intact.
 */
export function cancelRecoveryKeyRotation(dataDir: string): void {
	const keysData = readKeysFile(dataDir);
	if (!keysData.wrappedMasterKey.pendingRecovery) {
		return; // nothing to cancel
	}
 
	keysData.wrappedMasterKey.pendingRecovery = undefined;
	writeKeysFile(dataDir, keysData);
}
 
/**
 * Check whether a recovery key rotation is pending confirmation.
 */
export function hasPendingRecoveryRotation(dataDir: string): boolean {
	Iif (!keysFileExists(dataDir)) return false;
	const keysData = readKeysFile(dataDir);
	return !!keysData.wrappedMasterKey.pendingRecovery;
}
 
/**
 * @deprecated Use prepareRecoveryKeyRotation + confirmRecoveryKeyRotation instead.
 * Kept for backward compatibility — performs an atomic (non-resilient) rotation.
 */
export function rotateRecoveryKey(dataDir: string, currentPassword: string): string {
	const vaultKey = unlockWithPassword(dataDir, currentPassword);
	const keysData = readKeysFile(dataDir);
 
	const newMnemonic = generateMnemonic(256);
	const newRecoveryKey = recoveryMnemonicToKey(newMnemonic);
 
	keysData.wrappedMasterKey.recovery = wrapKey(vaultKey, newRecoveryKey);
 
	writeKeysFile(dataDir, keysData);
 
	zeroBuffer(newRecoveryKey);
	zeroBuffer(vaultKey);
 
	return newMnemonic;
}
 
/**
 * Set a new password using an already-unwrapped vault key.
 * Used during recovery unlock to set a new password without knowing the old one.
 */
export function setPasswordFromVaultKey(
	dataDir: string,
	vaultKey: Buffer,
	newPassword: string,
): void {
	const keysData = readKeysFile(dataDir);
 
	const newSalt = randomBytes(KEY_BYTES);
	const newKek = deriveKEK(newPassword, newSalt);
 
	keysData.kdf.salt = newSalt.toString("base64");
	keysData.wrappedMasterKey.password = wrapKey(vaultKey, newKek);
 
	writeKeysFile(dataDir, keysData);
 
	zeroBuffer(newKek);
}
 
/**
 * Unlock with vault key already in hand (used after initializeEncryption sets up keys
 * and the caller needs to also open the database in the same session).
 * Re-derives vault key from password.
 */
export function getVaultKey(dataDir: string, password: string): Buffer {
	return unlockWithPassword(dataDir, password);
}