diff --git a/src/matrix/e2ee/Account.js b/src/matrix/e2ee/Account.js index 7b4a5b0d..7d5075ae 100644 --- a/src/matrix/e2ee/Account.js +++ b/src/matrix/e2ee/Account.js @@ -149,10 +149,14 @@ export class Account { } } - createOutboundOlmSession(theirIdentityKey, theirOneTimeKey) { + async createOutboundOlmSession(theirIdentityKey, theirOneTimeKey) { const newSession = new this._olm.Session(); try { - newSession.create_outbound(this._account, theirIdentityKey, theirOneTimeKey); + if (this._olmWorker) { + await this._olmWorker.createOutboundOlmSession(this._account, newSession, theirIdentityKey, theirOneTimeKey); + } else { + newSession.create_outbound(this._account, theirIdentityKey, theirOneTimeKey); + } return newSession; } catch (err) { newSession.free(); diff --git a/src/matrix/e2ee/OlmWorker.js b/src/matrix/e2ee/OlmWorker.js index a6edd3cc..649db309 100644 --- a/src/matrix/e2ee/OlmWorker.js +++ b/src/matrix/e2ee/OlmWorker.js @@ -37,6 +37,12 @@ export class OlmWorker { account.unpickle("", pickle); } + async createOutboundSession(account, newSession, theirIdentityKey, theirOneTimeKey) { + const accountPickle = account.pickle(""); + const sessionPickle = await this._workerPool.send({type: "olm_create_outbound", accountPickle, theirIdentityKey, theirOneTimeKey}).response(); + newSession.unpickle("", sessionPickle); + } + dispose() { this._workerPool.dispose(); } diff --git a/src/matrix/e2ee/olm/Encryption.js b/src/matrix/e2ee/olm/Encryption.js index 18bbc5fa..8ce4583a 100644 --- a/src/matrix/e2ee/olm/Encryption.js +++ b/src/matrix/e2ee/olm/Encryption.js @@ -154,7 +154,7 @@ export class Encryption { try { for (const target of newEncryptionTargets) { const {device, oneTimeKey} = target; - target.session = this._account.createOutboundOlmSession(device.curve25519Key, oneTimeKey); + target.session = await this._account.createOutboundOlmSession(device.curve25519Key, oneTimeKey); } this._storeSessions(newEncryptionTargets, timestamp); } catch (err) { diff --git a/src/platform/web/worker/main.js b/src/platform/web/worker/main.js index a0016f67..e2413049 100644 --- a/src/platform/web/worker/main.js +++ b/src/platform/web/worker/main.js @@ -136,6 +136,18 @@ class MessageHandler { account.generate_one_time_keys(otkAmount); this._checkRandomValuesUsed(); return account.pickle(""); + _olmCreateOutbound(accountPickle, theirIdentityKey, theirOneTimeKey) { + return this._toMessage(() => { + const account = new this._olm.Account(); + const newSession = new this._olm.Session(); + try { + account.unpickle("", accountPickle); + newSession.create_outbound(account, newSession, theirIdentityKey, theirOneTimeKey); + return newSession.pickle(""); + } finally { + account.free(); + newSession.free(); + } }); } @@ -149,6 +161,8 @@ class MessageHandler { this._sendReply(message, this._megolmDecrypt(message.sessionKey, message.ciphertext)); } else if (type === "olm_create_account_otks") { this._sendReply(message, this._olmCreateAccountAndOTKs(message.randomValues, message.otkAmount)); + } else if (type === "olm_create_outbound") { + this._sendReply(message, this._olmCreateOutbound(message.accountPickle, message.theirIdentityKey, message.theirOneTimeKey)); } } }