From 70eed938d958e2368319fdfd428188d14eb25a42 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Fri, 26 Oct 2018 11:37:51 -0700 Subject: [PATCH] Store arrayBuffers in database for remote identity keys --- js/modules/metadata/SecretSessionCipher.js | 9 +-------- libtextsecure/libsignal-protocol.js | 13 ++++++++----- test/metadata/SecretSessionCipher_test.js | 7 ++++--- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/js/modules/metadata/SecretSessionCipher.js b/js/modules/metadata/SecretSessionCipher.js index 36dff56d0..6422a20cb 100644 --- a/js/modules/metadata/SecretSessionCipher.js +++ b/js/modules/metadata/SecretSessionCipher.js @@ -289,18 +289,11 @@ SecretSessionCipher.prototype = { signalProtocolStore, destinationAddress ); - const sessionRecord = await sessionCipher.getRecord( - destinationAddress.toString() - ); - const openSession = sessionRecord.getOpenSession(); - if (!openSession) { - throw new Error('No active session'); - } const message = await sessionCipher.encrypt(paddedPlaintext); const ourIdentity = await signalProtocolStore.getIdentityKeyPair(); const theirIdentity = fromEncodedBinaryToArrayBuffer( - openSession.indexInfo.remoteIdentityKey + await signalProtocolStore.loadIdentityKey(destinationAddress.getName()) ); const ephemeral = await libsignal.Curve.async.generateKeyPair(); diff --git a/libtextsecure/libsignal-protocol.js b/libtextsecure/libsignal-protocol.js index bb5b080a0..b349b11c5 100644 --- a/libtextsecure/libsignal-protocol.js +++ b/libtextsecure/libsignal-protocol.js @@ -35837,7 +35837,7 @@ SessionBuilder.prototype = { record.updateSessionState(session); return Promise.all([ this.storage.storeSession(address, record.serialize()), - this.storage.saveIdentity(this.remoteAddress.toString(), session.indexInfo.remoteIdentityKey) + this.storage.saveIdentity(this.remoteAddress.toString(), device.identityKey) ]); }.bind(this)); }.bind(this)); @@ -36080,9 +36080,12 @@ SessionCipher.prototype = { msg.ciphertext = ciphertext; var encodedMsg = msg.toArrayBuffer(); + var ourIdentityKeyBuffer = util.toArrayBuffer(ourIdentityKey.pubKey); + var theirIdentityKey = util.toArrayBuffer(session.indexInfo.remoteIdentityKey); var macInput = new Uint8Array(encodedMsg.byteLength + 33*2 + 1); - macInput.set(new Uint8Array(util.toArrayBuffer(ourIdentityKey.pubKey))); - macInput.set(new Uint8Array(util.toArrayBuffer(session.indexInfo.remoteIdentityKey)), 33); + + macInput.set(new Uint8Array(ourIdentityKeyBuffer)); + macInput.set(new Uint8Array(theirIdentityKey), 33); macInput[33*2] = (3 << 4) | 3; macInput.set(new Uint8Array(encodedMsg), 33*2 + 1); @@ -36093,13 +36096,13 @@ SessionCipher.prototype = { result.set(new Uint8Array(mac, 0, 8), encodedMsg.byteLength + 1); return this.storage.isTrustedIdentity( - this.remoteAddress.getName(), util.toArrayBuffer(session.indexInfo.remoteIdentityKey), this.storage.Direction.SENDING + this.remoteAddress.getName(), theirIdentityKey, this.storage.Direction.SENDING ).then(function(trusted) { if (!trusted) { throw new Error('Identity key changed'); } }).then(function() { - return this.storage.saveIdentity(this.remoteAddress.toString(), session.indexInfo.remoteIdentityKey); + return this.storage.saveIdentity(this.remoteAddress.toString(), theirIdentityKey); }.bind(this)).then(function() { record.updateSessionState(session); return this.storage.storeSession(address, record.serialize()).then(function() { diff --git a/test/metadata/SecretSessionCipher_test.js b/test/metadata/SecretSessionCipher_test.js index e1aff1227..c60805abd 100644 --- a/test/metadata/SecretSessionCipher_test.js +++ b/test/metadata/SecretSessionCipher_test.js @@ -76,14 +76,15 @@ InMemorySignalProtocolStore.prototype = { return Promise.resolve(toString(identityKey) === toString(trusted)); }, loadIdentityKey(identifier) { - if (identifier === null || identifier === undefined) + if (identifier === null || identifier === undefined) { throw new Error('Tried to get identity key for undefined/null key'); + } return Promise.resolve(this.get(`identityKey${identifier}`)); }, saveIdentity(identifier, identityKey) { - if (identifier === null || identifier === undefined) + if (identifier === null || identifier === undefined) { throw new Error('Tried to put identity key for undefined/null key'); - + } const address = libsignal.SignalProtocolAddress.fromString(identifier); const existing = this.get(`identityKey${address.getName()}`);