aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/keyWrapping.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/keyWrapping.ts')
-rw-r--r--src/lib/keyWrapping.ts17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/lib/keyWrapping.ts b/src/lib/keyWrapping.ts
index 31cb1e5c..cd68947f 100644
--- a/src/lib/keyWrapping.ts
+++ b/src/lib/keyWrapping.ts
@@ -28,9 +28,20 @@ export async function resolveCanonicalKey(
oldPrfKey: ArrayBuffer,
): Promise<{ canonicalKey: ArrayBuffer; encryptedKeys: Record<string, string> }> {
if (encryptedKeys && encryptedKeys[methodId]) {
- const canonicalKey = await unwrapCanonicalKey(wrappingKey, encryptedKeys[methodId]);
- rememberPrfKey(canonicalKey, defaultRememberForMs);
- return { canonicalKey, encryptedKeys };
+ try {
+ // Try new wrapping key first (e.g. PRF-derived)
+ const canonicalKey = await unwrapCanonicalKey(wrappingKey, encryptedKeys[methodId]);
+ rememberPrfKey(canonicalKey, defaultRememberForMs);
+ return { canonicalKey, encryptedKeys };
+ } catch {
+ // Fallback: try old key (e.g. SHA-256(credentialId)) and re-wrap with new key
+ const canonicalKey = await unwrapCanonicalKey(oldPrfKey, encryptedKeys[methodId]);
+ const wrapped = await wrapCanonicalKey(wrappingKey, canonicalKey);
+ const updated = { ...encryptedKeys, [methodId]: wrapped };
+ await saveEncryptedKeys(updated);
+ rememberPrfKey(canonicalKey, defaultRememberForMs);
+ return { canonicalKey, encryptedKeys: updated };
+ }
}
// Migration: old PRF key becomes canonical key