2020-09-11 14:31:38 +00:00
|
|
|
import EJSON from 'ejson';
|
|
|
|
import { Base64 } from 'js-base64';
|
|
|
|
import SimpleCrypto from 'react-native-simple-crypto';
|
2022-02-16 21:14:28 +00:00
|
|
|
import ByteBuffer from 'bytebuffer';
|
2020-09-11 14:31:38 +00:00
|
|
|
|
2022-02-16 21:14:28 +00:00
|
|
|
import { IMessage } from '../../definitions';
|
2021-09-13 20:41:05 +00:00
|
|
|
import RocketChat from '../rocketchat';
|
|
|
|
import Deferred from '../../utils/deferred';
|
|
|
|
import debounce from '../../utils/debounce';
|
|
|
|
import database from '../database';
|
|
|
|
import log from '../../utils/log';
|
2022-04-04 19:15:29 +00:00
|
|
|
import { E2E_MESSAGE_TYPE, E2E_STATUS } from '../constants';
|
2020-09-11 14:31:38 +00:00
|
|
|
import {
|
|
|
|
b64ToBuffer,
|
|
|
|
bufferToB64,
|
|
|
|
bufferToB64URI,
|
2021-09-13 20:41:05 +00:00
|
|
|
bufferToUtf8,
|
|
|
|
joinVectorData,
|
2020-09-11 14:31:38 +00:00
|
|
|
splitVectorData,
|
2021-09-13 20:41:05 +00:00
|
|
|
toString,
|
|
|
|
utf8ToBuffer
|
2020-09-11 14:31:38 +00:00
|
|
|
} from './utils';
|
|
|
|
import { Encryption } from './index';
|
2022-02-16 21:14:28 +00:00
|
|
|
import { IUser } from '../../definitions/IUser';
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
export default class EncryptionRoom {
|
2022-02-16 21:14:28 +00:00
|
|
|
ready: boolean;
|
|
|
|
roomId: string;
|
|
|
|
userId: string;
|
|
|
|
establishing: boolean;
|
|
|
|
readyPromise: Deferred;
|
|
|
|
sessionKeyExportedString: string | ByteBuffer;
|
|
|
|
keyID: string;
|
|
|
|
roomKey: ArrayBuffer;
|
|
|
|
|
|
|
|
constructor(roomId: string, userId: string) {
|
2020-09-11 14:31:38 +00:00
|
|
|
this.ready = false;
|
|
|
|
this.roomId = roomId;
|
2020-09-24 18:34:13 +00:00
|
|
|
this.userId = userId;
|
2020-09-11 14:31:38 +00:00
|
|
|
this.establishing = false;
|
2022-02-16 21:14:28 +00:00
|
|
|
this.keyID = '';
|
|
|
|
this.sessionKeyExportedString = '';
|
|
|
|
this.roomKey = new ArrayBuffer(0);
|
2020-09-11 14:31:38 +00:00
|
|
|
this.readyPromise = new Deferred();
|
|
|
|
this.readyPromise.then(() => {
|
|
|
|
// Mark as ready
|
|
|
|
this.ready = true;
|
|
|
|
// Mark as established
|
|
|
|
this.establishing = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the E2E room
|
2021-09-13 20:41:05 +00:00
|
|
|
handshake = async () => {
|
2020-09-11 14:31:38 +00:00
|
|
|
// If it's already ready we don't need to handshake again
|
|
|
|
if (this.ready) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's already establishing
|
|
|
|
if (this.establishing) {
|
|
|
|
// Return the ready promise to wait this client ready
|
|
|
|
return this.readyPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const subCollection = db.get('subscriptions');
|
2020-09-11 14:31:38 +00:00
|
|
|
try {
|
|
|
|
// Find the subscription
|
|
|
|
const subscription = await subCollection.find(this.roomId);
|
|
|
|
|
|
|
|
const { E2EKey, e2eKeyId } = subscription;
|
|
|
|
|
|
|
|
// If this room has a E2EKey, we import it
|
2022-02-16 21:14:28 +00:00
|
|
|
if (E2EKey && Encryption.privateKey) {
|
2020-09-11 14:31:38 +00:00
|
|
|
// We're establishing a new room encryption client
|
|
|
|
this.establishing = true;
|
|
|
|
await this.importRoomKey(E2EKey, Encryption.privateKey);
|
|
|
|
this.readyPromise.resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it doesn't have a e2eKeyId, we need to create keys to the room
|
|
|
|
if (!e2eKeyId) {
|
|
|
|
// We're establishing a new room encryption client
|
|
|
|
this.establishing = true;
|
|
|
|
await this.createRoomKey();
|
|
|
|
this.readyPromise.resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request a E2EKey for this room to other users
|
|
|
|
await this.requestRoomKey(e2eKeyId);
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Import roomKey as an AES Decrypt key
|
2022-02-16 21:14:28 +00:00
|
|
|
importRoomKey = async (E2EKey: string, privateKey: string) => {
|
2020-09-11 14:31:38 +00:00
|
|
|
const roomE2EKey = E2EKey.slice(12);
|
|
|
|
|
|
|
|
const decryptedKey = await SimpleCrypto.RSA.decrypt(roomE2EKey, privateKey);
|
|
|
|
this.sessionKeyExportedString = toString(decryptedKey);
|
|
|
|
|
2022-02-16 21:14:28 +00:00
|
|
|
this.keyID = Base64.encode(this.sessionKeyExportedString as string).slice(0, 12);
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Extract K from Web Crypto Secret Key
|
|
|
|
// K is a base64URL encoded array of bytes
|
|
|
|
// Web Crypto API uses this as a private key to decrypt/encrypt things
|
|
|
|
// Reference: https://www.javadoc.io/doc/com.nimbusds/nimbus-jose-jwt/5.1/com/nimbusds/jose/jwk/OctetSequenceKey.html
|
2022-02-16 21:14:28 +00:00
|
|
|
const { k } = EJSON.parse(this.sessionKeyExportedString as string);
|
2020-09-11 14:31:38 +00:00
|
|
|
this.roomKey = b64ToBuffer(k);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Create a key to a room
|
2021-09-13 20:41:05 +00:00
|
|
|
createRoomKey = async () => {
|
2022-02-16 21:14:28 +00:00
|
|
|
const key = (await SimpleCrypto.utils.randomBytes(16)) as Uint8Array;
|
2020-09-11 14:31:38 +00:00
|
|
|
this.roomKey = key;
|
|
|
|
|
|
|
|
// Web Crypto format of a Secret Key
|
|
|
|
const sessionKeyExported = {
|
|
|
|
// Type of Secret Key
|
|
|
|
kty: 'oct',
|
|
|
|
// Algorithm
|
|
|
|
alg: 'A128CBC',
|
|
|
|
// Base64URI encoded array of bytes
|
|
|
|
k: bufferToB64URI(this.roomKey),
|
|
|
|
// Specific Web Crypto properties
|
|
|
|
ext: true,
|
|
|
|
key_ops: ['encrypt', 'decrypt']
|
|
|
|
};
|
|
|
|
|
|
|
|
this.sessionKeyExportedString = EJSON.stringify(sessionKeyExported);
|
|
|
|
this.keyID = Base64.encode(this.sessionKeyExportedString).slice(0, 12);
|
|
|
|
|
|
|
|
await RocketChat.e2eSetRoomKeyID(this.roomId, this.keyID);
|
|
|
|
|
|
|
|
await this.encryptRoomKey();
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Request a key to this room
|
|
|
|
// We're debouncing this function to avoid multiple calls
|
|
|
|
// when you join a room with a lot of messages and nobody
|
|
|
|
// can send the encryption key at the moment.
|
|
|
|
// Each time you see a encrypted message of a room that you don't have a key
|
|
|
|
// this will be called again and run once in 5 seconds
|
2021-09-13 20:41:05 +00:00
|
|
|
requestRoomKey = debounce(
|
2022-02-16 21:14:28 +00:00
|
|
|
async (e2eKeyId: string) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
await RocketChat.e2eRequestRoomKey(this.roomId, e2eKeyId);
|
|
|
|
},
|
|
|
|
5000,
|
|
|
|
true
|
|
|
|
);
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Create an encrypted key for this room based on users
|
2021-09-13 20:41:05 +00:00
|
|
|
encryptRoomKey = async () => {
|
2020-09-11 14:31:38 +00:00
|
|
|
const result = await RocketChat.e2eGetUsersOfRoomWithoutKey(this.roomId);
|
|
|
|
if (result.success) {
|
|
|
|
const { users } = result;
|
2022-03-02 19:53:41 +00:00
|
|
|
await Promise.all(users.map(user => this.encryptRoomKeyForUser(user)));
|
2020-09-11 14:31:38 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Encrypt the room key to each user in
|
2022-03-02 19:53:41 +00:00
|
|
|
encryptRoomKeyForUser = async (user: Pick<IUser, '_id' | 'e2e'>) => {
|
2020-09-11 14:31:38 +00:00
|
|
|
if (user?.e2e?.public_key) {
|
|
|
|
const { public_key: publicKey } = user.e2e;
|
|
|
|
const userKey = await SimpleCrypto.RSA.importKey(EJSON.parse(publicKey));
|
2022-02-16 21:14:28 +00:00
|
|
|
const encryptedUserKey = await SimpleCrypto.RSA.encrypt(this.sessionKeyExportedString as string, userKey);
|
2020-09-11 14:31:38 +00:00
|
|
|
await RocketChat.e2eUpdateGroupKey(user?._id, this.roomId, this.keyID + encryptedUserKey);
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Provide this room key to a user
|
2022-02-16 21:14:28 +00:00
|
|
|
provideKeyToUser = async (keyId: string) => {
|
2020-09-11 14:31:38 +00:00
|
|
|
// Don't provide a key if the keyId received
|
|
|
|
// is different than the current one
|
|
|
|
if (this.keyID !== keyId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.encryptRoomKey();
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Encrypt text
|
2022-02-16 21:14:28 +00:00
|
|
|
encryptText = async (text: string | ArrayBuffer) => {
|
|
|
|
text = utf8ToBuffer(text as string);
|
2020-09-11 14:31:38 +00:00
|
|
|
const vector = await SimpleCrypto.utils.randomBytes(16);
|
2022-02-16 21:14:28 +00:00
|
|
|
const data = await SimpleCrypto.AES.encrypt(text, this.roomKey as ArrayBuffer, vector);
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
return this.keyID + bufferToB64(joinVectorData(vector, data));
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Encrypt messages
|
2022-02-16 21:14:28 +00:00
|
|
|
encrypt = async (message: IMessage) => {
|
2020-09-11 14:31:38 +00:00
|
|
|
if (!this.ready) {
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-09-13 20:41:05 +00:00
|
|
|
const msg = await this.encryptText(
|
|
|
|
EJSON.stringify({
|
|
|
|
_id: message._id,
|
|
|
|
text: message.msg,
|
|
|
|
userId: this.userId,
|
|
|
|
ts: new Date()
|
|
|
|
})
|
|
|
|
);
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
...message,
|
|
|
|
t: E2E_MESSAGE_TYPE,
|
|
|
|
e2e: E2E_STATUS.PENDING,
|
|
|
|
msg
|
|
|
|
};
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Decrypt text
|
2022-02-16 21:14:28 +00:00
|
|
|
decryptText = async (msg: string | ArrayBuffer) => {
|
|
|
|
msg = b64ToBuffer(msg.slice(12) as string);
|
2020-09-11 14:31:38 +00:00
|
|
|
const [vector, cipherText] = splitVectorData(msg);
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const decrypted = await SimpleCrypto.AES.decrypt(cipherText, this.roomKey, vector);
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
const m = EJSON.parse(bufferToUtf8(decrypted));
|
|
|
|
|
|
|
|
return m.text;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Decrypt messages
|
2022-02-16 21:14:28 +00:00
|
|
|
decrypt = async (message: IMessage) => {
|
2020-09-11 14:31:38 +00:00
|
|
|
if (!this.ready) {
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { t, e2e } = message;
|
|
|
|
|
|
|
|
// If message type is e2e and it's encrypted still
|
|
|
|
if (t === E2E_MESSAGE_TYPE && e2e !== E2E_STATUS.DONE) {
|
|
|
|
let { msg, tmsg } = message;
|
|
|
|
// Decrypt msg
|
2022-02-16 21:14:28 +00:00
|
|
|
msg = await this.decryptText(msg as string);
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Decrypt tmsg
|
|
|
|
if (tmsg) {
|
|
|
|
tmsg = await this.decryptText(tmsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...message,
|
|
|
|
tmsg,
|
|
|
|
msg,
|
|
|
|
e2e: E2E_STATUS.DONE
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
}
|