Debian Codegraph @

xmpp-vala/src/module/xep/0384_omemo/omemo_encryptor.vala

raw
1 using Gee;
2 using Xmpp.Xep;
3 using Xmpp;
4
5 namespace Xmpp.Xep.Omemo {
6
7 public const string NS_URI = "eu.siacs.conversations.axolotl";
8 public const string NODE_DEVICELIST = NS_URI + ".devicelist";
9 public const string NODE_BUNDLES = NS_URI + ".bundles";
10 public const string NODE_VERIFICATION = NS_URI + ".verification";
11
12 public abstract class OmemoEncryptor : XmppStreamModule {
13
14 public static Xmpp.ModuleIdentity<OmemoEncryptor> IDENTITY = new Xmpp.ModuleIdentity<OmemoEncryptor>(NS_URI, "0384_omemo_encryptor");
15
16 public abstract uint32 own_device_id { get; }
17
18 public abstract EncryptionData encrypt_plaintext(string plaintext) throws GLib.Error;
19
20 public abstract void encrypt_key(Xep.Omemo.EncryptionData encryption_data, Jid jid, int32 device_id) throws GLib.Error;
21
22 public abstract EncryptionResult encrypt_key_to_recipient(XmppStream stream, Xep.Omemo.EncryptionData enc_data, Jid recipient) throws GLib.Error;
23
24 public override void attach(XmppStream stream) { }
25 public override void detach(XmppStream stream) { }
26 public override string get_ns() { return NS_URI; }
27 public override string get_id() { return IDENTITY.id; }
28 }
29
30 public class EncryptionData {
31 public uint32 own_device_id;
32 public uint8[] ciphertext;
33 public uint8[] keytag;
34 public uint8[] iv;
35
36 public Gee.List<StanzaNode> key_nodes = new ArrayList<StanzaNode>();
37
38 public EncryptionData(uint32 own_device_id) {
39 this.own_device_id = own_device_id;
40 }
41
42 public void add_device_key(int device_id, uint8[] device_key, bool prekey) {
43 StanzaNode key_node = new StanzaNode.build("key", NS_URI)
44 .put_attribute("rid", device_id.to_string())
45 .put_node(new StanzaNode.text(Base64.encode(device_key)));
46 if (prekey) {
47 key_node.put_attribute("prekey", "true");
48 }
49 key_nodes.add(key_node);
50 }
51
52 public StanzaNode get_encrypted_node() {
53 StanzaNode encrypted_node = new StanzaNode.build("encrypted", NS_URI).add_self_xmlns();
54
55 StanzaNode header_node = new StanzaNode.build("header", NS_URI)
56 .put_attribute("sid", own_device_id.to_string())
57 .put_node(new StanzaNode.build("iv", NS_URI).put_node(new StanzaNode.text(Base64.encode(iv))));
58 encrypted_node.put_node(header_node);
59
60 if (ciphertext != null) {
61 StanzaNode payload_node = new StanzaNode.build("payload", NS_URI)
62 .put_node(new StanzaNode.text(Base64.encode(ciphertext)));
63 encrypted_node.put_node(payload_node);
64 }
65
66 foreach (StanzaNode key_node in key_nodes) {
67 header_node.put_node(key_node);
68 }
69
70 return encrypted_node;
71 }
72 }
73
74 public class EncryptionResult {
75 public int lost { get; set; }
76 public int success { get; set; }
77 public int unknown { get; set; }
78 public int failure { get; set; }
79 }
80
81 public class EncryptState {
82 public bool encrypted { get; set; }
83 public int other_devices { get; set; }
84 public int other_success { get; set; }
85 public int other_lost { get; set; }
86 public int other_unknown { get; set; }
87 public int other_failure { get; set; }
88 public int other_waiting_lists { get; set; }
89
90 public int own_devices { get; set; }
91 public int own_success { get; set; }
92 public int own_lost { get; set; }
93 public int own_unknown { get; set; }
94 public int own_failure { get; set; }
95 public bool own_list { get; set; }
96
97 public void add_result(EncryptionResult enc_res, bool own) {
98 if (own) {
99 own_lost += enc_res.lost;
100 own_success += enc_res.success;
101 own_unknown += enc_res.unknown;
102 own_failure += enc_res.failure;
103 } else {
104 other_lost += enc_res.lost;
105 other_success += enc_res.success;
106 other_unknown += enc_res.unknown;
107 other_failure += enc_res.failure;
108 }
109 }
110
111 public string to_string() {
112 return @"EncryptState (encrypted=$encrypted, other=(devices=$other_devices, success=$other_success, lost=$other_lost, unknown=$other_unknown, failure=$other_failure, waiting_lists=$other_waiting_lists, own=(devices=$own_devices, success=$own_success, lost=$own_lost, unknown=$own_unknown, failure=$own_failure, list=$own_list))";
113 }
114 }
115 }
116