From a2aeec8449fe406b00477368c311cf1e047380e5 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Fri, 16 Aug 2013 15:48:23 +0200 Subject: [PATCH] Add initial OTR code. - /otr will generate a new private key (or fetch it from storage). --- bower.json | 4 ++-- converse.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++++----- main.js | 6 +++++- 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/bower.json b/bower.json index 3770d7f46..4d50d1115 100644 --- a/bower.json +++ b/bower.json @@ -7,7 +7,6 @@ "dependencies": { "requirejs": "2.1.8", "jquery": "1.8.3", - "sjcl": "git://github.com/bitwiseshiftleft/sjcl.git", "jed": "0.5.4", "tinysort": "git://github.com/Sjeiti/TinySort.git", "underscore": "1.5.1", @@ -18,7 +17,8 @@ "strophe.vcard": "https://raw.github.com/jcbrand/strophejs-plugins/75c8693992bc357c699b6d615eeb396e799f5c02/vcard/strophe.vcard.js", "strophe.disco": "https://raw.github.com/jcbrand/strophejs-plugins/75c8693992bc357c699b6d615eeb396e799f5c02/disco/strophe.disco.js", "strophe.muc": "https://raw.github.com/jcbrand/strophejs-plugins/75c8693992bc357c699b6d615eeb396e799f5c02/muc/strophe.muc.js", - "otr": "~0.2.1" + "otr": "~0.2.1", + "crypto-js": "~3.1.2" }, "exportsOverride": {} } diff --git a/converse.js b/converse.js index f7d876293..641ff1b06 100644 --- a/converse.js +++ b/converse.js @@ -14,22 +14,22 @@ if (typeof define === 'function' && define.amd) { define("converse", [ "components/otr/build/otr", + "crypto.aes", "locales", "localstorage", "tinysort", - "sjcl", "strophe", "strophe.muc", "strophe.roster", "strophe.vcard", "strophe.disco" - ], function(otr) { + ], function(otr, crypto) { // Use Mustache style syntax for variable interpolation _.templateSettings = { evaluate : /\{\[([\s\S]+?)\]\}/g, interpolate : /\{\{([\s\S]+?)\}\}/g }; - return factory(jQuery, _, otr, console); + return factory(jQuery, _, crypto, otr, console); } ); } else { @@ -38,9 +38,9 @@ evaluate : /\{\[([\s\S]+?)\]\}/g, interpolate : /\{\{([\s\S]+?)\}\}/g }; - root.converse = factory(jQuery, _, otr, console || {log: function(){}}); + root.converse = factory(jQuery, _, crypto, otr, console || {log: function(){}}); } -}(this, function ($, _, otr, console) { +}(this, function ($, _, crypto, otr, console) { var converse = {}; converse.initialize = function (settings) { // Default values @@ -206,6 +206,40 @@ } }, + getPrivateKey: function () { + var savedKey = this.get('priv_key'); + var myKey, decrypted, ciphertextParams; + if (savedKey) { + decrypted = crypto.lib.PasswordBasedCipher.decrypt(crypto.algo.AES, savedKey, converse.connection.pass); + myKey = otr.DSA.parsePrivate(decrypted.toString(crypto.enc.Latin1)); + } else { + myKey = new otr.DSA(); + ciphertextParams = crypto.lib.PasswordBasedCipher.encrypt(crypto.algo.AES, myKey.packPrivate(), converse.connection.pass); + this.save({'priv_key': ciphertextParams.toString()}); + } + return myKey; + }, + + initiateOTR: function (myKey) { + var options = { + fragment_size: 140, + send_interval: 200, + priv: myKey, + debug: true + }; + var contact = new otr.OTR(options); + + contact.on('ui', function (msg) { + console.log("message to display to the user:"+msg); + }); + contact.on('io', function (msg) { + console.log("message to display to the user:"+msg); + }); + contact.on('error', function (msg) { + console.log("message to display to the user:"+msg); + }); + }, + messageReceived: function (message) { var $message = $(message), body = converse.autoLink($message.children('body').text()), @@ -382,6 +416,21 @@ this.addHelpMessages(msgs); return; } + else if (match[1] === "otr") { + msgs = [ + __('Initializing OTR.'), + __('Generating private key'), + __('...this might take a few seconds.') + ]; + this.addHelpMessages(msgs); + var privKey = this.model.getPrivateKey(); + msgs = [ + __('Private key generated.') + ]; + this.addHelpMessages(msgs); + this.model.initiateOTR(privKey); + return; + } } var message = $msg({from: converse.connection.jid, to: bare_jid, type: 'chat', id: timestamp}) .c('body').t(text).up() diff --git a/main.js b/main.js index e752e5962..37a9a32c1 100644 --- a/main.js +++ b/main.js @@ -11,7 +11,8 @@ require.config({ "strophe.muc": "components/strophe.muc/index", "strophe.roster": "components/strophe.roster/index", "strophe.vcard": "components/strophe.vcard/index", - "strophe.disco": "components/strophe.disco/index" + "strophe.disco": "components/strophe.disco/index", + "crypto.aes": "components/crypto-js/build/rollups/aes" }, // define module dependencies for modules not using define @@ -27,6 +28,9 @@ require.config({ //module value. exports: 'Backbone' }, + 'crypto.aes': { + exports: 'CryptoJS' + }, 'tinysort': { deps: ['jquery'] }, 'strophe': { deps: ['jquery'] }, 'underscore': { exports: '_' },