| 1 |
using Gee; |
| 2 |
using Xmpp.Xep; |
| 3 |
using Xmpp; |
| 4 |
|
| 5 |
namespace Xmpp.Xep.Omemo { |
| 6 |
|
| 7 |
public abstract class OmemoDecryptor : XmppStreamModule { |
| 8 |
|
| 9 |
public static Xmpp.ModuleIdentity<OmemoDecryptor> IDENTITY = new Xmpp.ModuleIdentity<OmemoDecryptor>(NS_URI, "0384_omemo_decryptor"); |
| 10 |
|
| 11 |
public abstract uint32 own_device_id { get; } |
| 12 |
|
| 13 |
public abstract string decrypt(uint8[] ciphertext, uint8[] key, uint8[] iv) throws GLib.Error; |
| 14 |
|
| 15 |
public abstract uint8[] decrypt_key(ParsedData data, Jid from_jid) throws GLib.Error; |
| 16 |
|
| 17 |
public ParsedData? parse_node(StanzaNode encrypted_node) { |
| 18 |
ParsedData ret = new ParsedData(); |
| 19 |
|
| 20 |
StanzaNode? header_node = encrypted_node.get_subnode("header"); |
| 21 |
if (header_node == null) { |
| 22 |
warning("Can't parse OMEMO node: No header node"); |
| 23 |
return null; |
| 24 |
} |
| 25 |
|
| 26 |
ret.sid = header_node.get_attribute_int("sid", -1); |
| 27 |
if (ret.sid == -1) { |
| 28 |
warning("Can't parse OMEMO node: No sid"); |
| 29 |
return null; |
| 30 |
} |
| 31 |
|
| 32 |
string? payload_str = encrypted_node.get_deep_string_content("payload"); |
| 33 |
if (payload_str != null) ret.ciphertext = Base64.decode(payload_str); |
| 34 |
|
| 35 |
string? iv_str = header_node.get_deep_string_content("iv"); |
| 36 |
if (iv_str == null) { |
| 37 |
warning("Can't parse OMEMO node: No iv"); |
| 38 |
return null; |
| 39 |
} |
| 40 |
ret.iv = Base64.decode(iv_str); |
| 41 |
|
| 42 |
foreach (StanzaNode key_node in header_node.get_subnodes("key")) { |
| 43 |
debug("Is ours? %d =? %u", key_node.get_attribute_int("rid"), own_device_id); |
| 44 |
if (key_node.get_attribute_int("rid") == own_device_id) { |
| 45 |
string? key_node_content = key_node.get_string_content(); |
| 46 |
if (key_node_content == null) continue; |
| 47 |
uchar[] encrypted_key = Base64.decode(key_node_content); |
| 48 |
ret.our_potential_encrypted_keys[new Bytes.take(encrypted_key)] = key_node.get_attribute_bool("prekey"); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return ret; |
| 53 |
} |
| 54 |
|
| 55 |
public override void attach(XmppStream stream) { } |
| 56 |
public override void detach(XmppStream stream) { } |
| 57 |
public override string get_ns() { return NS_URI; } |
| 58 |
public override string get_id() { return IDENTITY.id; } |
| 59 |
} |
| 60 |
|
| 61 |
public class ParsedData { |
| 62 |
public int sid; |
| 63 |
public uint8[] ciphertext; |
| 64 |
public uint8[] iv; |
| 65 |
public uchar[] encrypted_key; |
| 66 |
public bool is_prekey; |
| 67 |
|
| 68 |
public HashMap<Bytes, bool> our_potential_encrypted_keys = new HashMap<Bytes, bool>(); |
| 69 |
} |
| 70 |
} |
| 71 |
|