omemo.c

  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  printf("Device List\n");
 92  query = xmpp_stanza_get_child_by_name(stanza, "pubsub");
 93  query = xmpp_stanza_get_child_by_name(query, "items");
 94  query = xmpp_stanza_get_child_by_name(query, "item");
 95  query = xmpp_stanza_get_child_by_name(query, "list");
 96  for (item = xmpp_stanza_get_children(query); item;
 97       item = xmpp_stanza_get_next(item))
 98    if ((name = xmpp_stanza_get_attribute(item, "id"))) {
 99      response++;
100      printf("\t %s\n", name);
101      _omemo_bundles_query(xmppc, name);
102    }
103    
104  return 0;
105}
106
107void _omemo_bundles_query(xmppc_t *xmppc, const char* deviceid){
108  xmpp_conn_t *conn = xmppc->conn;
109  xmpp_stanza_t *iq, *query, *item;
110  char* id = xmpp_uuid_gen(xmppc->ctx);
111  iq = xmpp_iq_new(xmpp_conn_get_context(conn), "get", id);
112  const char *jid = xmpp_conn_get_jid(conn);
113  xmpp_stanza_set_from(iq, jid);
114  xmpp_stanza_set_to(iq, jid);
115  query = xmpp_stanza_new(xmpp_conn_get_context(conn));
116  xmpp_stanza_set_name(query, "pubsub");
117  xmpp_stanza_set_ns(query, "http://jabber.org/protocol/pubsub");
118  xmpp_stanza_add_child(iq, query);
119  item = xmpp_stanza_new(xmpp_conn_get_context(conn));
120  xmpp_stanza_set_name(item, "items");
121  char bundle[100] = "";
122  strcat(bundle, "eu.siacs.conversations.axolotl.bundles:");
123  strcat(bundle, deviceid);
124  xmpp_stanza_set_attribute(item, "node",bundle);
125  xmpp_stanza_add_child(query, item);
126  xmpp_stanza_release(query);
127  xmpp_stanza_release(item);
128  xmpp_id_handler_add(conn, _omemo_bundles_reply , id, xmppc);
129  xmpp_send(conn, iq);
130
131}
132
133int _omemo_bundles_reply(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
134                  void *const userdata)  {
135  xmppc_t *xmppc = (xmppc_t *)userdata;
136
137  if(strcmp(xmpp_stanza_get_type(stanza), "error") == 0) {
138    printf("Fehler\n");
139    return 0;
140  } 
141
142  xmpp_stanza_t *query;
143  query = xmpp_stanza_get_child_by_name(stanza, "pubsub");
144  query = xmpp_stanza_get_child_by_name(query, "items");
145  query = xmpp_stanza_get_child_by_name(query, "item");
146  query = xmpp_stanza_get_child_by_name(query, "bundle");
147  query = xmpp_stanza_get_child_by_name(query, "identityKey");
148  char* identityKey = xmpp_stanza_get_text(query);
149
150 size_t identity_public_key_len = 0;
151  unsigned char *x = g_base64_decode(identityKey, &identity_public_key_len);
152  unsigned char *identity_public_key_data = x;
153  if (identity_public_key_data[0] != DJB_TYPE) {
154    printf("Ist kein DJB_TYPE");
155  }
156
157  // Skip first byte corresponding to signal DJB_TYPE
158  identity_public_key_len--;
159  identity_public_key_data = &identity_public_key_data[1];
160
161  char *fingerprint = malloc(identity_public_key_len * 2 + 1);
162  fingerprint[identity_public_key_len*2] = '\0';
163
164  for (int i = 0; i < identity_public_key_len; i++) {
165    fingerprint[i * 2] = (identity_public_key_data[i] & 0xf0) >> 4;
166    fingerprint[i * 2] += '0';
167    if (fingerprint[i * 2] > '9') {
168      fingerprint[i * 2] += 0x27;
169    }
170
171    fingerprint[(i * 2) + 1] = identity_public_key_data[i] & 0x0f;
172    fingerprint[(i * 2) + 1] += '0';
173    if (fingerprint[(i * 2) + 1] > '9') {
174      fingerprint[(i * 2) + 1] += 0x27;
175    }
176  }
177  printf("Fingerprint: %s\n", 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