Unlock mutex on exceptions in gpgme helper

This commit is contained in:
Marvin W 2017-03-12 15:19:33 +01:00
parent f24b47c44d
commit a4cb998ec4
No known key found for this signature in database
GPG key ID: 072E9235DB996F2A

View file

@ -6,74 +6,87 @@ namespace GPGHelper {
private static bool initialized = false; private static bool initialized = false;
public static string encrypt_armor(string plain, Key[] keys, EncryptFlags flags) throws GLib.Error { public static string encrypt_armor(string plain, Key[] keys, EncryptFlags flags) throws GLib.Error {
initialize();
global_mutex.lock(); global_mutex.lock();
Data plain_data = Data.create_from_memory(plain.data, false); try {
Context context = Context.create(); initialize();
context.set_armor(true); Data plain_data = Data.create_from_memory(plain.data, false);
Data enc_data = context.op_encrypt(keys, flags, plain_data); Context context = Context.create();
global_mutex.unlock(); context.set_armor(true);
return get_string_from_data(enc_data); Data enc_data = context.op_encrypt(keys, flags, plain_data);
return get_string_from_data(enc_data);
} finally {
global_mutex.unlock();
}
} }
public static string decrypt(string encr) throws GLib.Error { public static string decrypt(string encr) throws GLib.Error {
initialize();
global_mutex.lock(); global_mutex.lock();
Data enc_data = Data.create_from_memory(encr.data, false); try {
Context context = Context.create(); initialize();
Data dec_data = context.op_decrypt(enc_data); Data enc_data = Data.create_from_memory(encr.data, false);
global_mutex.unlock(); Context context = Context.create();
return get_string_from_data(dec_data); Data dec_data = context.op_decrypt(enc_data);
return get_string_from_data(dec_data);
} finally {
global_mutex.unlock();
}
} }
public static string sign(string plain, SigMode mode, Key? key = null) throws GLib.Error { public static string sign(string plain, SigMode mode, Key? key = null) throws GLib.Error {
initialize();
global_mutex.lock(); global_mutex.lock();
Data plain_data = Data.create_from_memory(plain.data, false); try {
Context context = Context.create(); initialize();
if (key != null) context.signers_add(key); Data plain_data = Data.create_from_memory(plain.data, false);
Data signed_data = context.op_sign(plain_data, mode); Context context = Context.create();
global_mutex.unlock(); if (key != null) context.signers_add(key);
return get_string_from_data(signed_data); 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 { public static string? get_sign_key(string signature, string? text) throws GLib.Error {
initialize();
global_mutex.lock(); global_mutex.lock();
Data sig_data = Data.create_from_memory(signature.data, false); try {
Data text_data; initialize();
if (text != null) { Data sig_data = Data.create_from_memory(signature.data, false);
text_data = Data.create_from_memory(text.data, false); Data text_data;
} else { if (text != null) {
text_data = Data.create(); 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();
} }
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;
global_mutex.unlock();
return verify_res.signatures.fpr;
} }
public static Gee.List<Key> get_keylist(string? pattern = null, bool secret_only = false) throws GLib.Error { public static Gee.List<Key> get_keylist(string? pattern = null, bool secret_only = false) throws GLib.Error {
initialize(); global_mutex.lock();
Gee.List<Key> keys = new ArrayList<Key>();
Context context = Context.create();
context.op_keylist_start(pattern, secret_only ? 1 : 0);
try { try {
while (true) { initialize();
Key key = context.op_keylist_next();
keys.add(key); Gee.List<Key> keys = new ArrayList<Key>();
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;
} }
} catch (Error e) { return keys;
if (e.code != GPGError.ErrorCode.EOF) throw e; } finally {
global_mutex.unlock();
} }
return keys;
} }
public static Key? get_public_key(string sig) throws GLib.Error { public static Key? get_public_key(string sig) throws GLib.Error {
@ -85,18 +98,18 @@ public static Key? get_private_key(string sig) throws GLib.Error {
} }
private static Key? get_key(string sig, bool priv) throws GLib.Error { private static Key? get_key(string sig, bool priv) throws GLib.Error {
initialize();
global_mutex.lock(); global_mutex.lock();
Context context = Context.create(); try {
Key key = context.get_key(sig, priv); initialize();
global_mutex.unlock(); Context context = Context.create();
return key; Key key = context.get_key(sig, priv);
return key;
} finally {
global_mutex.unlock();
}
} }
private static string get_string_from_data(Data data) { private static string get_string_from_data(Data data) {
initialize();
data.seek(0); data.seek(0);
uint8[] buf = new uint8[256]; uint8[] buf = new uint8[256];
ssize_t? len = null; ssize_t? len = null;