@@ -36129,10 +36129,24 @@ SessionCipher.prototype = {
3612936129 result.set(new Uint8Array(encodedMsg), 1);
3613036130 result.set(new Uint8Array(mac, 0, 8), encodedMsg.byteLength + 1);
3613136131
36132- record.updateSessionState(session);
36133- return this.storage.storeSession(address, record.serialize()).then(function() {
36134- return result;
36135- });
36132+ return this.storage.isTrustedIdentity(
36133+ this.remoteAddress.getName(), util.toArrayBuffer(session.indexInfo.remoteIdentityKey), this.storage.Direction.SENDING
36134+ ).then(function(trusted) {
36135+ if (!trusted) {
36136+ throw new Error('Identity key changed');
36137+ }
36138+ }).then(function() {
36139+ return this.storage.saveIdentity(this.remoteAddress.getName(), session.indexInfo.remoteIdentityKey).then(function(changed) {
36140+ if (changed) {
36141+ return record.removePreviousSessions();
36142+ }
36143+ });
36144+ }.bind(this)).then(function() {
36145+ record.updateSessionState(session);
36146+ return this.storage.storeSession(address, record.serialize()).then(function() {
36147+ return result;
36148+ });
36149+ }.bind(this));
3613636150 }.bind(this));
3613736151 }.bind(this));
3613836152 }.bind(this)).then(function(message) {
@@ -36200,10 +36214,25 @@ SessionCipher.prototype = {
3620036214 record.archiveCurrentState();
3620136215 record.promoteState(result.session);
3620236216 }
36203- record.updateSessionState(result.session);
36204- return this.storage.storeSession(address, record.serialize()).then(function() {
36205- return result.plaintext;
36206- });
36217+
36218+ return this.storage.isTrustedIdentity(
36219+ this.remoteAddress.getName(), util.toArrayBuffer(result.session.indexInfo.remoteIdentityKey), this.storage.Direction.SENDING
36220+ ).then(function(trusted) {
36221+ if (!trusted) {
36222+ throw new Error('Identity key changed');
36223+ }
36224+ }).then(function() {
36225+ return this.storage.saveIdentity(this.remoteAddress.getName(), result.session.indexInfo.remoteIdentityKey).then(function(changed) {
36226+ if (changed) {
36227+ return record.removePreviousSessions();
36228+ }
36229+ });
36230+ }.bind(this)).then(function() {
36231+ record.updateSessionState(result.session);
36232+ return this.storage.storeSession(address, record.serialize()).then(function() {
36233+ return result.plaintext;
36234+ });
36235+ }.bind(this));
3620736236 }.bind(this));
3620836237 }.bind(this));
3620936238 }.bind(this));
@@ -36228,6 +36257,7 @@ SessionCipher.prototype = {
3622836257 );
3622936258 }
3623036259 var builder = new SessionBuilder(this.storage, this.remoteAddress);
36260+ // isTrustedIdentity is called within processV3, no need to call it here
3623136261 return builder.processV3(record, preKeyProto).then(function(preKeyId) {
3623236262 var session = record.getSessionByBaseKey(preKeyProto.baseKey);
3623336263 return this.doDecryptWhisperMessage(
0 commit comments