Add trust management utilities to the omemo plugin

This commit is contained in:
Samuel Hand 2018-06-11 07:11:04 +01:00
parent 630df3a2ee
commit 40c6835600
12 changed files with 539 additions and 48 deletions

View file

@ -98,9 +98,15 @@ public abstract class MetaConversationItem : Object {
public abstract Object? get_widget(WidgetType type); public abstract Object? get_widget(WidgetType type);
} }
public abstract class MetaConversationNotification : Object {
public abstract Object? get_widget(WidgetType type);
}
public interface ConversationItemCollection : Object { public interface ConversationItemCollection : Object {
public signal void insert_item(MetaConversationItem item); public signal void insert_item(MetaConversationItem item);
public signal void remove_item(MetaConversationItem item); public signal void remove_item(MetaConversationItem item);
public signal void add_meta_notification(MetaConversationNotification item);
public signal void remove_meta_notification(MetaConversationNotification item);
} }
public interface MessageDisplayProvider : Object { public interface MessageDisplayProvider : Object {

View file

@ -44,6 +44,8 @@ public class ConversationView : Box, Plugins.ConversationItemCollection {
insert_item.connect(on_insert_item); insert_item.connect(on_insert_item);
remove_item.connect(on_remove_item); remove_item.connect(on_remove_item);
add_meta_notification.connect(on_add_meta_notification);
remove_meta_notification.connect(on_remove_meta_notification);
Application app = GLib.Application.get_default() as Application; Application app = GLib.Application.get_default() as Application;
app.plugin_registry.register_conversation_item_populator(new ChatStatePopulator(stream_interactor)); app.plugin_registry.register_conversation_item_populator(new ChatStatePopulator(stream_interactor));
@ -126,6 +128,18 @@ public class ConversationView : Box, Plugins.ConversationItemCollection {
} }
} }
public void on_add_meta_notification(Plugins.MetaConversationNotification notification) {
Widget? widget = (Widget) notification.get_widget(Plugins.WidgetType.GTK);
if(widget != null)
add_notification(widget);
}
public void on_remove_meta_notification(Plugins.MetaConversationNotification notification){
Widget? widget = (Widget) notification.get_widget(Plugins.WidgetType.GTK);
if(widget != null)
remove_notification(widget);
}
public void add_notification(Widget widget) { public void add_notification(Widget widget) {
notifications.add(widget); notifications.add(widget);
Timeout.add(20, () => { Timeout.add(20, () => {

View file

@ -13,6 +13,7 @@ find_packages(OMEMO_PACKAGES REQUIRED
set(RESOURCE_LIST set(RESOURCE_LIST
account_settings_dialog.ui account_settings_dialog.ui
contact_details_dialog.ui
) )
compile_gresources( compile_gresources(
@ -32,7 +33,9 @@ SOURCES
src/account_settings_widget.vala src/account_settings_widget.vala
src/bundle.vala src/bundle.vala
src/contact_details_provider.vala src/contact_details_provider.vala
src/contact_details_dialog.vala
src/database.vala src/database.vala
src/device_notification_populator.vala
src/encrypt_state.vala src/encrypt_state.vala
src/encryption_list_entry.vala src/encryption_list_entry.vala
src/manager.vala src/manager.vala

View file

@ -0,0 +1,111 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="DinoPluginsOmemoContactDetailsDialog">
<property name="modal">True</property>
<property name="title" translatable="yes">OMEMO Keys</property>
<property name="resizable">False</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" id="fingerprints_prompt_label">
<property name="margin-top">12</property>
<property name="orientation">horizontal</property>
<property name="visible">False</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="halign">start</property>
<property name="valign">center</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">New Devices</property>
<property name="margin-bottom">2</property>
<attributes>
<attribute name="weight" value="PANGO_WEIGHT_BOLD"/>
</attributes>
</object>
</child>
</object>
</child>
<child>
<object class="GtkFrame" id="fingerprints_prompt_container">
<property name="visible">False</property>
<property name="margin-bottom">18</property>
<child>
<object class="GtkScrolledWindow">
<property name="hscrollbar_policy">never</property>
<property name="vscrollbar_policy">automatic</property>
<property name="visible">True</property>
<property name="propagate_natural_height">True</property>
<child>
<object class="GtkGrid" id="fingerprints_prompt">
<property name="visible">True</property>
<property name="vexpand">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
<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="halign">start</property>
<property name="valign">center</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Devices</property>
<property name="margin-bottom">2</property>
<attributes>
<attribute name="weight" value="PANGO_WEIGHT_BOLD"/>
</attributes>
</object>
</child>
<child>
<object class="GtkButton" id="scan_button">
<property name="visible">True</property>
<property name="can-focus">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>
<property name="margin-bottom">18</property>
<child>
<object class="GtkScrolledWindow">
<property name="hscrollbar_policy">never</property>
<property name="vscrollbar_policy">automatic</property>
<property name="visible">True</property>
<property name="propagate_natural_height">True</property>
<child>
<object class="GtkGrid" id="fingerprints">
<property name="visible">True</property>
<property name="vexpand">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</child>
</template>
</interface>

View file

@ -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;
}
}
}
}

View file

@ -1,4 +1,5 @@
using Gtk; using Gtk;
using Gee;
using Qlite; using Qlite;
using Dino.Entities; using Dino.Entities;
@ -15,20 +16,28 @@ public class ContactDetailsProvider : Plugins.ContactDetailsProvider, Object {
public void populate(Conversation conversation, Plugins.ContactDetails contact_details, WidgetType type) { public void populate(Conversation conversation, Plugins.ContactDetails contact_details, WidgetType type) {
if (conversation.type_ == Conversation.Type.CHAT && type == WidgetType.GTK) { if (conversation.type_ == Conversation.Type.CHAT && type == WidgetType.GTK) {
string res = "";
int i = 0; 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.counterpart.to_string())) {
if (row[plugin.db.identity_meta.identity_key_public_base64] != null) { 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++; i++;
} }
} }
if (i > 0) { if (i > 0) {
Label label = new Label(res) { use_markup=true, justify=Justification.RIGHT, selectable=true, visible=true }; Button btn = new Button();
contact_details.add(_("Encryption"), "OMEMO", n("%d OMEMO device", "%d OMEMO devices", i).printf(i), label); 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);
} }
} }
} }

View file

@ -9,28 +9,30 @@ public class Database : Qlite.Database {
private const int VERSION = 1; private const int VERSION = 1;
public class IdentityMetaTable : Table { 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<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<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<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<bool> now_active = new Column.BoolInt("now_active") { default = "1" };
public Column<long> last_active = new Column.Long("last_active"); public Column<long> last_active = new Column.Long("last_active");
internal IdentityMetaTable(Database db) { internal IdentityMetaTable(Database db) {
base(db, "identity_meta"); base(db, "identity_meta");
init({address_name, device_id, identity_key_public_base64, trusted_identity, now_active, last_active}); init({identity_id, 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_idx", {identity_id, address_name, device_id}, true);
index("identity_meta_list_idx", {address_name}); index("identity_meta_list_idx", {identity_id, address_name});
} }
public QueryBuilder with_address(string address_name) { public QueryBuilder with_address(string address_name) {
return select().with(this.address_name, "=", 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(); update().with(this.address_name, "=", address_name).set(now_active, false).perform();
foreach (int32 device_id in devices) { foreach (int32 device_id in devices) {
upsert() upsert()
.value(this.identity_id, identity_id, true)
.value(this.address_name, address_name, true) .value(this.address_name, address_name, true)
.value(this.device_id, device_id, true) .value(this.device_id, device_id, true)
.value(this.now_active, 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; 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.address_name, address_name, true)
.value(this.device_id, device_id, true) .value(this.device_id, device_id, true)
.value(this.identity_key_public_base64, Base64.encode(bundle.identity_key.serialize())) .value(this.identity_key_public_base64, Base64.encode(bundle.identity_key.serialize()));
.perform(); 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 IdentityMetaTable identity_meta { get; private set; }
public TrustTable trust { get; private set; }
public IdentityTable identity { get; private set; } public IdentityTable identity { get; private set; }
public SignedPreKeyTable signed_pre_key { get; private set; } public SignedPreKeyTable signed_pre_key { get; private set; }
public PreKeyTable 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) { public Database(string fileName) {
base(fileName, VERSION); base(fileName, VERSION);
identity_meta = new IdentityMetaTable(this); identity_meta = new IdentityMetaTable(this);
trust = new TrustTable(this);
identity = new IdentityTable(this); identity = new IdentityTable(this);
signed_pre_key = new SignedPreKeyTable(this); signed_pre_key = new SignedPreKeyTable(this);
pre_key = new PreKeyTable(this); pre_key = new PreKeyTable(this);
session = new SessionTable(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 { try {
exec("PRAGMA synchronous=0"); exec("PRAGMA synchronous=0");
} catch (Error e) { } } catch (Error e) { }

View file

@ -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;
}
}
}

View file

@ -116,6 +116,7 @@ public class Manager : StreamInteractionModule, Object {
if (!state.will_send_now) { if (!state.will_send_now) {
if (message.marked == Entities.Message.Marked.WONTSEND) { if (message.marked == Entities.Message.Marked.WONTSEND) {
if (Plugin.DEBUG) print(@"OMEMO: message was not sent: $state\n"); if (Plugin.DEBUG) print(@"OMEMO: message was not sent: $state\n");
message_states.unset(message);
} else { } else {
if (Plugin.DEBUG) print(@"OMEMO: message will be delayed: $state\n"); if (Plugin.DEBUG) print(@"OMEMO: message will be delayed: $state\n");
@ -206,7 +207,7 @@ public class Manager : StreamInteractionModule, Object {
return; return;
} }
ArrayList<int32> device_list = module.get_device_list(jid); 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; 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)) { 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]); 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 (inc > 0) {
if (Plugin.DEBUG) print(@"OMEMO: new bundles $inc/$(device_list.size) for $jid\n"); 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) { 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) { private void on_store_created(Account account, Store store) {

View file

@ -4,7 +4,7 @@ extern const string LOCALE_INSTALL_DIR;
namespace Dino.Plugins.Omemo { namespace Dino.Plugins.Omemo {
public class Plugin : RootInterface, Object { public class Plugin : RootInterface, Object {
public const bool DEBUG = false; public const bool DEBUG = true;
private static Signal.Context? _context; private static Signal.Context? _context;
public static Signal.Context get_context() { public static Signal.Context get_context() {
assert(_context != null); assert(_context != null);
@ -28,6 +28,7 @@ public class Plugin : RootInterface, Object {
public EncryptionListEntry list_entry; public EncryptionListEntry list_entry;
public AccountSettingsEntry settings_entry; public AccountSettingsEntry settings_entry;
public ContactDetailsProvider contact_details_provider; public ContactDetailsProvider contact_details_provider;
public DeviceNotificationPopulator device_notification_populator;
public void registered(Dino.Application app) { public void registered(Dino.Application app) {
ensure_context(); ensure_context();
@ -36,9 +37,11 @@ public class Plugin : RootInterface, Object {
this.list_entry = new EncryptionListEntry(this); this.list_entry = new EncryptionListEntry(this);
this.settings_entry = new AccountSettingsEntry(this); this.settings_entry = new AccountSettingsEntry(this);
this.contact_details_provider = new ContactDetailsProvider(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_encryption_list_entry(list_entry);
this.app.plugin_registry.register_account_settings_entry(settings_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_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) => { this.app.stream_interactor.module_manager.initialize_account_modules.connect((account, list) => {
list.add(new StreamModule()); list.add(new StreamModule());
}); });

View file

@ -103,6 +103,23 @@ public class StreamModule : XmppStreamModule {
return status; 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 { private StanzaNode create_encrypted_key(uint8[] key, Address address) throws GLib.Error {
SessionCipher cipher = store.create_session_cipher(address); SessionCipher cipher = store.create_session_cipher(address);
CiphertextMessage device_key = cipher.encrypt(key); CiphertextMessage device_key = cipher.encrypt(key);
@ -223,7 +240,7 @@ public class StreamModule : XmppStreamModule {
} }
ignored_devices[jid].add(device_id); 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) { 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) { private void on_other_bundle_result(XmppStream stream, Jid jid, int device_id, string? id, StanzaNode? node) {
bool fail = false;
if (node == null) { if (node == null) {
// Device not registered, shouldn't exist // Device not registered, shouldn't exist
fail = true; stream.get_module(IDENTITY).ignore_device(jid, device_id);
} else { } else {
Bundle bundle = new Bundle(node); Bundle bundle = new Bundle(node);
bundle_fetched(jid, device_id, bundle); bundle_fetched(jid, device_id, bundle);
int32 signed_pre_key_id = bundle.signed_pre_key_id; }
ECPublicKey? signed_pre_key = bundle.signed_pre_key; stream.get_module(IDENTITY).active_bundle_requests.remove(jid.bare_jid.to_string() + @":$device_id");
uint8[] signed_pre_key_signature = bundle.signed_pre_key_signature; }
ECPublicKey? identity_key = bundle.identity_key;
ArrayList<Bundle.PreKey> pre_keys = bundle.pre_keys; public bool create_session_if_needed(XmppStream stream, Jid jid, int32 device_id, Bundle bundle) {
if (signed_pre_key_id < 0 || signed_pre_key == null || identity_key == null || pre_keys.size == 0) { 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; fail = true;
} else { } else {
int pre_key_idx = Random.int_range(0, pre_keys.size); Address address = new Address(jid.bare_jid.to_string(), device_id);
int32 pre_key_id = pre_keys[pre_key_idx].key_id; try {
ECPublicKey? pre_key = pre_keys[pre_key_idx].key; if (store.contains_session(address)) {
if (pre_key_id < 0 || pre_key == null) { return false;
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.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) { if (fail) {
stream.get_module(IDENTITY).ignore_device(jid, device_id); 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) { public void publish_bundles_if_needed(XmppStream stream, Jid jid) {

View file

@ -375,6 +375,10 @@ public class Store : Object {
return throw_by_code(Protocol.Session.contains_session(native_context, other)) == 1; 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 { public SessionRecord load_session(Address other) throws Error {
SessionRecord record; SessionRecord record;
throw_by_code(Protocol.Session.load_session(native_context, out record, other)); throw_by_code(Protocol.Session.load_session(native_context, out record, other));
@ -410,4 +414,4 @@ public class Store : Object {
} }
} }
} }