From 6701fdd4867c7f3027e0c6d5ee5a15c70562a19c Mon Sep 17 00:00:00 2001 From: Zoe Date: Fri, 20 Mar 2020 10:18:06 +0000 Subject: [PATCH] Rename ssss cache functions to be more general --- spec/unit/crypto/backup.spec.js | 54 ------------------- src/crypto/CrossSigning.js | 4 +- .../store/indexeddb-crypto-store-backend.js | 21 +------- src/crypto/store/indexeddb-crypto-store.js | 28 ++-------- src/crypto/store/localStorage-crypto-store.js | 15 +----- src/crypto/store/memory-crypto-store.js | 13 +---- 6 files changed, 12 insertions(+), 123 deletions(-) diff --git a/spec/unit/crypto/backup.spec.js b/spec/unit/crypto/backup.spec.js index bd5f05bce..50c336325 100644 --- a/spec/unit/crypto/backup.spec.js +++ b/spec/unit/crypto/backup.spec.js @@ -23,15 +23,10 @@ import {MatrixEvent} from "../../../src/models/event"; import * as algorithms from "../../../src/crypto/algorithms"; import {WebStorageSessionStore} from "../../../src/store/session/webstorage"; import {MemoryCryptoStore} from "../../../src/crypto/store/memory-crypto-store"; -import { - IndexedDBCryptoStore, -} from '../../../src/crypto/store/indexeddb-crypto-store'; import {MockStorageApi} from "../../MockStorageApi"; import * as testUtils from "../../test-utils"; import {OlmDevice} from "../../../src/crypto/OlmDevice"; import {Crypto} from "../../../src/crypto"; -import 'fake-indexeddb/auto'; -import 'jest-localstorage-mock'; const Olm = global.Olm; @@ -84,13 +79,6 @@ const BACKUP_INFO = { }, }; -const testKey = new Uint8Array([ - 0xda, 0x5a, 0x27, 0x60, 0xe3, 0x3a, 0xc5, 0x82, - 0x9d, 0x12, 0xc3, 0xbe, 0xe8, 0xaa, 0xc2, 0xef, - 0xae, 0xb1, 0x05, 0xc1, 0xe7, 0x62, 0x78, 0xa6, - 0xd7, 0x1f, 0xf8, 0x2c, 0x51, 0x85, 0xf0, 0x1d, -]); - const keys = {}; function getCrossSigningKey(type) { @@ -555,45 +543,3 @@ describe("MegolmBackup", function() { }); }); }); - -describe.each([ - ["IndexedDBCryptoStore", - () => new IndexedDBCryptoStore(global.indexedDB, "tests")], - ["LocalStorageCryptoStore", - () => new IndexedDBCryptoStore(undefined, "tests")], - ["MemoryCryptoStore", () => { - const store = new IndexedDBCryptoStore(undefined, "tests"); - store._backend = new MemoryCryptoStore(); - store._backendPromise = Promise.resolve(store._backend); - return store; - }], -])("Crypto store backup key cache functions [%s]", function(name, dbFactory) { - let store; - - beforeAll(async () => { - store = dbFactory(); - await store.startup(); - }); - - it("Stores and retrieves a backup key", async () => { - expect(store._backend).not.toBeNull(); - - await store.doTxn( - 'readwrite', - [IndexedDBCryptoStore.STORE_ACCOUNT], - (txn) => { - store.storeBackupKey(txn, "m.megolm_backup.v1", testKey); - }, - ); - const result = await new Promise((resolve) => { - store.doTxn( - 'readonly', - [IndexedDBCryptoStore.STORE_ACCOUNT], - (txn) => { - store.getBackupKey(txn, resolve, "m.megolm_backup.v1"); - }, - ); - }); - expect(result).toEqual(testKey); - }); -}); diff --git a/src/crypto/CrossSigning.js b/src/crypto/CrossSigning.js index 5bb6dacd6..4dc464e82 100644 --- a/src/crypto/CrossSigning.js +++ b/src/crypto/CrossSigning.js @@ -585,7 +585,7 @@ export function createCryptoStoreCacheCallbacks(store) { 'readonly', [IndexedDBCryptoStore.STORE_ACCOUNT], (txn) => { - store.getCrossSigningPrivateKey(txn, resolve, type); + store.getSecretStorePrivateKey(txn, resolve, type); }, ); }); @@ -595,7 +595,7 @@ export function createCryptoStoreCacheCallbacks(store) { 'readwrite', [IndexedDBCryptoStore.STORE_ACCOUNT], (txn) => { - store.storeCrossSigningPrivateKey(txn, type, key); + store.storeSecretStorePrivateKey(txn, type, key); }, ); }, diff --git a/src/crypto/store/indexeddb-crypto-store-backend.js b/src/crypto/store/indexeddb-crypto-store-backend.js index c76590cb6..51c7006de 100644 --- a/src/crypto/store/indexeddb-crypto-store-backend.js +++ b/src/crypto/store/indexeddb-crypto-store-backend.js @@ -341,7 +341,7 @@ export class Backend { }; } - getCrossSigningPrivateKey(txn, func, type) { + getSecretStorePrivateKey(txn, func, type) { const objectStore = txn.objectStore("account"); const getReq = objectStore.get(`ssss_cache:${type}`); getReq.onsuccess = function() { @@ -358,28 +358,11 @@ export class Backend { objectStore.put(keys, "crossSigningKeys"); } - storeCrossSigningPrivateKey(txn, type, key) { + storeSecretStorePrivateKey(txn, type, key) { const objectStore = txn.objectStore("account"); objectStore.put(key, `ssss_cache:${type}`); } - getBackupKey(txn, func, type) { - const objectStore = txn.objectStore("account"); - const getReq = objectStore.get(`backup_key_cache:${type}`); - getReq.onsuccess = function() { - try { - func(getReq.result || null); - } catch (e) { - abortWithException(txn, e); - } - }; - } - - storeBackupKey(txn, type, key) { - const objectStore = txn.objectStore("account"); - objectStore.put(key, `backup_key_cache:${type}`); - } - // Olm Sessions countEndToEndSessions(txn, func) { diff --git a/src/crypto/store/indexeddb-crypto-store.js b/src/crypto/store/indexeddb-crypto-store.js index df8488971..44d3afb0a 100644 --- a/src/crypto/store/indexeddb-crypto-store.js +++ b/src/crypto/store/indexeddb-crypto-store.js @@ -313,8 +313,8 @@ export class IndexedDBCryptoStore { * @param {function(string)} func Called with the private key * @param {string} type A key type */ - getCrossSigningPrivateKey(txn, func, type) { - this._backend.getCrossSigningPrivateKey(txn, func, type); + getSecretStorePrivateKey(txn, func, type) { + this._backend.getSecretStorePrivateKey(txn, func, type); } /** @@ -334,28 +334,8 @@ export class IndexedDBCryptoStore { * @param {string} type The type of cross-signing private key to store * @param {string} key keys object as getCrossSigningKeys() */ - storeCrossSigningPrivateKey(txn, type, key) { - this._backend.storeCrossSigningPrivateKey(txn, type, key); - } - - /** - * Retrieve session backup keys from the store - * @param {*} txn An active transaction. See doTxn(). - * @param {function(Uint8Array)} func Called with the private key - * @param {string} type A key type - */ - getBackupKey(txn, func, type) { - this._backend.getBackupKey(txn, func, type); - } - - /** - * - * @param {*} txn - * @param {string} type A key type - * @param {Uint8Array} key A private key - */ - storeBackupKey(txn, type, key) { - this._backend.storeBackupKey(txn, type, key); + storeSecretStorePrivateKey(txn, type, key) { + this._backend.storeSecretStorePrivateKey(txn, type, key); } // Olm sessions diff --git a/src/crypto/store/localStorage-crypto-store.js b/src/crypto/store/localStorage-crypto-store.js index c120184c9..1e63affee 100644 --- a/src/crypto/store/localStorage-crypto-store.js +++ b/src/crypto/store/localStorage-crypto-store.js @@ -367,7 +367,7 @@ export class LocalStorageCryptoStore extends MemoryCryptoStore { func(keys); } - getCrossSigningPrivateKey(txn, func, type) { + getSecretStorePrivateKey(txn, func, type) { const key = getJsonItem(this.store, E2E_PREFIX + `ssss_cache.${type}`); func(key ? Uint8Array.from(key) : key); } @@ -378,23 +378,12 @@ export class LocalStorageCryptoStore extends MemoryCryptoStore { ); } - storeCrossSigningPrivateKey(txn, type, key) { + storeSecretStorePrivateKey(txn, type, key) { setJsonItem( this.store, E2E_PREFIX + `ssss_cache.${type}`, Array.from(key), ); } - getBackupKey(txn, func, type) { - const key = getJsonItem(this.store, `backup_key_cache.${type}`); - func(key ? Uint8Array.from(key) : key); - } - - storeBackupKey(txn, type, key) { - setJsonItem( - this.store, `backup_key_cache.${type}`, Array.from(key), - ); - } - doTxn(mode, stores, func) { return Promise.resolve(func(null)); } diff --git a/src/crypto/store/memory-crypto-store.js b/src/crypto/store/memory-crypto-store.js index f445978cc..5af806a94 100644 --- a/src/crypto/store/memory-crypto-store.js +++ b/src/crypto/store/memory-crypto-store.js @@ -257,7 +257,7 @@ export class MemoryCryptoStore { func(this._crossSigningKeys); } - getCrossSigningPrivateKey(txn, func, type) { + getSecretStorePrivateKey(txn, func, type) { const result = this._privateKeys[type]; return func(result || null); } @@ -266,19 +266,10 @@ export class MemoryCryptoStore { this._crossSigningKeys = keys; } - storeCrossSigningPrivateKey(txn, type, key) { + storeSecretStorePrivateKey(txn, type, key) { this._privateKeys[type] = key; } - getBackupKey(txn, func, type) { - const result = this._backupKeys[type]; - return func(result || null); - } - - storeBackupKey(txn, type, key) { - this._backupKeys[type] = key; - } - // Olm Sessions countEndToEndSessions(txn, func) {