Add initial OTR code.

- /otr will generate a new private key (or fetch it from storage).
This commit is contained in:
JC Brand 2013-08-16 15:48:23 +02:00
parent 57fc8b9832
commit a2aeec8449
3 changed files with 61 additions and 8 deletions

View File

@ -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": {}
}

View File

@ -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()

View File

@ -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: '_' },