Skip to content
This repository was archived by the owner on Aug 5, 2021. It is now read-only.

Commit b77842c

Browse files
committed
Promote old session states
1 parent b01ac22 commit b77842c

File tree

3 files changed

+18
-46
lines changed

3 files changed

+18
-46
lines changed

dist/libsignal-protocol.js

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35718,32 +35718,14 @@ Internal.SessionRecord = function() {
3571835718
archiveCurrentState: function() {
3571935719
var open_session = this.getOpenSession();
3572035720
if (open_session !== undefined) {
35721-
this.closeSession(open_session);
35721+
console.log('closing session');
35722+
open_session.indexInfo.closed = Date.now();
3572235723
this.updateSessionState(open_session);
3572335724
}
3572435725
},
35725-
closeSession: function(session) {
35726-
if (session.indexInfo.closed > -1) {
35727-
return;
35728-
}
35729-
console.log('closing session', session.indexInfo.baseKey);
35730-
35731-
// After this has run, we can still receive messages on ratchet chains which
35732-
// were already open (unless we know we dont need them),
35733-
// but we cannot send messages or step the ratchet
35734-
35735-
// Delete current sending ratchet
35736-
delete session[util.toString(session.currentRatchet.ephemeralKeyPair.pubKey)];
35737-
// Move all receive ratchets to the oldRatchetList to mark them for deletion
35738-
for (var i in session) {
35739-
if (session[i].chainKey !== undefined && session[i].chainKey.key !== undefined) {
35740-
session.oldRatchetList[session.oldRatchetList.length] = {
35741-
added: Date.now(), ephemeralKey: i
35742-
};
35743-
}
35744-
}
35745-
session.indexInfo.closed = Date.now();
35746-
this.removeOldChains(session);
35726+
promoteState: function(session) {
35727+
console.log('promoting session');
35728+
session.indexInfo.closed = -1;
3574735729
},
3574835730
removeOldChains: function(session) {
3574935731
// Sending ratchets are always removed when we step because we never need them again
@@ -36195,6 +36177,10 @@ SessionCipher.prototype = {
3619536177
var errors = [];
3619636178
return this.decryptWithSessionList(buffer, record.getSessions(), errors).then(function(result) {
3619736179
return this.getRecord(address).then(function(record) {
36180+
if (result.session !== record.getOpenSession()) {
36181+
record.archiveCurrentState();
36182+
record.promoteState(result.session);
36183+
}
3619836184
record.updateSessionState(result.session);
3619936185
return this.storage.storeSession(address, record.serialize()).then(function() {
3620036186
return result.plaintext;

src/SessionCipher.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ SessionCipher.prototype = {
143143
var errors = [];
144144
return this.decryptWithSessionList(buffer, record.getSessions(), errors).then(function(result) {
145145
return this.getRecord(address).then(function(record) {
146+
if (result.session !== record.getOpenSession()) {
147+
record.archiveCurrentState();
148+
record.promoteState(result.session);
149+
}
146150
record.updateSessionState(result.session);
147151
return this.storage.storeSession(address, record.serialize()).then(function() {
148152
return result.plaintext;

src/SessionRecord.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -219,32 +219,14 @@ Internal.SessionRecord = function() {
219219
archiveCurrentState: function() {
220220
var open_session = this.getOpenSession();
221221
if (open_session !== undefined) {
222-
this.closeSession(open_session);
222+
console.log('closing session');
223+
open_session.indexInfo.closed = Date.now();
223224
this.updateSessionState(open_session);
224225
}
225226
},
226-
closeSession: function(session) {
227-
if (session.indexInfo.closed > -1) {
228-
return;
229-
}
230-
console.log('closing session', session.indexInfo.baseKey);
231-
232-
// After this has run, we can still receive messages on ratchet chains which
233-
// were already open (unless we know we dont need them),
234-
// but we cannot send messages or step the ratchet
235-
236-
// Delete current sending ratchet
237-
delete session[util.toString(session.currentRatchet.ephemeralKeyPair.pubKey)];
238-
// Move all receive ratchets to the oldRatchetList to mark them for deletion
239-
for (var i in session) {
240-
if (session[i].chainKey !== undefined && session[i].chainKey.key !== undefined) {
241-
session.oldRatchetList[session.oldRatchetList.length] = {
242-
added: Date.now(), ephemeralKey: i
243-
};
244-
}
245-
}
246-
session.indexInfo.closed = Date.now();
247-
this.removeOldChains(session);
227+
promoteState: function(session) {
228+
console.log('promoting session');
229+
session.indexInfo.closed = -1;
248230
},
249231
removeOldChains: function(session) {
250232
// Sending ratchets are always removed when we step because we never need them again

0 commit comments

Comments
 (0)