From 6baf9e1c37dd8cf4908db4ce4a1b9a9873a54cdd Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Fri, 2 Sep 2016 11:33:50 +0100 Subject: [PATCH] Olm-related cleanups A couple of small refactors which fell out of the aborted stuff for upgrading to secure Ed25519 keys, but are useful in their own right. The main functional change here is to calculate the "algorithms" list from DECRYPTION_CLASSES (and hence include megolm in the list). --- lib/OlmDevice.js | 20 ++++++++++++-------- lib/client.js | 1 + lib/crypto.js | 18 ++++++++++-------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/lib/OlmDevice.js b/lib/OlmDevice.js index b9ca3942a..fe1f85510 100644 --- a/lib/OlmDevice.js +++ b/lib/OlmDevice.js @@ -46,14 +46,7 @@ function OlmDevice(sessionStore) { var e2eKeys; var account = new Olm.Account(); try { - var e2eAccount = this._sessionStore.getEndToEndAccount(); - if (e2eAccount === null) { - account.create(); - var pickled = account.pickle(this._pickleKey); - this._sessionStore.storeEndToEndAccount(pickled); - } else { - account.unpickle(this._pickleKey, e2eAccount); - } + _initialise_account(this._sessionStore, this._pickleKey, account); e2eKeys = JSON.parse(account.identity_keys()); } finally { account.free(); @@ -67,6 +60,17 @@ function OlmDevice(sessionStore) { this._outboundGroupSessionStore = {}; } +function _initialise_account(sessionStore, pickleKey, account) { + var e2eAccount = sessionStore.getEndToEndAccount(); + if (e2eAccount !== null) { + account.unpickle(pickleKey, e2eAccount); + return; + } + + account.create(); + var pickled = account.pickle(pickleKey); + sessionStore.storeEndToEndAccount(pickled); +} /** * extract our OlmAccount from the session store and call the given function diff --git a/lib/client.js b/lib/client.js index accdaa3db..a9c2ac740 100644 --- a/lib/client.js +++ b/lib/client.js @@ -46,6 +46,7 @@ try { var Crypto = require("./crypto"); CRYPTO_ENABLED = true; } catch (e) { + console.error("olm load error", e); // Olm not installed. } diff --git a/lib/crypto.js b/lib/crypto.js index e94f14bc8..213b0ec9c 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -23,11 +23,10 @@ limitations under the License. var anotherjson = require('another-json'); var q = require("q"); +var utils = require("./utils"); var OlmDevice = require("./OlmDevice"); var olmlib = require("./olmlib"); - var algorithms = require("./crypto-algorithms"); - var DeviceInfo = require("./crypto-deviceinfo"); var DeviceVerification = DeviceInfo.DeviceVerification; @@ -58,6 +57,10 @@ function Crypto(baseApis, sessionStore, userId, deviceId) { // EncryptionAlgorithm instance for each room this._roomAlgorithms = {}; + this._supportedAlgorithms = utils.keys( + algorithms.DECRYPTION_CLASSES + ); + // build our device keys: these will later be uploaded this._deviceKeys = {}; this._deviceKeys["ed25519:" + this._deviceId] = @@ -68,7 +71,7 @@ function Crypto(baseApis, sessionStore, userId, deviceId) { // add our own deviceinfo to the sessionstore var deviceInfo = { keys: this._deviceKeys, - algorithms: [olmlib.OLM_ALGORITHM], + algorithms: this._supportedAlgorithms, verified: DeviceVerification.VERIFIED, }; var myDevices = this._sessionStore.getEndToEndDevicesForUser( @@ -121,7 +124,7 @@ function _uploadDeviceKeys(crypto) { var deviceId = crypto._deviceId; var deviceKeys = { - algorithms: [olmlib.OLM_ALGORITHM], + algorithms: crypto._supportedAlgorithms, device_id: deviceId, keys: crypto._deviceKeys, user_id: userId, @@ -288,7 +291,7 @@ function _storeDeviceKeys(_olmDevice, userId, deviceId, userStore, deviceResult) return false; } - var unsigned = deviceResult.unsigned; + var unsigned = deviceResult.unsigned || {}; var signatures = deviceResult.signatures || {}; var userSigs = signatures[userId] || {}; var signature = userSigs[signKeyId]; @@ -332,13 +335,12 @@ function _storeDeviceKeys(_olmDevice, userId, deviceId, userStore, deviceResult) userStore[deviceId] = deviceStore = new DeviceInfo(deviceId); } - deviceStore.keys = deviceResult.keys; - deviceStore.algorithms = deviceResult.algorithms; + deviceStore.keys = deviceResult.keys || {}; + deviceStore.algorithms = deviceResult.algorithms || []; deviceStore.unsigned = unsigned; return true; } - /** * Get the stored device keys for a user id *