2017-03-02 14:37:32 +00:00
|
|
|
using GPG;
|
|
|
|
|
2017-03-12 01:49:53 +00:00
|
|
|
using Xmpp;
|
2017-03-02 14:37:32 +00:00
|
|
|
using Xmpp.Core;
|
|
|
|
|
2017-03-12 01:49:53 +00:00
|
|
|
namespace Dino.Plugins.OpenPgp {
|
2017-03-02 14:37:32 +00:00
|
|
|
private const string NS_URI = "jabber:x";
|
|
|
|
private const string NS_URI_ENCRYPTED = NS_URI + ":encrypted";
|
|
|
|
private const string NS_URI_SIGNED = NS_URI + ":signed";
|
|
|
|
|
|
|
|
public class Module : XmppStreamModule {
|
2017-03-19 11:55:36 +00:00
|
|
|
public static Core.ModuleIdentity<Module> IDENTITY = new Core.ModuleIdentity<Module>(NS_URI, "0027_current_pgp_usage");
|
2017-03-02 14:37:32 +00:00
|
|
|
|
|
|
|
public signal void received_jid_key_id(XmppStream stream, string jid, string key_id);
|
|
|
|
|
2017-03-12 13:44:09 +00:00
|
|
|
private string? signed_status = null;
|
|
|
|
private Key? own_key = null;
|
2017-11-21 22:27:27 +00:00
|
|
|
private ReceivedPipelineDecryptListener received_pipeline_decrypt_listener = new ReceivedPipelineDecryptListener();
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-03-12 13:44:09 +00:00
|
|
|
public Module(string? own_key_id = null) {
|
|
|
|
set_private_key_id(own_key_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void set_private_key_id(string? own_key_id) {
|
|
|
|
if (own_key_id != null) {
|
|
|
|
try {
|
|
|
|
own_key = GPGHelper.get_private_key(own_key_id);
|
|
|
|
if (own_key == null) print("PRIV KEY NULL\n");
|
|
|
|
} catch (Error e) { }
|
|
|
|
if (own_key != null) {
|
|
|
|
signed_status = gpg_sign("", own_key);
|
|
|
|
}
|
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
2017-10-15 22:23:51 +00:00
|
|
|
public bool encrypt(Message.Stanza message, GPG.Key[] keys) {
|
|
|
|
string? enc_body = gpg_encrypt(message.body, keys);
|
2017-03-02 14:37:32 +00:00
|
|
|
if (enc_body != null) {
|
|
|
|
message.stanza.put_node(new StanzaNode.build("x", NS_URI_ENCRYPTED).add_self_xmlns().put_node(new StanzaNode.text(enc_body)));
|
|
|
|
message.body = "[This message is OpenPGP encrypted (see XEP-0027)]";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void attach(XmppStream stream) {
|
2017-03-11 00:40:42 +00:00
|
|
|
stream.get_module(Presence.Module.IDENTITY).received_presence.connect(on_received_presence);
|
|
|
|
stream.get_module(Presence.Module.IDENTITY).pre_send_presence_stanza.connect(on_pre_send_presence_stanza);
|
2017-11-21 22:27:27 +00:00
|
|
|
stream.get_module(Message.Module.IDENTITY).received_pipeline.connect(received_pipeline_decrypt_listener);
|
2017-03-02 14:37:32 +00:00
|
|
|
stream.add_flag(new Flag());
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void detach(XmppStream stream) {
|
2017-03-11 00:40:42 +00:00
|
|
|
stream.get_module(Presence.Module.IDENTITY).received_presence.disconnect(on_received_presence);
|
|
|
|
stream.get_module(Presence.Module.IDENTITY).pre_send_presence_stanza.disconnect(on_pre_send_presence_stanza);
|
2017-11-21 22:27:27 +00:00
|
|
|
stream.get_module(Message.Module.IDENTITY).received_pipeline.disconnect(received_pipeline_decrypt_listener);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void require(XmppStream stream) {
|
2017-03-11 00:40:42 +00:00
|
|
|
if (stream.get_module(IDENTITY) == null) stream.add_module(new Module());
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override string get_ns() { return NS_URI; }
|
2017-03-19 11:55:36 +00:00
|
|
|
public override string get_id() { return IDENTITY.id; }
|
2017-03-02 14:37:32 +00:00
|
|
|
|
|
|
|
private void on_received_presence(XmppStream stream, Presence.Stanza presence) {
|
2017-11-21 22:27:27 +00:00
|
|
|
StanzaNode x_node = presence.stanza.get_subnode("x", NS_URI_SIGNED);
|
|
|
|
if (x_node == null) return;
|
|
|
|
string? sig = x_node.get_string_content();
|
|
|
|
if (sig == null) return;
|
2017-11-11 20:29:13 +00:00
|
|
|
new Thread<void*> (null, () => {
|
2017-11-21 22:27:27 +00:00
|
|
|
string signed_data = presence.status == null ? "" : presence.status;
|
|
|
|
string? key_id = get_sign_key(sig, signed_data);
|
|
|
|
if (key_id != null) {
|
|
|
|
stream.get_flag(Flag.IDENTITY).set_key_id(presence.from, key_id);
|
|
|
|
Idle.add(() => {
|
|
|
|
received_jid_key_id(stream, presence.from, key_id);
|
|
|
|
return false;
|
|
|
|
});
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
2017-11-11 20:29:13 +00:00
|
|
|
return null;
|
|
|
|
});
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void on_pre_send_presence_stanza(XmppStream stream, Presence.Stanza presence) {
|
|
|
|
if (presence.type_ == Presence.Stanza.TYPE_AVAILABLE && signed_status != null) {
|
|
|
|
presence.stanza.put_node(new StanzaNode.build("x", NS_URI_SIGNED).add_self_xmlns().put_node(new StanzaNode.text(signed_status)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:23:51 +00:00
|
|
|
private static string? gpg_encrypt(string plain, GPG.Key[] keys) {
|
2017-03-10 15:16:48 +00:00
|
|
|
string encr;
|
|
|
|
try {
|
|
|
|
encr = GPGHelper.encrypt_armor(plain, keys, GPG.EncryptFlags.ALWAYS_TRUST);
|
|
|
|
} catch (Error e) {
|
|
|
|
return null;
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
2017-03-10 15:16:48 +00:00
|
|
|
int encryption_start = encr.index_of("\n\n") + 2;
|
|
|
|
return encr.substring(encryption_start, encr.length - "\n-----END PGP MESSAGE-----".length - encryption_start);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 13:44:09 +00:00
|
|
|
private static string? get_sign_key(string sig, string signed_text) {
|
2017-03-10 15:16:48 +00:00
|
|
|
string armor = "-----BEGIN PGP MESSAGE-----\n\n" + sig + "\n-----END PGP MESSAGE-----";
|
|
|
|
string? sign_key = null;
|
|
|
|
try {
|
|
|
|
sign_key = GPGHelper.get_sign_key(armor, signed_text);
|
|
|
|
} catch (Error e) { }
|
|
|
|
return sign_key;
|
|
|
|
}
|
|
|
|
|
2017-03-12 13:44:09 +00:00
|
|
|
private static string? gpg_sign(string str, Key key) {
|
2017-03-10 15:16:48 +00:00
|
|
|
string signed;
|
|
|
|
try {
|
2017-03-12 13:44:09 +00:00
|
|
|
signed = GPGHelper.sign(str, GPG.SigMode.CLEAR, key);
|
2017-03-10 15:16:48 +00:00
|
|
|
} catch (Error e) {
|
|
|
|
return null;
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
2017-03-10 15:16:48 +00:00
|
|
|
int signature_start = signed.index_of("-----BEGIN PGP SIGNATURE-----");
|
|
|
|
signature_start = signed.index_of("\n\n", signature_start) + 2;
|
|
|
|
return signed.substring(signature_start, signed.length - "\n-----END PGP SIGNATURE-----".length - signature_start);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class MessageFlag : Message.MessageFlag {
|
|
|
|
public const string id = "pgp";
|
|
|
|
|
|
|
|
public bool decrypted = false;
|
|
|
|
|
|
|
|
public static MessageFlag? get_flag(Message.Stanza message) {
|
|
|
|
return (MessageFlag) message.get_flag(NS_URI, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string get_ns() { return NS_URI; }
|
|
|
|
public override string get_id() { return id; }
|
|
|
|
}
|
2017-11-11 20:29:13 +00:00
|
|
|
|
|
|
|
public class ReceivedPipelineDecryptListener : StanzaListener<Message.Stanza> {
|
|
|
|
|
|
|
|
private const string[] after_actions_const = {"MODIFY_BODY"};
|
|
|
|
|
|
|
|
public override string action_group { get { return "ENCRYPT_BODY"; } }
|
|
|
|
public override string[] after_actions { get { return after_actions_const; } }
|
|
|
|
|
|
|
|
public override async void run(Core.XmppStream stream, Message.Stanza message) {
|
|
|
|
string? encrypted = get_cyphertext(message);
|
|
|
|
if (encrypted != null) {
|
|
|
|
MessageFlag flag = new MessageFlag();
|
|
|
|
message.add_flag(flag);
|
|
|
|
string? decrypted = yield gpg_decrypt(encrypted);
|
|
|
|
if (decrypted != null) {
|
|
|
|
flag.decrypted = true;
|
|
|
|
message.body = decrypted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static async string? gpg_decrypt(string enc) {
|
|
|
|
SourceFunc callback = gpg_decrypt.callback;
|
|
|
|
string? res = null;
|
|
|
|
new Thread<void*> (null, () => {
|
|
|
|
string armor = "-----BEGIN PGP MESSAGE-----\n\n" + enc + "\n-----END PGP MESSAGE-----";
|
|
|
|
try {
|
|
|
|
res = GPGHelper.decrypt(armor);
|
|
|
|
} catch (Error e) {
|
|
|
|
res = null;
|
|
|
|
}
|
|
|
|
Idle.add((owned) callback);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
yield;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string? get_cyphertext(Message.Stanza message) {
|
|
|
|
StanzaNode? x_node = message.stanza.get_subnode("x", NS_URI_ENCRYPTED);
|
|
|
|
return x_node == null ? null : x_node.get_string_content();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|