From 33aa475e18f7f42a45f152292e4bdc59a7650e33 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 17 Sep 2020 15:16:01 +0200 Subject: [PATCH] add function to create key from either passphrase or recovery key --- src/matrix/e2ee/ssss/common.js | 38 ------------------ src/matrix/e2ee/ssss/index.js | 73 ++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 38 deletions(-) create mode 100644 src/matrix/e2ee/ssss/index.js diff --git a/src/matrix/e2ee/ssss/common.js b/src/matrix/e2ee/ssss/common.js index 2f70a4cb..38a14790 100644 --- a/src/matrix/e2ee/ssss/common.js +++ b/src/matrix/e2ee/ssss/common.js @@ -51,41 +51,3 @@ export class Key { return this._keyDescription.algorithm; } } - -export async function readDefaultKeyDescription(storage) { - const txn = await storage.readTxn([ - storage.storeNames.accountData - ]); - const defaultKeyEvent = await txn.accountData.get("m.secret_storage.default_key"); - const id = defaultKeyEvent?.content?.key; - if (!id) { - return; - } - const keyAccountData = await txn.accountData.get(`m.secret_storage.key.${id}`); - if (!keyAccountData) { - return; - } - return new KeyDescription(id, keyAccountData); -} - -export async function writeKey(storage, key) { - const txn = await storage.readWriteTxn([ - storage.storeNames.session - ]); - try { - txn.session.set("ssssKey", {id: key.id, binaryKey: key.binaryKey}); - } catch (err) { - txn.abort(); - throw err; - } - await txn.complete(); -} - -export async function readKey(txn) { - const keyData = await txn.session.get("ssssKey"); - if (!keyData) { - return; - } - const keyAccountData = await txn.accountData.get(`m.secret_storage.key.${keyData.id}`); - return new Key(new KeyDescription(keyData.id, keyAccountData), keyData.binaryKey); -} diff --git a/src/matrix/e2ee/ssss/index.js b/src/matrix/e2ee/ssss/index.js new file mode 100644 index 00000000..120a0909 --- /dev/null +++ b/src/matrix/e2ee/ssss/index.js @@ -0,0 +1,73 @@ +/* +Copyright 2020 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import {KeyDescription, Key} from "./common.js"; +import {keyFromPassphrase} from "./passphrase.js"; +import {keyFromRecoveryKey} from "./recoveryKey.js"; + +async function readDefaultKeyDescription(storage) { + const txn = await storage.readTxn([ + storage.storeNames.accountData + ]); + const defaultKeyEvent = await txn.accountData.get("m.secret_storage.default_key"); + const id = defaultKeyEvent?.content?.key; + if (!id) { + return; + } + const keyAccountData = await txn.accountData.get(`m.secret_storage.key.${id}`); + if (!keyAccountData) { + return; + } + return new KeyDescription(id, keyAccountData); +} + +export async function writeKey(storage, key) { + const txn = await storage.readWriteTxn([ + storage.storeNames.session + ]); + try { + txn.session.set("ssssKey", {id: key.id, binaryKey: key.binaryKey}); + } catch (err) { + txn.abort(); + throw err; + } + await txn.complete(); +} + +export async function readKey(txn) { + const keyData = await txn.session.get("ssssKey"); + if (!keyData) { + return; + } + const keyAccountData = await txn.accountData.get(`m.secret_storage.key.${keyData.id}`); + return new Key(new KeyDescription(keyData.id, keyAccountData), keyData.binaryKey); +} + +export async function keyFromCredential(type, credential, storage, cryptoDriver) { + const keyDescription = await readDefaultKeyDescription(storage); + if (!keyDescription) { + throw new Error("Could not find any secret storage key in account data"); + } + let key; + if (type === "passphrase") { + key = await keyFromPassphrase(keyDescription, credential, cryptoDriver); + } else if (type === "recoverykey") { + key = keyFromRecoveryKey(this._olm, keyDescription, credential); + } else { + throw new Error(`Invalid type: ${type}`); + } + return key; +}