2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2015-04-01 20:08:09 +00:00
|
|
|
*/
|
|
|
|
;(function() {
|
|
|
|
'use strict';
|
2017-05-26 22:59:27 +00:00
|
|
|
var TIMESTAMP_THRESHOLD = 5 * 1000; // 5 seconds
|
2017-05-24 22:11:03 +00:00
|
|
|
var Direction = {
|
|
|
|
SENDING: 1,
|
|
|
|
RECEIVING: 2,
|
|
|
|
};
|
2015-04-01 20:08:09 +00:00
|
|
|
|
2017-06-12 19:55:16 +00:00
|
|
|
var VerifiedStatus = {
|
|
|
|
DEFAULT: 0,
|
|
|
|
VERIFIED: 1,
|
|
|
|
UNVERIFIED: 2,
|
|
|
|
};
|
|
|
|
|
2017-06-13 19:38:44 +00:00
|
|
|
function validateVerifiedStatus(status) {
|
|
|
|
if ( status === VerifiedStatus.DEFAULT
|
|
|
|
|| status === VerifiedStatus.VERIFIED
|
|
|
|
|| status === VerifiedStatus.UNVERIFIED) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-26 22:15:23 +00:00
|
|
|
var StaticByteBufferProto = new dcodeIO.ByteBuffer().__proto__;
|
|
|
|
var StaticArrayBufferProto = new ArrayBuffer().__proto__;
|
|
|
|
var StaticUint8ArrayProto = new Uint8Array().__proto__;
|
|
|
|
|
2015-04-01 20:08:09 +00:00
|
|
|
function isStringable(thing) {
|
|
|
|
return (thing === Object(thing) &&
|
|
|
|
(thing.__proto__ == StaticArrayBufferProto ||
|
|
|
|
thing.__proto__ == StaticUint8ArrayProto ||
|
|
|
|
thing.__proto__ == StaticByteBufferProto));
|
|
|
|
}
|
|
|
|
function convertToArrayBuffer(thing) {
|
2016-02-18 01:08:50 +00:00
|
|
|
if (thing === undefined) {
|
2015-04-01 20:08:09 +00:00
|
|
|
return undefined;
|
2016-02-18 01:08:50 +00:00
|
|
|
}
|
2015-04-01 20:08:09 +00:00
|
|
|
if (thing === Object(thing)) {
|
2016-02-18 01:08:50 +00:00
|
|
|
if (thing.__proto__ == StaticArrayBufferProto) {
|
2015-04-01 20:08:09 +00:00
|
|
|
return thing;
|
2016-02-18 01:08:50 +00:00
|
|
|
}
|
2015-04-01 20:08:09 +00:00
|
|
|
//TODO: Several more cases here...
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thing instanceof Array) {
|
|
|
|
// Assuming Uint16Array from curve25519
|
|
|
|
var res = new ArrayBuffer(thing.length * 2);
|
|
|
|
var uint = new Uint16Array(res);
|
2016-02-18 01:08:50 +00:00
|
|
|
for (var i = 0; i < thing.length; i++) {
|
2015-04-01 20:08:09 +00:00
|
|
|
uint[i] = thing[i];
|
2016-02-18 01:08:50 +00:00
|
|
|
}
|
2015-04-01 20:08:09 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
var str;
|
2016-02-18 01:08:50 +00:00
|
|
|
if (isStringable(thing)) {
|
2015-04-01 20:08:09 +00:00
|
|
|
str = stringObject(thing);
|
2016-02-18 01:08:50 +00:00
|
|
|
} else if (typeof thing == "string") {
|
2015-04-01 20:08:09 +00:00
|
|
|
str = thing;
|
2016-02-18 01:08:50 +00:00
|
|
|
} else {
|
2015-04-01 20:08:09 +00:00
|
|
|
throw new Error("Tried to convert a non-stringable thing of type " + typeof thing + " to an array buffer");
|
2016-02-18 01:08:50 +00:00
|
|
|
}
|
2015-04-01 20:08:09 +00:00
|
|
|
var res = new ArrayBuffer(str.length);
|
|
|
|
var uint = new Uint8Array(res);
|
2016-02-18 01:08:50 +00:00
|
|
|
for (var i = 0; i < str.length; i++) {
|
2015-04-01 20:08:09 +00:00
|
|
|
uint[i] = str.charCodeAt(i);
|
2016-02-18 01:08:50 +00:00
|
|
|
}
|
2015-04-01 20:08:09 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-04-21 22:54:25 +00:00
|
|
|
function equalArrayBuffers(ab1, ab2) {
|
2015-07-15 20:24:50 +00:00
|
|
|
if (!(ab1 instanceof ArrayBuffer && ab2 instanceof ArrayBuffer)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (ab1.byteLength !== ab2.byteLength) {
|
2015-04-21 22:54:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var result = true;
|
|
|
|
var ta1 = new Uint8Array(ab1);
|
|
|
|
var ta2 = new Uint8Array(ab2);
|
2015-07-15 20:24:50 +00:00
|
|
|
for (var i = 0; i < ab1.byteLength; ++i) {
|
2015-04-21 22:54:25 +00:00
|
|
|
if (ta1[i] !== ta2[i]) { result = false; }
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-04-01 20:08:09 +00:00
|
|
|
var Model = Backbone.Model.extend({ database: Whisper.Database });
|
|
|
|
var PreKey = Model.extend({ storeName: 'preKeys' });
|
|
|
|
var SignedPreKey = Model.extend({ storeName: 'signedPreKeys' });
|
2017-02-16 02:16:33 +00:00
|
|
|
var SignedPreKeyCollection = Backbone.Collection.extend({
|
|
|
|
storeName: 'signedPreKeys',
|
|
|
|
database: Whisper.Database,
|
|
|
|
model: SignedPreKey
|
|
|
|
});
|
2015-05-05 03:26:26 +00:00
|
|
|
var Session = Model.extend({ storeName: 'sessions' });
|
|
|
|
var SessionCollection = Backbone.Collection.extend({
|
|
|
|
storeName: 'sessions',
|
|
|
|
database: Whisper.Database,
|
|
|
|
model: Session,
|
|
|
|
fetchSessionsForNumber: function(number) {
|
|
|
|
return this.fetch({range: [number + '.1', number + '.' + ':']});
|
|
|
|
}
|
|
|
|
});
|
2017-06-13 19:38:44 +00:00
|
|
|
var IdentityKey = Model.extend({
|
|
|
|
storeName: 'identityKeys',
|
|
|
|
validAttributes: [
|
|
|
|
'id',
|
|
|
|
'publicKey',
|
|
|
|
'firstUse',
|
|
|
|
'timestamp',
|
|
|
|
'verified',
|
|
|
|
'nonblockingApproval'
|
|
|
|
],
|
|
|
|
validate: function(attrs, options) {
|
|
|
|
var attributeNames = _.keys(attrs);
|
|
|
|
var validAttributes = this.validAttributes;
|
|
|
|
var allValid = _.all(attributeNames, function(attributeName) {
|
|
|
|
return _.contains(validAttributes, attributeName);
|
|
|
|
});
|
|
|
|
if (!allValid) {
|
|
|
|
return new Error("Invalid identity key attribute names");
|
|
|
|
}
|
|
|
|
var allPresent = _.all(validAttributes, function(attributeName) {
|
|
|
|
return _.contains(attributeNames, attributeName);
|
|
|
|
});
|
|
|
|
if (!allPresent) {
|
|
|
|
return new Error("Missing identity key attributes");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof attrs.id !== 'string') {
|
|
|
|
return new Error("Invalid identity key id");
|
|
|
|
}
|
|
|
|
if (!(attrs.publicKey instanceof ArrayBuffer)) {
|
|
|
|
return new Error("Invalid identity key publicKey");
|
|
|
|
}
|
|
|
|
if (typeof attrs.firstUse !== 'boolean') {
|
|
|
|
return new Error("Invalid identity key firstUse");
|
|
|
|
}
|
|
|
|
if (typeof attrs.timestamp !== 'number' || !(attrs.timestamp >= 0)) {
|
|
|
|
return new Error("Invalid identity key timestamp");
|
|
|
|
}
|
|
|
|
if (!validateVerifiedStatus(attrs.verified)) {
|
|
|
|
return new Error("Invalid identity key verified");
|
|
|
|
}
|
|
|
|
if (typeof attrs.nonblockingApproval !== 'boolean') {
|
|
|
|
return new Error("Invalid identity key nonblockingApproval");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2015-05-06 22:09:03 +00:00
|
|
|
var Group = Model.extend({ storeName: 'groups' });
|
2015-05-11 22:40:21 +00:00
|
|
|
var Item = Model.extend({ storeName: 'items' });
|
2015-04-01 20:08:09 +00:00
|
|
|
|
2016-04-21 23:45:21 +00:00
|
|
|
function SignalProtocolStore() {}
|
2015-04-01 20:08:09 +00:00
|
|
|
|
2016-04-21 23:45:21 +00:00
|
|
|
SignalProtocolStore.prototype = {
|
|
|
|
constructor: SignalProtocolStore,
|
2016-04-21 22:40:43 +00:00
|
|
|
getIdentityKeyPair: function() {
|
2015-05-11 22:40:21 +00:00
|
|
|
var item = new Item({id: 'identityKey'});
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
item.fetch().then(function() {
|
|
|
|
resolve(item.get('value'));
|
|
|
|
});
|
|
|
|
});
|
2015-04-01 20:08:09 +00:00
|
|
|
},
|
2016-04-21 22:40:43 +00:00
|
|
|
getLocalRegistrationId: function() {
|
2015-05-11 22:40:21 +00:00
|
|
|
var item = new Item({id: 'registrationId'});
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
item.fetch().then(function() {
|
|
|
|
resolve(item.get('value'));
|
|
|
|
});
|
|
|
|
});
|
2015-04-01 20:08:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/* Returns a prekeypair object or undefined */
|
2016-04-21 22:40:43 +00:00
|
|
|
loadPreKey: function(keyId) {
|
2015-04-01 20:08:09 +00:00
|
|
|
var prekey = new PreKey({id: keyId});
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
prekey.fetch().then(function() {
|
|
|
|
resolve({
|
|
|
|
pubKey: prekey.attributes.publicKey,
|
|
|
|
privKey: prekey.attributes.privateKey
|
|
|
|
});
|
|
|
|
}).fail(resolve);
|
|
|
|
});
|
|
|
|
},
|
2016-04-21 22:40:43 +00:00
|
|
|
storePreKey: function(keyId, keyPair) {
|
2015-04-01 20:08:09 +00:00
|
|
|
var prekey = new PreKey({
|
|
|
|
id : keyId,
|
|
|
|
publicKey : keyPair.pubKey,
|
|
|
|
privateKey : keyPair.privKey
|
|
|
|
});
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
prekey.save().always(function() {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
removePreKey: function(keyId) {
|
|
|
|
var prekey = new PreKey({id: keyId});
|
2015-05-05 20:29:42 +00:00
|
|
|
|
2017-05-22 22:24:24 +00:00
|
|
|
this.trigger('removePreKey');
|
|
|
|
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
prekey.destroy().then(function() {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
2015-04-01 20:08:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/* Returns a signed keypair object or undefined */
|
2016-04-21 22:40:43 +00:00
|
|
|
loadSignedPreKey: function(keyId) {
|
2015-04-01 20:08:09 +00:00
|
|
|
var prekey = new SignedPreKey({id: keyId});
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
prekey.fetch().then(function() {
|
|
|
|
resolve({
|
2017-02-15 21:41:31 +00:00
|
|
|
pubKey : prekey.get('publicKey'),
|
|
|
|
privKey : prekey.get('privateKey'),
|
|
|
|
created_at : prekey.get('created_at'),
|
|
|
|
keyId : prekey.get('id')
|
2015-04-01 20:08:09 +00:00
|
|
|
});
|
2017-04-12 19:01:00 +00:00
|
|
|
}).fail(function() {
|
|
|
|
console.log("Failed to load signed prekey:", keyId);
|
|
|
|
resolve();
|
|
|
|
});
|
2015-04-01 20:08:09 +00:00
|
|
|
});
|
|
|
|
},
|
2017-02-16 02:16:33 +00:00
|
|
|
loadSignedPreKeys: function() {
|
|
|
|
if (arguments.length > 0) {
|
|
|
|
return Promise.reject(new Error("loadSignedPreKeys takes no arguments"));
|
|
|
|
}
|
|
|
|
var signedPreKeys = new SignedPreKeyCollection();
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
signedPreKeys.fetch().then(function() {
|
|
|
|
resolve(signedPreKeys.map(function(prekey) {
|
|
|
|
return {
|
|
|
|
pubKey : prekey.get('publicKey'),
|
|
|
|
privKey : prekey.get('privateKey'),
|
|
|
|
created_at : prekey.get('created_at'),
|
|
|
|
keyId : prekey.get('id')
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2016-04-21 22:40:43 +00:00
|
|
|
storeSignedPreKey: function(keyId, keyPair) {
|
2015-04-01 20:08:09 +00:00
|
|
|
var prekey = new SignedPreKey({
|
|
|
|
id : keyId,
|
|
|
|
publicKey : keyPair.pubKey,
|
2017-02-14 23:26:34 +00:00
|
|
|
privateKey : keyPair.privKey,
|
|
|
|
created_at : Date.now()
|
2015-04-01 20:08:09 +00:00
|
|
|
});
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
prekey.save().always(function() {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
removeSignedPreKey: function(keyId) {
|
|
|
|
var prekey = new SignedPreKey({id: keyId});
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
prekey.destroy().then(function() {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-04-21 22:40:43 +00:00
|
|
|
loadSession: function(encodedNumber) {
|
2016-02-18 01:08:50 +00:00
|
|
|
if (encodedNumber === null || encodedNumber === undefined) {
|
2015-10-29 23:46:08 +00:00
|
|
|
throw new Error("Tried to get session for undefined/null number");
|
2016-02-18 01:08:50 +00:00
|
|
|
}
|
2015-04-21 21:11:29 +00:00
|
|
|
return new Promise(function(resolve) {
|
2015-05-05 03:26:26 +00:00
|
|
|
var session = new Session({id: encodedNumber});
|
|
|
|
session.fetch().always(function() {
|
|
|
|
resolve(session.get('record'));
|
2015-04-21 21:11:29 +00:00
|
|
|
});
|
2015-04-21 20:30:22 +00:00
|
|
|
|
2015-04-21 21:11:29 +00:00
|
|
|
});
|
2015-04-01 20:08:09 +00:00
|
|
|
},
|
2016-04-21 22:40:43 +00:00
|
|
|
storeSession: function(encodedNumber, record) {
|
2016-02-18 01:08:50 +00:00
|
|
|
if (encodedNumber === null || encodedNumber === undefined) {
|
2015-10-29 23:46:08 +00:00
|
|
|
throw new Error("Tried to put session for undefined/null number");
|
2016-02-18 01:08:50 +00:00
|
|
|
}
|
2015-04-21 21:11:29 +00:00
|
|
|
return new Promise(function(resolve) {
|
|
|
|
var number = textsecure.utils.unencodeNumber(encodedNumber)[0];
|
2015-05-05 03:26:26 +00:00
|
|
|
var deviceId = parseInt(textsecure.utils.unencodeNumber(encodedNumber)[1]);
|
2015-04-21 20:30:22 +00:00
|
|
|
|
2015-05-05 03:26:26 +00:00
|
|
|
var session = new Session({id: encodedNumber});
|
|
|
|
session.fetch().always(function() {
|
|
|
|
session.save({
|
|
|
|
record: record,
|
|
|
|
deviceId: deviceId,
|
|
|
|
number: number
|
2016-06-27 20:33:04 +00:00
|
|
|
}).fail(function(e) {
|
|
|
|
console.log('Failed to save session', encodedNumber, e);
|
2016-02-17 18:46:29 +00:00
|
|
|
}).always(function() {
|
|
|
|
resolve();
|
|
|
|
});
|
2015-04-21 21:11:29 +00:00
|
|
|
});
|
2015-04-21 20:30:22 +00:00
|
|
|
});
|
|
|
|
},
|
2015-04-22 02:13:13 +00:00
|
|
|
getDeviceIds: function(number) {
|
2016-02-18 01:08:50 +00:00
|
|
|
if (number === null || number === undefined) {
|
2015-10-29 23:46:08 +00:00
|
|
|
throw new Error("Tried to get device ids for undefined/null number");
|
2016-02-18 01:08:50 +00:00
|
|
|
}
|
2015-04-22 02:13:13 +00:00
|
|
|
return new Promise(function(resolve) {
|
2015-05-05 03:26:26 +00:00
|
|
|
var sessions = new SessionCollection();
|
|
|
|
sessions.fetchSessionsForNumber(number).always(function() {
|
|
|
|
resolve(sessions.pluck('deviceId'));
|
2015-04-22 02:13:13 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2015-04-22 02:30:52 +00:00
|
|
|
removeSession: function(encodedNumber) {
|
2017-02-27 20:50:21 +00:00
|
|
|
console.log('deleting session for ', encodedNumber);
|
2015-04-22 02:30:52 +00:00
|
|
|
return new Promise(function(resolve) {
|
2015-05-05 03:26:26 +00:00
|
|
|
var session = new Session({id: encodedNumber});
|
|
|
|
session.fetch().then(function() {
|
|
|
|
session.destroy().then(resolve);
|
2017-02-27 20:24:17 +00:00
|
|
|
}).fail(resolve);
|
2015-04-22 02:30:52 +00:00
|
|
|
});
|
|
|
|
},
|
2015-04-21 20:30:22 +00:00
|
|
|
removeAllSessions: function(number) {
|
2016-02-18 01:08:50 +00:00
|
|
|
if (number === null || number === undefined) {
|
2015-10-29 23:46:08 +00:00
|
|
|
throw new Error("Tried to remove sessions for undefined/null number");
|
2016-02-18 01:08:50 +00:00
|
|
|
}
|
2015-04-21 21:11:29 +00:00
|
|
|
return new Promise(function(resolve) {
|
2015-05-05 03:26:26 +00:00
|
|
|
var sessions = new SessionCollection();
|
|
|
|
sessions.fetchSessionsForNumber(number).always(function() {
|
|
|
|
var promises = [];
|
2016-02-18 01:08:50 +00:00
|
|
|
while (sessions.length > 0) {
|
2015-05-05 03:26:26 +00:00
|
|
|
promises.push(new Promise(function(res) {
|
|
|
|
sessions.pop().destroy().then(res);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
Promise.all(promises).then(resolve);
|
2015-04-21 21:11:29 +00:00
|
|
|
});
|
|
|
|
});
|
2015-04-21 20:30:22 +00:00
|
|
|
},
|
2015-11-02 21:27:36 +00:00
|
|
|
clearSessionStore: function() {
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
var sessions = new SessionCollection();
|
|
|
|
sessions.sync('delete', sessions, {}).always(resolve);
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
2017-05-24 22:11:03 +00:00
|
|
|
isTrustedIdentity: function(identifier, publicKey, direction) {
|
2016-05-04 06:58:57 +00:00
|
|
|
if (identifier === null || identifier === undefined) {
|
|
|
|
throw new Error("Tried to get identity key for undefined/null key");
|
|
|
|
}
|
|
|
|
var number = textsecure.utils.unencodeNumber(identifier)[0];
|
2017-05-24 22:11:03 +00:00
|
|
|
var isOurNumber = number === textsecure.storage.user.getNumber();
|
|
|
|
var identityKey = new IdentityKey({id: number});
|
2016-05-04 06:58:57 +00:00
|
|
|
return new Promise(function(resolve) {
|
2017-05-24 22:11:03 +00:00
|
|
|
identityKey.fetch().always(resolve);
|
|
|
|
}).then(function() {
|
|
|
|
var existing = identityKey.get('publicKey');
|
|
|
|
|
|
|
|
if (isOurNumber) {
|
|
|
|
return equalArrayBuffers(existing, publicKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(direction) {
|
|
|
|
case Direction.SENDING: return this.isTrustedForSending(publicKey, identityKey);
|
|
|
|
case Direction.RECEIVING: return true;
|
|
|
|
default: throw new Error("Unknown direction: " + direction);
|
|
|
|
}
|
2016-09-15 19:11:42 +00:00
|
|
|
}.bind(this));
|
2016-05-04 06:58:57 +00:00
|
|
|
},
|
2017-05-24 22:11:03 +00:00
|
|
|
isTrustedForSending: function(publicKey, identityKey) {
|
|
|
|
var existing = identityKey.get('publicKey');
|
|
|
|
|
|
|
|
if (!existing) {
|
|
|
|
console.log("isTrustedForSending: Nothing here, returning true...");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!equalArrayBuffers(existing, publicKey)) {
|
|
|
|
console.log("isTrustedForSending: Identity keys don't match...");
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-12 19:55:16 +00:00
|
|
|
if (identityKey.get('verified') === VerifiedStatus.UNVERIFIED) {
|
|
|
|
console.log("Needs unverified approval!");
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-24 22:11:03 +00:00
|
|
|
if (this.isNonBlockingApprovalRequired(identityKey)) {
|
|
|
|
console.log("isTrustedForSending: Needs non-blocking approval!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
2016-04-21 22:40:43 +00:00
|
|
|
loadIdentityKey: function(identifier) {
|
2016-02-18 01:08:50 +00:00
|
|
|
if (identifier === null || identifier === undefined) {
|
2015-04-21 20:33:29 +00:00
|
|
|
throw new Error("Tried to get identity key for undefined/null key");
|
2016-02-18 01:08:50 +00:00
|
|
|
}
|
2015-04-21 20:33:29 +00:00
|
|
|
var number = textsecure.utils.unencodeNumber(identifier)[0];
|
2015-04-21 22:54:25 +00:00
|
|
|
return new Promise(function(resolve) {
|
2015-05-05 03:26:26 +00:00
|
|
|
var identityKey = new IdentityKey({id: number});
|
|
|
|
identityKey.fetch().always(function() {
|
|
|
|
resolve(identityKey.get('publicKey'));
|
2015-04-21 22:54:25 +00:00
|
|
|
});
|
|
|
|
});
|
2015-04-21 20:33:29 +00:00
|
|
|
},
|
2017-06-12 19:18:14 +00:00
|
|
|
saveIdentity: function(identifier, publicKey, nonblockingApproval) {
|
2016-02-18 01:08:50 +00:00
|
|
|
if (identifier === null || identifier === undefined) {
|
2015-04-21 20:33:29 +00:00
|
|
|
throw new Error("Tried to put identity key for undefined/null key");
|
2016-02-18 01:08:50 +00:00
|
|
|
}
|
2015-07-16 19:10:11 +00:00
|
|
|
if (!(publicKey instanceof ArrayBuffer)) {
|
|
|
|
publicKey = convertToArrayBuffer(publicKey);
|
|
|
|
}
|
2015-04-21 20:33:29 +00:00
|
|
|
var number = textsecure.utils.unencodeNumber(identifier)[0];
|
2015-07-22 19:48:08 +00:00
|
|
|
return new Promise(function(resolve, reject) {
|
2015-05-05 03:26:26 +00:00
|
|
|
var identityKey = new IdentityKey({id: number});
|
|
|
|
identityKey.fetch().always(function() {
|
|
|
|
var oldpublicKey = identityKey.get('publicKey');
|
2015-07-09 19:47:16 +00:00
|
|
|
if (!oldpublicKey) {
|
|
|
|
// Lookup failed, or the current key was removed, so save this one.
|
2017-05-24 20:54:14 +00:00
|
|
|
console.log("Saving new identity...");
|
|
|
|
identityKey.save({
|
|
|
|
publicKey : publicKey,
|
|
|
|
firstUse : true,
|
|
|
|
timestamp : Date.now(),
|
2017-06-12 19:55:16 +00:00
|
|
|
verified : VerifiedStatus.DEFAULT,
|
2017-05-24 20:54:14 +00:00
|
|
|
nonblockingApproval : nonblockingApproval,
|
|
|
|
}).then(function() {
|
|
|
|
resolve(false);
|
|
|
|
});
|
|
|
|
} else if (!equalArrayBuffers(oldpublicKey, publicKey)) {
|
|
|
|
console.log("Replacing existing identity...");
|
2017-06-12 19:55:16 +00:00
|
|
|
var verifiedStatus;
|
|
|
|
if (identityKey.get('verified') === VerifiedStatus.VERIFIED) {
|
|
|
|
verifiedStatus = VerifiedStatus.UNVERIFIED;
|
|
|
|
} else {
|
|
|
|
verifiedStatus = VerifiedStatus.DEFAULT;
|
|
|
|
}
|
2017-05-24 20:54:14 +00:00
|
|
|
identityKey.save({
|
|
|
|
publicKey : publicKey,
|
|
|
|
firstUse : false,
|
|
|
|
timestamp : Date.now(),
|
2017-06-12 19:55:16 +00:00
|
|
|
verified : verifiedStatus,
|
2017-05-24 20:54:14 +00:00
|
|
|
nonblockingApproval : nonblockingApproval,
|
|
|
|
}).then(function() {
|
|
|
|
this.trigger('keychange', identifier);
|
|
|
|
resolve(true);
|
|
|
|
}.bind(this));
|
2017-06-12 19:18:14 +00:00
|
|
|
} else if (this.isNonBlockingApprovalRequired(identityKey)) {
|
2017-05-24 20:54:14 +00:00
|
|
|
console.log("Setting approval status...");
|
|
|
|
identityKey.save({
|
|
|
|
nonblockingApproval : nonblockingApproval,
|
|
|
|
}).then(function() {
|
|
|
|
resolve(false);
|
|
|
|
});
|
2015-07-09 19:47:16 +00:00
|
|
|
} else {
|
2017-05-24 20:54:14 +00:00
|
|
|
resolve(false);
|
2015-07-09 19:47:16 +00:00
|
|
|
}
|
2017-05-24 20:54:14 +00:00
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
2015-04-21 20:33:29 +00:00
|
|
|
},
|
2017-05-26 22:59:27 +00:00
|
|
|
isNonBlockingApprovalRequired: function(identityKey) {
|
|
|
|
return (!identityKey.get('firstUse')
|
|
|
|
&& Date.now() - identityKey.get('timestamp') < TIMESTAMP_THRESHOLD
|
|
|
|
&& !identityKey.get('nonblockingApproval'));
|
|
|
|
},
|
2015-04-21 20:33:29 +00:00
|
|
|
removeIdentityKey: function(number) {
|
2016-03-10 02:26:57 +00:00
|
|
|
return new Promise(function(resolve, reject) {
|
2015-05-05 03:26:26 +00:00
|
|
|
var identityKey = new IdentityKey({id: number});
|
|
|
|
identityKey.fetch().then(function() {
|
|
|
|
identityKey.save({publicKey: undefined});
|
2015-04-21 22:54:25 +00:00
|
|
|
}).fail(function() {
|
2016-03-10 02:26:57 +00:00
|
|
|
reject(new Error("Tried to remove identity for unknown number"));
|
2015-04-21 22:54:25 +00:00
|
|
|
});
|
2016-04-22 20:39:05 +00:00
|
|
|
resolve(textsecure.storage.protocol.removeAllSessions(number));
|
2015-04-21 22:54:25 +00:00
|
|
|
});
|
2015-04-21 20:33:29 +00:00
|
|
|
},
|
2015-05-06 21:22:18 +00:00
|
|
|
getGroup: function(groupId) {
|
2016-02-18 01:08:50 +00:00
|
|
|
if (groupId === null || groupId === undefined) {
|
2015-05-06 22:09:03 +00:00
|
|
|
throw new Error("Tried to get group for undefined/null id");
|
2016-02-18 01:08:50 +00:00
|
|
|
}
|
2015-05-06 22:09:03 +00:00
|
|
|
return new Promise(function(resolve) {
|
|
|
|
var group = new Group({id: groupId});
|
|
|
|
group.fetch().always(function() {
|
|
|
|
resolve(group.get('data'));
|
|
|
|
});
|
|
|
|
});
|
2015-05-06 21:22:18 +00:00
|
|
|
},
|
|
|
|
putGroup: function(groupId, group) {
|
2016-02-18 01:08:50 +00:00
|
|
|
if (groupId === null || groupId === undefined) {
|
2015-05-06 22:09:03 +00:00
|
|
|
throw new Error("Tried to put group key for undefined/null id");
|
2016-02-18 01:08:50 +00:00
|
|
|
}
|
|
|
|
if (group === null || group === undefined) {
|
2015-05-06 22:09:03 +00:00
|
|
|
throw new Error("Tried to put undefined/null group object");
|
2016-02-18 01:08:50 +00:00
|
|
|
}
|
2015-05-06 22:09:03 +00:00
|
|
|
var group = new Group({id: groupId, data: group});
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
group.save().always(resolve);
|
|
|
|
});
|
2015-05-06 21:22:18 +00:00
|
|
|
},
|
|
|
|
removeGroup: function(groupId) {
|
2016-02-18 01:08:50 +00:00
|
|
|
if (groupId === null || groupId === undefined) {
|
2015-05-06 22:09:03 +00:00
|
|
|
throw new Error("Tried to remove group key for undefined/null id");
|
2016-02-18 01:08:50 +00:00
|
|
|
}
|
2015-05-06 22:09:03 +00:00
|
|
|
return new Promise(function(resolve) {
|
|
|
|
var group = new Group({id: groupId});
|
|
|
|
group.destroy().always(resolve);
|
|
|
|
});
|
2015-05-06 21:22:18 +00:00
|
|
|
},
|
2015-04-21 20:30:22 +00:00
|
|
|
|
2015-04-01 20:08:09 +00:00
|
|
|
};
|
2016-09-13 23:43:49 +00:00
|
|
|
_.extend(SignalProtocolStore.prototype, Backbone.Events);
|
2015-04-01 20:08:09 +00:00
|
|
|
|
2016-04-21 23:45:21 +00:00
|
|
|
window.SignalProtocolStore = SignalProtocolStore;
|
2017-05-24 22:11:03 +00:00
|
|
|
window.SignalProtocolStore.prototype.Direction = Direction;
|
2017-06-12 19:55:16 +00:00
|
|
|
window.SignalProtocolStore.prototype.VerifiedStatus = VerifiedStatus;
|
2015-04-01 20:08:09 +00:00
|
|
|
})();
|