mtx::crypto::OlmClient class

Client for all the cryptography related functionality like olm accounts, session keys encryption, signing and a few more things.

Public types

struct CrossSigningSetup
Data needed for bootstrapping crosssigning.
struct OnlineKeyBackupSetup
Data needed to setup the online key backup.
struct SSSSSetup
Data needed to setup SSSS.
using Base64String = std::string
Base64 encoded string.
using SignedOneTimeKeys = std::map<std::string, requests::SignedOneTimeKey>
A signed set of one time keys indexed by <algorithm>:<key_id>.

Public static functions

static auto create_ssss_key(const std::string& password = "") -> std::optional<SSSSSetup>
Create a new SSSS storage key. Should be uploaded to account_data. The password is optional.

Constructors, destructors, conversion operators

OlmClient(std::string user_id, std::string device_id)
Initialize a crypto client for the specified device of the specified user.

Public functions

void set_device_id(std::string device_id)
Set the id of this device.
void set_user_id(std::string user_id)
Set the id of this user.
auto sign_message(const std::string& msg) const -> Base64String
Sign the given message.
void create_new_account()
Create a new olm Account. Must be called before any other operation.
void create_new_utility()
Create a new olm utility object.
void restore_account(const std::string& saved_data, const std::string& key)
Restore the olm account from a pickled string encrypted by key
auto identity_keys() const -> IdentityKeys
Retrieve the json representation of the identity keys for the given account.
auto sign_identity_keys() -> Base64String
Sign the identity keys. The result should be used as part of the /keys/upload/ request.
auto generate_one_time_keys(std::size_t nkeys, bool generate_fallback = false) -> std::size_t
Generate a number of one time keys.
auto one_time_keys() -> OneTimeKeys
Retrieve the json representation of the one time keys for the given account.
auto unpublished_fallback_keys() -> OneTimeKeys
Retrieve the json representation of the unpublished fallback one time keys for the given account.
auto sign_one_time_key(const Base64String& encoded_key, bool fallback = false) -> Base64String
Sign the given one time keys and encode it to base64.
auto sign_one_time_keys(const OneTimeKeys& keys, bool fallback = false) -> SignedOneTimeKeys
Sign one_time_keys and generate the appropriate structure for the /keys/upload request.
auto signed_one_time_key(const std::string& key, const std::string& signature, bool fallback = false) -> requests::SignedOneTimeKey
Generate the json structure for the signed one time key.
void mark_keys_as_published()
Marks the current set of one time keys as being published.
void forget_old_fallback_key()
Forgets an old fallback key. Call this when you are sure the old key is no longer in use, i.e. 5 minutes after publishing a new one.
auto create_upload_keys_request(const OneTimeKeys& keys, const OneTimeKeys& fallback_keys) -> mtx::requests::UploadKeys
Prepare request for the /keys/upload endpoint by signing identity & one time keys.
auto create_upload_keys_request() -> mtx::requests::UploadKeys
Prepare an empty /keys/upload request.
auto create_crosssigning_keys() -> std::optional<CrossSigningSetup>
Create the cross-signing keys (including signatures). Needs to be uploaded to the server after this.
auto create_online_key_backup(const std::string& masterKey) -> std::optional<OnlineKeyBackupSetup>
Create a new online key backup. Needs to be uploaded to the server after this.
auto decrypt_group_message(OlmInboundGroupSession* session, const std::string& message, uint32_t message_index = 0) -> GroupPlaintext
Decrypt a message using megolm.
auto encrypt_group_message(OlmOutboundGroupSession* session, const std::string& plaintext) -> BinaryBuf
Encrypt a message using megolm.
auto encrypt_message(OlmSession* session, const std::string& msg) -> BinaryBuf
Encrypt a message using olm.
auto decrypt_message(OlmSession* session, std::size_t msg_type, const std::string& msg) -> BinaryBuf
Decrypt a message using olm.
auto init_outbound_group_session() -> OutboundGroupSessionPtr
Create an outbound megolm session.
auto init_inbound_group_session(const std::string& session_key) -> InboundGroupSessionPtr
Initialize an inbound group session from a shared session key (an m.room_key event).
auto import_inbound_group_session(const std::string& session_key) -> InboundGroupSessionPtr
Initialize an inbound group session from a forwarded session key (an m.forwarded_room_key event).
auto create_outbound_session(const std::string& identity_key, const std::string& one_time_key) -> OlmSessionPtr
create an outbound session to encrypt to device messages.
auto create_inbound_session(const BinaryBuf& one_time_key_message) -> OlmSessionPtr
Creates an inbound session from an inbound message. DON'T USE THIS, use create_inbound_session_from() instead.
auto create_inbound_session(const std::string& one_time_key_message) -> OlmSessionPtr
Creates an inbound session from an inbound message. DON'T USE THIS, use create_inbound_session_from() instead.
auto create_inbound_session_from(const std::string& their_curve25519, const BinaryBuf& one_time_key_message) -> OlmSessionPtr
Create an inbound olm session from the other users message and identity key.
auto create_inbound_session_from(const std::string& their_curve25519, const std::string& one_time_key_message) -> OlmSessionPtr
Create an inbound olm session from the other users message and identity key.
auto create_olm_encrypted_content(OlmSession* session, nlohmann::json event, const UserId& recipient, const std::string& recipient_ed25519_key, const std::string& recipient_curve25519_key) -> nlohmann::json
Create the content for an m.room.encrypted event. algorithm: m.olm.v1.curve25519-aes-sha2.
auto save(const std::string& key) -> std::string
store the account in a pickled string encrypted by key
void load(const std::string& data, const std::string& key)
Restore the account from a pickled string encrypted by key
auto account() -> OlmAccount*
Access the olm account directly.
auto utility() -> OlmUtility*
Access the olm utility object directly.
auto sas_init() -> std::unique_ptr<SAS>
SAS related stuff this creates a unique pointer of struct SAS.

Function documentation

void mtx::crypto::OlmClient::restore_account(const std::string& saved_data, const std::string& key)

Restore the olm account from a pickled string encrypted by key

OutboundGroupSessionPtr mtx::crypto::OlmClient::init_outbound_group_session()

Create an outbound megolm session.

InboundGroupSessionPtr mtx::crypto::OlmClient::init_inbound_group_session(const std::string& session_key)

Initialize an inbound group session from a shared session key (an m.room_key event).

InboundGroupSessionPtr mtx::crypto::OlmClient::import_inbound_group_session(const std::string& session_key)

Initialize an inbound group session from a forwarded session key (an m.forwarded_room_key event).

OlmSessionPtr mtx::crypto::OlmClient::create_outbound_session(const std::string& identity_key, const std::string& one_time_key)

create an outbound session to encrypt to device messages.

Parameters
identity_key The curve25519 key of the other party.
one_time_key The claimed one time key of the other party.

OlmSessionPtr mtx::crypto::OlmClient::create_inbound_session(const BinaryBuf& one_time_key_message)

Creates an inbound session from an inbound message. DON'T USE THIS, use create_inbound_session_from() instead.

OlmSessionPtr mtx::crypto::OlmClient::create_inbound_session(const std::string& one_time_key_message)

Creates an inbound session from an inbound message. DON'T USE THIS, use create_inbound_session_from() instead.

OlmSessionPtr mtx::crypto::OlmClient::create_inbound_session_from(const std::string& their_curve25519, const BinaryBuf& one_time_key_message)

Create an inbound olm session from the other users message and identity key.

OlmSessionPtr mtx::crypto::OlmClient::create_inbound_session_from(const std::string& their_curve25519, const std::string& one_time_key_message)

Create an inbound olm session from the other users message and identity key.

void mtx::crypto::OlmClient::load(const std::string& data, const std::string& key)

Restore the account from a pickled string encrypted by key