Skip to content

Commit 24439ca

Browse files
authored
Fixed error in Session pickle() & unpickle() (#7)
1 parent 6ac9c65 commit 24439ca

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

javascript/test/olm.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,24 @@ describe("olm", function() {
107107
decrypted = aliceSession.decrypt(encrypted.type, encrypted.body);
108108
console.log(TEST_TEXT, "->", decrypted);
109109
expect(decrypted).toEqual(TEST_TEXT);
110+
111+
// Pickle and restore sessions
112+
var alicePickleKey = 'SomeSecretAlice';
113+
var bobPickleKey = 'SomeSecretBob';
114+
var aliceSessionPickled = aliceSession.pickle(alicePickleKey);
115+
var bobSessionPickled = bobSession.pickle(bobPickleKey);
116+
117+
aliceSession = new Olm.Session();
118+
bobSession = new Olm.Session();
119+
120+
aliceSession.unpickle(alicePickleKey, aliceSessionPickled);
121+
bobSession.unpickle(bobPickleKey, bobSessionPickled);
122+
123+
TEST_TEXT='some emoji: ☕ 123 // after pickling';
124+
encrypted = bobSession.encrypt(TEST_TEXT);
125+
expect(encrypted.type).toEqual(1);
126+
decrypted = aliceSession.decrypt(encrypted.type, encrypted.body);
127+
console.log(TEST_TEXT, "->", decrypted);
128+
expect(decrypted).toEqual(TEST_TEXT);
110129
});
111130
});

src/session.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ std::size_t olm::pickle_length(
508508
length += olm::pickle_length(value.alice_identity_key);
509509
length += olm::pickle_length(value.alice_base_key);
510510
length += olm::pickle_length(value.bob_one_time_key);
511+
length += olm::pickle_length(value.bob_prekey);
511512
length += olm::pickle_length(value.ratchet);
512513
return length;
513514
}
@@ -522,6 +523,7 @@ std::uint8_t * olm::pickle(
522523
pos = olm::pickle(pos, value.alice_identity_key);
523524
pos = olm::pickle(pos, value.alice_base_key);
524525
pos = olm::pickle(pos, value.bob_one_time_key);
526+
pos = olm::pickle(pos, value.bob_prekey);
525527
pos = olm::pickle(pos, value.ratchet);
526528
return pos;
527529
}
@@ -553,6 +555,7 @@ std::uint8_t const * olm::unpickle(
553555
pos = olm::unpickle(pos, end, value.alice_identity_key); UNPICKLE_OK(pos);
554556
pos = olm::unpickle(pos, end, value.alice_base_key); UNPICKLE_OK(pos);
555557
pos = olm::unpickle(pos, end, value.bob_one_time_key); UNPICKLE_OK(pos);
558+
pos = olm::unpickle(pos, end, value.bob_prekey); UNPICKLE_OK(pos);
556559
pos = olm::unpickle(pos, end, value.ratchet, includes_chain_index); UNPICKLE_OK(pos);
557560

558561
return pos;

tests/test_olm.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,25 @@ ::olm_create_account(account, random.data(), random.size());
130130
std::vector<std::uint8_t> session_buffer(::olm_session_size());
131131
::OlmSession *session = ::olm_session(session_buffer.data());
132132
std::uint8_t identity_key[32];
133+
std::uint8_t identity_signing_key[32];
133134
std::uint8_t one_time_key[32];
134135
std::uint8_t pre_key[32];
135136
mock_random(identity_key, sizeof(identity_key));
137+
mock_random(identity_signing_key, sizeof(identity_key));
136138
mock_random(one_time_key, sizeof(one_time_key));
137139
mock_random(pre_key, sizeof(one_time_key));
138140
std::vector<std::uint8_t> random2(::olm_create_outbound_session_random_length(session));
139141
mock_random(random2.data(), random2.size());
142+
std::uint8_t pre_key_signature[86];
143+
olm_account_prekey_signature(account, pre_key_signature);
144+
// ::olm_account_sign(account, pre_key, sizeof(pre_key), pre_key_signature, olm_account_signature_length(account));
140145

141146
::olm_create_outbound_session(
142147
session, account,
143148
identity_key, sizeof(identity_key),
149+
identity_signing_key, sizeof(identity_signing_key),
144150
pre_key, sizeof(pre_key),
151+
pre_key_signature, sizeof(pre_key_signature),
145152
one_time_key, sizeof(one_time_key),
146153
random2.data(), random2.size()
147154
);
@@ -217,9 +224,11 @@ ::olm_account_identity_keys(a_account, a_id_keys.data(), a_id_keys.size());
217224

218225
std::vector<std::uint8_t> b_id_keys(::olm_account_identity_keys_length(b_account));
219226
std::vector<std::uint8_t> b_pre_key(::olm_account_prekey_length(b_account));
227+
std::vector<std::uint8_t> b_pre_key_signature(::olm_account_signature_length(b_account));
220228
std::vector<std::uint8_t> b_ot_keys(::olm_account_one_time_keys_length(b_account));
221229
::olm_account_identity_keys(b_account, b_id_keys.data(), b_id_keys.size());
222230
::olm_account_prekey(b_account, b_pre_key.data(), b_pre_key.size());
231+
::olm_account_prekey_signature(b_account, b_pre_key_signature.data());
223232
::olm_account_one_time_keys(b_account, b_ot_keys.data(), b_ot_keys.size());
224233

225234
std::vector<std::uint8_t> a_session_buffer(::olm_session_size());
@@ -229,7 +238,9 @@ mock_random_a(a_rand.data(), a_rand.size());
229238
CHECK_NE(std::size_t(-1), ::olm_create_outbound_session(
230239
a_session, a_account,
231240
b_id_keys.data() + 15, 43, // B's curve25519 identity key
241+
b_id_keys.data() + 71, 43, // B's ed25519 signing key
232242
b_pre_key.data() + 25, 43, // B's curve25519 pre key
243+
b_pre_key_signature.data(), 86, // B's ed25519 prekey signature
233244
b_ot_keys.data() + 25, 43, // B's curve25519 one time key
234245
a_rand.data(), a_rand.size()
235246
));
@@ -369,9 +380,11 @@ ::olm_account_generate_prekey(b_account, p_random.data(), p_random.size());
369380

370381
std::vector<std::uint8_t> b_id_keys(::olm_account_identity_keys_length(b_account));
371382
std::vector<std::uint8_t> b_pre_key(::olm_account_prekey_length(b_account));
383+
std::vector<std::uint8_t> b_pre_key_signature(::olm_account_signature_length(b_account));
372384
std::vector<std::uint8_t> b_ot_keys(::olm_account_one_time_keys_length(b_account));
373385
::olm_account_identity_keys(b_account, b_id_keys.data(), b_id_keys.size());
374386
::olm_account_prekey(b_account, b_pre_key.data(), b_pre_key.size());
387+
::olm_account_prekey_signature(b_account, b_pre_key_signature.data());
375388
::olm_account_one_time_keys(b_account, b_ot_keys.data(), b_ot_keys.size());
376389

377390
std::vector<std::uint8_t> a_session_buffer(::olm_session_size());
@@ -381,7 +394,9 @@ mock_random_a(a_rand.data(), a_rand.size());
381394
CHECK_NE(std::size_t(-1), ::olm_create_outbound_session(
382395
a_session, a_account,
383396
b_id_keys.data() + 15, 43,
397+
b_id_keys.data() + 71, 43,
384398
b_pre_key.data() + 25, 43,
399+
b_pre_key_signature.data(), 86,
385400
b_ot_keys.data() + 25, 43,
386401
a_rand.data(), a_rand.size()
387402
));
@@ -492,9 +507,11 @@ mock_random_b(f_random.data(), f_random.size());
492507
::olm_account_generate_fallback_key(b_account, f_random.data(), f_random.size());
493508
std::vector<std::uint8_t> b_fb_key(::olm_account_unpublished_fallback_key_length(b_account));
494509
std::vector<std::uint8_t> b_pre_key(::olm_account_prekey_length(b_account));
510+
std::vector<std::uint8_t> b_pre_key_signature(::olm_account_signature_length(b_account));
495511

496512
::olm_account_identity_keys(b_account, b_id_keys.data(), b_id_keys.size());
497513
::olm_account_prekey(b_account, b_pre_key.data(), b_pre_key.size());
514+
::olm_account_prekey_signature(b_account, b_pre_key_signature.data());
498515
::olm_account_unpublished_fallback_key(b_account, b_fb_key.data(), b_fb_key.size());
499516

500517
// start a new olm session and encrypt a message
@@ -505,7 +522,9 @@ mock_random_a(a_rand.data(), a_rand.size());
505522
CHECK_NE(std::size_t(-1), ::olm_create_outbound_session(
506523
a_session1, a_account,
507524
b_id_keys.data() + 15, 43, // B's curve25519 identity key
525+
b_id_keys.data() + 71, 43, // B's ed25519 identity key
508526
b_pre_key.data() + 25, 43, // B's curve25519 pre key
527+
b_pre_key_signature.data(), 86, // B's ed25519 prekey signature
509528
b_fb_key.data() + 25, 43, // B's curve25519 one time key
510529
a_rand.data(), a_rand.size()
511530
));
@@ -580,7 +599,9 @@ mock_random_a(a_rand.data(), a_rand.size());
580599
CHECK_NE(std::size_t(-1), ::olm_create_outbound_session(
581600
a_session2, a_account,
582601
b_id_keys.data() + 15, 43, // B's curve25519 identity key
602+
b_id_keys.data() + 71, 43, // B's ed25519 identity key
583603
b_pre_key.data() + 25, 43, // B's curve25519 pre key
604+
b_pre_key_signature.data(), 86, // B's ed25519 prekey signature
584605
b_fb_key.data() + 25, 43, // B's curve25519 one time key
585606
a_rand.data(), a_rand.size()
586607
));
@@ -649,7 +670,9 @@ mock_random_a(a_rand.data(), a_rand.size());
649670
CHECK_NE(std::size_t(-1), ::olm_create_outbound_session(
650671
a_session3, a_account,
651672
b_id_keys.data() + 15, 43, // B's curve25519 identity key
673+
b_id_keys.data() + 71, 43, // B's ed25519 identity key
652674
b_pre_key.data() + 25, 43, // B's curve25519 pre key
675+
b_pre_key_signature.data(), 86, // B's ed25519 prekey signature
653676
b_fb_key.data() + 25, 43, // B's curve25519 one time key
654677
a_rand.data(), a_rand.size()
655678
));

tests/test_olm_using_malloc.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,15 @@ free(o_random);
7272

7373
std::size_t b_id_keys_size = ::olm_account_identity_keys_length(b_account);
7474
std::size_t b_pre_key_size = ::olm_account_prekey_length(b_account);
75+
std::size_t b_pre_key_signature_size = ::olm_account_signature_length(b_account);
7576
std::size_t b_ot_keys_size = ::olm_account_one_time_keys_length(b_account);
7677
std::uint8_t * b_id_keys = (std::uint8_t *) check_malloc(b_id_keys_size);
7778
std::uint8_t * b_pre_key = (std::uint8_t *) check_malloc(b_pre_key_size);
79+
std::uint8_t * b_pre_key_signature = (std::uint8_t *) check_malloc(b_pre_key_signature_size);
7880
std::uint8_t * b_ot_keys = (std::uint8_t *) check_malloc(b_ot_keys_size);
7981
::olm_account_identity_keys(b_account, b_id_keys, b_id_keys_size);
8082
::olm_account_prekey(b_account, b_pre_key, b_pre_key_size);
83+
::olm_account_prekey_signature(b_account, b_pre_key_signature);
8184
::olm_account_one_time_keys(b_account, b_ot_keys, b_ot_keys_size);
8285

8386
void * a_session_buffer = check_malloc(::olm_session_size());
@@ -89,7 +92,9 @@ mock_random_a(a_rand, a_rand_size);
8992
CHECK_NE(std::size_t(-1), ::olm_create_outbound_session(
9093
a_session, a_account,
9194
b_id_keys + 15, 43,
95+
b_id_keys + 71, 43,
9296
b_pre_key + 25, 43,
97+
b_pre_key_signature, 86,
9398
b_ot_keys + 25, 43,
9499
a_rand, a_rand_size
95100
));

0 commit comments

Comments
 (0)