1/*!
2 * @file omemo.c
3 *
4 * vim: expandtab:ts=2:sts=2:sw=2
5 *
6 * @copyright
7 *
8 * Copyright (C) 2020 Anoxinon e.V.
9 *
10 * This file is part of xmppc.
11 *
12 * xmppc is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * xmppc is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * German
26 *
27 * Diese Datei ist Teil von xmppc.
28 *
29 * xmppc ist Freie Software: Sie können es unter den Bedingungen
30 * der GNU General Public License, wie von der Free Software Foundation,
31 * Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
32 * veröffentlichten Version, weiter verteilen und/oder modifizieren.
33 *
34 * xmppc wird in der Hoffnung, dass es nützlich sein wird, aber
35 * OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
36 * Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
37 * Siehe die GNU General Public License für weitere Details.
38 *
39 * Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
40 * Programm erhalten haben. Wenn nicht, siehe <https://www.gnu.org/licenses/>.
41 */
42
43#include "omemo.h"
44
45#include <glib.h>
46#include <unistd.h>
47#define DJB_TYPE 0x05
48
49static int response = 0;
50
51static void _omemo_device_list_query(xmppc_t *xmppc);
52static int _omemo_device_list_reply(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
53 void *const userdata);
54
55static void _omemo_delete_device_list_query(xmppc_t *xmppc);
56static int _omemo_delete_device_list_reply(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
57 void *const userdata);
58
59static void _omemo_bundles_query(xmppc_t *xmppc, const char* deviceid);
60static int _omemo_bundles_reply(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
61 void *const userdata);
62
63void omemo_execute_command(xmppc_t *xmppc, int argc, char *argv[]) {
64 if(argc > 0) {
65 if(strcmp("list", argv[0]) == 0) {
66 _omemo_device_list_query(xmppc);
67 } else if(strcmp("delete-device-list", argv[0]) == 0) {
68 _omemo_delete_device_list_query(xmppc);
69 } else {
70 logError(xmppc, "Unbekanner Befehl: %s\n", argv[0]);
71 }
72 }
73}
74
75void _omemo_device_list_query(xmppc_t *xmppc) {
76 xmpp_conn_t *conn = xmppc->conn;
77 xmpp_stanza_t *iq, *query, *item;
78 char* id = xmpp_uuid_gen(xmppc->ctx);
79 iq = xmpp_iq_new(xmpp_conn_get_context(conn), "get", id);
80 const char *jid = xmpp_conn_get_jid(conn);
81 xmpp_stanza_set_from(iq, jid);
82 xmpp_stanza_set_to(iq, jid);
83 query = xmpp_stanza_new(xmpp_conn_get_context(conn));
84 xmpp_stanza_set_name(query, "pubsub");
85 xmpp_stanza_set_ns(query, "http://jabber.org/protocol/pubsub");
86 xmpp_stanza_add_child(iq, query);
87 item = xmpp_stanza_new(xmpp_conn_get_context(conn));
88 xmpp_stanza_set_name(item, "items");
89 xmpp_stanza_set_attribute(item, "node",
90 "eu.siacs.conversations.axolotl.devicelist");
91 xmpp_stanza_add_child(query, item);
92 xmpp_stanza_release(query);
93 xmpp_stanza_release(item);
94 xmpp_id_handler_add(conn, _omemo_device_list_reply , id, xmppc);
95 xmpp_send(conn, iq);
96}
97
98static int _omemo_device_list_reply(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
99 void *const userdata) {
100 xmppc_t *xmppc = (xmppc_t *)userdata;
101 xmpp_stanza_t *query, *item;
102 const char *name;
103 query = xmpp_stanza_get_child_by_name(stanza, "pubsub");
104 query = xmpp_stanza_get_child_by_name(query, "items");
105 query = xmpp_stanza_get_child_by_name(query, "item");
106 query = xmpp_stanza_get_child_by_name(query, "list");
107 for (item = xmpp_stanza_get_children(query); item;
108 item = xmpp_stanza_get_next(item))
109 if ((name = xmpp_stanza_get_attribute(item, "id"))) {
110 response++;
111 logInfo(xmppc,"\t %s\n", name);
112 _omemo_bundles_query(xmppc, name);
113 }
114
115 return 0;
116}
117
118void _omemo_bundles_query(xmppc_t *xmppc, const char* deviceid){
119 xmpp_conn_t *conn = xmppc->conn;
120 xmpp_stanza_t *iq, *query, *item;
121// char* id = xmpp_uuid_gen(xmppc->ctx);
122 iq = xmpp_iq_new(xmpp_conn_get_context(conn), "get", deviceid);
123 const char *jid = xmpp_conn_get_jid(conn);
124 xmpp_stanza_set_from(iq, jid);
125 xmpp_stanza_set_to(iq, jid);
126 query = xmpp_stanza_new(xmpp_conn_get_context(conn));
127 xmpp_stanza_set_name(query, "pubsub");
128 xmpp_stanza_set_ns(query, "http://jabber.org/protocol/pubsub");
129 xmpp_stanza_add_child(iq, query);
130 item = xmpp_stanza_new(xmpp_conn_get_context(conn));
131 xmpp_stanza_set_name(item, "items");
132 char bundle[100] = "";
133 strcat(bundle, "eu.siacs.conversations.axolotl.bundles:");
134 strcat(bundle, deviceid);
135 xmpp_stanza_set_attribute(item, "node",bundle);
136 xmpp_stanza_add_child(query, item);
137 xmpp_stanza_release(query);
138 xmpp_stanza_release(item);
139 xmpp_id_handler_add(conn, _omemo_bundles_reply , deviceid, xmppc);
140 xmpp_send(conn, iq);
141
142}
143
144int _omemo_bundles_reply(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
145 void *const userdata) {
146 xmppc_t *xmppc = (xmppc_t *)userdata;
147
148 if(strcmp(xmpp_stanza_get_type(stanza), "error") == 0) {
149 printf("Fehler\n");
150 return 0;
151 }
152
153 xmpp_stanza_t *query;
154 query = xmpp_stanza_get_child_by_name(stanza, "pubsub");
155 query = xmpp_stanza_get_child_by_name(query, "items");
156 query = xmpp_stanza_get_child_by_name(query, "item");
157 query = xmpp_stanza_get_child_by_name(query, "bundle");
158 query = xmpp_stanza_get_child_by_name(query, "identityKey");
159 char* identityKey = xmpp_stanza_get_text(query);
160
161 size_t identity_public_key_len = 0;
162 unsigned char *x = g_base64_decode(identityKey, &identity_public_key_len);
163 unsigned char *identity_public_key_data = x;
164 if (identity_public_key_data[0] != DJB_TYPE) {
165 printf("Ist kein DJB_TYPE");
166 }
167
168 // Skip first byte corresponding to signal DJB_TYPE
169 identity_public_key_len--;
170 identity_public_key_data = &identity_public_key_data[1];
171
172 char *fingerprint = malloc(identity_public_key_len * 2 + 1);
173 fingerprint[identity_public_key_len*2] = '\0';
174
175 for (int i = 0; i < identity_public_key_len; i++) {
176 fingerprint[i * 2] = (identity_public_key_data[i] & 0xf0) >> 4;
177 fingerprint[i * 2] += '0';
178 if (fingerprint[i * 2] > '9') {
179 fingerprint[i * 2] += 0x27;
180 }
181
182 fingerprint[(i * 2) + 1] = identity_public_key_data[i] & 0x0f;
183 fingerprint[(i * 2) + 1] += '0';
184 if (fingerprint[(i * 2) + 1] > '9') {
185 fingerprint[(i * 2) + 1] += 0x27;
186 }
187 }
188
189 printf("xmpp:%s?omemo-sid-%s=%s\n", xmpp_conn_get_jid(conn), xmpp_stanza_get_id(stanza), fingerprint);
190 response--;
191 if(response == 0) {
192 xmpp_disconnect(xmppc->conn);
193 xmpp_stop(xmppc->ctx);
194 }
195
196 return 0;
197}
198
199/**
200 <iq type='set'
201 from='hamlet@denmark.lit/elsinore'
202 to='pubsub.shakespeare.lit'
203 id='delete1'>
204 <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'>
205 <delete node='princely_musings'/>
206 </pubsub>
207</iq>
208**/
209static void _omemo_delete_device_list_query(xmppc_t *xmppc) {
210 xmpp_conn_t *conn = xmppc->conn;
211 xmpp_stanza_t *iq, *query, *item;
212 char* id = xmpp_uuid_gen(xmppc->ctx);
213 iq = xmpp_iq_new(xmpp_conn_get_context(conn), "set", id);
214 const char *jid = xmpp_conn_get_jid(conn);
215 xmpp_stanza_set_from(iq, jid);
216 xmpp_stanza_set_to(iq, jid);
217 query = xmpp_stanza_new(xmpp_conn_get_context(conn));
218 xmpp_stanza_set_name(query, "pubsub");
219 xmpp_stanza_set_ns(query, "http://jabber.org/protocol/pubsub#owner");
220 xmpp_stanza_add_child(iq, query);
221 item = xmpp_stanza_new(xmpp_conn_get_context(conn));
222 xmpp_stanza_set_name(item, "delete");
223 xmpp_stanza_set_attribute(item, "node",
224 "eu.siacs.conversations.axolotl.devicelist");
225 xmpp_stanza_add_child(query, item);
226 xmpp_stanza_release(query);
227 xmpp_stanza_release(item);
228 xmpp_id_handler_add(conn, _omemo_delete_device_list_reply , id, xmppc);
229 xmpp_send(conn, iq);
230}
231
232int _omemo_delete_device_list_reply(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
233 void *const userdata) {
234 xmppc_t *xmppc = (xmppc_t *)userdata;
235
236 if(strcmp(xmpp_stanza_get_type(stanza), "error") == 0) {
237 printf("Fehler\n");
238 return 0;
239 }
240 printf("Done\n");
241 xmpp_disconnect(xmppc->conn);
242 xmpp_stop(xmppc->ctx);
243 return 0;
244}