From e2d801b5f74b60c38a75310066c48468c8a4bc93 Mon Sep 17 00:00:00 2001 From: hrxi Date: Sun, 4 Jun 2023 09:24:16 +0200 Subject: Merge `gpgme-vala` into `openpgp` plugin There's no reason for it to be a statically linked library anymore, it can be directly compiled into the plugin. --- plugins/openpgp/src/gpgme_fix.c | 12 +++ plugins/openpgp/src/gpgme_fix.h | 12 +++ plugins/openpgp/src/gpgme_helper.vala | 184 ++++++++++++++++++++++++++++++++++ 3 files changed, 208 insertions(+) create mode 100644 plugins/openpgp/src/gpgme_fix.c create mode 100644 plugins/openpgp/src/gpgme_fix.h create mode 100644 plugins/openpgp/src/gpgme_helper.vala (limited to 'plugins/openpgp/src') diff --git a/plugins/openpgp/src/gpgme_fix.c b/plugins/openpgp/src/gpgme_fix.c new file mode 100644 index 00000000..bf457a6c --- /dev/null +++ b/plugins/openpgp/src/gpgme_fix.c @@ -0,0 +1,12 @@ +#include + +GRecMutex gpgme_global_mutex = {0}; + +gpgme_key_t gpgme_key_ref_vapi (gpgme_key_t key) { + gpgme_key_ref(key); + return key; +} +gpgme_key_t gpgme_key_unref_vapi (gpgme_key_t key) { + gpgme_key_unref(key); + return key; +} diff --git a/plugins/openpgp/src/gpgme_fix.h b/plugins/openpgp/src/gpgme_fix.h new file mode 100644 index 00000000..714614fc --- /dev/null +++ b/plugins/openpgp/src/gpgme_fix.h @@ -0,0 +1,12 @@ +#ifndef GPGME_FIX +#define GPGME_FIX 1 + +#include +#include + +extern GRecMutex gpgme_global_mutex; + +gpgme_key_t gpgme_key_ref_vapi (gpgme_key_t key); +gpgme_key_t gpgme_key_unref_vapi (gpgme_key_t key); + +#endif diff --git a/plugins/openpgp/src/gpgme_helper.vala b/plugins/openpgp/src/gpgme_helper.vala new file mode 100644 index 00000000..f28bc6d6 --- /dev/null +++ b/plugins/openpgp/src/gpgme_helper.vala @@ -0,0 +1,184 @@ +using Gee; +using GPG; + +namespace GPGHelper { + +private static bool initialized = false; + +public static string encrypt_armor(string plain, Key[] keys, EncryptFlags flags) throws GLib.Error { + global_mutex.lock(); + try { + initialize(); + Data plain_data = Data.create_from_memory(plain.data, false); + Context context = Context.create(); + context.set_armor(true); + Data enc_data = context.op_encrypt(keys, flags, plain_data); + return get_string_from_data(enc_data); + } finally { + global_mutex.unlock(); + } +} + +public static uint8[] encrypt_file(string uri, Key[] keys, EncryptFlags flags, string file_name) throws GLib.Error { + global_mutex.lock(); + try { + initialize(); + Data plain_data = Data.create_from_file(uri); + plain_data.set_file_name(file_name); + Context context = Context.create(); + context.set_armor(true); + Data enc_data = context.op_encrypt(keys, flags, plain_data); + return get_uint8_from_data(enc_data); + } finally { + global_mutex.unlock(); + } +} + +public static string decrypt(string encr) throws GLib.Error { + global_mutex.lock(); + try { + initialize(); + Data enc_data = Data.create_from_memory(encr.data, false); + Context context = Context.create(); + Data dec_data = context.op_decrypt(enc_data); + return get_string_from_data(dec_data); + } finally { + global_mutex.unlock(); + } +} + +public class DecryptedData { + public uint8[] data { get; set; } + public string filename { get; set; } +} + +public static DecryptedData decrypt_data(uint8[] data) throws GLib.Error { + global_mutex.lock(); + try { + initialize(); + Data enc_data = Data.create_from_memory(data, false); + Context context = Context.create(); + Data dec_data = context.op_decrypt(enc_data); + DecryptResult* dec_res = context.op_decrypt_result(); + return new DecryptedData() { data=get_uint8_from_data(dec_data), filename=dec_res->file_name}; + } finally { + global_mutex.unlock(); + } +} + +public static string sign(string plain, SigMode mode, Key? key = null) throws GLib.Error { + global_mutex.lock(); + try { + initialize(); + Data plain_data = Data.create_from_memory(plain.data, false); + Context context = Context.create(); + if (key != null) context.signers_add(key); + Data signed_data = context.op_sign(plain_data, mode); + return get_string_from_data(signed_data); + } finally { + global_mutex.unlock(); + } +} + +public static string? get_sign_key(string signature, string? text) throws GLib.Error { + global_mutex.lock(); + try { + initialize(); + Data sig_data = Data.create_from_memory(signature.data, false); + Data text_data; + if (text != null) { + text_data = Data.create_from_memory(text.data, false); + } else { + text_data = Data.create(); + } + Context context = Context.create(); + context.op_verify(sig_data, text_data); + VerifyResult* verify_res = context.op_verify_result(); + if (verify_res == null || verify_res.signatures == null) return null; + return verify_res.signatures.fpr; + } finally { + global_mutex.unlock(); + } +} + +public static Gee.List get_keylist(string? pattern = null, bool secret_only = false) throws GLib.Error { + global_mutex.lock(); + try { + initialize(); + + Gee.List keys = new ArrayList(); + Context context = Context.create(); + context.op_keylist_start(pattern, secret_only ? 1 : 0); + try { + while (true) { + Key key = context.op_keylist_next(); + keys.add(key); + } + } catch (Error e) { + if (e.code != GPGError.ErrorCode.EOF) throw e; + } + return keys; + } finally { + global_mutex.unlock(); + } +} + +public static Key? get_public_key(string sig) throws GLib.Error { + return get_key(sig, false); +} + +public static Key? get_private_key(string sig) throws GLib.Error { + return get_key(sig, true); +} + +private static Key? get_key(string sig, bool priv) throws GLib.Error { + global_mutex.lock(); + try { + initialize(); + Context context = Context.create(); + Key key = context.get_key(sig, priv); + return key; + } finally { + global_mutex.unlock(); + } +} + +private static string get_string_from_data(Data data) { + const size_t BUF_SIZE = 256; + data.seek(0); + uint8[] buf = new uint8[BUF_SIZE + 1]; + ssize_t len = 0; + string res = ""; + do { + len = data.read(buf, BUF_SIZE); + if (len > 0) { + buf[len] = 0; + res += (string) buf; + } + } while (len > 0); + return res; +} + +private static uint8[] get_uint8_from_data(Data data) { + const size_t BUF_SIZE = 256; + data.seek(0); + uint8[] buf = new uint8[BUF_SIZE + 1]; + ssize_t len = 0; + ByteArray res = new ByteArray(); + do { + len = data.read(buf, BUF_SIZE); + if (len > 0) { + res.append(buf[0:len]); + } + } while (len > 0); + return res.data; +} + +private static void initialize() { + if (!initialized) { + check_version(); + initialized = true; + } +} + +} -- cgit v1.2.3-54-g00ecf From 8cb195a2749b0335c8d5fefc2d4fb78023cffe71 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Sat, 7 Oct 2023 16:53:37 +0200 Subject: Fix crash due to gpg binding issue --- plugins/openpgp/src/account_settings_entry.vala | 6 ++++-- plugins/openpgp/src/gpgme_helper.vala | 1 + plugins/openpgp/vapi/gpgme.vapi | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) (limited to 'plugins/openpgp/src') diff --git a/plugins/openpgp/src/account_settings_entry.vala b/plugins/openpgp/src/account_settings_entry.vala index d2e5ac23..7c99942f 100644 --- a/plugins/openpgp/src/account_settings_entry.vala +++ b/plugins/openpgp/src/account_settings_entry.vala @@ -116,8 +116,10 @@ public class AccountSettingsEntry : Plugins.AccountSettingsEntry { SourceFunc callback = fetch_keys.callback; new Thread (null, () => { // Querying GnuPG might take some time try { - keys = GPGHelper.get_keylist(null, true); - } catch (Error e) { } + keys = GPGHelper.get_keylist(null, true); + } catch (Error e) { + warning(e.message); + } Idle.add((owned)callback); return null; }); diff --git a/plugins/openpgp/src/gpgme_helper.vala b/plugins/openpgp/src/gpgme_helper.vala index f28bc6d6..18d07c06 100644 --- a/plugins/openpgp/src/gpgme_helper.vala +++ b/plugins/openpgp/src/gpgme_helper.vala @@ -117,6 +117,7 @@ public static Gee.List get_keylist(string? pattern = null, bool secret_only } catch (Error e) { if (e.code != GPGError.ErrorCode.EOF) throw e; } + context.op_keylist_end(); return keys; } finally { global_mutex.unlock(); diff --git a/plugins/openpgp/vapi/gpgme.vapi b/plugins/openpgp/vapi/gpgme.vapi index 10fdb89d..2fc27c65 100644 --- a/plugins/openpgp/vapi/gpgme.vapi +++ b/plugins/openpgp/vapi/gpgme.vapi @@ -38,7 +38,7 @@ namespace GPG { } [Compact] - [CCode (cname = "struct _gpgme_key", ref_function = "gpgme_key_ref", ref_function_void = true, unref_function = "gpgme_key_unref", free_function = "gpgme_key_release")] + [CCode (cname = "struct _gpgme_key", ref_function = "gpgme_key_ref_vapi", unref_function = "gpgme_key_unref_vapi", free_function = "gpgme_key_release")] public class Key { public bool revoked; public bool expired; -- cgit v1.2.3-54-g00ecf