From 8b56ff4effcb121b4b1f391b080eaa6bdb83bc17 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 26 Jun 2020 10:59:04 +0200 Subject: [PATCH] ask general crypto callbacks for 4S privkey if operation adapter doesn't --- src/crypto/EncryptionSetup.js | 21 +++++++++++++++++---- src/crypto/index.js | 6 +++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/crypto/EncryptionSetup.js b/src/crypto/EncryptionSetup.js index 38aee3338dc..69a047dbea7 100644 --- a/src/crypto/EncryptionSetup.js +++ b/src/crypto/EncryptionSetup.js @@ -18,11 +18,12 @@ import { export class EncryptionSetupBuilder { /** * @param {Object.} accountData pre-existing account data, will only be read, not written. + * @param {CryptoCallbacks} delegateCryptoCallbacks crypto callbacks to delegate to if the key isn't in cache yet */ - constructor(accountData) { + constructor(accountData, delegateCryptoCallbacks) { this.accountDataClientAdapter = new AccountDataClientAdapter(accountData); this.crossSigningCallbacks = new CrossSigningCallbacks(); - this.ssssCryptoCallbacks = new SSSSCryptoCallbacks(); + this.ssssCryptoCallbacks = new SSSSCryptoCallbacks(delegateCryptoCallbacks); this._crossSigningKeys = null; this._keySignatures = null; @@ -308,17 +309,29 @@ class CrossSigningCallbacks { * the SecretStorage crypto callbacks */ class SSSSCryptoCallbacks { - constructor() { + constructor(delegateCryptoCallbacks) { this._privateKeys = new Map(); + this._delegateCryptoCallbacks = delegateCryptoCallbacks; } - getSecretStorageKey({ keys }, name) { + async getSecretStorageKey({ keys }, name) { for (const keyId of Object.keys(keys)) { const privateKey = this._privateKeys.get(keyId); if (privateKey) { return [keyId, privateKey]; } } + // if we don't have the key cached yet, ask + // for it to the general crypto callbacks and cache it + if (this._delegateCryptoCallbacks) { + const result = await this._delegateCryptoCallbacks. + getSecretStorageKey({keys}, name); + if (result) { + const [keyId, privateKey] = result; + this._privateKeys.set(keyId, privateKey); + } + return result; + } } addPrivateKey(keyId, privKey) { diff --git a/src/crypto/index.js b/src/crypto/index.js index 1c92b4fede8..caaaba5e481 100644 --- a/src/crypto/index.js +++ b/src/crypto/index.js @@ -483,7 +483,11 @@ Crypto.prototype.bootstrapSecretStorage = async function({ getKeyBackupPassphrase, } = {}) { logger.log("Bootstrapping Secure Secret Storage"); - const builder = new EncryptionSetupBuilder(this._baseApis.store.accountData); + const delegateCryptoCallbacks = this._baseApis._cryptoCallbacks; + const builder = new EncryptionSetupBuilder( + this._baseApis.store.accountData, + delegateCryptoCallbacks, + ); const secretStorage = new SecretStorage( builder.accountDataClientAdapter, builder.ssssCryptoCallbacks);