Browse Source

Renamed stuff to "EndToEndEncryption".

pull/225/head
Joachim Bauch 11 years ago
parent
commit
c33d2cb416
  1. 20
      static/js/mediastream/api.js
  2. 4
      static/js/services/api.js
  3. 76
      static/js/services/endtoendencryption.js
  4. 6
      static/js/services/services.js

20
static/js/mediastream/api.js

@ -22,19 +22,19 @@
"use strict"; "use strict";
define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) { define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
var Api = function(version, connector, encryption) { var Api = function(version, connector, endToEndEncryption) {
this.e = $({}); this.e = $({});
this.version = version; this.version = version;
this.id = null; this.id = null;
this.sid = null; this.sid = null;
this.session = {}; this.session = {};
this.connector = connector; this.connector = connector;
if (!encryption.initialize(this)) { if (!endToEndEncryption.initialize(this)) {
console.warn("Encryption services failed to initialize"); console.warn("Encryption services failed to initialize");
this.encryption = null; this.endToEndEncryption = null;
} else { } else {
console.log("Encryption services initialized"); console.log("Encryption services initialized");
this.encryption = encryption; this.endToEndEncryption = endToEndEncryption;
} }
this.iids= 0; this.iids= 0;
@ -118,11 +118,11 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
Api.prototype.sendEncrypted = function(type, data, noqueue) { Api.prototype.sendEncrypted = function(type, data, noqueue) {
var to = data.To; var to = data.To;
if (!to || !this.encryption) { if (!to || !this.endToEndEncryption) {
return this.send(type, data, noqueue); return this.send(type, data, noqueue);
} }
this.encryption.encrypt(to, type, data, _.bind(function(type, encrypted) { this.endToEndEncryption.encrypt(to, type, data, _.bind(function(type, encrypted) {
encrypted.To = to encrypted.To = to
this.send(type, encrypted, noqueue); this.send(type, encrypted, noqueue);
}, this)); }, this));
@ -152,11 +152,11 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
if (!obj.hasOwnProperty("sendEncrypted")) { if (!obj.hasOwnProperty("sendEncrypted")) {
obj.sendEncrypted = _.bind(function(type, data) { obj.sendEncrypted = _.bind(function(type, data) {
var to = data.To; var to = data.To;
if (!to || !this.encryption) { if (!to || !this.endToEndEncryption) {
return obj.send(type, data, type, data); return obj.send(type, data, type, data);
} }
this.encryption.encrypt(to, type, data, _.bind(function(encryptedType, encrypted) { this.endToEndEncryption.encrypt(to, type, data, _.bind(function(encryptedType, encrypted) {
encrypted.To = to encrypted.To = to
encrypted.Type = encryptedType encrypted.Type = encryptedType
obj.send(encryptedType, encrypted, type, data); obj.send(encryptedType, encrypted, type, data);
@ -190,11 +190,11 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
} }
if (dataType === "Encrypted") { if (dataType === "Encrypted") {
if (!this.encryption) { if (!this.endToEndEncryption) {
console.log("Encryption is not supported, can't handle", data); console.log("Encryption is not supported, can't handle", data);
return; return;
} }
this.encryption.decrypt(d.From, data, _.bind(function(decrypted) { this.endToEndEncryption.decrypt(d.From, data, _.bind(function(decrypted) {
this.processReceived(d, decrypted.Type, decrypted[decrypted.Type]); this.processReceived(d, decrypted.Type, decrypted[decrypted.Type]);
}, this)); }, this));
return; return;

4
static/js/services/api.js

@ -23,7 +23,7 @@
define([ define([
'mediastream/api' 'mediastream/api'
], function(Api) { ], function(Api) {
return ["globalContext", "connector", "encryption", function(context, connector, encryption) { return ["globalContext", "connector", "endToEndEncryption", function(context, connector, endToEndEncryption) {
return new Api(context.Cfg.Version, connector, encryption); return new Api(context.Cfg.Version, connector, endToEndEncryption);
}]; }];
}); });

76
static/js/services/encryption.js → static/js/services/endtoendencryption.js

@ -115,13 +115,13 @@ define([
return JSON.stringify(data); return JSON.stringify(data);
}; };
var LocalStorageDataStore = function(encryption) { var LocalStorageDataStore = function(endToEndEncryption) {
this.e = $({}); this.e = $({});
this.encryption = encryption; this.endToEndEncryption = endToEndEncryption;
this.userid = null; this.userid = null;
this.id = encryption.api.sid; this.id = endToEndEncryption.api.sid;
this.anonymous_storage = {}; this.anonymous_storage = {};
encryption.api.e.on("received.self", _.bind(function(event, data) { endToEndEncryption.api.e.on("received.self", _.bind(function(event, data) {
var userid = data.Suserid || null; var userid = data.Suserid || null;
if (this.id === (userid || data.Sid) && this.userid === userid) { if (this.id === (userid || data.Sid) && this.userid === userid) {
return; return;
@ -132,7 +132,7 @@ define([
this.userid = userid; this.userid = userid;
this.anonymous_storage = {}; this.anonymous_storage = {};
} }
this.encryption.resetStore(); this.endToEndEncryption.resetStore();
if (initial) { if (initial) {
this.e.triggerHandler("ready"); this.e.triggerHandler("ready");
} }
@ -231,7 +231,7 @@ define([
return this.save("signed_pre_key_" + this.id + "_" + key.id, data); return this.save("signed_pre_key_" + this.id + "_" + key.id, data);
}; };
var Encryption = function(api) { var EndToEndEncryption = function(api) {
this.e = $({}); this.e = $({});
this.api = api; this.api = api;
// TODO(fancycode): Look into using IndexedDB as storage backend. // TODO(fancycode): Look into using IndexedDB as storage backend.
@ -271,11 +271,11 @@ define([
this.resetStore(); this.resetStore();
}; };
Encryption.prototype.isSupported = function() { EndToEndEncryption.prototype.isSupported = function() {
return !!this.store; return !!this.store;
}; };
Encryption.prototype.resetStore = function() { EndToEndEncryption.prototype.resetStore = function() {
// TODO(fancycode): We should process any pending messages before // TODO(fancycode): We should process any pending messages before
// resetting everything. // resetting everything.
this.identity_keypair = null; this.identity_keypair = null;
@ -292,11 +292,11 @@ define([
} }
}; };
Encryption.prototype.getIdentityFingerprint = function() { EndToEndEncryption.prototype.getIdentityFingerprint = function() {
return this.formatIdentityFingerprint(this.identity_keypair.public); return this.formatIdentityFingerprint(this.identity_keypair.public);
}; };
Encryption.prototype.formatIdentityFingerprint = function(public_key) { EndToEndEncryption.prototype.formatIdentityFingerprint = function(public_key) {
// TODO(jojo): Change this to be the SHA-1 hash of a three-byte key // TODO(jojo): Change this to be the SHA-1 hash of a three-byte key
// id and the public key. // id and the public key.
// See https://github.com/WhisperSystems/TextSecure/blob/08ed90c5ece49c92e35c492afb4e60160983015a/src/org/thoughtcrime/securesms/crypto/PublicKey.java#L95 // See https://github.com/WhisperSystems/TextSecure/blob/08ed90c5ece49c92e35c492afb4e60160983015a/src/org/thoughtcrime/securesms/crypto/PublicKey.java#L95
@ -309,7 +309,7 @@ define([
return fingerprint.substr(2); return fingerprint.substr(2);
}; };
Encryption.prototype.storePeerIdentity = function(peer, public_key) { EndToEndEncryption.prototype.storePeerIdentity = function(peer, public_key) {
var fingerprint = this.formatIdentityFingerprint(public_key); var fingerprint = this.formatIdentityFingerprint(public_key);
var existing = this.peer_identities[peer]; var existing = this.peer_identities[peer];
if (existing === fingerprint) { if (existing === fingerprint) {
@ -324,7 +324,7 @@ define([
this.peer_identities[peer] = fingerprint; this.peer_identities[peer] = fingerprint;
}; };
Encryption.prototype.getReadyPromise = function() { EndToEndEncryption.prototype.getReadyPromise = function() {
if (!this.ready_promise) { if (!this.ready_promise) {
// Load/create initial objects // Load/create initial objects
this.ready_promise = this.loadData().then(_.bind(function() { this.ready_promise = this.loadData().then(_.bind(function() {
@ -356,7 +356,7 @@ define([
return this.ready_promise; return this.ready_promise;
}; };
Encryption.prototype.loadData = function() { EndToEndEncryption.prototype.loadData = function() {
var deferred = $q.defer(); var deferred = $q.defer();
var doLoadData = _.bind(function() { var doLoadData = _.bind(function() {
this.identity_keypair = this.store.loadKeypair(); this.identity_keypair = this.store.loadKeypair();
@ -378,7 +378,7 @@ define([
return deferred.promise; return deferred.promise;
}; };
Encryption.prototype.getLocalIdentityKeyPair = function() { EndToEndEncryption.prototype.getLocalIdentityKeyPair = function() {
var deferred = $q.defer(); var deferred = $q.defer();
_.defer(_.bind(function() { _.defer(_.bind(function() {
if (this.identity_keypair) { if (this.identity_keypair) {
@ -398,7 +398,7 @@ define([
return deferred.promise; return deferred.promise;
}; };
Encryption.prototype.getLocalRegistrationId = function() { EndToEndEncryption.prototype.getLocalRegistrationId = function() {
var deferred = $q.defer(); var deferred = $q.defer();
_.defer(_.bind(function() { _.defer(_.bind(function() {
if (this.registration_id) { if (this.registration_id) {
@ -417,7 +417,7 @@ define([
return deferred.promise; return deferred.promise;
}; };
Encryption.prototype.getLastResortPreKey = function() { EndToEndEncryption.prototype.getLastResortPreKey = function() {
var deferred = $q.defer(); var deferred = $q.defer();
_.defer(_.bind(function() { _.defer(_.bind(function() {
if (this.last_resort_pre_key) { if (this.last_resort_pre_key) {
@ -437,7 +437,7 @@ define([
return deferred.promise; return deferred.promise;
}; };
Encryption.prototype.getLocalSignedPreKeyPair = function(id) { EndToEndEncryption.prototype.getLocalSignedPreKeyPair = function(id) {
var deferred = $q.defer(); var deferred = $q.defer();
_.defer(_.bind(function() { _.defer(_.bind(function() {
var signed_pre_key; var signed_pre_key;
@ -451,7 +451,7 @@ define([
return deferred.promise; return deferred.promise;
}; };
Encryption.prototype.getSignedPreKey = function(id) { EndToEndEncryption.prototype.getSignedPreKey = function(id) {
if (!id) { if (!id) {
id = this.next_signed_pre_key_id; id = this.next_signed_pre_key_id;
this.next_signed_pre_key_id++; this.next_signed_pre_key_id++;
@ -475,7 +475,7 @@ define([
return deferred.promise; return deferred.promise;
}; };
Encryption.prototype.apiSend = function(type, message) { EndToEndEncryption.prototype.apiSend = function(type, message) {
var msg = { var msg = {
"Type": type "Type": type
}; };
@ -483,7 +483,7 @@ define([
this.api.send(type, message); this.api.send(type, message);
}; };
Encryption.prototype.register = function() { EndToEndEncryption.prototype.register = function() {
this.getReadyPromise().then(_.bind(function() { this.getReadyPromise().then(_.bind(function() {
var message = { var message = {
"RegistrationId": this.registration_id, "RegistrationId": this.registration_id,
@ -500,7 +500,7 @@ define([
}, this)); }, this));
}; };
Encryption.prototype.processPendingMessages = function(peer) { EndToEndEncryption.prototype.processPendingMessages = function(peer) {
var pending = this.pending_messages[peer]; var pending = this.pending_messages[peer];
if (pending && pending.length > 0) { if (pending && pending.length > 0) {
var msg = pending.shift(); var msg = pending.shift();
@ -526,7 +526,7 @@ define([
} }
}; };
Encryption.prototype.requestKeyBundle = function(peer) { EndToEndEncryption.prototype.requestKeyBundle = function(peer) {
this.getSignedPreKey().then(_.bind(function(signed_pre_key) { this.getSignedPreKey().then(_.bind(function(signed_pre_key) {
var bundle = { var bundle = {
"To": peer, "To": peer,
@ -543,7 +543,7 @@ define([
}, this)); }, this));
}; };
Encryption.prototype.receivedKeyBundle = function(peer, data) { EndToEndEncryption.prototype.receivedKeyBundle = function(peer, data) {
var bundle = { var bundle = {
"identityKey": deserializePublicKey(data.Identity), "identityKey": deserializePublicKey(data.Identity),
"preKeyId": null, "preKeyId": null,
@ -571,7 +571,7 @@ define([
}, this)); }, this));
}; };
Encryption.prototype.encrypt = function(peer, type, message, callback) { EndToEndEncryption.prototype.encrypt = function(peer, type, message, callback) {
var msg = {"Type": type}; var msg = {"Type": type};
msg[type] = message; msg[type] = message;
message = JSON.stringify(msg); message = JSON.stringify(msg);
@ -599,7 +599,7 @@ define([
this.encryptWithSession(peer, session, message, callback); this.encryptWithSession(peer, session, message, callback);
}; };
Encryption.prototype.encryptWithSession = function( EndToEndEncryption.prototype.encryptWithSession = function(
peer, session, message, callback) { peer, session, message, callback) {
var buffer = ByteBuffer.fromUTF8(message).toArrayBuffer(); var buffer = ByteBuffer.fromUTF8(message).toArrayBuffer();
// TODO(fancycode): Add error handling. // TODO(fancycode): Add error handling.
@ -617,7 +617,7 @@ define([
}, this)); }, this));
}; };
Encryption.prototype.decrypt = function(peer, message, callback) { EndToEndEncryption.prototype.decrypt = function(peer, message, callback) {
if (this.is_processing_messages[peer]) { if (this.is_processing_messages[peer]) {
this.pending_messages[peer].push({ this.pending_messages[peer].push({
"type": "decrypt", "type": "decrypt",
@ -632,7 +632,7 @@ define([
this.decryptWithSession(peer, session, message, callback); this.decryptWithSession(peer, session, message, callback);
}; };
Encryption.prototype.decryptWithSession = function( EndToEndEncryption.prototype.decryptWithSession = function(
peer, session, message, callback) { peer, session, message, callback) {
var handle_decrypted = _.bind(function(decrypted) { var handle_decrypted = _.bind(function(decrypted) {
if (decrypted.identityKey) { if (decrypted.identityKey) {
@ -662,38 +662,38 @@ define([
} }
}; };
var encryption; var endToEndEncryption;
// Only export limited public encryption API. // Only export limited public encryption API.
var encryptionApi = { var endToEndEncryptionApi = {
"initialize": function(api) { "initialize": function(api) {
if (encryption) { if (endToEndEncryption) {
return true; return true;
} }
encryption = new Encryption(api); endToEndEncryption = new EndToEndEncryption(api);
if (!encryption.isSupported()) { if (!endToEndEncryption.isSupported()) {
encryption = null; endToEndEncryption = null;
return false; return false;
} }
return true; return true;
}, },
"encrypt": function(peer, type, message, callback) { "encrypt": function(peer, type, message, callback) {
if (!encryption) { if (!endToEndEncryption) {
callback(type, message); callback(type, message);
return; return;
} }
encryption.encrypt(peer, type, message, callback); endToEndEncryption.encrypt(peer, type, message, callback);
}, },
"decrypt": function(peer, message, callback) { "decrypt": function(peer, message, callback) {
if (!encryption) { if (!endToEndEncryption) {
callback(message); callback(message);
return; return;
} }
encryption.decrypt(peer, message, callback); endToEndEncryption.decrypt(peer, message, callback);
} }
}; };
return encryptionApi; return endToEndEncryptionApi;
}]; }];

6
static/js/services/services.js

@ -71,7 +71,7 @@ define([
'services/sandbox', 'services/sandbox',
'services/dummystream', 'services/dummystream',
'services/usermedia', 'services/usermedia',
'services/encryption'], function(_, 'services/endtoendencryption'], function(_,
desktopNotify, desktopNotify,
playSound, playSound,
safeApply, safeApply,
@ -120,7 +120,7 @@ mediaDevices,
sandbox, sandbox,
dummyStream, dummyStream,
userMedia, userMedia,
encryption) { endToEndEncryption) {
var services = { var services = {
desktopNotify: desktopNotify, desktopNotify: desktopNotify,
@ -171,7 +171,7 @@ encryption) {
sandbox: sandbox, sandbox: sandbox,
dummyStream: dummyStream, dummyStream: dummyStream,
userMedia: userMedia, userMedia: userMedia,
encryption: encryption endToEndEncryption: endToEndEncryption
}; };
var initialize = function(angModule) { var initialize = function(angModule) {

Loading…
Cancel
Save