Skip to content

Commit

Permalink
Set messageKeysLimit to unlimited if communicating with our devices (#…
Browse files Browse the repository at this point in the history
…1348)

Set messageKeysLimit to unlimited if communicating with our devices

FREEBIE
  • Loading branch information
scottnonnenberg authored Aug 4, 2017
1 parent 7e0bd82 commit e223db5
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 14 deletions.
51 changes: 44 additions & 7 deletions js/libtextsecure.js
Original file line number Diff line number Diff line change
Expand Up @@ -36169,7 +36169,14 @@ libsignal.SessionBuilder = function (storage, remoteAddress) {
this.processV3 = builder.processV3.bind(builder);
};

function SessionCipher(storage, remoteAddress) {
function SessionCipher(storage, remoteAddress, options) {
options = options || {};

if (typeof options.messageKeysLimit === 'undefined') {
options.messageKeysLimit = 1000;
}

this.messageKeysLimit = options.messageKeysLimit;
this.remoteAddress = remoteAddress;
this.storage = storage;
}
Expand Down Expand Up @@ -36442,7 +36449,7 @@ SessionCipher.prototype = {
});
},
fillMessageKeys: function(chain, counter) {
if (Object.keys(chain.messageKeys).length >= 1000) {
if (this.messageKeysLimit && Object.keys(chain.messageKeys).length >= this.messageKeysLimit) {
console.log("Too many message keys for chain");
return Promise.resolve(); // Stalker, much?
}
Expand Down Expand Up @@ -36565,8 +36572,8 @@ SessionCipher.prototype = {
}
};

libsignal.SessionCipher = function(storage, remoteAddress) {
var cipher = new SessionCipher(storage, remoteAddress);
libsignal.SessionCipher = function(storage, remoteAddress, options) {
var cipher = new SessionCipher(storage, remoteAddress, options);

// returns a Promise that resolves to a ciphertext object
this.encrypt = cipher.encrypt.bind(cipher);
Expand Down Expand Up @@ -38567,7 +38574,17 @@ MessageReceiver.prototype.extend({
decrypt: function(envelope, ciphertext) {
var promise;
var address = new libsignal.SignalProtocolAddress(envelope.source, envelope.sourceDevice);
var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address);

var ourNumber = textsecure.storage.user.getNumber();
var number = address.toString().split('.')[0];
var options = {};

// No limit on message keys if we're communicating with our other devices
if (ourNumber === number) {
options.messageKeysLimit = false;
}

var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address, options);
switch(envelope.type) {
case textsecure.protobuf.Envelope.Type.CIPHERTEXT:
console.log('message from', this.getEnvelopeId(envelope));
Expand Down Expand Up @@ -38877,7 +38894,17 @@ MessageReceiver.prototype.extend({
},
tryMessageAgain: function(from, ciphertext) {
var address = libsignal.SignalProtocolAddress.fromString(from);
var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address);

var ourNumber = textsecure.storage.user.getNumber();
var number = address.toString().split('.')[0];
var options = {};

// No limit on message keys if we're communicating with our other devices
if (ourNumber === number) {
options.messageKeysLimit = false;
}

var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address, options);
console.log('retrying prekey whisper message');
return this.decryptPreKeyWhisperMessage(ciphertext, sessionCipher, address).then(function(plaintext) {
var finalMessage = textsecure.protobuf.DataMessage.decode(plaintext);
Expand Down Expand Up @@ -39153,7 +39180,17 @@ OutgoingMessage.prototype = {

return Promise.all(deviceIds.map(function(deviceId) {
var address = new libsignal.SignalProtocolAddress(number, deviceId);
var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address);

var ourNumber = textsecure.storage.user.getNumber();
var number = address.toString().split('.')[0];
var options = {};

// No limit on message keys if we're communicating with our other devices
if (ourNumber === number) {
options.messageKeysLimit = false;
}

var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address, options);
ciphers[address.getDeviceId()] = sessionCipher;
return sessionCipher.encrypt(plaintext).then(function(ciphertext) {
return {
Expand Down
15 changes: 11 additions & 4 deletions libtextsecure/libsignal-protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -36032,7 +36032,14 @@ libsignal.SessionBuilder = function (storage, remoteAddress) {
this.processV3 = builder.processV3.bind(builder);
};

function SessionCipher(storage, remoteAddress) {
function SessionCipher(storage, remoteAddress, options) {
options = options || {};

if (typeof options.messageKeysLimit === 'undefined') {
options.messageKeysLimit = 1000;
}

this.messageKeysLimit = options.messageKeysLimit;
this.remoteAddress = remoteAddress;
this.storage = storage;
}
Expand Down Expand Up @@ -36305,7 +36312,7 @@ SessionCipher.prototype = {
});
},
fillMessageKeys: function(chain, counter) {
if (Object.keys(chain.messageKeys).length >= 1000) {
if (this.messageKeysLimit && Object.keys(chain.messageKeys).length >= this.messageKeysLimit) {
console.log("Too many message keys for chain");
return Promise.resolve(); // Stalker, much?
}
Expand Down Expand Up @@ -36428,8 +36435,8 @@ SessionCipher.prototype = {
}
};

libsignal.SessionCipher = function(storage, remoteAddress) {
var cipher = new SessionCipher(storage, remoteAddress);
libsignal.SessionCipher = function(storage, remoteAddress, options) {
var cipher = new SessionCipher(storage, remoteAddress, options);

// returns a Promise that resolves to a ciphertext object
this.encrypt = cipher.encrypt.bind(cipher);
Expand Down
24 changes: 22 additions & 2 deletions libtextsecure/message_receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,17 @@ MessageReceiver.prototype.extend({
decrypt: function(envelope, ciphertext) {
var promise;
var address = new libsignal.SignalProtocolAddress(envelope.source, envelope.sourceDevice);
var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address);

var ourNumber = textsecure.storage.user.getNumber();
var number = address.toString().split('.')[0];
var options = {};

// No limit on message keys if we're communicating with our other devices
if (ourNumber === number) {
options.messageKeysLimit = false;
}

var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address, options);
switch(envelope.type) {
case textsecure.protobuf.Envelope.Type.CIPHERTEXT:
console.log('message from', this.getEnvelopeId(envelope));
Expand Down Expand Up @@ -635,7 +645,17 @@ MessageReceiver.prototype.extend({
},
tryMessageAgain: function(from, ciphertext) {
var address = libsignal.SignalProtocolAddress.fromString(from);
var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address);

var ourNumber = textsecure.storage.user.getNumber();
var number = address.toString().split('.')[0];
var options = {};

// No limit on message keys if we're communicating with our other devices
if (ourNumber === number) {
options.messageKeysLimit = false;
}

var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address, options);
console.log('retrying prekey whisper message');
return this.decryptPreKeyWhisperMessage(ciphertext, sessionCipher, address).then(function(plaintext) {
var finalMessage = textsecure.protobuf.DataMessage.decode(plaintext);
Expand Down
12 changes: 11 additions & 1 deletion libtextsecure/outgoing_message.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,17 @@ OutgoingMessage.prototype = {

return Promise.all(deviceIds.map(function(deviceId) {
var address = new libsignal.SignalProtocolAddress(number, deviceId);
var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address);

var ourNumber = textsecure.storage.user.getNumber();
var number = address.toString().split('.')[0];
var options = {};

// No limit on message keys if we're communicating with our other devices
if (ourNumber === number) {
options.messageKeysLimit = false;
}

var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address, options);
ciphers[address.getDeviceId()] = sessionCipher;
return sessionCipher.encrypt(plaintext).then(function(ciphertext) {
return {
Expand Down

0 comments on commit e223db5

Please sign in to comment.