From a1a528ccdd2cdaf5ceebd9a11acc65b578eb4463 Mon Sep 17 00:00:00 2001 From: lilia Date: Sat, 8 Nov 2014 17:26:20 -0800 Subject: [PATCH] Finish abstracting native client Firstly, don't initialize textsecure.nativclient unless the browser supports it. The mimetype-check trick is hewn from nacl-common.js. Secondly, nativeclient crypto functions will all automatically wait for the module to load before sending messages, so we needn't register any onload callbacks outside nativeclient.js. (Previously, if you wanted to do crypto with native client, you would have to register a call back and wait for the module to load.) Now that the native client crypto is encapsulated behind a nice interface, it can handle all that onload-callback jazz internally: if the module isn't loaded when you call a nativeclient function, return a promise that waits for the load callback, and eventually resolves with the result of the requested command. This removes the need for textsecure.registerOnLoadCallback. Finally, although native client has its quirks, it's significantly faster than the alternative (emscripten compiled js), so this commit also lets the crypto backend use native client opportunistically, if it's available, falling back to js if not, which should make us compatible with older versions of chrome and chromium. --- js/background.js | 40 ++--- js/crypto.js | 14 +- js/helpers.js | 4 - js/index.js | 18 +-- js/nativeclient.js | 37 +++-- js/options.js | 268 +++++++++++++++---------------- js/protocol.js | 9 +- test/crypto_test.js | 72 ++++----- test/curve25519_compiled_test.js | 1 - test/index.html | 2 +- test/nativeclient_test.js | 12 +- test/protocol_test.js | 40 +++-- 12 files changed, 252 insertions(+), 265 deletions(-) diff --git a/js/background.js b/js/background.js index cf74fcbf6ec4..33779699cf79 100644 --- a/js/background.js +++ b/js/background.js @@ -1,4 +1,4 @@ -/* vim: ts=4:sw=4 +/* vim: ts=4:sw=4:expandtab * * 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 @@ -14,21 +14,23 @@ * along with this program. If not, see . */ -textsecure.registerOnLoadFunction(function() { - if (!localStorage.getItem('first_install_ran')) { - localStorage.setItem('first_install_ran', 1); - extension.navigator.tabs.create("options.html"); - } else { - if (isRegistrationDone()) { - textsecure.subscribeToPush(function(message) { - Whisper.Messages.addIncomingMessage(message).then(function() { - console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice + - ': "' + getString(message.message.body) + '"'); - var newUnreadCount = textsecure.storage.getUnencrypted("unreadCount", 0) + 1; - textsecure.storage.putUnencrypted("unreadCount", newUnreadCount); - extension.navigator.setBadgeText(newUnreadCount); - }); - }); - } - } -}); +;(function() { + 'use strict'; + + if (!localStorage.getItem('first_install_ran')) { + localStorage.setItem('first_install_ran', 1); + extension.navigator.tabs.create("options.html"); + } else { + if (isRegistrationDone()) { + textsecure.subscribeToPush(function(message) { + Whisper.Messages.addIncomingMessage(message).then(function() { + console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice + + ': "' + getString(message.message.body) + '"'); + var newUnreadCount = textsecure.storage.getUnencrypted("unreadCount", 0) + 1; + textsecure.storage.putUnencrypted("unreadCount", newUnreadCount); + extension.navigator.setBadgeText(newUnreadCount); + }); + }); + } + } +})(); diff --git a/js/crypto.js b/js/crypto.js index 460bee3c6582..e106e14c717d 100644 --- a/js/crypto.js +++ b/js/crypto.js @@ -22,8 +22,10 @@ * for all low-level crypto operations, */ - var curve25519 = window.curve25519; - if (textsecure.NATIVE_CLIENT) curve25519 = textsecure.nativeclient; + function curve25519() { + // use native client opportunistically, since it's faster + return textsecure.nativeclient || window.curve25519; + } window.textsecure.crypto = { getRandomBytes: function(size) { @@ -81,7 +83,7 @@ throw new Error("Invalid private key"); } - return curve25519.privToPub(privKey).then(function(raw_keys) { + return curve25519().privToPub(privKey).then(function(raw_keys) { // prepend version byte var origPub = new Uint8Array(raw_keys.pubKey); var pub = new Uint8Array(33); @@ -99,7 +101,7 @@ if (pubKey === undefined || pubKey.byteLength != 32) throw new Error("Invalid public key"); - return curve25519.ECDHE(pubKey, privKey); + return curve25519().ECDHE(pubKey, privKey); }, Ed25519Sign: function(privKey, message) { if (privKey === undefined || privKey.byteLength != 32) @@ -108,7 +110,7 @@ if (message === undefined) throw new Error("Invalid message"); - return curve25519.Ed25519Sign(privKey, message); + return curve25519().Ed25519Sign(privKey, message); }, Ed25519Verify: function(pubKey, msg, sig) { pubKey = validatePubKeyFormat(pubKey); @@ -122,7 +124,7 @@ if (sig === undefined || sig.byteLength != 64) throw new Error("Invalid signature"); - return curve25519.Ed25519Verify(pubKey, msg, sig); + return curve25519().Ed25519Verify(pubKey, msg, sig); } }; diff --git a/js/helpers.js b/js/helpers.js index 14542701d19b..cd19048420bc 100644 --- a/js/helpers.js +++ b/js/helpers.js @@ -205,10 +205,6 @@ window.textsecure.throwHumanError = function(error, type, humanError) { throw e; } -window.textsecure.registerOnLoadFunction = textsecure.registerOnLoadFunction || function(func) { - return Promise.resolve(func()); -}; - window.textsecure.replay = function() { var self = {}; diff --git a/js/index.js b/js/index.js index 78a1760a0a26..65d28f36a919 100644 --- a/js/index.js +++ b/js/index.js @@ -62,14 +62,12 @@ Whisper.Layout = new (Backbone.View.extend({ } }))({el: document}); -textsecure.registerOnLoadFunction(function() { - if (textsecure.storage.getUnencrypted("number_id") === undefined) { - extension.navigator.tabs.create("options.html"); - } else { - textsecure.storage.putUnencrypted("unreadCount", 0); - extension.navigator.setBadgeText(""); - if (Whisper.Threads.length) { - Whisper.Threads.at(0).trigger('render'); - } +if (textsecure.storage.getUnencrypted("number_id") === undefined) { + extension.navigator.tabs.create("options.html"); +} else { + textsecure.storage.putUnencrypted("unreadCount", 0); + extension.navigator.setBadgeText(""); + if (Whisper.Threads.length) { + Whisper.Threads.at(0).trigger('render'); } -}); +} diff --git a/js/nativeclient.js b/js/nativeclient.js index 24f8fcaf7557..94605ae19476 100644 --- a/js/nativeclient.js +++ b/js/nativeclient.js @@ -16,10 +16,11 @@ ;(function() { 'use strict'; window.textsecure = window.textsecure || {}; - window.textsecure.NATIVE_CLIENT = window.textsecure.NATIVE_CLIENT || true; - if (!textsecure.NATIVE_CLIENT) { - window.textsecure.registerOnLoadFunction = window.textsecure.nativeclient.registerOnLoadFunction; + if (navigator.mimeTypes['application/x-nacl'] === undefined && + navigator.mimeTypes['application/x-pnacl'] === undefined) { + // browser does not support native client. + return; } var naclMessageNextId = 0; @@ -30,9 +31,11 @@ function postMessage(message) { return new Promise(function(resolve) { - naclMessageIdCallbackMap[naclMessageNextId] = resolve; - message.call_id = naclMessageNextId++; - common.naclModule.postMessage(message); + return registerOnLoadFunction(function() { + naclMessageIdCallbackMap[naclMessageNextId] = resolve; + message.call_id = naclMessageNextId++; + common.naclModule.postMessage(message); + }); }); }; @@ -49,7 +52,17 @@ } } onLoadCallbacks = []; - } + }; + + function registerOnLoadFunction(func) { + return new Promise(function(resolve, reject) { + if (naclLoaded) { + return resolve(func()); + } else { + onLoadCallbacks[onLoadCallbacks.length] = [ func, resolve, reject ]; + } + }); + }; window.textsecure.nativeclient = { privToPub: function(priv) { @@ -75,16 +88,6 @@ if (!message.res) throw new Error("Invalid signature"); }); - }, - registerOnLoadFunction: function(func) { - return new Promise(function(resolve, reject) { - if (naclLoaded) { - return resolve(func()); - } else { - onLoadCallbacks[onLoadCallbacks.length] = [ func, resolve, reject ]; - } - }); } }; - })(); diff --git a/js/options.js b/js/options.js index e3f82efb2f23..0eb58d146480 100644 --- a/js/options.js +++ b/js/options.js @@ -44,165 +44,163 @@ $('#error').hide().text(error).addClass('in').fadeIn(); }; - textsecure.registerOnLoadFunction(function() { - $(function() { - if (isRegistrationDone()) { - $('#complete-number').text(textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0]);//TODO: no - $('#setup-complete').show().addClass('in'); - } else { - $('#choose-setup').show().addClass('in'); - $('#number').keyup(validateNumber); - $('#regionCode').change(validateNumber); + $(function() { + if (isRegistrationDone()) { + $('#complete-number').text(textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0]);//TODO: no + $('#setup-complete').show().addClass('in'); + } else { + $('#choose-setup').show().addClass('in'); + $('#number').keyup(validateNumber); + $('#regionCode').change(validateNumber); - $.each(libphonenumber.util.getAllRegionCodes(), function (regionCode, countryName) { - $('#regionCode').append( - $('