aboutsummaryrefslogtreecommitdiff
path: root/plugins/signal-protocol/tests/session_builder.vala
blob: 7e2448e172134f3478d498742467c0dfe5b3d869 (plain) (blame)
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
namespace Signal.Test {

class SessionBuilderTest : Gee.TestCase {
    Address alice_address;
    Address bob_address;

    public SessionBuilderTest() {
        base("SessionBuilder");

        add_test("basic_pre_key_v2", test_basic_pre_key_v2);
        add_test("basic_pre_key_v3", test_basic_pre_key_v3);
        add_test("bad_signed_pre_key_signature", test_bad_signed_pre_key_signature);
        add_test("repeat_bundle_message_v2", test_repeat_bundle_message_v2);
    }

    private Context global_context;

    public override void set_up() {
        try {
            global_context = new Context();
            alice_address = new Address("+14151111111", 1);
            bob_address = new Address("+14152222222", 1);
        } catch (Error e) {
            fail_if_reached(@"Unexpected error: $(e.message)");
        }
    }

    public override void tear_down() {
        global_context = null;
        alice_address = null;
        bob_address = null;
    }

    void test_basic_pre_key_v2() {
        try {
            /* Create Alice's data store and session builder */
            Store alice_store = setup_test_store_context(global_context);
            SessionBuilder alice_session_builder = alice_store.create_session_builder(bob_address);

            /* Create Bob's data store and pre key bundle */
            Store bob_store = setup_test_store_context(global_context);
            uint32 bob_local_registration_id = bob_store.local_registration_id;
            IdentityKeyPair bob_identity_key_pair = bob_store.identity_key_pair;
            ECKeyPair bob_pre_key_pair = global_context.generate_key_pair();

            PreKeyBundle bob_pre_key = create_pre_key_bundle(bob_local_registration_id, 1, 31337, bob_pre_key_pair.public, 0, null, null, bob_identity_key_pair.public);

            /*
             * Have Alice process Bob's pre key bundle, which should fail due to a
             * missing unsigned pre key.
             */
            fail_if_not_error_code(() => alice_session_builder.process_pre_key_bundle(bob_pre_key), ErrorCode.INVALID_KEY);
        } catch(Error e) {
            fail_if_reached(@"Unexpected error: $(e.message)");
        }
    }

    void test_basic_pre_key_v3() {
        try {
            /* Create Alice's data store and session builder */
            Store alice_store = setup_test_store_context(global_context);
            SessionBuilder alice_session_builder = alice_store.create_session_builder(bob_address);

            /* Create Bob's data store and pre key bundle */
            Store bob_store = setup_test_store_context(global_context);
            uint32 bob_local_registration_id = bob_store.local_registration_id;
            ECKeyPair bob_pre_key_pair = global_context.generate_key_pair();
            ECKeyPair bob_signed_pre_key_pair = global_context.generate_key_pair();
            IdentityKeyPair bob_identity_key_pair = bob_store.identity_key_pair;

            uint8[] bob_signed_pre_key_signature = global_context.calculate_signature(bob_identity_key_pair.private, bob_signed_pre_key_pair.public.serialize());

            PreKeyBundle bob_pre_key = create_pre_key_bundle(bob_local_registration_id, 1, 31337, bob_pre_key_pair.public, 22, bob_signed_pre_key_pair.public, bob_signed_pre_key_signature, bob_identity_key_pair.public);

            /* Have Alice process Bob's pre key bundle */
            alice_session_builder.process_pre_key_bundle(bob_pre_key);

            /* Check that we can load the session state and verify its version */
            fail_if_not(alice_store.contains_session(bob_address));

            SessionRecord loaded_record = alice_store.load_session(bob_address);
            fail_if_not_eq_int((int)loaded_record.state.session_version, 3);

            /* Encrypt an outgoing message to send to Bob */
            string original_message = "L'homme est condamné à être libre";
            SessionCipher alice_session_cipher = alice_store.create_session_cipher(bob_address);

            CiphertextMessage outgoing_message = alice_session_cipher.encrypt(original_message.data);
            fail_if_not_eq_int(outgoing_message.type, CiphertextType.PREKEY);

            /* Convert to an incoming message for Bob */
            PreKeySignalMessage incoming_message = global_context.deserialize_pre_key_signal_message(outgoing_message.serialized);

            /* Save the pre key and signed pre key in Bob's data store */
            PreKeyRecord bob_pre_key_record;
            throw_by_code(PreKeyRecord.create(out bob_pre_key_record, bob_pre_key.pre_key_id, bob_pre_key_pair));
            bob_store.store_pre_key(bob_pre_key_record);

            SignedPreKeyRecord bob_signed_pre_key_record;
            throw_by_code(SignedPreKeyRecord.create(out bob_signed_pre_key_record, 22, new DateTime.now_utc().to_unix(), bob_signed_pre_key_pair, bob_signed_pre_key_signature));
            bob_store.store_signed_pre_key(bob_signed_pre_key_record);

            /* Create Bob's session cipher and decrypt the message from Alice */
            SessionCipher bob_session_cipher = bob_store.create_session_cipher(alice_address);

            /* Prepare the data for the callback test */
            //int callback_context = 1234;
            //bob_session_cipher.user_data =
            //bob_session_cipher.decryption_callback =
            uint8[] plaintext = bob_session_cipher.decrypt_pre_key_signal_message(incoming_message);

            /* Clean up callback data */
            bob_session_cipher.user_data = null;
            bob_session_cipher.decryption_callback = null;

            /* Verify Bob's session state and the decrypted message */
            fail_if_not(bob_store.contains_session(alice_address));

            SessionRecord alice_recipient_session_record = bob_store.load_session(alice_address);

            SessionState alice_recipient_session_state = alice_recipient_session_record.state;
            fail_if_not_eq_int((int)alice_recipient_session_state.session_version, 3);
            fail_if_null(alice_recipient_session_state.alice_base_key);

            fail_if_not_eq_uint8_arr(original_message.data, plaintext);

            /* Have Bob send a reply to Alice */
            CiphertextMessage bob_outgoing_message = bob_session_cipher.encrypt(original_message.data);
            fail_if_not_eq_int(bob_outgoing_message.type, CiphertextType.SIGNAL);

            /* Verify that Alice can decrypt it */
            SignalMessage bob_outgoing_message_copy = global_context.copy_signal_message(bob_outgoing_message);

            uint8[] alice_plaintext = alice_session_cipher.decrypt_signal_message(bob_outgoing_message_copy);

            fail_if_not_eq_uint8_arr(original_message.data, alice_plaintext);

            GLib.Test.message("Pre-interaction tests complete");

            /* Interaction tests */
            run_interaction(alice_store, bob_store);

            /* Cleanup state from previous tests that we need to replace */
            alice_store = null;
            bob_pre_key_pair = null;
            bob_signed_pre_key_pair = null;
            bob_identity_key_pair = null;
            bob_signed_pre_key_signature = null;
            bob_pre_key_record = null;
            bob_signed_pre_key_record = null;

            /* Create Alice's new session data */
            alice_store = setup_test_store_context(global_context);
            alice_session_builder = alice_store.create_session_builder(bob_address);
            alice_session_cipher = alice_store.create_session_cipher(bob_address);

            /* Create Bob's new pre key bundle */
            bob_pre_key_pair = global_context.generate_key_pair();
            bob_signed_pre_key_pair = global_context.generate_key_pair();
            bob_identity_key_pair = bob_store.identity_key_pair;
            bob_signed_pre_key_signature = global_context.calculate_signature(bob_identity_key_pair.private, bob_signed_pre_key_pair.public.serialize());
            bob_pre_key = create_pre_key_bundle(bob_local_registration_id, 1, 31338, bob_pre_key_pair.public, 23, bob_signed_pre_key_pair.public, bob_signed_pre_key_signature, bob_identity_key_pair.public);

            /* Save the new pre key and signed pre key in Bob's data store */
            throw_by_code(PreKeyRecord.create(out bob_pre_key_record, bob_pre_key.pre_key_id, bob_pre_key_pair));
            bob_store.store_pre_key(bob_pre_key_record);

            throw_by_code(SignedPreKeyRecord.create(out bob_signed_pre_key_record, 23, new DateTime.now_utc().to_unix(), bob_signed_pre_key_pair, bob_signed_pre_key_signature));
            bob_store.store_signed_pre_key(bob_signed_pre_key_record);

            /* Have Alice process Bob's pre key bundle */
            alice_session_builder.process_pre_key_bundle(bob_pre_key);

            /* Have Alice encrypt a message for Bob */
            outgoing_message = alice_session_cipher.encrypt(original_message.data);
            fail_if_not_eq_int(outgoing_message.type, CiphertextType.PREKEY);

            /* Have Bob try to decrypt the message */
            PreKeySignalMessage outgoing_message_copy = global_context.copy_pre_key_signal_message(outgoing_message);

            /* The decrypt should fail with a specific error */
            fail_if_not_error_code(() => bob_session_cipher.decrypt_pre_key_signal_message(outgoing_message_copy), ErrorCode.UNTRUSTED_IDENTITY);

            outgoing_message_copy = global_context.copy_pre_key_signal_message(outgoing_message);

            /* Save the identity key to Bob's store */
            bob_store.save_identity(alice_address, outgoing_message_copy.identity_key);

            /* Try the decrypt again, this time it should succeed */
            outgoing_message_copy = global_context.copy_pre_key_signal_message(outgoing_message);
            plaintext = bob_session_cipher.decrypt_pre_key_signal_message(outgoing_message_copy);

            fail_if_not_eq_uint8_arr(original_message.data, plaintext);

            /* Create a new pre key for Bob */
            ECPublicKey test_public_key = create_test_ec_public_key(global_context);

            IdentityKeyPair alice_identity_key_pair = alice_store.identity_key_pair;

            bob_pre_key = create_pre_key_bundle(bob_local_registration_id, 1, 31337, test_public_key, 23, bob_signed_pre_key_pair.public, bob_signed_pre_key_signature, alice_identity_key_pair.public);

            /* Have Alice process Bob's new pre key bundle, which should fail */
            fail_if_not_error_code(() => alice_session_builder.process_pre_key_bundle(bob_pre_key), ErrorCode.UNTRUSTED_IDENTITY);

            GLib.Test.message("Post-interaction tests complete");
        } catch(Error e) {
            fail_if_reached(@"Unexpected error: $(e.message)");
        }
    }

    void test_bad_signed_pre_key_signature() {
        try {
            /* Create Alice's data store and session builder */
            Store alice_store = setup_test_store_context(global_context);
            SessionBuilder alice_session_builder = alice_store.create_session_builder(bob_address);

            /* Create Bob's data store */
            Store bob_store = setup_test_store_context(global_context);

            /* Create Bob's regular and signed pre key pairs */
            ECKeyPair bob_pre_key_pair = global_context.generate_key_pair();
            ECKeyPair bob_signed_pre_key_pair = global_context.generate_key_pair();

            /* Create Bob's signed pre key signature */
            IdentityKeyPair bob_identity_key_pair = bob_store.identity_key_pair;
            uint8[] bob_signed_pre_key_signature = global_context.calculate_signature(bob_identity_key_pair.private, bob_signed_pre_key_pair.public.serialize());

            for (int i = 0; i < bob_signed_pre_key_signature.length * 8; i++) {
                uint8[] modified_signature = bob_signed_pre_key_signature[0:bob_signed_pre_key_signature.length];

                /* Intentionally corrupt the signature data */
                modified_signature[i/8] ^= (1 << ((uint8)i % 8));

                /* Create a pre key bundle */
                PreKeyBundle bob_pre_key = create_pre_key_bundle(bob_store.local_registration_id,1,31137,bob_pre_key_pair.public,22,bob_signed_pre_key_pair.public,modified_signature,bob_identity_key_pair.public);

                /* Process the bundle and make sure we fail with an invalid key error */
                fail_if_not_error_code(() => alice_session_builder.process_pre_key_bundle(bob_pre_key), ErrorCode.INVALID_KEY);
            }

            /* Create a correct pre key bundle */
            PreKeyBundle bob_pre_key = create_pre_key_bundle(bob_store.local_registration_id,1,31137,bob_pre_key_pair.public,22,bob_signed_pre_key_pair.public,bob_signed_pre_key_signature,bob_identity_key_pair.public);

            /* Process the bundle and make sure we do not fail */
            alice_session_builder.process_pre_key_bundle(bob_pre_key);
        } catch(Error e) {
            fail_if_reached(@"Unexpected error: $(e.message)");
        }
    }

    void test_repeat_bundle_message_v2() {
        try {
            /* Create Alice's data store and session builder */
            Store alice_store = setup_test_store_context(global_context);
            SessionBuilder alice_session_builder = alice_store.create_session_builder(bob_address);

            /* Create Bob's data store and pre key bundle */
            Store bob_store = setup_test_store_context(global_context);
            ECKeyPair bob_pre_key_pair = global_context.generate_key_pair();
            ECKeyPair bob_signed_pre_key_pair = global_context.generate_key_pair();
            uint8[] bob_signed_pre_key_signature = global_context.calculate_signature(bob_store.identity_key_pair.private, bob_signed_pre_key_pair.public.serialize());
            PreKeyBundle bob_pre_key = create_pre_key_bundle(bob_store.local_registration_id,1,31337,bob_pre_key_pair.public,0,null,null,bob_store.identity_key_pair.public);

            /* Add Bob's pre keys to Bob's data store */
            PreKeyRecord bob_pre_key_record;
            throw_by_code(PreKeyRecord.create(out bob_pre_key_record, bob_pre_key.pre_key_id, bob_pre_key_pair));
            bob_store.store_pre_key(bob_pre_key_record);
            SignedPreKeyRecord bob_signed_pre_key_record;
            throw_by_code(SignedPreKeyRecord.create(out bob_signed_pre_key_record, 22, new DateTime.now_utc().to_unix(), bob_signed_pre_key_pair, bob_signed_pre_key_signature));
            bob_store.store_signed_pre_key(bob_signed_pre_key_record);

            /*
             * Have Alice process Bob's pre key bundle, which should fail due to a
             * missing signed pre key.
             */
            fail_if_not_error_code(() => alice_session_builder.process_pre_key_bundle(bob_pre_key), ErrorCode.INVALID_KEY);
        } catch(Error e) {
            fail_if_reached(@"Unexpected error: $(e.message)");
        }
    }

    class Holder {
        public uint8[] data { get; private set; }

        public Holder(uint8[] data) {
            this.data = data;
        }
    }

    void run_interaction(Store alice_store, Store bob_store) throws Error {

        /* Create the session ciphers */
        SessionCipher alice_session_cipher = alice_store.create_session_cipher(bob_address);
        SessionCipher bob_session_cipher = bob_store.create_session_cipher(alice_address);

        /* Create a test message */
        string original_message = "smert ze smert";

        /* Simulate Alice sending a message to Bob */
        CiphertextMessage alice_message = alice_session_cipher.encrypt(original_message.data);
        fail_if_not_eq_int(alice_message.type, CiphertextType.SIGNAL);

        SignalMessage alice_message_copy = global_context.copy_signal_message(alice_message);
        uint8[] plaintext = bob_session_cipher.decrypt_signal_message(alice_message_copy);
        fail_if_not_eq_uint8_arr(original_message.data, plaintext);

        GLib.Test.message("Interaction complete: Alice -> Bob");

        /* Simulate Bob sending a message to Alice */
        CiphertextMessage bob_message = bob_session_cipher.encrypt(original_message.data);
        fail_if_not_eq_int(alice_message.type, CiphertextType.SIGNAL);

        SignalMessage bob_message_copy = global_context.copy_signal_message(bob_message);
        plaintext = alice_session_cipher.decrypt_signal_message(bob_message_copy);
        fail_if_not_eq_uint8_arr(original_message.data, plaintext);

        GLib.Test.message("Interaction complete: Bob -> Alice");

        /* Looping Alice -> Bob */
        for (int i = 0; i < 10; i++) {
            uint8[] looping_message = create_looping_message(i);
            CiphertextMessage alice_looping_message = alice_session_cipher.encrypt(looping_message);
            SignalMessage alice_looping_message_copy = global_context.copy_signal_message(alice_looping_message);
            uint8[] looping_plaintext = bob_session_cipher.decrypt_signal_message(alice_looping_message_copy);
            fail_if_not_eq_uint8_arr(looping_message, looping_plaintext);
        }
        GLib.Test.message("Interaction complete: Alice -> Bob (looping)");

        /* Looping Bob -> Alice */
        for (int i = 0; i < 10; i++) {
            uint8[] looping_message = create_looping_message(i);
            CiphertextMessage bob_looping_message = bob_session_cipher.encrypt(looping_message);
            SignalMessage bob_looping_message_copy = global_context.copy_signal_message(bob_looping_message);
            uint8[] looping_plaintext = alice_session_cipher.decrypt_signal_message(bob_looping_message_copy);
            fail_if_not_eq_uint8_arr(looping_message, looping_plaintext);
        }
        GLib.Test.message("Interaction complete: Bob -> Alice (looping)");

        /* Generate a shuffled list of encrypted messages for later use */
        Holder[] alice_ooo_plaintext = new Holder[10];
        Holder[] alice_ooo_ciphertext = new Holder[10];
        for (int i = 0; i < 10; i++) {
            alice_ooo_plaintext[i] = new Holder(create_looping_message(i));
            alice_ooo_ciphertext[i] = new Holder(alice_session_cipher.encrypt(alice_ooo_plaintext[i].data).serialized);
        }

        for (int i = 0; i < 10; i++) {
            uint32 s = Random.next_int() % 10;
            Holder tmp = alice_ooo_plaintext[s];
            alice_ooo_plaintext[s] = alice_ooo_plaintext[i];
            alice_ooo_plaintext[i] = tmp;
            tmp = alice_ooo_ciphertext[s];
            alice_ooo_ciphertext[s] = alice_ooo_ciphertext[i];
            alice_ooo_ciphertext[i] = tmp;
        }
        GLib.Test.message("Shuffled Alice->Bob messages created");

        /* Looping Alice -> Bob (repeated) */
        for (int i = 0; i < 10; i++) {
            uint8[] looping_message = create_looping_message(i);
            CiphertextMessage alice_looping_message = alice_session_cipher.encrypt(looping_message);
            SignalMessage alice_looping_message_copy = global_context.copy_signal_message(alice_looping_message);
            uint8[] looping_plaintext = bob_session_cipher.decrypt_signal_message(alice_looping_message_copy);
            fail_if_not_eq_uint8_arr(looping_message, looping_plaintext);
        }
        GLib.Test.message("Interaction complete: Alice -> Bob (looping, repeated)");

        /* Looping Bob -> Alice (repeated) */
        for (int i = 0; i < 10; i++) {
            uint8[] looping_message = create_looping_message(i);
            CiphertextMessage bob_looping_message = bob_session_cipher.encrypt(looping_message);
            SignalMessage bob_looping_message_copy = global_context.copy_signal_message(bob_looping_message);
            uint8[] looping_plaintext = alice_session_cipher.decrypt_signal_message(bob_looping_message_copy);
            fail_if_not_eq_uint8_arr(looping_message, looping_plaintext);
        }
        GLib.Test.message("Interaction complete: Bob -> Alice (looping, repeated)");

        /* Shuffled Alice -> Bob */
        for (int i = 0; i < 10; i++) {
            SignalMessage ooo_message_deserialized = global_context.deserialize_signal_message(alice_ooo_ciphertext[i].data);
            uint8[] ooo_plaintext = bob_session_cipher.decrypt_signal_message(ooo_message_deserialized);
            fail_if_not_eq_uint8_arr(alice_ooo_plaintext[i].data, ooo_plaintext);
        }
        GLib.Test.message("Interaction complete: Alice -> Bob (shuffled)");
    }

    uint8[] create_looping_message(int index) {
        return (@"You can only desire based on what you know: $index").data;
    }

    /*
    uint8[] create_looping_message_short(int index) {
        return ("What do we mean by saying that existence precedes essence? " +
            "We mean that man first of all exists, encounters himself, " +
            @"surges up in the world--and defines himself aftward. $index").data;
    }
    */
}

}