2018-06-10 20:03:45 +03:00
|
|
|
#pragma once
|
|
|
|
|
2018-06-15 01:35:31 +03:00
|
|
|
#include <boost/optional.hpp>
|
|
|
|
|
2018-06-10 20:03:45 +03:00
|
|
|
#include <memory>
|
2018-06-13 12:28:00 +03:00
|
|
|
#include <mtx.hpp>
|
2018-06-10 20:03:45 +03:00
|
|
|
#include <mtxclient/crypto/client.hpp>
|
|
|
|
|
|
|
|
constexpr auto OLM_ALGO = "m.olm.v1.curve25519-aes-sha2";
|
|
|
|
|
|
|
|
namespace olm {
|
|
|
|
|
|
|
|
struct OlmMessage
|
|
|
|
{
|
|
|
|
std::string sender_key;
|
|
|
|
std::string sender;
|
|
|
|
|
|
|
|
using RecipientKey = std::string;
|
2018-06-27 21:24:25 +03:00
|
|
|
std::map<RecipientKey, mtx::events::msg::OlmCipherContent> ciphertext;
|
2018-06-10 20:03:45 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
inline void
|
|
|
|
from_json(const nlohmann::json &obj, OlmMessage &msg)
|
|
|
|
{
|
|
|
|
if (obj.at("type") != "m.room.encrypted")
|
|
|
|
throw std::invalid_argument("invalid type for olm message");
|
|
|
|
|
|
|
|
if (obj.at("content").at("algorithm") != OLM_ALGO)
|
|
|
|
throw std::invalid_argument("invalid algorithm for olm message");
|
|
|
|
|
|
|
|
msg.sender = obj.at("sender");
|
|
|
|
msg.sender_key = obj.at("content").at("sender_key");
|
2018-06-27 21:24:25 +03:00
|
|
|
msg.ciphertext = obj.at("content")
|
|
|
|
.at("ciphertext")
|
|
|
|
.get<std::map<std::string, mtx::events::msg::OlmCipherContent>>();
|
2018-06-10 20:03:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mtx::crypto::OlmClient *
|
|
|
|
client();
|
|
|
|
|
|
|
|
void
|
|
|
|
handle_to_device_messages(const std::vector<nlohmann::json> &msgs);
|
|
|
|
|
2018-07-14 16:27:51 +03:00
|
|
|
nlohmann::json
|
2018-06-27 21:24:25 +03:00
|
|
|
try_olm_decryption(const std::string &sender_key,
|
|
|
|
const mtx::events::msg::OlmCipherContent &content);
|
2018-06-15 01:35:31 +03:00
|
|
|
|
2018-06-10 20:03:45 +03:00
|
|
|
void
|
|
|
|
handle_olm_message(const OlmMessage &msg);
|
|
|
|
|
2018-06-15 01:35:31 +03:00
|
|
|
//! Establish a new inbound megolm session with the decrypted payload from olm.
|
2018-06-10 20:03:45 +03:00
|
|
|
void
|
2018-06-15 01:35:31 +03:00
|
|
|
create_inbound_megolm_session(const std::string &sender,
|
|
|
|
const std::string &sender_key,
|
|
|
|
const nlohmann::json &payload);
|
2018-06-10 20:03:45 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
handle_pre_key_olm_message(const std::string &sender,
|
|
|
|
const std::string &sender_key,
|
2018-06-27 21:24:25 +03:00
|
|
|
const mtx::events::msg::OlmCipherContent &content);
|
2018-06-13 12:28:00 +03:00
|
|
|
|
|
|
|
mtx::events::msg::Encrypted
|
|
|
|
encrypt_group_message(const std::string &room_id,
|
|
|
|
const std::string &device_id,
|
|
|
|
const std::string &body);
|
|
|
|
|
2018-06-18 12:56:47 +03:00
|
|
|
void
|
|
|
|
mark_keys_as_published();
|
|
|
|
|
2018-07-07 23:59:23 +03:00
|
|
|
//! Request the encryption keys from sender's device for the given event.
|
|
|
|
void
|
|
|
|
request_keys(const std::string &room_id, const std::string &event_id);
|
|
|
|
|
|
|
|
void
|
|
|
|
send_key_request_for(const std::string &room_id,
|
|
|
|
const mtx::events::EncryptedEvent<mtx::events::msg::Encrypted> &);
|
|
|
|
|
|
|
|
void
|
|
|
|
handle_key_request_message(const mtx::events::msg::KeyRequest &);
|
|
|
|
|
|
|
|
void
|
|
|
|
send_megolm_key_to_device(const std::string &user_id,
|
|
|
|
const std::string &device_id,
|
|
|
|
const json &payload);
|
|
|
|
|
2018-06-10 20:03:45 +03:00
|
|
|
} // namespace olm
|