diff options
author | Samuel Hand <samuel.hand@openmailbox.org> | 2018-06-11 07:11:04 +0100 |
---|---|---|
committer | Samuel Hand <samuel.hand@openmailbox.org> | 2018-06-11 07:11:22 +0100 |
commit | 40c6835600cc6ebcb816f9aee4a6540ef8e362f0 (patch) | |
tree | a3bcbb33edeb469e1b7c17241a5cc40189287056 /plugins/omemo/src | |
parent | 630df3a2ee3ac5086ab15ecbb4da88e0e2e9c82d (diff) | |
download | dino-40c6835600cc6ebcb816f9aee4a6540ef8e362f0.tar.gz dino-40c6835600cc6ebcb816f9aee4a6540ef8e362f0.zip |
Add trust management utilities to the omemo plugin
Diffstat (limited to 'plugins/omemo/src')
-rw-r--r-- | plugins/omemo/src/contact_details_dialog.vala | 130 | ||||
-rw-r--r-- | plugins/omemo/src/contact_details_provider.vala | 23 | ||||
-rw-r--r-- | plugins/omemo/src/database.vala | 46 | ||||
-rw-r--r-- | plugins/omemo/src/device_notification_populator.vala | 92 | ||||
-rw-r--r-- | plugins/omemo/src/manager.vala | 76 | ||||
-rw-r--r-- | plugins/omemo/src/plugin.vala | 5 | ||||
-rw-r--r-- | plugins/omemo/src/stream_module.vala | 75 |
7 files changed, 400 insertions, 47 deletions
diff --git a/plugins/omemo/src/contact_details_dialog.vala b/plugins/omemo/src/contact_details_dialog.vala new file mode 100644 index 00000000..3b60580e --- /dev/null +++ b/plugins/omemo/src/contact_details_dialog.vala @@ -0,0 +1,130 @@ +using Gtk; +using Xmpp; +using Gee; +using Qlite; +using Dino.Entities; + +namespace Dino.Plugins.Omemo { + +[GtkTemplate (ui = "/im/dino/Dino/omemo/contact_details_dialog.ui")] +public class ContactDetailsDialog : Gtk.Dialog { + + private Plugin plugin; + private Account account; + private Jid jid; + + [GtkChild] private Grid fingerprints; + [GtkChild] private Box fingerprints_prompt_label; + [GtkChild] private Frame fingerprints_prompt_container; + [GtkChild] private Grid fingerprints_prompt; + + + private void set_device_trust(Row device, bool trust) { + plugin.db.identity_meta.update() + .with(plugin.db.identity_meta.identity_id, "=", account.id) + .with(plugin.db.identity_meta.address_name, "=", device[plugin.db.identity_meta.address_name]) + .with(plugin.db.identity_meta.device_id, "=", device[plugin.db.identity_meta.device_id]) + .set(plugin.db.identity_meta.trusted_identity, trust).perform(); + + if(!trust) { + plugin.app.stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).untrust_device(jid, device[plugin.db.identity_meta.device_id]); + } else { + plugin.app.stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).trust_device(jid, device[plugin.db.identity_meta.device_id]); + } + } + + private void add_fingerprint(Row device, int row, bool trust) { + string res = fingerprint_markup(fingerprint_from_base64(device[plugin.db.identity_meta.identity_key_public_base64])); + Label lbl = new Label(res) + { use_markup=true, justify=Justification.RIGHT, visible=true, margin = 8, halign = Align.START, valign = Align.CENTER }; + Switch tgl = new Switch() {visible = true, halign = Align.END, valign = Align.CENTER, margin = 8, hexpand = true, active = trust }; + tgl.state_set.connect((active) => { + set_device_trust(device, active); + + return false; + }); + + fingerprints.attach(lbl, 0, row); + fingerprints.attach(tgl, 1, row); + } + + public ContactDetailsDialog(Plugin plugin, Account account, Jid jid) { + Object(use_header_bar : 1); + this.plugin = plugin; + this.account = account; + this.jid = jid; + + int i = 0; + foreach (Row device in plugin.db.identity_meta.with_address(jid.to_string()).without_null(plugin.db.identity_meta.trusted_identity).with(plugin.db.identity_meta.identity_id, "=", account.id)) { + if(device[plugin.db.identity_meta.identity_key_public_base64] == null) + continue; + add_fingerprint(device, i, device[plugin.db.identity_meta.trusted_identity]); + + i++; + + } + + int j = 0; + foreach (Row device in plugin.db.identity_meta.with_address(jid.to_string()).with_null(plugin.db.identity_meta.trusted_identity).with(plugin.db.identity_meta.identity_id, "=", account.id)) { + if(device[plugin.db.identity_meta.identity_key_public_base64] == null) + continue; + + string res = fingerprint_markup(fingerprint_from_base64(device[plugin.db.identity_meta.identity_key_public_base64])); + Label lbl = new Label(res) + { use_markup=true, justify=Justification.RIGHT, visible=true, margin = 8, halign = Align.START }; + + Box box = new Box(Gtk.Orientation.HORIZONTAL, 0) { visible = true, valign = Align.CENTER, hexpand = true, margin = 8 }; + + Button yes = new Button() { visible = true, valign = Align.CENTER, hexpand = true}; + yes.image = new Image.from_icon_name("list-add-symbolic", IconSize.BUTTON); + + yes.clicked.connect(() => { + set_device_trust(device, true); + + fingerprints_prompt.remove(box); + fingerprints_prompt.remove(lbl); + j--; + + add_fingerprint(device, i, true); + i++; + + if(j == 0) + fingerprints_prompt.attach(new Label("No more new devices") { visible = true, valign = Align.CENTER, halign = Align.CENTER, margin = 8, hexpand = true }, 0, 0); + }); + + Button no = new Button() { visible = true, valign = Align.CENTER, hexpand = true}; + no.image = new Image.from_icon_name("list-remove-symbolic", IconSize.BUTTON); + + no.clicked.connect(() => { + set_device_trust(device, false); + + fingerprints_prompt.remove(box); + fingerprints_prompt.remove(lbl); + j--; + + add_fingerprint(device, i, false); + i++; + + if(j == 0) + fingerprints_prompt.attach(new Label("No more new devices") { visible = true, valign = Align.CENTER, halign = Align.CENTER, margin = 8, hexpand = true }, 0, 0); + }); + + box.pack_start(yes); + box.pack_start(no); + + box.get_style_context().add_class("linked"); + + fingerprints_prompt.attach(lbl, 0, j); + fingerprints_prompt.attach(box, 1, j); + j++; + } + if( j > 0 ){ + fingerprints_prompt_label.visible = true; + fingerprints_prompt_container.visible = true; + } + + } + +} + +} diff --git a/plugins/omemo/src/contact_details_provider.vala b/plugins/omemo/src/contact_details_provider.vala index 05b85d9f..2c6c4944 100644 --- a/plugins/omemo/src/contact_details_provider.vala +++ b/plugins/omemo/src/contact_details_provider.vala @@ -1,4 +1,5 @@ using Gtk; +using Gee; using Qlite; using Dino.Entities; @@ -15,20 +16,28 @@ public class ContactDetailsProvider : Plugins.ContactDetailsProvider, Object { public void populate(Conversation conversation, Plugins.ContactDetails contact_details, WidgetType type) { if (conversation.type_ == Conversation.Type.CHAT && type == WidgetType.GTK) { - string res = ""; + int i = 0; foreach (Row row in plugin.db.identity_meta.with_address(conversation.counterpart.to_string())) { if (row[plugin.db.identity_meta.identity_key_public_base64] != null) { - if (i != 0) { - res += "\n\n"; - } - res += fingerprint_markup(fingerprint_from_base64(row[plugin.db.identity_meta.identity_key_public_base64])); i++; } } + if (i > 0) { - Label label = new Label(res) { use_markup=true, justify=Justification.RIGHT, selectable=true, visible=true }; - contact_details.add(_("Encryption"), "OMEMO", n("%d OMEMO device", "%d OMEMO devices", i).printf(i), label); + Button btn = new Button(); + btn.image = new Image.from_icon_name("view-list-symbolic", IconSize.BUTTON); + btn.relief = ReliefStyle.NONE; + btn.visible = true; + btn.valign = Align.CENTER; + btn.clicked.connect(() => { + btn.activate(); + ContactDetailsDialog dialog = new ContactDetailsDialog(plugin, conversation.account, conversation.counterpart); + dialog.set_transient_for((Window) btn.get_toplevel()); + dialog.present(); + }); + + contact_details.add(_("Encryption"), "OMEMO", n("%d OMEMO device", "%d OMEMO devices", i).printf(i), btn); } } } diff --git a/plugins/omemo/src/database.vala b/plugins/omemo/src/database.vala index 5c7309f3..2b295322 100644 --- a/plugins/omemo/src/database.vala +++ b/plugins/omemo/src/database.vala @@ -9,28 +9,30 @@ public class Database : Qlite.Database { private const int VERSION = 1; public class IdentityMetaTable : Table { + public Column<int> identity_id = new Column.Integer("identity_id") { not_null = true }; public Column<string> address_name = new Column.Text("address_name") { not_null = true }; public Column<int> device_id = new Column.Integer("device_id") { not_null = true }; public Column<string?> identity_key_public_base64 = new Column.Text("identity_key_public_base64"); - public Column<bool> trusted_identity = new Column.BoolInt("trusted_identity") { default = "0" }; + public Column<bool> trusted_identity = new Column.BoolInt("trusted_identity"); public Column<bool> now_active = new Column.BoolInt("now_active") { default = "1" }; public Column<long> last_active = new Column.Long("last_active"); internal IdentityMetaTable(Database db) { base(db, "identity_meta"); - init({address_name, device_id, identity_key_public_base64, trusted_identity, now_active, last_active}); - index("identity_meta_idx", {address_name, device_id}, true); - index("identity_meta_list_idx", {address_name}); + init({identity_id, address_name, device_id, identity_key_public_base64, trusted_identity, now_active, last_active}); + index("identity_meta_idx", {identity_id, address_name, device_id}, true); + index("identity_meta_list_idx", {identity_id, address_name}); } public QueryBuilder with_address(string address_name) { return select().with(this.address_name, "=", address_name); } - public void insert_device_list(string address_name, ArrayList<int32> devices) { + public void insert_device_list(int32 identity_id, string address_name, ArrayList<int32> devices) { update().with(this.address_name, "=", address_name).set(now_active, false).perform(); foreach (int32 device_id in devices) { upsert() + .value(this.identity_id, identity_id, true) .value(this.address_name, address_name, true) .value(this.device_id, device_id, true) .value(this.now_active, true) @@ -39,13 +41,35 @@ public class Database : Qlite.Database { } } - public int64 insert_device_bundle(string address_name, int device_id, Bundle bundle) { + public int64 insert_device_bundle(int32 identity_id, string address_name, int device_id, Bundle bundle, bool? trust) { if (bundle == null || bundle.identity_key == null) return -1; - return upsert() + UpsertBuilder query = upsert() + .value(this.identity_id, identity_id, true) .value(this.address_name, address_name, true) .value(this.device_id, device_id, true) - .value(this.identity_key_public_base64, Base64.encode(bundle.identity_key.serialize())) - .perform(); + .value(this.identity_key_public_base64, Base64.encode(bundle.identity_key.serialize())); + if(trust != null) + query.value(this.trusted_identity, trust); + return query.perform(); + } + } + + + public class TrustTable : Table { + public Column<int> identity_id = new Column.Integer("identity_id") { not_null = true }; + public Column<string> address_name = new Column.Text("address_name"); + public Column<bool> blind_trust = new Column.BoolInt("blind_trust") { default = "1" } ; + + internal TrustTable(Database db) { + base(db, "trust"); + init({identity_id, address_name, blind_trust}); + index("trust_idx", {identity_id, address_name}, true); + } + + public bool get_blind_trust(int32 identity_id, string address_name) { + return this.select().with(this.identity_id, "=", identity_id) + .with(this.address_name, "=", address_name) + .with(this.blind_trust, "=", true).count() > 0; } } @@ -103,6 +127,7 @@ public class Database : Qlite.Database { } public IdentityMetaTable identity_meta { get; private set; } + public TrustTable trust { get; private set; } public IdentityTable identity { get; private set; } public SignedPreKeyTable signed_pre_key { get; private set; } public PreKeyTable pre_key { get; private set; } @@ -111,11 +136,12 @@ public class Database : Qlite.Database { public Database(string fileName) { base(fileName, VERSION); identity_meta = new IdentityMetaTable(this); + trust = new TrustTable(this); identity = new IdentityTable(this); signed_pre_key = new SignedPreKeyTable(this); pre_key = new PreKeyTable(this); session = new SessionTable(this); - init({identity_meta, identity, signed_pre_key, pre_key, session}); + init({identity_meta, trust, identity, signed_pre_key, pre_key, session}); try { exec("PRAGMA synchronous=0"); } catch (Error e) { } diff --git a/plugins/omemo/src/device_notification_populator.vala b/plugins/omemo/src/device_notification_populator.vala new file mode 100644 index 00000000..6b3e668b --- /dev/null +++ b/plugins/omemo/src/device_notification_populator.vala @@ -0,0 +1,92 @@ +using Dino.Entities; +using Xmpp; +using Gtk; + +namespace Dino.Plugins.Omemo { + +public class DeviceNotificationPopulator : ConversationItemPopulator, Object { + + public string id { get { return "device_notification"; } } + + private StreamInteractor? stream_interactor; + private Plugin plugin; + private Conversation? current_conversation; + private ConversationItemCollection? item_collection; + private ConversationNotification notification; + + public DeviceNotificationPopulator(Plugin plugin, StreamInteractor stream_interactor) { + this.stream_interactor = stream_interactor; + this.plugin = plugin; + } + + public bool has_new_devices(Jid jid) { + return plugin.db.identity_meta.with_address(jid.bare_jid.to_string()).with(plugin.db.identity_meta.identity_id, "=", current_conversation.account.id).with_null(plugin.db.identity_meta.trusted_identity).without_null(plugin.db.identity_meta.identity_key_public_base64).count() > 0; + } + + public void init(Conversation conversation, ConversationItemCollection item_collection, Plugins.WidgetType type) { + current_conversation = conversation; + this.item_collection = item_collection; + stream_interactor.module_manager.get_module(conversation.account, StreamModule.IDENTITY).device_list_loaded.connect((jid) => { + if(jid == conversation.counterpart && has_new_devices(conversation.counterpart) && conversation.type_ == Conversation.Type.CHAT) + display_notification(); + }); + if (has_new_devices(conversation.counterpart) && conversation.type_ == Conversation.Type.CHAT) + display_notification(); + } + + public void close(Conversation conversation) { } + + public void populate_timestamp(Conversation conversation, DateTime from, DateTime to) { } + + public void populate_between_widgets(Conversation conversation, DateTime from, DateTime to) { } + + private void display_notification() { + if(notification == null) { + notification = new ConversationNotification(plugin, current_conversation.account, current_conversation.counterpart); + notification.should_hide.connect(should_hide); + item_collection.add_meta_notification(notification); + } + } + + private void should_hide() { + if (!has_new_devices(current_conversation.counterpart)){ + item_collection.remove_meta_notification(notification); + notification = null; + } + } +} + +private class ConversationNotification : MetaConversationNotification { + private Widget widget; + private Plugin plugin; + private Jid jid; + private Account account; + public signal void should_hide(); + + public ConversationNotification(Plugin plugin, Account account, Jid jid) { + this.plugin = plugin; + this.jid = jid; + this.account = account; + + Box box = new Box(Orientation.HORIZONTAL, 5) { visible=true }; + Button manage_button = new Button() { label=_("Manage"), visible=true }; + manage_button.clicked.connect(() => { + manage_button.activate(); + ContactDetailsDialog dialog = new ContactDetailsDialog(plugin, account, jid); + dialog.set_transient_for((Window) manage_button.get_toplevel()); + dialog.response.connect((response_type) => { + should_hide(); + }); + dialog.present(); + }); + box.add(new Label(_("This contact has new devices")) { margin_end=10, visible=true }); + box.add(manage_button); + widget = box; + } + + public override Object? get_widget(WidgetType type) { + return widget; + } +} + +} diff --git a/plugins/omemo/src/manager.vala b/plugins/omemo/src/manager.vala index eb0c6378..3047410c 100644 --- a/plugins/omemo/src/manager.vala +++ b/plugins/omemo/src/manager.vala @@ -116,6 +116,7 @@ public class Manager : StreamInteractionModule, Object { if (!state.will_send_now) { if (message.marked == Entities.Message.Marked.WONTSEND) { if (Plugin.DEBUG) print(@"OMEMO: message was not sent: $state\n"); + message_states.unset(message); } else { if (Plugin.DEBUG) print(@"OMEMO: message will be delayed: $state\n"); @@ -206,7 +207,7 @@ public class Manager : StreamInteractionModule, Object { return; } ArrayList<int32> device_list = module.get_device_list(jid); - db.identity_meta.insert_device_list(jid.bare_jid.to_string(), device_list); + db.identity_meta.insert_device_list(account.id, jid.bare_jid.to_string(), device_list); int inc = 0; foreach (Row row in db.identity_meta.with_address(jid.bare_jid.to_string()).with_null(db.identity_meta.identity_key_public_base64)) { module.fetch_bundle(stream, Jid.parse(row[db.identity_meta.address_name]), row[db.identity_meta.device_id]); @@ -215,10 +216,81 @@ public class Manager : StreamInteractionModule, Object { if (inc > 0) { if (Plugin.DEBUG) print(@"OMEMO: new bundles $inc/$(device_list.size) for $jid\n"); } + + if(db.trust.select().with(db.trust.identity_id, "=", account.id).with(db.trust.address_name, " =", jid.bare_jid.to_string()).count() == 0) + db.trust.insert().value(db.trust.identity_id, account.id).value(db.trust.address_name, jid .bare_jid.to_string()).value(db.trust.blind_trust, true).perform(); + } public void on_bundle_fetched(Account account, Jid jid, int32 device_id, Bundle bundle) { - db.identity_meta.insert_device_bundle(jid.bare_jid.to_string(), device_id, bundle); + bool blind_trust = db.trust.get_blind_trust(account.id, jid.bare_jid.to_string()); + + bool untrust = !(blind_trust || db.identity_meta.with_address(jid.bare_jid.to_string()) + .with(db.identity_meta.identity_id, "=", account.id) + .with(db.identity_meta.device_id, "=", device_id) + .with(db.identity_meta.identity_key_public_base64, "=", Base64.encode(bundle.identity_key.serialize())) + .count() > 0); + + bool? trusted = null; + foreach(Row row in db.identity_meta.with_address(jid.bare_jid.to_string()) + .with(db.identity_meta.identity_id, "=", account.id) + .with(db.identity_meta.device_id, "=", device_id)) { + trusted = row[db.identity_meta.trusted_identity]; + break; + } + + if(db.identity_meta.with_address(jid.bare_jid.to_string()) + .with(db.identity_meta.identity_id, "=", account.id) + .with(db.identity_meta.device_id, "=", device_id) + .with_null(db.identity_meta.trusted_identity).count() > 0) + trusted = null; + + if(untrust) + trusted = null; + else if (blind_trust && trusted == null) + trusted = true; + + db.identity_meta.insert_device_bundle(account.id, jid.bare_jid.to_string(), device_id, bundle, trusted); + + if(trusted == null) + trusted = false; + + XmppStream? stream = stream_interactor.get_stream(account); + if(stream == null) return; + StreamModule? module = ((!)stream).get_module(StreamModule.IDENTITY); + if(module == null) return; + + HashSet<Entities.Message> send_now = new HashSet<Entities.Message>(); + bool session_needed = false; + lock (message_states) { + foreach (Entities.Message msg in message_states.keys) { + bool session_created = true; + if (!msg.account.equals(account)) continue; + MessageState state = message_states[msg]; + + if (trusted != true) { + module.untrust_device(jid, device_id); + } else { + if(account.bare_jid.equals(jid) || (msg.counterpart != null && msg.counterpart.equals_bare(jid))) + session_created = module.create_session_if_needed(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) { + state.waiting_other_sessions--; + } + if (state.should_retry_now()){ + send_now.add(msg); + state.active_send_attempt = true; + } + } + } + foreach (Entities.Message msg in send_now) { + if (msg.counterpart == null) continue; + Entities.Conversation? conv = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation((!)msg.counterpart, account); + if (conv == null) continue; + stream_interactor.get_module(MessageProcessor.IDENTITY).send_xmpp_message(msg, (!)conv, true); + } } private void on_store_created(Account account, Store store) { diff --git a/plugins/omemo/src/plugin.vala b/plugins/omemo/src/plugin.vala index b9ce500d..6c66814d 100644 --- a/plugins/omemo/src/plugin.vala +++ b/plugins/omemo/src/plugin.vala @@ -4,7 +4,7 @@ extern const string LOCALE_INSTALL_DIR; namespace Dino.Plugins.Omemo { public class Plugin : RootInterface, Object { - public const bool DEBUG = false; + public const bool DEBUG = true; private static Signal.Context? _context; public static Signal.Context get_context() { assert(_context != null); @@ -28,6 +28,7 @@ public class Plugin : RootInterface, Object { public EncryptionListEntry list_entry; public AccountSettingsEntry settings_entry; public ContactDetailsProvider contact_details_provider; + public DeviceNotificationPopulator device_notification_populator; public void registered(Dino.Application app) { ensure_context(); @@ -36,9 +37,11 @@ public class Plugin : RootInterface, Object { this.list_entry = new EncryptionListEntry(this); this.settings_entry = new AccountSettingsEntry(this); this.contact_details_provider = new ContactDetailsProvider(this); + this.device_notification_populator = new DeviceNotificationPopulator(this, this.app.stream_interactor); this.app.plugin_registry.register_encryption_list_entry(list_entry); this.app.plugin_registry.register_account_settings_entry(settings_entry); this.app.plugin_registry.register_contact_details_entry(contact_details_provider); + this.app.plugin_registry.register_conversation_item_populator(device_notification_populator); this.app.stream_interactor.module_manager.initialize_account_modules.connect((account, list) => { list.add(new StreamModule()); }); diff --git a/plugins/omemo/src/stream_module.vala b/plugins/omemo/src/stream_module.vala index 6e6772ca..a84c3582 100644 --- a/plugins/omemo/src/stream_module.vala +++ b/plugins/omemo/src/stream_module.vala @@ -103,6 +103,23 @@ public class StreamModule : XmppStreamModule { return status; } + public void untrust_device(Jid jid, int device_id) { + if(device_lists.has_key(jid) && device_lists[jid].contains(device_id)) + device_lists[jid].remove(device_id); + if(store.contains_session(new Address(jid.bare_jid.to_string(), device_id))) + store.delete_session(new Address(jid.bare_jid.to_string(), device_id)); + } + + public void trust_device(Jid jid, int device_id) { + if(is_ignored_device(jid, device_id)){ + ignored_devices[jid].remove(device_id); + } + if(!device_lists.has_key(jid)) + device_lists[jid] = new ArrayList<int32>(); + if(!device_lists[jid].contains(device_id)) + device_lists[jid].add(device_id); + } + private StanzaNode create_encrypted_key(uint8[] key, Address address) throws GLib.Error { SessionCipher cipher = store.create_session_cipher(address); CiphertextMessage device_key = cipher.encrypt(key); @@ -223,7 +240,7 @@ public class StreamModule : XmppStreamModule { } ignored_devices[jid].add(device_id); } - session_start_failed(jid, device_id); + //session_start_failed(jid, device_id); } public bool is_ignored_device(Jid jid, int32 device_id) { @@ -234,47 +251,51 @@ public class StreamModule : XmppStreamModule { } private void on_other_bundle_result(XmppStream stream, Jid jid, int device_id, string? id, StanzaNode? node) { - bool fail = false; if (node == null) { // Device not registered, shouldn't exist - fail = true; + stream.get_module(IDENTITY).ignore_device(jid, device_id); } else { Bundle bundle = new Bundle(node); bundle_fetched(jid, device_id, bundle); - int32 signed_pre_key_id = bundle.signed_pre_key_id; - ECPublicKey? signed_pre_key = bundle.signed_pre_key; - uint8[] signed_pre_key_signature = bundle.signed_pre_key_signature; - ECPublicKey? identity_key = bundle.identity_key; + } + stream.get_module(IDENTITY).active_bundle_requests.remove(jid.bare_jid.to_string() + @":$device_id"); + } - ArrayList<Bundle.PreKey> pre_keys = bundle.pre_keys; - if (signed_pre_key_id < 0 || signed_pre_key == null || identity_key == null || pre_keys.size == 0) { + public bool create_session_if_needed(XmppStream stream, Jid jid, int32 device_id, Bundle bundle) { + bool fail = false; + int32 signed_pre_key_id = bundle.signed_pre_key_id; + ECPublicKey? signed_pre_key = bundle.signed_pre_key; + uint8[] signed_pre_key_signature = bundle.signed_pre_key_signature; + ECPublicKey? identity_key = bundle.identity_key; + + ArrayList<Bundle.PreKey> pre_keys = bundle.pre_keys; + if (signed_pre_key_id < 0 || signed_pre_key == null || identity_key == null || pre_keys.size == 0) { + fail = true; + } else { + int pre_key_idx = Random.int_range(0, pre_keys.size); + int32 pre_key_id = pre_keys[pre_key_idx].key_id; + ECPublicKey? pre_key = pre_keys[pre_key_idx].key; + if (pre_key_id < 0 || pre_key == null) { fail = true; } else { - int pre_key_idx = Random.int_range(0, pre_keys.size); - int32 pre_key_id = pre_keys[pre_key_idx].key_id; - ECPublicKey? pre_key = pre_keys[pre_key_idx].key; - if (pre_key_id < 0 || pre_key == null) { - fail = true; - } else { - Address address = new Address(jid.bare_jid.to_string(), device_id); - try { - if (store.contains_session(address)) { - return; - } - SessionBuilder builder = store.create_session_builder(address); - builder.process_pre_key_bundle(create_pre_key_bundle(device_id, device_id, pre_key_id, pre_key, signed_pre_key_id, signed_pre_key, signed_pre_key_signature, identity_key)); - stream.get_module(IDENTITY).session_started(jid, device_id); - } catch (Error e) { - fail = true; + Address address = new Address(jid.bare_jid.to_string(), device_id); + try { + if (store.contains_session(address)) { + return false; } - address.device_id = 0; // TODO: Hack to have address obj live longer + SessionBuilder builder = store.create_session_builder(address); + builder.process_pre_key_bundle(create_pre_key_bundle(device_id, device_id, pre_key_id, pre_key, signed_pre_key_id, signed_pre_key, signed_pre_key_signature, identity_key)); + //stream.get_module(IDENTITY).session_started(jid, device_id); + } catch (Error e) { + fail = true; } + address.device_id = 0; // TODO: Hack to have address obj live longer } } if (fail) { stream.get_module(IDENTITY).ignore_device(jid, device_id); } - stream.get_module(IDENTITY).active_bundle_requests.remove(jid.bare_jid.to_string() + @":$device_id"); + return true; } public void publish_bundles_if_needed(XmppStream stream, Jid jid) { |