From 29a073493aafc26c2576a928681364d9652d2830 Mon Sep 17 00:00:00 2001 From: moson-mo Date: Fri, 26 Jan 2024 15:48:27 +0100 Subject: [PATCH] Add option to display Master key Signed-off-by: moson-mo --- src/Gocrypt.vala | 18 ++++++++++++++++++ src/ui/CryptorWindow.vala | 16 +++++++++++++++- src/ui/Utils.vala | 13 +++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/Gocrypt.vala b/src/Gocrypt.vala index 4547387..38dbda2 100644 --- a/src/Gocrypt.vala +++ b/src/Gocrypt.vala @@ -70,6 +70,24 @@ namespace Cryptor { } } + public static string ? get_master_key (string path, string password) throws Error { + string ? standard_output, standard_error; + string[] cmd = { "gocryptfs-xray", "-dumpmasterkey", path + "/gocryptfs.conf" }; + + var sp = new Subprocess.newv (cmd, SubprocessFlags.STDIN_PIPE | SubprocessFlags.STDERR_PIPE | SubprocessFlags.STDOUT_PIPE); + sp.communicate_utf8 (password + "\n", null, out standard_output, out standard_error); + var status = sp.get_exit_status (); + + if (standard_error != null && standard_error != "") { + throw new Error (Quark.from_string ("Cryptor"), status, standard_error); + } + if (status != 0) { + throw new Error (Quark.from_string ("Cryptor"), status, standard_output); + } + + return standard_output; + } + private static File write_password_file (string password) throws Error { FileIOStream ps; var temp_file = File.new_tmp (null, out ps); diff --git a/src/ui/CryptorWindow.vala b/src/ui/CryptorWindow.vala index 5d8eea6..6bb4631 100644 --- a/src/ui/CryptorWindow.vala +++ b/src/ui/CryptorWindow.vala @@ -30,7 +30,7 @@ namespace Cryptor.UI { public CryptorWindow (Gtk.Application app) { Object ( - application: app + application : app ); config = new Config (); @@ -293,6 +293,19 @@ namespace Cryptor.UI { edit.activate.connect (() => { show_vault_window (get_selected_rownumber ()); }); + var mkey = Utils.get_image_menu_item ("gtk-justify-fill", _("Show Master key")); + mkey.activate.connect (() => { + var password = Utils.show_password_entry (this, false, false); + if (password == null) { + return; + } + try { + var key = Gocrypt.get_master_key (vault.path, password); + Utils.show_info_textbox (this, _("Master key:"), key.replace ("\n", "")); + } catch (Error e) { + Utils.show_error (this, e.message); + } + }); var remove = Utils.get_image_menu_item ("gtk-remove", _("Remove")); remove.sensitive = !vault.is_mounted; remove.activate.connect (() => { @@ -301,6 +314,7 @@ namespace Cryptor.UI { }); menu.add (open); menu.add (mount); + menu.add (mkey); menu.add (edit); menu.add (remove); menu.show_all (); diff --git a/src/ui/Utils.vala b/src/ui/Utils.vala index 833c623..5f7ebf9 100644 --- a/src/ui/Utils.vala +++ b/src/ui/Utils.vala @@ -33,6 +33,19 @@ namespace Cryptor.UI { dialog.destroy (); } + public static void show_info_textbox (Window parent, string title, string message) { + var dialog = new MessageDialog (parent, Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "%s", title); + var area = dialog.get_content_area (); + var entry = new Entry (); + entry.editable = false; + entry.width_chars = message.length; + entry.text = message; + area.pack_end (entry, false, false, 0); + dialog.show_all (); + dialog.run (); + dialog.destroy (); + } + public static string ? show_password_entry (Window parent, bool reenter, bool newpw) { string message; string ? ret = null;