Skip to content

Commit

Permalink
Update libsignal-protocol 1.1.8
Browse files Browse the repository at this point in the history
// FREEBIE
  • Loading branch information
liliakai committed Feb 9, 2017
1 parent 819ba8e commit f0f6bbb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 40 deletions.
48 changes: 28 additions & 20 deletions js/libtextsecure.js
Expand Up @@ -35639,6 +35639,7 @@ Internal.ChainType = {
Internal.SessionRecord = function() {
'use strict';
var ARCHIVED_STATES_MAX_LENGTH = 40;
var OLD_RATCHETS_MAX_LENGTH = 10;
var SESSION_RECORD_VERSION = 'v1';

var StaticByteBufferProto = new dcodeIO.ByteBuffer().__proto__;
Expand Down Expand Up @@ -35724,7 +35725,7 @@ Internal.SessionRecord = function() {
}

var SessionRecord = function() {
this._sessions = {};
this.sessions = {};
this.version = SESSION_RECORD_VERSION;
};

Expand All @@ -35733,8 +35734,8 @@ Internal.SessionRecord = function() {
if (data.version !== SESSION_RECORD_VERSION) { migrate(data); }

var record = new SessionRecord();
record._sessions = data.sessions;
if (record._sessions === undefined || record._sessions === null || typeof record._sessions !== "object" || Array.isArray(record._sessions)) {
record.sessions = data.sessions;
if (record.sessions === undefined || record.sessions === null || typeof record.sessions !== "object" || Array.isArray(record.sessions)) {
throw new Error("Error deserializing SessionRecord");
}
return record;
Expand All @@ -35743,7 +35744,7 @@ Internal.SessionRecord = function() {
SessionRecord.prototype = {
serialize: function() {
return jsonThing({
sessions : this._sessions,
sessions : this.sessions,
version : this.version
});
},
Expand All @@ -35753,7 +35754,7 @@ Internal.SessionRecord = function() {
},

getSessionByBaseKey: function(baseKey) {
var session = this._sessions[util.toString(baseKey)];
var session = this.sessions[util.toString(baseKey)];
if (session && session.indexInfo.baseKeyType === Internal.BaseKeyType.OURS) {
console.log("Tried to lookup a session using our basekey");
return undefined;
Expand All @@ -35762,7 +35763,7 @@ Internal.SessionRecord = function() {
},
getSessionByRemoteEphemeralKey: function(remoteEphemeralKey) {
this.detectDuplicateOpenSessions();
var sessions = this._sessions;
var sessions = this.sessions;

var searchKey = util.toString(remoteEphemeralKey);

Expand All @@ -35782,7 +35783,7 @@ Internal.SessionRecord = function() {
return undefined;
},
getOpenSession: function() {
var sessions = this._sessions;
var sessions = this.sessions;
if (sessions === undefined) {
return undefined;
}
Expand All @@ -35798,7 +35799,7 @@ Internal.SessionRecord = function() {
},
detectDuplicateOpenSessions: function() {
var openSession;
var sessions = this._sessions;
var sessions = this.sessions;
for (var key in sessions) {
if (sessions[key].indexInfo.closed == -1) {
if (openSession !== undefined) {
Expand All @@ -35809,7 +35810,7 @@ Internal.SessionRecord = function() {
}
},
updateSessionState: function(session) {
var sessions = this._sessions;
var sessions = this.sessions;

this.removeOldChains(session);

Expand All @@ -35823,11 +35824,11 @@ Internal.SessionRecord = function() {
// followed by the open session
var list = [];
var openSession;
for (var k in this._sessions) {
if (this._sessions[k].indexInfo.closed === -1) {
openSession = this._sessions[k];
for (var k in this.sessions) {
if (this.sessions[k].indexInfo.closed === -1) {
openSession = this.sessions[k];
} else {
list.push(this._sessions[k]);
list.push(this.sessions[k]);
}
}
list = list.sort(function(s1, s2) {
Expand Down Expand Up @@ -35871,8 +35872,8 @@ Internal.SessionRecord = function() {
removeOldChains: function(session) {
// Sending ratchets are always removed when we step because we never need them again
// Receiving ratchets are added to the oldRatchetList, which we parse
// here and remove all but the last five.
while (session.oldRatchetList.length > 5) {
// here and remove all but the last ten.
while (session.oldRatchetList.length > OLD_RATCHETS_MAX_LENGTH) {
var index = 0;
var oldest = session.oldRatchetList[0];
for (var i = 0; i < session.oldRatchetList.length; i++) {
Expand All @@ -35888,7 +35889,7 @@ Internal.SessionRecord = function() {
},
removeOldSessions: function() {
// Retain only the last 20 sessions
var sessions = this._sessions;
var sessions = this.sessions;
var oldestBaseKey, oldestSession;
while (Object.keys(sessions).length > ARCHIVED_STATES_MAX_LENGTH) {
for (var key in sessions) {
Expand Down Expand Up @@ -35968,15 +35969,20 @@ SessionBuilder.prototype = {
}).then(function() {
return Internal.crypto.createKeyPair();
}).then(function(baseKey) {
var devicePreKey = (device.preKey.publicKey);
var devicePreKey;
if (device.preKey) {
devicePreKey = device.preKey.publicKey;
}
return this.initSession(true, baseKey, undefined, device.identityKey,
devicePreKey, device.signedPreKey.publicKey, device.registrationId
).then(function(session) {
session.pendingPreKey = {
preKeyId : device.preKey.keyId,
signedKeyId : device.signedPreKey.keyId,
baseKey : baseKey.pubKey
};
if (device.preKey) {
session.pendingPreKey.preKeyId = device.preKey.keyId;
}
return session;
});
}.bind(this)).then(function(session) {
Expand Down Expand Up @@ -36259,7 +36265,9 @@ SessionCipher.prototype = {
preKeyMsg.registrationId = myRegistrationId;

preKeyMsg.baseKey = util.toArrayBuffer(session.pendingPreKey.baseKey);
preKeyMsg.preKeyId = session.pendingPreKey.preKeyId;
if (session.pendingPreKey.preKeyId) {
preKeyMsg.preKeyId = session.pendingPreKey.preKeyId;
}
preKeyMsg.signedPreKeyId = session.pendingPreKey.signedKeyId;

preKeyMsg.message = message;
Expand Down Expand Up @@ -36342,7 +36350,7 @@ SessionCipher.prototype = {
).then(function(plaintext) {
record.updateSessionState(session);
return this.storage.storeSession(address, record.serialize()).then(function() {
if (preKeyId !== undefined) {
if (preKeyId !== undefined && preKeyId !== null) {
return this.storage.removePreKey(preKeyId);
}
}.bind(this)).then(function() {
Expand Down
48 changes: 28 additions & 20 deletions libtextsecure/libsignal-protocol.js
Expand Up @@ -35515,6 +35515,7 @@ Internal.ChainType = {
Internal.SessionRecord = function() {
'use strict';
var ARCHIVED_STATES_MAX_LENGTH = 40;
var OLD_RATCHETS_MAX_LENGTH = 10;
var SESSION_RECORD_VERSION = 'v1';

var StaticByteBufferProto = new dcodeIO.ByteBuffer().__proto__;
Expand Down Expand Up @@ -35600,7 +35601,7 @@ Internal.SessionRecord = function() {
}

var SessionRecord = function() {
this._sessions = {};
this.sessions = {};
this.version = SESSION_RECORD_VERSION;
};

Expand All @@ -35609,8 +35610,8 @@ Internal.SessionRecord = function() {
if (data.version !== SESSION_RECORD_VERSION) { migrate(data); }

var record = new SessionRecord();
record._sessions = data.sessions;
if (record._sessions === undefined || record._sessions === null || typeof record._sessions !== "object" || Array.isArray(record._sessions)) {
record.sessions = data.sessions;
if (record.sessions === undefined || record.sessions === null || typeof record.sessions !== "object" || Array.isArray(record.sessions)) {
throw new Error("Error deserializing SessionRecord");
}
return record;
Expand All @@ -35619,7 +35620,7 @@ Internal.SessionRecord = function() {
SessionRecord.prototype = {
serialize: function() {
return jsonThing({
sessions : this._sessions,
sessions : this.sessions,
version : this.version
});
},
Expand All @@ -35629,7 +35630,7 @@ Internal.SessionRecord = function() {
},

getSessionByBaseKey: function(baseKey) {
var session = this._sessions[util.toString(baseKey)];
var session = this.sessions[util.toString(baseKey)];
if (session && session.indexInfo.baseKeyType === Internal.BaseKeyType.OURS) {
console.log("Tried to lookup a session using our basekey");
return undefined;
Expand All @@ -35638,7 +35639,7 @@ Internal.SessionRecord = function() {
},
getSessionByRemoteEphemeralKey: function(remoteEphemeralKey) {
this.detectDuplicateOpenSessions();
var sessions = this._sessions;
var sessions = this.sessions;

var searchKey = util.toString(remoteEphemeralKey);

Expand All @@ -35658,7 +35659,7 @@ Internal.SessionRecord = function() {
return undefined;
},
getOpenSession: function() {
var sessions = this._sessions;
var sessions = this.sessions;
if (sessions === undefined) {
return undefined;
}
Expand All @@ -35674,7 +35675,7 @@ Internal.SessionRecord = function() {
},
detectDuplicateOpenSessions: function() {
var openSession;
var sessions = this._sessions;
var sessions = this.sessions;
for (var key in sessions) {
if (sessions[key].indexInfo.closed == -1) {
if (openSession !== undefined) {
Expand All @@ -35685,7 +35686,7 @@ Internal.SessionRecord = function() {
}
},
updateSessionState: function(session) {
var sessions = this._sessions;
var sessions = this.sessions;

this.removeOldChains(session);

Expand All @@ -35699,11 +35700,11 @@ Internal.SessionRecord = function() {
// followed by the open session
var list = [];
var openSession;
for (var k in this._sessions) {
if (this._sessions[k].indexInfo.closed === -1) {
openSession = this._sessions[k];
for (var k in this.sessions) {
if (this.sessions[k].indexInfo.closed === -1) {
openSession = this.sessions[k];
} else {
list.push(this._sessions[k]);
list.push(this.sessions[k]);
}
}
list = list.sort(function(s1, s2) {
Expand Down Expand Up @@ -35747,8 +35748,8 @@ Internal.SessionRecord = function() {
removeOldChains: function(session) {
// Sending ratchets are always removed when we step because we never need them again
// Receiving ratchets are added to the oldRatchetList, which we parse
// here and remove all but the last five.
while (session.oldRatchetList.length > 5) {
// here and remove all but the last ten.
while (session.oldRatchetList.length > OLD_RATCHETS_MAX_LENGTH) {
var index = 0;
var oldest = session.oldRatchetList[0];
for (var i = 0; i < session.oldRatchetList.length; i++) {
Expand All @@ -35764,7 +35765,7 @@ Internal.SessionRecord = function() {
},
removeOldSessions: function() {
// Retain only the last 20 sessions
var sessions = this._sessions;
var sessions = this.sessions;
var oldestBaseKey, oldestSession;
while (Object.keys(sessions).length > ARCHIVED_STATES_MAX_LENGTH) {
for (var key in sessions) {
Expand Down Expand Up @@ -35844,15 +35845,20 @@ SessionBuilder.prototype = {
}).then(function() {
return Internal.crypto.createKeyPair();
}).then(function(baseKey) {
var devicePreKey = (device.preKey.publicKey);
var devicePreKey;
if (device.preKey) {
devicePreKey = device.preKey.publicKey;
}
return this.initSession(true, baseKey, undefined, device.identityKey,
devicePreKey, device.signedPreKey.publicKey, device.registrationId
).then(function(session) {
session.pendingPreKey = {
preKeyId : device.preKey.keyId,
signedKeyId : device.signedPreKey.keyId,
baseKey : baseKey.pubKey
};
if (device.preKey) {
session.pendingPreKey.preKeyId = device.preKey.keyId;
}
return session;
});
}.bind(this)).then(function(session) {
Expand Down Expand Up @@ -36135,7 +36141,9 @@ SessionCipher.prototype = {
preKeyMsg.registrationId = myRegistrationId;

preKeyMsg.baseKey = util.toArrayBuffer(session.pendingPreKey.baseKey);
preKeyMsg.preKeyId = session.pendingPreKey.preKeyId;
if (session.pendingPreKey.preKeyId) {
preKeyMsg.preKeyId = session.pendingPreKey.preKeyId;
}
preKeyMsg.signedPreKeyId = session.pendingPreKey.signedKeyId;

preKeyMsg.message = message;
Expand Down Expand Up @@ -36218,7 +36226,7 @@ SessionCipher.prototype = {
).then(function(plaintext) {
record.updateSessionState(session);
return this.storage.storeSession(address, record.serialize()).then(function() {
if (preKeyId !== undefined) {
if (preKeyId !== undefined && preKeyId !== null) {
return this.storage.removePreKey(preKeyId);
}
}.bind(this)).then(function() {
Expand Down

0 comments on commit f0f6bbb

Please sign in to comment.