textsecure.storage, chromium.js

This commit is contained in:
Matt Corallo 2014-05-20 22:21:07 -04:00
parent ee2f43aba4
commit d9bf0a41fb
10 changed files with 84 additions and 81 deletions

View file

@ -37,6 +37,7 @@
<script type="text/javascript" src="js/helpers.js"></script> <script type="text/javascript" src="js/helpers.js"></script>
<script type="text/javascript" src="js/api.js"></script> <script type="text/javascript" src="js/api.js"></script>
<script type="text/javascript" src="js/background.js"></script> <script type="text/javascript" src="js/background.js"></script>
<script type="text/javascript" src="js/chromium.js"></script>
</head> </head>
<body data-name="curve25519" data-tools="pnacl" data-configs="Debug Release" data-path="pnacl/{config}"> <body data-name="curve25519" data-tools="pnacl" data-configs="Debug Release" data-path="pnacl/{config}">
<div id="listener"></div> <div id="listener"></div>

View file

@ -54,8 +54,8 @@ window.textsecure.api = function() {
param.urlParameters = ""; param.urlParameters = "";
if (param.do_auth) { if (param.do_auth) {
param.user = storage.getUnencrypted("number_id"); param.user = textsecure.storage.getUnencrypted("number_id");
param.password = storage.getEncrypted("password"); param.password = textsecure.storage.getEncrypted("password");
} }
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
@ -223,8 +223,8 @@ window.textsecure.api = function() {
}; };
self.getWebsocket = function() { self.getWebsocket = function() {
var user = storage.getUnencrypted("number_id"); var user = textsecure.storage.getUnencrypted("number_id");
var password = storage.getEncrypted("password"); var password = textsecure.storage.getEncrypted("password");
var URL = URL_BASE.replace(/^http/g, 'ws') + URL_CALLS['push'] + '/?'; var URL = URL_BASE.replace(/^http/g, 'ws') + URL_CALLS['push'] + '/?';
var params = $.param({ var params = $.param({
user: '+' + getString(user).substring(1), user: '+' + getString(user).substring(1),

View file

@ -24,8 +24,8 @@ textsecure.registerOnLoadFunction(function() {
Whisper.Messages.addIncomingMessage(message); Whisper.Messages.addIncomingMessage(message);
console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice + console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice +
': "' + getString(message.message.body) + '"'); ': "' + getString(message.message.body) + '"');
var newUnreadCount = storage.getUnencrypted("unreadCount", 0) + 1; var newUnreadCount = textsecure.storage.getUnencrypted("unreadCount", 0) + 1;
storage.putUnencrypted("unreadCount", newUnreadCount); textsecure.storage.putUnencrypted("unreadCount", newUnreadCount);
chrome.browserAction.setBadgeText({text: newUnreadCount + ""}); chrome.browserAction.setBadgeText({text: newUnreadCount + ""});
}); });
} }

27
js/chromium.js Normal file
View file

@ -0,0 +1,27 @@
/* vim: ts=4:sw=4
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Random shared utilities that are used only by chromium things
function registrationDone() {
textsecure.storage.putUnencrypted("registration_done", "");
//TODO: Fix dirty hack:
chrome.runtime.reload();
}
function isRegistrationDone() {
return textsecure.storage.getUnencrypted("registration_done") !== undefined;
}

View file

@ -51,6 +51,15 @@ window.textsecure.crypto = new function() {
return res; return res;
} }
function objectContainsKeys(object) {
var count = 0;
for (key in object) {
count++;
break;
}
return count != 0;
}
function HmacSHA256(key, input) { function HmacSHA256(key, input) {
return window.crypto.subtle.sign({name: "HMAC", hash: "SHA-256"}, key, input); return window.crypto.subtle.sign({name: "HMAC", hash: "SHA-256"}, key, input);
} }
@ -104,17 +113,17 @@ window.textsecure.crypto = new function() {
crypto_storage.getNewPubKeySTORINGPrivKey = function(keyName, isIdentity) { crypto_storage.getNewPubKeySTORINGPrivKey = function(keyName, isIdentity) {
return createNewKeyPair(isIdentity).then(function(keyPair) { return createNewKeyPair(isIdentity).then(function(keyPair) {
storage.putEncrypted("25519Key" + keyName, keyPair); textsecure.storage.putEncrypted("25519Key" + keyName, keyPair);
return keyPair.pubKey; return keyPair.pubKey;
}); });
} }
crypto_storage.getStoredPubKey = function(keyName) { crypto_storage.getStoredPubKey = function(keyName) {
return toArrayBuffer(storage.getEncrypted("25519Key" + keyName, { pubKey: undefined }).pubKey); return toArrayBuffer(textsecure.storage.getEncrypted("25519Key" + keyName, { pubKey: undefined }).pubKey);
} }
crypto_storage.getStoredKeyPair = function(keyName) { crypto_storage.getStoredKeyPair = function(keyName) {
var res = storage.getEncrypted("25519Key" + keyName); var res = textsecure.storage.getEncrypted("25519Key" + keyName);
if (res === undefined) if (res === undefined)
return undefined; return undefined;
return { pubKey: toArrayBuffer(res.pubKey), privKey: toArrayBuffer(res.privKey) }; return { pubKey: toArrayBuffer(res.pubKey), privKey: toArrayBuffer(res.privKey) };
@ -122,7 +131,7 @@ window.textsecure.crypto = new function() {
crypto_storage.getAndRemoveStoredKeyPair = function(keyName) { crypto_storage.getAndRemoveStoredKeyPair = function(keyName) {
var keyPair = this.getStoredKeyPair(keyName); var keyPair = this.getStoredKeyPair(keyName);
storage.removeEncrypted("25519Key" + keyName); textsecure.storage.removeEncrypted("25519Key" + keyName);
return keyPair; return keyPair;
} }
@ -135,7 +144,7 @@ window.textsecure.crypto = new function() {
} }
crypto_storage.saveSession = function(encodedNumber, session) { crypto_storage.saveSession = function(encodedNumber, session) {
var sessions = storage.getEncrypted("session" + getEncodedNumber(encodedNumber)); var sessions = textsecure.storage.getEncrypted("session" + getEncodedNumber(encodedNumber));
if (sessions === undefined) if (sessions === undefined)
sessions = {}; sessions = {};
@ -162,11 +171,11 @@ window.textsecure.crypto = new function() {
else else
sessions[getString(session.indexInfo.baseKey)] = session; sessions[getString(session.indexInfo.baseKey)] = session;
storage.putEncrypted("session" + getEncodedNumber(encodedNumber), sessions); textsecure.storage.putEncrypted("session" + getEncodedNumber(encodedNumber), sessions);
} }
crypto_storage.getOpenSession = function(encodedNumber) { crypto_storage.getOpenSession = function(encodedNumber) {
var sessions = storage.getEncrypted("session" + getEncodedNumber(encodedNumber)); var sessions = textsecure.storage.getEncrypted("session" + getEncodedNumber(encodedNumber));
if (sessions === undefined) if (sessions === undefined)
return undefined; return undefined;
@ -181,7 +190,7 @@ window.textsecure.crypto = new function() {
} }
crypto_storage.getSessionByRemoteEphemeralKey = function(encodedNumber, remoteEphemeralKey) { crypto_storage.getSessionByRemoteEphemeralKey = function(encodedNumber, remoteEphemeralKey) {
var sessions = storage.getEncrypted("session" + getEncodedNumber(encodedNumber)); var sessions = textsecure.storage.getEncrypted("session" + getEncodedNumber(encodedNumber));
if (sessions === undefined) if (sessions === undefined)
return undefined; return undefined;
@ -208,7 +217,7 @@ window.textsecure.crypto = new function() {
crypto_storage.getSessionOrIdentityKeyByBaseKey = function(encodedNumber, baseKey) { crypto_storage.getSessionOrIdentityKeyByBaseKey = function(encodedNumber, baseKey) {
var sessions = storage.getEncrypted("session" + getEncodedNumber(encodedNumber)); var sessions = textsecure.storage.getEncrypted("session" + getEncodedNumber(encodedNumber));
if (sessions === undefined) if (sessions === undefined)
return undefined; return undefined;
@ -557,7 +566,7 @@ window.textsecure.crypto = new function() {
*************************/ *************************/
// Decrypts message into a raw string // Decrypts message into a raw string
self.decryptWebsocketMessage = function(message) { self.decryptWebsocketMessage = function(message) {
var signaling_key = storage.getEncrypted("signaling_key"); //TODO: in crypto_storage var signaling_key = textsecure.storage.getEncrypted("signaling_key"); //TODO: in crypto_storage
var aes_key = toArrayBuffer(signaling_key.substring(0, 32)); var aes_key = toArrayBuffer(signaling_key.substring(0, 32));
var mac_key = toArrayBuffer(signaling_key.substring(32, 32 + 20)); var mac_key = toArrayBuffer(signaling_key.substring(32, 32 + 20));
@ -592,11 +601,9 @@ window.textsecure.crypto = new function() {
self.handleIncomingPushMessageProto = function(proto) { self.handleIncomingPushMessageProto = function(proto) {
switch(proto.type) { switch(proto.type) {
case 0: //TYPE_MESSAGE_PLAINTEXT case 0: //TYPE_MESSAGE_PLAINTEXT
return Promise.resolve({message: decodePushMessageContentProtobuf(getString(proto.message)), pushMessage:proto}); return Promise.resolve(decodePushMessageContentProtobuf(getString(proto.message)));
case 1: //TYPE_MESSAGE_CIPHERTEXT case 1: //TYPE_MESSAGE_CIPHERTEXT
return decryptWhisperMessage(proto.source, getString(proto.message)).then(function(result) { return decryptWhisperMessage(proto.source, getString(proto.message));
return {message: result, pushMessage: proto};
});
case 3: //TYPE_MESSAGE_PREKEY_BUNDLE case 3: //TYPE_MESSAGE_PREKEY_BUNDLE
if (proto.message.readUint8() != (2 << 4 | 2)) if (proto.message.readUint8() != (2 << 4 | 2))
throw new Error("Bad version byte"); throw new Error("Bad version byte");
@ -605,7 +612,7 @@ window.textsecure.crypto = new function() {
return decryptWhisperMessage(proto.source, getString(preKeyProto.message), sessions[0]).then(function(result) { return decryptWhisperMessage(proto.source, getString(preKeyProto.message), sessions[0]).then(function(result) {
if (sessions[1] !== undefined) if (sessions[1] !== undefined)
crypto_storage.saveSession(proto.source, sessions[1]); crypto_storage.saveSession(proto.source, sessions[1]);
return {message: result, pushMessage: proto}; return result;
}); });
}); });
} }
@ -649,7 +656,7 @@ window.textsecure.crypto = new function() {
var preKeyMsg = new PreKeyWhisperMessageProtobuf(); var preKeyMsg = new PreKeyWhisperMessageProtobuf();
preKeyMsg.identityKey = toArrayBuffer(crypto_storage.getStoredPubKey("identityKey")); preKeyMsg.identityKey = toArrayBuffer(crypto_storage.getStoredPubKey("identityKey"));
preKeyMsg.preKeyId = deviceObject.preKeyId; preKeyMsg.preKeyId = deviceObject.preKeyId;
preKeyMsg.registrationId = storage.getUnencrypted("registrationId"); preKeyMsg.registrationId = textsecure.storage.getUnencrypted("registrationId");
if (session === undefined) { if (session === undefined) {
return createNewKeyPair(false).then(function(baseKey) { return createNewKeyPair(false).then(function(baseKey) {
@ -684,8 +691,8 @@ window.textsecure.crypto = new function() {
var identityKeyCalculated = function(pubKey) { var identityKeyCalculated = function(pubKey) {
identityKey = pubKey; identityKey = pubKey;
var firstKeyId = storage.getEncrypted("maxPreKeyId", -1) + 1; var firstKeyId = textsecure.storage.getEncrypted("maxPreKeyId", -1) + 1;
storage.putEncrypted("maxPreKeyId", firstKeyId + GENERATE_KEYS_KEYS_GENERATED); textsecure.storage.putEncrypted("maxPreKeyId", firstKeyId + GENERATE_KEYS_KEYS_GENERATED);
if (firstKeyId > 16777000) if (firstKeyId > 16777000)
return new Promise(function() { throw new Error("You crazy motherfucker") }); return new Promise(function() { throw new Error("You crazy motherfucker") });

View file

@ -226,28 +226,10 @@ function verifyNumber(string) {
return getEncodedNumber(string.trim()); return getEncodedNumber(string.trim());
} }
// Other
function timestampToHumanReadable(timestamp) {
var date = new Date();
date.setTime(timestamp*1000);
return date.toUTCString();
}
function objectContainsKeys(object) {
var count = 0;
for (key in object) {
count++;
break;
}
return count != 0;
}
/************************************************ /************************************************
*** Utilities to store data in local storage *** *** Utilities to store data in local storage ***
************************************************/ ************************************************/
//TODO: textsecure.storage window.textsecure.storage = function() {
window.storage = function() {
var self = {}; var self = {};
/***************************** /*****************************
@ -296,11 +278,11 @@ window.storage = function() {
var self = {}; var self = {};
self.getDeviceObject = function(encodedNumber) { self.getDeviceObject = function(encodedNumber) {
return storage.getEncrypted("deviceObject" + getEncodedNumber(encodedNumber)); return textsecure.storage.getEncrypted("deviceObject" + getEncodedNumber(encodedNumber));
} }
self.getDeviceIdListFromNumber = function(number) { self.getDeviceIdListFromNumber = function(number) {
return storage.getEncrypted("deviceIdList" + getNumberFromString(number), []); return textsecure.storage.getEncrypted("deviceIdList" + getNumberFromString(number), []);
} }
self.addDeviceIdForNumber = function(number, deviceId) { self.addDeviceIdForNumber = function(number, deviceId) {
@ -310,7 +292,7 @@ window.storage = function() {
return; return;
} }
deviceIdList[deviceIdList.length] = deviceId; deviceIdList[deviceIdList.length] = deviceId;
storage.putEncrypted("deviceIdList" + getNumberFromString(number), deviceIdList); textsecure.storage.putEncrypted("deviceIdList" + getNumberFromString(number), deviceIdList);
} }
var getDeviceId = function(encodedNumber) { var getDeviceId = function(encodedNumber) {
@ -334,7 +316,7 @@ window.storage = function() {
existing[key] = deviceObject[key]; existing[key] = deviceObject[key];
} }
storage.putEncrypted("deviceObject" + getEncodedNumber(deviceObject.encodedNumber), existing); textsecure.storage.putEncrypted("deviceObject" + getEncodedNumber(deviceObject.encodedNumber), existing);
this.addDeviceIdForNumber(deviceObject.encodedNumber, getDeviceId(deviceObject.encodedNumber)); this.addDeviceIdForNumber(deviceObject.encodedNumber, getDeviceId(deviceObject.encodedNumber));
} }
@ -352,16 +334,6 @@ window.storage = function() {
return self; return self;
}(); }();
function registrationDone() {
storage.putUnencrypted("registration_done", "");
//TODO: Fix dirty hack:
chrome.runtime.reload();
}
function isRegistrationDone() {
return storage.getUnencrypted("registration_done") !== undefined;
}
/********************** /**********************
*** NaCL Interface *** *** NaCL Interface ***
**********************/ **********************/
@ -489,7 +461,7 @@ window.textsecure.sendMessage = function() {
function getKeysForNumber(number) { function getKeysForNumber(number) {
return textsecure.api.getKeysForNumber(number).then(function(response) { return textsecure.api.getKeysForNumber(number).then(function(response) {
for (var i = 0; i < response.length; i++) { for (var i = 0; i < response.length; i++) {
storage.devices.saveDeviceObject({ textsecure.storage.devices.saveDeviceObject({
encodedNumber: number + "." + response[i].deviceId, encodedNumber: number + "." + response[i].deviceId,
identityKey: response[i].identityKey, identityKey: response[i].identityKey,
publicKey: response[i].publicKey, publicKey: response[i].publicKey,
@ -569,11 +541,11 @@ window.textsecure.sendMessage = function() {
for (var i = 0; i < numbers.length; i++) { for (var i = 0; i < numbers.length; i++) {
var number = numbers[i]; var number = numbers[i];
var devicesForNumber = storage.devices.getDeviceObjectListFromNumber(number); var devicesForNumber = textsecure.storage.devices.getDeviceObjectListFromNumber(number);
if (devicesForNumber.length == 0) { if (devicesForNumber.length == 0) {
getKeysForNumber(number).then(function(identity_key) { getKeysForNumber(number).then(function(identity_key) {
devicesForNumber = storage.devices.getDeviceObjectListFromNumber(number); devicesForNumber = textsecure.storage.devices.getDeviceObjectListFromNumber(number);
if (devicesForNumber.length == 0) if (devicesForNumber.length == 0)
registerError(number, "Failed to retreive new device keys for number " + number, null); registerError(number, "Failed to retreive new device keys for number " + number, null);
else else
@ -588,7 +560,7 @@ window.textsecure.sendMessage = function() {
}(); }();
function requestIdentityPrivKeyFromMasterDevice(number, identityKey) { function requestIdentityPrivKeyFromMasterDevice(number, identityKey) {
sendMessageToDevices([storage.devices.getDeviceObject(getNumberFromString(number)) + ".1"], sendMessageToDevices([textsecure.storage.devices.getDeviceObject(getNumberFromString(number)) + ".1"],
{message: "Identity Key request"}, function() {}, function() {});//TODO {message: "Identity Key request"}, function() {}, function() {});//TODO
} }

View file

@ -81,10 +81,10 @@ $('#init-go').click(function() {
if (single_device) if (single_device)
response = 1; response = 1;
var number_id = number + "." + response; var number_id = number + "." + response;
storage.putEncrypted("password", password); textsecure.storage.putEncrypted("password", password);
storage.putEncrypted('signaling_key', signaling_key); textsecure.storage.putEncrypted('signaling_key', signaling_key);
storage.putUnencrypted("number_id", number_id); textsecure.storage.putUnencrypted("number_id", number_id);
storage.putUnencrypted("registrationId", registrationId); textsecure.storage.putUnencrypted("registrationId", registrationId);
$('#verify1done').html('done'); $('#verify1done').html('done');
var register_keys_func = function() { var register_keys_func = function() {
@ -142,7 +142,7 @@ textsecure.registerOnLoadFunction(function() {
if (!isRegistrationDone()) { if (!isRegistrationDone()) {
$('#init-setup').show(); $('#init-setup').show();
} else { } else {
$('#complete-number').html(storage.getUnencrypted("number_id").split(".")[0]); $('#complete-number').html(textsecure.storage.getUnencrypted("number_id").split(".")[0]);
$('#setup-complete').show(); $('#setup-complete').show();
} }
}); });

View file

@ -28,13 +28,13 @@ $('#send_link').click(function(e) {
}); });
textsecure.registerOnLoadFunction(function() { textsecure.registerOnLoadFunction(function() {
if (storage.getUnencrypted("number_id") === undefined) { if (textsecure.storage.getUnencrypted("number_id") === undefined) {
chrome.tabs.create({url: "options.html"}); chrome.tabs.create({url: "options.html"});
} else { } else {
$(window).bind('storage', function(e) { Whisper.Messages.fetch(); }); $(window).bind('storage', function(e) { Whisper.Messages.fetch(); });
Whisper.Messages.fetch(); Whisper.Messages.fetch();
$('.my-number').text(storage.getUnencrypted("number_id").split(".")[0]); $('.my-number').text(textsecure.storage.getUnencrypted("number_id").split(".")[0]);
storage.putUnencrypted("unreadCount", 0); textsecure.storage.putUnencrypted("unreadCount", 0);
chrome.browserAction.setBadgeText({text: ""}); chrome.browserAction.setBadgeText({text: ""});
$("#me").click(function() { $("#me").click(function() {
$('#popup_send_numbers').val($('.my-number').text()); $('#popup_send_numbers').val($('.my-number').text());

View file

@ -98,11 +98,6 @@ function hexToArrayBuffer(str) {
textsecure.registerOnLoadFunction(function() { textsecure.registerOnLoadFunction(function() {
localStorage.clear(); localStorage.clear();
// Random tests to check my JS knowledge
TEST(function() { return Promise.resolve(!objectContainsKeys({})); });
TEST(function() { return Promise.resolve(objectContainsKeys({ a: undefined })); });
TEST(function() { return Promise.resolve(objectContainsKeys({ a: null })); });
TEST(function() { TEST(function() {
var b = new ArrayBuffer(3); var b = new ArrayBuffer(3);
var a = new Uint8Array(b); var a = new Uint8Array(b);
@ -131,13 +126,13 @@ textsecure.registerOnLoadFunction(function() {
TEST(function() { TEST(function() {
return textsecure.crypto.generateKeys().then(function() { return textsecure.crypto.generateKeys().then(function() {
if (storage.getEncrypted("25519KeyidentityKey") === undefined) if (textsecure.storage.getEncrypted("25519KeyidentityKey") === undefined)
return false; return false;
if (storage.getEncrypted("25519KeypreKey16777215") === undefined) if (textsecure.storage.getEncrypted("25519KeypreKey16777215") === undefined)
return false; return false;
for (var i = 0; i < 100; i++) for (var i = 0; i < 100; i++)
if (storage.getEncrypted("25519KeypreKey" + i) === undefined) if (textsecure.storage.getEncrypted("25519KeypreKey" + i) === undefined)
return false; return false;
return true; return true;
@ -352,9 +347,9 @@ textsecure.registerOnLoadFunction(function() {
if (data.ourIdentityKey !== undefined) if (data.ourIdentityKey !== undefined)
return textsecure.crypto.testing_only.privToPub(data.ourIdentityKey, true).then(function(keyPair) { return textsecure.crypto.testing_only.privToPub(data.ourIdentityKey, true).then(function(keyPair) {
storage.putEncrypted("25519KeyidentityKey", keyPair); textsecure.storage.putEncrypted("25519KeyidentityKey", keyPair);
return textsecure.crypto.testing_only.privToPub(data.ourPreKey, false).then(function(keyPair) { return textsecure.crypto.testing_only.privToPub(data.ourPreKey, false).then(function(keyPair) {
storage.putEncrypted("25519KeypreKey" + data.preKeyId, keyPair); textsecure.storage.putEncrypted("25519KeypreKey" + data.preKeyId, keyPair);
return postLocalKeySetup(); return postLocalKeySetup();
}); });
}); });
@ -368,7 +363,7 @@ textsecure.registerOnLoadFunction(function() {
if (data.theirPreKey !== undefined) { if (data.theirPreKey !== undefined) {
remoteDevice.publicKey = data.theirPreKey; remoteDevice.publicKey = data.theirPreKey;
remoteDevice.preKeyId = data.theirPreKeyId; remoteDevice.preKeyId = data.theirPreKeyId;
storage.putUnencrypted("registrationId", data.registrationId); textsecure.storage.putUnencrypted("registrationId", data.registrationId);
} }
var message = new PushMessageContentProtobuf(); var message = new PushMessageContentProtobuf();
@ -396,7 +391,7 @@ textsecure.registerOnLoadFunction(function() {
if (data.ourIdentityKey !== undefined) if (data.ourIdentityKey !== undefined)
return textsecure.crypto.testing_only.privToPub(data.ourIdentityKey, true).then(function(keyPair) { return textsecure.crypto.testing_only.privToPub(data.ourIdentityKey, true).then(function(keyPair) {
storage.putEncrypted("25519KeyidentityKey", keyPair); textsecure.storage.putEncrypted("25519KeyidentityKey", keyPair);
return postLocalKeySetup(); return postLocalKeySetup();
}); });
else else

View file

@ -57,5 +57,6 @@
<script type="text/javascript" src="js/api.js"></script> <script type="text/javascript" src="js/api.js"></script>
<script type="text/javascript" src="js/helpers.js"></script> <script type="text/javascript" src="js/helpers.js"></script>
<script type="text/javascript" src="js/options.js"></script> <script type="text/javascript" src="js/options.js"></script>
<script type="text/javascript" src="js/chromium.js"></script>
</body> </body>
</html> </html>