diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/omemo/CMakeLists.txt | 9 | ||||
-rw-r--r-- | plugins/omemo/data/account_settings_dialog.ui | 124 | ||||
-rw-r--r-- | plugins/omemo/data/contact_details_dialog.ui | 206 | ||||
-rw-r--r-- | plugins/omemo/data/manage_key_dialog.ui | 156 | ||||
-rw-r--r-- | plugins/omemo/src/account_settings_dialog.vala | 54 | ||||
-rw-r--r-- | plugins/omemo/src/account_settings_widget.vala | 2 | ||||
-rw-r--r-- | plugins/omemo/src/contact_details_dialog.vala | 235 | ||||
-rw-r--r-- | plugins/omemo/src/contact_details_provider.vala | 28 | ||||
-rw-r--r-- | plugins/omemo/src/database.vala | 65 | ||||
-rw-r--r-- | plugins/omemo/src/device_notification_populator.vala | 92 | ||||
-rw-r--r-- | plugins/omemo/src/encrypt_state.vala | 6 | ||||
-rw-r--r-- | plugins/omemo/src/manage_key_dialog.vala | 193 | ||||
-rw-r--r-- | plugins/omemo/src/manager.vala | 192 | ||||
-rw-r--r-- | plugins/omemo/src/own_notifications.vala | 38 | ||||
-rw-r--r-- | plugins/omemo/src/plugin.vala | 4 | ||||
-rw-r--r-- | plugins/omemo/src/stream_module.vala | 194 | ||||
-rw-r--r-- | plugins/omemo/src/trust_manager.vala | 168 | ||||
-rw-r--r-- | plugins/signal-protocol/src/store.vala | 6 |
18 files changed, 1362 insertions, 410 deletions
diff --git a/plugins/omemo/CMakeLists.txt b/plugins/omemo/CMakeLists.txt index 50cd6627..5126d05b 100644 --- a/plugins/omemo/CMakeLists.txt +++ b/plugins/omemo/CMakeLists.txt @@ -12,7 +12,8 @@ find_packages(OMEMO_PACKAGES REQUIRED ) set(RESOURCE_LIST - account_settings_dialog.ui + contact_details_dialog.ui + manage_key_dialog.ui ) compile_gresources( @@ -27,14 +28,17 @@ compile_gresources( vala_precompile(OMEMO_VALA_C SOURCES - src/account_settings_dialog.vala src/account_settings_entry.vala src/account_settings_widget.vala src/bundle.vala src/contact_details_provider.vala + src/contact_details_dialog.vala src/database.vala + src/device_notification_populator.vala + src/own_notifications.vala src/encrypt_state.vala src/encryption_list_entry.vala + src/manage_key_dialog.vala src/manager.vala src/message_flag.vala src/plugin.vala @@ -43,6 +47,7 @@ SOURCES src/session_store.vala src/signed_pre_key_store.vala src/stream_module.vala + src/trust_manager.vala src/util.vala CUSTOM_VAPIS ${CMAKE_BINARY_DIR}/exports/signal-protocol.vapi diff --git a/plugins/omemo/data/account_settings_dialog.ui b/plugins/omemo/data/account_settings_dialog.ui deleted file mode 100644 index 31996d05..00000000 --- a/plugins/omemo/data/account_settings_dialog.ui +++ /dev/null @@ -1,124 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<interface> - <template class="DinoPluginsOmemoAccountSettingsDialog"> - <property name="modal">True</property> - <property name="title" translatable="yes">OMEMO Keys</property> - <child internal-child="vbox"> - <object class="GtkBox"> - <property name="visible">True</property> - <property name="margin-left">40</property> - <property name="margin-right">40</property> - <child> - <object class="GtkBox"> - <property name="margin-top">12</property> - <property name="orientation">horizontal</property> - <property name="visible">True</property> - <child> - <object class="GtkLabel"> - <property name="visible">True</property> - <property name="label" translatable="yes">Own fingerprint</property> - <property name="xalign">0</property> - <property name="yalign">1</property> - <property name="hexpand">True</property> - <property name="margin-bottom">2</property> - <attributes> - <attribute name="weight" value="PANGO_WEIGHT_BOLD"/> - </attributes> - </object> - </child> - <child> - <object class="GtkButton" id="copy_button"> - <property name="visible">True</property> - <property name="can-focus">False</property> - <style> - <class name="flat"/> - </style> - <signal name="clicked" handler="copy_button_clicked"/> - <child> - <object class="GtkImage"> - <property name="icon-name">edit-copy-symbolic</property> - <property name="icon-size">1</property> - <property name="visible">True</property> - </object> - </child> - </object> - </child> - <!--<child> - <object class="GtkButton" id="qr_button"> - <property name="visible">True</property> - <property name="can-focus">False</property> - <property name="sensitive">False</property> - <style> - <class name="flat"/> - </style> - <child> - <object class="GtkImage"> - <property name="icon-name">camera-photo-symbolic</property> - <property name="icon-size">1</property> - <property name="visible">True</property> - </object> - </child> - </object> - </child>--> - </object> - </child> - <child> - <object class="GtkFrame"> - <property name="visible">True</property> - <child> - <object class="GtkListBox"> - <property name="visible">True</property> - <property name="selection-mode">none</property> - <child> - <object class="GtkLabel" id="own_fingerprint"> - <property name="visible">True</property> - <property name="margin">8</property> - <property name="label">...</property> - </object> - </child> - </object> - </child> - </object> - </child> - <child> - <object class="GtkLabel"> - <property name="margin-top">12</property> - <property name="visible">True</property> - <property name="xalign">0</property> - <property name="label" translatable="yes">Other devices</property> - <property name="margin-bottom">2</property> - <attributes> - <attribute name="weight" value="PANGO_WEIGHT_BOLD"/> - </attributes> - </object> - </child> - <child> - <object class="GtkFrame"> - <property name="visible">True</property> - <property name="margin-bottom">18</property> - <child> - <object class="GtkScrolledWindow"> - <property name="hscrollbar_policy">never</property> - <property name="vscrollbar_policy">never</property> - <property name="visible">True</property> - <child> - <object class="GtkListBox" id="other_list"> - <property name="visible">True</property> - <property name="selection-mode">none</property> - <child> - <object class="GtkLabel"> - <property name="visible">True</property> - <property name="margin">8</property> - <property name="label" translatable="yes">- None -</property> - </object> - </child> - </object> - </child> - </object> - </child> - </object> - </child> - </object> - </child> - </template> -</interface>
\ No newline at end of file diff --git a/plugins/omemo/data/contact_details_dialog.ui b/plugins/omemo/data/contact_details_dialog.ui new file mode 100644 index 00000000..856c7af4 --- /dev/null +++ b/plugins/omemo/data/contact_details_dialog.ui @@ -0,0 +1,206 @@ +<?xml version="1.0" encoding="UTF-8"?> +<interface> + <template class="DinoPluginsOmemoContactDetailsDialog"> + <property name="modal">True</property> + <property name="title">OMEMO Key Management</property> + <property name="resizable">False</property> + <child internal-child="vbox"> + <object class="GtkBox"> + <property name="visible">True</property> + <property name="margin">12</property> + <property name="spacing">12</property> + <child> + <object class="GtkFrame"> + <property name="visible">True</property> + <child> + <object class="GtkListBox"> + <property name="visible">True</property> + <property name="selection-mode">none</property> + <child> + <object class="GtkListBoxRow"> + <property name="visible">True</property> + <property name="activatable">False</property> + <child> + <object class="GtkBox"> + <property name="visible">True</property> + <property name="orientation">horizontal</property> + <property name="margin-start">20</property> + <property name="margin-end">20</property> + <property name="margin-top">14</property> + <property name="margin-bottom">14</property> + <property name="spacing">40</property> + <child> + <object class="GtkBox"> + <property name="visible">True</property> + <property name="orientation">vertical</property> + <property name="hexpand">True</property> + <child> + <object class="GtkLabel"> + <property name="visible">True</property> + <property name="halign">start</property> + <property name="label" translatable="yes">Automatically accept new keys</property> + <attributes> + <attribute name="scale" value="1.1"/> + </attributes> + </object> + </child> + <child> + <object class="GtkLabel"> + <property name="visible">True</property> + <property name="max_width_chars">1</property> + <property name="expand">True</property> + <property name="xalign">0</property> + <property name="wrap">True</property> + <property name="label" translatable="yes">When this contact adds new encryption keys to their account, automatically accept them.</property> + <attributes> + <attribute name="scale" value="0.8"/> + </attributes> + <style> + <class name="dim-label"/> + </style> + </object> + </child> + </object> + </child> + <child> + <object class="GtkSwitch" id="auto_accept"> + <property name="visible">True</property> + <property name="halign">end</property> + <property name="valign">center</property> + </object> + </child> + </object> + </child> + </object> + </child> + </object> + </child> + </object> + </child> + <child> + <object class="GtkBox" id="own_fingerprint_container"> + <property name="visible">False</property> + <property name="orientation">vertical</property> + <property name="spacing">2</property> + <child> + <object class="GtkLabel"> + <property name="visible">True</property> + <property name="halign">start</property> + <property name="label" translatable="yes">Own key</property> + <attributes> + <attribute name="weight" value="PANGO_WEIGHT_BOLD"/> + </attributes> + </object> + </child> + <child> + <object class="GtkFrame"> + <property name="visible">True</property> + <child> + <object class="GtkListBox"> + <property name="visible">True</property> + <property name="selection-mode">none</property> + <child> + <object class="GtkListBoxRow"> + <property name="visible">True</property> + <property name="activatable">False</property> + <property name="hexpand">True</property> + <child> + <object class="GtkBox"> + <property name="visible">True</property> + <property name="margin-start">20</property> + <property name="margin-end">20</property> + <property name="margin-top">14</property> + <property name="margin-bottom">14</property> + <property name="orientation">horizontal</property> + <property name="hexpand">True</property> + <child> + <object class="GtkLabel" id="own_fingerprint"> + <property name="visible">True</property> + <property name="halign">start</property> + <property name="justify">right</property> + <property name="hexpand">True</property> + </object> + </child> + <child> + <object class="GtkButton" id="copy"> + <property name="visible">True</property> + <property name="halign">end</property> + <child> + <object class="GtkImage"> + <property name="visible">True</property> + <property name="icon-size">1</property> + <property name="icon-name">edit-copy-symbolic</property> + </object> + </child> + </object> + </child> + </object> + </child> + </object> + </child> + </object> + </child> + </object> + </child> + </object> + </child> + <child> + <object class="GtkBox" id="new_keys_container"> + <property name="visible">False</property> + <property name="orientation">vertical</property> + <property name="spacing">2</property> + <child> + <object class="GtkLabel"> + <property name="visible">True</property> + <property name="halign">start</property> + <property name="label" translatable="yes">New keys</property> + <attributes> + <attribute name="weight" value="PANGO_WEIGHT_BOLD"/> + </attributes> + </object> + </child> + <child> + <object class="GtkFrame"> + <property name="visible">True</property> + <child> + <object class="GtkListBox" id="new_keys"> + <property name="visible">True</property> + <property name="selection-mode">none</property> + </object> + </child> + </object> + </child> + </object> + </child> + <child> + <object class="GtkBox" id="keys_container"> + <property name="visible">False</property> + <property name="orientation">vertical</property> + <property name="spacing">2</property> + <child> + <object class="GtkLabel"> + <property name="visible">True</property> + <property name="halign">start</property> + <property name="label" translatable="yes">Associated keys</property> + <attributes> + <attribute name="weight" value="PANGO_WEIGHT_BOLD"/> + </attributes> + </object> + </child> + <child> + <object class="GtkFrame"> + <property name="visible">True</property> + <child> + <object class="GtkListBox" id="keys"> + <property name="visible">True</property> + <property name="selection-mode">none</property> + </object> + </child> + </object> + </child> + </object> + </child> + </object> + </child> + </template> +</interface> diff --git a/plugins/omemo/data/manage_key_dialog.ui b/plugins/omemo/data/manage_key_dialog.ui new file mode 100644 index 00000000..cd14e16c --- /dev/null +++ b/plugins/omemo/data/manage_key_dialog.ui @@ -0,0 +1,156 @@ +<?xml version="1.0" encoding="UTF-8"?> +<interface> + <template class="DinoPluginsOmemoManageKeyDialog"> + <property name="modal">True</property> + <property name="resizable">False</property> + <child type="titlebar"> + <object class="GtkHeaderBar" id="header_bar"> + <property name="visible">True</property> + <property name="title">Manage Key</property> + <property name="show_close_button">False</property> + <child> + <object class="GtkButton" id="cancel_button"> + <property name="label" translatable="yes">Cancel</property> + <property name="sensitive">True</property> + <property name="visible">True</property> + </object> + <packing> + <property name="pack_type">start</property> + </packing> + </child> + <child> + <object class="GtkButton" id="ok_button"> + <property name="has_default">True</property> + <property name="can_default">True</property> + <property name="label" translatable="yes">Confirm</property> + <property name="sensitive">False</property> + <property name="visible">True</property> + <style> + <class name="suggested-action"/> + </style> + </object> + <packing> + <property name="pack_type">end</property> + </packing> + </child> + </object> + </child> + <child internal-child="vbox"> + <object class="GtkBox"> + <property name="visible">True</property> + <child> + <object class="GtkBox" id="main_screen"> + <property name="visible">True</property> + <property name="margin">12</property> + <property name="spacing">12</property> + <property name="orientation">vertical</property> + <child> + <object class="GtkLabel" id="main_desc"> + <property name="visible">True</property> + <property name="wrap">True</property> + <property name="xalign">0</property> + <property name="max-width-chars">1</property> + </object> + </child> + <child> + <object class="GtkFrame"> + <property name="visible">True</property> + <child> + <object class="GtkListBox" id="main_action_list"> + <property name="visible">True</property> + <property name="selection-mode">none</property> + </object> + </child> + </object> + </child> + </object> + </child> + <child> + <object class="GtkBox" id="confirm_screen"> + <property name="visible">False</property> + <property name="margin">12</property> + <property name="spacing">12</property> + <property name="orientation">vertical</property> + <child> + <object class="GtkImage" id="confirm_image"> + <property name="visible">True</property> + </object> + </child> + <child> + <object class="GtkLabel" id="confirm_title"> + <property name="visible">True</property> + <attributes> + <attribute name="scale" value="1.1"/> + </attributes> + </object> + </child> + <child> + <object class="GtkLabel" id="confirm_desc"> + <property name="visible">True</property> + <property name="justify">center</property> + <property name="wrap">True</property> + <property name="max-width-chars">40</property> + <attributes> + <attribute name="scale" value="0.8"/> + </attributes> + <style> + <class name="dim-label"/> + </style> + </object> + </child> + </object> + </child> + <child> + <object class="GtkBox" id="verify_screen"> + <property name="visible">False</property> + <property name="margin">12</property> + <property name="spacing">12</property> + <property name="orientation">vertical</property> + <child> + <object class="GtkLabel"> + <property name="visible">True</property> + <property name="label">Compare the fingerprint, character by character, with the one shown on your contacts device.</property> + <property name="wrap">True</property> + <property name="xalign">0</property> + <property name="max-width-chars">45</property> + </object> + </child> + <child> + <object class="GtkBox"> + <property name="visible">True</property> + <property name="orientation">vertical</property> + <property name="margin-left">12</property> + <property name="margin-right">12</property> + <property name="spacing">12</property> + <property name="hexpand">False</property> + <property name="halign">center</property> + <child> + <object class="GtkLabel" id="verify_label"> + <property name="visible">True</property> + <property name="margin-top">12</property> + <property name="margin-bottom">12</property> + <property name="justify">right</property> + </object> + </child> + <child> + <object class="GtkButton" id="verify_no"> + <property name="visible">True</property> + <property name="hexpand">True</property> + <property name="label">Not Matching</property> + </object> + </child> + <child> + <object class="GtkButton" id="verify_yes"> + <property name="visible">True</property> + <property name="hexpand">True</property> + <property name="label">Matching</property> + </object> + </child> + </object> + </child> + </object> + </child> + </object> + </child> + </template> +</interface> diff --git a/plugins/omemo/src/account_settings_dialog.vala b/plugins/omemo/src/account_settings_dialog.vala deleted file mode 100644 index f0262c31..00000000 --- a/plugins/omemo/src/account_settings_dialog.vala +++ /dev/null @@ -1,54 +0,0 @@ -using Gtk; -using Qlite; -using Dino.Entities; - -namespace Dino.Plugins.Omemo { - -[GtkTemplate (ui = "/im/dino/Dino/omemo/account_settings_dialog.ui")] -public class AccountSettingsDialog : Gtk.Dialog { - - private Plugin plugin; - private Account account; - private string fingerprint; - - [GtkChild] private Label own_fingerprint; - [GtkChild] private ListBox other_list; - - public AccountSettingsDialog(Plugin plugin, Account account) { - Object(use_header_bar : 1); - this.plugin = plugin; - this.account = account; - - string own_b64 = plugin.db.identity.row_with(plugin.db.identity.account_id, account.id)[plugin.db.identity.identity_key_public_base64]; - fingerprint = fingerprint_from_base64(own_b64); - own_fingerprint.set_markup(fingerprint_markup(fingerprint)); - - int own_id = plugin.db.identity.row_with(plugin.db.identity.account_id, account.id)[plugin.db.identity.device_id]; - - int i = 0; - foreach (Row row in plugin.db.identity_meta.with_address(account.bare_jid.to_string())) { - if (row[plugin.db.identity_meta.device_id] == own_id) continue; - if (i == 0) { - other_list.foreach((widget) => { widget.destroy(); }); - } - string? other_b64 = row[plugin.db.identity_meta.identity_key_public_base64]; - Label lbl = new Label(other_b64 != null ? fingerprint_markup(fingerprint_from_base64(other_b64)) : _("Unknown device (0x%.8x)").printf(row[plugin.db.identity_meta.device_id])) { use_markup = true, visible = true, margin = 8, selectable=true }; - if (row[plugin.db.identity_meta.now_active] && other_b64 != null) { - other_list.insert(lbl, 0); - } else { - lbl.sensitive = false; - other_list.insert(lbl, i); - } - i++; - } - } - - [GtkCallback] - public void copy_button_clicked() { - Clipboard.get_default(get_display()).set_text(fingerprint, fingerprint.length); - } - - -} - -} diff --git a/plugins/omemo/src/account_settings_widget.vala b/plugins/omemo/src/account_settings_widget.vala index 6db193fc..6148da56 100644 --- a/plugins/omemo/src/account_settings_widget.vala +++ b/plugins/omemo/src/account_settings_widget.vala @@ -27,7 +27,7 @@ public class AccountSettingWidget : Plugins.AccountSettingsWidget, Box { btn.valign = Align.CENTER; btn.clicked.connect(() => { activated(); - AccountSettingsDialog dialog = new AccountSettingsDialog(plugin, account); + ContactDetailsDialog dialog = new ContactDetailsDialog(plugin, account, account.bare_jid); dialog.set_transient_for((Window) get_toplevel()); dialog.present(); }); diff --git a/plugins/omemo/src/contact_details_dialog.vala b/plugins/omemo/src/contact_details_dialog.vala new file mode 100644 index 00000000..326a1b79 --- /dev/null +++ b/plugins/omemo/src/contact_details_dialog.vala @@ -0,0 +1,235 @@ +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; + private bool own = false; + private int own_id = 0; + + [GtkChild] private Box own_fingerprint_container; + [GtkChild] private Label own_fingerprint; + [GtkChild] private Box new_keys_container; + [GtkChild] private ListBox new_keys; + [GtkChild] private Box keys_container; + [GtkChild] private ListBox keys; + [GtkChild] private Switch auto_accept; + [GtkChild] private Button copy; + + private void set_device_trust(Row device, bool trust) { + Database.IdentityMetaTable.TrustLevel trust_level = trust ? Database.IdentityMetaTable.TrustLevel.TRUSTED : Database.IdentityMetaTable.TrustLevel.UNTRUSTED; + 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.trust_level, trust_level).perform(); + } + + private void add_fingerprint(Row device, Database.IdentityMetaTable.TrustLevel trust) { + keys_container.visible = true; + + ListBoxRow lbr = new ListBoxRow() { visible = true, activatable = true, hexpand = true }; + Box box = new Box(Gtk.Orientation.HORIZONTAL, 40) { visible = true, margin_start = 20, margin_end = 20, margin_top = 14, margin_bottom = 14, hexpand = true }; + + Box status = new Box(Gtk.Orientation.HORIZONTAL, 5) { visible = true, hexpand = true }; + Label status_lbl = new Label(null) { visible = true, hexpand = true, xalign = 0 }; + + Image img = new Image() { visible = true, halign = Align.END, icon_size = IconSize.BUTTON }; + + 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, halign = Align.START, valign = Align.CENTER, hexpand = false }; + + switch(trust) { + case Database.IdentityMetaTable.TrustLevel.TRUSTED: + img.icon_name = "emblem-ok-symbolic"; + status_lbl.set_markup("<span size='large' color='#1A63D9'>Accepted</span>"); + break; + case Database.IdentityMetaTable.TrustLevel.UNTRUSTED: + img.icon_name = "action-unavailable-symbolic"; + status_lbl.set_markup("<span size='large' color='#D91900'>Rejected</span>"); + lbl.get_style_context().add_class("dim-label"); + break; + case Database.IdentityMetaTable.TrustLevel.VERIFIED: + img.icon_name = "security-high-symbolic"; + status_lbl.set_markup("<span size='large' color='#1A63D9'>Verified</span>"); + break; + } + + if (!device[plugin.db.identity_meta.now_active]) { + img.icon_name= "appointment-missed-symbolic"; + status_lbl.set_markup("<span size='large' color='#8b8e8f'>Unused</span>"); + } + + box.add(lbl); + box.add(status); + + status.add(status_lbl); + status.add(img); + + lbr.add(box); + keys.add(lbr); + + keys.row_activated.connect((row) => { + if(row == lbr) { + Row updated_device = plugin.db.identity_meta.with_address(device[plugin.db.identity_meta.identity_id], device[plugin.db.identity_meta.address_name]).with(plugin.db.identity_meta.device_id, "=", device[plugin.db.identity_meta.device_id]).single().row().inner; + ManageKeyDialog manage_dialog = new ManageKeyDialog(updated_device, plugin.db); + manage_dialog.set_transient_for((Window) get_toplevel()); + manage_dialog.present(); + manage_dialog.response.connect((response) => update_row(response, img, lbl, status_lbl, device)); + } + }); + } + + private void update_row(int response, Image img, Label lbl, Label status_lbl, Row device){ + switch (response) { + case Database.IdentityMetaTable.TrustLevel.TRUSTED: + img.icon_name = "emblem-ok-symbolic"; + status_lbl.set_markup("<span size='large' color='#1A63D9'>Accepted</span>"); + lbl.get_style_context().remove_class("dim-label"); + set_device_trust(device, true); + break; + case Database.IdentityMetaTable.TrustLevel.UNTRUSTED: + img.icon_name = "action-unavailable-symbolic"; + status_lbl.set_markup("<span size='large' color='#D91900'>Rejected</span>"); + lbl.get_style_context().add_class("dim-label"); + set_device_trust(device, false); + break; + case Database.IdentityMetaTable.TrustLevel.VERIFIED: + img.icon_name = "security-high-symbolic"; + status_lbl.set_markup("<span size='large' color='#1A63D9'>Verified</span>"); + lbl.get_style_context().remove_class("dim-label"); + 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.trust_level, Database.IdentityMetaTable.TrustLevel.VERIFIED).perform(); + plugin.db.trust.update().with(plugin.db.trust.identity_id, "=", account.id).with(plugin.db.trust.address_name, "=", jid.bare_jid.to_string()).set(plugin.db.trust.blind_trust, false).perform(); + auto_accept.set_active(false); + break; + } + } + + private void add_new_fingerprint(Row device){ + new_keys_container.visible = true; + + ListBoxRow lbr = new ListBoxRow() { visible = true, activatable = false, hexpand = true }; + Box box = new Box(Gtk.Orientation.HORIZONTAL, 40) { visible = true, margin_start = 20, margin_end = 20, margin_top = 14, margin_bottom = 14, hexpand = true }; + + Box control = new Box(Gtk.Orientation.HORIZONTAL, 0) { visible = true, hexpand = true }; + + Button yes = new Button() { visible = true, valign = Align.CENTER, hexpand = true }; + yes.image = new Image.from_icon_name("emblem-ok-symbolic", IconSize.BUTTON); + yes.get_style_context().add_class("suggested-action"); + + Button no = new Button() { visible = true, valign = Align.CENTER, hexpand = true }; + no.image = new Image.from_icon_name("action-unavailable-symbolic", IconSize.BUTTON); + no.get_style_context().add_class("destructive-action"); + + yes.clicked.connect(() => { + set_device_trust(device, true); + add_fingerprint(device, Database.IdentityMetaTable.TrustLevel.TRUSTED); + new_keys.remove(lbr); + if (new_keys.get_children().length() < 1) new_keys_container.visible = false; + }); + + no.clicked.connect(() => { + set_device_trust(device, false); + add_fingerprint(device, Database.IdentityMetaTable.TrustLevel.UNTRUSTED); + new_keys.remove(lbr); + if (new_keys.get_children().length() < 1) new_keys_container.visible = false; + }); + + 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, halign = Align.START, valign = Align.CENTER, hexpand = false }; + + + box.add(lbl); + + control.add(yes); + control.add(no); + control.get_style_context().add_class("linked"); + + box.add(control); + + lbr.add(box); + new_keys.add(lbr); + } + + public ContactDetailsDialog(Plugin plugin, Account account, Jid jid) { + Object(use_header_bar : 1); + this.plugin = plugin; + this.account = account; + this.jid = jid; + + (get_header_bar() as HeaderBar).set_subtitle(jid.bare_jid.to_string()); + + + if(jid.equals(account.bare_jid)) { + own = true; + own_id = plugin.db.identity.row_with(plugin.db.identity.account_id, account.id)[plugin.db.identity.device_id]; + + own_fingerprint_container.visible = true; + + string own_b64 = plugin.db.identity.row_with(plugin.db.identity.account_id, account.id)[plugin.db.identity.identity_key_public_base64]; + string fingerprint = fingerprint_from_base64(own_b64); + own_fingerprint.set_markup(fingerprint_markup(fingerprint)); + + copy.clicked.connect(() => {Clipboard.get_default(get_display()).set_text(fingerprint, fingerprint.length);}); + } + + new_keys.set_header_func((row, before_row) => { + if (row.get_header() == null && before_row != null) { + row.set_header(new Separator(Orientation.HORIZONTAL)); + } + }); + + keys.set_header_func((row, before_row) => { + if (row.get_header() == null && before_row != null) { + row.set_header(new Separator(Orientation.HORIZONTAL)); + } + }); + + foreach (Row device in plugin.db.identity_meta.with_address(account.id, jid.to_string()).with(plugin.db.identity_meta.trust_level, "=", Database.IdentityMetaTable.TrustLevel.UNKNOWN).without_null(plugin.db.identity_meta.identity_key_public_base64)) { + add_new_fingerprint(device); + } + + foreach (Row device in plugin.db.identity_meta.with_address(account.id, jid.to_string()).with(plugin.db.identity_meta.trust_level, "!=", Database.IdentityMetaTable.TrustLevel.UNKNOWN).without_null(plugin.db.identity_meta.identity_key_public_base64)) { + if(own && device[plugin.db.identity_meta.device_id] == own_id) { + continue; + } + add_fingerprint(device, (Database.IdentityMetaTable.TrustLevel) device[plugin.db.identity_meta.trust_level]); + + } + + auto_accept.set_active(plugin.db.trust.get_blind_trust(account.id, jid.bare_jid.to_string())); + + auto_accept.state_set.connect((active) => { + plugin.db.trust.update().with(plugin.db.trust.identity_id, "=", account.id).with(plugin.db.trust.address_name, "=", jid.bare_jid.to_string()).set(plugin.db.trust.blind_trust, active).perform(); + + if (active) { + new_keys_container.visible = false; + + foreach (Row device in plugin.db.identity_meta.with_address(account.id, jid.to_string()).with(plugin.db.identity_meta.trust_level, "=", Database.IdentityMetaTable.TrustLevel.UNKNOWN).without_null(plugin.db.identity_meta.identity_key_public_base64)) { + set_device_trust(device, true); + add_fingerprint(device, Database.IdentityMetaTable.TrustLevel.TRUSTED); + } + } + + return false; + }); + + } + +} + +} diff --git a/plugins/omemo/src/contact_details_provider.vala b/plugins/omemo/src/contact_details_provider.vala index 05b85d9f..1cf635c2 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,31 @@ 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())) { + foreach (Row row in plugin.db.identity_meta.with_address(conversation.account.id, 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.response.connect((response_type) => { + plugin.device_notification_populator.should_hide(); + }); + 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..05864e38 100644 --- a/plugins/omemo/src/database.vala +++ b/plugins/omemo/src/database.vala @@ -6,31 +6,47 @@ using Dino.Entities; namespace Dino.Plugins.Omemo { public class Database : Qlite.Database { - private const int VERSION = 1; + private const int VERSION = 2; public class IdentityMetaTable : Table { + public enum TrustLevel { + VERIFIED, + TRUSTED, + UNTRUSTED, + UNKNOWN; + + public string to_string() { + int val = this; + return val.to_string(); + } + } + + //Default to provide backwards compatability + public Column<int> identity_id = new Column.Integer("identity_id") { not_null = true, min_version = 2, default = "-1" }; 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") { default = "0", max_version = 1 }; + public Column<int> trust_level = new Column.Integer("trust_level") { default = TrustLevel.UNKNOWN.to_string(), min_version = 2 }; 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, trust_level, 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 QueryBuilder with_address(int identity_id, string address_name) { + return select().with(this.identity_id, "=", identity_id).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 +55,33 @@ 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, TrustLevel trust) { if (bundle == null || bundle.identity_key == null) return -1; return 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.trust_level, trust).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 +139,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,18 +148,22 @@ 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) { } } public override void migrate(long oldVersion) { - // new table columns are added, outdated columns are still present + exec("DROP INDEX identity_meta_idx"); + exec("DROP INDEX identity_meta_list_idx"); + exec("CREATE UNIQUE INDEX identity_meta_idx ON identity_meta (identity_id, address_name, device_id)"); + exec("CREATE INDEX identity_meta_list_idx ON identity_meta (identity_id, address_name)"); } } diff --git a/plugins/omemo/src/device_notification_populator.vala b/plugins/omemo/src/device_notification_populator.vala new file mode 100644 index 00000000..6940c723 --- /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 : NotificationPopulator, Object { + + public string id { get { return "device_notification"; } } + + private StreamInteractor? stream_interactor; + private Plugin plugin; + private Conversation? current_conversation; + private NotificationCollection? notification_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(current_conversation.account.id, jid.bare_jid.to_string()).with(plugin.db.identity_meta.trust_level, "=", Database.IdentityMetaTable.TrustLevel.UNKNOWN).without_null(plugin.db.identity_meta.identity_key_public_base64).count() > 0; + } + + public void init(Conversation conversation, NotificationCollection notification_collection, Plugins.WidgetType type) { + current_conversation = conversation; + this.notification_collection = notification_collection; + stream_interactor.module_manager.get_module(conversation.account, StreamModule.IDENTITY).bundle_fetched.connect_after((jid, device_id, bundle) => { + if (jid.equals(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) { + notification = null; + } + + private void display_notification() { + if(notification == null) { + notification = new ConversationNotification(plugin, current_conversation.account, current_conversation.counterpart); + notification.should_hide.connect(should_hide); + notification_collection.add_meta_notification(notification); + } + } + + public void should_hide() { + if (!has_new_devices(current_conversation.counterpart) && notification != null){ + notification_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/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/manage_key_dialog.vala b/plugins/omemo/src/manage_key_dialog.vala new file mode 100644 index 00000000..bb41ea0d --- /dev/null +++ b/plugins/omemo/src/manage_key_dialog.vala @@ -0,0 +1,193 @@ +using Gtk; +using Qlite; + +namespace Dino.Plugins.Omemo { + +[GtkTemplate (ui = "/im/dino/Dino/omemo/manage_key_dialog.ui")] +public class ManageKeyDialog : Gtk.Dialog { + + [GtkChild] private Button cancel_button; + [GtkChild] private Button ok_button; + + [GtkChild] private Box main_screen; + [GtkChild] private Label main_desc; + [GtkChild] private ListBox main_action_list; + + [GtkChild] private Box confirm_screen; + [GtkChild] private Image confirm_image; + [GtkChild] private Label confirm_title; + [GtkChild] private Label confirm_desc; + + [GtkChild] private Box verify_screen; + [GtkChild] private Label verify_label; + [GtkChild] private Button verify_yes; + [GtkChild] private Button verify_no; + + private Row device; + private Database db; + + private bool return_to_main; + private int current_response; + + private void handle_cancel() { + if (main_screen.visible) close(); + + if (verify_screen.visible) { + verify_screen.visible = false; + main_screen.visible = true; + cancel_button.label = "Cancel"; + } + + if (confirm_screen.visible) { + if (return_to_main) { + confirm_screen.visible = false; + main_screen.visible = true; + cancel_button.label = "Cancel"; + } else { + confirm_screen.visible = false; + verify_screen.visible = true; + } + } + + ok_button.sensitive = false; + } + + public ManageKeyDialog(Row device, Database db) { + Object(use_header_bar : 1); + + this.device = device; + this.db = db; + + setup_main_screen(); + setup_verify_screen(); + + cancel_button.clicked.connect(handle_cancel); + ok_button.clicked.connect(() => { + response(current_response); + close(); + }); + + verify_yes.clicked.connect(() => { + confirm_image.set_from_icon_name("security-high-symbolic", IconSize.DIALOG); + confirm_title.label = "Complete key verfication"; + confirm_desc.set_markup(@"Once confirmed, any future messages sent by <b>$(device[db.identity_meta.address_name])</b> using this key will be highlighted accordingly in the chat window."); + return_to_main = false; + verify_screen.visible = false; + confirm_screen.visible = true; + ok_button.sensitive = true; + current_response = Database.IdentityMetaTable.TrustLevel.VERIFIED; + }); + + verify_no.clicked.connect(() => { + confirm_image.set_from_icon_name("action-unavailable-symbolic", IconSize.DIALOG); + confirm_title.label = "Complete key rejection"; + confirm_desc.set_markup(@"Once confirmed, any future messages sent by <b>$(device[db.identity_meta.address_name])</b> using this key will be ignored and none of your messages will be readable using this key."); + return_to_main = false; + verify_screen.visible = false; + confirm_screen.visible = true; + ok_button.sensitive = true; + current_response = Database.IdentityMetaTable.TrustLevel.UNTRUSTED; + }); + } + + private Box make_action_box(string title, string desc){ + Box box = new Box(Orientation.VERTICAL, 0) { visible = true, margin_start = 20, margin_end = 20, margin_top = 14, margin_bottom = 14 }; + Label lbl_title = new Label(title) { visible = true, halign = Align.START }; + Label lbl_desc = new Label(desc) { visible = true, xalign = 0, wrap = true, max_width_chars = 40 }; + + Pango.AttrList title_attrs = new Pango.AttrList(); + title_attrs.insert(Pango.attr_scale_new(1.1)); + lbl_title.attributes = title_attrs; + Pango.AttrList desc_attrs = new Pango.AttrList(); + desc_attrs.insert(Pango.attr_scale_new(0.8)); + lbl_desc.attributes = desc_attrs; + lbl_desc.get_style_context().add_class("dim-label"); + + box.add(lbl_title); + box.add(lbl_desc); + + return box; + } + + private Box make_trust_screen(string icon_name, string title, string desc) { + Box box = new Box(Orientation.VERTICAL, 12) { margin = 12, spacing = 12 }; + Image icon = new Image.from_icon_name(icon_name, IconSize.DIALOG) { visible = true }; + box.add(icon); + Label lbl_title = new Label(title) { visible = true }; + Label lbl_desc = new Label(desc) { visible = true, use_markup = true, max_width_chars = 1, wrap = true, justify = Justification.CENTER }; + + Pango.AttrList title_attrs = new Pango.AttrList(); + title_attrs.insert(Pango.attr_scale_new(1.1)); + lbl_title.attributes = title_attrs; + Pango.AttrList desc_attrs = new Pango.AttrList(); + desc_attrs.insert(Pango.attr_scale_new(0.8)); + lbl_desc.attributes = desc_attrs; + lbl_desc.get_style_context().add_class("dim-label"); + + box.add(lbl_title); + box.add(lbl_desc); + + return box; + } + + private void setup_main_screen() { + main_action_list.set_header_func((row, before_row) => { + if (row.get_header() == null && before_row != null) { + row.set_header(new Separator(Orientation.HORIZONTAL)); + } + }); + + ListBoxRow verify = new ListBoxRow() { visible = true }; + verify.add(make_action_box("Verify Key Fingerprint", "Compare this key's fingerprint with the fingerprint displayed on the contact's device.")); + ListBoxRow reject = new ListBoxRow() { visible = true }; + reject.add(make_action_box("Reject Key", "Stop accepting this key during communication with its associated contact.")); + ListBoxRow accept = new ListBoxRow() {visible = true }; + accept.add(make_action_box("Accept Key", "Start accepting this key during communication with its assoicated contact")); + + switch((Database.IdentityMetaTable.TrustLevel) device[db.identity_meta.trust_level]) { + case Database.IdentityMetaTable.TrustLevel.TRUSTED: + main_desc.set_markup(@"This key is currently <span color='#1A63D9'>accepted</span>. This means it can be used by <b>$(device[db.identity_meta.address_name])</b> to receive and send messages."); + main_action_list.add(verify); + main_action_list.add(reject); + break; + case Database.IdentityMetaTable.TrustLevel.VERIFIED: + main_desc.set_markup(@"This key is currently <span color='#1A63D9'>verified</span>. This means it can be used by <b>$(device[db.identity_meta.address_name])</b> to receive and send messages. Additionaly it has been verified out-of-band to match the key on the contact's device."); + main_action_list.add(reject); + break; + case Database.IdentityMetaTable.TrustLevel.UNTRUSTED: + main_desc.set_markup(@"This key is currently <span color='#D91900'>rejected</span>. This means it cannot be used by <b>$(device[db.identity_meta.address_name])</b> to receive messages, and any messages sent by it will be ignored"); + main_action_list.add(accept); + break; + } + + main_action_list.row_activated.connect((row) => { + if(row == verify) { + verify_screen.visible = true; + } else if (row == reject) { + confirm_image.set_from_icon_name("action-unavailable-symbolic", IconSize.DIALOG); + confirm_title.label = "Complete key rejection"; + confirm_desc.set_markup(@"Once confirmed, any future messages sent by <b>$(device[db.identity_meta.address_name])</b> using this key will be ignored and none of your messages will be readable using this key."); + return_to_main = true; + confirm_screen.visible = true; + ok_button.sensitive = true; + current_response = Database.IdentityMetaTable.TrustLevel.UNTRUSTED; + } else if (row == accept) { + confirm_image.set_from_icon_name("emblem-ok-symbolic", IconSize.DIALOG); + confirm_title.label = "Complete key acception"; + confirm_desc.set_markup(@"Once confirmed this key will be usable by <b>$(device[db.identity_meta.address_name])</b> to receive and send messages."); + return_to_main = true; + confirm_screen.visible = true; + ok_button.sensitive = true; + current_response = Database.IdentityMetaTable.TrustLevel.TRUSTED; + } + cancel_button.label = "Back"; + main_screen.visible = false; + }); + } + + private void setup_verify_screen() { + verify_label.set_markup(fingerprint_markup(fingerprint_from_base64(device[db.identity_meta.identity_key_public_base64]))); + } +} + +} diff --git a/plugins/omemo/src/manager.vala b/plugins/omemo/src/manager.vala index eb0c6378..8654a4f5 100644 --- a/plugins/omemo/src/manager.vala +++ b/plugins/omemo/src/manager.vala @@ -12,6 +12,7 @@ public class Manager : StreamInteractionModule, Object { private StreamInteractor stream_interactor; private Database db; + private TrustManager trust_manager; private Map<Entities.Message, MessageState> message_states = new HashMap<Entities.Message, MessageState>(Entities.Message.hash_func, Entities.Message.equals_func); private ReceivedMessageListener received_message_listener = new ReceivedMessageListener(); @@ -21,7 +22,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 +38,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 +53,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))"; } } @@ -64,6 +65,8 @@ public class Manager : StreamInteractionModule, Object { this.stream_interactor = stream_interactor; this.db = db; + this.trust_manager = new TrustManager(stream_interactor, db); + stream_interactor.stream_negotiated.connect(on_stream_negotiated); stream_interactor.account_added.connect(on_account_added); stream_interactor.get_module(MessageProcessor.IDENTITY).received_pipeline.connect(received_message_listener); @@ -85,6 +88,23 @@ public class Manager : StreamInteractionModule, Object { } } + private Gee.List<Jid> get_occupants(Jid jid, Account account){ + Gee.List<Jid> occupants = new ArrayList<Jid>(Jid.equals_bare_func); + if(!stream_interactor.get_module(MucManager.IDENTITY).is_groupchat(jid, account)){ + occupants.add(jid); + } + Gee.List<Jid>? occupant_jids = stream_interactor.get_module(MucManager.IDENTITY).get_offline_members(jid, account); + if(occupant_jids == null) { + return occupants; + } + foreach (Jid occupant in occupant_jids) { + if(!occupant.equals(account.bare_jid)){ + occupants.add(occupant.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); @@ -98,7 +118,20 @@ public class Manager : StreamInteractionModule, Object { return; } StreamModule module = (!)module_; - EncryptState enc_state = module.encrypt(message_stanza, conversation.account.bare_jid); + + Gee.List<Jid> 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>(Jid.equals_bare_func); + recipients.add(message_stanza.to); + } + + EncryptState enc_state = trust_manager.encrypt(message_stanza, conversation.account.bare_jid, recipients, stream, conversation.account); MessageState state; lock (message_states) { if (message_states.has_key(message)) { @@ -116,17 +149,22 @@ 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"); if (state.waiting_own_sessions > 0) { - module.start_sessions_with((!)stream, conversation.account.bare_jid); + module.fetch_bundles((!)stream, conversation.account.bare_jid, trust_manager.get_trusted_devices(conversation.account, conversation.account.bare_jid)); } if (state.waiting_other_sessions > 0 && message.counterpart != null) { - module.start_sessions_with((!)stream, ((!)message.counterpart).bare_jid); + foreach(Jid jid in get_occupants(((!)message.counterpart).bare_jid, conversation.account)) { + module.fetch_bundles((!)stream, jid, trust_manager.get_trusted_devices(conversation.account, jid)); + } } - if (state.waiting_other_devicelist && message.counterpart != null) { - module.request_user_devicelist((!)stream, ((!)message.counterpart).bare_jid); + if (state.waiting_other_devicelists > 0 && message.counterpart != null) { + foreach(Jid jid in get_occupants(((!)message.counterpart).bare_jid, conversation.account)) { + module.request_user_devicelist((!)stream, jid); + } } } } @@ -135,27 +173,51 @@ public class Manager : StreamInteractionModule, Object { private void on_account_added(Account account) { stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).store_created.connect((store) => on_store_created(account, store)); - stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).device_list_loaded.connect((jid) => on_device_list_loaded(account, jid)); + stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).device_list_loaded.connect((jid, devices) => on_device_list_loaded(account, jid, devices)); stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).bundle_fetched.connect((jid, device_id, bundle) => on_bundle_fetched(account, jid, device_id, bundle)); - stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).session_started.connect((jid, device_id) => on_session_started(account, jid, false)); - stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).session_start_failed.connect((jid, device_id) => on_session_started(account, jid, true)); } private void on_stream_negotiated(Account account, XmppStream stream) { stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).request_user_devicelist(stream, account.bare_jid); } - private void on_session_started(Account account, Jid jid, bool failed) { - if (Plugin.DEBUG) print(@"OMEMO: session start between $(account.bare_jid) and $jid $(failed ? "failed" : "successful")\n"); + private void on_device_list_loaded(Account account, Jid jid, ArrayList<int32> device_list) { + if (Plugin.DEBUG) print(@"OMEMO: received device list for $(account.bare_jid) from $jid\n"); + + // Update meta database + XmppStream? stream = stream_interactor.get_stream(account); + if (stream == null) { + return; + } + StreamModule? module = ((!)stream).get_module(StreamModule.IDENTITY); + if (module == null) { + return; + } + + 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(account.id, 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]); + inc++; + } + 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(); + } + HashSet<Entities.Message> send_now = new HashSet<Entities.Message>(); lock (message_states) { foreach (Entities.Message msg in message_states.keys) { if (!msg.account.equals(account)) continue; + Gee.List<Jid> occupants = get_occupants(msg.counterpart.bare_jid, account); MessageState state = message_states[msg]; if (account.bare_jid.equals(jid)) { - state.waiting_own_sessions--; - } else if (msg.counterpart != null && msg.counterpart.equals_bare(jid)) { - state.waiting_other_sessions--; + state.waiting_own_devicelist = false; + } else if (msg.counterpart != null && occupants.contains(jid)) { + state.waiting_other_devicelists--; } if (state.should_retry_now()) { send_now.add(msg); @@ -165,25 +227,57 @@ public class Manager : StreamInteractionModule, Object { } 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); + 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_device_list_loaded(Account account, Jid jid) { - if (Plugin.DEBUG) print(@"OMEMO: received device list for $(account.bare_jid) from $jid\n"); + public void on_bundle_fetched(Account account, Jid jid, int32 device_id, Bundle 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(account.id, jid.bare_jid.to_string()) + .with(db.identity_meta.device_id, "=", device_id) + .with(db.identity_meta.identity_key_public_base64, "=", Base64.encode(bundle.identity_key.serialize())) + .single().row().is_present()); + + Database.IdentityMetaTable.TrustLevel trusted = (Database.IdentityMetaTable.TrustLevel) db.identity_meta.with_address(account.id, jid.bare_jid.to_string()).with(db.identity_meta.device_id, "=", device_id).single()[db.identity_meta.trust_level, Database.IdentityMetaTable.TrustLevel.UNKNOWN]; + + if(untrust) { + trusted = Database.IdentityMetaTable.TrustLevel.UNKNOWN; + } else if (blind_trust && trusted == Database.IdentityMetaTable.TrustLevel.UNKNOWN) { + trusted = Database.IdentityMetaTable.TrustLevel.TRUSTED; + } + + db.identity_meta.insert_device_bundle(account.id, jid.bare_jid.to_string(), device_id, bundle, trusted); + + 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>(); lock (message_states) { foreach (Entities.Message msg in message_states.keys) { + + bool session_created = true; if (!msg.account.equals(account)) continue; + Gee.List<Jid> 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; + + if (trusted == Database.IdentityMetaTable.TrustLevel.TRUSTED || trusted == Database.IdentityMetaTable.TrustLevel.VERIFIED) { + 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 (state.should_retry_now()) { + if (account.bare_jid.equals(jid) && session_created) { + state.waiting_own_sessions--; + } else if (msg.counterpart != null && (msg.counterpart.equals_bare(jid) || occupants.contains(jid)) && session_created) { + state.waiting_other_sessions--; + } + if (state.should_retry_now()){ send_now.add(msg); state.active_send_attempt = true; } @@ -191,34 +285,10 @@ public class Manager : StreamInteractionModule, Object { } 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); + 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); } - - // Update meta database - XmppStream? stream = stream_interactor.get_stream(account); - if (stream == null) { - return; - } - StreamModule? module = ((!)stream).get_module(StreamModule.IDENTITY); - if (module == null) { - return; - } - ArrayList<int32> device_list = module.get_device_list(jid); - db.identity_meta.insert_device_list(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]); - inc++; - } - if (inc > 0) { - if (Plugin.DEBUG) print(@"OMEMO: new bundles $inc/$(device_list.size) for $jid\n"); - } - } - - 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); } private void on_store_created(Account account, Store store) { @@ -265,7 +335,25 @@ 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); + if (stream_interactor.get_module(MucManager.IDENTITY).is_groupchat(conversation.counterpart, conversation.account)){ + Xep.Muc.Flag? flag = stream.get_flag(Xep.Muc.Flag.IDENTITY); + if (flag == null) return false; + if (flag.has_room_feature(conversation.counterpart, Xep.Muc.Feature.NON_ANONYMOUS) && flag.has_room_feature(conversation.counterpart, Xep.Muc.Feature.MEMBERS_ONLY)) { + foreach(Jid jid in stream_interactor.get_module(MucManager.IDENTITY).get_offline_members(conversation.counterpart, conversation.account)) { + if (!trust_manager.is_known_address(conversation.account, jid.bare_jid)) { + module.request_user_devicelist(stream, jid.bare_jid); + return false; + } + } + return true; + } else { + return false; + } + } else if (!trust_manager.is_known_address(conversation.account, conversation.counterpart.bare_jid)) { + module.request_user_devicelist(stream, conversation.counterpart.bare_jid); + return false; + } + return true; } public static void start(StreamInteractor stream_interactor, Database db) { diff --git a/plugins/omemo/src/own_notifications.vala b/plugins/omemo/src/own_notifications.vala new file mode 100644 index 00000000..df0c4740 --- /dev/null +++ b/plugins/omemo/src/own_notifications.vala @@ -0,0 +1,38 @@ +using Dino.Entities; +using Xmpp; +using Gtk; + +namespace Dino.Plugins.Omemo { + +public class OwnNotifications { + + private StreamInteractor stream_interactor; + private Plugin plugin; + private Account account; + + public OwnNotifications (Plugin plugin, StreamInteractor stream_interactor, Account account) { + this.stream_interactor = (!)stream_interactor; + this.plugin = plugin; + this.account = account; + stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).bundle_fetched.connect_after((jid, device_id, bundle) => { + if (jid.equals(account.bare_jid) && has_new_devices(account.bare_jid)) { + display_notification(); + } + }); + if (has_new_devices(account.bare_jid)) { + display_notification(); + } + + } + + public bool has_new_devices(Jid jid) { + return plugin.db.identity_meta.with_address(account.id, jid.bare_jid.to_string()).with(plugin.db.identity_meta.trust_level, "=", Database.IdentityMetaTable.TrustLevel.UNKNOWN).without_null(plugin.db.identity_meta.identity_key_public_base64).count() > 0; + } + + private void display_notification() { + Notification notification = new Notification("Trust decision required"); + notification.set_body(@"A new OMEMO device has been added for the account $(account.bare_jid)"); + plugin.app.send_notification(account.id.to_string()+"-new-device", notification); + } +} +} diff --git a/plugins/omemo/src/plugin.vala b/plugins/omemo/src/plugin.vala index b9ce500d..b63ed3f2 100644 --- a/plugins/omemo/src/plugin.vala +++ b/plugins/omemo/src/plugin.vala @@ -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,11 +37,14 @@ 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_notification_populator(device_notification_populator); this.app.stream_interactor.module_manager.initialize_account_modules.connect((account, list) => { list.add(new StreamModule()); + new OwnNotifications(this, this.app.stream_interactor, account); }); Manager.start(this.app.stream_interactor, db); diff --git a/plugins/omemo/src/stream_module.vala b/plugins/omemo/src/stream_module.vala index 6e6772ca..f086bd4c 100644 --- a/plugins/omemo/src/stream_module.vala +++ b/plugins/omemo/src/stream_module.vala @@ -16,102 +16,15 @@ private const int NUM_KEYS_TO_PUBLISH = 100; public class StreamModule : XmppStreamModule { public static Xmpp.ModuleIdentity<StreamModule> IDENTITY = new Xmpp.ModuleIdentity<StreamModule>(NS_URI, "omemo_module"); - private Store store; + public Store store { public get; private set; } private ConcurrentSet<string> active_bundle_requests = new ConcurrentSet<string>(); private ConcurrentSet<Jid> active_devicelist_requests = new ConcurrentSet<Jid>(); - private Map<Jid, ArrayList<int32>> device_lists = new HashMap<Jid, ArrayList<int32>>(Jid.hash_bare_func, Jid.equals_bare_func); private Map<Jid, ArrayList<int32>> ignored_devices = new HashMap<Jid, ArrayList<int32>>(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 device_list_loaded(Jid jid, ArrayList<int32> devices); public signal void bundle_fetched(Jid jid, int device_id, Bundle bundle); - public signal void session_started(Jid jid, int device_id); - public signal void session_start_failed(Jid jid, int device_id); - - public EncryptState encrypt(MessageStanza message, Jid self_jid) { - EncryptState status = new EncryptState(); - if (!Plugin.ensure_context()) return status; - if (message.to == null) return status; - 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; - if (status.own_devices == 0 || status.other_devices == 0) return status; - - uint8[] key = new uint8[16]; - Plugin.get_context().randomize(key); - uint8[] iv = new uint8[16]; - Plugin.get_context().randomize(iv); - - uint8[] ciphertext = aes_encrypt(Cipher.AES_GCM_NOPADDING, key, iv, message.body.data); - - StanzaNode header; - StanzaNode encrypted = new StanzaNode.build("encrypted", NS_URI).add_self_xmlns() - .put_node(header = new StanzaNode.build("header", NS_URI) - .put_attribute("sid", store.local_registration_id.to_string()) - .put_node(new StanzaNode.build("iv", NS_URI) - .put_node(new StanzaNode.text(Base64.encode(iv))))) - .put_node(new StanzaNode.build("payload", NS_URI) - .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++; - } - } - address.name = self_jid.bare_jid.to_string(); - foreach(int32 device_id in device_lists[self_jid]) { - if (is_ignored_device(self_jid, device_id)) { - status.own_lost++; - continue; - } - if (device_id != store.local_registration_id) { - address.device_id = (int) device_id; - try { - StanzaNode key_node = create_encrypted_key(key, address); - header.put_node(key_node); - status.own_success++; - } catch (Error e) { - if (e.code == ErrorCode.UNKNOWN) status.own_unknown++; - else status.own_failure++; - } - } - } - - message.stanza.put_node(encrypted); - Xep.ExplicitEncryption.add_encryption_tag_to_message(message, NS_URI, "OMEMO"); - message.body = "[This message is OMEMO encrypted]"; - status.encrypted = true; - } catch (Error e) { - if (Plugin.DEBUG) print(@"OMEMO: Signal error while encrypting message: $(e.message)\n"); - } - return status; - } - - 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); - StanzaNode key_node = new StanzaNode.build("key", NS_URI) - .put_attribute("rid", address.device_id.to_string()) - .put_node(new StanzaNode.text(Base64.encode(device_key.serialized))); - if (device_key.type == CiphertextType.PREKEY) key_node.put_attribute("prekey", "true"); - return key_node; - } public override void attach(XmppStream stream) { if (!Plugin.ensure_context()) return; @@ -150,31 +63,26 @@ public class StreamModule : XmppStreamModule { if (Plugin.DEBUG) print(@"OMEMO: Not on device list, adding id\n"); node.put_node(new StanzaNode.build("device", NS_URI).put_attribute("id", store.local_registration_id.to_string())); stream.get_module(Pubsub.Module.IDENTITY).publish(stream, jid, NODE_DEVICELIST, NODE_DEVICELIST, id, node); - } else { - publish_bundles_if_needed(stream, jid); } + publish_bundles_if_needed(stream, jid); } - lock(device_lists) { - device_lists[jid] = new ArrayList<int32>(); - foreach (StanzaNode device_node in node.get_subnodes("device")) { - device_lists[jid].add(device_node.get_attribute_int("id")); - } + + ArrayList<int32> device_list = new ArrayList<int32>(); + foreach (StanzaNode device_node in node.get_subnodes("device")) { + device_list.add(device_node.get_attribute_int("id")); } active_devicelist_requests.remove(jid); - device_list_loaded(jid); + device_list_loaded(jid, device_list); } - public void start_sessions_with(XmppStream stream, Jid jid) { - if (!device_lists.has_key(jid)) { - return; - } + public void fetch_bundles(XmppStream stream, Jid jid, Gee.List<int32> devices) { Address address = new Address(jid.bare_jid.to_string(), 0); - foreach(int32 device_id in device_lists[jid]) { + foreach(int32 device_id in devices) { if (!is_ignored_device(jid, device_id)) { address.device_id = device_id; try { if (!store.contains_session(address)) { - start_session_with(stream, jid, device_id); + fetch_bundle(stream, jid, device_id); } } catch (Error e) { // Ignore @@ -184,37 +92,15 @@ public class StreamModule : XmppStreamModule { address.device_id = 0; } - public void start_session_with(XmppStream stream, Jid jid, int device_id) { - if (active_bundle_requests.add(jid.bare_jid.to_string() + @":$device_id")) { - if (Plugin.DEBUG) print(@"OMEMO: Asking for bundle from $(jid.bare_jid.to_string()):$device_id\n"); - stream.get_module(Pubsub.Module.IDENTITY).request(stream, jid.bare_jid, @"$NODE_BUNDLES:$device_id", (stream, jid, id, node) => { - on_other_bundle_result(stream, jid, device_id, id, node); - }); - } - } - public void fetch_bundle(XmppStream stream, Jid jid, int device_id) { if (active_bundle_requests.add(jid.bare_jid.to_string() + @":$device_id")) { if (Plugin.DEBUG) print(@"OMEMO: Asking for bundle from $(jid.bare_jid.to_string()):$device_id\n"); stream.get_module(Pubsub.Module.IDENTITY).request(stream, jid.bare_jid, @"$NODE_BUNDLES:$device_id", (stream, jid, id, node) => { - stream.get_module(IDENTITY).active_bundle_requests.remove(jid.bare_jid.to_string() + @":$device_id"); - bundle_fetched(jid, device_id, new Bundle(node)); + on_other_bundle_result(stream, jid, device_id, id, node); }); } } - public ArrayList<int32> get_device_list(Jid jid) { - if (is_known_address(jid)) { - return device_lists[jid]; - } else { - return new ArrayList<int32>(); - } - } - - public bool is_known_address(Jid jid) { - return device_lists.has_key(jid); - } - public void ignore_device(Jid jid, int32 device_id) { if (device_id <= 0) return; lock (ignored_devices) { @@ -223,7 +109,6 @@ public class StreamModule : XmppStreamModule { } ignored_devices[jid].add(device_id); } - session_start_failed(jid, device_id); } public bool is_ignored_device(Jid jid, int32 device_id) { @@ -234,47 +119,50 @@ 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 start_session(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)); + } 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) { diff --git a/plugins/omemo/src/trust_manager.vala b/plugins/omemo/src/trust_manager.vala new file mode 100644 index 00000000..ca505c3f --- /dev/null +++ b/plugins/omemo/src/trust_manager.vala @@ -0,0 +1,168 @@ +using Dino.Entities; +using Gee; +using Xmpp; +using Signal; +using Qlite; + +namespace Dino.Plugins.Omemo { + +public class TrustManager { + + private StreamInteractor stream_interactor; + private Database db; + private ReceivedMessageListener received_message_listener; + + public TrustManager(StreamInteractor stream_interactor, Database db) { + this.stream_interactor = stream_interactor; + this.db = db; + + received_message_listener = new ReceivedMessageListener(stream_interactor, db); + stream_interactor.get_module(MessageProcessor.IDENTITY).received_pipeline.connect(received_message_listener); + } + + private StanzaNode create_encrypted_key(uint8[] key, Address address, Store store) throws GLib.Error { + SessionCipher cipher = store.create_session_cipher(address); + CiphertextMessage device_key = cipher.encrypt(key); + StanzaNode key_node = new StanzaNode.build("key", NS_URI) + .put_attribute("rid", address.device_id.to_string()) + .put_node(new StanzaNode.text(Base64.encode(device_key.serialized))); + if (device_key.type == CiphertextType.PREKEY) key_node.put_attribute("prekey", "true"); + return key_node; + } + + public EncryptState encrypt(MessageStanza message, Jid self_jid, Gee.List<Jid> recipients, XmppStream stream, Account account) { + EncryptState status = new EncryptState(); + if (!Plugin.ensure_context()) return status; + if (message.to == null) return status; + + StreamModule module = stream.get_module(StreamModule.IDENTITY); + + try { + if (!is_known_address(account, self_jid)) return status; + status.own_list = true; + status.own_devices = get_trusted_devices(account, self_jid).size; + status.other_waiting_lists = 0; + status.other_devices = 0; + foreach (Jid recipient in recipients) { + if (!is_known_address(account, recipient)) { + status.other_waiting_lists++; + return status; + } + status.other_devices += get_trusted_devices(account, recipient).size; + } + if (status.own_devices == 0 || status.other_devices == 0) return status; + + uint8[] key = new uint8[16]; + Plugin.get_context().randomize(key); + uint8[] iv = new uint8[16]; + Plugin.get_context().randomize(iv); + + uint8[] ciphertext = aes_encrypt(Cipher.AES_GCM_NOPADDING, key, iv, message.body.data); + + StanzaNode header; + StanzaNode encrypted = new StanzaNode.build("encrypted", NS_URI).add_self_xmlns() + .put_node(header = new StanzaNode.build("header", NS_URI) + .put_attribute("sid", module.store.local_registration_id.to_string()) + .put_node(new StanzaNode.build("iv", NS_URI) + .put_node(new StanzaNode.text(Base64.encode(iv))))) + .put_node(new StanzaNode.build("payload", NS_URI) + .put_node(new StanzaNode.text(Base64.encode(ciphertext)))); + + Address address = new Address(message.to.bare_jid.to_string(), 0); + foreach (Jid recipient in recipients) { + foreach(int32 device_id in get_trusted_devices(account, recipient)) { + if (module.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, module.store); + 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(); + foreach(int32 device_id in get_trusted_devices(account, self_jid)) { + if (module.is_ignored_device(self_jid, device_id)) { + status.own_lost++; + continue; + } + if (device_id != module.store.local_registration_id) { + address.device_id = (int) device_id; + try { + StanzaNode key_node = create_encrypted_key(key, address, module.store); + header.put_node(key_node); + status.own_success++; + } catch (Error e) { + if (e.code == ErrorCode.UNKNOWN) status.own_unknown++; + else status.own_failure++; + } + } + } + + message.stanza.put_node(encrypted); + Xep.ExplicitEncryption.add_encryption_tag_to_message(message, NS_URI, "OMEMO"); + message.body = "[This message is OMEMO encrypted]"; + status.encrypted = true; + } catch (Error e) { + if (Plugin.DEBUG) print(@"OMEMO: Signal error while encrypting message: $(e.message)\n"); + } + return status; + } + + public bool is_known_address(Account account, Jid jid) { + return db.identity_meta.with_address(account.id, jid.to_string()).count() > 0; + } + + public Gee.List<int32> get_trusted_devices(Account account, Jid jid) { + Gee.List<int32> devices = new ArrayList<int32>(); + foreach (Row device in db.identity_meta.with_address(account.id, jid.to_string()).with(db.identity_meta.trust_level, "!=", Database.IdentityMetaTable.TrustLevel.UNTRUSTED)) { + if(device[db.identity_meta.trust_level] != Database.IdentityMetaTable.TrustLevel.UNKNOWN || device[db.identity_meta.identity_key_public_base64] == null) + devices.add(device[db.identity_meta.device_id]); + } + return devices; + } + + private class ReceivedMessageListener : MessageListener { + public string[] after_actions_const = new string[]{ }; + public override string action_group { get { return "DECRYPT"; } } + public override string[] after_actions { get { return after_actions_const; } } + + private StreamInteractor stream_interactor; + private Database db; + + public ReceivedMessageListener(StreamInteractor stream_interactor, Database db) { + this.stream_interactor = stream_interactor; + this.db = db; + } + + public override async bool run(Entities.Message message, Xmpp.MessageStanza stanza, Conversation conversation) { + MessageFlag? flag = MessageFlag.get_flag(stanza); + if(flag != null && ((!)flag).decrypted) { + StanzaNode header = stanza.stanza.get_subnode("encrypted", "eu.siacs.conversations.axolotl").get_subnode("header"); + Jid jid = message.from; + if(conversation.type_ == Conversation.Type.GROUPCHAT) { + jid = stream_interactor.get_module(MucManager.IDENTITY).get_real_jid(jid, conversation.account); + } + Database.IdentityMetaTable.TrustLevel trust_level = (Database.IdentityMetaTable.TrustLevel) db.identity_meta.with_address(conversation.account.id, jid.bare_jid.to_string()).with(db.identity_meta.device_id, "=", header.get_attribute_int("sid")).single()[db.identity_meta.trust_level]; + if (trust_level == Database.IdentityMetaTable.TrustLevel.UNTRUSTED) { + message.body = "OMEMO message from a rejected device"; + message.marked = Message.Marked.WONTSEND; + } + if (trust_level == Database.IdentityMetaTable.TrustLevel.UNKNOWN) { + message.body = "OMEMO message from an unknown device: "+message.body; + message.marked = Message.Marked.WONTSEND; + } + } + return false; + } + } +} + +} diff --git a/plugins/signal-protocol/src/store.vala b/plugins/signal-protocol/src/store.vala index eab57e5b..8a0e5853 100644 --- a/plugins/signal-protocol/src/store.vala +++ b/plugins/signal-protocol/src/store.vala @@ -375,6 +375,10 @@ public class Store : Object { return throw_by_code(Protocol.Session.contains_session(native_context, other)) == 1; } + public void delete_session(Address address) throws Error { + throw_by_code(Protocol.Session.delete_session(native_context, address)); + } + public SessionRecord load_session(Address other) throws Error { SessionRecord record; throw_by_code(Protocol.Session.load_session(native_context, out record, other)); @@ -410,4 +414,4 @@ public class Store : Object { } } -}
\ No newline at end of file +} |