Skip to content
Snippets Groups Projects
Commit 755cc434 authored by Konstantinos Sideris's avatar Konstantinos Sideris
Browse files

Add methods for importing/exporting inbound megolm sessions

parent 3328fded
No related branches found
No related tags found
No related merge requests found
...@@ -214,10 +214,18 @@ session_id(OlmSession *s); ...@@ -214,10 +214,18 @@ session_id(OlmSession *s);
std::string std::string
session_id(OlmOutboundGroupSession *s); session_id(OlmOutboundGroupSession *s);
//! Retrieve the session key. //! Retrieve the session key from an *outbound* megolm session.
std::string std::string
session_key(OlmOutboundGroupSession *s); session_key(OlmOutboundGroupSession *s);
//! Retrieve the session key from an *inbound* megolm session.
std::string
export_session(OlmInboundGroupSession *s);
//! Create an *inbound* megolm session from an exported session key.
InboundGroupSessionPtr
import_session(const std::string &session_key);
bool bool
matches_inbound_session(OlmSession *session, const std::string &one_time_key_message); matches_inbound_session(OlmSession *session, const std::string &one_time_key_message);
......
...@@ -437,6 +437,36 @@ mtx::crypto::session_key(OlmOutboundGroupSession *s) ...@@ -437,6 +437,36 @@ mtx::crypto::session_key(OlmOutboundGroupSession *s)
return std::string(tmp.begin(), tmp.end()); return std::string(tmp.begin(), tmp.end());
} }
std::string
mtx::crypto::export_session(OlmInboundGroupSession *s)
{
const size_t len = olm_export_inbound_group_session_length(s);
const uint32_t index = olm_inbound_group_session_first_known_index(s);
auto session_key = create_buffer(len);
const int rc =
olm_export_inbound_group_session(s, session_key.data(), session_key.size(), index);
if (rc == -1)
throw olm_exception("session_key", s);
return std::string(session_key.begin(), session_key.end());
}
InboundGroupSessionPtr
mtx::crypto::import_session(const std::string &session_key)
{
auto session = create_olm_object<InboundSessionObject>();
const int rc = olm_import_inbound_group_session(
session.get(), reinterpret_cast<const uint8_t *>(session_key.data()), session_key.size());
if (rc == -1)
throw olm_exception("import_session", session.get());
return session;
}
bool bool
mtx::crypto::matches_inbound_session(OlmSession *session, const std::string &one_time_key_message) mtx::crypto::matches_inbound_session(OlmSession *session, const std::string &one_time_key_message)
{ {
......
...@@ -1071,6 +1071,65 @@ TEST(ExportSessions, EncryptDecrypt) ...@@ -1071,6 +1071,65 @@ TEST(ExportSessions, EncryptDecrypt)
EXPECT_EQ(json(keys).dump(), json(restored_keys).dump()); EXPECT_EQ(json(keys).dump(), json(restored_keys).dump());
} }
TEST(ExportSessions, InboundMegolmSessions)
{
auto alice = std::make_shared<OlmClient>();
alice->create_new_account();
alice->generate_one_time_keys(1);
auto bob = std::make_shared<OlmClient>();
bob->create_new_account();
bob->generate_one_time_keys(1);
// ==================== SESSION SETUP =================== //
// Alice wants to send an encrypted megolm message to Bob.
const std::string secret_message = "Hey, Bob!";
// Alice creates an outbound megolm session that will be used by both parties.
auto outbound_megolm_session = alice->init_outbound_group_session();
auto msg_index = olm_outbound_group_session_message_index(outbound_megolm_session.get());
ASSERT_EQ(msg_index, 0);
// Alice extracts the session id & session key so she can share them with Bob.
const auto session_id = mtx::crypto::session_id(outbound_megolm_session.get());
const auto session_key = mtx::crypto::session_key(outbound_megolm_session.get());
// Encrypt the message using megolm.
auto encrypted_secret_message =
alice->encrypt_group_message(outbound_megolm_session.get(), secret_message);
msg_index = olm_outbound_group_session_message_index(outbound_megolm_session.get());
ASSERT_EQ(msg_index, 1);
// Bob will use the session_key to create an inbound megolm session.
// The session_id will be used to map future messages to this session.
auto inbound_megolm_session = bob->init_inbound_group_session(session_key);
// Bob can finally decrypt Alice's original message.
auto ciphertext =
std::string((char *)encrypted_secret_message.data(), encrypted_secret_message.size());
auto bob_plaintext = bob->decrypt_group_message(inbound_megolm_session.get(), ciphertext);
auto output_str = std::string((char *)bob_plaintext.data.data(), bob_plaintext.data.size());
ASSERT_EQ(output_str, secret_message);
// ==================== SESSION IMPORT/EXPORT =================== //
auto exported_session_key = export_session(inbound_megolm_session.get());
auto restored_inbound_session = import_session(exported_session_key);
// Decrypt message again.
auto restored_ciphertext =
std::string((char *)encrypted_secret_message.data(), encrypted_secret_message.size());
auto restored_plaintext =
bob->decrypt_group_message(restored_inbound_session.get(), restored_ciphertext);
auto restored_output_str =
std::string((char *)restored_plaintext.data.data(), restored_plaintext.data.size());
ASSERT_EQ(restored_output_str, secret_message);
}
TEST(Encryption, DISABLED_HandleRoomKeyEvent) {} TEST(Encryption, DISABLED_HandleRoomKeyEvent) {}
TEST(Encryption, DISABLED_HandleRoomKeyRequestEvent) {} TEST(Encryption, DISABLED_HandleRoomKeyRequestEvent) {}
TEST(Encryption, DISABLED_HandleNewDevices) {} TEST(Encryption, DISABLED_HandleNewDevices) {}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment