diff options
author | fiaxh <git@lightrise.org> | 2018-11-23 20:11:32 +0100 |
---|---|---|
committer | fiaxh <git@lightrise.org> | 2018-11-27 03:41:31 +0100 |
commit | 01360a73ae8a148ca19c75e3b11167965b887d8b (patch) | |
tree | 5138e52bea6362d3e0b2324befdfe77674c2afe6 | |
parent | 2208ff9799b8b551b4da5227a32a09f9e00ffb6a (diff) | |
download | dino-01360a73ae8a148ca19c75e3b11167965b887d8b.tar.gz dino-01360a73ae8a148ca19c75e3b11167965b887d8b.zip |
FileProvider for aesgcm links
Co-authored-by: Thibaut Girka <thib@sitedethib.com>
-rw-r--r-- | main/src/ui/conversation_summary/content_item_widget_factory.vala | 3 | ||||
-rw-r--r-- | plugins/CMakeLists.txt | 8 | ||||
-rw-r--r-- | plugins/http-files/src/file_provider.vala | 3 | ||||
-rw-r--r-- | plugins/omemo/CMakeLists.txt | 2 | ||||
-rw-r--r-- | plugins/omemo/src/file_provider.vala | 155 | ||||
-rw-r--r-- | plugins/omemo/src/plugin.vala | 2 |
6 files changed, 166 insertions, 7 deletions
diff --git a/main/src/ui/conversation_summary/content_item_widget_factory.vala b/main/src/ui/conversation_summary/content_item_widget_factory.vala index 26b66664..d8f65b3c 100644 --- a/main/src/ui/conversation_summary/content_item_widget_factory.vala +++ b/main/src/ui/conversation_summary/content_item_widget_factory.vala @@ -1,6 +1,7 @@ using Gee; using Gdk; using Gtk; +using Pango; using Xmpp; using Dino.Entities; @@ -191,7 +192,7 @@ public class FileItemWidgetGenerator : WidgetGenerator, Object { main_box.add(content_type_image); Box right_box = new Box(Orientation.VERTICAL, 0) { visible=true }; - Label name_label = new Label(file_transfer.file_name) { xalign=0, yalign=0, visible=true}; + Label name_label = new Label(file_transfer.file_name) { ellipsize=EllipsizeMode.END, xalign=0, yalign=0, visible=true}; right_box.add(name_label); Label mime_label = new Label("<span size='small'>" + _("File") + ": " + file_transfer.mime_type + "</span>") { use_markup=true, xalign=0, yalign=1, visible=true}; mime_label.get_style_context().add_class("dim-label"); diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 79523b8f..2ec82c91 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -1,3 +1,7 @@ +if(PLUGIN_ENABLED_http-files) + add_subdirectory(http-files) +endif(PLUGIN_ENABLED_http-files) + if(PLUGIN_ENABLED_openpgp) add_subdirectory(gpgme-vala) add_subdirectory(openpgp) @@ -8,10 +12,6 @@ if(PLUGIN_ENABLED_omemo) add_subdirectory(signal-protocol) endif(PLUGIN_ENABLED_omemo) -if(PLUGIN_ENABLED_http-files) - add_subdirectory(http-files) -endif(PLUGIN_ENABLED_http-files) - if(PLUGIN_ENABLED_notification-sound) add_subdirectory(notification-sound) endif(PLUGIN_ENABLED_notification-sound) diff --git a/plugins/http-files/src/file_provider.vala b/plugins/http-files/src/file_provider.vala index 7b52a7fb..a0fe1218 100644 --- a/plugins/http-files/src/file_provider.vala +++ b/plugins/http-files/src/file_provider.vala @@ -44,7 +44,7 @@ public class FileProvider : Dino.FileProvider, Object { if (outer.url_regex.match(message.body)) { string? oob_url = Xmpp.Xep.OutOfBandData.get_url_from_message(message.stanza); if (oob_url != null && oob_url == message.body) { - outer.on_file_message(message, conversation); + yield outer.on_file_message(message, conversation); } } return false; @@ -82,7 +82,6 @@ public class FileProvider : Dino.FileProvider, Object { var session = new Soup.Session(); var head_message = new Soup.Message("HEAD", url_body); if (head_message != null) { - SourceFunc callback = get_meta_info.callback; yield session.send_async(head_message, null); string? content_type = null, content_length = null; diff --git a/plugins/omemo/CMakeLists.txt b/plugins/omemo/CMakeLists.txt index f88abbd5..5ffb9c39 100644 --- a/plugins/omemo/CMakeLists.txt +++ b/plugins/omemo/CMakeLists.txt @@ -9,6 +9,7 @@ find_packages(OMEMO_PACKAGES REQUIRED GModule GObject GTK3 + Soup ) set(RESOURCE_LIST @@ -38,6 +39,7 @@ SOURCES src/own_notifications.vala src/encrypt_state.vala src/encryption_list_entry.vala + src/file_provider.vala src/manage_key_dialog.vala src/manager.vala src/message_flag.vala diff --git a/plugins/omemo/src/file_provider.vala b/plugins/omemo/src/file_provider.vala new file mode 100644 index 00000000..fb1c1d1e --- /dev/null +++ b/plugins/omemo/src/file_provider.vala @@ -0,0 +1,155 @@ +using Gee; +using Gtk; + +using Dino.Entities; +using Xmpp; +using Signal; + +namespace Dino.Plugins.Omemo { + +public class FileProvider : Dino.FileProvider, Object { + public string id { get { return "aesgcm"; } } + + private StreamInteractor stream_interactor; + private Dino.Database dino_db; + private Regex url_regex; + + private Gee.List<string> ignore_once = new ArrayList<string>(); + + public FileProvider(StreamInteractor stream_interactor, Dino.Database dino_db) { + this.stream_interactor = stream_interactor; + this.dino_db = dino_db; + this.url_regex = new Regex("""^aesgcm://(.*)#(([A-Fa-f0-9]{2}){48}|([A-Fa-f0-9]{2}){44})$"""); + + stream_interactor.get_module(MessageProcessor.IDENTITY).received_pipeline.connect(new ReceivedMessageListener(this)); + } + + private class ReceivedMessageListener : MessageListener { + + public string[] after_actions_const = new string[]{ "STORE" }; + public override string action_group { get { return ""; } } + public override string[] after_actions { get { return after_actions_const; } } + + private FileProvider outer; + private StreamInteractor stream_interactor; + + public ReceivedMessageListener(FileProvider outer) { + this.outer = outer; + this.stream_interactor = outer.stream_interactor; + } + + public override async bool run(Entities.Message message, Xmpp.MessageStanza stanza, Conversation conversation) { + if (message.body.has_prefix("aesgcm://") && outer.url_regex.match(message.body)) { + yield outer.on_file_message(message, conversation); + } + return false; + } + } + + private async void on_file_message(Entities.Message message, Conversation conversation) { + MatchInfo match_info; + this.url_regex.match(message.body, 0, out match_info); + string url_without_hash = match_info.fetch(1); + + FileTransfer file_transfer = new FileTransfer(); + file_transfer.account = conversation.account; + file_transfer.counterpart = message.counterpart; + file_transfer.ourpart = message.ourpart; + file_transfer.encryption = Encryption.NONE; + file_transfer.time = message.time; + file_transfer.local_time = message.local_time; + file_transfer.direction = message.direction; + file_transfer.file_name = url_without_hash.substring(url_without_hash.last_index_of("/") + 1); + file_transfer.mime_type = null; + file_transfer.size = -1; + file_transfer.state = FileTransfer.State.NOT_STARTED; + file_transfer.provider = 0; + file_transfer.info = message.id.to_string(); + + if (stream_interactor.get_module(FileManager.IDENTITY).is_sender_trustworthy(file_transfer, conversation)) { + ContentItem? content_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item(conversation, 1, message.id); + if (content_item != null) { + stream_interactor.get_module(ContentItemStore.IDENTITY).set_item_hide(content_item, true); + } + yield get_meta_info(file_transfer); + file_incoming(file_transfer, conversation); + } + } + + private async void get_meta_info(FileTransfer file_transfer) { + string url_body = dino_db.message.select({dino_db.message.body}).with(dino_db.message.id, "=", int.parse(file_transfer.info))[dino_db.message.body]; + var session = new Soup.Session(); + var head_message = new Soup.Message("HEAD", url_body); + if (head_message != null) { + yield session.send_async(head_message, null); + + string? content_type = null, content_length = null; + head_message.response_headers.foreach((name, val) => { + if (name == "Content-Type") content_type = val; + if (name == "Content-Length") content_length = val; + }); + file_transfer.mime_type = content_type; + file_transfer.size = int.parse(content_length); + } + } + + public async void download(FileTransfer file_transfer, File file) { + try { + string url_body = dino_db.message.select({dino_db.message.body}).with(dino_db.message.id, "=", int.parse(file_transfer.info))[dino_db.message.body]; + string url = "https://" + url_body.substring(9); + var session = new Soup.Session(); + Soup.Request request = session.request(url); + + file_transfer.input_stream = decrypt_file(yield request.send_async(null), url_body); + file_transfer.encryption = Encryption.OMEMO; + + OutputStream os = file.create(FileCreateFlags.REPLACE_DESTINATION); + os.splice(file_transfer.input_stream, 0); + os.close(); + file_transfer.path = file.get_basename(); + file_transfer.input_stream = file.read(); + + file_transfer.state = FileTransfer.State.COMPLETE; + } catch (Error e) { + file_transfer.state = FileTransfer.State.FAILED; + } + } + + public InputStream? decrypt_file(InputStream input_stream, string url) { + // Decode IV and key + MatchInfo match_info; + this.url_regex.match(url, 0, out match_info); + uint8[] iv_and_key = hex_to_bin(match_info.fetch(2).up()); + uint8[] iv, key; + if (iv_and_key.length == 44) { + iv = iv_and_key[0:12]; + key = iv_and_key[12:44]; + } else { + iv = iv_and_key[0:16]; + key = iv_and_key[16:48]; + } + + // Read data + uint8[] buf = new uint8[256]; + Array<uint8> data = new Array<uint8>(false, true, 0); + size_t len = -1; + do { + len = input_stream.read(buf); + data.append_vals(buf, (uint) len); + } while(len > 0); + + // Decrypt + return new MemoryInputStream.from_data(aes_decrypt(Cipher.AES_GCM_NOPADDING, key, iv, data.data)); + } + + private uint8[] hex_to_bin(string hex) { + uint8[] bin = new uint8[hex.length / 2]; + const string HEX = "0123456789ABCDEF"; + for (int i = 0; i < hex.length / 2; i++) { + bin[i] = (uint8) (HEX.index_of_char(hex[i*2]) << 4) | HEX.index_of_char(hex[i*2+1]); + } + return bin; + } +} + +} diff --git a/plugins/omemo/src/plugin.vala b/plugins/omemo/src/plugin.vala index ab22651f..be6229cc 100644 --- a/plugins/omemo/src/plugin.vala +++ b/plugins/omemo/src/plugin.vala @@ -49,6 +49,8 @@ public class Plugin : RootInterface, Object { list.add(new StreamModule()); this.own_notifications = new OwnNotifications(this, this.app.stream_interactor, account); }); + + app.stream_interactor.get_module(FileManager.IDENTITY).add_provider(new FileProvider(app.stream_interactor, app.db)); Manager.start(this.app.stream_interactor, db, trust_manager); SimpleAction own_keys_action = new SimpleAction("own-keys", VariantType.INT32); |