From 74c48e65671058225ea77a6d1cc48e12798d9d21 Mon Sep 17 00:00:00 2001 From: Samuel Hand Date: Wed, 11 Jul 2018 18:17:57 +0100 Subject: Enable encryption in MUCs --- plugins/omemo/src/encrypt_state.vala | 6 +-- plugins/omemo/src/manager.vala | 57 ++++++++++++++++----- plugins/omemo/src/stream_module.vala | 97 ++++++++++++++++++++++++------------ 3 files changed, 113 insertions(+), 47 deletions(-) diff --git a/plugins/omemo/src/encrypt_state.vala b/plugins/omemo/src/encrypt_state.vala index 80ae40d7..fd72faf4 100644 --- a/plugins/omemo/src/encrypt_state.vala +++ b/plugins/omemo/src/encrypt_state.vala @@ -7,7 +7,7 @@ public class EncryptState { public int other_lost { get; internal set; } public int other_unknown { get; internal set; } public int other_failure { get; internal set; } - public bool other_list { get; internal set; } + public int other_waiting_lists { get; internal set; } public int own_devices { get; internal set; } public int own_success { get; internal set; } @@ -17,8 +17,8 @@ public class EncryptState { public bool own_list { get; internal set; } public string to_string() { - return @"EncryptState (encrypted=$encrypted, other=(devices=$other_devices, success=$other_success, lost=$other_lost, unknown=$other_unknown, failure=$other_failure, list=$other_list), own=(devices=$own_devices, success=$own_success, lost=$own_lost, unknown=$own_unknown, failure=$own_failure, list=$own_list))"; + return @"EncryptState (encrypted=$encrypted, other=(devices=$other_devices, success=$other_success, lost=$other_lost, unknown=$other_unknown, failure=$other_failure, waiting_lists=$other_waiting_lists, own=(devices=$own_devices, success=$own_success, lost=$own_lost, unknown=$own_unknown, failure=$own_failure, list=$own_list))"; } } -} \ No newline at end of file +} diff --git a/plugins/omemo/src/manager.vala b/plugins/omemo/src/manager.vala index 2f641196..9bfa93d6 100644 --- a/plugins/omemo/src/manager.vala +++ b/plugins/omemo/src/manager.vala @@ -21,7 +21,7 @@ public class Manager : StreamInteractionModule, Object { public int waiting_other_sessions { get; set; } public int waiting_own_sessions { get; set; } public bool waiting_own_devicelist { get; set; } - public bool waiting_other_devicelist { get; set; } + public int waiting_other_devicelists { get; set; } public bool force_next_attempt { get; set; } public bool will_send_now { get; private set; } public bool active_send_attempt { get; set; } @@ -37,12 +37,12 @@ public class Manager : StreamInteractionModule, Object { this.waiting_other_sessions = new_try.other_unknown; this.waiting_own_sessions = new_try.own_unknown; this.waiting_own_devicelist = !new_try.own_list; - this.waiting_other_devicelist = !new_try.other_list; + this.waiting_other_devicelists = new_try.other_waiting_lists; this.active_send_attempt = false; will_send_now = false; if (new_try.other_failure > 0 || (new_try.other_lost == new_try.other_devices && new_try.other_devices > 0)) { msg.marked = Entities.Message.Marked.WONTSEND; - } else if (new_try.other_unknown > 0 || new_try.own_unknown > 0 || !new_try.other_list || !new_try.own_list || new_try.own_devices == 0) { + } else if (new_try.other_unknown > 0 || new_try.own_unknown > 0 || new_try.other_waiting_lists > 0 || !new_try.own_list || new_try.own_devices == 0) { msg.marked = Entities.Message.Marked.UNSENT; } else if (!new_try.encrypted) { msg.marked = Entities.Message.Marked.WONTSEND; @@ -52,11 +52,11 @@ public class Manager : StreamInteractionModule, Object { } public bool should_retry_now() { - return !waiting_own_devicelist && !waiting_other_devicelist && waiting_other_sessions <= 0 && waiting_own_sessions <= 0 && !active_send_attempt; + return !waiting_own_devicelist && waiting_other_devicelists <= 0 && waiting_other_sessions <= 0 && waiting_own_sessions <= 0 && !active_send_attempt; } public string to_string() { - return @"MessageState (waiting=(others=$waiting_other_sessions, own=$waiting_own_sessions, other_list=$waiting_other_devicelist, own_list=$waiting_own_devicelist))"; + return @"MessageState (waiting=(others=$waiting_other_sessions, own=$waiting_own_sessions, other_lists=$waiting_other_devicelists, own_list=$waiting_own_devicelist))"; } } @@ -85,6 +85,21 @@ public class Manager : StreamInteractionModule, Object { } } + private Gee.List get_occupants(Jid muc, Account account){ + Gee.List occupants = new ArrayList(Jid.equals_bare_func); + Gee.List? occupant_jids = stream_interactor.get_module(MucManager.IDENTITY).get_other_occupants(muc, account); + if(occupant_jids == null) { + return occupants; + } + foreach (Jid occupant in occupant_jids) { + Jid? occupant_jid = stream_interactor.get_module(MucManager.IDENTITY).get_real_jid(occupant, account); + if(occupant_jid != null){ + occupants.add(occupant_jid.bare_jid); + } + } + return occupants; + } + private void on_pre_message_send(Entities.Message message, Xmpp.MessageStanza message_stanza, Conversation conversation) { if (message.encryption == Encryption.OMEMO) { XmppStream? stream = stream_interactor.get_stream(conversation.account); @@ -107,7 +122,19 @@ public class Manager : StreamInteractionModule, Object { } } - EncryptState enc_state = module.encrypt(message_stanza, conversation.account.bare_jid); + Gee.List recipients; + if (message_stanza.type_ == MessageStanza.TYPE_GROUPCHAT) { + recipients = get_occupants((!)message.to.bare_jid, conversation.account); + if (recipients.size == 0) { + message.marked = Entities.Message.Marked.WONTSEND; + return; + } + } else { + recipients = new ArrayList(Jid.equals_bare_func); + recipients.add(message_stanza.to); + } + + EncryptState enc_state = module.encrypt(message_stanza, conversation.account.bare_jid, recipients); MessageState state; lock (message_states) { if (message_states.has_key(message)) { @@ -135,7 +162,7 @@ public class Manager : StreamInteractionModule, Object { if (state.waiting_other_sessions > 0 && message.counterpart != null) { module.fetch_bundles((!)stream, ((!)message.counterpart).bare_jid); } - if (state.waiting_other_devicelist && message.counterpart != null) { + if (state.waiting_other_devicelists > 0 && message.counterpart != null) { module.request_user_devicelist((!)stream, ((!)message.counterpart).bare_jid); } } @@ -159,11 +186,12 @@ public class Manager : StreamInteractionModule, Object { lock (message_states) { foreach (Entities.Message msg in message_states.keys) { if (!msg.account.equals(account)) continue; + Gee.List occupants = get_occupants(msg.counterpart.bare_jid, account); MessageState state = message_states[msg]; if (account.bare_jid.equals(jid)) { state.waiting_own_devicelist = false; - } else if (msg.counterpart != null && msg.counterpart.equals_bare(jid)) { - state.waiting_other_devicelist = false; + } else if (msg.counterpart != null && (msg.counterpart.equals_bare(jid) || occupants.contains(jid))) { + state.waiting_other_devicelists--; } if (state.should_retry_now()) { send_now.add(msg); @@ -187,6 +215,7 @@ public class Manager : StreamInteractionModule, Object { if (module == null) { return; } + ArrayList device_list = module.get_device_list(jid); db.identity_meta.insert_device_list(account.id, jid.bare_jid.to_string(), device_list); int inc = 0; @@ -229,20 +258,23 @@ public class Manager : StreamInteractionModule, Object { HashSet send_now = new HashSet(); lock (message_states) { foreach (Entities.Message msg in message_states.keys) { + bool session_created = true; if (!msg.account.equals(account)) continue; + Gee.List occupants = get_occupants(msg.counterpart.bare_jid, account); + MessageState state = message_states[msg]; if (trusted != Database.IdentityMetaTable.TrustLevel.TRUSTED && trusted != Database.IdentityMetaTable.TrustLevel.VERIFIED) { module.untrust_device(jid, device_id); } else { - if(account.bare_jid.equals(jid) || (msg.counterpart != null && msg.counterpart.equals_bare(jid))) { + if(account.bare_jid.equals(jid) || (msg.counterpart != null && (msg.counterpart.equals_bare(jid) || occupants.contains(jid)))) { session_created = module.start_session(stream, jid, device_id, bundle); } } if (account.bare_jid.equals(jid) && session_created) { state.waiting_own_sessions--; - } else if (msg.counterpart != null && msg.counterpart.equals_bare(jid) && session_created) { + } else if (msg.counterpart != null && (msg.counterpart.equals_bare(jid) || occupants.contains(jid)) && session_created) { state.waiting_other_sessions--; } if (state.should_retry_now()){ @@ -303,7 +335,8 @@ public class Manager : StreamInteractionModule, Object { if (stream == null) return false; StreamModule? module = ((!)stream).get_module(StreamModule.IDENTITY); if (module == null) return false; - return ((!)module).is_known_address(conversation.counterpart.bare_jid); + //return ((!)module).is_known_address(conversation.counterpart.bare_jid); + return true; } public static void start(StreamInteractor stream_interactor, Database db) { diff --git a/plugins/omemo/src/stream_module.vala b/plugins/omemo/src/stream_module.vala index 2c792a2f..13406738 100644 --- a/plugins/omemo/src/stream_module.vala +++ b/plugins/omemo/src/stream_module.vala @@ -21,23 +21,35 @@ public class StreamModule : XmppStreamModule { private ConcurrentSet active_devicelist_requests = new ConcurrentSet(); private Map> device_lists = new HashMap>(Jid.hash_bare_func, Jid.equals_bare_func); private Map> ignored_devices = new HashMap>(Jid.hash_bare_func, Jid.equals_bare_func); + private Map> occupants = new HashMap>(Jid.hash_bare_func, Jid.equals_bare_func); private ReceivedPipelineListener received_pipeline_listener; public signal void store_created(Store store); public signal void device_list_loaded(Jid jid); public signal void bundle_fetched(Jid jid, int device_id, Bundle bundle); - public EncryptState encrypt(MessageStanza message, Jid self_jid) { + public EncryptState encrypt(MessageStanza message, Jid self_jid, Gee.List recipients) { EncryptState status = new EncryptState(); if (!Plugin.ensure_context()) return status; if (message.to == null) return status; + + if(message.type_ == MessageStanza.TYPE_GROUPCHAT) { + occupants[message.to] = recipients; + } + try { if (!device_lists.has_key(self_jid)) return status; status.own_list = true; status.own_devices = device_lists.get(self_jid).size; - if (!device_lists.has_key(message.to)) return status; - status.other_list = true; - status.other_devices = device_lists.get(message.to).size; + status.other_waiting_lists = 0; + status.other_devices = 0; + foreach (Jid recipient in recipients) { + if (!device_lists.has_key(recipient)) { + status.other_waiting_lists++; + return status; + } + status.other_devices += device_lists.get(recipient).size; + } if (status.own_devices == 0 || status.other_devices == 0) return status; uint8[] key = new uint8[16]; @@ -57,19 +69,22 @@ public class StreamModule : XmppStreamModule { .put_node(new StanzaNode.text(Base64.encode(ciphertext)))); Address address = new Address(message.to.bare_jid.to_string(), 0); - foreach(int32 device_id in device_lists[message.to]) { - if (is_ignored_device(message.to, device_id)) { - status.other_lost++; - continue; - } - try { - address.device_id = (int) device_id; - StanzaNode key_node = create_encrypted_key(key, address); - header.put_node(key_node); - status.other_success++; - } catch (Error e) { - if (e.code == ErrorCode.UNKNOWN) status.other_unknown++; - else status.other_failure++; + foreach (Jid recipient in recipients) { + foreach(int32 device_id in device_lists[recipient]) { + if (is_ignored_device(recipient, device_id)) { + status.other_lost++; + continue; + } + try { + address.name = recipient.bare_jid.to_string(); + address.device_id = (int) device_id; + StanzaNode key_node = create_encrypted_key(key, address); + header.put_node(key_node); + status.other_success++; + } catch (Error e) { + if (e.code == ErrorCode.UNKNOWN) status.other_unknown++; + else status.other_failure++; + } } } address.name = self_jid.bare_jid.to_string(); @@ -147,9 +162,18 @@ public class StreamModule : XmppStreamModule { } public void request_user_devicelist(XmppStream stream, Jid jid) { - if (active_devicelist_requests.add(jid)) { - if (Plugin.DEBUG) print(@"OMEMO: requesting device list for $jid\n"); - stream.get_module(Pubsub.Module.IDENTITY).request(stream, jid, NODE_DEVICELIST, (stream, jid, id, node) => on_devicelist(stream, jid, id, node)); + Gee.List recipients; + if (occupants.contains(jid)) { + recipients = occupants.get(jid); + } else { + recipients = new ArrayList(Jid.equals_bare_func); + recipients.add(jid); + } + foreach (Jid recipient in recipients) { + if (active_devicelist_requests.add(recipient)) { + if (Plugin.DEBUG) print(@"OMEMO: requesting device list for $jid\n"); + stream.get_module(Pubsub.Module.IDENTITY).request(stream, recipient, NODE_DEVICELIST, (stream, jid, id, node) => on_devicelist(stream, jid, id, node)); + } } } @@ -184,23 +208,32 @@ public class StreamModule : XmppStreamModule { } public void fetch_bundles(XmppStream stream, Jid jid) { - if (!device_lists.has_key(jid)) { - return; + Gee.List recipients; + if (occupants.contains(jid)) { + recipients = occupants.get(jid); + } else { + recipients = new ArrayList(Jid.equals_bare_func); + recipients.add(jid); } - Address address = new Address(jid.bare_jid.to_string(), 0); - foreach(int32 device_id in device_lists[jid]) { - if (!is_ignored_device(jid, device_id)) { - address.device_id = device_id; - try { - if (!store.contains_session(address)) { - fetch_bundle(stream, jid, device_id); + foreach (Jid recipient in recipients) { + if (!device_lists.has_key(recipient)) { + return; + } + Address address = new Address(recipient.bare_jid.to_string(), 0); + foreach(int32 device_id in device_lists[recipient]) { + if (!is_ignored_device(recipient, device_id)) { + address.device_id = device_id; + try { + if (!store.contains_session(address)) { + fetch_bundle(stream, recipient, device_id); + } + } catch (Error e) { + // Ignore } - } catch (Error e) { - // Ignore } } + address.device_id = 0; } - address.device_id = 0; } public void fetch_bundle(XmppStream stream, Jid jid, int device_id) { -- cgit v1.2.3-54-g00ecf