2015-09-07 23:53:43 +02:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2015-04-01 22:08:09 +02:00
|
|
|
*/
|
|
|
|
;(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function isStringable(thing) {
|
|
|
|
return (thing === Object(thing) &&
|
|
|
|
(thing.__proto__ == StaticArrayBufferProto ||
|
|
|
|
thing.__proto__ == StaticUint8ArrayProto ||
|
|
|
|
thing.__proto__ == StaticByteBufferProto));
|
|
|
|
}
|
|
|
|
function convertToArrayBuffer(thing) {
|
|
|
|
if (thing === undefined)
|
|
|
|
return undefined;
|
|
|
|
if (thing === Object(thing)) {
|
|
|
|
if (thing.__proto__ == StaticArrayBufferProto)
|
|
|
|
return thing;
|
|
|
|
//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);
|
|
|
|
for (var i = 0; i < thing.length; i++)
|
|
|
|
uint[i] = thing[i];
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
var str;
|
|
|
|
if (isStringable(thing))
|
|
|
|
str = stringObject(thing);
|
|
|
|
else if (typeof thing == "string")
|
|
|
|
str = thing;
|
|
|
|
else
|
|
|
|
throw new Error("Tried to convert a non-stringable thing of type " + typeof thing + " to an array buffer");
|
|
|
|
var res = new ArrayBuffer(str.length);
|
|
|
|
var uint = new Uint8Array(res);
|
|
|
|
for (var i = 0; i < str.length; i++)
|
|
|
|
uint[i] = str.charCodeAt(i);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-04-22 00:54:25 +02:00
|
|
|
function equalArrayBuffers(ab1, ab2) {
|
2015-07-15 22:24:50 +02:00
|
|
|
if (!(ab1 instanceof ArrayBuffer && ab2 instanceof ArrayBuffer)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (ab1.byteLength !== ab2.byteLength) {
|
2015-04-22 00:54:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var result = true;
|
|
|
|
var ta1 = new Uint8Array(ab1);
|
|
|
|
var ta2 = new Uint8Array(ab2);
|
2015-07-15 22:24:50 +02:00
|
|
|
for (var i = 0; i < ab1.byteLength; ++i) {
|
2015-04-22 00:54:25 +02:00
|
|
|
if (ta1[i] !== ta2[i]) { result = false; }
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-04-01 22:08:09 +02:00
|
|
|
var Model = Backbone.Model.extend({ database: Whisper.Database });
|
|
|
|
var PreKey = Model.extend({ storeName: 'preKeys' });
|
|
|
|
var SignedPreKey = Model.extend({ storeName: 'signedPreKeys' });
|
2015-05-05 05:26:26 +02: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 + '.' + ':']});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var IdentityKey = Model.extend({ storeName: 'identityKeys' });
|
2015-05-07 00:09:03 +02:00
|
|
|
var Group = Model.extend({ storeName: 'groups' });
|
2015-05-12 00:40:21 +02:00
|
|
|
var Item = Model.extend({ storeName: 'items' });
|
2015-04-01 22:08:09 +02:00
|
|
|
|
|
|
|
function AxolotlStore() {}
|
|
|
|
|
|
|
|
AxolotlStore.prototype = {
|
|
|
|
constructor: AxolotlStore,
|
|
|
|
getMyIdentityKey: function() {
|
2015-05-12 00:40:21 +02:00
|
|
|
var item = new Item({id: 'identityKey'});
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
item.fetch().then(function() {
|
|
|
|
resolve(item.get('value'));
|
|
|
|
});
|
|
|
|
});
|
2015-04-01 22:08:09 +02:00
|
|
|
},
|
|
|
|
getMyRegistrationId: function() {
|
2015-05-12 00:40:21 +02:00
|
|
|
var item = new Item({id: 'registrationId'});
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
item.fetch().then(function() {
|
|
|
|
resolve(item.get('value'));
|
|
|
|
});
|
|
|
|
});
|
2015-04-01 22:08:09 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/* Returns a prekeypair object or undefined */
|
|
|
|
getPreKey: function(keyId) {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
putPreKey: function(keyId, keyPair) {
|
|
|
|
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 22:29:42 +02:00
|
|
|
|
|
|
|
new Promise(function(resolve) {
|
|
|
|
var accountManager = new textsecure.AccountManager();
|
|
|
|
accountManager.refreshPreKeys().then(resolve);
|
|
|
|
});
|
|
|
|
|
2015-04-01 22:08:09 +02:00
|
|
|
return new Promise(function(resolve) {
|
|
|
|
prekey.destroy().then(function() {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/* Returns a signed keypair object or undefined */
|
|
|
|
getSignedPreKey: function(keyId) {
|
|
|
|
var prekey = new SignedPreKey({id: keyId});
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
prekey.fetch().then(function() {
|
|
|
|
resolve({
|
|
|
|
pubKey: prekey.attributes.publicKey,
|
|
|
|
privKey: prekey.attributes.privateKey
|
|
|
|
});
|
|
|
|
}).fail(resolve);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
putSignedPreKey: function(keyId, keyPair) {
|
|
|
|
var prekey = new SignedPreKey({
|
|
|
|
id : keyId,
|
|
|
|
publicKey : keyPair.pubKey,
|
|
|
|
privateKey : keyPair.privKey
|
|
|
|
});
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-04-21 22:30:22 +02:00
|
|
|
getSession: function(encodedNumber) {
|
|
|
|
if (encodedNumber === null || encodedNumber === undefined)
|
2015-04-01 22:08:09 +02:00
|
|
|
throw new Error("Tried to get session for undefined/null key");
|
2015-04-21 23:11:29 +02:00
|
|
|
return new Promise(function(resolve) {
|
2015-05-05 05:26:26 +02:00
|
|
|
var session = new Session({id: encodedNumber});
|
|
|
|
session.fetch().always(function() {
|
|
|
|
resolve(session.get('record'));
|
2015-04-21 23:11:29 +02:00
|
|
|
});
|
2015-04-21 22:30:22 +02:00
|
|
|
|
2015-04-21 23:11:29 +02:00
|
|
|
});
|
2015-04-01 22:08:09 +02:00
|
|
|
},
|
2015-04-21 22:30:22 +02:00
|
|
|
putSession: function(encodedNumber, record) {
|
|
|
|
if (encodedNumber === null || encodedNumber === undefined)
|
2015-04-01 22:08:09 +02:00
|
|
|
throw new Error("Tried to put session for undefined/null key");
|
2015-04-21 23:11:29 +02:00
|
|
|
return new Promise(function(resolve) {
|
|
|
|
var number = textsecure.utils.unencodeNumber(encodedNumber)[0];
|
2015-05-05 05:26:26 +02:00
|
|
|
var deviceId = parseInt(textsecure.utils.unencodeNumber(encodedNumber)[1]);
|
2015-04-21 22:30:22 +02:00
|
|
|
|
2015-05-05 05:26:26 +02:00
|
|
|
var session = new Session({id: encodedNumber});
|
|
|
|
session.fetch().always(function() {
|
|
|
|
session.save({
|
|
|
|
record: record,
|
|
|
|
deviceId: deviceId,
|
|
|
|
number: number
|
|
|
|
}).always(resolve);
|
2015-04-21 23:11:29 +02:00
|
|
|
});
|
2015-04-21 22:30:22 +02:00
|
|
|
});
|
|
|
|
},
|
2015-04-22 04:13:13 +02:00
|
|
|
getDeviceIds: function(number) {
|
|
|
|
if (number === null || number === undefined)
|
|
|
|
throw new Error("Tried to put session for undefined/null key");
|
|
|
|
return new Promise(function(resolve) {
|
2015-05-05 05:26:26 +02:00
|
|
|
var sessions = new SessionCollection();
|
|
|
|
sessions.fetchSessionsForNumber(number).always(function() {
|
|
|
|
resolve(sessions.pluck('deviceId'));
|
2015-04-22 04:13:13 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2015-04-22 04:30:52 +02:00
|
|
|
removeSession: function(encodedNumber) {
|
|
|
|
return new Promise(function(resolve) {
|
2015-05-05 05:26:26 +02:00
|
|
|
var session = new Session({id: encodedNumber});
|
|
|
|
session.fetch().then(function() {
|
|
|
|
session.destroy().then(resolve);
|
2015-04-22 04:30:52 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2015-04-21 22:30:22 +02:00
|
|
|
removeAllSessions: function(number) {
|
|
|
|
if (number === null || number === undefined)
|
|
|
|
throw new Error("Tried to put session for undefined/null key");
|
2015-04-21 23:11:29 +02:00
|
|
|
return new Promise(function(resolve) {
|
2015-05-05 05:26:26 +02:00
|
|
|
var sessions = new SessionCollection();
|
|
|
|
sessions.fetchSessionsForNumber(number).always(function() {
|
|
|
|
var promises = [];
|
|
|
|
while(sessions.length > 0) {
|
|
|
|
promises.push(new Promise(function(res) {
|
|
|
|
sessions.pop().destroy().then(res);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
Promise.all(promises).then(resolve);
|
2015-04-21 23:11:29 +02:00
|
|
|
});
|
|
|
|
});
|
2015-04-21 22:30:22 +02:00
|
|
|
},
|
2015-04-21 22:33:29 +02:00
|
|
|
getIdentityKey: function(identifier) {
|
|
|
|
if (identifier === null || identifier === undefined)
|
|
|
|
throw new Error("Tried to get identity key for undefined/null key");
|
|
|
|
var number = textsecure.utils.unencodeNumber(identifier)[0];
|
2015-04-22 00:54:25 +02:00
|
|
|
return new Promise(function(resolve) {
|
2015-05-05 05:26:26 +02:00
|
|
|
var identityKey = new IdentityKey({id: number});
|
|
|
|
identityKey.fetch().always(function() {
|
|
|
|
resolve(identityKey.get('publicKey'));
|
2015-04-22 00:54:25 +02:00
|
|
|
});
|
|
|
|
});
|
2015-04-21 22:33:29 +02:00
|
|
|
},
|
2015-05-05 05:26:26 +02:00
|
|
|
putIdentityKey: function(identifier, publicKey) {
|
2015-04-21 22:33:29 +02:00
|
|
|
if (identifier === null || identifier === undefined)
|
|
|
|
throw new Error("Tried to put identity key for undefined/null key");
|
2015-07-16 21:10:11 +02:00
|
|
|
if (!(publicKey instanceof ArrayBuffer)) {
|
|
|
|
publicKey = convertToArrayBuffer(publicKey);
|
|
|
|
}
|
2015-04-21 22:33:29 +02:00
|
|
|
var number = textsecure.utils.unencodeNumber(identifier)[0];
|
2015-07-22 21:48:08 +02:00
|
|
|
return new Promise(function(resolve, reject) {
|
2015-05-05 05:26:26 +02:00
|
|
|
var identityKey = new IdentityKey({id: number});
|
|
|
|
identityKey.fetch().always(function() {
|
|
|
|
var oldpublicKey = identityKey.get('publicKey');
|
2015-07-09 21:47:16 +02:00
|
|
|
if (!oldpublicKey) {
|
|
|
|
// Lookup failed, or the current key was removed, so save this one.
|
|
|
|
identityKey.save({publicKey: publicKey}).then(resolve);
|
|
|
|
} else {
|
|
|
|
// Key exists, if it matches do nothing, else throw
|
|
|
|
if (equalArrayBuffers(oldpublicKey, publicKey)) {
|
|
|
|
resolve();
|
|
|
|
} else {
|
2015-07-22 21:48:08 +02:00
|
|
|
reject(new Error("Attempted to overwrite a different identity key"));
|
2015-07-09 21:47:16 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-22 00:54:25 +02:00
|
|
|
});
|
|
|
|
});
|
2015-04-21 22:33:29 +02:00
|
|
|
},
|
|
|
|
removeIdentityKey: function(number) {
|
2015-04-22 00:54:25 +02:00
|
|
|
return new Promise(function(resolve) {
|
2015-05-05 05:26:26 +02:00
|
|
|
var identityKey = new IdentityKey({id: number});
|
|
|
|
identityKey.fetch().then(function() {
|
|
|
|
identityKey.save({publicKey: undefined});
|
2015-04-22 00:54:25 +02:00
|
|
|
}).fail(function() {
|
2015-04-21 22:33:29 +02:00
|
|
|
throw new Error("Tried to remove identity for unknown number");
|
2015-04-22 00:54:25 +02:00
|
|
|
});
|
|
|
|
resolve(textsecure.storage.axolotl.removeAllSessions(number));
|
|
|
|
});
|
2015-04-21 22:33:29 +02:00
|
|
|
},
|
2015-05-06 23:22:18 +02:00
|
|
|
getGroup: function(groupId) {
|
2015-05-07 00:09:03 +02:00
|
|
|
if (groupId === null || groupId === undefined)
|
|
|
|
throw new Error("Tried to get group for undefined/null id");
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
var group = new Group({id: groupId});
|
|
|
|
group.fetch().always(function() {
|
|
|
|
resolve(group.get('data'));
|
|
|
|
});
|
|
|
|
});
|
2015-05-06 23:22:18 +02:00
|
|
|
},
|
|
|
|
putGroup: function(groupId, group) {
|
2015-05-07 00:09:03 +02:00
|
|
|
if (groupId === null || groupId === undefined)
|
|
|
|
throw new Error("Tried to put group key for undefined/null id");
|
|
|
|
if (group === null || group === undefined)
|
|
|
|
throw new Error("Tried to put undefined/null group object");
|
|
|
|
var group = new Group({id: groupId, data: group});
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
group.save().always(resolve);
|
|
|
|
});
|
2015-05-06 23:22:18 +02:00
|
|
|
},
|
|
|
|
removeGroup: function(groupId) {
|
2015-05-07 00:09:03 +02:00
|
|
|
if (groupId === null || groupId === undefined)
|
|
|
|
throw new Error("Tried to remove group key for undefined/null id");
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
var group = new Group({id: groupId});
|
|
|
|
group.destroy().always(resolve);
|
|
|
|
});
|
2015-05-06 23:22:18 +02:00
|
|
|
},
|
2015-04-21 22:30:22 +02:00
|
|
|
|
2015-04-01 22:08:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
window.AxolotlStore = AxolotlStore;
|
|
|
|
})();
|