Secure key exchange for DMs.

This commit is contained in:
Mega-Mewthree 2018-08-11 16:21:08 -07:00
parent c122457565
commit 7e41fe73e7
3 changed files with 33 additions and 14 deletions

View File

@ -62,6 +62,16 @@ export default new class E2EE extends BuiltinModule {
return haveKey.value.value;
}
setKey(channelId, key) {
const items = Settings.getSetting('security', 'e2eedb', 'e2ekvps').items;
const index = items.findIndex(kvp => kvp.value.key === channelId);
if (index > -1) {
items[index].value = {key: channelId, value: key};
return;
}
Settings.getSetting('security', 'e2eedb', 'e2ekvps').addItem({ value: { key: channelId, value: key } });
}
async enabled(e) {
this.patchDispatcher();
this.patchMessageContent();
@ -79,7 +89,7 @@ export default new class E2EE extends BuiltinModule {
const key = this.getKey(event.message.channel_id);
if (!key) return; // We don't have a key for this channel
if (typeof event.message.content !== 'string') return; // Ignore any non string content
if (!event.message.content.startsWith('$:')) return; // Not an encrypted string
let decrypt;
@ -91,10 +101,10 @@ export default new class E2EE extends BuiltinModule {
const Permissions = WebpackModules.getModuleByName('GuildPermissions');
const DiscordConstants = WebpackModules.getModuleByName('DiscordConstants');
const currentChannel = DiscordApi.Channel.fromId(event.message.channel_id).discordObject;
// Create a generic message object to parse mentions with
const parsed = MessageParser.parse(currentChannel, decrypt).content;
if (userMentionPattern.test(parsed))
event.message.mentions = parsed.match(userMentionPattern).map(m => {return {id: m.replace(/[^0-9]/g, '')}});
if (roleMentionPattern.test(parsed))
@ -122,7 +132,7 @@ export default new class E2EE extends BuiltinModule {
const Permissions = WebpackModules.getModuleByName('GuildPermissions');
const DiscordConstants = WebpackModules.getModuleByName('DiscordConstants');
const currentChannel = DiscordApi.Channel.fromId(component.props.message.channel_id).discordObject;
if (typeof component.props.message.content !== 'string') return; // Ignore any non string content
if (!component.props.message.content.startsWith('$:')) return; // Not an encrypted string
let decrypt;

View File

@ -37,23 +37,31 @@
import { MiLock } from '../ui/components/common/MaterialIcon';
import contextMenu from 'vue-context-menu';
import { clipboard } from 'electron';
import { Toasts } from 'ui';
function generatePublicKey() {
const userID = location.pathname.split("/")[3];
const publicKeyMessage = `My public key is: \`${E2EE.createKeyExchange(userID)}\`. Please give me your public key if you haven't done so and add my public key by pasting it in the chat textbox, right clicking the lock icon, and selecting \`Receive Public Key\`.`;
const dmChannelID = location.pathname.split("/")[3];
const publicKeyMessage = `My public key is: \`${E2EE.createKeyExchange(dmChannelID)}\`. Please give me your public key if you haven't done so and add my public key by pasting it in the chat textbox, right clicking the lock icon, and selecting \`Receive Public Key\`.`;
const chatInput = document.getElementsByClassName('da-textArea')[0];
chatInput.value = publicKeyMessage;
chatInput._valueTracker.setValue(publicKeyMessage);
chatInput[Object.keys(chatInput).find(k => k.startsWith('__reactInternalInstance'))].memoizedProps.value = publicKeyMessage;
const evt = { currentTarget: chatInput };
chatInput[Object.keys(chatInput).find(k => k.startsWith('__reactEventHandlers'))].onChange.call(chatInput, evt);
}
function computeSharedSecret() {
try {
const userID = location.pathname.split("/")[3];
const otherPublicKey = document.getElementsByClassName("da-textArea")[0].value;
const secret = E2EE.computeSecret(userID, otherPublicKey);
clipboard.writeText(secret);
} catch (e) {}
const dmChannelID = location.pathname.split("/")[3];
const chatInput = document.getElementsByClassName('da-textArea')[0];
const otherPublicKey = chatInput.value;
const secret = E2EE.computeSecret(dmChannelID, otherPublicKey);
E2EE.setKey(dmChannelID, secret);
chatInput.value = "";
const evt = { currentTarget: chatInput };
chatInput[Object.keys(chatInput).find(k => k.startsWith('__reactEventHandlers'))].onChange.call(chatInput, evt);
Toasts.success("Encryption key has been set for this DM channel.");
} catch (e) {
throw e;
}
}
export default {

View File

@ -53,7 +53,8 @@
.bd-e2eeLockContextMenuOption {
background: #23272A;
color: #99AAB5;
padding: 5px 5px;
padding: 8px 8px;
font-weight: 500;
cursor: default;
&:hover {