-
Notifications
You must be signed in to change notification settings - Fork 32
/
lurch.c
2743 lines (2314 loc) · 90.8 KB
/
lurch.c
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
29
30
31
32
33
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
172
173
174
175
176
177
178
179
180
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
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
661
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
709
710
711
712
713
714
715
716
717
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
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
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
#define PURPLE_PLUGINS
#include <glib.h>
#include <inttypes.h>
#include <string.h>
#include <time.h>
#include "account.h"
#include "cmds.h"
#include "conversation.h"
#include "debug.h"
#include "notify.h"
#include "plugin.h"
#include "util.h"
#include "version.h"
#include "xmlnode.h"
#include "jabber.h"
#include "jutil.h"
#include "pep.h"
#include "libomemo.h"
#include "libomemo_crypto.h"
#include "libomemo_storage.h"
#include "axc.h"
#include "axc_store.h"
#ifdef _WIN32
#define strnlen(a, b) (MIN(strlen(a), (b)))
#endif
#define JABBER_PROTOCOL_ID "prpl-jabber"
// see https://www.ietf.org/rfc/rfc3920.txt
#define JABBER_MAX_LEN_NODE 1023
#define JABBER_MAX_LEN_DOMAIN 1023
#define JABBER_MAX_LEN_BARE JABBER_MAX_LEN_NODE + JABBER_MAX_LEN_DOMAIN + 1
#define LURCH_ACC_SETTING_INITIALIZED "lurch_initialised"
#define LURCH_DB_SUFFIX "_db.sqlite"
#define LURCH_DB_NAME_OMEMO "omemo"
#define LURCH_DB_NAME_AXC "axc"
#define LURCH_ERR -1000000
#define LURCH_ERR_NOMEM -1000001
#define LURCH_ERR_NO_BUNDLE -1000010
#define LURCH_ERR_STRING_ENCRYPT "There was an error encrypting the message and it was not sent. " \
"You can try again, or try to find the problem by looking at the debug log."
#define LURCH_ERR_STRING_DECRYPT "There was an error decrypting an OMEMO message addressed to this device. " \
"See the debug log for details."
typedef struct lurch_addr {
char * jid;
uint32_t device_id;
} lurch_addr;
typedef struct lurch_queued_msg {
omemo_message * om_msg_p;
GList * recipient_addr_l_p;
GList * no_sess_l_p;
GHashTable * sess_handled_p;
} lurch_queued_msg;
omemo_crypto_provider crypto = {
.random_bytes_func = omemo_default_crypto_random_bytes,
.aes_gcm_encrypt_func = omemo_default_crypto_aes_gcm_encrypt,
.aes_gcm_decrypt_func = omemo_default_crypto_aes_gcm_decrypt
};
int topic_changed = 0;
int uninstall = 0;
PurpleCmdId lurch_cmd_id = 0;
// key: char * (name of muc), value: GHashTable * (mapping of user alias to full jid)
GHashTable * chat_users_ht_p = (void *) 0;
static void lurch_addr_list_destroy_func(gpointer data) {
lurch_addr * addr_p = (lurch_addr *) data;
free(addr_p->jid);
free(addr_p);
}
/**
* Creates a queued msg.
* Note that it just saves the pointers, so make sure they are not freed during
* the lifetime of this struct and instead use the destroy function when done.
*
* @param om_msg_p Pointer to the omemo_message.
* @param recipient_addr_l_p Pointer to the list of recipient addresses.
* @param no_sess_l_p Pointer to the list that contains the addresses that do
* not have sessions, i.e. for which bundles were requested.
* @param cmsg_pp Will point to the pointer of the created queued msg struct.
* @return 0 on success, negative on error.
*/
static int lurch_queued_msg_create(omemo_message * om_msg_p,
GList * recipient_addr_l_p,
GList * no_sess_l_p,
lurch_queued_msg ** qmsg_pp) {
int ret_val = 0;
char * err_msg_dbg = (void *) 0;
lurch_queued_msg * qmsg_p = (void *) 0;
GHashTable * sess_handled_p = (void *) 0;
qmsg_p = malloc(sizeof(lurch_queued_msg));
if (!qmsg_p) {
ret_val = LURCH_ERR_NOMEM;
err_msg_dbg = g_strdup_printf("failed to malloc space for queued msg struct");
goto cleanup;
}
sess_handled_p = g_hash_table_new(g_str_hash, g_str_equal);
qmsg_p->om_msg_p = om_msg_p;
qmsg_p->recipient_addr_l_p = recipient_addr_l_p;
qmsg_p->no_sess_l_p = no_sess_l_p;
qmsg_p->sess_handled_p = sess_handled_p;
*qmsg_pp = qmsg_p;
cleanup:
if (ret_val) {
free(qmsg_p);
}
if (err_msg_dbg) {
purple_debug_error("lurch", "%s: %s (%i)\n", __func__, err_msg_dbg, ret_val);
free(err_msg_dbg);
}
return ret_val;
}
static int lurch_queued_msg_is_handled(const lurch_queued_msg * qmsg_p) {
return (g_list_length(qmsg_p->no_sess_l_p) == g_hash_table_size(qmsg_p->sess_handled_p)) ? 1 : 0;
}
/**
* Frees all the memory used by the queued msg.
*/
static void lurch_queued_msg_destroy(lurch_queued_msg * qmsg_p) {
if (qmsg_p) {
omemo_message_destroy(qmsg_p->om_msg_p);
g_list_free_full(qmsg_p->recipient_addr_l_p, free);
g_hash_table_destroy(qmsg_p->sess_handled_p);
free(qmsg_p);
}
}
static char * lurch_queue_make_key_string_s(const char * name, const char * device_id) {
return g_strconcat(name, "/", device_id, NULL);
}
/**
* Returns the db name, has to be g_free()d.
*
* @param uname The username.
* @param which Either LURCH_DB_NAME_OMEMO or LURCH_DB_NAME_AXC
* @return The path string.
*/
static char * lurch_uname_get_db_fn(const char * uname, char * which) {
return g_strconcat(purple_user_dir(), "/", uname, "_", which, LURCH_DB_SUFFIX, NULL);
}
/**
* For some reason pidgin returns account names with a trailing "/".
* This function removes it.
* All other functions asking for the username assume the "/" is already stripped.
*
* @param uname The username.
* @return A duplicated string with the trailing "/" removed. free() when done.
*/
static char * lurch_uname_strip(const char * uname) {
char ** split;
char * stripped;
split = g_strsplit(uname, "/", 2);
stripped = g_strdup(split[0]);
g_strfreev(split);
return stripped;
}
/**
* Creates and initializes the axc context.
*
* @param uname The username.
* @param ctx_pp Will point to an initialized axc context on success.
* @return 0 on success, negative on error.
*/
static int lurch_axc_get_init_ctx(char * uname, axc_context ** ctx_pp) {
int ret_val = 0;
char * err_msg_dbg = (void *) 0;
axc_context * ctx_p = (void *) 0;
char * db_fn = (void *) 0;
ret_val = axc_context_create(&ctx_p);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to create axc context");
goto cleanup;
}
db_fn = lurch_uname_get_db_fn(uname, LURCH_DB_NAME_AXC);
ret_val = axc_context_set_db_fn(ctx_p, db_fn, strlen(db_fn));
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to set axc db filename");
goto cleanup;
}
ret_val = axc_init(ctx_p);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to init axc context");
goto cleanup;
}
*ctx_pp = ctx_p;
cleanup:
if (ret_val) {
axc_context_destroy_all(ctx_p);
}
if (err_msg_dbg) {
purple_debug_error("lurch", "%s: %s (%i)\n", __func__, err_msg_dbg, ret_val);
free(err_msg_dbg);
}
free (db_fn);
return ret_val;
}
/**
* Does the first-time install of the axc DB.
* As specified in OMEMO, it checks if the generated device ID already exists.
* Therefore, it should be called at a point in time when other entries exist.
*
* @param uname The username.
* @return 0 on success, negative on error.
*/
static int lurch_axc_prepare(char * uname) {
int ret_val = 0;
char * err_msg_dbg = (void *) 0;
axc_context * axc_ctx_p = (void *) 0;
uint32_t device_id = 0;
char * db_fn_omemo = (void *) 0;
ret_val = lurch_axc_get_init_ctx(uname, &axc_ctx_p);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to get init axc ctx");
goto cleanup;
}
ret_val = axc_get_device_id(axc_ctx_p, &device_id);
if (!ret_val) {
// already installed
goto cleanup;
}
db_fn_omemo = lurch_uname_get_db_fn(uname, LURCH_DB_NAME_OMEMO);
while (1) {
ret_val = axc_install(axc_ctx_p);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to install axc");
goto cleanup;
}
ret_val = axc_get_device_id(axc_ctx_p, &device_id);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to get own device id");
goto cleanup;
}
ret_val = omemo_storage_global_device_id_exists(device_id, db_fn_omemo);
if (ret_val == 1) {
ret_val = axc_db_init_status_set(AXC_DB_NEEDS_ROLLBACK, axc_ctx_p);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to set axc db to rollback");
goto cleanup;
}
} else if (ret_val < 0) {
err_msg_dbg = g_strdup_printf("failed to access the db %s", db_fn_omemo);
goto cleanup;
} else {
break;
}
}
cleanup:
if (err_msg_dbg) {
purple_debug_error("lurch", "%s: %s (%i)\n", __func__, err_msg_dbg, ret_val);
free(err_msg_dbg);
}
axc_context_destroy_all(axc_ctx_p);
free(db_fn_omemo);
return ret_val;
}
/**
* Encrypts a data buffer, usually the omemo symmetric key, using axolotl.
* Assumes a valid session already exists.
*
* @param recipient_addr_p Pointer to the lurch_addr of the recipient.
* @param key_p Pointer to the key data.
* @param key_len Length of the key data.
* @param axc_ctx_p Pointer to the axc_context to use.
* @param key_ct_pp Will point to a pointer to an axc_buf containing the key ciphertext on success.
* @return 0 on success, negative on error
*/
static int lurch_key_encrypt(const lurch_addr * recipient_addr_p,
const uint8_t * key_p,
size_t key_len,
axc_context * axc_ctx_p,
axc_buf ** key_ct_buf_pp) {
int ret_val = 0;
char * err_msg_dbg = (void *) 0;
axc_buf * key_buf_p = (void *) 0;
axc_buf * key_ct_buf_p = (void *) 0;
axc_address axc_addr = {0};
purple_debug_info("lurch", "%s: encrypting key for %s:%i\n", __func__, recipient_addr_p->jid, recipient_addr_p->device_id);
key_buf_p = axc_buf_create(key_p, key_len);
if (!key_buf_p) {
err_msg_dbg = g_strdup_printf("failed to create buffer for the key");
goto cleanup;
}
axc_addr.name = recipient_addr_p->jid;
axc_addr.name_len = strnlen(axc_addr.name, JABBER_MAX_LEN_BARE);
axc_addr.device_id = recipient_addr_p->device_id;
ret_val = axc_message_encrypt_and_serialize(key_buf_p, &axc_addr, axc_ctx_p, &key_ct_buf_p);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to encrypt the key");
goto cleanup;
}
*key_ct_buf_pp = key_ct_buf_p;
cleanup:
if (ret_val) {
axc_buf_free(key_ct_buf_p);
}
if (err_msg_dbg) {
purple_debug_error("lurch", "%s: %s (%i)\n", __func__, err_msg_dbg, ret_val);
free(err_msg_dbg);
}
axc_buf_free(key_buf_p);
return ret_val;
}
/**
* For each of the recipients, encrypts the symmetric key using the existing axc session,
* then adds it to the omemo message.
* If the session does not exist, the recipient is skipped.
*
* @param om_msg_p Pointer to the omemo message.
* @param addr_l_p Pointer to the head of a list of the intended recipients' lurch_addrs.
* @param axc_ctx_p Pointer to the axc_context to use.
* @return 0 on success, negative on error.
*/
static int lurch_msg_encrypt_for_addrs(omemo_message * om_msg_p, GList * addr_l_p, axc_context * axc_ctx_p) {
int ret_val = 0;
char * err_msg_dbg = (void *) 0;
GList * curr_l_p = (void *) 0;
lurch_addr * curr_addr_p = (void *) 0;
axc_address addr = {0};
axc_buf * curr_key_ct_buf_p = (void *) 0;
purple_debug_info("lurch", "%s: trying to encrypt key for %i devices\n", __func__, g_list_length(addr_l_p));
for (curr_l_p = addr_l_p; curr_l_p; curr_l_p = curr_l_p->next) {
curr_addr_p = (lurch_addr *) curr_l_p->data;
addr.name = curr_addr_p->jid;
addr.name_len = strnlen(addr.name, JABBER_MAX_LEN_BARE);
addr.device_id = curr_addr_p->device_id;
ret_val = axc_session_exists_initiated(&addr, axc_ctx_p);
if (ret_val < 0) {
err_msg_dbg = g_strdup_printf("failed to check if session exists, aborting");
goto cleanup;
} else if (!ret_val) {
continue;
} else {
ret_val = lurch_key_encrypt(curr_addr_p,
omemo_message_get_key(om_msg_p),
omemo_message_get_key_len(om_msg_p),
axc_ctx_p,
&curr_key_ct_buf_p);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to encrypt key for %s:%i", curr_addr_p->jid, curr_addr_p->device_id);
goto cleanup;
}
ret_val = omemo_message_add_recipient(om_msg_p,
curr_addr_p->device_id,
axc_buf_get_data(curr_key_ct_buf_p),
axc_buf_get_len(curr_key_ct_buf_p));
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to add recipient to omemo msg");
goto cleanup;
}
axc_buf_free(curr_key_ct_buf_p);
curr_key_ct_buf_p = (void *) 0;
}
}
cleanup:
if (err_msg_dbg) {
purple_debug_error("lurch", "%s: %s (%i)\n", __func__, err_msg_dbg, ret_val);
free(err_msg_dbg);
}
axc_buf_free(curr_key_ct_buf_p);
return ret_val;
}
/**
* Collects the information needed for a bundle and publishes it.
*
* @param uname The username.
* @param js_p Pointer to the connection to use for publishing.
*/
static int lurch_bundle_publish_own(JabberStream * js_p) {
int ret_val = 0;
char * err_msg_dbg = (void *) 0;
char * uname = (void *) 0;
axc_context * axc_ctx_p = (void *) 0;
axc_bundle * axcbundle_p = (void *) 0;
omemo_bundle * omemobundle_p = (void *) 0;
axc_buf * curr_buf_p = (void *) 0;
axc_buf_list_item * next_p = (void *) 0;
char * bundle_xml = (void *) 0;
xmlnode * publish_node_bundle_p = (void *) 0;
uname = lurch_uname_strip(purple_account_get_username(purple_connection_get_account(js_p->gc)));
ret_val = lurch_axc_get_init_ctx(uname, &axc_ctx_p);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to init axc ctx");
goto cleanup;
}
ret_val = axc_bundle_collect(AXC_PRE_KEYS_AMOUNT, axc_ctx_p, &axcbundle_p);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to collect axc bundle");
goto cleanup;
}
ret_val = omemo_bundle_create(&omemobundle_p);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to create omemo_bundle");
goto cleanup;
}
ret_val = omemo_bundle_set_device_id(omemobundle_p, axc_bundle_get_reg_id(axcbundle_p));
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to set device id in omemo bundle");
goto cleanup;
}
curr_buf_p = axc_bundle_get_signed_pre_key(axcbundle_p);
ret_val = omemo_bundle_set_signed_pre_key(omemobundle_p,
axc_bundle_get_signed_pre_key_id(axcbundle_p),
axc_buf_get_data(curr_buf_p),
axc_buf_get_len(curr_buf_p));
if(ret_val) {
err_msg_dbg = g_strdup_printf("failed to set signed pre key in omemo bundle");
goto cleanup;
}
curr_buf_p = axc_bundle_get_signature(axcbundle_p);
ret_val = omemo_bundle_set_signature(omemobundle_p,
axc_buf_get_data(curr_buf_p),
axc_buf_get_len(curr_buf_p));
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to set signature in omemo bundle");
goto cleanup;
}
curr_buf_p = axc_bundle_get_identity_key(axcbundle_p);
ret_val = omemo_bundle_set_identity_key(omemobundle_p,
axc_buf_get_data(curr_buf_p),
axc_buf_get_len(curr_buf_p));
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to set public identity key in omemo bundle");
goto cleanup;
}
next_p = axc_bundle_get_pre_key_list(axcbundle_p);
while (next_p) {
curr_buf_p = axc_buf_list_item_get_buf(next_p);
ret_val = omemo_bundle_add_pre_key(omemobundle_p,
axc_buf_list_item_get_id(next_p),
axc_buf_get_data(curr_buf_p),
axc_buf_get_len(curr_buf_p));
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to add public pre key to omemo bundle");
goto cleanup;
}
next_p = axc_buf_list_item_get_next(next_p);
}
ret_val = omemo_bundle_export(omemobundle_p, &bundle_xml);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to export omemo bundle to xml");
goto cleanup;
}
publish_node_bundle_p = xmlnode_from_str(bundle_xml, -1);
jabber_pep_publish(js_p, publish_node_bundle_p);
purple_debug_info("lurch", "%s: published own bundle for %s\n", __func__, uname);
cleanup:
if (err_msg_dbg) {
purple_debug_error("lurch", "%s: %s (%i)\n", __func__, err_msg_dbg, ret_val);
free(err_msg_dbg);
}
free(uname);
axc_context_destroy_all(axc_ctx_p);
axc_bundle_destroy(axcbundle_p);
omemo_bundle_destroy(omemobundle_p);
free(bundle_xml);
return ret_val;
}
/**
* Parses the device ID from a received bundle update.
*
* @param bundle_node_ns The node name.
* @return The ID.
*/
static uint32_t lurch_bundle_name_get_device_id(const char * bundle_node_name) {
char ** split = (void *) 0;
uint32_t id = 0;
split = g_strsplit(bundle_node_name, ":", -1);
id = strtol(split[5], (void *) 0, 0);
g_strfreev(split);
return id;
}
/**
* Creates an axc session from a received bundle.
*
* @param uname The own username.
* @param from The sender of the bundle.
* @param items_p The bundle update as received in the PEP request handler.
*/
static int lurch_bundle_create_session(const char * uname,
const char * from,
const xmlnode * items_p,
axc_context * axc_ctx_p) {
int ret_val = 0;
char * err_msg_dbg = (void *) 0;
int len;
omemo_bundle * om_bundle_p = (void *) 0;
axc_address remote_addr = {0};
uint32_t pre_key_id = 0;
uint8_t * pre_key_p = (void *) 0;
size_t pre_key_len = 0;
uint32_t signed_pre_key_id = 0;
uint8_t * signed_pre_key_p = (void *) 0;
size_t signed_pre_key_len = 0;
uint8_t * signature_p = (void *) 0;
size_t signature_len = 0;
uint8_t * identity_key_p = (void *) 0;
size_t identity_key_len = 0;
axc_buf * pre_key_buf_p = (void *) 0;
axc_buf * signed_pre_key_buf_p = (void *) 0;
axc_buf * signature_buf_p = (void *) 0;
axc_buf * identity_key_buf_p = (void *) 0;
purple_debug_info("lurch", "%s: creating a session between %s and %s from a received bundle\n", __func__, uname, from);
ret_val = omemo_bundle_import(xmlnode_to_str(items_p, &len), &om_bundle_p);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to import xml into bundle");
goto cleanup;
}
remote_addr.name = from;
remote_addr.name_len = strnlen(from, JABBER_MAX_LEN_BARE);
remote_addr.device_id = omemo_bundle_get_device_id(om_bundle_p);
purple_debug_info("lurch", "%s: bundle's device id is %i\n", __func__, remote_addr.device_id);
ret_val = omemo_bundle_get_random_pre_key(om_bundle_p, &pre_key_id, &pre_key_p, &pre_key_len);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed get a random pre key from the bundle");
goto cleanup;
}
ret_val = omemo_bundle_get_signed_pre_key(om_bundle_p, &signed_pre_key_id, &signed_pre_key_p, &signed_pre_key_len);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to get the signed pre key from the bundle");
goto cleanup;
}
ret_val = omemo_bundle_get_signature(om_bundle_p, &signature_p, &signature_len);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to get the signature from the bundle");
goto cleanup;
}
ret_val = omemo_bundle_get_identity_key(om_bundle_p, &identity_key_p, &identity_key_len);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to get the public identity key from the bundle");
goto cleanup;
}
pre_key_buf_p = axc_buf_create(pre_key_p, pre_key_len);
signed_pre_key_buf_p = axc_buf_create(signed_pre_key_p, signed_pre_key_len);
signature_buf_p = axc_buf_create(signature_p, signature_len);
identity_key_buf_p = axc_buf_create(identity_key_p, identity_key_len);
if (!pre_key_buf_p || !signed_pre_key_buf_p || !signature_buf_p || !identity_key_buf_p) {
ret_val = LURCH_ERR;
err_msg_dbg = g_strdup_printf("failed to create one of the buffers");
goto cleanup;
}
ret_val = axc_session_from_bundle(pre_key_id, pre_key_buf_p,
signed_pre_key_id, signed_pre_key_buf_p,
signature_buf_p,
identity_key_buf_p,
&remote_addr,
axc_ctx_p);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to create a session from a bundle");
goto cleanup;
}
cleanup:
if (err_msg_dbg) {
purple_debug_error("lurch", "%s: %s (%i)\n", __func__, err_msg_dbg, ret_val);
free(err_msg_dbg);
}
omemo_bundle_destroy(om_bundle_p);
free(pre_key_p);
free(signed_pre_key_p);
free(signature_p);
free(identity_key_p);
axc_buf_free(pre_key_buf_p);
axc_buf_free(signed_pre_key_buf_p);
axc_buf_free(signature_buf_p);
axc_buf_free(identity_key_buf_p);
return ret_val;
}
/**
* Implements JabberIqCallback.
* Callback for a bundle request.
*/
static void lurch_bundle_request_cb(JabberStream * js_p, const char * from,
JabberIqType type, const char * id,
xmlnode * packet_p, gpointer data_p) {
int ret_val = 0;
char * err_msg_conv = (void *) 0;
char * err_msg_dbg = (void *) 0;
char * uname = (void *) 0;
char ** split = (void *) 0;
char * device_id_str = (void *) 0;
axc_address addr = {0};
axc_context * axc_ctx_p = (void *) 0;
char * recipient = (void *) 0;
xmlnode * pubsub_node_p = (void *) 0;
xmlnode * items_node_p = (void *) 0;
int msg_handled = 0;
char * addr_key = (void *) 0;
char * msg_xml = (void *) 0;
xmlnode * msg_node_p = (void *) 0;
lurch_queued_msg * qmsg_p = (lurch_queued_msg *) data_p;
uname = lurch_uname_strip(purple_account_get_username(purple_connection_get_account(js_p->gc)));
recipient = omemo_message_get_recipient_name_bare(qmsg_p->om_msg_p);
if (!from) {
// own user
from = uname;
}
split = g_strsplit(id, "#", 3);
device_id_str = split[1];
purple_debug_info("lurch", "%s: %s received bundle update from %s:%s\n", __func__, uname, from, device_id_str);
addr.name = from;
addr.name_len = strnlen(from, JABBER_MAX_LEN_BARE);
addr.device_id = strtol(device_id_str, (void *) 0, 10);
ret_val = lurch_axc_get_init_ctx(uname, &axc_ctx_p);
if (ret_val) {
err_msg_dbg = "failed to get axc ctx";
goto cleanup;
}
if (type == JABBER_IQ_ERROR) {
err_msg_conv = g_strdup_printf("The device %s owned by %s does not have a bundle and will be skipped. "
"The owner should fix this, or remove the device from the list.", device_id_str, from);
} else {
pubsub_node_p = xmlnode_get_child(packet_p, "pubsub");
if (!pubsub_node_p) {
ret_val = LURCH_ERR;
err_msg_dbg = "no <pubsub> node in response";
goto cleanup;
}
items_node_p = xmlnode_get_child(pubsub_node_p, "items");
if (!items_node_p) {
ret_val = LURCH_ERR;
err_msg_dbg = "no <items> node in response";
goto cleanup;
}
ret_val = axc_session_exists_initiated(&addr, axc_ctx_p);
if (!ret_val) {
ret_val = lurch_bundle_create_session(uname, from, items_node_p, axc_ctx_p);
if (ret_val) {
err_msg_dbg = "failed to create a session";
goto cleanup;
}
} else if (ret_val < 0) {
err_msg_dbg = "failed to check if session exists";
goto cleanup;
}
}
addr_key = lurch_queue_make_key_string_s(from, device_id_str);
if (!addr_key) {
err_msg_dbg = "failed to make a key string";
ret_val = LURCH_ERR;
goto cleanup;
}
(void) g_hash_table_replace(qmsg_p->sess_handled_p, addr_key, addr_key);
if (lurch_queued_msg_is_handled(qmsg_p)) {
msg_handled = 1;
}
if (msg_handled) {
ret_val = lurch_msg_encrypt_for_addrs(qmsg_p->om_msg_p, qmsg_p->recipient_addr_l_p, axc_ctx_p);
if (ret_val) {
err_msg_dbg = "failed to encrypt the symmetric key";
goto cleanup;
}
ret_val = omemo_message_export_encrypted(qmsg_p->om_msg_p, OMEMO_ADD_MSG_EME, &msg_xml);
if (ret_val) {
err_msg_dbg = "failed to export the message to xml";
goto cleanup;
}
msg_node_p = xmlnode_from_str(msg_xml, -1);
if (!msg_node_p) {
err_msg_dbg = "failed to parse xml from string";
ret_val = LURCH_ERR;
goto cleanup;
}
purple_debug_info("lurch", "sending encrypted msg\n");
purple_signal_emit(purple_plugins_find_with_id("prpl-jabber"), "jabber-sending-xmlnode", js_p->gc, &msg_node_p);
lurch_queued_msg_destroy(qmsg_p);
}
cleanup:
if (err_msg_conv) {
purple_conv_present_error(recipient, purple_connection_get_account(js_p->gc), err_msg_conv);
g_free(err_msg_conv);
}
if (err_msg_dbg) {
purple_conv_present_error(recipient, purple_connection_get_account(js_p->gc), LURCH_ERR_STRING_ENCRYPT);
purple_debug_error("lurch", "%s: %s (%i)\n", __func__, err_msg_dbg, ret_val);
}
free(uname);
g_strfreev(split);
axc_context_destroy_all(axc_ctx_p);
free(addr_key);
free(recipient);
free(msg_xml);
if (msg_node_p) {
xmlnode_free(msg_node_p);
}
}
/**
* Requests a bundle.
*
* @param js_p Pointer to the JabberStream to use.
* @param to The recipient of this request.
* @param device_id The ID of the device whose bundle is needed.
* @param qmsg_p Pointer to the queued message waiting on (at least) this bundle.
* @return 0 on success, negative on error.
*/
static int lurch_bundle_request_do(JabberStream * js_p,
const char * to,
uint32_t device_id,
lurch_queued_msg * qmsg_p) {
int ret_val = 0;
JabberIq * jiq_p = (void *) 0;
xmlnode * pubsub_node_p = (void *) 0;
char * device_id_str = (void *) 0;
char * rand_str = (void *) 0;
char * req_id = (void *) 0;
char * bundle_node_name = (void *) 0;
xmlnode * items_node_p = (void *) 0;
purple_debug_info("lurch", "%s: %s is requesting bundle from %s:%i\n", __func__,
purple_account_get_username(purple_connection_get_account(js_p->gc)), to, device_id);
jiq_p = jabber_iq_new(js_p, JABBER_IQ_GET);
xmlnode_set_attrib(jiq_p->node, "to", to);
pubsub_node_p = xmlnode_new_child(jiq_p->node, "pubsub");
xmlnode_set_namespace(pubsub_node_p, "http://jabber.org/protocol/pubsub");
device_id_str = g_strdup_printf("%i", device_id);
rand_str = g_strdup_printf("%i", g_random_int());
req_id = g_strconcat(to, "#", device_id_str, "#", rand_str, NULL);
ret_val = omemo_bundle_get_pep_node_name(device_id, &bundle_node_name);
if (ret_val) {
purple_debug_error("lurch", "%s: failed to get bundle pep node name for %s:%i\n", __func__, to, device_id);
goto cleanup;
}
items_node_p = xmlnode_new_child(pubsub_node_p, "items");
xmlnode_set_attrib(items_node_p, "node", bundle_node_name);
xmlnode_set_attrib(items_node_p, "max_items", "1");
jabber_iq_set_id(jiq_p, req_id);
jabber_iq_set_callback(jiq_p, lurch_bundle_request_cb, qmsg_p);
jabber_iq_send(jiq_p);
purple_debug_info("lurch", "%s: ...request sent\n", __func__);
cleanup:
g_free(device_id_str);
g_free(rand_str);
g_free(req_id);
g_free(bundle_node_name);
return ret_val;
}
/**
* A JabberPEPHandler function.
* When a prekey message containing an invalid key ID is received,
* the bundle of the sender is requested and a KeyTransport message is sent
* in response so that a session can still be established.
*/
static void lurch_pep_bundle_for_keytransport(JabberStream * js_p, const char * from, xmlnode * items_p) {
int ret_val = 0;
char * err_msg_dbg = (void *) 0;
char * uname = (void *) 0;
axc_context * axc_ctx_p = (void *) 0;
uint32_t own_id = 0;
omemo_message * msg_p = (void *) 0;
axc_address addr = {0};
lurch_addr laddr = {0};
axc_buf * key_ct_buf_p = (void *) 0;
char * msg_xml = (void *) 0;
xmlnode * msg_node_p = (void *) 0;
void * jabber_handle_p = purple_plugins_find_with_id("prpl-jabber");
uname = lurch_uname_strip(purple_account_get_username(purple_connection_get_account(js_p->gc)));
addr.name = from;
addr.name_len = strnlen(from, JABBER_MAX_LEN_BARE);
addr.device_id = lurch_bundle_name_get_device_id(xmlnode_get_attrib(items_p, "node"));
purple_debug_info("lurch", "%s: %s received bundle from %s:%i\n", __func__, uname, from, addr.device_id);
laddr.jid = g_strndup(addr.name, addr.name_len);
laddr.device_id = addr.device_id;
ret_val = lurch_axc_get_init_ctx(uname, &axc_ctx_p);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to init axc ctx");
goto cleanup;
}
// make sure it's gonna be a pre_key_message
ret_val = axc_session_delete(addr.name, addr.device_id, axc_ctx_p);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to delete possibly existing session");
goto cleanup;
}
ret_val = lurch_bundle_create_session(uname, from, items_p, axc_ctx_p);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to create session");
goto cleanup;
}
purple_debug_info("lurch", "%s: %s created session with %s:%i\n", __func__, uname, from, addr.device_id);
ret_val = axc_get_device_id(axc_ctx_p, &own_id);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to get own device id");
goto cleanup;
}
ret_val = omemo_message_create(own_id, &crypto, &msg_p);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to create omemo msg");
goto cleanup;
}
ret_val = lurch_key_encrypt(&laddr,
omemo_message_get_key(msg_p),
omemo_message_get_key_len(msg_p),
axc_ctx_p,
&key_ct_buf_p);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to encrypt key for %s:%i", addr.name, addr.device_id);
goto cleanup;
}
ret_val = omemo_message_add_recipient(msg_p,
addr.device_id,
axc_buf_get_data(key_ct_buf_p),
axc_buf_get_len(key_ct_buf_p));
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to add %s:%i as recipient to message", addr.name, addr.device_id);
goto cleanup;
}
ret_val = omemo_message_export_encrypted(msg_p, OMEMO_ADD_MSG_NONE, &msg_xml);
if (ret_val) {
err_msg_dbg = g_strdup_printf("failed to export encrypted msg");
goto cleanup;
}
msg_node_p = xmlnode_from_str(msg_xml, -1);
if (!msg_node_p) {
err_msg_dbg = g_strdup_printf("failed to create xml node from xml string");
goto cleanup;
}
purple_signal_emit(jabber_handle_p, "jabber-sending-xmlnode", js_p->gc, &msg_node_p);
purple_debug_info("lurch", "%s: %s sent keytransportmsg to %s:%i\n", __func__, uname, from, addr.device_id);
cleanup:
if (err_msg_dbg) {
purple_debug_error("lurch", "%s: %s (%i)\n", __func__, err_msg_dbg, ret_val);
free(err_msg_dbg);
}
free(laddr.jid);
free(uname);
axc_context_destroy_all(axc_ctx_p);
omemo_message_destroy(msg_p);
axc_buf_free(key_ct_buf_p);
free(msg_xml);
if (msg_node_p) {
xmlnode_free(msg_node_p);
}
}
/**
* Processes a devicelist by updating the database with it.
*
* @param uname The username.
* @param dl_in_p Pointer to the incoming devicelist.
* @param js_p Pointer to the JabberStream.
* @return 0 on success, negative on error.
*/
static int lurch_devicelist_process(char * uname, omemo_devicelist * dl_in_p, JabberStream * js_p) {
int ret_val = 0;
char * err_msg_dbg = (void *) 0;
const char * from = (void *) 0;
char * db_fn_omemo = (void *) 0;
axc_context * axc_ctx_p = (void *) 0;
omemo_devicelist * dl_db_p = (void *) 0;
GList * add_l_p = (void *) 0;
GList * del_l_p = (void *) 0;