From a61462bc8559815a4ea08d599268b773b681483c Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Thu, 11 Feb 2021 16:34:15 -0500 Subject: [PATCH 1/2] use the default SSSS key if the default is set implements MSC2874 --- src/SecurityManager.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/SecurityManager.ts b/src/SecurityManager.ts index 220320470a..11d228e7ab 100644 --- a/src/SecurityManager.ts +++ b/src/SecurityManager.ts @@ -98,11 +98,24 @@ async function getSecretStorageKey( { keys: keyInfos }: { keys: Record }, ssssItemName, ): Promise<[string, Uint8Array]> { - const keyInfoEntries = Object.entries(keyInfos); - if (keyInfoEntries.length > 1) { - throw new Error("Multiple storage key requests not implemented"); + const cli = MatrixClientPeg.get(); + let keyId = await cli.getDefaultSecretStorageKeyId(); + let keyInfo; + if (keyId) { + // use the default SSSS key if set + keyInfo = keyInfos[keyId]; + if (!keyInfo) { + throw new Error("Unable to use default SSSS key"); + } + } else { + // if no default SSSS key is set, fall back to a heuristic of using the + // only available key, if only one key is set + const keyInfoEntries = Object.entries(keyInfos); + if (keyInfoEntries.length > 1) { + throw new Error("Multiple storage key requests not implemented"); + } + [keyId, keyInfo] = keyInfoEntries[0]; } - const [keyId, keyInfo] = keyInfoEntries[0]; // Check the in-memory cache if (isCachingAllowed() && secretStorageKeys[keyId]) { From 5f74fac2e84057ff9155474416e00793c671c89f Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Wed, 24 Feb 2021 17:55:27 -0500 Subject: [PATCH 2/2] fall back to the old method if the default key isn't available --- src/SecurityManager.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/SecurityManager.ts b/src/SecurityManager.ts index 11d228e7ab..03cbe88c22 100644 --- a/src/SecurityManager.ts +++ b/src/SecurityManager.ts @@ -105,9 +105,12 @@ async function getSecretStorageKey( // use the default SSSS key if set keyInfo = keyInfos[keyId]; if (!keyInfo) { - throw new Error("Unable to use default SSSS key"); + // if the default key is not available, pretend the default key + // isn't set + keyId = undefined; } - } else { + } + if (!keyId) { // if no default SSSS key is set, fall back to a heuristic of using the // only available key, if only one key is set const keyInfoEntries = Object.entries(keyInfos);