-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_api.c
1255 lines (984 loc) · 45.4 KB
/
test_api.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
/*
* This file is part of the Off-the-Record Next Generation Messaging
* library (libotr-ng).
*
* Copyright (C) 2016-2019, the libotr-ng contributors.
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef S_SPLINT_S
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wstrict-prototypes"
#include <libotr/b64.h>
#include <libotr/privkey.h>
#pragma clang diagnostic pop
#endif
#include <glib.h>
#include <string.h>
#include "test_fixtures.h"
#include "test_helpers.h"
#include "list.h"
#include "otrng.h"
#include "str.h"
static void test_api_interactive_conversation(void) {
otrng_client_s *alice_client = otrng_client_new(ALICE_IDENTITY);
otrng_client_s *bob_client = otrng_client_new(BOB_IDENTITY);
otrng_s *alice = set_up(alice_client, 1);
otrng_s *bob = set_up(bob_client, 2);
otrng_client_set_padding(256, alice_client);
otrng_client_set_padding(256, bob_client);
// DAKE has finished
do_dake_fixture(alice, bob);
int message_id;
otrng_response_s *response_to_bob = NULL;
otrng_response_s *response_to_alice = NULL;
string_p to_send = NULL;
otrng_result result;
for (message_id = 1; message_id < 4; message_id++) {
// Alice sends a data message
result = otrng_send_message(&to_send, "hi", NULL, 0, alice);
assert_message_sent(result, to_send);
otrng_assert(!alice->keys->old_mac_keys);
g_assert_cmpint(alice->keys->i, ==, 1);
g_assert_cmpint(alice->keys->j, ==, message_id + 1);
g_assert_cmpint(alice->keys->k, ==, 0);
g_assert_cmpint(alice->keys->pn, ==, 0);
// Alice should not delete priv keys
otrng_assert_not_zero(alice->keys->our_ecdh->priv, ED448_SCALAR_BYTES);
otrng_assert(alice->keys->our_dh->priv);
// Bob receives a data message
response_to_alice = otrng_response_new();
result = otrng_receive_message(response_to_alice, to_send, bob);
assert_message_rec(result, "hi", response_to_alice);
otrng_assert(bob->keys->old_mac_keys);
free_message_and_response(response_to_alice, &to_send);
g_assert_cmpint(otrng_list_len(bob->keys->old_mac_keys), ==,
message_id + 1);
g_assert_cmpint(bob->keys->i, ==, 1);
g_assert_cmpint(bob->keys->j, ==, 0);
g_assert_cmpint(bob->keys->k, ==, message_id + 1);
g_assert_cmpint(bob->keys->pn, ==, 0);
// Bob's priv key should be deleted
otrng_assert_zero(bob->keys->our_ecdh->priv, ED448_SCALAR_BYTES);
otrng_assert(!bob->keys->our_dh->priv);
}
// Next message Bob sends is a new DH ratchet
for (message_id = 1; message_id < 4; message_id++) {
// Bob sends a data message
result = otrng_send_message(&to_send, "hello", NULL, 0, bob);
assert_message_sent(result, to_send);
g_assert_cmpint(otrng_list_len(bob->keys->old_mac_keys), ==, 0);
g_assert_cmpint(bob->keys->i, ==, 2);
g_assert_cmpint(bob->keys->j, ==, message_id);
g_assert_cmpint(bob->keys->k, ==, 4);
g_assert_cmpint(bob->keys->pn, ==, 0);
// Bob should have a new ECDH priv key but no DH
otrng_assert_not_zero(bob->keys->our_ecdh->priv, ED448_SCALAR_BYTES);
otrng_assert(!bob->keys->our_dh->priv);
// Alice receives a data message
response_to_bob = otrng_response_new();
result = otrng_receive_message(response_to_bob, to_send, alice);
assert_message_rec(result, "hello", response_to_bob);
g_assert_cmpint(otrng_list_len(alice->keys->old_mac_keys), ==, message_id);
free_message_and_response(response_to_bob, &to_send);
g_assert_cmpint(alice->keys->i, ==, 2);
g_assert_cmpint(alice->keys->j, ==, 0);
g_assert_cmpint(alice->keys->k, ==, message_id);
g_assert_cmpint(alice->keys->pn, ==, 4);
// Alice should delete the ECDH priv key but not the DH priv key
otrng_assert_zero(alice->keys->our_ecdh->priv, ED448_SCALAR_BYTES);
otrng_assert(alice->keys->our_dh->priv);
}
const size_t secret_len = 2;
uint8_t secret_data[2] = {0x08, 0x05};
// Bob sends a message with TLV
result = otrng_smp_start(&to_send, NULL, 0, secret_data, secret_len, bob);
assert_message_sent(result, to_send);
g_assert_cmpint(otrng_list_len(bob->keys->old_mac_keys), ==, 0);
// Alice receives a data message with TLV
response_to_bob = otrng_response_new();
otrng_assert_is_success(
otrng_receive_message(response_to_bob, to_send, alice));
g_assert_cmpint(otrng_list_len(alice->keys->old_mac_keys), ==, 4);
// Check TLVs
otrng_assert(response_to_bob->tlvs);
g_assert_cmpint(response_to_bob->tlvs->data->type, ==, OTRNG_TLV_SMP_MSG_1);
g_assert_cmpint(response_to_bob->tlvs->data->len, ==, 342);
// Check Padding
otrng_assert(response_to_bob->tlvs->next);
g_assert_cmpint(response_to_bob->tlvs->next->data->type, ==,
OTRNG_TLV_PADDING);
free_message_and_response(response_to_bob, &to_send);
// Bob closes the encrypted conversation
otrng_close(&to_send, bob);
otrng_assert(bob->state == OTRNG_STATE_START);
// Alice receives a disconnected TLV from Bob
response_to_bob = otrng_response_new();
otrng_receive_message(response_to_bob, to_send, alice);
otrng_assert(response_to_bob->tlvs);
g_assert_cmpint(response_to_bob->tlvs->data->type, ==,
OTRNG_TLV_DISCONNECTED);
otrng_assert(alice->state == OTRNG_STATE_FINISHED);
free_message_and_response(response_to_bob, &to_send);
otrng_global_state_free(alice_client->global_state);
otrng_global_state_free(bob_client->global_state);
otrng_conn_free_all(alice, bob);
}
/* Specifies the behavior of the API for offline messages */
static void test_otrng_send_offline_message() {
otrng_client_s *alice_client = otrng_client_new(ALICE_IDENTITY);
otrng_client_s *bob_client = otrng_client_new(BOB_IDENTITY);
otrng_s *alice = set_up(alice_client, 1);
otrng_s *bob = set_up(bob_client, 2);
g_assert_cmpint(alice->keys->i, ==, 0);
g_assert_cmpint(alice->keys->j, ==, 0);
g_assert_cmpint(alice->keys->k, ==, 0);
g_assert_cmpint(alice->keys->pn, ==, 0);
g_assert_cmpint(bob->keys->i, ==, 0);
g_assert_cmpint(bob->keys->j, ==, 0);
g_assert_cmpint(bob->keys->k, ==, 0);
g_assert_cmpint(bob->keys->pn, ==, 0);
prekey_ensemble_s *ensemble = otrng_build_prekey_ensemble(bob);
otrng_assert(ensemble);
otrng_assert_is_success(otrng_prekey_ensemble_validate(ensemble));
g_assert_cmpint(bob->their_prekeys_id, ==, 0);
otrng_assert(bob->running_version == 0);
g_assert_cmpint(bob->keys->i, ==, 0);
g_assert_cmpint(bob->keys->j, ==, 0);
g_assert_cmpint(bob->keys->k, ==, 0);
g_assert_cmpint(bob->keys->pn, ==, 0);
char *to_bob = NULL;
otrng_assert_is_success(
otrng_send_non_interactive_auth(&to_bob, ensemble, alice));
otrng_prekey_ensemble_free(ensemble);
otrng_assert(to_bob);
otrng_assert_cmpmem("?OTR:AAQN", to_bob, 9);
otrng_assert(alice->state == OTRNG_STATE_ENCRYPTED_MESSAGES);
otrng_assert(alice->running_version == 4);
g_assert_cmpint(alice->keys->i, ==, 0);
g_assert_cmpint(alice->keys->j, ==, 0);
g_assert_cmpint(alice->keys->k, ==, 0);
g_assert_cmpint(alice->keys->pn, ==, 0);
// Bob receives an offline message
otrng_response_s *response = otrng_response_new();
otrng_assert_is_success(otrng_receive_message(response, to_bob, bob));
otrng_free(to_bob);
otrng_assert(!response->to_display);
otrng_assert(!response->to_send);
otrng_response_free_all(response);
g_assert_cmpint(bob->their_prekeys_id, ==, 0);
otrng_assert(bob->state == OTRNG_STATE_WAITING_DAKE_DATA_MESSAGE);
otrng_assert(bob->running_version == 4);
g_assert_cmpint(bob->keys->i, ==, 0);
g_assert_cmpint(bob->keys->j, ==, 0);
g_assert_cmpint(bob->keys->k, ==, 0);
g_assert_cmpint(bob->keys->pn, ==, 0);
otrng_assert_ec_public_key_eq(bob->keys->their_ecdh,
alice->keys->our_ecdh->pub);
otrng_assert_dh_public_key_eq(bob->keys->their_dh, alice->keys->our_dh->pub);
// Both have the same shared shared secret/root key
otrng_assert_root_key_eq(alice->keys->current->root_key,
bob->keys->current->root_key);
otrng_response_s *response_to_bob = NULL;
otrng_response_s *response_to_alice = NULL;
// Bob sends a data message
int message_id;
string_p to_send = NULL;
otrng_result result;
for (message_id = 1; message_id < 4; message_id++) {
result = otrng_send_message(&to_send, "hi", NULL, 0, alice);
assert_message_sent(result, to_send);
otrng_assert(!alice->keys->old_mac_keys);
g_assert_cmpint(alice->keys->i, ==, 1);
g_assert_cmpint(alice->keys->j, ==, message_id);
g_assert_cmpint(alice->keys->k, ==, 0);
g_assert_cmpint(alice->keys->pn, ==, 0);
// Alice receives a data message
response_to_alice = otrng_response_new();
result = otrng_receive_message(response_to_alice, to_send, bob);
assert_message_rec(result, "hi", response_to_alice);
otrng_assert(bob->keys->old_mac_keys);
g_assert_cmpint(otrng_list_len(bob->keys->old_mac_keys), ==, message_id);
g_assert_cmpint(bob->keys->i, ==, 1);
g_assert_cmpint(bob->keys->j, ==, 0);
g_assert_cmpint(bob->keys->k, ==, message_id);
g_assert_cmpint(bob->keys->pn, ==, 0);
free_message_and_response(response_to_alice, &to_send);
}
for (message_id = 1; message_id < 4; message_id++) {
// Alice sends a data message
result = otrng_send_message(&to_send, "hello", NULL, 0, bob);
assert_message_sent(result, to_send);
g_assert_cmpint(otrng_list_len(bob->keys->old_mac_keys), ==, 0);
g_assert_cmpint(bob->keys->i, ==, 2);
g_assert_cmpint(bob->keys->j, ==, message_id);
g_assert_cmpint(bob->keys->k, ==, 3);
g_assert_cmpint(bob->keys->pn, ==, 0);
// Bob receives a data message
response_to_bob = otrng_response_new();
result = otrng_receive_message(response_to_bob, to_send, alice);
assert_message_rec(result, "hello", response_to_bob);
g_assert_cmpint(otrng_list_len(alice->keys->old_mac_keys), ==, message_id);
g_assert_cmpint(alice->keys->i, ==, 2);
g_assert_cmpint(alice->keys->j, ==, 0);
g_assert_cmpint(alice->keys->k, ==, message_id);
g_assert_cmpint(alice->keys->pn, ==, 3);
free_message_and_response(response_to_bob, &to_send);
}
const size_t secret_len = 2;
uint8_t secret_data[2] = {0x08, 0x05};
// Bob sends a message with TLV
result = otrng_smp_start(&to_send, NULL, 0, secret_data, secret_len, bob);
assert_message_sent(result, to_send);
g_assert_cmpint(otrng_list_len(bob->keys->old_mac_keys), ==, 0);
// Alice receives a data message with TLV
response_to_bob = otrng_response_new();
otrng_assert_is_success(
otrng_receive_message(response_to_bob, to_send, alice));
g_assert_cmpint(otrng_list_len(alice->keys->old_mac_keys), ==, 4);
// Check TLVS
otrng_assert(response_to_bob->tlvs);
g_assert_cmpint(response_to_bob->tlvs->data->type, ==, OTRNG_TLV_SMP_MSG_1);
g_assert_cmpint(response_to_bob->tlvs->data->len, ==, 342);
free_message_and_response(response_to_bob, &to_send);
otrng_global_state_free(alice_client->global_state);
otrng_global_state_free(bob_client->global_state);
otrng_conn_free_all(alice, bob);
}
static void test_otrng_incorrect_offline_dake() {
otrng_client_s *alice_client = otrng_client_new(ALICE_IDENTITY);
otrng_client_s *bob_client = otrng_client_new(BOB_IDENTITY);
otrng_s *alice = set_up(alice_client, 1);
otrng_s *bob = set_up(bob_client, 2);
g_assert_cmpint(alice->keys->i, ==, 0);
g_assert_cmpint(alice->keys->j, ==, 0);
g_assert_cmpint(alice->keys->k, ==, 0);
g_assert_cmpint(alice->keys->pn, ==, 0);
g_assert_cmpint(bob->keys->i, ==, 0);
g_assert_cmpint(bob->keys->j, ==, 0);
g_assert_cmpint(bob->keys->k, ==, 0);
g_assert_cmpint(bob->keys->pn, ==, 0);
prekey_ensemble_s *ensemble = otrng_build_prekey_ensemble(bob);
otrng_assert(ensemble);
// TODO: @non_interactive should this validation happen outside?
otrng_assert_is_success(otrng_prekey_ensemble_validate(ensemble));
g_assert_cmpint(bob->their_prekeys_id, ==, 0);
otrng_assert(bob->running_version == 0);
g_assert_cmpint(bob->keys->i, ==, 0);
g_assert_cmpint(bob->keys->j, ==, 0);
g_assert_cmpint(bob->keys->k, ==, 0);
g_assert_cmpint(bob->keys->pn, ==, 0);
char *to_bob = NULL;
otrng_assert_is_success(
otrng_send_non_interactive_auth(&to_bob, ensemble, alice));
otrng_prekey_ensemble_free(ensemble);
otrng_assert(to_bob);
otrng_assert_cmpmem("?OTR:AAQN", to_bob, 9);
otrng_assert(alice->state == OTRNG_STATE_ENCRYPTED_MESSAGES);
otrng_assert(alice->running_version == 4);
g_assert_cmpint(alice->keys->i, ==, 0);
g_assert_cmpint(alice->keys->j, ==, 0);
g_assert_cmpint(alice->keys->k, ==, 0);
g_assert_cmpint(alice->keys->pn, ==, 0);
// Bob receives an offline message
otrng_response_s *response = otrng_response_new();
otrng_assert_is_success(otrng_receive_message(response, to_bob, bob));
otrng_free(to_bob);
otrng_assert(!response->to_display);
otrng_assert(!response->to_send);
otrng_response_free_all(response);
g_assert_cmpint(bob->their_prekeys_id, ==, 0);
otrng_assert(bob->state == OTRNG_STATE_WAITING_DAKE_DATA_MESSAGE);
otrng_assert(bob->running_version == 4);
g_assert_cmpint(bob->keys->i, ==, 0);
g_assert_cmpint(bob->keys->j, ==, 0);
g_assert_cmpint(bob->keys->k, ==, 0);
g_assert_cmpint(bob->keys->pn, ==, 0);
otrng_assert_ec_public_key_eq(bob->keys->their_ecdh,
alice->keys->our_ecdh->pub);
otrng_assert_dh_public_key_eq(bob->keys->their_dh, alice->keys->our_dh->pub);
// Both have the same shared shared secret/root key
otrng_assert_root_key_eq(alice->keys->current->root_key,
bob->keys->current->root_key);
string_p to_send = NULL;
otrng_result result;
// Alice is unable to send a data message
result = otrng_send_message(&to_send, "hi", NULL, 0, bob);
otrng_assert_is_error(result);
otrng_assert(!to_send);
g_assert_cmpint(alice->keys->i, ==, 0);
g_assert_cmpint(alice->keys->j, ==, 0);
g_assert_cmpint(alice->keys->k, ==, 0);
g_assert_cmpint(alice->keys->pn, ==, 0);
otrng_global_state_free(alice_client->global_state);
otrng_global_state_free(bob_client->global_state);
otrng_conn_free_all(alice, bob);
}
static void test_api_whitespace_tag(void) {
otrng_client_s *alice_client = otrng_client_new(ALICE_IDENTITY);
otrng_client_s *bob_client = otrng_client_new(BOB_IDENTITY);
otrng_s *alice = NULL;
otrng_s *bob = NULL;
set_up_client(alice_client, 1);
otrng_policy_s policy = {.allows = OTRNG_ALLOW_V34,
.type = OTRNG_POLICY_OPPORTUNISTIC};
alice = otrng_new(alice_client, policy);
set_up_client(bob_client, 1);
bob = otrng_new(bob_client, policy);
otrng_client_set_padding(256, alice_client);
otrng_client_set_padding(256, bob_client);
otrng_response_s *response_to_bob = otrng_response_new();
otrng_response_s *response_to_alice = otrng_response_new();
/* Alice sends plaintext and the policy adds the whitespace tag */
string_p to_send = NULL;
otrng_result result = otrng_send_message(&to_send, "hi", NULL, 0, alice);
const char *expected_tag =
" \t \t\t\t\t \t \t \t \t\t \t \t\t \t\thi";
otrng_assert_is_success(result);
otrng_assert(to_send);
g_assert_cmpstr(to_send, ==, expected_tag);
/* Bob receives a Whitespace tag */
otrng_assert_is_success(
otrng_receive_message(response_to_alice, to_send, bob));
otrng_free(to_send);
const char *to_display = "hi";
otrng_assert(bob->state == OTRNG_STATE_WAITING_AUTH_R);
g_assert_cmpstr(response_to_alice->to_display, ==, to_display);
otrng_assert(response_to_alice->to_send);
otrng_assert_cmpmem("?OTR:AAQ1", response_to_alice->to_send, 9);
free(response_to_alice->to_display);
/* Alice receives an Identity Message */
otrng_assert_is_success(otrng_receive_message(
response_to_bob, response_to_alice->to_send, alice));
otrng_free(response_to_alice->to_send);
response_to_alice->to_send = NULL;
/* Alice has Bob's ephemeral keys */
otrng_assert_ec_public_key_eq(alice->keys->their_ecdh,
bob->keys->our_ecdh->pub);
otrng_assert_dh_public_key_eq(alice->keys->their_dh, bob->keys->our_dh->pub);
otrng_assert_not_zero(alice->keys->ssid, sizeof(alice->keys->ssid));
otrng_assert_not_zero(alice->keys->shared_secret, sizeof(k_shared_secret));
/* Alice replies with an Auth-R message */
otrng_assert(alice->state == OTRNG_STATE_WAITING_AUTH_I);
otrng_assert(response_to_bob->to_display == NULL);
otrng_assert(response_to_bob->to_send);
otrng_assert_cmpmem("?OTR:AAQ2", response_to_bob->to_send, 9);
/* Bob receives an Auth-R message */
otrng_assert_is_success(
otrng_receive_message(response_to_alice, response_to_bob->to_send, bob));
otrng_free(response_to_bob->to_send);
response_to_bob->to_send = NULL;
/* Bob has Alice's ephemeral keys */
otrng_assert_ec_public_key_eq(bob->keys->their_ecdh,
alice->keys->our_ecdh_first->pub);
otrng_assert_dh_public_key_eq(bob->keys->their_dh,
alice->keys->our_dh_first->pub);
otrng_assert_not_zero(bob->keys->ssid, sizeof(alice->keys->ssid));
otrng_assert_zero(bob->keys->shared_secret, sizeof(k_shared_secret));
otrng_assert_not_zero(bob->keys->current->root_key, sizeof(k_root));
g_assert_cmpint(bob->keys->i, ==, 0);
g_assert_cmpint(bob->keys->j, ==, 0);
g_assert_cmpint(bob->keys->k, ==, 0);
/* Bob replies with an Auth-I message */
otrng_assert(bob->state == OTRNG_STATE_WAITING_DAKE_DATA_MESSAGE);
otrng_assert(response_to_alice->to_display == NULL);
otrng_assert(response_to_alice->to_send);
otrng_assert_cmpmem("?OTR:AAQ3", response_to_alice->to_send, 9);
/* The double ratchet is initialized */
otrng_assert(bob->keys->current);
/* Alice receives an Auth-I message */
otrng_assert_is_success(otrng_receive_message(
response_to_bob, response_to_alice->to_send, alice));
otrng_free(response_to_alice->to_send);
response_to_alice->to_send = NULL;
/* The double ratchet is initialized */
otrng_assert(alice->keys->current);
/* Both participants have the same shared secret */
otrng_assert_root_key_eq(alice->keys->shared_secret,
bob->keys->shared_secret);
/* Alice replies with initial data message Dake Data Message */
otrng_assert(alice->state == OTRNG_STATE_ENCRYPTED_MESSAGES);
otrng_assert_cmpmem("?OTR:AAQD", response_to_bob->to_send, 9);
otrng_assert(response_to_bob->to_display == NULL);
g_assert_cmpint(alice->keys->i, ==, 1);
g_assert_cmpint(alice->keys->j, ==, 1);
g_assert_cmpint(alice->keys->k, ==, 0);
/* Bob receives the initial data message */
otrng_assert_is_success(
otrng_receive_message(response_to_alice, response_to_bob->to_send, bob));
otrng_free(response_to_bob->to_send);
response_to_bob->to_send = NULL;
otrng_assert(bob->state == OTRNG_STATE_ENCRYPTED_MESSAGES);
otrng_assert(response_to_alice->to_send == NULL);
otrng_assert(response_to_alice->to_display == NULL);
otrng_assert(response_to_alice);
g_assert_cmpint(otrng_list_len(bob->keys->old_mac_keys), ==, 1);
g_assert_cmpint(bob->keys->i, ==, 1);
g_assert_cmpint(bob->keys->j, ==, 0);
g_assert_cmpint(bob->keys->k, ==, 1);
g_assert_cmpint(bob->keys->pn, ==, 0);
/* Bob closes the encrypted conversation */
otrng_close(&to_send, bob);
otrng_assert(bob->state == OTRNG_STATE_START);
/* Alice receives a disconnected TLV from Bob */
otrng_receive_message(response_to_bob, to_send, alice);
free(to_send);
otrng_assert(response_to_bob->tlvs);
g_assert_cmpint(response_to_bob->tlvs->data->type, ==,
OTRNG_TLV_DISCONNECTED);
otrng_assert(alice->state == OTRNG_STATE_FINISHED);
otrng_response_free(response_to_alice);
otrng_response_free(response_to_bob);
otrng_global_state_free(alice_client->global_state);
otrng_global_state_free(bob_client->global_state);
otrng_conn_free_all(alice, bob);
}
static void test_api_conversation_errors_1(void) {
otrng_client_s *alice_client = otrng_client_new(ALICE_IDENTITY);
otrng_client_s *bob_client = otrng_client_new(BOB_IDENTITY);
otrng_s *alice = set_up(alice_client, 1);
otrng_s *bob = set_up(bob_client, 2);
// DAKE HAS FINISHED
do_dake_fixture(alice, bob);
otrng_response_s *response_to_alice = NULL;
otrng_response_s *response_to_bob = NULL;
string_p to_send = NULL;
otrng_result result;
// Alice sends a data message
result = otrng_send_message(&to_send, "hi", NULL, 0, alice);
assert_message_sent(result, to_send);
otrng_assert(!alice->keys->old_mac_keys);
g_assert_cmpint(alice->keys->i, ==, 1);
g_assert_cmpint(alice->keys->j, ==, 2);
// To trigger the error message
bob->state = OTRNG_STATE_START;
// Bob receives a data message in the incorrect state
response_to_alice = otrng_response_new();
otrng_assert_is_error(otrng_receive_message(response_to_alice, to_send, bob));
const string_p err_code =
"?OTR Error: ERROR_2: OTRNG_ERR_MSG_NOT_PRIVATE_STATE";
otrng_assert_cmpmem(err_code, response_to_alice->to_send, strlen(err_code));
otrng_assert(response_to_alice->to_send != NULL);
g_assert_cmpint(otrng_list_len(bob->keys->old_mac_keys), ==, 1);
g_assert_cmpint(bob->keys->i, ==, 1);
g_assert_cmpint(bob->keys->j, ==, 0);
// Alice receives the error message
response_to_bob = otrng_response_new();
otrng_assert_is_success(otrng_receive_message(
response_to_bob, response_to_alice->to_send, alice));
const string_p err_human = "Not in private state message";
otrng_assert(response_to_bob);
otrng_assert_cmpmem(err_human, response_to_bob->to_display,
strlen(err_human));
const string_p query_msg = "?OTRv43?";
otrng_assert_cmpmem(query_msg, response_to_bob->to_send, strlen(query_msg));
otrng_response_free(response_to_alice);
otrng_response_free(response_to_bob);
otrng_free(to_send);
to_send = NULL;
otrng_global_state_free(alice_client->global_state);
otrng_global_state_free(bob_client->global_state);
otrng_conn_free_all(alice, bob);
}
static void test_api_conversation_errors_2(void) {
otrng_client_s *alice_client = otrng_client_new(ALICE_IDENTITY);
otrng_client_s *bob_client = otrng_client_new(BOB_IDENTITY);
otrng_s *alice = set_up(alice_client, 1);
otrng_s *bob = set_up(bob_client, 2);
// DAKE HAS FINISHED
do_dake_fixture(alice, bob);
otrng_response_s *response_to_alice = NULL;
string_p to_send = NULL;
otrng_result result;
// Alice sends a data message
result = otrng_send_message(&to_send, "hi", NULL, 0, alice);
assert_message_sent(result, to_send);
otrng_assert(!alice->keys->old_mac_keys);
// Corrupt message
size_t dec_len = 0;
uint8_t *decoded = NULL;
otrl_base64_otr_decode(to_send, &decoded, &dec_len);
otrng_free(to_send);
decoded[dec_len - 1] = decoded[dec_len - 1] + 3;
to_send = otrl_base64_otr_encode(decoded, dec_len);
otrng_free(decoded);
// Bob receives a non valid data message
response_to_alice = otrng_response_new();
result = otrng_receive_message(response_to_alice, to_send, bob);
otrng_assert(response_to_alice->to_send == NULL);
otrng_assert_is_error(result);
free_message_and_response(response_to_alice, &to_send);
otrng_global_state_free(alice_client->global_state);
otrng_global_state_free(bob_client->global_state);
otrng_conn_free_all(alice, bob);
}
static void do_ake_v3(otrng_s *alice, otrng_s *bob) {
otrng_response_s *response_to_bob = otrng_response_new();
otrng_response_s *response_to_alice = otrng_response_new();
// Alice sends query message
string_p query_message = NULL;
otrng_assert_is_success(otrng_build_query_message(&query_message, "", alice));
otrng_assert_cmpmem("?OTRv3", query_message, 6);
// Bob receives query message
otrng_assert_is_success(
otrng_receive_message(response_to_alice, query_message, bob));
otrng_free(query_message);
query_message = NULL;
// Should reply with a DH-Commit
otrng_assert(response_to_alice->to_display == NULL);
otrng_assert(response_to_alice->to_send);
otrng_assert_cmpmem("?OTR:AAMC", response_to_alice->to_send, 9);
// Alice receives DH-Commit
otrng_assert_is_success(otrng_receive_message(
response_to_bob, response_to_alice->to_send, alice));
otrng_free(response_to_alice->to_send);
response_to_alice->to_send = NULL;
// Should reply with a DH Key
otrng_assert(response_to_bob->to_display == NULL);
otrng_assert(response_to_bob->to_send);
otrng_assert_cmpmem("?OTR:AAMK", response_to_bob->to_send, 9);
// Bob receives a DH Key
otrng_assert_is_success(
otrng_receive_message(response_to_alice, response_to_bob->to_send, bob));
otrng_free(response_to_bob->to_send);
response_to_bob->to_send = NULL;
// Bob should reply with a Reveal Sig
otrng_assert(response_to_alice->to_display == NULL);
otrng_assert(response_to_alice->to_send);
otrng_assert_cmpmem("?OTR:AAMR", response_to_alice->to_send, 9);
// Alice receives Reveal Sig
otrng_assert_is_success(otrng_receive_message(
response_to_bob, response_to_alice->to_send, alice));
otrng_free(response_to_alice->to_send);
response_to_alice->to_send = NULL;
// Should reply with a Sig
otrng_assert(response_to_bob->to_display == NULL);
otrng_assert(response_to_bob->to_send);
otrng_assert_cmpmem("?OTR:AAMS", response_to_bob->to_send, 9);
// Alice should be encrypted
g_assert_cmpint(OTRL_MSGSTATE_ENCRYPTED, ==, alice->v3_conn->ctx->msgstate);
// Bob receives a Sig
otrng_assert_is_success(
otrng_receive_message(response_to_alice, response_to_bob->to_send, bob));
otrng_free(response_to_bob->to_send);
response_to_bob->to_send = NULL;
// Bob should NOT reply
otrng_assert(response_to_alice->to_display == NULL);
otrng_assert(!response_to_alice->to_send);
// Alice should be encrypted
g_assert_cmpint(OTRL_MSGSTATE_ENCRYPTED, ==, bob->v3_conn->ctx->msgstate);
otrng_response_free_all(response_to_alice, response_to_bob);
}
static void test_api_conversation_v3(void) {
otrng_client_s *alice_client = otrng_client_new(ALICE_IDENTITY);
otrng_client_s *bob_client = otrng_client_new(BOB_IDENTITY);
set_up_client(alice_client, 1);
set_up_client(bob_client, 2);
otrng_policy_s policy = {.allows = OTRNG_ALLOW_V3,
.type = OTRNG_POLICY_DEFAULT};
otrng_s *alice = otrng_new(alice_client, policy);
otrng_s *bob = otrng_new(bob_client, policy);
// Set up v3 context
// TODO: This initialization should be automatic
alice->v3_conn = otrng_v3_conn_new(alice_client, "bob");
bob->v3_conn = otrng_v3_conn_new(bob_client, "alice");
alice->v3_conn->opdata = alice;
bob->v3_conn->opdata = bob;
// Generate long term private key.
otrng_assert_is_success(otrng_v3_create_private_key(alice_client));
otrng_assert_is_success(otrng_v3_create_private_key(bob_client));
// Generate instance tag
// TODO: use callback?
otrng_client_add_instance_tag(alice_client, 0x100 + 1);
otrng_client_add_instance_tag(bob_client, 0x100 + 2);
// AKE HAS FINISHED.
do_ake_v3(alice, bob);
otrng_response_s *response_to_bob = NULL;
otrng_response_s *response_to_alice = NULL;
string_p to_send = NULL;
// Alice sends a data message
otrng_assert_is_success(otrng_send_message(&to_send, "hi", NULL, 0, alice));
otrng_assert(to_send);
otrng_assert_cmpmem("?OTR:AAMD", to_send, 9);
// Bob receives a data message
response_to_alice = otrng_response_new();
otrng_assert_is_success(
otrng_receive_message(response_to_alice, to_send, bob));
otrng_assert(response_to_alice->to_display);
otrng_assert_cmpmem("hi", response_to_alice->to_display, 3);
otrng_assert(!response_to_alice->to_send);
free_message_and_response(response_to_alice, &to_send);
// Bob sends a data message
otrng_assert_is_success(otrng_send_message(&to_send, "hi", NULL, 0, bob));
otrng_assert(to_send);
otrng_assert_cmpmem("?OTR:AAMD", to_send, 9);
// Alice receives a data message
response_to_bob = otrng_response_new();
otrng_assert_is_success(
otrng_receive_message(response_to_bob, to_send, alice));
otrng_assert(response_to_bob->to_display);
otrng_assert_cmpmem("hi", response_to_bob->to_display, 3);
otrng_assert(!response_to_bob->to_send);
free_message_and_response(response_to_bob, &to_send);
otrng_global_state_free(alice_client->global_state);
otrng_global_state_free(bob_client->global_state);
otrng_conn_free_all(alice, bob);
}
static void test_api_multiple_clients(void) {
otrng_bool send_response = otrng_true;
otrng_result result;
// TODO: The next comment is WRONG.
// There should be 2 separate protocols, one for each instance tag
// Alice has seen for Bob.
// We will postpone fixing this test until we implement this behavior.
return;
otrng_client_s *alice_client = otrng_client_new(ALICE_IDENTITY);
otrng_client_s *bob_phone_state = otrng_client_new(BOB_IDENTITY);
otrng_client_s *bob_pc_state = otrng_client_new(BOB_IDENTITY);
// The account name should be the same. The account can be logged
// on different clients. Instance tags are used for that. This
// account name can be used as phi.
otrng_s *alice = set_up(alice_client, 1);
otrng_s *bob_phone = set_up(bob_phone_state, 2);
otrng_s *bob_pc = set_up(bob_pc_state, 3);
otrng_response_s *pc_to_alice = otrng_response_new();
otrng_response_s *phone_to_alice = otrng_response_new();
otrng_response_s *alice_to_pc = otrng_response_new();
otrng_response_s *alice_to_phone = otrng_response_new();
string_p query_message = NULL;
// Alice sends a query message
otrng_assert_is_success(
otrng_build_query_message(&query_message, "?OTRv4", alice));
otrng_assert(alice->state == OTRNG_STATE_START);
otrng_assert_cmpmem("?OTRv4?", query_message, 7);
// PC receives query message and sends identity message
result = otrng_receive_message(pc_to_alice, query_message, bob_pc);
assert_rec_message_in_state(result, pc_to_alice, bob_pc,
OTRNG_STATE_WAITING_AUTH_R, send_response);
// PHONE receives query message and sends identity message
result = otrng_receive_message(phone_to_alice, query_message, bob_phone);
assert_rec_message_in_state(result, phone_to_alice, bob_phone,
OTRNG_STATE_WAITING_AUTH_R, send_response);
otrng_free(query_message);
// ALICE receives Identity message from PC and sends AUTH-R
result = otrng_receive_message(alice_to_pc, pc_to_alice->to_send, alice);
assert_rec_message_in_state(result, alice_to_pc, alice,
OTRNG_STATE_WAITING_AUTH_I, send_response);
otrng_response_free(pc_to_alice);
// ALICE receives Identity message from PHONE (on state
// OTRNG_STATE_WAITING_AUTH_I) and sends AUTH-R. ALICE will replace keys and
// profile info from PC with info from PHONE.
result =
otrng_receive_message(alice_to_phone, phone_to_alice->to_send, alice);
assert_rec_message_in_state(result, alice_to_phone, alice,
OTRNG_STATE_WAITING_AUTH_I, send_response);
otrng_response_free(phone_to_alice);
// PC receives Auth-R succesfully and sends an Auth-I
pc_to_alice = otrng_response_new();
result = otrng_receive_message(pc_to_alice, alice_to_pc->to_send, bob_pc);
assert_rec_message_in_state(result, pc_to_alice, bob_pc,
OTRNG_STATE_WAITING_DAKE_DATA_MESSAGE,
send_response);
// PC generates the first keys after Auth-R has been received
otrng_assert(bob_pc->keys->our_dh->pub);
otrng_assert(bob_pc->keys->our_dh->priv);
otrng_assert_not_zero(bob_pc->keys->our_ecdh->pub, ED448_POINT_BYTES);
otrng_assert_not_zero(bob_pc->keys->our_ecdh->priv, ED448_SCALAR_BYTES);
// PHONE receives Auth-R with PC instance tag - Ignores
phone_to_alice = otrng_response_new();
result =
otrng_receive_message(phone_to_alice, alice_to_pc->to_send, bob_phone);
assert_rec_message_in_state(result, phone_to_alice, bob_phone,
OTRNG_STATE_WAITING_AUTH_R, !send_response);
otrng_response_free_all(phone_to_alice, alice_to_pc);
otrng_assert(bob_phone->keys->our_dh->pub);
otrng_assert(bob_phone->keys->our_dh->priv);
otrng_assert_not_zero(bob_phone->keys->our_ecdh->pub, ED448_POINT_BYTES);
otrng_assert_not_zero(bob_phone->keys->our_ecdh->priv, ED448_SCALAR_BYTES);
// ALICE receives Auth-I from PC - Authentication fails
alice_to_pc = otrng_response_new();
result = otrng_receive_message(alice_to_pc, pc_to_alice->to_send, alice);
assert_rec_message_in_state(result, alice_to_pc, alice,
OTRNG_STATE_WAITING_AUTH_I, !send_response);
otrng_response_free_all(pc_to_alice, alice_to_pc);
// PC receives Auth-R again - ignores
pc_to_alice = otrng_response_new();
result = otrng_receive_message(pc_to_alice, alice_to_phone->to_send, bob_pc);
assert_rec_message_in_state(result, pc_to_alice, bob_pc,
OTRNG_STATE_ENCRYPTED_MESSAGES, !send_response);
otrng_response_free(pc_to_alice);
// PHONE receives correct Auth-R message and sends Auth-I
phone_to_alice = otrng_response_new();
result =
otrng_receive_message(phone_to_alice, alice_to_phone->to_send, bob_phone);
assert_rec_message_in_state(result, phone_to_alice, bob_phone,
OTRNG_STATE_ENCRYPTED_MESSAGES, send_response);
otrng_response_free(alice_to_phone);
// PHONE generates the first keys after Auth-R has been received
otrng_assert(bob_phone->keys->our_dh->pub);
otrng_assert(bob_phone->keys->our_dh->priv);
otrng_assert_not_zero(bob_phone->keys->our_ecdh->pub, ED448_POINT_BYTES);
otrng_assert_not_zero(bob_phone->keys->our_ecdh->priv, ED448_SCALAR_BYTES);
// ALICE receives Auth-I from PHONE
alice_to_phone = otrng_response_new();
result =
otrng_receive_message(alice_to_phone, phone_to_alice->to_send, alice);
assert_rec_message_in_state(result, alice_to_phone, alice,
OTRNG_STATE_ENCRYPTED_MESSAGES, !send_response);
// ALICE and PHONE have the same shared secret
otrng_assert_root_key_eq(alice->keys->current->root_key,
bob_phone->keys->current->root_key);
otrng_response_free_all(phone_to_alice, alice_to_phone);
otrng_global_state_free(alice_client->global_state);
otrng_global_state_free(bob_phone_state->global_state);
otrng_global_state_free(bob_pc_state->global_state);
otrng_conn_free_all(alice, bob_pc, bob_phone);
}
static void test_api_smp(void) {
otrng_client_s *alice_client = otrng_client_new(ALICE_IDENTITY);
otrng_client_s *bob_client = otrng_client_new(BOB_IDENTITY);
otrng_s *alice = set_up(alice_client, 1);
otrng_s *bob = set_up(bob_client, 2);
// Starts an smp state machine
g_assert_cmpint(alice->smp->state_expect, ==, SMP_STATE_EXPECT_1);
g_assert_cmpint(bob->smp->state_expect, ==, SMP_STATE_EXPECT_1);
// DAKE HAS FINISHED.
do_dake_fixture(alice, bob);
otrng_response_s *response_to_bob = NULL;
otrng_response_s *response_to_alice = NULL;
string_p to_send = NULL;
const char *secret = "secret";
// Alice sends SMP1
otrng_assert_is_success(otrng_smp_start(&to_send, NULL, 0, (uint8_t *)secret,
strlen(secret), alice));
otrng_assert(to_send);
otrng_assert_cmpmem("?OTR:AAQD", to_send, 9); // SMP1
g_assert_cmpint(alice->smp->state_expect, ==, SMP_STATE_EXPECT_2);
// Bob receives SMP1
response_to_alice = otrng_response_new();
otrng_assert_is_success(
otrng_receive_message(response_to_alice, to_send, bob));
otrng_assert(!response_to_alice->to_send);
free_message_and_response(response_to_alice, &to_send);
// This will be called by Bob when the OTRNG_SMPEVENT_ASK_FOR_SECRET is
// triggered.
otrng_assert_is_success(
otrng_smp_continue(&to_send, (uint8_t *)secret, strlen(secret), bob));
otrng_assert(to_send);
otrng_assert_cmpmem("?OTR:AAQD", to_send, 9); // SMP2
g_assert_cmpint(bob->smp->state_expect, ==, SMP_STATE_EXPECT_3);
// Alice receives SMP2
response_to_bob = otrng_response_new();
otrng_assert_is_success(
otrng_receive_message(response_to_bob, to_send, alice));
otrng_assert(response_to_bob->to_send);
otrng_assert_cmpmem("?OTR:AAQD", response_to_bob->to_send, 9); // SMP3