aboutsummaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/omemo/src/stream_module.vala137
-rw-r--r--plugins/openpgp/src/stream_module.vala100
2 files changed, 137 insertions, 100 deletions
diff --git a/plugins/omemo/src/stream_module.vala b/plugins/omemo/src/stream_module.vala
index 46bc0ecf..75a919f7 100644
--- a/plugins/omemo/src/stream_module.vala
+++ b/plugins/omemo/src/stream_module.vala
@@ -117,70 +117,10 @@ public class StreamModule : XmppStreamModule {
this.store = Plugin.get_context().create_store();
store_created(store);
- stream.get_module(Message.Module.IDENTITY).pre_received_message.connect(on_pre_received_message);
+ stream.get_module(Message.Module.IDENTITY).received_pipeline.connect(new ReceivedPipelineListener(store));
stream.get_module(Pubsub.Module.IDENTITY).add_filtered_notification(stream, NODE_DEVICELIST, (stream, jid, id, node) => on_devicelist(stream, jid, id, node));
}
- private void on_pre_received_message(XmppStream stream, Message.Stanza message) {
- StanzaNode? _encrypted = message.stanza.get_subnode("encrypted", NS_URI);
- if (_encrypted == null || MessageFlag.get_flag(message) != null || message.from == null) return;
- StanzaNode encrypted = (!)_encrypted;
- if (!Plugin.ensure_context()) return;
- MessageFlag flag = new MessageFlag();
- message.add_flag(flag);
- StanzaNode? _header = encrypted.get_subnode("header");
- if (_header == null) return;
- StanzaNode header = (!)_header;
- if (header.get_attribute_int("sid") <= 0) return;
- foreach (StanzaNode key_node in header.get_subnodes("key")) {
- if (key_node.get_attribute_int("rid") == store.local_registration_id) {
- try {
- string? payload = encrypted.get_deep_string_content("payload");
- string? iv_node = header.get_deep_string_content("iv");
- string? key_node_content = key_node.get_string_content();
- if (payload == null || iv_node == null || key_node_content == null) continue;
- uint8[] key;
- uint8[] ciphertext = Base64.decode((!)payload);
- uint8[] iv = Base64.decode((!)iv_node);
- Address address = new Address(get_bare_jid((!)message.from), header.get_attribute_int("sid"));
- if (key_node.get_attribute_bool("prekey")) {
- PreKeySignalMessage msg = Plugin.get_context().deserialize_pre_key_signal_message(Base64.decode((!)key_node_content));
- SessionCipher cipher = store.create_session_cipher(address);
- key = cipher.decrypt_pre_key_signal_message(msg);
- } else {
- SignalMessage msg = Plugin.get_context().deserialize_signal_message(Base64.decode((!)key_node_content));
- SessionCipher cipher = store.create_session_cipher(address);
- key = cipher.decrypt_signal_message(msg);
- }
- address.device_id = 0; // TODO: Hack to have address obj live longer
-
- if (key.length >= 32) {
- int authtaglength = key.length - 16;
- uint8[] new_ciphertext = new uint8[ciphertext.length + authtaglength];
- uint8[] new_key = new uint8[16];
- Memory.copy(new_ciphertext, ciphertext, ciphertext.length);
- Memory.copy((uint8*)new_ciphertext + ciphertext.length, (uint8*)key + 16, authtaglength);
- Memory.copy(new_key, key, 16);
- ciphertext = new_ciphertext;
- key = new_key;
- }
-
- message.body = arr_to_str(aes_decrypt(Cipher.AES_GCM_NOPADDING, key, iv, ciphertext));
- flag.decrypted = true;
- } catch (Error e) {
- if (Plugin.DEBUG) print(@"OMEMO: Signal error while decrypting message: $(e.message)\n");
- }
- }
- }
- }
-
- private string arr_to_str(uint8[] arr) {
- // null-terminate the array
- uint8[] rarr = new uint8[arr.length+1];
- Memory.copy(rarr, arr, arr.length);
- return (string)rarr;
- }
-
public void request_user_devicelist(XmppStream stream, string jid) {
if (active_devicelist_requests.add(jid)) {
if (Plugin.DEBUG) print(@"OMEMO: requesting device list for $jid\n");
@@ -442,4 +382,79 @@ public class StreamModule : XmppStreamModule {
}
}
+
+public class ReceivedPipelineListener : StanzaListener<Message.Stanza> {
+
+ private const string[] after_actions_const = {"EXTRACT_MESSAGE_2"};
+
+ public override string action_group { get { return "ENCRYPT_BODY"; } }
+ public override string[] after_actions { get { return after_actions_const; } }
+
+ private Store store;
+
+ public ReceivedPipelineListener(Store store) {
+ this.store = store;
+ }
+
+ public override async void run(Core.XmppStream stream, Message.Stanza message) {
+ StanzaNode? _encrypted = message.stanza.get_subnode("encrypted", NS_URI);
+ if (_encrypted == null || MessageFlag.get_flag(message) != null || message.from == null) return;
+ StanzaNode encrypted = (!)_encrypted;
+ if (!Plugin.ensure_context()) return;
+ MessageFlag flag = new MessageFlag();
+ message.add_flag(flag);
+ StanzaNode? _header = encrypted.get_subnode("header");
+ if (_header == null) return;
+ StanzaNode header = (!)_header;
+ if (header.get_attribute_int("sid") <= 0) return;
+ foreach (StanzaNode key_node in header.get_subnodes("key")) {
+ if (key_node.get_attribute_int("rid") == store.local_registration_id) {
+ try {
+ string? payload = encrypted.get_deep_string_content("payload");
+ string? iv_node = header.get_deep_string_content("iv");
+ string? key_node_content = key_node.get_string_content();
+ if (payload == null || iv_node == null || key_node_content == null) continue;
+ uint8[] key;
+ uint8[] ciphertext = Base64.decode((!)payload);
+ uint8[] iv = Base64.decode((!)iv_node);
+ Address address = new Address(get_bare_jid((!)message.from), header.get_attribute_int("sid"));
+ if (key_node.get_attribute_bool("prekey")) {
+ PreKeySignalMessage msg = Plugin.get_context().deserialize_pre_key_signal_message(Base64.decode((!)key_node_content));
+ SessionCipher cipher = store.create_session_cipher(address);
+ key = cipher.decrypt_pre_key_signal_message(msg);
+ } else {
+ SignalMessage msg = Plugin.get_context().deserialize_signal_message(Base64.decode((!)key_node_content));
+ SessionCipher cipher = store.create_session_cipher(address);
+ key = cipher.decrypt_signal_message(msg);
+ }
+ address.device_id = 0; // TODO: Hack to have address obj live longer
+
+ if (key.length >= 32) {
+ int authtaglength = key.length - 16;
+ uint8[] new_ciphertext = new uint8[ciphertext.length + authtaglength];
+ uint8[] new_key = new uint8[16];
+ Memory.copy(new_ciphertext, ciphertext, ciphertext.length);
+ Memory.copy((uint8*)new_ciphertext + ciphertext.length, (uint8*)key + 16, authtaglength);
+ Memory.copy(new_key, key, 16);
+ ciphertext = new_ciphertext;
+ key = new_key;
+ }
+
+ message.body = arr_to_str(aes_decrypt(Cipher.AES_GCM_NOPADDING, key, iv, ciphertext));
+ flag.decrypted = true;
+ } catch (Error e) {
+ if (Plugin.DEBUG) print(@"OMEMO: Signal error while decrypting message: $(e.message)\n");
+ }
+ }
+ }
+ }
+
+ private string arr_to_str(uint8[] arr) {
+ // null-terminate the array
+ uint8[] rarr = new uint8[arr.length+1];
+ Memory.copy(rarr, arr, arr.length);
+ return (string)rarr;
+ }
+}
+
}
diff --git a/plugins/openpgp/src/stream_module.vala b/plugins/openpgp/src/stream_module.vala
index 068370fd..6264ce49 100644
--- a/plugins/openpgp/src/stream_module.vala
+++ b/plugins/openpgp/src/stream_module.vala
@@ -43,22 +43,16 @@ namespace Dino.Plugins.OpenPgp {
return false;
}
- public string? get_cyphertext(Message.Stanza message) {
- StanzaNode? x_node = message.stanza.get_subnode("x", NS_URI_ENCRYPTED);
- return x_node == null ? null : x_node.get_string_content();
- }
-
public override void attach(XmppStream stream) {
stream.get_module(Presence.Module.IDENTITY).received_presence.connect(on_received_presence);
stream.get_module(Presence.Module.IDENTITY).pre_send_presence_stanza.connect(on_pre_send_presence_stanza);
- stream.get_module(Message.Module.IDENTITY).pre_received_message.connect(on_pre_received_message);
+ stream.get_module(Message.Module.IDENTITY).received_pipeline.connect(new ReceivedPipelineDecryptListener());
stream.add_flag(new Flag());
}
public override void detach(XmppStream stream) {
stream.get_module(Presence.Module.IDENTITY).received_presence.disconnect(on_received_presence);
stream.get_module(Presence.Module.IDENTITY).pre_send_presence_stanza.disconnect(on_pre_send_presence_stanza);
- stream.get_module(Message.Module.IDENTITY).pre_received_message.disconnect(on_pre_received_message);
}
public static void require(XmppStream stream) {
@@ -69,18 +63,24 @@ namespace Dino.Plugins.OpenPgp {
public override string get_id() { return IDENTITY.id; }
private void on_received_presence(XmppStream stream, Presence.Stanza presence) {
- StanzaNode x_node = presence.stanza.get_subnode("x", NS_URI_SIGNED);
- if (x_node != null) {
- string? sig = x_node.get_string_content();
- if (sig != null) {
- string signed_data = presence.status == null ? "" : presence.status;
- string? key_id = get_sign_key(sig, signed_data);
- if (key_id != null) {
- stream.get_flag(Flag.IDENTITY).set_key_id(presence.from, key_id);
- received_jid_key_id(stream, presence.from, key_id);
+ new Thread<void*> (null, () => {
+ StanzaNode x_node = presence.stanza.get_subnode("x", NS_URI_SIGNED);
+ if (x_node != null) {
+ string? sig = x_node.get_string_content();
+ if (sig != null) {
+ string signed_data = presence.status == null ? "" : presence.status;
+ string? key_id = get_sign_key(sig, signed_data);
+ if (key_id != null) {
+ stream.get_flag(Flag.IDENTITY).set_key_id(presence.from, key_id);
+ Idle.add(() => {
+ received_jid_key_id(stream, presence.from, key_id);
+ return false;
+ });
+ }
}
}
- }
+ return null;
+ });
}
private void on_pre_send_presence_stanza(XmppStream stream, Presence.Stanza presence) {
@@ -89,19 +89,6 @@ namespace Dino.Plugins.OpenPgp {
}
}
- private void on_pre_received_message(XmppStream stream, Message.Stanza message) {
- string? encrypted = get_cyphertext(message);
- if (encrypted != null) {
- MessageFlag flag = new MessageFlag();
- message.add_flag(flag);
- string? decrypted = gpg_decrypt(encrypted);
- if (decrypted != null) {
- flag.decrypted = true;
- message.body = decrypted;
- }
- }
- }
-
private static string? gpg_encrypt(string plain, GPG.Key[] keys) {
string encr;
try {
@@ -113,15 +100,6 @@ namespace Dino.Plugins.OpenPgp {
return encr.substring(encryption_start, encr.length - "\n-----END PGP MESSAGE-----".length - encryption_start);
}
- private static string? gpg_decrypt(string enc) {
- string armor = "-----BEGIN PGP MESSAGE-----\n\n" + enc + "\n-----END PGP MESSAGE-----";
- string? decr = null;
- try {
- decr = GPGHelper.decrypt(armor);
- } catch (Error e) { }
- return decr;
- }
-
private static string? get_sign_key(string sig, string signed_text) {
string armor = "-----BEGIN PGP MESSAGE-----\n\n" + sig + "\n-----END PGP MESSAGE-----";
string? sign_key = null;
@@ -156,4 +134,48 @@ namespace Dino.Plugins.OpenPgp {
public override string get_ns() { return NS_URI; }
public override string get_id() { return id; }
}
+
+public class ReceivedPipelineDecryptListener : StanzaListener<Message.Stanza> {
+
+ private const string[] after_actions_const = {"MODIFY_BODY"};
+
+ public override string action_group { get { return "ENCRYPT_BODY"; } }
+ public override string[] after_actions { get { return after_actions_const; } }
+
+ public override async void run(Core.XmppStream stream, Message.Stanza message) {
+ string? encrypted = get_cyphertext(message);
+ if (encrypted != null) {
+ MessageFlag flag = new MessageFlag();
+ message.add_flag(flag);
+ string? decrypted = yield gpg_decrypt(encrypted);
+ if (decrypted != null) {
+ flag.decrypted = true;
+ message.body = decrypted;
+ }
+ }
+ }
+
+ private static async string? gpg_decrypt(string enc) {
+ SourceFunc callback = gpg_decrypt.callback;
+ string? res = null;
+ new Thread<void*> (null, () => {
+ string armor = "-----BEGIN PGP MESSAGE-----\n\n" + enc + "\n-----END PGP MESSAGE-----";
+ try {
+ res = GPGHelper.decrypt(armor);
+ } catch (Error e) {
+ res = null;
+ }
+ Idle.add((owned) callback);
+ return null;
+ });
+ yield;
+ return res;
+ }
+
+ private string? get_cyphertext(Message.Stanza message) {
+ StanzaNode? x_node = message.stanza.get_subnode("x", NS_URI_ENCRYPTED);
+ return x_node == null ? null : x_node.get_string_content();
+ }
+}
+
}