libomemo.c 40.9 KB
Newer Older
Richard Bayerle's avatar
Richard Bayerle committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <inttypes.h>
#include <stdarg.h> // vsnprintf
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <glib.h> // b64, glist

#include <mxml.h>

#include "libomemo.h"

#define HINTS_XMLNS "urn:xmpp:hints"

#ifndef OMEMO_XMLNS
#define OMEMO_XMLNS "urn:xmpp:omemo"
#endif

Richard Bayerle's avatar
Richard Bayerle committed
19
20
21
22
23
24
#ifndef OMEMO_NS_SEPARATOR
#define OMEMO_NS_SEPARATOR ":"
#endif

#define OMEMO_NS_SEPARATOR_FINAL ":"

Richard Bayerle's avatar
Richard Bayerle committed
25
#define OMEMO_VERSION "0"
Richard Bayerle's avatar
Richard Bayerle committed
26
27
28
29

#ifdef OMEMO_NS_NOVERSION
#define OMEMO_NS OMEMO_XMLNS
#else
Richard Bayerle's avatar
Richard Bayerle committed
30
#define OMEMO_NS OMEMO_XMLNS ":" OMEMO_VERSION
Richard Bayerle's avatar
Richard Bayerle committed
31
#endif
Richard Bayerle's avatar
Richard Bayerle committed
32
33
34
35
36

#define PEP_NODE_NAME "node"
#define DEVICELIST_PEP_NAME "devicelist"
#define BUNDLE_PEP_NAME "bundles"

Richard Bayerle's avatar
Richard Bayerle committed
37
#define OMEMO_DEVICELIST_PEP_NODE OMEMO_NS OMEMO_NS_SEPARATOR DEVICELIST_PEP_NAME
Richard Bayerle's avatar
Richard Bayerle committed
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

#define MESSAGE_NODE_NAME "message"
#define BODY_NODE_NAME "body"
#define ENCRYPTED_NODE_NAME "encrypted"
#define HEADER_NODE_NAME "header"
#define IV_NODE_NAME "iv"
#define KEY_NODE_NAME "key"
#define PAYLOAD_NODE_NAME "payload"
#define STORE_NODE_NAME "store"

#define PUBLISH_NODE_NAME "publish"
#define ITEMS_NODE_NAME "items"
#define ITEM_NODE_NAME "item"
#define LIST_NODE_NAME "list"
#define BUNDLE_NODE_NAME "bundle"
#define SIGNED_PRE_KEY_NODE_NAME "signedPreKeyPublic"
#define SIGNATURE_NODE_NAME "signedPreKeySignature"
#define PREKEYS_NODE_NAME "prekeys"
#define PRE_KEY_NODE_NAME "preKeyPublic"
#define IDENTITY_KEY_NODE_NAME "identityKey"
#define DEVICE_NODE_NAME "device"

#define XMLNS_ATTR_NAME "xmlns"
#define MESSAGE_NODE_FROM_ATTR_NAME "from"
#define MESSAGE_NODE_TO_ATTR_NAME "to"
#define HEADER_NODE_SID_ATTR_NAME "sid"
#define KEY_NODE_RID_ATTR_NAME "rid"
#define PUBLISH_NODE_NODE_ATTR_NAME "node"
#define SIGNED_PRE_KEY_NODE_ID_ATTR_NAME "signedPreKeyId"
#define PRE_KEY_NODE_ID_ATTR_NAME "preKeyId"
#define DEVICE_NODE_ID_ATTR_NAME "id"

#define OMEMO_DB_DEFAULT_FN "omemo.sqlite"

72
#define NO_OMEMO_MSG "You received an OMEMO encrypted message, but your client does not support it."
73
74
75
76
77

#define EME_NODE_NAME "encryption"
#define EME_XMLNS "urn:xmpp:eme:0"
#define EME_NAMESPACE_ATTR_NAME "namespace"
#define EME_NAME_ATTR_NAME "name"
78

Richard Bayerle's avatar
Richard Bayerle committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
struct omemo_bundle {
  char * device_id;
  mxml_node_t * signed_pk_node_p;
  mxml_node_t * signature_node_p;
  mxml_node_t * identity_key_node_p;
  mxml_node_t * pre_keys_node_p;
  size_t pre_keys_amount;
};

struct omemo_devicelist {
  char * from;
  GList * id_list_p;
  mxml_node_t * list_node_p;
};

struct omemo_message {
  mxml_node_t * message_node_p;
  mxml_node_t * header_node_p;
  mxml_node_t * payload_node_p;
  uint8_t * key_p;
  size_t key_len;
  uint8_t * iv_p;
  size_t iv_len;
102
  size_t tag_len; //tag is appended to key buf, i.e. tag_p = key_p + key_len
Richard Bayerle's avatar
Richard Bayerle committed
103
104
105
106
107
108
109
110
111
112
};

/**
 * Mostly helps dealing with the device ids that come as an int and have to be a string for XML.
 *
 * @param in The int to convert.
 * @param out Will be set to the number string, needs to be free()d.
 * @return Returns the length on success, and negative on error.
 */
static int int_to_string(uint32_t in, char ** out) {
113
114
115
116
117
118
119
120
121
122
  int len;
  size_t buf_len;
  char * int_string;

  len = snprintf((void *) 0, 0, "%i", in);
  if (len < 0) {
    return -1;
  }
  buf_len = len + 1;
  int_string = malloc(buf_len);
Richard Bayerle's avatar
Richard Bayerle committed
123
124
125
126
127
  if (!int_string) {
    return OMEMO_ERR_NOMEM;
  }
  memset(int_string, 0, buf_len);

128
  int result = snprintf(int_string, buf_len, "%i", in);
Richard Bayerle's avatar
Richard Bayerle committed
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
172
173
174
175
176
177
178
179
  if (result != len) {
    free(int_string);
    return -1;
  }

  *out = int_string;
  return len;
}

/**
 * Helps basic sanity checking of received XML.
 *
 * @param node_p Pointer to the current node.
 * @param next_node_func The function that returns the next node (e.g. child or sibling)
 * @param expected_name The name of the expected node.
 * @param next_node_pp Will be set to a pointer to the next node if it is the right one.
 * @return 0 on success, negative on error (specifically, OMEMO_ERR_MALFORMED_XML)
 */
static int expect_next_node(mxml_node_t * node_p, mxml_node_t * (*next_node_func)(mxml_node_t * node_p), char * expected_name, mxml_node_t ** next_node_pp) {
  mxml_node_t * next_node_p = next_node_func(node_p);
  if (!next_node_p) {
    return OMEMO_ERR_MALFORMED_XML;
  }

  const char * element_name = mxmlGetElement(next_node_p);
  if (!element_name) {
    return OMEMO_ERR_MALFORMED_XML;
  }

  if (strncmp(mxmlGetElement(next_node_p), expected_name, strlen(expected_name))) {
    return OMEMO_ERR_MALFORMED_XML;
  }
  *next_node_pp = next_node_p;
  return 0;
}

int omemo_bundle_create(omemo_bundle ** bundle_pp) {
  omemo_bundle * bundle_p = malloc(sizeof(omemo_bundle));
  if (!bundle_p) {
    return OMEMO_ERR_NOMEM;
  }
  memset(bundle_p, 0, sizeof(omemo_bundle));

  *bundle_pp = bundle_p;

  return 0;
}

int omemo_bundle_set_device_id(omemo_bundle * bundle_p, uint32_t device_id) {
  char * id_string = (void *) 0;
  int ret = int_to_string(device_id, &id_string);
180
  if (ret <= 0) {
Richard Bayerle's avatar
Richard Bayerle committed
181
182
183
184
185
186
187
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
214
215
216
217
218
219
220
221
222
223
224
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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
375
376
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
    return ret;
  }

  bundle_p->device_id = id_string;

  return OMEMO_SUCCESS;
}

uint32_t omemo_bundle_get_device_id(omemo_bundle * bundle_p) {
  return strtol(bundle_p->device_id, (void *) 0, 0);
}

int omemo_bundle_set_signed_pre_key(omemo_bundle * bundle_p, uint32_t pre_key_id, uint8_t * data_p, size_t data_len) {
  int ret_val = 0;

  mxml_node_t * signed_pre_key_node_p = (void *) 0;
  char * pre_key_id_string = (void *) 0;
  gchar * b64_string = (void *) 0;

  signed_pre_key_node_p = mxmlNewElement(MXML_NO_PARENT, SIGNED_PRE_KEY_NODE_NAME);
  if (int_to_string(pre_key_id, &pre_key_id_string) <= 0) {
    ret_val = -1;
    goto cleanup;
  }
  mxmlElementSetAttr(signed_pre_key_node_p, SIGNED_PRE_KEY_NODE_ID_ATTR_NAME, pre_key_id_string);

  b64_string = g_base64_encode(data_p, data_len);
  (void) mxmlNewOpaque(signed_pre_key_node_p, b64_string);

  bundle_p->signed_pk_node_p = signed_pre_key_node_p;

cleanup:
  if (ret_val < 0) {
    mxmlDelete(signed_pre_key_node_p);
  }
  g_free(b64_string);
  free(pre_key_id_string);

  return ret_val;
}

int omemo_bundle_get_signed_pre_key(omemo_bundle * bundle_p, uint32_t * pre_key_id_p, uint8_t ** data_pp, size_t * data_len_p) {
  int ret_val = 0;

  const char * b64_string = (void *) 0;
  guchar * data_p = (void *) 0;
  gsize len = 0;
  const char * pre_key_id_string = (void *) 0;

  if (!bundle_p || !bundle_p->signed_pk_node_p) {
    ret_val = OMEMO_ERR_NULL;
    goto cleanup;
  }

  b64_string = mxmlGetOpaque(bundle_p->signed_pk_node_p);
  if (!b64_string) {
    ret_val = OMEMO_ERR_MALFORMED_BUNDLE;
    goto cleanup;
  }

  pre_key_id_string = mxmlElementGetAttr(bundle_p->signed_pk_node_p, SIGNED_PRE_KEY_NODE_ID_ATTR_NAME);
  if (!pre_key_id_string) {
    ret_val = OMEMO_ERR_MALFORMED_BUNDLE;
    goto cleanup;
  }

  data_p = g_base64_decode(b64_string, &len);

  *pre_key_id_p = strtol(pre_key_id_string, (void *) 0, 0);
  *data_pp = data_p;
  *data_len_p = len;

cleanup:

  return ret_val;
}

int omemo_bundle_set_signature(omemo_bundle * bundle_p, uint8_t * data_p, size_t data_len) {
  mxml_node_t * signature_node_p = mxmlNewElement(MXML_NO_PARENT, SIGNATURE_NODE_NAME);

  gchar * b64_string = g_base64_encode(data_p, data_len);
  (void) mxmlNewOpaque(signature_node_p, b64_string);

  bundle_p->signature_node_p = signature_node_p;

  g_free(b64_string);

  return 0;
}

int omemo_bundle_get_signature(omemo_bundle * bundle_p, uint8_t ** data_pp, size_t * data_len_p) {
  int ret_val = 0;

  const char * b64_string = (void *) 0;
  guchar * data_p = (void *) 0;
  gsize len = 0;

  if (!bundle_p || !bundle_p->signature_node_p) {
    ret_val = OMEMO_ERR_NULL;
    goto cleanup;
  }

  b64_string = mxmlGetOpaque(bundle_p->signature_node_p);
  if (!b64_string) {
    ret_val = OMEMO_ERR_MALFORMED_BUNDLE;
    goto cleanup;
  }

  data_p = g_base64_decode(b64_string, &len);

  *data_pp = data_p;
  *data_len_p = len;

cleanup:

  return ret_val;
}

int omemo_bundle_set_identity_key(omemo_bundle * bundle_p, uint8_t * data_p, size_t data_len) {
  mxml_node_t * identity_node_p = mxmlNewElement(MXML_NO_PARENT, IDENTITY_KEY_NODE_NAME);

  gchar * b64_string = g_base64_encode(data_p, data_len);
  (void) mxmlNewOpaque(identity_node_p, b64_string);

  bundle_p->identity_key_node_p = identity_node_p;

  g_free(b64_string);

  return 0;
}

int omemo_bundle_get_identity_key(omemo_bundle * bundle_p, uint8_t ** data_pp, size_t * data_len_p) {
  int ret_val = 0;

  const char * b64_string = (void *) 0;
  guchar * data_p = (void *) 0;
  gsize len = 0;

  if (!bundle_p || !bundle_p->identity_key_node_p) {
    ret_val = OMEMO_ERR_NULL;
    goto cleanup;
  }

  b64_string = mxmlGetOpaque(bundle_p->identity_key_node_p);
  if (!b64_string) {
    ret_val = OMEMO_ERR_MALFORMED_BUNDLE;
    goto cleanup;
  }

  data_p = g_base64_decode(b64_string, &len);

  *data_pp = data_p;
  *data_len_p = len;

cleanup:
  return ret_val;
}

int omemo_bundle_add_pre_key(omemo_bundle * bundle_p, uint32_t pre_key_id, uint8_t * data_p, size_t data_len) {
  int ret_val = 0;

  mxml_node_t * prekeys_node_p = (void *) 0;
  mxml_node_t * pre_key_node_p = (void *) 0;
  char * pre_key_id_string = (void *) 0;
  gchar * b64_string = (void *) 0;

  prekeys_node_p = bundle_p->pre_keys_node_p;
  if (!prekeys_node_p) {
    prekeys_node_p = mxmlNewElement(MXML_NO_PARENT, PREKEYS_NODE_NAME);
  }

  pre_key_node_p = mxmlNewElement(MXML_NO_PARENT, PRE_KEY_NODE_NAME);
  if (int_to_string(pre_key_id, &pre_key_id_string) <= 0) {
    ret_val = -1;
    goto cleanup;
  }
  mxmlElementSetAttr(pre_key_node_p, PRE_KEY_NODE_ID_ATTR_NAME, pre_key_id_string);

  b64_string = g_base64_encode(data_p, data_len);
  (void) mxmlNewOpaque(pre_key_node_p, b64_string);

  mxmlAdd(prekeys_node_p, MXML_ADD_AFTER, MXML_ADD_TO_PARENT, pre_key_node_p);

  bundle_p->pre_keys_node_p = prekeys_node_p;
  bundle_p->pre_keys_amount++;

cleanup:
  if (ret_val < 0) {
    mxmlDelete(pre_key_node_p);
  }
  g_free(b64_string);
  free(pre_key_id_string);

  return ret_val;
}


int omemo_bundle_get_random_pre_key(omemo_bundle * bundle_p, uint32_t * pre_key_id_p, uint8_t ** data_pp, size_t * data_len_p) {
  int ret_val = 0;

  mxml_node_t * pre_key_node_p = (void *) 0;
  gint32 random = 0;
  mxml_node_t * next_p = (void *) 0;
  const char * b64_string = (void *) 0;
  guchar * data_p = (void *) 0;
  gsize len = 0;
  const char * pre_key_id_string = (void *) 0;

  if (!bundle_p || !bundle_p->pre_keys_node_p) {
    ret_val = OMEMO_ERR_NULL;
    goto cleanup;
  }

  ret_val = expect_next_node(bundle_p->pre_keys_node_p, mxmlGetFirstChild, PRE_KEY_NODE_NAME, &pre_key_node_p);
  if (ret_val) {
    goto cleanup;
  }

  random = g_random_int_range(0, bundle_p->pre_keys_amount);
  next_p = pre_key_node_p;
  for (int i = 0; i < random; i++) {
    next_p = mxmlGetNextSibling(next_p);
    if (!next_p) {
      ret_val = OMEMO_ERR_MALFORMED_BUNDLE;
      goto cleanup;
    }
  }

  pre_key_id_string = mxmlElementGetAttr(next_p, PRE_KEY_NODE_ID_ATTR_NAME);
  if (!pre_key_id_string) {
    ret_val = OMEMO_ERR_MALFORMED_BUNDLE;
    goto cleanup;
  }

  b64_string = mxmlGetOpaque(next_p);
  if (!b64_string) {
    ret_val = OMEMO_ERR_MALFORMED_BUNDLE;
    goto cleanup;
  }

  data_p = g_base64_decode(b64_string, &len);

  *pre_key_id_p = strtol(pre_key_id_string, (void *) 0, 0);
  *data_pp = data_p;
  *data_len_p = len;

cleanup:

  return ret_val;
}

int omemo_bundle_export(omemo_bundle * bundle_p, char ** publish) {
  int ret_val = 0;

  char * node_value = (void *) 0;
Richard Bayerle's avatar
Richard Bayerle committed
436
  const char * format = "%s%s%s%s%s";
Richard Bayerle's avatar
Richard Bayerle committed
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
  size_t len = 0;
  mxml_node_t * publish_node_p = (void *) 0;
  mxml_node_t * item_node_p = (void *) 0;
  mxml_node_t * bundle_node_p = (void *) 0;
  char * out = (void *) 0;

  if (!bundle_p->device_id || !bundle_p->signed_pk_node_p || !bundle_p->signature_node_p || !bundle_p->identity_key_node_p || !bundle_p->pre_keys_node_p) {
    ret_val = -1;
    goto cleanup;
  }
  if (bundle_p->pre_keys_amount < 20) {
    ret_val = -2;
    goto cleanup;
  }
  if (bundle_p->pre_keys_amount < 100) {
    //TODO: maybe issue warning
    //FIXME: numbers into constants
  }

Richard Bayerle's avatar
Richard Bayerle committed
456
  len = snprintf((void *) 0, 0, format, OMEMO_NS, OMEMO_NS_SEPARATOR, BUNDLE_PEP_NAME, OMEMO_NS_SEPARATOR_FINAL, bundle_p->device_id) + 1;
Richard Bayerle's avatar
Richard Bayerle committed
457
  node_value = malloc(len);
Richard Bayerle's avatar
Richard Bayerle committed
458
  if (snprintf(node_value, len, format, OMEMO_NS, OMEMO_NS_SEPARATOR, BUNDLE_PEP_NAME, OMEMO_NS_SEPARATOR_FINAL, bundle_p->device_id) <= 0) {
Richard Bayerle's avatar
Richard Bayerle committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
    ret_val = -4;
    goto cleanup;
  }

  publish_node_p = mxmlNewElement(MXML_NO_PARENT, PUBLISH_NODE_NAME);
  mxmlElementSetAttr(publish_node_p, PUBLISH_NODE_NODE_ATTR_NAME, node_value);

  item_node_p = mxmlNewElement(publish_node_p, ITEM_NODE_NAME);

  bundle_node_p = mxmlNewElement(item_node_p, BUNDLE_NODE_NAME);
  mxmlElementSetAttr(bundle_node_p, "xmlns", OMEMO_NS);

  mxmlAdd(bundle_node_p, MXML_ADD_AFTER, MXML_ADD_TO_PARENT, bundle_p->signed_pk_node_p);
  mxmlAdd(bundle_node_p, MXML_ADD_AFTER, MXML_ADD_TO_PARENT, bundle_p->signature_node_p);
  mxmlAdd(bundle_node_p, MXML_ADD_AFTER, MXML_ADD_TO_PARENT, bundle_p->identity_key_node_p);
  mxmlAdd(bundle_node_p, MXML_ADD_AFTER, MXML_ADD_TO_PARENT, bundle_p->pre_keys_node_p);

  out = mxmlSaveAllocString(publish_node_p, MXML_NO_CALLBACK);
  if (!out) {
    ret_val = -5;
    goto cleanup;
  }

  *publish = out;

cleanup:
  free(node_value);

  return ret_val;
}

int omemo_bundle_import (const char * received_bundle, omemo_bundle ** bundle_pp) {
  int ret_val = 0;

  omemo_bundle * bundle_p = (void *) 0;
  mxml_node_t * items_node_p = (void *) 0;
  mxml_node_t * item_node_p = (void *) 0;
  mxml_node_t * bundle_node_p = (void *) 0;
  const char * bundle_node_name = (void *) 0;
498
  char ** split = (void *) 0;
Richard Bayerle's avatar
Richard Bayerle committed
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
  char * device_id = (void *) 0;
  mxml_node_t * signed_pk_node_p = (void *) 0;
  mxml_node_t * signature_node_p = (void *) 0;
  mxml_node_t * identity_key_node_p = (void *) 0;
  mxml_node_t * prekeys_node_p = (void *) 0;
  mxml_node_t * pre_key_node_p = (void *) 0;
  size_t pre_keys_count = 0;

  ret_val = omemo_bundle_create(&bundle_p);
  if (ret_val) {
    goto cleanup;
  }

  items_node_p = mxmlLoadString((void *) 0, received_bundle, MXML_OPAQUE_CALLBACK);
  if (!items_node_p) {
    ret_val = OMEMO_ERR_MALFORMED_XML;
    goto cleanup;
  }
517

Richard Bayerle's avatar
Richard Bayerle committed
518
519
520
521
522
523
524
525
526
527
528
  ret_val = strncmp(mxmlGetElement(items_node_p), ITEMS_NODE_NAME, strlen(ITEMS_NODE_NAME));
  if (ret_val) {
    ret_val = OMEMO_ERR_MALFORMED_XML;
    goto cleanup;
  }

  bundle_node_name = mxmlElementGetAttr(items_node_p, PEP_NODE_NAME);
  if (!bundle_node_name) {
    ret_val = OMEMO_ERR_MALFORMED_XML;
    goto cleanup;
  }
529

530
531
532
533
534
  split = g_strsplit(bundle_node_name, OMEMO_NS_SEPARATOR_FINAL, 6);
  if (!g_strcmp0(OMEMO_NS_SEPARATOR, OMEMO_NS_SEPARATOR_FINAL)) {
    device_id = g_strdup(split[5]);
  } else {
    device_id = g_strdup(split[1]);
Richard Bayerle's avatar
Richard Bayerle committed
535
  }
536
  bundle_p->device_id = device_id;
Richard Bayerle's avatar
Richard Bayerle committed
537

538
539
540
  item_node_p = mxmlFindPath(items_node_p, ITEM_NODE_NAME);
  if (!item_node_p) {
    ret_val = OMEMO_ERR_MALFORMED_XML;
Richard Bayerle's avatar
Richard Bayerle committed
541
542
543
    goto cleanup;
  }

544
545
546
  bundle_node_p = mxmlFindPath(item_node_p, BUNDLE_NODE_NAME);
  if (!bundle_node_p) {
    ret_val = OMEMO_ERR_MALFORMED_XML;
Richard Bayerle's avatar
Richard Bayerle committed
547
548
549
    goto cleanup;
  }

550
551
552
  signed_pk_node_p = mxmlFindPath(bundle_node_p, SIGNED_PRE_KEY_NODE_NAME);
  if (!signed_pk_node_p) {
    ret_val = OMEMO_ERR_MALFORMED_XML;
Richard Bayerle's avatar
Richard Bayerle committed
553
554
    goto cleanup;
  }
555
  signed_pk_node_p = mxmlGetParent(signed_pk_node_p);
Richard Bayerle's avatar
Richard Bayerle committed
556
557
  bundle_p->signed_pk_node_p = signed_pk_node_p;

558
559
560
  signature_node_p = mxmlFindPath(bundle_node_p, SIGNATURE_NODE_NAME);
  if (!signature_node_p) {
    ret_val = OMEMO_ERR_MALFORMED_XML;
Richard Bayerle's avatar
Richard Bayerle committed
561
562
    goto cleanup;
  }
563
  signature_node_p = mxmlGetParent(signature_node_p);
Richard Bayerle's avatar
Richard Bayerle committed
564
565
  bundle_p->signature_node_p = signature_node_p;

566
567
568
  identity_key_node_p = mxmlFindPath(bundle_node_p, IDENTITY_KEY_NODE_NAME);
  if (!identity_key_node_p) {
    ret_val = OMEMO_ERR_MALFORMED_XML;
Richard Bayerle's avatar
Richard Bayerle committed
569
570
    goto cleanup;
  }
571
  identity_key_node_p = mxmlGetParent(identity_key_node_p);
Richard Bayerle's avatar
Richard Bayerle committed
572
573
  bundle_p->identity_key_node_p = identity_key_node_p;

574
575
576
  prekeys_node_p = mxmlFindPath(bundle_node_p, PREKEYS_NODE_NAME);
  if (!prekeys_node_p) {
    ret_val = OMEMO_ERR_MALFORMED_XML;
Richard Bayerle's avatar
Richard Bayerle committed
577
578
579
580
    goto cleanup;
  }
  bundle_p->pre_keys_node_p = prekeys_node_p;

581
582
583
  pre_key_node_p = mxmlFindPath(prekeys_node_p, PRE_KEY_NODE_NAME);
  if (!pre_key_node_p) {
    ret_val = OMEMO_ERR_MALFORMED_XML;
Richard Bayerle's avatar
Richard Bayerle committed
584
585
    goto cleanup;
  }
586
  pre_key_node_p = mxmlGetParent(pre_key_node_p);
Richard Bayerle's avatar
Richard Bayerle committed
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
  pre_keys_count++;
  pre_key_node_p = mxmlGetNextSibling(pre_key_node_p);

  while (pre_key_node_p) {
    pre_keys_count++;
    pre_key_node_p = mxmlGetNextSibling(pre_key_node_p);
  }
  bundle_p->pre_keys_amount = pre_keys_count;

  mxmlRemove(signed_pk_node_p);
  mxmlRemove(signature_node_p);
  mxmlRemove(identity_key_node_p);
  mxmlRemove(prekeys_node_p);

  *bundle_pp = bundle_p;

cleanup:
  if (ret_val) {
    omemo_bundle_destroy(bundle_p);
  }
  mxmlDelete(items_node_p);
608
  g_strfreev(split);
Richard Bayerle's avatar
Richard Bayerle committed
609
610
611
612
613

  return ret_val;
}

int omemo_bundle_get_pep_node_name(uint32_t device_id, char ** node_name_p) {
Richard Bayerle's avatar
Richard Bayerle committed
614
615
  char * format = "%s%s%s%s%i";
  size_t len = snprintf((void *) 0, 0, format, OMEMO_NS, OMEMO_NS_SEPARATOR, BUNDLE_PEP_NAME, OMEMO_NS_SEPARATOR_FINAL, device_id);
Richard Bayerle's avatar
Richard Bayerle committed
616
617
618
619
620
621
622
  size_t buf_len = len + 1;

  char * node_name = malloc(buf_len);
  if (!node_name) {
    return -1;
  }

Richard Bayerle's avatar
Richard Bayerle committed
623
  size_t actual_len = snprintf(node_name, buf_len, format, OMEMO_NS, OMEMO_NS_SEPARATOR, BUNDLE_PEP_NAME, OMEMO_NS_SEPARATOR_FINAL, device_id);
Richard Bayerle's avatar
Richard Bayerle committed
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
  if (actual_len != len) {
    free(node_name);
    return -2;
  }

  *node_name_p = node_name;
  return 0;
}

void omemo_bundle_destroy(omemo_bundle * bundle_p) {
  if (bundle_p) {
    mxmlDelete(bundle_p->signed_pk_node_p);
    mxmlDelete(bundle_p->signature_node_p);
    mxmlDelete(bundle_p->identity_key_node_p);
    mxmlDelete(bundle_p->pre_keys_node_p);
    free(bundle_p->device_id);
    free(bundle_p);
  }
}

int omemo_devicelist_create(const char * from, omemo_devicelist ** dl_pp) {
  if (!from || !dl_pp) {
    return OMEMO_ERR_NULL;
  }

  int ret_val = 0;

  omemo_devicelist * dl_p = (void *) 0;
  char * from_dup = (void *) 0;
  mxml_node_t * list_node_p = (void *) 0;

  dl_p = malloc(sizeof(omemo_devicelist));
  if (!dl_p) {
    ret_val = OMEMO_ERR_NOMEM;
    goto cleanup;
  }

661
  from_dup = g_strndup(from, strlen(from));
Richard Bayerle's avatar
Richard Bayerle committed
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
  if (!from_dup) {
    ret_val = OMEMO_ERR_NOMEM;
    goto cleanup;
  }

  list_node_p = mxmlNewElement(MXML_NO_PARENT, LIST_NODE_NAME);
  mxmlElementSetAttr(list_node_p, XMLNS_ATTR_NAME, OMEMO_NS);

  dl_p->list_node_p = list_node_p;
  dl_p->id_list_p = (void *) 0;
  dl_p->from = from_dup;
  *dl_pp = dl_p;

cleanup:
  if (ret_val) {
    free(from_dup);
    free(dl_p);
  }
  return ret_val;
}

int omemo_devicelist_import(char * received_devicelist, const char * from, omemo_devicelist ** dl_pp) {
  if (!received_devicelist || !from || !dl_pp) {
    return OMEMO_ERR_NULL;
  }

  int ret_val = 0;

  omemo_devicelist * dl_p = (void *) 0;
  mxml_node_t * items_node_p = (void *) 0;
  mxml_node_t * item_node_p = (void *) 0;
  mxml_node_t * list_node_p = (void *) 0;
  mxml_node_t * device_node_p = (void *) 0;
  GList * id_list_p = (void *) 0;

  ret_val = omemo_devicelist_create(from, &dl_p);
  if (ret_val) {
    goto cleanup;
  }

  items_node_p = mxmlLoadString((void *) 0, received_devicelist, MXML_NO_CALLBACK);
  ret_val = strncmp(mxmlGetElement(items_node_p), ITEMS_NODE_NAME, strlen(ITEMS_NODE_NAME));
  if (ret_val) {
    ret_val = OMEMO_ERR_MALFORMED_XML;
    goto cleanup;
  }

709
710
711
712
713
714
715
  item_node_p = mxmlGetFirstChild(items_node_p);
  if (!item_node_p) {
    ret_val = 0;
    *dl_pp = dl_p;
    goto cleanup;
  }
  ret_val = strncmp(mxmlGetElement(item_node_p), ITEM_NODE_NAME, strlen(ITEM_NODE_NAME));
Richard Bayerle's avatar
Richard Bayerle committed
716
  if (ret_val) {
717
    ret_val = OMEMO_ERR_MALFORMED_XML;
Richard Bayerle's avatar
Richard Bayerle committed
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
    goto cleanup;
  }

  ret_val = expect_next_node(item_node_p, mxmlGetFirstChild, LIST_NODE_NAME, &list_node_p);
  if (ret_val) {
    goto cleanup;
  }

  mxmlDelete(dl_p->list_node_p);
  mxmlRemove(list_node_p);
  dl_p->list_node_p = list_node_p;

  ret_val = expect_next_node(list_node_p, mxmlGetFirstChild, DEVICE_NODE_NAME, &device_node_p);
  if (ret_val) {
    ret_val = 0;
    *dl_pp = dl_p;
    goto cleanup;
  }

  while (device_node_p) {
    const char * id_string = mxmlElementGetAttr(device_node_p, DEVICE_NODE_ID_ATTR_NAME);
    if (!id_string) {
      ret_val = OMEMO_ERR_MALFORMED_XML;
      goto cleanup;
    }

    uint32_t * id_temp_p = malloc(sizeof(uint32_t));
    if (!id_temp_p) {
      ret_val = OMEMO_ERR_NOMEM;
      goto cleanup;
    }

    *id_temp_p = strtol(id_string, (void *) 0, 0);
    id_list_p = g_list_append(id_list_p, id_temp_p);

    device_node_p = mxmlGetNextSibling(device_node_p);
  }
  dl_p->id_list_p = id_list_p;

  *dl_pp = dl_p;

cleanup:
  if (ret_val) {
    omemo_devicelist_destroy(dl_p);
    g_list_free_full(id_list_p, free);
  }
  mxmlDelete(items_node_p);
  return ret_val;
}

int omemo_devicelist_add(omemo_devicelist * dl_p, uint32_t device_id) {
  if (!dl_p || !dl_p->list_node_p) {
    return OMEMO_ERR_NULL;
  }

  uint32_t * id_p = malloc(sizeof(uint32_t));
  if (!id_p) {
    return OMEMO_ERR_NOMEM;
  }
  *id_p = device_id;

  char * id_string;
  int id_string_len = int_to_string(device_id, &id_string);
  if (id_string_len < 1) {
    free(id_p);
    return OMEMO_ERR;
  }

  mxml_node_t * device_node_p = mxmlNewElement(MXML_NO_PARENT, DEVICE_NODE_NAME);
  mxmlElementSetAttr(device_node_p, DEVICE_NODE_ID_ATTR_NAME, id_string);
  mxmlAdd(dl_p->list_node_p, MXML_ADD_AFTER, MXML_ADD_TO_PARENT, device_node_p);
  dl_p->id_list_p = g_list_append(dl_p->id_list_p, id_p);

  return 0;
}

int omemo_devicelist_contains_id(const omemo_devicelist * dl_p, uint32_t device_id) {
  if (!dl_p || !dl_p->list_node_p) {
    return 0;
  }

  GList * curr = dl_p->id_list_p;
  while(curr) {
    if (*((uint32_t *) curr->data) == device_id) {
      return 1;
    }

    curr = curr->next;
  }

  return 0;
}

int omemo_devicelist_remove(omemo_devicelist * dl_p, uint32_t device_id) {
  if (!dl_p) {
    return OMEMO_ERR_NULL;
  }
  int ret_val = 0;

  char * device_id_str = (void *) 0;
  mxml_node_t * device_node_p = (void *) 0;
  GList * curr_p = (void *) 0;
  uint32_t * remove_id_p = (void *) 0;

  ret_val = int_to_string(device_id, &device_id_str);
  if (ret_val < 1) {
    ret_val = OMEMO_ERR;
    goto cleanup;
  }
  ret_val = 0;

  device_node_p = mxmlFindElement(dl_p->list_node_p, dl_p->list_node_p, DEVICE_NODE_NAME, DEVICE_NODE_ID_ATTR_NAME, device_id_str, MXML_DESCEND);
  if (!device_node_p) {
    goto cleanup;
  }
  mxmlDelete(device_node_p);

  for (curr_p = dl_p->id_list_p; curr_p; curr_p = curr_p->next) {
    if (omemo_devicelist_list_data(curr_p) == device_id) {
      remove_id_p = curr_p->data;
      break;
    }
  }

  dl_p->id_list_p = g_list_remove(dl_p->id_list_p, remove_id_p);

cleanup:
  free(device_id_str);
  return ret_val;
}

int omemo_devicelist_is_empty(omemo_devicelist * dl_p) {
  return dl_p->id_list_p ? 0 : 1;
}

int omemo_devicelist_diff(const omemo_devicelist * dl_a_p, const omemo_devicelist * dl_b_p, GList ** a_minus_b_pp, GList ** b_minus_a_pp) {
  if (!dl_a_p || !dl_b_p || !a_minus_b_pp || !b_minus_a_pp) {
    return OMEMO_ERR_NULL;
  }

  GList * a_l_p = (void *) 0;
  GList * b_l_p = (void *) 0;
  GList * amb_p = (void *) 0;
  GList * bma_p = (void *) 0;
  GList * curr_p = (void *) 0;
  GList * temp_p = (void *) 0;

  a_l_p = omemo_devicelist_get_id_list(dl_a_p);
  b_l_p = omemo_devicelist_get_id_list(dl_b_p);

  curr_p = a_l_p;
  while (curr_p) {
    temp_p = curr_p;
    curr_p = curr_p->next;
    if (!omemo_devicelist_contains_id(dl_b_p, omemo_devicelist_list_data(temp_p))) {
      a_l_p = g_list_remove_link(a_l_p, temp_p);
      amb_p = g_list_prepend(amb_p, temp_p->data);
      g_list_free_1(temp_p);
    }
  }

  curr_p = b_l_p;
  while (curr_p) {
    temp_p = curr_p;
    curr_p = curr_p->next;
    if (!omemo_devicelist_contains_id(dl_a_p, omemo_devicelist_list_data(temp_p))) {
      b_l_p = g_list_remove_link(b_l_p, temp_p);
      bma_p = g_list_prepend(bma_p, temp_p->data);
      g_list_free_1(temp_p);
    }
  }

  *a_minus_b_pp = amb_p;
  *b_minus_a_pp = bma_p;

  g_list_free_full(a_l_p, free);
  g_list_free_full(b_l_p, free);

  return 0;
}

int omemo_devicelist_export(omemo_devicelist * dl_p, char ** xml_p) {
  if (!dl_p || !dl_p->list_node_p || !xml_p) {
    return OMEMO_ERR_NULL;
  }

  mxml_node_t * publish_node_p = mxmlNewElement(MXML_NO_PARENT, PUBLISH_NODE_NAME);
  mxmlElementSetAttr(publish_node_p, PUBLISH_NODE_NODE_ATTR_NAME, OMEMO_DEVICELIST_PEP_NODE);

  mxml_node_t * item_node_p = mxmlNewElement(publish_node_p, ITEM_NODE_NAME);
  mxmlAdd(item_node_p, MXML_ADD_AFTER, MXML_ADD_TO_PARENT, dl_p->list_node_p);

  char * xml = mxmlSaveAllocString(publish_node_p, MXML_NO_CALLBACK);
  if (!xml) {
    return OMEMO_ERR;
  }

  *xml_p = xml;
  return 0;
}

GList * omemo_devicelist_get_id_list(const omemo_devicelist * dl_p) {
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
  GList * new_l_p = (void *) 0;
  GList * curr_p = (void *) 0;
  uint32_t * cpy_p = (void *) 0;

  for (curr_p = dl_p->id_list_p; curr_p; curr_p = curr_p->next) {
    cpy_p = malloc(sizeof(uint32_t));
    if (!cpy_p) {
      g_list_free_full(new_l_p, free);
      return (void *) 0;
    }
    memcpy(cpy_p, curr_p->data, (sizeof(uint32_t)));

    new_l_p = g_list_append(new_l_p, cpy_p);
  }

  return new_l_p;
Richard Bayerle's avatar
Richard Bayerle committed
936
937
938
939
940
941
942
943
944
945
946
}

int omemo_devicelist_has_id_list(const omemo_devicelist * dl_p) {
  return (dl_p->id_list_p) ? 1 : 0;
}

const char * omemo_devicelist_get_owner(const omemo_devicelist * dl_p) {
  return dl_p ? dl_p->from : (void *) 0;
}

int omemo_devicelist_get_pep_node_name(char ** node_name_p) {
Richard Bayerle's avatar
Richard Bayerle committed
947
948
  char * format = "%s%s%s";
  size_t len = snprintf((void *) 0, 0, format, OMEMO_NS, OMEMO_NS_SEPARATOR, DEVICELIST_PEP_NAME);
Richard Bayerle's avatar
Richard Bayerle committed
949
950
951
952
953
954
955
  size_t buf_len = len + 1;

  char * node_name = malloc(buf_len);
  if (!node_name) {
    return -1;
  }

Richard Bayerle's avatar
Richard Bayerle committed
956
  size_t actual_len = snprintf(node_name, buf_len, format, OMEMO_NS, OMEMO_NS_SEPARATOR, DEVICELIST_PEP_NAME);
Richard Bayerle's avatar
Richard Bayerle committed
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
  if (actual_len != len) {
    free(node_name);
    return -2;
  }

  *node_name_p = node_name;
  return 0;
}

void omemo_devicelist_destroy(omemo_devicelist * dl_p) {
  if (dl_p) {
    g_list_free_full(dl_p->id_list_p, free);
    mxmlDelete(dl_p->list_node_p);
    free(dl_p->from);
    free(dl_p);
  }
}

975
int omemo_message_create(uint32_t sender_device_id, const omemo_crypto_provider * crypto_p, omemo_message ** message_pp) {
Richard Bayerle's avatar
Richard Bayerle committed
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
  if (!crypto_p || !crypto_p->random_bytes_func || !crypto_p->aes_gcm_encrypt_func || !message_pp) {
    return OMEMO_ERR_NULL;
  }

  int ret_val = 0;

  omemo_message * msg_p = (void *) 0;
  uint8_t * iv_p = (void *) 0;
  gchar * iv_b64 = (void *) 0;
  char * device_id_string = (void *) 0;
  mxml_node_t * header_node_p = (void *) 0;
  mxml_node_t * iv_node_p = (void *) 0;
  uint8_t * key_p = (void *) 0;

  msg_p = malloc(sizeof(omemo_message));
  if (!msg_p) {
    ret_val = OMEMO_ERR_NOMEM;
    goto cleanup;
  }
  memset(msg_p, 0, sizeof(omemo_message));

  ret_val = crypto_p->random_bytes_func(&iv_p, OMEMO_AES_GCM_IV_LENGTH, crypto_p->user_data_p);
  if (ret_val) {
    goto cleanup;
  }