2017-03-10 15:16:48 +00:00
|
|
|
using Gee;
|
|
|
|
using GPG;
|
|
|
|
|
|
|
|
namespace GPGHelper {
|
|
|
|
|
2017-03-11 22:06:34 +00:00
|
|
|
private static bool initialized = false;
|
|
|
|
|
2017-03-10 15:16:48 +00:00
|
|
|
public static string encrypt_armor(string plain, Key[] keys, EncryptFlags flags) throws GLib.Error {
|
|
|
|
global_mutex.lock();
|
2017-03-12 14:19:33 +00:00
|
|
|
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();
|
|
|
|
}
|
2017-03-10 15:16:48 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 19:56:27 +00:00
|
|
|
public static uint8[] encrypt_file(string uri, Key[] keys, EncryptFlags flags, string file_name) throws GLib.Error {
|
2017-10-15 22:23:51 +00:00
|
|
|
global_mutex.lock();
|
|
|
|
try {
|
|
|
|
initialize();
|
|
|
|
Data plain_data = Data.create_from_file(uri);
|
2018-01-28 19:56:27 +00:00
|
|
|
plain_data.set_file_name(file_name);
|
2017-10-15 22:23:51 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 15:16:48 +00:00
|
|
|
public static string decrypt(string encr) throws GLib.Error {
|
|
|
|
global_mutex.lock();
|
2017-03-12 14:19:33 +00:00
|
|
|
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();
|
|
|
|
}
|
2017-03-10 15:16:48 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 19:56:27 +00:00
|
|
|
public class DecryptedData {
|
|
|
|
public uint8[] data { get; set; }
|
|
|
|
public string filename { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public static DecryptedData decrypt_data(uint8[] data) throws GLib.Error {
|
2017-10-15 22:23:51 +00:00
|
|
|
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);
|
2018-01-28 19:56:27 +00:00
|
|
|
DecryptResult* dec_res = context.op_decrypt_result();
|
|
|
|
return new DecryptedData() { data=get_uint8_from_data(dec_data), filename=dec_res->file_name};
|
2017-10-15 22:23:51 +00:00
|
|
|
} finally {
|
|
|
|
global_mutex.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 13:44:09 +00:00
|
|
|
public static string sign(string plain, SigMode mode, Key? key = null) throws GLib.Error {
|
2017-03-10 15:16:48 +00:00
|
|
|
global_mutex.lock();
|
2017-03-12 14:19:33 +00:00
|
|
|
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();
|
|
|
|
}
|
2017-03-10 15:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static string? get_sign_key(string signature, string? text) throws GLib.Error {
|
|
|
|
global_mutex.lock();
|
2017-03-12 14:19:33 +00:00
|
|
|
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();
|
2017-03-10 15:16:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Gee.List<Key> get_keylist(string? pattern = null, bool secret_only = false) throws GLib.Error {
|
2017-03-12 14:19:33 +00:00
|
|
|
global_mutex.lock();
|
2017-03-10 15:16:48 +00:00
|
|
|
try {
|
2017-03-12 14:19:33 +00:00
|
|
|
initialize();
|
|
|
|
|
|
|
|
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;
|
2017-03-10 15:16:48 +00:00
|
|
|
}
|
2023-10-07 14:53:37 +00:00
|
|
|
context.op_keylist_end();
|
2017-03-12 14:19:33 +00:00
|
|
|
return keys;
|
|
|
|
} finally {
|
|
|
|
global_mutex.unlock();
|
2017-03-10 15:16:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Key? get_public_key(string sig) throws GLib.Error {
|
2017-03-12 13:44:09 +00:00
|
|
|
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 {
|
2017-03-10 15:16:48 +00:00
|
|
|
global_mutex.lock();
|
2017-03-12 14:19:33 +00:00
|
|
|
try {
|
|
|
|
initialize();
|
|
|
|
Context context = Context.create();
|
|
|
|
Key key = context.get_key(sig, priv);
|
|
|
|
return key;
|
|
|
|
} finally {
|
|
|
|
global_mutex.unlock();
|
|
|
|
}
|
2017-03-10 15:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static string get_string_from_data(Data data) {
|
2020-06-28 09:25:10 +00:00
|
|
|
const size_t BUF_SIZE = 256;
|
2017-03-10 15:16:48 +00:00
|
|
|
data.seek(0);
|
2020-06-28 09:25:10 +00:00
|
|
|
uint8[] buf = new uint8[BUF_SIZE + 1];
|
|
|
|
ssize_t len = 0;
|
2017-03-10 15:16:48 +00:00
|
|
|
string res = "";
|
|
|
|
do {
|
2020-06-28 09:25:10 +00:00
|
|
|
len = data.read(buf, BUF_SIZE);
|
2017-03-10 15:16:48 +00:00
|
|
|
if (len > 0) {
|
2020-06-28 09:25:10 +00:00
|
|
|
buf[len] = 0;
|
|
|
|
res += (string) buf;
|
2017-03-10 15:16:48 +00:00
|
|
|
}
|
|
|
|
} while (len > 0);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:23:51 +00:00
|
|
|
private static uint8[] get_uint8_from_data(Data data) {
|
2020-06-28 09:25:10 +00:00
|
|
|
const size_t BUF_SIZE = 256;
|
2017-10-15 22:23:51 +00:00
|
|
|
data.seek(0);
|
2020-06-28 09:25:10 +00:00
|
|
|
uint8[] buf = new uint8[BUF_SIZE + 1];
|
|
|
|
ssize_t len = 0;
|
2019-09-10 18:56:00 +00:00
|
|
|
ByteArray res = new ByteArray();
|
2017-10-15 22:23:51 +00:00
|
|
|
do {
|
2020-06-28 09:25:10 +00:00
|
|
|
len = data.read(buf, BUF_SIZE);
|
2017-10-15 22:23:51 +00:00
|
|
|
if (len > 0) {
|
2019-09-10 18:56:00 +00:00
|
|
|
res.append(buf[0:len]);
|
2017-10-15 22:23:51 +00:00
|
|
|
}
|
|
|
|
} while (len > 0);
|
|
|
|
return res.data;
|
|
|
|
}
|
|
|
|
|
2017-03-11 22:06:34 +00:00
|
|
|
private static void initialize() {
|
|
|
|
if (!initialized) {
|
|
|
|
check_version();
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:23:51 +00:00
|
|
|
}
|