-
Notifications
You must be signed in to change notification settings - Fork 4
/
omemo.cpp
2399 lines (2054 loc) · 84.6 KB
/
omemo.cpp
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <fmt/core.h>
#include <memory>
#include <numeric>
#include <sstream>
#include <string>
#include <string_view>
#include <stdlib.h>
#include <stdint.h>
#include <sys/param.h>
#include <time.h>
#include <math.h>
#include <limits.h>
#include <optional>
#include <ranges>
#include <filesystem>
#include <strophe.h>
#include <weechat/weechat-plugin.h>
#include "plugin.hh"
#include "xmpp/stanza.hh"
#include "account.hh"
#include "omemo.hh"
#include "gcrypt.hh"
#include "util.hh"
using namespace weechat::xmpp;
using t_omemo = omemo;
#define mdb_val_str(s) { \
.mv_size = strlen(s), .mv_data = (char*)s \
}
#define mdb_val_intptr(i) { \
.mv_size = sizeof(*i), .mv_data = i \
}
#define mdb_val_sizeof(t) { \
.mv_size = sizeof(t), .mv_data = NULL \
}
#define PRE_KEY_START 1
#define PRE_KEY_COUNT 100
#define AES_KEY_SIZE (16)
#define AES_IV_SIZE (12)
const char *OMEMO_ADVICE = "[OMEMO encrypted message (XEP-0384)]";
size_t base64_decode(const char *buffer, size_t length, uint8_t **result)
{
*result = (uint8_t*)calloc(length + 1, sizeof(uint8_t));
return weechat_string_base_decode(64, buffer, (char*)*result);
}
size_t base64_encode(const uint8_t *buffer, size_t length, char **result)
{
*result = (char*)calloc(length * 2, sizeof(char));
return weechat_string_base_encode(64, (char*)buffer, length, *result);
}
std::vector<std::uint8_t> base64_decode(std::string_view buffer)
{
auto result = std::make_unique<std::uint8_t[]>(buffer.size() + 1);
return std::vector<std::uint8_t>(result.get(), result.get() + weechat_string_base_decode(64, buffer.data(), (char*)result.get()));
}
std::string base64_encode(std::vector<std::uint8_t> buffer)
{
auto result = std::make_unique<char[]>(buffer.size() * 2);
return std::string(result.get(), result.get() + weechat_string_base_encode(64, (char*)buffer.data(), buffer.size(), result.get()));
}
int aes_decrypt(const uint8_t *ciphertext, size_t ciphertext_len,
uint8_t *key, uint8_t *iv, uint8_t *tag, size_t tag_len,
uint8_t **plaintext, size_t *plaintext_len)
{
gcry_cipher_hd_t cipher = NULL;
if (gcry_cipher_open(&cipher, GCRY_CIPHER_AES128,
GCRY_CIPHER_MODE_GCM, GCRY_CIPHER_SECURE)) goto cleanup;
if (gcry_cipher_setkey(cipher, key, AES_KEY_SIZE)) goto cleanup;
if (gcry_cipher_setiv(cipher, iv, AES_IV_SIZE)) goto cleanup;
*plaintext_len = ciphertext_len;
*plaintext = (uint8_t*)malloc((sizeof(uint8_t) * *plaintext_len) + 1);
if (gcry_cipher_decrypt(cipher, *plaintext, *plaintext_len,
ciphertext, ciphertext_len)) goto cleanup;
if (gcry_cipher_checktag(cipher, tag, tag_len)) goto cleanup;
gcry_cipher_close(cipher);
return 1;
cleanup:
gcry_cipher_close(cipher);
return 0;
}
int aes_encrypt(const uint8_t *plaintext, size_t plaintext_len,
uint8_t **key, uint8_t **iv, uint8_t **tag, size_t *tag_len,
uint8_t **ciphertext, size_t *ciphertext_len)
{
*tag_len = 16;
*tag = (uint8_t*)calloc(*tag_len, sizeof(uint8_t));
*iv = (uint8_t*)gcry_random_bytes(AES_IV_SIZE, GCRY_STRONG_RANDOM);
*key = (uint8_t*)gcry_random_bytes(AES_KEY_SIZE, GCRY_STRONG_RANDOM);
gcry_cipher_hd_t cipher = NULL;
if (gcry_cipher_open(&cipher, GCRY_CIPHER_AES128,
GCRY_CIPHER_MODE_GCM, GCRY_CIPHER_SECURE)) goto cleanup;
if (gcry_cipher_setkey(cipher, *key, AES_KEY_SIZE)) goto cleanup;
if (gcry_cipher_setiv(cipher, *iv, AES_IV_SIZE)) goto cleanup;
*ciphertext_len = plaintext_len;
*ciphertext = (uint8_t*)malloc((sizeof(uint8_t) * *ciphertext_len) + 1);
if (gcry_cipher_encrypt(cipher, *ciphertext, *ciphertext_len,
plaintext, plaintext_len)) goto cleanup;
if (gcry_cipher_gettag(cipher, *tag, *tag_len)) goto cleanup;
gcry_cipher_close(cipher);
return 1;
cleanup:
gcry_cipher_close(cipher);
return 0;
}
void signal_protocol_address_free(signal_protocol_address* ptr) {
if (!ptr)
return;
if (ptr->name) {
free((void*)ptr->name);
}
return free(ptr);
}
void signal_protocol_address_set_name(signal_protocol_address* self, const char* name) {
if (!self)
return;
if (!name)
return;
char* n = (char*)malloc(strlen(name)+1);
memcpy(n, name, strlen(name));
n[strlen(name)] = 0;
if (self->name) {
free((void*)self->name);
}
self->name = n;
self->name_len = strlen(n);
}
char* signal_protocol_address_get_name(signal_protocol_address* self) {
if (!self)
return NULL;
if (!self->name)
return 0;
char* res = (char*)malloc(sizeof(char) * (self->name_len + 1));
memcpy(res, self->name, self->name_len);
res[self->name_len] = 0;
return res;
}
int32_t signal_protocol_address_get_device_id(signal_protocol_address* self) {
if (!self)
return -1;
return self->device_id;
}
void signal_protocol_address_set_device_id(signal_protocol_address* self, int32_t device_id) {
if (!self)
return;
self->device_id = device_id;
}
signal_protocol_address* signal_protocol_address_new(const char* name, int32_t device_id) {
if (!name)
return NULL;
signal_protocol_address* address = (signal_protocol_address*)malloc(sizeof(signal_protocol_address));
address->device_id = -1;
address->name = NULL;
signal_protocol_address_set_name(address, name);
signal_protocol_address_set_device_id(address, device_id);
return address;
}
int aes_cipher(int cipher, size_t key_len, int* algo, int* mode) {
switch (key_len) {
case 16:
*algo = GCRY_CIPHER_AES128;
break;
case 24:
*algo = GCRY_CIPHER_AES192;
break;
case 32:
*algo = GCRY_CIPHER_AES256;
break;
default:
return SG_ERR_UNKNOWN;
}
switch (cipher) {
case SG_CIPHER_AES_CBC_PKCS5:
*mode = GCRY_CIPHER_MODE_CBC;
break;
case SG_CIPHER_AES_CTR_NOPADDING:
*mode = GCRY_CIPHER_MODE_CTR;
break;
default:
return SG_ERR_UNKNOWN;
}
return SG_SUCCESS;
}
void lock_function(void *user_data)
{
(void) user_data;
}
void unlock_function(void *user_data)
{
(void) user_data;
}
int cp_randomize(uint8_t *data, size_t len) {
gcry_randomize(data, len, GCRY_STRONG_RANDOM);
return SG_SUCCESS;
}
int cp_random_generator(uint8_t *data, size_t len, void *) {
gcry_randomize(data, len, GCRY_STRONG_RANDOM);
return SG_SUCCESS;
}
int cp_hmac_sha256_init(void **hmac_context, const uint8_t *key, size_t key_len, void *) {
gcry_mac_hd_t* ctx = (gcry_mac_hd_t*)malloc(sizeof(gcry_mac_hd_t));
if (!ctx) return SG_ERR_NOMEM;
if (gcry_mac_open(ctx, GCRY_MAC_HMAC_SHA256, 0, 0)) {
free(ctx);
return SG_ERR_UNKNOWN;
}
if (gcry_mac_setkey(*ctx, key, key_len)) {
free(ctx);
return SG_ERR_UNKNOWN;
}
*hmac_context = ctx;
return SG_SUCCESS;
}
int cp_hmac_sha256_update(void *hmac_context, const uint8_t *data, size_t data_len, void *) {
gcry_mac_hd_t* ctx = (gcry_mac_hd_t*)hmac_context;
if (gcry_mac_write(*ctx, data, data_len)) return SG_ERR_UNKNOWN;
return SG_SUCCESS;
}
int cp_hmac_sha256_final(void *hmac_context, struct signal_buffer **output, void *) {
size_t len = gcry_mac_get_algo_maclen(GCRY_MAC_HMAC_SHA256);
auto md = std::unique_ptr<uint8_t[]>(new uint8_t[len]);
gcry_mac_hd_t* ctx = (gcry_mac_hd_t*)hmac_context;
if (gcry_mac_read(*ctx, md.get(), &len)) return SG_ERR_UNKNOWN;
struct signal_buffer *output_buffer = signal_buffer_create(md.get(), len);
if (!output_buffer) return SG_ERR_NOMEM;
*output = output_buffer;
return SG_SUCCESS;
}
void cp_hmac_sha256_cleanup(void *hmac_context, void *) {
gcry_mac_hd_t* ctx = (gcry_mac_hd_t*)hmac_context;
if (ctx) {
gcry_mac_close(*ctx);
free(ctx);
}
}
int cp_sha512_digest_init(void **digest_context, void *) {
gcry_md_hd_t* ctx = (gcry_md_hd_t*)malloc(sizeof(gcry_mac_hd_t));
if (!ctx) return SG_ERR_NOMEM;
if (gcry_md_open(ctx, GCRY_MD_SHA512, 0)) {
free(ctx);
return SG_ERR_UNKNOWN;
}
*digest_context = ctx;
return SG_SUCCESS;
}
int cp_sha512_digest_update(void *digest_context, const uint8_t *data, size_t data_len, void *) {
gcry_md_hd_t* ctx = (gcry_md_hd_t*)digest_context;
gcry_md_write(*ctx, data, data_len);
return SG_SUCCESS;
}
int cp_sha512_digest_final(void *digest_context, struct signal_buffer **output, void *) {
size_t len = gcry_md_get_algo_dlen(GCRY_MD_SHA512);
gcry_md_hd_t* ctx = (gcry_md_hd_t*)digest_context;
uint8_t* md = gcry_md_read(*ctx, GCRY_MD_SHA512);
if (!md) return SG_ERR_UNKNOWN;
gcry_md_reset(*ctx);
struct signal_buffer *output_buffer = signal_buffer_create(md, len);
free(md);
if (!output_buffer) return SG_ERR_NOMEM;
*output = output_buffer;
return SG_SUCCESS;
}
void cp_sha512_digest_cleanup(void *digest_context, void *) {
gcry_md_hd_t* ctx = (gcry_md_hd_t*)digest_context;
if (ctx) {
gcry_md_close(*ctx);
free(ctx);
}
}
int cp_encrypt(struct signal_buffer **output,
int cipher,
const uint8_t *key, size_t key_len,
const uint8_t *iv, size_t iv_len,
const uint8_t *plaintext, size_t plaintext_len,
void *) {
int algo, mode, error_code = SG_ERR_UNKNOWN;
if (aes_cipher(cipher, key_len, &algo, &mode)) return SG_ERR_INVAL;
gcry_cipher_hd_t ctx = {0};
if (gcry_cipher_open(&ctx, algo, mode, 0)) return SG_ERR_NOMEM;
signal_buffer* padded = 0;
signal_buffer* out_buf = 0;
goto no_error;
error:
gcry_cipher_close(ctx);
if (padded != 0) {
signal_buffer_bzero_free(padded);
}
if (out_buf != 0) {
signal_buffer_free(out_buf);
}
return error_code;
no_error:
if (gcry_cipher_setkey(ctx, key, key_len)) goto error;
uint8_t tag_len = 0, pad_len = 0;
switch (cipher) {
case SG_CIPHER_AES_CBC_PKCS5:
if (gcry_cipher_setiv(ctx, iv, iv_len)) goto error;
pad_len = 16 - (plaintext_len % 16);
if (pad_len == 0) pad_len = 16;
break;
case SG_CIPHER_AES_CTR_NOPADDING:
if (gcry_cipher_setctr(ctx, iv, iv_len)) goto error;
break;
default:
return SG_ERR_UNKNOWN;
}
size_t padded_len = plaintext_len + pad_len;
padded = signal_buffer_alloc(padded_len);
if (padded == 0) {
error_code = SG_ERR_NOMEM;
goto error;
}
memset(signal_buffer_data(padded) + plaintext_len, pad_len, pad_len);
memcpy(signal_buffer_data(padded), plaintext, plaintext_len);
out_buf = signal_buffer_alloc(padded_len + tag_len);
if (out_buf == 0) {
error_code = SG_ERR_NOMEM;
goto error;
}
if (gcry_cipher_encrypt(ctx, signal_buffer_data(out_buf), padded_len, signal_buffer_data(padded), padded_len)) goto error;
if (tag_len > 0) {
if (gcry_cipher_gettag(ctx, signal_buffer_data(out_buf) + padded_len, tag_len)) goto error;
}
*output = out_buf;
out_buf = 0;
signal_buffer_bzero_free(padded);
padded = 0;
gcry_cipher_close(ctx);
return SG_SUCCESS;
}
int cp_decrypt(struct signal_buffer **output,
int cipher,
const uint8_t *key, size_t key_len,
const uint8_t *iv, size_t iv_len,
const uint8_t *ciphertext, size_t ciphertext_len,
void *) {
int algo, mode, error_code = SG_ERR_UNKNOWN;
*output = 0;
if (aes_cipher(cipher, key_len, &algo, &mode)) return SG_ERR_INVAL;
if (ciphertext_len == 0) return SG_ERR_INVAL;
gcry_cipher_hd_t ctx = {0};
if (gcry_cipher_open(&ctx, algo, mode, 0)) return SG_ERR_NOMEM;
signal_buffer* out_buf = 0;
goto no_error;
error:
gcry_cipher_close(ctx);
if (out_buf != 0) {
signal_buffer_bzero_free(out_buf);
}
return error_code;
no_error:
if (gcry_cipher_setkey(ctx, key, key_len)) goto error;
uint8_t tag_len = 0, pkcs_pad = 0;
switch (cipher) {
case SG_CIPHER_AES_CBC_PKCS5:
if (gcry_cipher_setiv(ctx, iv, iv_len)) goto error;
pkcs_pad = 1;
break;
case SG_CIPHER_AES_CTR_NOPADDING:
if (gcry_cipher_setctr(ctx, iv, iv_len)) goto error;
break;
default:
goto error;
}
size_t padded_len = ciphertext_len - tag_len;
out_buf = signal_buffer_alloc(padded_len);
if (out_buf == 0) {
error_code = SG_ERR_NOMEM;
goto error;
}
if (gcry_cipher_decrypt(ctx, signal_buffer_data(out_buf), signal_buffer_len(out_buf), ciphertext, padded_len)) goto error;
if (tag_len > 0) {
if (gcry_cipher_checktag(ctx, ciphertext + padded_len, tag_len)) goto error;
}
if (pkcs_pad) {
uint8_t pad_len = signal_buffer_data(out_buf)[padded_len - 1];
if (pad_len > 16 || pad_len > padded_len) goto error;
*output = signal_buffer_create(signal_buffer_data(out_buf), padded_len - pad_len);
signal_buffer_bzero_free(out_buf);
out_buf = 0;
} else {
*output = out_buf;
out_buf = 0;
}
gcry_cipher_close(ctx);
return SG_SUCCESS;
}
int iks_get_identity_key_pair(struct signal_buffer **public_data, signal_buffer **private_data, void *user_data)
{
auto omemo = reinterpret_cast<t_omemo*>(user_data);
MDB_txn *transaction = NULL;
MDB_val k_local_private_key = mdb_val_str("local_private_key");
MDB_val k_local_public_key = mdb_val_str("local_public_key");
MDB_val v_local_private_key, v_local_public_key;
if (mdb_txn_begin(omemo->db_env, NULL, MDB_RDONLY, &transaction)) {
weechat_printf(NULL, "%sxmpp: failed to open lmdb transaction",
weechat_prefix("error"));
return -1;
}
if (!mdb_get(transaction, omemo->dbi.omemo,
&k_local_private_key, &v_local_private_key) &&
!mdb_get(transaction, omemo->dbi.omemo,
&k_local_public_key, &v_local_public_key))
{
*private_data = signal_buffer_create((const uint8_t*)v_local_private_key.mv_data, v_local_private_key.mv_size);
*public_data = signal_buffer_create((const uint8_t*)v_local_public_key.mv_data, v_local_public_key.mv_size);
if (mdb_txn_commit(transaction)) {
weechat_printf(NULL, "%sxmpp: failed to write lmdb transaction",
weechat_prefix("error"));
goto cleanup;
};
}
else
{
auto identity = libsignal::identity_key_pair::generate(omemo->context);
auto private_key = identity.get_private();
auto public_key = identity.get_public();
ec_private_key_serialize(private_data, private_key);
ec_public_key_serialize(public_data, public_key);
v_local_private_key.mv_data = signal_buffer_data(*private_data);
v_local_private_key.mv_size = signal_buffer_len(*private_data);
v_local_public_key.mv_data = signal_buffer_data(*public_data);
v_local_public_key.mv_size = signal_buffer_len(*public_data);
mdb_txn_abort(transaction);
if (mdb_txn_begin(omemo->db_env, NULL, 0, &transaction)) {
weechat_printf(NULL, "%sxmpp: failed to open lmdb transaction",
weechat_prefix("error"));
return -1;
}
if (mdb_put(transaction, omemo->dbi.omemo,
&k_local_private_key, &v_local_private_key, MDB_NOOVERWRITE) ||
mdb_put(transaction, omemo->dbi.omemo,
&k_local_public_key, &v_local_public_key, MDB_NOOVERWRITE))
{
weechat_printf(NULL, "%sxmpp: failed to write lmdb value",
weechat_prefix("error"));
goto cleanup;
};
if (mdb_txn_commit(transaction)) {
weechat_printf(NULL, "%sxmpp: failed to write lmdb transaction",
weechat_prefix("error"));
goto cleanup;
};
*private_data = signal_buffer_create((const uint8_t*)v_local_private_key.mv_data,
v_local_private_key.mv_size);
*public_data = signal_buffer_create((const uint8_t*)v_local_public_key.mv_data,
v_local_public_key.mv_size);
omemo->identity = std::move(identity);
}
return 0;
cleanup:
mdb_txn_abort(transaction);
return -1;
}
int iks_get_local_registration_id(void *user_data, uint32_t *registration_id)
{
auto omemo = reinterpret_cast<t_omemo*>(user_data);
MDB_txn *transaction = NULL;
MDB_val k_local_registration_id = mdb_val_str("local_registration_id");
MDB_val v_local_registration_id = mdb_val_sizeof(uint32_t);
// Return the local client's registration ID
if (mdb_txn_begin(omemo->db_env, NULL, MDB_RDONLY, &transaction)) {
weechat_printf(NULL, "%sxmpp: failed to open lmdb transaction",
weechat_prefix("error"));
return -1;
}
if (!mdb_get(transaction, omemo->dbi.omemo,
&k_local_registration_id,
&v_local_registration_id))
{
*registration_id = *(uint32_t*)v_local_registration_id.mv_data;
if (mdb_txn_commit(transaction)) {
weechat_printf(NULL, "%sxmpp: failed to read lmdb transaction",
weechat_prefix("error"));
goto cleanup;
};
}
else
{
uint32_t generated_id;
signal_protocol_key_helper_generate_registration_id(
&generated_id, 0, omemo->context);
v_local_registration_id.mv_data = &generated_id;
mdb_txn_abort(transaction);
if (mdb_txn_begin(omemo->db_env, NULL, 0, &transaction)) {
weechat_printf(NULL, "%sxmpp: failed to open lmdb transaction",
weechat_prefix("error"));
return -1;
}
if (mdb_put(transaction, omemo->dbi.omemo,
&k_local_registration_id,
&v_local_registration_id, MDB_NOOVERWRITE))
{
weechat_printf(NULL, "%sxmpp: failed to write lmdb value",
weechat_prefix("error"));
goto cleanup;
};
if (mdb_txn_commit(transaction)) {
weechat_printf(NULL, "%sxmpp: failed to write lmdb transaction",
weechat_prefix("error"));
goto cleanup;
};
*registration_id = generated_id;
}
return 0;
cleanup:
mdb_txn_abort(transaction);
return -1;
}
int iks_save_identity(const struct signal_protocol_address *address, uint8_t *key_data, size_t key_len, void *user_data)
{
auto omemo = reinterpret_cast<t_omemo*>(user_data);
MDB_txn *transaction = NULL;
MDB_val k_identity_key = {
.mv_size = strlen("identity_key_") + address->name_len
+ 1 + 10,
.mv_data = NULL,
};
MDB_val v_identity_key = {.mv_size = key_len, .mv_data = key_data};
k_identity_key.mv_data = malloc(sizeof(char) * (
k_identity_key.mv_size + 1));
k_identity_key.mv_size =
snprintf((char*)k_identity_key.mv_data, k_identity_key.mv_size + 1,
"identity_key_%s_%u", address->name, address->device_id);
if (mdb_txn_begin(omemo->db_env, NULL, 0, &transaction)) {
weechat_printf(NULL, "%sxmpp: failed to open lmdb transaction",
weechat_prefix("error"));
return -1;
}
if (mdb_put(transaction, omemo->dbi.omemo, &k_identity_key,
&v_identity_key, 0)) {
weechat_printf(NULL, "%sxmpp: failed to write lmdb value",
weechat_prefix("error"));
goto cleanup;
};
if (mdb_txn_commit(transaction)) {
weechat_printf(NULL, "%sxmpp: failed to write lmdb transaction",
weechat_prefix("error"));
goto cleanup;
};
return 0;
cleanup:
mdb_txn_abort(transaction);
return -1;
}
int iks_is_trusted_identity(const struct signal_protocol_address *address, uint8_t *key_data, size_t key_len, void *user_data)
{
auto omemo = reinterpret_cast<t_omemo*>(user_data);
MDB_txn *transaction = NULL;
MDB_val k_identity_key = {
.mv_size = strlen("identity_key_") + address->name_len
+ 1 + 10,
.mv_data = NULL,
};
MDB_val v_identity_key = {.mv_size = key_len, .mv_data = key_data};
int trusted = 1;
k_identity_key.mv_data = malloc(sizeof(char) * (
k_identity_key.mv_size + 1));
k_identity_key.mv_size =
snprintf((char*)k_identity_key.mv_data, k_identity_key.mv_size + 1,
"identity_key_%s_%u", address->name, address->device_id);
if (mdb_txn_begin(omemo->db_env, NULL, MDB_RDONLY, &transaction)) {
weechat_printf(NULL, "%sxmpp: failed to open lmdb transaction",
weechat_prefix("error"));
return -1;
}
if (mdb_get(transaction, omemo->dbi.omemo, &k_identity_key,
&v_identity_key)) {
weechat_printf(NULL, "%sxmpp: failed to read lmdb value",
weechat_prefix("error"));
goto cleanup;
};
if (v_identity_key.mv_size != key_len ||
memcmp(v_identity_key.mv_data, key_data, key_len) != 0)
trusted = 0;
if (mdb_txn_commit(transaction)) {
weechat_printf(NULL, "%sxmpp: failed to write lmdb transaction",
weechat_prefix("error"));
goto cleanup;
};
return 1 | trusted;
cleanup:
mdb_txn_abort(transaction);
return -1;
}
void iks_destroy_func(void *user_data)
{
auto omemo = reinterpret_cast<t_omemo*>(user_data);
(void) omemo;
// Function called to perform cleanup when the data store context is being destroyed
}
int pks_store_pre_key(uint32_t pre_key_id, uint8_t *record, size_t record_len, void *user_data)
{
auto omemo = reinterpret_cast<t_omemo*>(user_data);
MDB_txn *transaction = NULL;
MDB_val k_pre_key = {
.mv_size = strlen("pre_key_") + 10, // strlen(UINT32_MAX)
.mv_data = NULL,
};
MDB_val v_pre_key = {.mv_size = record_len, .mv_data = record};
k_pre_key.mv_data = malloc(sizeof(char) * (
k_pre_key.mv_size + 1));
k_pre_key.mv_size =
snprintf((char*)k_pre_key.mv_data, k_pre_key.mv_size + 1,
"pre_key_%-10u", pre_key_id);
if (mdb_txn_begin(omemo->db_env, NULL, 0, &transaction)) {
weechat_printf(NULL, "%sxmpp: failed to open lmdb transaction",
weechat_prefix("error"));
return -1;
}
if (mdb_put(transaction, omemo->dbi.omemo, &k_pre_key,
&v_pre_key, 0)) {
weechat_printf(NULL, "%sxmpp: failed to write lmdb value",
weechat_prefix("error"));
goto cleanup;
};
if (mdb_txn_commit(transaction)) {
weechat_printf(NULL, "%sxmpp: failed to write lmdb transaction",
weechat_prefix("error"));
goto cleanup;
};
return 0;
cleanup:
mdb_txn_abort(transaction);
return -1;
}
int pks_contains_pre_key(uint32_t pre_key_id, void *user_data)
{
auto omemo = reinterpret_cast<t_omemo*>(user_data);
MDB_txn *transaction = NULL;
MDB_val k_pre_key = {
.mv_size = strlen("pre_key_") + 10, // strlen(UINT32_MAX)
.mv_data = NULL,
};
MDB_val v_pre_key;
k_pre_key.mv_data = malloc(sizeof(char) * (
k_pre_key.mv_size + 1));
k_pre_key.mv_size =
snprintf((char*)k_pre_key.mv_data, k_pre_key.mv_size + 1,
"pre_key_%-10u", pre_key_id);
if (mdb_txn_begin(omemo->db_env, NULL, MDB_RDONLY, &transaction)) {
weechat_printf(NULL, "%sxmpp: failed to open lmdb transaction",
weechat_prefix("error"));
return -1;
}
if (mdb_get(transaction, omemo->dbi.omemo, &k_pre_key,
&v_pre_key)) {
weechat_printf(NULL, "%sxmpp: failed to read lmdb value",
weechat_prefix("error"));
mdb_txn_abort(transaction);
goto cleanup;
};
mdb_txn_abort(transaction);
return 1;
cleanup:
mdb_txn_abort(transaction);
return 0;
}
uint32_t pks_get_count(t_omemo *omemo, int increment)
{
uint32_t count = PRE_KEY_START;
MDB_txn *transaction = NULL;
MDB_val k_pre_key_idx = mdb_val_str("pre_key_idx");
MDB_val v_pre_key_idx = mdb_val_intptr(&count);
if (mdb_txn_begin(omemo->db_env, NULL, 0, &transaction)) {
weechat_printf(NULL, "%sxmpp: failed to open lmdb transaction",
weechat_prefix("error"));
return -1;
}
if (!mdb_get(transaction, omemo->dbi.omemo,
&k_pre_key_idx, &v_pre_key_idx))
{
if (increment)
count += PRE_KEY_COUNT;
}
if (mdb_put(transaction, omemo->dbi.omemo,
&k_pre_key_idx, &v_pre_key_idx, 0))
{
weechat_printf(NULL, "%sxmpp: failed to read lmdb value",
weechat_prefix("error"));
goto cleanup;
};
if (mdb_txn_commit(transaction)) {
weechat_printf(NULL, "%sxmpp: failed to write lmdb transaction",
weechat_prefix("error"));
goto cleanup;
};
return count;
cleanup:
mdb_txn_abort(transaction);
return 0;
}
int pks_load_pre_key(struct signal_buffer **record, uint32_t pre_key_id, void *user_data)
{
auto omemo = reinterpret_cast<t_omemo*>(user_data);
MDB_txn *transaction = NULL;
MDB_val k_pre_key = {
.mv_size = strlen("pre_key_") + 10, // strlen(UINT32_MAX)
.mv_data = NULL,
};
MDB_val v_pre_key;
k_pre_key.mv_data = malloc(sizeof(char) * (
k_pre_key.mv_size + 1));
k_pre_key.mv_size =
snprintf((char*)k_pre_key.mv_data, k_pre_key.mv_size + 1,
"pre_key_%-10u", pre_key_id);
if (mdb_txn_begin(omemo->db_env, NULL, 0, &transaction)) {
weechat_printf(NULL, "%sxmpp: failed to open lmdb transaction",
weechat_prefix("error"));
return -1;
}
if (!mdb_get(transaction, omemo->dbi.omemo,
&k_pre_key, &v_pre_key))
{
*record = signal_buffer_create((const uint8_t*)v_pre_key.mv_data, v_pre_key.mv_size);
if (mdb_txn_commit(transaction)) {
weechat_printf(NULL, "%sxmpp: failed to close lmdb transaction",
weechat_prefix("error"));
goto cleanup;
};
}
else
{
mdb_txn_abort(transaction);
signal_protocol_key_helper_pre_key_list_node *pre_keys_list;
session_pre_key *pre_key = NULL;
for (signal_protocol_key_helper_generate_pre_keys(&pre_keys_list,
pks_get_count(omemo, 1), PRE_KEY_COUNT,
omemo->context); pre_keys_list;
pre_keys_list = signal_protocol_key_helper_key_list_next(pre_keys_list))
{
pre_key = signal_protocol_key_helper_key_list_element(pre_keys_list);
uint32_t id = session_pre_key_get_id(pre_key);
session_pre_key_serialize(record, pre_key);
pks_store_pre_key(id, signal_buffer_data(*record),
signal_buffer_len(*record), user_data);
}
signal_protocol_key_helper_key_list_free(pre_keys_list);
}
return 0;
cleanup:
mdb_txn_abort(transaction);
return -1;
}
int pks_remove_pre_key(uint32_t pre_key_id, void *user_data)
{
auto omemo = reinterpret_cast<t_omemo*>(user_data);
MDB_txn *transaction = NULL;
MDB_val k_pre_key = {
.mv_size = strlen("pre_key_") + 10, // strlen(UINT32_MAX)
.mv_data = NULL,
};
MDB_val v_pre_key;
k_pre_key.mv_data = malloc(sizeof(char) * (
k_pre_key.mv_size + 1));
k_pre_key.mv_size =
snprintf((char*)k_pre_key.mv_data, k_pre_key.mv_size + 1,
"pre_key_%-10u", pre_key_id);
if (mdb_txn_begin(omemo->db_env, NULL, 0, &transaction)) {
weechat_printf(NULL, "%sxmpp: failed to open lmdb transaction",
weechat_prefix("error"));
return -1;
}
if (mdb_del(transaction, omemo->dbi.omemo, &k_pre_key,
&v_pre_key)) {
weechat_printf(NULL, "%sxmpp: failed to erase lmdb value",
weechat_prefix("error"));
goto cleanup;
};
if (mdb_txn_commit(transaction)) {
weechat_printf(NULL, "%sxmpp: failed to close lmdb transaction",
weechat_prefix("error"));
goto cleanup;
};
return 0;
cleanup:
mdb_txn_abort(transaction);
return -1;
}
void pks_destroy_func(void *user_data)
{
auto omemo = reinterpret_cast<t_omemo*>(user_data);
(void) omemo;
// Function called to perform cleanup when the data store context is being destroyed
}
int spks_load_signed_pre_key(struct signal_buffer **record, uint32_t signed_pre_key_id, void *user_data)
{
auto omemo = reinterpret_cast<t_omemo*>(user_data);
MDB_txn *transaction = NULL;
MDB_val k_signed_pre_key = {
.mv_size = strlen("signed_pre_key_") + 10, // strlen(UINT32_MAX)
.mv_data = NULL,
};
MDB_val v_signed_pre_key;
k_signed_pre_key.mv_data = malloc(sizeof(char) * (
k_signed_pre_key.mv_size + 1));
k_signed_pre_key.mv_size =
snprintf((char*)k_signed_pre_key.mv_data, k_signed_pre_key.mv_size + 1,
"signed_pre_key_%-10u", signed_pre_key_id);
if (mdb_txn_begin(omemo->db_env, NULL, 0, &transaction)) {
weechat_printf(NULL, "%sxmpp: failed to open lmdb transaction",
weechat_prefix("error"));
return -1;
}
if (!mdb_get(transaction, omemo->dbi.omemo,
&k_signed_pre_key, &v_signed_pre_key))
{
*record = signal_buffer_create((const uint8_t*)v_signed_pre_key.mv_data, v_signed_pre_key.mv_size);
if (mdb_txn_commit(transaction)) {
weechat_printf(NULL, "%sxmpp: failed to close lmdb transaction",
weechat_prefix("error"));
goto cleanup;
};
}
else
{
session_signed_pre_key *signed_pre_key = NULL;
struct signal_buffer *serialized_key = NULL;
signal_protocol_key_helper_generate_signed_pre_key(&signed_pre_key, omemo->identity, signed_pre_key_id, time(NULL), omemo->context);
session_signed_pre_key_serialize(&serialized_key, signed_pre_key);
v_signed_pre_key.mv_data = signal_buffer_data(serialized_key);
v_signed_pre_key.mv_size = signal_buffer_len(serialized_key);
if (mdb_put(transaction, omemo->dbi.omemo,
&k_signed_pre_key, &v_signed_pre_key, MDB_NOOVERWRITE))
{
weechat_printf(NULL, "%sxmpp: failed to read lmdb value",
weechat_prefix("error"));
goto cleanup;
};
if (mdb_txn_commit(transaction)) {
weechat_printf(NULL, "%sxmpp: failed to write lmdb transaction",
weechat_prefix("error"));
goto cleanup;
};
*record = serialized_key;
}
return 0;
cleanup:
mdb_txn_abort(transaction);