2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
2022-01-01 06:57:53 +03:00
|
|
|
// SPDX-FileCopyrightText: 2022 Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2018-06-10 20:03:45 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
2020-01-31 19:00:13 +03:00
|
|
|
#include <mtx/events.hpp>
|
|
|
|
#include <mtx/events/encrypted.hpp>
|
2018-06-10 20:03:45 +03:00
|
|
|
#include <mtxclient/crypto/client.hpp>
|
|
|
|
|
2020-08-06 22:46:16 +03:00
|
|
|
#include <CacheCryptoStructs.h>
|
|
|
|
|
2018-06-10 20:03:45 +03:00
|
|
|
constexpr auto OLM_ALGO = "m.olm.v1.curve25519-aes-sha2";
|
|
|
|
|
|
|
|
namespace olm {
|
2021-08-07 23:51:09 +03:00
|
|
|
Q_NAMESPACE
|
2018-06-10 20:03:45 +03:00
|
|
|
|
2021-08-07 23:51:09 +03:00
|
|
|
enum DecryptionErrorCode
|
2020-08-06 22:46:16 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
NoError,
|
|
|
|
MissingSession, // Session was not found, retrieve from backup or request from other devices
|
|
|
|
// and try again
|
|
|
|
MissingSessionIndex, // Session was found, but it does not reach back enough to this index,
|
|
|
|
// retrieve from backup or request from other devices and try again
|
|
|
|
DbError, // DB read failed
|
|
|
|
DecryptionFailed, // libolm error
|
|
|
|
ParsingFailed, // Failed to parse the actual event
|
|
|
|
ReplayAttack, // Megolm index reused
|
2020-08-06 22:46:16 +03:00
|
|
|
};
|
2021-08-07 23:51:09 +03:00
|
|
|
Q_ENUM_NS(DecryptionErrorCode)
|
2020-08-06 22:46:16 +03:00
|
|
|
|
|
|
|
struct DecryptionResult
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
DecryptionErrorCode error;
|
|
|
|
std::optional<std::string> error_message;
|
|
|
|
std::optional<mtx::events::collections::TimelineEvents> event;
|
2020-08-06 22:46:16 +03:00
|
|
|
};
|
|
|
|
|
2018-06-10 20:03:45 +03:00
|
|
|
struct OlmMessage
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string sender_key;
|
|
|
|
std::string sender;
|
2018-06-10 20:03:45 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
using RecipientKey = std::string;
|
|
|
|
std::map<RecipientKey, mtx::events::msg::OlmCipherContent> ciphertext;
|
2018-06-10 20:03:45 +03:00
|
|
|
};
|
|
|
|
|
2020-10-27 19:45:28 +03:00
|
|
|
void
|
|
|
|
from_json(const nlohmann::json &obj, OlmMessage &msg);
|
2018-06-10 20:03:45 +03:00
|
|
|
|
|
|
|
mtx::crypto::OlmClient *
|
|
|
|
client();
|
|
|
|
|
|
|
|
void
|
2020-05-12 20:09:53 +03:00
|
|
|
handle_to_device_messages(const std::vector<mtx::events::collections::DeviceEvents> &msgs);
|
2018-06-10 20:03:45 +03:00
|
|
|
|
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
|
2021-07-17 02:27:37 +03:00
|
|
|
handle_olm_message(const OlmMessage &msg, const UserKeyCache &otherUserDeviceKeys);
|
2018-06-10 20:03:45 +03:00
|
|
|
|
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
|
2020-10-02 14:46:32 +03:00
|
|
|
create_inbound_megolm_session(const mtx::events::DeviceEvent<mtx::events::msg::RoomKey> &roomKey,
|
2021-07-17 02:27:37 +03:00
|
|
|
const std::string &sender_key,
|
|
|
|
const std::string &sender_ed25519);
|
2018-06-10 20:03:45 +03:00
|
|
|
void
|
2020-10-02 14:46:32 +03:00
|
|
|
import_inbound_megolm_session(
|
|
|
|
const mtx::events::DeviceEvent<mtx::events::msg::ForwardedRoomKey> &roomKey);
|
2021-08-17 04:23:51 +03:00
|
|
|
void
|
|
|
|
lookup_keybackup(const std::string room, const std::string session_id);
|
2020-10-02 14:46:32 +03:00
|
|
|
|
|
|
|
nlohmann::json
|
2018-06-10 20:03:45 +03:00
|
|
|
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,
|
2020-01-13 14:37:02 +03:00
|
|
|
nlohmann::json body);
|
2018-06-13 12:28:00 +03:00
|
|
|
|
2021-08-08 00:54:35 +03:00
|
|
|
//! Decrypt an event. Use dont_write_db to prevent db writes when already in a write transaction.
|
2020-08-06 22:46:16 +03:00
|
|
|
DecryptionResult
|
|
|
|
decryptEvent(const MegolmSessionIndex &index,
|
2021-08-08 00:54:35 +03:00
|
|
|
const mtx::events::EncryptedEvent<mtx::events::msg::Encrypted> &event,
|
|
|
|
bool dont_write_db = false);
|
2021-05-07 13:19:46 +03:00
|
|
|
crypto::Trust
|
2021-08-17 04:23:51 +03:00
|
|
|
calculate_trust(const std::string &user_id, const MegolmSessionIndex &index);
|
2020-08-06 22:46:16 +03:00
|
|
|
|
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
|
2020-10-20 20:46:37 +03:00
|
|
|
send_key_request_for(mtx::events::EncryptedEvent<mtx::events::msg::Encrypted> e,
|
|
|
|
const std::string &request_id,
|
|
|
|
bool cancel = false);
|
2018-07-07 23:59:23 +03:00
|
|
|
|
|
|
|
void
|
2020-05-12 20:09:53 +03:00
|
|
|
handle_key_request_message(const mtx::events::DeviceEvent<mtx::events::msg::KeyRequest> &);
|
2018-07-07 23:59:23 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
send_megolm_key_to_device(const std::string &user_id,
|
|
|
|
const std::string &device_id,
|
2020-10-18 03:02:14 +03:00
|
|
|
const mtx::events::msg::ForwardedRoomKey &payload);
|
2018-07-07 23:59:23 +03:00
|
|
|
|
2020-10-20 19:10:09 +03:00
|
|
|
//! Send encrypted to device messages, targets is a map from userid to device ids or {} for all
|
|
|
|
//! devices
|
2020-10-20 14:46:05 +03:00
|
|
|
void
|
|
|
|
send_encrypted_to_device_messages(const std::map<std::string, std::vector<std::string>> targets,
|
2020-10-20 19:10:09 +03:00
|
|
|
const mtx::events::collections::DeviceEvents &event,
|
|
|
|
bool force_new_session = false);
|
2020-10-20 14:46:05 +03:00
|
|
|
|
2020-12-15 17:48:33 +03:00
|
|
|
//! Request backup and signing keys and cache them locally
|
|
|
|
void
|
|
|
|
request_cross_signing_keys();
|
|
|
|
//! Download backup and signing keys and cache them locally
|
|
|
|
void
|
|
|
|
download_cross_signing_keys();
|
|
|
|
|
2018-06-10 20:03:45 +03:00
|
|
|
} // namespace olm
|