Debian Codegraph @

xmpp-vala/src/module/xep/0115_entity_capabilities.vala

raw
1 using Gee;
2
3 namespace Xmpp.Xep.EntityCapabilities {
4 private const string NS_URI = "http://jabber.org/protocol/caps";
5
6 private Regex? sha1_base64_regex = null;
7
8 private Regex get_sha1_base64_regex() {
9 if (sha1_base64_regex == null) {
10 sha1_base64_regex = /^[A-Za-z0-9+\/]{27}=$/;
11 }
12 return sha1_base64_regex;
13 }
14
15 public string? get_caps_hash(Presence.Stanza presence) {
16 Regex sha1_base64_regex = get_sha1_base64_regex();
17 StanzaNode? c_node = presence.stanza.get_subnode("c", NS_URI);
18 if (c_node == null) return null;
19 string? ver_attribute = c_node.get_attribute("ver", NS_URI);
20 if (ver_attribute == null || !sha1_base64_regex.match(ver_attribute)) return null;
21 return ver_attribute;
22 }
23
24 public string? get_server_caps_hash(XmppStream stream) {
25 StanzaNode? node = stream.features.get_subnode("c", NS_URI);
26 if (node == null) return null;
27
28 string? ver_attribute = node.get_attribute("ver", NS_URI);
29 if (ver_attribute == null) return null;
30
31 return ver_attribute;
32 }
33
34 public class Module : XmppStreamModule {
35 public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0115_entity_capabilities");
36
37 private string own_ver_hash;
38 private Storage storage;
39
40 public Module(Storage storage) {
41 this.storage = storage;
42 }
43
44 private string get_own_hash(XmppStream stream) {
45 if (own_ver_hash == null) {
46 own_ver_hash = compute_hash(stream.get_flag(ServiceDiscovery.Flag.IDENTITY).own_identities, stream.get_flag(ServiceDiscovery.Flag.IDENTITY).own_features, new ArrayList<DataForms.DataForm>());
47 }
48 return own_ver_hash;
49 }
50
51 public override void attach(XmppStream stream) {
52 stream.get_module(Presence.Module.IDENTITY).pre_send_presence_stanza.connect(on_pre_send_presence_stanza);
53 stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI);
54 }
55
56 public override void detach(XmppStream stream) {
57 stream.get_module(Presence.Module.IDENTITY).pre_send_presence_stanza.disconnect(on_pre_send_presence_stanza);
58 stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI);
59 }
60
61 public override string get_ns() { return NS_URI; }
62 public override string get_id() { return IDENTITY.id; }
63
64 private void on_pre_send_presence_stanza(XmppStream stream, Presence.Stanza presence) {
65 if (presence.type_ == Presence.Stanza.TYPE_AVAILABLE) {
66 presence.stanza.put_node(new StanzaNode.build("c", NS_URI).add_self_xmlns()
67 .put_attribute("hash", "sha-1")
68 .put_attribute("node", "https://dino.im")
69 .put_attribute("ver", get_own_hash(stream)));
70 }
71 }
72
73 public static string compute_hash_for_info_result(ServiceDiscovery.InfoResult info_result) {
74 Gee.List<DataForms.DataForm> data_forms = new ArrayList<DataForms.DataForm>();
75 foreach (StanzaNode node in info_result.iq.stanza.get_deep_subnodes(ServiceDiscovery.NS_URI_INFO + ":query", DataForms.NS_URI + ":x")) {
76 data_forms.add(DataForms.DataForm.create_from_node(node));
77 }
78
79 return compute_hash(info_result.identities, info_result.features, data_forms);
80 }
81
82 public static string compute_hash(Gee.Set<ServiceDiscovery.Identity> identities_set, Gee.List<string> features, Gee.List<DataForms.DataForm> data_forms) {
83 var identities = new ArrayList<ServiceDiscovery.Identity>();
84 foreach (var identity in identities_set) identities.add(identity);
85
86 identities.sort(compare_identities);
87 features.sort();
88
89 StringBuilder sb = new StringBuilder();
90 foreach (ServiceDiscovery.Identity identity in identities) {
91 sb.append(sanitize(identity.category))
92 .append("/")
93 .append(sanitize(identity.type_))
94 .append("//");
95 if (identity.name != null) {
96 sb.append(sanitize(identity.name));
97 }
98 sb.append("<");
99 }
100 foreach (string feature in features) {
101 sb.append(sanitize(feature))
102 .append("<");
103 }
104
105 data_forms.sort(compare_data_forms);
106 foreach (DataForms.DataForm data_form in data_forms) {
107 if (data_form.form_type == null) {
108 // If [..] the FORM_TYPE field is not of type "hidden" or the form does not include a FORM_TYPE field, ignore the form but continue processing. (XEP-0115 5.4)
109 continue;
110 }
111 sb.append(sanitize(data_form.form_type))
112 .append("<");
113
114 data_form.fields.sort(compare_data_fields);
115 foreach (DataForms.DataForm.Field field in data_form.fields) {
116 sb.append(sanitize(field.var))
117 .append("<");
118 Gee.List<string> values = field.get_values();
119 values.sort();
120 foreach (string value in values) {
121 sb.append(sanitize(value ?? ""))
122 .append("<");
123 }
124 }
125 }
126
127 Checksum c = new Checksum(ChecksumType.SHA1);
128 c.update(sb.str.data, -1);
129 size_t size = 20;
130 uint8[] buf = new uint8[size];
131 c.get_digest(buf, ref size);
132
133 return Base64.encode(buf);
134 }
135
136 /*
137 * If the four characters '&', 'l', 't', ';' appear consecutively in any of the factors of the verification
138 * string S [...] then that string of characters MUST be treated as literally '&lt;' and MUST NOT be converted to
139 * the character '<', because completing such a conversion would open the protocol to trivial attacks. (XEP-0115 5.1)
140 */
141 private static string sanitize(string s) {
142 return s.replace("<", "&lt;");
143 }
144
145 private static int compare_identities(ServiceDiscovery.Identity a, ServiceDiscovery.Identity b) {
146 int category_comp = a.category.collate(b.category);
147 if (category_comp != 0) return category_comp;
148 int type_comp = a.type_.collate(b.type_);
149 if (type_comp != 0) return type_comp;
150 // TODO lang
151 return 0;
152 }
153
154 private static int compare_data_forms(DataForms.DataForm a, DataForms.DataForm b) {
155 if (a.form_type != null && b.form_type != null) {
156 return a.form_type.collate(b.form_type);
157 }
158 return 0;
159 }
160
161 private static int compare_data_fields(DataForms.DataForm.Field a, DataForms.DataForm.Field b) {
162 if (a.var != null && b.var != null) {
163 return a.var.collate(b.var);
164 }
165 return 0;
166 }
167 }
168
169 public interface Storage : Object {
170 public abstract void store_identities(string entity, Gee.Set<ServiceDiscovery.Identity> identities);
171 public abstract void store_features(string entity, Gee.List<string> capabilities);
172 public abstract ServiceDiscovery.Identity? get_identities(string entity);
173 public abstract Gee.List<string> get_features(string entity);
174 }
175 }