-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NEW] Support for end to end encryption (#10094)
- Loading branch information
1 parent
a4ce812
commit 808619b
Showing
44 changed files
with
1,251 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,6 +146,7 @@ rocketchat:[email protected] | |
rocketchat:[email protected] | ||
rocketchat:[email protected] | ||
rocketchat:[email protected] | ||
rocketchat:[email protected] | ||
rocketchat:[email protected] | ||
rocketchat:[email protected] | ||
rocketchat:[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
RocketChat.API.v1.addRoute('e2e.fetchKeychain', { authRequired: true }, { | ||
get() { | ||
const { uid } = this.queryParams; | ||
|
||
let result; | ||
Meteor.runAsUser(this.userId, () => result = Meteor.call('fetchKeychain', uid)); | ||
|
||
return RocketChat.API.v1.success(result); | ||
}, | ||
}); | ||
|
||
RocketChat.API.v1.addRoute('e2e.fetchMyKeys', { authRequired: true }, { | ||
get() { | ||
let result; | ||
Meteor.runAsUser(this.userId, () => result = Meteor.call('fetchMyKeys')); | ||
|
||
return RocketChat.API.v1.success(result); | ||
}, | ||
}); | ||
|
||
RocketChat.API.v1.addRoute('e2e.addKeyToChain', { authRequired: true }, { | ||
post() { | ||
const { RSAPubKey, RSAEPrivKey } = this.bodyParams; | ||
|
||
Meteor.runAsUser(this.userId, () => { | ||
RocketChat.API.v1.success(Meteor.call('addKeyToChain', { | ||
public_key: RSAPubKey, | ||
private_key: RSAEPrivKey, | ||
})); | ||
}); | ||
|
||
return RocketChat.API.v1.success(); | ||
}, | ||
}); | ||
|
||
RocketChat.API.v1.addRoute('e2e.updateGroupE2EKey', { authRequired: true }, { | ||
post() { | ||
const { uid, rid, key } = this.bodyParams; | ||
|
||
Meteor.runAsUser(this.userId, () => { | ||
RocketChat.API.v1.success(Meteor.call('updateGroupE2EKey', rid, uid, key)); | ||
}); | ||
|
||
return RocketChat.API.v1.success(); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": ["@rocket.chat/eslint-config"], | ||
"root": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* eslint-disable new-cap, no-proto */ | ||
|
||
import ByteBuffer from 'bytebuffer'; | ||
|
||
const StaticArrayBufferProto = new ArrayBuffer().__proto__; | ||
|
||
export function toString(thing) { | ||
if (typeof thing === 'string') { | ||
return thing; | ||
} | ||
return new ByteBuffer.wrap(thing).toString('binary'); | ||
} | ||
|
||
export function toArrayBuffer(thing) { | ||
if (thing === undefined) { | ||
return undefined; | ||
} | ||
if (thing === Object(thing)) { | ||
if (thing.__proto__ === StaticArrayBufferProto) { | ||
return thing; | ||
} | ||
} | ||
|
||
if (typeof thing !== 'string') { | ||
throw new Error(`Tried to convert a non-string of type ${ typeof thing } to an array buffer`); | ||
} | ||
return new ByteBuffer.wrap(thing, 'binary').toArrayBuffer(); | ||
} | ||
|
||
export function joinVectorAndEcryptedData(vector, encryptedData) { | ||
const cipherText = new Uint8Array(encryptedData); | ||
const output = new Uint8Array(vector.length + cipherText.length); | ||
output.set(vector, 0); | ||
output.set(cipherText, vector.length); | ||
return output; | ||
} | ||
|
||
export function splitVectorAndEcryptedData(cipherText) { | ||
const vector = cipherText.slice(0, 16); | ||
const encryptedData = cipherText.slice(16); | ||
|
||
return [vector, encryptedData]; | ||
} | ||
|
||
export async function encryptRSA(key, data) { | ||
return await crypto.subtle.encrypt({ name: 'RSA-OAEP' }, key, data); | ||
} | ||
|
||
export async function encryptAES(vector, key, data) { | ||
return await crypto.subtle.encrypt({ name: 'AES-CBC', iv: vector }, key, data); | ||
} | ||
|
||
export async function decryptRSA(key, data) { | ||
return await crypto.subtle.decrypt({ name: 'RSA-OAEP' }, key, data); | ||
} | ||
|
||
export async function decryptAES(vector, key, data) { | ||
return await crypto.subtle.decrypt({ name: 'AES-CBC', iv: vector }, key, data); | ||
} | ||
|
||
export async function generateAESKey() { | ||
return await crypto.subtle.generateKey({ name: 'AES-CBC', length: 128 }, true, ['encrypt', 'decrypt']); | ||
} | ||
|
||
export async function generateRSAKey() { | ||
return await crypto.subtle.generateKey({ name: 'RSA-OAEP', modulusLength: 2048, publicExponent: new Uint8Array([0x01, 0x00, 0x01]), hash: { name: 'SHA-256' } }, true, ['encrypt', 'decrypt']); | ||
} | ||
|
||
export async function exportJWKKey(key) { | ||
return await crypto.subtle.exportKey('jwk', key); | ||
} | ||
|
||
export async function importRSAKey(keyData, keyUsages = ['encrypt', 'decrypt']) { | ||
return await crypto.subtle.importKey('jwk', keyData, { name: 'RSA-OAEP', modulusLength: 2048, publicExponent: new Uint8Array([0x01, 0x00, 0x01]), hash: { name: 'SHA-256' } }, true, keyUsages); | ||
} | ||
|
||
export async function importAESKey(keyData, keyUsages = ['encrypt', 'decrypt']) { | ||
return await crypto.subtle.importKey('jwk', keyData, { name: 'AES-CBC' }, true, keyUsages); | ||
} | ||
|
||
export async function importRawKey(keyData, keyUsages = ['deriveKey']) { | ||
return await crypto.subtle.importKey('raw', keyData, { name: 'PBKDF2' }, false, keyUsages); | ||
} | ||
|
||
export async function deriveKey(salt, baseKey, keyUsages = ['encrypt', 'decrypt']) { | ||
const iterations = 1000; | ||
const hash = 'SHA-256'; | ||
|
||
return await crypto.subtle.deriveKey({ name: 'PBKDF2', salt, iterations, hash }, baseKey, { name: 'AES-CBC', length: 256 }, true, keyUsages); | ||
} | ||
|
||
export async function readFileAsArrayBuffer(file) { | ||
return new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.onload = function(evt) { | ||
resolve(evt.target.result); | ||
}; | ||
reader.onerror = function(evt) { | ||
reject(evt); | ||
}; | ||
reader.readAsArrayBuffer(file); | ||
}); | ||
} | ||
|
||
export class Deferred { | ||
constructor() { | ||
const p = new Promise((resolve, reject) => { | ||
this.resolve = resolve; | ||
this.reject = reject; | ||
}); | ||
|
||
p.resolve = this.resolve; | ||
p.reject = this.reject; | ||
|
||
return p; | ||
} | ||
} |
Oops, something went wrong.