Attempt to fix #2528 by accepting webkitSubtle as crypto.

pull/2552/head^2
Marcelo Schmidt 10 years ago
parent 1a6c9644e8
commit 03b045fbcc
  1. 16
      packages/rocketchat-otr/client/rocketchat.otr.room.js
  2. 3
      packages/rocketchat-otr/client/tabBar.js

@ -66,12 +66,12 @@ RocketChat.OTR.Room = class {
});
// Generate an ephemeral key pair.
return window.crypto.subtle.generateKey({
return RocketChat.OTR.crypto.generateKey({
name: 'ECDH',
namedCurve: 'P-256'
}, false, ['deriveKey', 'deriveBits']).then((keyPair) => {
this.keyPair = keyPair;
return crypto.subtle.exportKey('jwk', keyPair.publicKey);
return RocketChat.OTR.crypto.exportKey('jwk', keyPair.publicKey);
})
.then((exportedPublicKey) => {
this.exportedPublicKey = exportedPublicKey;
@ -85,23 +85,23 @@ RocketChat.OTR.Room = class {
}
importPublicKey(publicKey) {
return window.crypto.subtle.importKey('jwk', EJSON.parse(publicKey), {
return RocketChat.OTR.crypto.importKey('jwk', EJSON.parse(publicKey), {
name: 'ECDH',
namedCurve: 'P-256'
}, false, []).then((peerPublicKey) => {
return crypto.subtle.deriveBits({
return RocketChat.OTR.crypto.deriveBits({
name: 'ECDH',
namedCurve: 'P-256',
public: peerPublicKey
}, this.keyPair.privateKey, 256);
}).then((bits) => {
return crypto.subtle.digest({
return RocketChat.OTR.crypto.digest({
name: 'SHA-256'
}, bits);
}).then((hashedBits) => {
// We truncate the hash to 128 bits.
var sessionKeyData = new Uint8Array(hashedBits).slice(0, 16);
return crypto.subtle.importKey('raw', sessionKeyData, {
return RocketChat.OTR.crypto.importKey('raw', sessionKeyData, {
name: 'AES-GCM'
}, false, ['encrypt', 'decrypt']);
}).then((sessionKey) => {
@ -116,7 +116,7 @@ RocketChat.OTR.Room = class {
}
var iv = crypto.getRandomValues(new Uint8Array(12));
return crypto.subtle.encrypt({
return RocketChat.OTR.crypto.encrypt({
name: 'AES-GCM',
iv: iv
}, this.sessionKey, data).then((cipherText) => {
@ -141,7 +141,7 @@ RocketChat.OTR.Room = class {
var iv = cipherText.slice(0, 12);
cipherText = cipherText.slice(12);
return crypto.subtle.decrypt({
return RocketChat.OTR.crypto.decrypt({
name: 'AES-GCM',
iv: iv
}, this.sessionKey, cipherText).then((data) => {

@ -1,6 +1,7 @@
Meteor.startup(function() {
Tracker.autorun(function() {
if (RocketChat.settings.get('OTR_Enable') && window.crypto && window.crypto.subtle) {
if (RocketChat.settings.get('OTR_Enable') && window.crypto) {
RocketChat.OTR.crypto = window.crypto.subtle || window.crypto.webkitSubtle;
RocketChat.OTR.enabled.set(true);
RocketChat.TabBar.addButton({
groups: ['directmessage'],

Loading…
Cancel
Save