chatty-ma-buddy.c 10.1 KB
Newer Older
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
/* chatty-ma-buddy.c
 *
 * Copyright 2020 Purism SPC
 *
 * Author(s):
 *   Mohammed Sadiq <sadiq@sadiqpk.org>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#define G_LOG_DOMAIN "chatty-ma-buddy"

#define _GNU_SOURCE
#include <string.h>
#include <glib/gi18n.h>

#include "matrix-utils.h"
#include "chatty-ma-buddy.h"

struct _ChattyMaBuddy
{
  ChattyItem      parent_instance;

  char           *matrix_id;
  char           *name;
  GList          *devices;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
29 30
  MatrixApi      *matrix_api;
  MatrixEnc      *matrix_enc;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
31

Mohammed Sadiq's avatar
Mohammed Sadiq committed
32 33
  /* generated using g_str_hash for faster comparison */
  guint           id_hash;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  gboolean        is_self;
};

struct _BuddyDevice
{

  char *device_id;
  char *device_name;
  char *curve_key; /* Public part Curve25519 identity key pair */
  char *ed_key;    /* Public part of Ed25519 fingerprint key pair */
  char *one_time_key;

  gboolean meagolm_v1;
  gboolean olm_v1;
};


G_DEFINE_TYPE (ChattyMaBuddy, chatty_ma_buddy, CHATTY_TYPE_ITEM)

enum {
  CHANGED,
  N_SIGNALS
};

static guint signals[N_SIGNALS];

static ChattyProtocol
chatty_ma_buddy_get_protocols (ChattyItem *item)
{
  return CHATTY_PROTOCOL_MATRIX;
}

static gboolean
chatty_ma_buddy_matches (ChattyItem     *item,
                         const char     *needle,
                         ChattyProtocol  protocols,
                         gboolean        match_name)
{
  ChattyMaBuddy *self = (ChattyMaBuddy *)item;

  if (needle == self->matrix_id)
    return TRUE;

  if (!needle || !self->matrix_id)
    return FALSE;

  return strcasestr (needle, self->matrix_id) != NULL;
}

static const char *
chatty_ma_buddy_get_name (ChattyItem *item)
{
  ChattyMaBuddy *self = (ChattyMaBuddy *)item;

  g_assert (CHATTY_IS_MA_BUDDY (self));

  if (self->name)
    return self->name;

  if (self->matrix_id)
    return self->matrix_id;

  return "";
}

static void
chatty_ma_buddy_set_name (ChattyItem *item,
                          const char *name)
{
  ChattyMaBuddy *self = (ChattyMaBuddy *)item;

  g_assert (CHATTY_IS_MA_BUDDY (self));

  g_free (self->name);

  if (!name || !*name)
    self->name = NULL;
  else
    self->name = g_strdup (name);
}

static GdkPixbuf *
chatty_ma_buddy_get_avatar (ChattyItem *item)
{
  ChattyMaBuddy *self = (ChattyMaBuddy *)item;

  g_assert (CHATTY_IS_MA_BUDDY (self));

  return NULL;
}

static void
chatty_ma_buddy_dispose (GObject *object)
{
  ChattyMaBuddy *self = (ChattyMaBuddy *)object;

  g_clear_pointer (&self->matrix_id, g_free);
  g_clear_pointer (&self->name, g_free);

  G_OBJECT_CLASS (chatty_ma_buddy_parent_class)->dispose (object);
}

static void
chatty_ma_buddy_class_init (ChattyMaBuddyClass *klass)
{
  GObjectClass *object_class  = G_OBJECT_CLASS (klass);
  ChattyItemClass *item_class = CHATTY_ITEM_CLASS (klass);

  object_class->dispose = chatty_ma_buddy_dispose;

  item_class->get_protocols = chatty_ma_buddy_get_protocols;
  item_class->matches  = chatty_ma_buddy_matches;
  item_class->get_name = chatty_ma_buddy_get_name;
  item_class->set_name = chatty_ma_buddy_set_name;
  item_class->get_avatar = chatty_ma_buddy_get_avatar;

  /**
   * ChattyMaBuddy::changed:
   * @self: a #ChattyMaBuddy
   *
   * changed signal is emitted when any detail
   * of the buddy changes.
   */
  signals [CHANGED] =
    g_signal_new ("changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  0, NULL, NULL, NULL,
                  G_TYPE_NONE, 0);
}

static void
chatty_ma_buddy_init (ChattyMaBuddy *self)
{
}

ChattyMaBuddy *
chatty_ma_buddy_new (const char *matrix_id,
Mohammed Sadiq's avatar
Mohammed Sadiq committed
172 173
                     MatrixApi  *api,
                     MatrixEnc  *enc)
Mohammed Sadiq's avatar
Mohammed Sadiq committed
174 175 176 177
{
  ChattyMaBuddy *self;

  g_return_val_if_fail (matrix_id && *matrix_id == '@', NULL);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
178 179
  g_return_val_if_fail (MATRIX_IS_API (api), NULL);
  g_return_val_if_fail (MATRIX_IS_ENC (enc), NULL);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
180 181 182

  self = g_object_new (CHATTY_TYPE_MA_BUDDY, NULL);
  self->matrix_id = g_strdup (matrix_id);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
183 184 185 186 187
  self->matrix_api = g_object_ref (api);
  self->matrix_enc = g_object_ref (enc);

  if (g_str_equal (matrix_id, matrix_api_get_username (api)))
    self->is_self = TRUE;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

  return self;
}

/**
 * chatty_ma_buddy_get_id:
 * @self: a #ChattyMaBuddy
 *
 * Get the user id of @self. The id is usually a
 * fully qualified Matrix ID (@user:example.com),
 * but it can also be the username alone (user).
 *
 * Returns: (transfer none): the id of Buddy.
 * or an empty string if not found or on error.
 */
const char *
chatty_ma_buddy_get_id (ChattyMaBuddy *self)
{
  g_return_val_if_fail (CHATTY_IS_MA_BUDDY (self), "");

  if (self->matrix_id)
    return self->matrix_id;

  return "";
}

Mohammed Sadiq's avatar
Mohammed Sadiq committed
214 215 216 217 218 219 220 221 222 223 224
guint
chatty_ma_buddy_get_id_hash (ChattyMaBuddy *self)
{
  g_return_val_if_fail (CHATTY_IS_MA_BUDDY (self), 0);

  if (!self->id_hash && self->matrix_id)
    self->id_hash = g_str_hash (self->matrix_id);

  return self->id_hash;
}

Mohammed Sadiq's avatar
Mohammed Sadiq committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
void
chatty_ma_buddy_add_devices (ChattyMaBuddy *self,
                             JsonObject    *root)
{
  g_autoptr(GList) members = NULL;
  JsonObject *object, *child;
  BuddyDevice *device;

  g_return_if_fail (CHATTY_IS_MA_BUDDY (self));
  g_return_if_fail (root);

  members = json_object_get_members (root);

  for (GList *member = members; member; member = member->next) {
    g_autofree char *device_name = NULL;
    const char *device_id, *user, *key;
    JsonArray *array;
    char *key_name;

    child = matrix_utils_json_object_get_object (root, member->data);
    device_id = matrix_utils_json_object_get_string (child, "device_id");
    user = matrix_utils_json_object_get_string (child, "user_id");

    if (g_strcmp0 (user, self->matrix_id) != 0) {
      g_warning ("‘%s’ and ‘%s’ are not the same users", user, self->matrix_id);
      continue;
    }

Mohammed Sadiq's avatar
Mohammed Sadiq committed
253 254 255 256
    if (self->is_self &&
        g_strcmp0 (device_id, matrix_api_get_device_id (self->matrix_api)) == 0)
      continue;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
257 258 259 260 261 262 263 264 265 266 267 268 269
    if (g_strcmp0 (member->data, device_id) != 0) {
      g_warning ("‘%s’ and ‘%s’ are not the same device", (char *)member->data, device_id);
      continue;
    }

    object = matrix_utils_json_object_get_object (child, "unsigned");
    device_name = g_strdup (matrix_utils_json_object_get_string (object, "device_display_name"));

    key_name = g_strconcat ("ed25519:", device_id, NULL);
    object = matrix_utils_json_object_get_object (child, "keys");
    key = matrix_utils_json_object_get_string (object, key_name);
    g_free (key_name);

Mohammed Sadiq's avatar
Mohammed Sadiq committed
270 271 272 273 274
    if (!matrix_enc_verify (self->matrix_enc, child, self->matrix_id, device_id, key)) {
      g_warning ("failed to verify signature for %s with device %s", self->matrix_id, device_id);
      continue;
    }

Mohammed Sadiq's avatar
Mohammed Sadiq committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    device = g_new0 (BuddyDevice, 1);
    device->device_id = g_strdup (device_id);
    device->device_name = g_steal_pointer (&device_name);
    device->ed_key = g_strdup (key);

    key_name = g_strconcat ("curve25519:", device_id, NULL);
    object = matrix_utils_json_object_get_object (child, "keys");
    key = matrix_utils_json_object_get_string (object, key_name);
    device->curve_key = g_strdup (key);
    g_free (key_name);

    array = matrix_utils_json_object_get_array (child, "algorithms");
    for (guint i = 0; array && i < json_array_get_length (array); i++) {
      const char *algorithm;

      algorithm = json_array_get_string_element (array, i);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
291
      if (g_strcmp0 (algorithm, ALGORITHM_MEGOLM) == 0)
Mohammed Sadiq's avatar
Mohammed Sadiq committed
292
        device->meagolm_v1 = TRUE;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
293
      else if (g_strcmp0 (algorithm, ALGORITHM_OLM) == 0)
Mohammed Sadiq's avatar
Mohammed Sadiq committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
        device->olm_v1 = TRUE;
    }

    self->devices = g_list_prepend (self->devices, device);
  }
}

GList *
chatty_ma_buddy_get_devices (ChattyMaBuddy *self)
{
  g_return_val_if_fail (CHATTY_IS_MA_BUDDY (self), NULL);

  return g_list_copy (self->devices);
}

/**
 * chatty_ma_buddy_device_key_json:
 * @self: A #ChattyMaBuddy
 *
 * Get A JSON object with all the devices
 * that we don't have an one time key for.
 *
 * The JSON created will have the following format:
 *
 *  {
 *    "@alice:example.com": {
 *      "JLAFKJWSCS": "signed_curve25519"
 *     },
 *    "@bob:example.com": {
 *      "JOJOAEWBZY": "signed_curve25519"
 *  }
 *
 * Returns: (transfer full): A #JsonObject
 */
JsonObject *
chatty_ma_buddy_device_key_json (ChattyMaBuddy *self)
{
  JsonObject *object;

  g_return_val_if_fail (CHATTY_IS_MA_BUDDY (self), NULL);

  if (!self->devices)
    return NULL;

  object = json_object_new ();

  for (GList *node = self->devices; node; node = node->next) {
    BuddyDevice *device = node->data;

    if (!device->one_time_key)
      json_object_set_string_member (object, device->device_id, "signed_curve25519");
  }

  return object;
}

void
chatty_ma_buddy_add_one_time_keys (ChattyMaBuddy *self,
                                   JsonObject    *root)
{
  JsonObject *object, *child;

  g_return_if_fail (CHATTY_IS_MA_BUDDY (self));
  g_return_if_fail (root);

  for (GList *item = self->devices; item; item = item->next) {
    g_autoptr(GList) members = NULL;
    BuddyDevice *device = item->data;

    child = matrix_utils_json_object_get_object (root, device->device_id);

    if (!child) {
      g_warning ("device '%s' not found", device->device_id);
      continue;
    }

    members = json_object_get_members (child);

    for (GList *node = members; node; node = node->next) {
      object = matrix_utils_json_object_get_object (child, node->data);

Mohammed Sadiq's avatar
Mohammed Sadiq committed
375 376
      if (matrix_enc_verify (self->matrix_enc, object, self->matrix_id,
                             device->device_id, device->ed_key)) {
Mohammed Sadiq's avatar
Mohammed Sadiq committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
        const char *key;

        key = matrix_utils_json_object_get_string (object, "key");
        g_free (device->one_time_key);
        device->one_time_key = g_strdup (key);
      }
    }
  }
}

const char *
chatty_ma_device_get_id (BuddyDevice *device)
{
  g_return_val_if_fail (device, "");

  return device->device_id;
}

const char *
chatty_ma_device_get_ed_key (BuddyDevice *device)
{
  g_return_val_if_fail (device, "");

  return device->ed_key;
}

const char *
chatty_ma_device_get_curve_key (BuddyDevice *device)
{
  g_return_val_if_fail (device, "");

  return device->curve_key;
}

char *
chatty_ma_device_get_one_time_key (BuddyDevice *device)
{
  g_return_val_if_fail (device, g_strdup (""));

  return g_steal_pointer (&device->one_time_key);
}