Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F7680098
D228.1756686792.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
D228.1756686792.diff
View Options
diff --git a/src/crypto/inbound-group-session-p.hpp b/src/crypto/inbound-group-session-p.hpp
--- a/src/crypto/inbound-group-session-p.hpp
+++ b/src/crypto/inbound-group-session-p.hpp
@@ -57,6 +57,7 @@
std::string ed25519Key;
bool valid{false};
+ bool isImported{false};
immer::map<std::uint32_t /* index */, KeyOfDecryptedEvent> decryptedEvents;
diff --git a/src/crypto/inbound-group-session.hpp b/src/crypto/inbound-group-session.hpp
--- a/src/crypto/inbound-group-session.hpp
+++ b/src/crypto/inbound-group-session.hpp
@@ -21,6 +21,7 @@
class InboundGroupSession
{
public:
+ struct ImportTag {};
explicit InboundGroupSession();
explicit InboundGroupSession(std::string sessionKey, std::string ed25519Key);
@@ -34,6 +35,15 @@
bool valid() const;
+ /**
+ * Check whether this session is imported (from the session-export format).
+ *
+ * Precondition: valid() is true.
+ *
+ * @return true iff this session is imported.
+ */
+ bool isImported() const;
+
std::string ed25519Key() const;
/**
@@ -43,6 +53,16 @@
* @return true iff the two sessions are mergeable.
*/
bool merge(InboundGroupSession &that);
+
+ /**
+ * Export the session to session-export format.
+ *
+ * Precondition: valid() is true.
+ *
+ * @return The base64 encoded session-export format.
+ */
+ std::string toExportFormat() const;
+
private:
friend void to_json(nlohmann::json &j, const InboundGroupSession &s);
friend void from_json(const nlohmann::json &j, InboundGroupSession &s);
diff --git a/src/crypto/inbound-group-session.cpp b/src/crypto/inbound-group-session.cpp
--- a/src/crypto/inbound-group-session.cpp
+++ b/src/crypto/inbound-group-session.cpp
@@ -27,13 +27,27 @@
this->ed25519Key = ed25519Key;
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;
- }
-
- this->session = checkVodozemacError([&]() { return vodozemac::megolm::new_inbound_group_session(*(keyRust.value())); });
- if (this->session.has_value()) {
- valid = true;
+ } 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([&]() {
+ return vodozemac::megolm::import_inbound_group_session(*(exportedKeyRust.value()));
+ });
+ if (this->session.has_value()) {
+ isImported = true;
+ valid = true;
+ }
}
}
@@ -44,6 +58,7 @@
if (that.valid) {
valid = unpickle(that.pickle());
}
+ isImported = that.isImported;
decryptedEvents = that.decryptedEvents;
}
@@ -109,6 +124,11 @@
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)
{
auto messageRust = checkVodozemacError([&]() { return vodozemac::megolm::megolm_message_from_base64(rust::Str(message)); });
@@ -158,10 +178,20 @@
return false;
} else {
m_d->session = std::move(merged);
+ if (!m_d->isImported || !that.m_d->isImported) {
+ m_d->isImported = false;
+ }
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)
{
j = nlohmann::json::object();
@@ -171,6 +201,7 @@
j["decryptedEvents"] = s.m_d->decryptedEvents;
if (s.m_d->valid) {
j["session"] = s.m_d->pickle();
+ j["isImported"] = s.m_d->isImported;
}
}
@@ -185,6 +216,9 @@
} else { // libolm format
s.m_d->valid = s.m_d->unpickleFromLibolm(j.at("session"));
}
+ if (j.contains("isImported")) {
+ s.m_d->isImported = j["isImported"].template get<bool>();
+ }
}
}
}
diff --git a/src/tests/crypto/inbound-group-session-test.cpp b/src/tests/crypto/inbound-group-session-test.cpp
--- a/src/tests/crypto/inbound-group-session-test.cpp
+++ b/src/tests/crypto/inbound-group-session-test.cpp
@@ -20,6 +20,7 @@
auto sessionJson = resource["a"]["inboundGroupSessions"][0][1];
auto session = sessionJson.template get<InboundGroupSession>();
REQUIRE(session.valid());
+ REQUIRE(!session.isImported());
REQUIRE(session.ed25519Key() == sessionJson["ed25519Key"]);
auto encrypted = resource["megolmEncrypted"];
auto plainText = resource["megolmPlainText"];
@@ -82,3 +83,26 @@
auto session2 = session;
REQUIRE(!session.valid());
}
+
+TEST_CASE("export and import InboundGroupSession", "[crypto]")
+{
+ auto ogs = OutboundGroupSession(RandomTag{}, genRandomData(OutboundGroupSession::constructRandomSize()), 0);
+ auto igs = InboundGroupSession(ogs.sessionKey(), "placeholder");
+ auto exported = igs.toExportFormat();
+ auto imported = InboundGroupSession(exported, "placeholder");
+ REQUIRE(imported.valid());
+ REQUIRE(imported.isImported());
+ WHEN("try to decrypt") {
+ auto encrypted1 = ogs.encrypt("text");
+ auto res = imported.decrypt(encrypted1, "$1", 1234);
+ REQUIRE(res.has_value());
+ REQUIRE(res.value() == "text");
+ }
+
+ WHEN("serialization") {
+ auto j = json(imported);
+ auto deserialized = j.template get<InboundGroupSession>();
+ REQUIRE(deserialized.valid());
+ REQUIRE(deserialized.isImported());
+ }
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Aug 31, 5:33 PM (9 h, 9 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
435322
Default Alt Text
D228.1756686792.diff (6 KB)
Attached To
Mode
D228: Accept session-export format in InboundGroupSession constructor
Attached
Detach File
Event Timeline
Log In to Comment