Changeset View
Changeset View
Standalone View
Standalone View
src/crypto/inbound-group-session.cpp
| Show All 21 Lines | namespace Kazv | ||||
| InboundGroupSessionPrivate::InboundGroupSessionPrivate(std::string sessionKey, std::string ed25519Key) | InboundGroupSessionPrivate::InboundGroupSessionPrivate(std::string sessionKey, std::string ed25519Key) | ||||
| : InboundGroupSessionPrivate() | : InboundGroupSessionPrivate() | ||||
| { | { | ||||
| valid = false; | valid = false; | ||||
| this->ed25519Key = ed25519Key; | this->ed25519Key = ed25519Key; | ||||
| auto keyRust = checkVodozemacError([&]() { return vodozemac::megolm::session_key_from_base64(rust::Str(sessionKey)); }); | auto keyRust = checkVodozemacError([&]() { return vodozemac::megolm::session_key_from_base64(rust::Str(sessionKey)); }); | ||||
| if (!keyRust) { | if (keyRust) { | ||||
| this->session = checkVodozemacError([&]() { return vodozemac::megolm::new_inbound_group_session(*(keyRust.value())); }); | |||||
| if (this->session.has_value()) { | |||||
| valid = true; | |||||
| } | |||||
| return; | return; | ||||
| } else { | |||||
| // Try if this is the session export format | |||||
| auto exportedKeyRust = checkVodozemacError([&]() { | |||||
| return vodozemac::megolm::exported_session_key_from_base64(rust::Str(sessionKey)); | |||||
| }); | |||||
| if (!exportedKeyRust) { | |||||
| return; | |||||
| } | } | ||||
| this->session = checkVodozemacError([&]() { | |||||
| this->session = checkVodozemacError([&]() { return vodozemac::megolm::new_inbound_group_session(*(keyRust.value())); }); | return vodozemac::megolm::import_inbound_group_session(*(exportedKeyRust.value())); | ||||
| }); | |||||
| if (this->session.has_value()) { | if (this->session.has_value()) { | ||||
| isImported = true; | |||||
| valid = true; | valid = true; | ||||
| } | } | ||||
| } | } | ||||
| } | |||||
| InboundGroupSessionPrivate::InboundGroupSessionPrivate(const InboundGroupSessionPrivate &that) | InboundGroupSessionPrivate::InboundGroupSessionPrivate(const InboundGroupSessionPrivate &that) | ||||
| : InboundGroupSessionPrivate() | : InboundGroupSessionPrivate() | ||||
| { | { | ||||
| ed25519Key = that.ed25519Key; | ed25519Key = that.ed25519Key; | ||||
| if (that.valid) { | if (that.valid) { | ||||
| valid = unpickle(that.pickle()); | valid = unpickle(that.pickle()); | ||||
| } | } | ||||
| isImported = that.isImported; | |||||
| decryptedEvents = that.decryptedEvents; | decryptedEvents = that.decryptedEvents; | ||||
| } | } | ||||
| std::string InboundGroupSessionPrivate::pickle() const | std::string InboundGroupSessionPrivate::pickle() const | ||||
| { | { | ||||
| auto pickleData = this->session.value()->pickle( | auto pickleData = this->session.value()->pickle( | ||||
| VODOZEMAC_PICKLE_KEY); | VODOZEMAC_PICKLE_KEY); | ||||
| return static_cast<std::string>(pickleData); | return static_cast<std::string>(pickleData); | ||||
| ▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | InboundGroupSession &InboundGroupSession::operator=(InboundGroupSession &&that) | ||||
| return *this; | return *this; | ||||
| } | } | ||||
| bool InboundGroupSession::valid() const | bool InboundGroupSession::valid() const | ||||
| { | { | ||||
| return m_d && m_d->valid; | return m_d && m_d->valid; | ||||
| } | } | ||||
| bool InboundGroupSession::isImported() const | |||||
| { | |||||
| return m_d->isImported; | |||||
| } | |||||
| MaybeString InboundGroupSession::decrypt(std::string message, std::string eventId, std::int_fast64_t originServerTs) | MaybeString InboundGroupSession::decrypt(std::string message, std::string eventId, std::int_fast64_t originServerTs) | ||||
| { | { | ||||
| auto messageRust = checkVodozemacError([&]() { return vodozemac::megolm::megolm_message_from_base64(rust::Str(message)); }); | auto messageRust = checkVodozemacError([&]() { return vodozemac::megolm::megolm_message_from_base64(rust::Str(message)); }); | ||||
| if (!messageRust.has_value()) { | if (!messageRust.has_value()) { | ||||
| return NotBut(messageRust.reason()); | return NotBut(messageRust.reason()); | ||||
| } | } | ||||
| auto decrypted = checkVodozemacError([&]() { return m_d->session.value()->decrypt(*(messageRust.value())); }); | auto decrypted = checkVodozemacError([&]() { return m_d->session.value()->decrypt(*(messageRust.value())); }); | ||||
| Show All 33 Lines | bool InboundGroupSession::merge(InboundGroupSession &that) | ||||
| auto merged = checkVodozemacError([this, &that]() { | auto merged = checkVodozemacError([this, &that]() { | ||||
| return m_d->session.value()->merge(*that.m_d->session.value()); | return m_d->session.value()->merge(*that.m_d->session.value()); | ||||
| }); | }); | ||||
| if (!merged.has_value()) { | if (!merged.has_value()) { | ||||
| return false; | return false; | ||||
| } else { | } else { | ||||
| m_d->session = std::move(merged); | m_d->session = std::move(merged); | ||||
| if (!m_d->isImported || !that.m_d->isImported) { | |||||
| m_d->isImported = false; | |||||
| } | |||||
| return true; | return true; | ||||
| } | } | ||||
| } | } | ||||
| std::string InboundGroupSession::toExportFormat() const | |||||
| { | |||||
| auto exported = m_d->session.value()->export_at(m_d->session.value()->first_known_index()); | |||||
| auto exportedStr = exported->to_base64(); | |||||
| return std::string(exportedStr); | |||||
| } | |||||
| void to_json(nlohmann::json &j, const InboundGroupSession &s) | void to_json(nlohmann::json &j, const InboundGroupSession &s) | ||||
| { | { | ||||
| j = nlohmann::json::object(); | j = nlohmann::json::object(); | ||||
| j["version"] = 1; | j["version"] = 1; | ||||
| j["ed25519Key"] = s.m_d->ed25519Key; | j["ed25519Key"] = s.m_d->ed25519Key; | ||||
| j["valid"] = s.m_d->valid; | j["valid"] = s.m_d->valid; | ||||
| j["decryptedEvents"] = s.m_d->decryptedEvents; | j["decryptedEvents"] = s.m_d->decryptedEvents; | ||||
| if (s.m_d->valid) { | if (s.m_d->valid) { | ||||
| j["session"] = s.m_d->pickle(); | j["session"] = s.m_d->pickle(); | ||||
| j["isImported"] = s.m_d->isImported; | |||||
| } | } | ||||
| } | } | ||||
| void from_json(const nlohmann::json &j, InboundGroupSession &s) | void from_json(const nlohmann::json &j, InboundGroupSession &s) | ||||
| { | { | ||||
| s.m_d->ed25519Key = j.at("ed25519Key"); | s.m_d->ed25519Key = j.at("ed25519Key"); | ||||
| s.m_d->valid = j.at("valid"); | s.m_d->valid = j.at("valid"); | ||||
| s.m_d->decryptedEvents = j.at("decryptedEvents"); | s.m_d->decryptedEvents = j.at("decryptedEvents"); | ||||
| if (s.m_d->valid) { | if (s.m_d->valid) { | ||||
| if (j.contains("version") && j["version"] == 1) { // vodozemac format | if (j.contains("version") && j["version"] == 1) { // vodozemac format | ||||
| s.m_d->valid = s.m_d->unpickle(j.at("session")); | s.m_d->valid = s.m_d->unpickle(j.at("session")); | ||||
| } else { // libolm format | } else { // libolm format | ||||
| s.m_d->valid = s.m_d->unpickleFromLibolm(j.at("session")); | s.m_d->valid = s.m_d->unpickleFromLibolm(j.at("session")); | ||||
| } | |||||
| if (j.contains("isImported")) { | |||||
| s.m_d->isImported = j["isImported"].template get<bool>(); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||