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/api.js"></script>
<script type="text/javascript" src="js/background.js"></script>
<script type="text/javascript" src="js/chromium.js"></script>
</head>
<body data-name="curve25519" data-tools="pnacl" data-configs="Debug Release" data-path="pnacl/{config}">
<div id="listener"></div>

View File

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

View File

@ -24,8 +24,8 @@ textsecure.registerOnLoadFunction(function() {
Whisper.Messages.addIncomingMessage(message);
console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice +
': "' + getString(message.message.body) + '"');
var newUnreadCount = storage.getUnencrypted("unreadCount", 0) + 1;
storage.putUnencrypted("unreadCount", newUnreadCount);
var newUnreadCount = textsecure.storage.getUnencrypted("unreadCount", 0) + 1;
textsecure.storage.putUnencrypted("unreadCount", 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;
}
function objectContainsKeys(object) {
var count = 0;
for (key in object) {
count++;
break;
}
return count != 0;
}
function HmacSHA256(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) {
return createNewKeyPair(isIdentity).then(function(keyPair) {
storage.putEncrypted("25519Key" + keyName, keyPair);
textsecure.storage.putEncrypted("25519Key" + keyName, keyPair);
return keyPair.pubKey;
});
}
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) {
var res = storage.getEncrypted("25519Key" + keyName);
var res = textsecure.storage.getEncrypted("25519Key" + keyName);
if (res === undefined)
return undefined;
return { pubKey: toArrayBuffer(res.pubKey), privKey: toArrayBuffer(res.privKey) };
@ -122,7 +131,7 @@ window.textsecure.crypto = new function() {
crypto_storage.getAndRemoveStoredKeyPair = function(keyName) {
var keyPair = this.getStoredKeyPair(keyName);
storage.removeEncrypted("25519Key" + keyName);
textsecure.storage.removeEncrypted("25519Key" + keyName);
return keyPair;
}
@ -135,7 +144,7 @@ window.textsecure.crypto = new function() {
}
crypto_storage.saveSession = function(encodedNumber, session) {
var sessions = storage.getEncrypted("session" + getEncodedNumber(encodedNumber));
var sessions = textsecure.storage.getEncrypted("session" + getEncodedNumber(encodedNumber));
if (sessions === undefined)
sessions = {};
@ -162,11 +171,11 @@ window.textsecure.crypto = new function() {
else
sessions[getString(session.indexInfo.baseKey)] = session;
storage.putEncrypted("session" + getEncodedNumber(encodedNumber), sessions);
textsecure.storage.putEncrypted("session" + getEncodedNumber(encodedNumber), sessions);
}
crypto_storage.getOpenSession = function(encodedNumber) {
var sessions = storage.getEncrypted("session" + getEncodedNumber(encodedNumber));
var sessions = textsecure.storage.getEncrypted("session" + getEncodedNumber(encodedNumber));
if (sessions === undefined)
return undefined;
@ -181,7 +190,7 @@ window.textsecure.crypto = new function() {
}
crypto_storage.getSessionByRemoteEphemeralKey = function(encodedNumber, remoteEphemeralKey) {
var sessions = storage.getEncrypted("session" + getEncodedNumber(encodedNumber));
var sessions = textsecure.storage.getEncrypted("session" + getEncodedNumber(encodedNumber));
if (sessions === undefined)
return undefined;
@ -208,7 +217,7 @@ window.textsecure.crypto = new function() {
crypto_storage.getSessionOrIdentityKeyByBaseKey = function(encodedNumber, baseKey) {
var sessions = storage.getEncrypted("session" + getEncodedNumber(encodedNumber));
var sessions = textsecure.storage.getEncrypted("session" + getEncodedNumber(encodedNumber));
if (sessions === undefined)
return undefined;
@ -557,7 +566,7 @@ window.textsecure.crypto = new function() {
*************************/
// Decrypts message into a raw string
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 mac_key = toArrayBuffer(signaling_key.substring(32, 32 + 20));
@ -592,11 +601,9 @@ window.textsecure.crypto = new function() {
self.handleIncomingPushMessageProto = function(proto) {
switch(proto.type) {
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
return decryptWhisperMessage(proto.source, getString(proto.message)).then(function(result) {
return {message: result, pushMessage: proto};
});
return decryptWhisperMessage(proto.source, getString(proto.message));
case 3: //TYPE_MESSAGE_PREKEY_BUNDLE
if (proto.message.readUint8() != (2 << 4 | 2))
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) {
if (sessions[1] !== undefined)
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();
preKeyMsg.identityKey = toArrayBuffer(crypto_storage.getStoredPubKey("identityKey"));
preKeyMsg.preKeyId = deviceObject.preKeyId;
preKeyMsg.registrationId = storage.getUnencrypted("registrationId");
preKeyMsg.registrationId = textsecure.storage.getUnencrypted("registrationId");
if (session === undefined) {
return createNewKeyPair(false).then(function(baseKey) {
@ -684,8 +691,8 @@ window.textsecure.crypto = new function() {
var identityKeyCalculated = function(pubKey) {
identityKey = pubKey;
var firstKeyId = storage.getEncrypted("maxPreKeyId", -1) + 1;
storage.putEncrypted("maxPreKeyId", firstKeyId + GENERATE_KEYS_KEYS_GENERATED);
var firstKeyId = textsecure.storage.getEncrypted("maxPreKeyId", -1) + 1;
textsecure.storage.putEncrypted("maxPreKeyId", firstKeyId + GENERATE_KEYS_KEYS_GENERATED);
if (firstKeyId > 16777000)
return new Promise(function() { throw new Error("You crazy motherfucker") });

View File

@ -226,28 +226,10 @@ function verifyNumber(string) {
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 ***
************************************************/
//TODO: textsecure.storage
window.storage = function() {
window.textsecure.storage = function() {
var self = {};
/*****************************
@ -296,11 +278,11 @@ window.storage = function() {
var self = {};
self.getDeviceObject = function(encodedNumber) {
return storage.getEncrypted("deviceObject" + getEncodedNumber(encodedNumber));
return textsecure.storage.getEncrypted("deviceObject" + getEncodedNumber(encodedNumber));
}
self.getDeviceIdListFromNumber = function(number) {
return storage.getEncrypted("deviceIdList" + getNumberFromString(number), []);
return textsecure.storage.getEncrypted("deviceIdList" + getNumberFromString(number), []);
}
self.addDeviceIdForNumber = function(number, deviceId) {
@ -310,7 +292,7 @@ window.storage = function() {
return;
}
deviceIdList[deviceIdList.length] = deviceId;
storage.putEncrypted("deviceIdList" + getNumberFromString(number), deviceIdList);
textsecure.storage.putEncrypted("deviceIdList" + getNumberFromString(number), deviceIdList);
}
var getDeviceId = function(encodedNumber) {
@ -334,7 +316,7 @@ window.storage = function() {
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));
}
@ -352,16 +334,6 @@ window.storage = function() {
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 ***
**********************/
@ -489,7 +461,7 @@ window.textsecure.sendMessage = function() {
function getKeysForNumber(number) {
return textsecure.api.getKeysForNumber(number).then(function(response) {
for (var i = 0; i < response.length; i++) {
storage.devices.saveDeviceObject({
textsecure.storage.devices.saveDeviceObject({
encodedNumber: number + "." + response[i].deviceId,
identityKey: response[i].identityKey,
publicKey: response[i].publicKey,
@ -569,11 +541,11 @@ window.textsecure.sendMessage = function() {
for (var i = 0; i < numbers.length; i++) {
var number = numbers[i];
var devicesForNumber = storage.devices.getDeviceObjectListFromNumber(number);
var devicesForNumber = textsecure.storage.devices.getDeviceObjectListFromNumber(number);
if (devicesForNumber.length == 0) {
getKeysForNumber(number).then(function(identity_key) {
devicesForNumber = storage.devices.getDeviceObjectListFromNumber(number);
devicesForNumber = textsecure.storage.devices.getDeviceObjectListFromNumber(number);
if (devicesForNumber.length == 0)
registerError(number, "Failed to retreive new device keys for number " + number, null);
else
@ -588,7 +560,7 @@ window.textsecure.sendMessage = function() {
}();
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
}

View File

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

View File

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

View File

@ -98,11 +98,6 @@ function hexToArrayBuffer(str) {
textsecure.registerOnLoadFunction(function() {
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() {
var b = new ArrayBuffer(3);
var a = new Uint8Array(b);
@ -131,13 +126,13 @@ textsecure.registerOnLoadFunction(function() {
TEST(function() {
return textsecure.crypto.generateKeys().then(function() {
if (storage.getEncrypted("25519KeyidentityKey") === undefined)
if (textsecure.storage.getEncrypted("25519KeyidentityKey") === undefined)
return false;
if (storage.getEncrypted("25519KeypreKey16777215") === undefined)
if (textsecure.storage.getEncrypted("25519KeypreKey16777215") === undefined)
return false;
for (var i = 0; i < 100; i++)
if (storage.getEncrypted("25519KeypreKey" + i) === undefined)
if (textsecure.storage.getEncrypted("25519KeypreKey" + i) === undefined)
return false;
return true;
@ -352,9 +347,9 @@ textsecure.registerOnLoadFunction(function() {
if (data.ourIdentityKey !== undefined)
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) {
storage.putEncrypted("25519KeypreKey" + data.preKeyId, keyPair);
textsecure.storage.putEncrypted("25519KeypreKey" + data.preKeyId, keyPair);
return postLocalKeySetup();
});
});
@ -368,7 +363,7 @@ textsecure.registerOnLoadFunction(function() {
if (data.theirPreKey !== undefined) {
remoteDevice.publicKey = data.theirPreKey;
remoteDevice.preKeyId = data.theirPreKeyId;
storage.putUnencrypted("registrationId", data.registrationId);
textsecure.storage.putUnencrypted("registrationId", data.registrationId);
}
var message = new PushMessageContentProtobuf();
@ -396,7 +391,7 @@ textsecure.registerOnLoadFunction(function() {
if (data.ourIdentityKey !== undefined)
return textsecure.crypto.testing_only.privToPub(data.ourIdentityKey, true).then(function(keyPair) {
storage.putEncrypted("25519KeyidentityKey", keyPair);
textsecure.storage.putEncrypted("25519KeyidentityKey", keyPair);
return postLocalKeySetup();
});
else

View File

@ -57,5 +57,6 @@
<script type="text/javascript" src="js/api.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/chromium.js"></script>
</body>
</html>