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_bundles_query(xmppc_t *xmppc, const char* deviceid);
56static int _omemo_bundles_reply(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
57 void *const userdata);
58
59void omemo_execute_command(xmppc_t *xmppc, int agrc, char *argv[]) {
60 _omemo_device_list_query(xmppc);
61}
62
63void _omemo_device_list_query(xmppc_t *xmppc) {
64 xmpp_conn_t *conn = xmppc->conn;
65 xmpp_stanza_t *iq, *query, *item;
66 char* id = xmpp_uuid_gen(xmppc->ctx);
67 iq = xmpp_iq_new(xmpp_conn_get_context(conn), "get", id);
68 const char *jid = xmpp_conn_get_jid(conn);
69 xmpp_stanza_set_from(iq, jid);
70 xmpp_stanza_set_to(iq, jid);
71 query = xmpp_stanza_new(xmpp_conn_get_context(conn));
72 xmpp_stanza_set_name(query, "pubsub");
73 xmpp_stanza_set_ns(query, "http://jabber.org/protocol/pubsub");
74 xmpp_stanza_add_child(iq, query);
75 item = xmpp_stanza_new(xmpp_conn_get_context(conn));
76 xmpp_stanza_set_name(item, "items");
77 xmpp_stanza_set_attribute(item, "node",
78 "eu.siacs.conversations.axolotl.devicelist");
79 xmpp_stanza_add_child(query, item);
80 xmpp_stanza_release(query);
81 xmpp_stanza_release(item);
82 xmpp_id_handler_add(conn, _omemo_device_list_reply , id, xmppc);
83 xmpp_send(conn, iq);
84}
85
86static int _omemo_device_list_reply(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
87 void *const userdata) {
88 xmppc_t *xmppc = (xmppc_t *)userdata;
89 xmpp_stanza_t *query, *item;
90 const char *name;
91 query = xmpp_stanza_get_child_by_name(stanza, "pubsub");
92 query = xmpp_stanza_get_child_by_name(query, "items");
93 query = xmpp_stanza_get_child_by_name(query, "item");
94 query = xmpp_stanza_get_child_by_name(query, "list");
95 for (item = xmpp_stanza_get_children(query); item;
96 item = xmpp_stanza_get_next(item))
97 if ((name = xmpp_stanza_get_attribute(item, "id"))) {
98 response++;
99 logInfo(xmppc,"\t %s\n", name);
100 _omemo_bundles_query(xmppc, name);
101 }
102
103 return 0;
104}
105
106void _omemo_bundles_query(xmppc_t *xmppc, const char* deviceid){
107 xmpp_conn_t *conn = xmppc->conn;
108 xmpp_stanza_t *iq, *query, *item;
109// char* id = xmpp_uuid_gen(xmppc->ctx);
110 iq = xmpp_iq_new(xmpp_conn_get_context(conn), "get", deviceid);
111 const char *jid = xmpp_conn_get_jid(conn);
112 xmpp_stanza_set_from(iq, jid);
113 xmpp_stanza_set_to(iq, jid);
114 query = xmpp_stanza_new(xmpp_conn_get_context(conn));
115 xmpp_stanza_set_name(query, "pubsub");
116 xmpp_stanza_set_ns(query, "http://jabber.org/protocol/pubsub");
117 xmpp_stanza_add_child(iq, query);
118 item = xmpp_stanza_new(xmpp_conn_get_context(conn));
119 xmpp_stanza_set_name(item, "items");
120 char bundle[100] = "";
121 strcat(bundle, "eu.siacs.conversations.axolotl.bundles:");
122 strcat(bundle, deviceid);
123 xmpp_stanza_set_attribute(item, "node",bundle);
124 xmpp_stanza_add_child(query, item);
125 xmpp_stanza_release(query);
126 xmpp_stanza_release(item);
127 xmpp_id_handler_add(conn, _omemo_bundles_reply , deviceid, xmppc);
128 xmpp_send(conn, iq);
129
130}
131
132int _omemo_bundles_reply(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
133 void *const userdata) {
134 xmppc_t *xmppc = (xmppc_t *)userdata;
135
136 if(strcmp(xmpp_stanza_get_type(stanza), "error") == 0) {
137 printf("Fehler\n");
138 return 0;
139 }
140
141 xmpp_stanza_t *query;
142 query = xmpp_stanza_get_child_by_name(stanza, "pubsub");
143 query = xmpp_stanza_get_child_by_name(query, "items");
144 query = xmpp_stanza_get_child_by_name(query, "item");
145 query = xmpp_stanza_get_child_by_name(query, "bundle");
146 query = xmpp_stanza_get_child_by_name(query, "identityKey");
147 char* identityKey = xmpp_stanza_get_text(query);
148
149 size_t identity_public_key_len = 0;
150 unsigned char *x = g_base64_decode(identityKey, &identity_public_key_len);
151 unsigned char *identity_public_key_data = x;
152 if (identity_public_key_data[0] != DJB_TYPE) {
153 printf("Ist kein DJB_TYPE");
154 }
155
156 // Skip first byte corresponding to signal DJB_TYPE
157 identity_public_key_len--;
158 identity_public_key_data = &identity_public_key_data[1];
159
160 char *fingerprint = malloc(identity_public_key_len * 2 + 1);
161 fingerprint[identity_public_key_len*2] = '\0';
162
163 for (int i = 0; i < identity_public_key_len; i++) {
164 fingerprint[i * 2] = (identity_public_key_data[i] & 0xf0) >> 4;
165 fingerprint[i * 2] += '0';
166 if (fingerprint[i * 2] > '9') {
167 fingerprint[i * 2] += 0x27;
168 }
169
170 fingerprint[(i * 2) + 1] = identity_public_key_data[i] & 0x0f;
171 fingerprint[(i * 2) + 1] += '0';
172 if (fingerprint[(i * 2) + 1] > '9') {
173 fingerprint[(i * 2) + 1] += 0x27;
174 }
175 }
176
177 printf("xmpp:%s?omemo-sid-%s=%s\n", xmpp_conn_get_jid(conn), xmpp_stanza_get_id(stanza), fingerprint);
178 response--;
179 if(response == 0) {
180 xmpp_disconnect(xmppc->conn);
181 xmpp_stop(xmppc->ctx);
182 }
183
184 return 0;
185}
186
187