aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/xep/0384_omemo/omemo_decryptor.vala
blob: a8ca5016e70267b8dc704d17dea37b7e8d43f819 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using Gee;
using Xmpp.Xep;
using Xmpp;

namespace Xmpp.Xep.Omemo {

    public abstract class OmemoDecryptor : XmppStreamModule {

        public static Xmpp.ModuleIdentity<OmemoDecryptor> IDENTITY = new Xmpp.ModuleIdentity<OmemoDecryptor>(NS_URI, "0384_omemo_decryptor");

        public abstract uint32 own_device_id { get; }

        public abstract string decrypt(uint8[] ciphertext, uint8[] key, uint8[] iv) throws GLib.Error;

        public abstract uint8[] decrypt_key(ParsedData data, Jid from_jid) throws GLib.Error;

        public ParsedData? parse_node(StanzaNode encrypted_node) {
            ParsedData ret = new ParsedData();

            StanzaNode? header_node = encrypted_node.get_subnode("header");
            if (header_node == null) {
                warning("Can't parse OMEMO node: No header node");
                return null;
            }

            ret.sid = header_node.get_attribute_int("sid", -1);
            if (ret.sid == -1) {
                warning("Can't parse OMEMO node: No sid");
                return null;
            }

            string? payload_str = encrypted_node.get_deep_string_content("payload");
            if (payload_str != null) ret.ciphertext = Base64.decode(payload_str);

            string? iv_str = header_node.get_deep_string_content("iv");
            if (iv_str == null) {
                warning("Can't parse OMEMO node: No iv");
                return null;
            }
            ret.iv = Base64.decode(iv_str);

            foreach (StanzaNode key_node in header_node.get_subnodes("key")) {
                debug("Is ours? %d =? %u", key_node.get_attribute_int("rid"), own_device_id);
                if (key_node.get_attribute_int("rid") == own_device_id) {
                    string? key_node_content = key_node.get_string_content();
                    if (key_node_content == null) continue;
                    uchar[] encrypted_key = Base64.decode(key_node_content);
                    ret.our_potential_encrypted_keys[new Bytes.take(encrypted_key)] = key_node.get_attribute_bool("prekey");
                }
            }

            return ret;
        }

        public override void attach(XmppStream stream) { }
        public override void detach(XmppStream stream) { }
        public override string get_ns() { return NS_URI; }
        public override string get_id() { return IDENTITY.id; }
    }

    public class ParsedData {
        public int sid;
        public uint8[] ciphertext;
        public uint8[] iv;
        public uchar[] encrypted_key;
        public bool is_prekey;

        public HashMap<Bytes, bool> our_potential_encrypted_keys = new HashMap<Bytes, bool>();
    }
}