2020-09-11 14:31:38 +00:00
|
|
|
import EJSON from 'ejson';
|
|
|
|
import SimpleCrypto from 'react-native-simple-crypto';
|
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2022-02-16 21:14:28 +00:00
|
|
|
import { Q, Model } from '@nozbe/watermelondb';
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
import RocketChat from '../rocketchat';
|
|
|
|
import UserPreferences from '../userPreferences';
|
|
|
|
import database from '../database';
|
|
|
|
import protectedFunction from '../methods/helpers/protectedFunction';
|
|
|
|
import Deferred from '../../utils/deferred';
|
|
|
|
import log from '../../utils/log';
|
2022-02-09 21:16:20 +00:00
|
|
|
import { store } from '../auxStore';
|
2021-09-13 20:41:05 +00:00
|
|
|
import {
|
|
|
|
E2E_BANNER_TYPE,
|
|
|
|
E2E_MESSAGE_TYPE,
|
|
|
|
E2E_PRIVATE_KEY,
|
|
|
|
E2E_PUBLIC_KEY,
|
|
|
|
E2E_RANDOM_PASSWORD_KEY,
|
|
|
|
E2E_STATUS
|
|
|
|
} from './constants';
|
|
|
|
import { joinVectorData, randomPassword, splitVectorData, toString, utf8ToBuffer } from './utils';
|
|
|
|
import { EncryptionRoom } from './index';
|
2022-02-16 21:14:28 +00:00
|
|
|
import { IMessage, ISubscription, TMessageModel, TSubscriptionModel, TThreadMessageModel, TThreadModel } from '../../definitions';
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
class Encryption {
|
2022-02-16 21:14:28 +00:00
|
|
|
ready: boolean;
|
|
|
|
privateKey: string | null;
|
|
|
|
readyPromise: Deferred;
|
|
|
|
userId: string | null;
|
|
|
|
roomInstances: {
|
|
|
|
[rid: string]: {
|
|
|
|
ready: boolean;
|
|
|
|
provideKeyToUser: Function;
|
|
|
|
handshake: Function;
|
|
|
|
decrypt: Function;
|
|
|
|
encrypt: Function;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-09-11 14:31:38 +00:00
|
|
|
constructor() {
|
2022-02-16 21:14:28 +00:00
|
|
|
this.userId = '';
|
2020-09-11 14:31:38 +00:00
|
|
|
this.ready = false;
|
|
|
|
this.privateKey = null;
|
|
|
|
this.roomInstances = {};
|
|
|
|
this.readyPromise = new Deferred();
|
|
|
|
this.readyPromise
|
|
|
|
.then(() => {
|
|
|
|
this.ready = true;
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
this.ready = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize Encryption client
|
2022-02-16 21:14:28 +00:00
|
|
|
initialize = (userId: string) => {
|
2020-09-24 18:34:13 +00:00
|
|
|
this.userId = userId;
|
2020-09-11 14:31:38 +00:00
|
|
|
this.roomInstances = {};
|
|
|
|
|
|
|
|
// Don't await these promises
|
|
|
|
// so they can run parallelized
|
|
|
|
this.decryptPendingSubscriptions();
|
|
|
|
this.decryptPendingMessages();
|
|
|
|
|
|
|
|
// Mark Encryption client as ready
|
|
|
|
this.readyPromise.resolve();
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
get establishing() {
|
|
|
|
const { banner } = store.getState().encryption;
|
|
|
|
// If the password was not inserted yet
|
2020-09-23 17:16:04 +00:00
|
|
|
if (!banner || banner === E2E_BANNER_TYPE.REQUEST_PASSWORD) {
|
2020-09-11 14:31:38 +00:00
|
|
|
// We can't decrypt/encrypt, so, reject this try
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait the client ready state
|
|
|
|
return this.readyPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop Encryption client
|
|
|
|
stop = () => {
|
2020-09-24 18:34:13 +00:00
|
|
|
this.userId = null;
|
2020-09-11 14:31:38 +00:00
|
|
|
this.privateKey = null;
|
|
|
|
this.roomInstances = {};
|
|
|
|
// Cancel ongoing encryption/decryption requests
|
|
|
|
this.readyPromise.reject();
|
|
|
|
// Reset Deferred
|
|
|
|
this.ready = false;
|
|
|
|
this.readyPromise = new Deferred();
|
|
|
|
this.readyPromise
|
|
|
|
.then(() => {
|
|
|
|
this.ready = true;
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
this.ready = false;
|
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// When a new participant join and request a new room encryption key
|
2022-02-16 21:14:28 +00:00
|
|
|
provideRoomKeyToUser = async (keyId: string, rid: string) => {
|
2020-09-11 14:31:38 +00:00
|
|
|
// If the client is not ready
|
|
|
|
if (!this.ready) {
|
|
|
|
try {
|
|
|
|
// Wait for ready status
|
|
|
|
await this.establishing;
|
|
|
|
} catch {
|
|
|
|
// If it can't be initialized (missing password)
|
|
|
|
// return and don't provide a key
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const roomE2E = await this.getRoomInstance(rid);
|
|
|
|
return roomE2E.provideKeyToUser(keyId);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Persist keys on UserPreferences
|
2022-02-16 21:14:28 +00:00
|
|
|
persistKeys = async (server: string, publicKey: string, privateKey: string) => {
|
2020-09-11 14:31:38 +00:00
|
|
|
this.privateKey = await SimpleCrypto.RSA.importKey(EJSON.parse(privateKey));
|
2021-09-13 20:41:05 +00:00
|
|
|
await UserPreferences.setStringAsync(`${server}-${E2E_PUBLIC_KEY}`, EJSON.stringify(publicKey));
|
|
|
|
await UserPreferences.setStringAsync(`${server}-${E2E_PRIVATE_KEY}`, privateKey);
|
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Could not obtain public-private keypair from server.
|
2022-02-16 21:14:28 +00:00
|
|
|
createKeys = async (userId: string, server: string) => {
|
2020-09-11 14:31:38 +00:00
|
|
|
// Generate new keys
|
|
|
|
const key = await SimpleCrypto.RSA.generateKeys(2048);
|
|
|
|
|
|
|
|
// Cast these keys to the properly server format
|
|
|
|
const publicKey = await SimpleCrypto.RSA.exportKey(key.public);
|
|
|
|
const privateKey = await SimpleCrypto.RSA.exportKey(key.private);
|
|
|
|
|
|
|
|
// Persist these new keys
|
|
|
|
this.persistKeys(server, publicKey, EJSON.stringify(privateKey));
|
|
|
|
|
|
|
|
// Create a password to encode the private key
|
|
|
|
const password = await this.createRandomPassword(server);
|
|
|
|
|
|
|
|
// Encode the private key
|
|
|
|
const encodedPrivateKey = await this.encodePrivateKey(EJSON.stringify(privateKey), password, userId);
|
|
|
|
|
|
|
|
// Send the new keys to the server
|
|
|
|
await RocketChat.e2eSetUserPublicAndPrivateKeys(EJSON.stringify(publicKey), encodedPrivateKey);
|
|
|
|
|
|
|
|
// Request e2e keys of all encrypted rooms
|
|
|
|
await RocketChat.e2eRequestSubscriptionKeys();
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Encode a private key before send it to the server
|
2022-02-16 21:14:28 +00:00
|
|
|
encodePrivateKey = async (privateKey: string, password: string, userId: string) => {
|
2020-09-11 14:31:38 +00:00
|
|
|
const masterKey = await this.generateMasterKey(password, userId);
|
|
|
|
|
|
|
|
const vector = await SimpleCrypto.utils.randomBytes(16);
|
2021-09-13 20:41:05 +00:00
|
|
|
const data = await SimpleCrypto.AES.encrypt(utf8ToBuffer(privateKey), masterKey, vector);
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
return EJSON.stringify(new Uint8Array(joinVectorData(vector, data)));
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Decode a private key fetched from server
|
2022-02-16 21:14:28 +00:00
|
|
|
decodePrivateKey = async (privateKey: string, password: string, userId: string) => {
|
2020-09-11 14:31:38 +00:00
|
|
|
const masterKey = await this.generateMasterKey(password, userId);
|
|
|
|
const [vector, cipherText] = splitVectorData(EJSON.parse(privateKey));
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const privKey = await SimpleCrypto.AES.decrypt(cipherText, masterKey, vector);
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
return toString(privKey);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Generate a user master key, this is based on userId and a password
|
2022-02-16 21:14:28 +00:00
|
|
|
generateMasterKey = async (password: string, userId: string) => {
|
2020-09-11 14:31:38 +00:00
|
|
|
const iterations = 1000;
|
|
|
|
const hash = 'SHA256';
|
|
|
|
const keyLen = 32;
|
|
|
|
|
|
|
|
const passwordBuffer = utf8ToBuffer(password);
|
|
|
|
const saltBuffer = utf8ToBuffer(userId);
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const masterKey = await SimpleCrypto.PBKDF2.hash(passwordBuffer, saltBuffer, iterations, keyLen, hash);
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
return masterKey;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Create a random password to local created keys
|
2022-02-16 21:14:28 +00:00
|
|
|
createRandomPassword = async (server: string) => {
|
2020-09-11 14:31:38 +00:00
|
|
|
const password = randomPassword();
|
2021-09-13 20:41:05 +00:00
|
|
|
await UserPreferences.setStringAsync(`${server}-${E2E_RANDOM_PASSWORD_KEY}`, password);
|
2020-09-11 14:31:38 +00:00
|
|
|
return password;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
2022-02-16 21:14:28 +00:00
|
|
|
changePassword = async (server: string, password: string) => {
|
2020-10-30 18:31:04 +00:00
|
|
|
// Cast key to the format server is expecting
|
2022-02-16 21:14:28 +00:00
|
|
|
const privateKey = await SimpleCrypto.RSA.exportKey(this.privateKey as string);
|
2020-10-30 18:31:04 +00:00
|
|
|
|
|
|
|
// Encode the private key
|
2022-02-16 21:14:28 +00:00
|
|
|
const encodedPrivateKey = await this.encodePrivateKey(EJSON.stringify(privateKey), password, this.userId as string);
|
2021-09-13 20:41:05 +00:00
|
|
|
const publicKey = await UserPreferences.getStringAsync(`${server}-${E2E_PUBLIC_KEY}`);
|
2020-10-30 18:31:04 +00:00
|
|
|
|
|
|
|
// Send the new keys to the server
|
|
|
|
await RocketChat.e2eSetUserPublicAndPrivateKeys(EJSON.stringify(publicKey), encodedPrivateKey);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-10-30 18:31:04 +00:00
|
|
|
|
2020-09-11 14:31:38 +00:00
|
|
|
// get a encryption room instance
|
2022-02-16 21:14:28 +00:00
|
|
|
getRoomInstance = async (rid: string) => {
|
2020-09-11 14:31:38 +00:00
|
|
|
// Prevent handshake again
|
|
|
|
if (this.roomInstances[rid]?.ready) {
|
|
|
|
return this.roomInstances[rid];
|
|
|
|
}
|
|
|
|
|
|
|
|
// If doesn't have a instance of this room
|
|
|
|
if (!this.roomInstances[rid]) {
|
2022-02-16 21:14:28 +00:00
|
|
|
this.roomInstances[rid] = new EncryptionRoom(rid, this.userId as string);
|
2020-09-11 14:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const roomE2E = this.roomInstances[rid];
|
|
|
|
|
|
|
|
// Start Encryption Room instance handshake
|
|
|
|
await roomE2E.handshake();
|
|
|
|
|
|
|
|
return roomE2E;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Logic to decrypt all pending messages/threads/threadMessages
|
|
|
|
// after initialize the encryption client
|
2022-02-16 21:14:28 +00:00
|
|
|
decryptPendingMessages = async (roomId?: string) => {
|
2020-09-11 14:31:38 +00:00
|
|
|
const db = database.active;
|
|
|
|
|
2021-02-26 16:25:51 +00:00
|
|
|
const messagesCollection = db.get('messages');
|
|
|
|
const threadsCollection = db.get('threads');
|
|
|
|
const threadMessagesCollection = db.get('thread_messages');
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// e2e status is null or 'pending' and message type is 'e2e'
|
2021-09-13 20:41:05 +00:00
|
|
|
const whereClause = [Q.where('t', E2E_MESSAGE_TYPE), Q.or(Q.where('e2e', null), Q.where('e2e', E2E_STATUS.PENDING))];
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// decrypt messages of a room
|
|
|
|
if (roomId) {
|
|
|
|
whereClause.push(Q.where('rid', roomId));
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Find all messages/threads/threadsMessages that have pending e2e status
|
|
|
|
const messagesToDecrypt = await messagesCollection.query(...whereClause).fetch();
|
|
|
|
const threadsToDecrypt = await threadsCollection.query(...whereClause).fetch();
|
|
|
|
const threadMessagesToDecrypt = await threadMessagesCollection.query(...whereClause).fetch();
|
|
|
|
|
|
|
|
// Concat messages/threads/threadMessages
|
2022-02-16 21:14:28 +00:00
|
|
|
let toDecrypt: (TThreadModel | TThreadMessageModel)[] = [
|
|
|
|
...messagesToDecrypt,
|
|
|
|
...threadsToDecrypt,
|
|
|
|
...threadMessagesToDecrypt
|
|
|
|
];
|
|
|
|
toDecrypt = (await Promise.all(
|
2021-09-13 20:41:05 +00:00
|
|
|
toDecrypt.map(async message => {
|
|
|
|
const { t, msg, tmsg } = message;
|
2022-02-17 00:07:24 +00:00
|
|
|
let newMessage: TMessageModel = {} as TMessageModel;
|
|
|
|
if (message.subscription) {
|
|
|
|
const { id: rid } = message.subscription;
|
|
|
|
// WM Object -> Plain Object
|
|
|
|
newMessage = await this.decryptMessage({
|
|
|
|
t,
|
|
|
|
rid,
|
|
|
|
msg,
|
|
|
|
tmsg
|
|
|
|
});
|
|
|
|
}
|
2022-02-16 21:14:28 +00:00
|
|
|
|
2022-02-10 20:16:10 +00:00
|
|
|
try {
|
|
|
|
return message.prepareUpdate(
|
2022-02-16 21:14:28 +00:00
|
|
|
protectedFunction((m: TMessageModel) => {
|
2022-02-10 20:16:10 +00:00
|
|
|
Object.assign(m, newMessage);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch {
|
|
|
|
return null;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
})
|
2022-02-16 21:14:28 +00:00
|
|
|
)) as (TThreadModel | TThreadMessageModel)[];
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-02-16 21:14:28 +00:00
|
|
|
await db.write(async () => {
|
2020-09-11 14:31:38 +00:00
|
|
|
await db.batch(...toDecrypt);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Logic to decrypt all pending subscriptions
|
|
|
|
// after initialize the encryption client
|
2021-09-13 20:41:05 +00:00
|
|
|
decryptPendingSubscriptions = async () => {
|
2020-09-11 14:31:38 +00:00
|
|
|
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 all rooms that can have a lastMessage encrypted
|
|
|
|
// If we select only encrypted rooms we can miss some room that changed their encrypted status
|
|
|
|
const subsEncrypted = await subCollection.query(Q.where('e2e_key_id', Q.notEq(null))).fetch();
|
|
|
|
// We can't do this on database level since lastMessage is not a database object
|
2021-09-13 20:41:05 +00:00
|
|
|
const subsToDecrypt = subsEncrypted.filter(
|
2022-02-16 21:14:28 +00:00
|
|
|
(sub: ISubscription) =>
|
2021-09-13 20:41:05 +00:00
|
|
|
// Encrypted message
|
|
|
|
sub?.lastMessage?.t === E2E_MESSAGE_TYPE &&
|
|
|
|
// Message pending decrypt
|
|
|
|
sub?.lastMessage?.e2e === E2E_STATUS.PENDING
|
|
|
|
);
|
|
|
|
await Promise.all(
|
2022-02-16 21:14:28 +00:00
|
|
|
subsToDecrypt.map(async (sub: TSubscriptionModel) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
const { rid, lastMessage } = sub;
|
|
|
|
const newSub = await this.decryptSubscription({ rid, lastMessage });
|
2022-02-10 20:16:10 +00:00
|
|
|
try {
|
|
|
|
return sub.prepareUpdate(
|
2022-02-16 21:14:28 +00:00
|
|
|
protectedFunction((m: TSubscriptionModel) => {
|
2022-02-10 20:16:10 +00:00
|
|
|
Object.assign(m, newSub);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch {
|
|
|
|
return null;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2022-02-16 21:14:28 +00:00
|
|
|
await db.write(async () => {
|
2020-09-11 14:31:38 +00:00
|
|
|
await db.batch(...subsToDecrypt);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Decrypt a subscription lastMessage
|
2022-02-16 21:14:28 +00:00
|
|
|
decryptSubscription = async (subscription: Partial<ISubscription>) => {
|
2020-09-11 14:31:38 +00:00
|
|
|
// If the subscription doesn't have a lastMessage just return
|
|
|
|
if (!subscription?.lastMessage) {
|
|
|
|
return subscription;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { lastMessage } = subscription;
|
|
|
|
const { t, e2e } = lastMessage;
|
|
|
|
|
|
|
|
// If it's not a encrypted message or was decrypted before
|
|
|
|
if (t !== E2E_MESSAGE_TYPE || e2e === E2E_STATUS.DONE) {
|
|
|
|
return subscription;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the client is not ready
|
|
|
|
if (!this.ready) {
|
|
|
|
try {
|
|
|
|
// Wait for ready status
|
|
|
|
await this.establishing;
|
|
|
|
} catch {
|
|
|
|
// If it can't be initialized (missing password)
|
|
|
|
// return the encrypted message
|
|
|
|
return subscription;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const { rid } = subscription;
|
|
|
|
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
|
|
|
|
|
|
|
let subRecord;
|
|
|
|
try {
|
2022-02-16 21:14:28 +00:00
|
|
|
subRecord = await subCollection.find(rid as string);
|
2020-09-11 14:31:38 +00:00
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-02-16 21:14:28 +00:00
|
|
|
const batch: (Model | null | void | false | Promise<void>)[] = [];
|
2020-09-11 14:31:38 +00:00
|
|
|
// If the subscription doesn't exists yet
|
|
|
|
if (!subRecord) {
|
|
|
|
// Let's create the subscription with the data received
|
2021-09-13 20:41:05 +00:00
|
|
|
batch.push(
|
2022-02-16 21:14:28 +00:00
|
|
|
subCollection.prepareCreate((s: TSubscriptionModel) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
s._raw = sanitizedRaw({ id: rid }, subCollection.schema);
|
|
|
|
Object.assign(s, subscription);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
// If the subscription already exists but doesn't have the E2EKey yet
|
2020-09-11 14:31:38 +00:00
|
|
|
} else if (!subRecord.E2EKey && subscription.E2EKey) {
|
2022-02-10 20:16:10 +00:00
|
|
|
try {
|
2020-09-11 14:31:38 +00:00
|
|
|
// Let's update the subscription with the received E2EKey
|
2021-09-13 20:41:05 +00:00
|
|
|
batch.push(
|
2022-02-16 21:14:28 +00:00
|
|
|
subRecord.prepareUpdate((s: TSubscriptionModel) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
s.E2EKey = subscription.E2EKey;
|
|
|
|
})
|
|
|
|
);
|
2022-02-10 20:16:10 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2020-09-11 14:31:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If batch has some operation
|
|
|
|
if (batch.length) {
|
2022-02-16 21:14:28 +00:00
|
|
|
await db.write(async () => {
|
2020-09-11 14:31:38 +00:00
|
|
|
await db.batch(...batch);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// Abort the decryption process
|
|
|
|
// Return as received
|
|
|
|
return subscription;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a instance using the subscription
|
2022-02-16 21:14:28 +00:00
|
|
|
const roomE2E = await this.getRoomInstance(rid as string);
|
2020-09-11 14:31:38 +00:00
|
|
|
const decryptedMessage = await roomE2E.decrypt(lastMessage);
|
|
|
|
return {
|
|
|
|
...subscription,
|
|
|
|
lastMessage: decryptedMessage
|
|
|
|
};
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Encrypt a message
|
2022-02-16 21:14:28 +00:00
|
|
|
encryptMessage = async (message: IMessage) => {
|
2020-09-11 14:31:38 +00:00
|
|
|
const { rid } = message;
|
|
|
|
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 subRecord = await subCollection.find(rid);
|
|
|
|
|
|
|
|
// Subscription is not encrypted at the moment
|
|
|
|
if (!subRecord.encrypted) {
|
|
|
|
// Send a non encrypted message
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the client is not ready
|
|
|
|
if (!this.ready) {
|
|
|
|
// Wait for ready status
|
|
|
|
await this.establishing;
|
|
|
|
}
|
|
|
|
|
|
|
|
const roomE2E = await this.getRoomInstance(rid);
|
|
|
|
return roomE2E.encrypt(message);
|
|
|
|
} catch {
|
|
|
|
// Subscription not found
|
|
|
|
// or client can't be initialized (missing password)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a non encrypted message
|
|
|
|
return message;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Decrypt a message
|
2022-02-22 16:01:35 +00:00
|
|
|
decryptMessage = async (message: Pick<IMessage, 't' | 'e2e' | 'rid' | 'msg' | 'tmsg'>) => {
|
2020-09-11 14:31:38 +00:00
|
|
|
const { t, e2e } = message;
|
|
|
|
|
|
|
|
// Prevent create a new instance if this room was encrypted sometime ago
|
|
|
|
if (t !== E2E_MESSAGE_TYPE || e2e === E2E_STATUS.DONE) {
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the client is not ready
|
|
|
|
if (!this.ready) {
|
|
|
|
try {
|
|
|
|
// Wait for ready status
|
|
|
|
await this.establishing;
|
|
|
|
} catch {
|
|
|
|
// If it can't be initialized (missing password)
|
|
|
|
// return the encrypted message
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const { rid } = message;
|
2022-02-16 21:14:28 +00:00
|
|
|
const roomE2E = await this.getRoomInstance(rid as string);
|
2020-09-11 14:31:38 +00:00
|
|
|
return roomE2E.decrypt(message);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Decrypt multiple messages
|
2022-02-16 21:14:28 +00:00
|
|
|
decryptMessages = (messages: IMessage[]) => Promise.all(messages.map((m: IMessage) => this.decryptMessage(m)));
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
// Decrypt multiple subscriptions
|
2022-02-16 21:14:28 +00:00
|
|
|
decryptSubscriptions = (subscriptions: ISubscription[]) => Promise.all(subscriptions.map(s => this.decryptSubscription(s)));
|
2020-09-11 14:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const encryption = new Encryption();
|
|
|
|
export default encryption;
|