Page MenuHomePhorge

D225.1756022791.diff
No OneTemporary

Size
5 KB
Referenced Files
None
Subscribers
None

D225.1756022791.diff

diff --git a/src/client/validator.hpp b/src/base/validator.hpp
rename from src/client/validator.hpp
rename to src/base/validator.hpp
diff --git a/src/crypto/crypto.hpp b/src/crypto/crypto.hpp
--- a/src/crypto/crypto.hpp
+++ b/src/crypto/crypto.hpp
@@ -164,6 +164,18 @@
*/
bool createInboundGroupSession(KeyOfGroupSession k, std::string sessionKey, std::string ed25519Key);
+ /**
+ * Import group sessions from json.
+ *
+ * This function first validates the format. If a key passes
+ * format validation, this calls createInboundGroupSession.
+ *
+ * @param keys The json array of group session keys. In the
+ * key-export format specified in the spec.
+ * @return The number of keys successfully imported.
+ */
+ std::size_t importInboundGroupSessions(const nlohmann::json &keys);
+
bool hasInboundGroupSession(KeyOfGroupSession k) const;
std::string outboundGroupSessionInitialKey(std::string roomId);
diff --git a/src/crypto/crypto.cpp b/src/crypto/crypto.cpp
--- a/src/crypto/crypto.cpp
+++ b/src/crypto/crypto.cpp
@@ -18,7 +18,7 @@
#include <event.hpp>
#include <cursorutil.hpp>
#include <types.hpp>
-
+#include <validator.hpp>
#include "crypto-p.hpp"
#include "session-p.hpp"
#include "crypto-util-p.hpp"
@@ -368,6 +368,44 @@
return m_d->createInboundGroupSession(std::move(k), std::move(sessionKey), std::move(ed25519Key));
}
+ std::size_t Crypto::importInboundGroupSessions(const nlohmann::json &keys)
+ {
+ if (!keys.is_array()) {
+ return 0;
+ }
+ auto validateStr = identValidate(&nlohmann::json::is_string);
+ std::size_t count = 0;
+ for (const auto &data : keys) {
+ if (!data.is_object()) {
+ continue;
+ }
+
+ auto key = nlohmann::json::object();
+ if (!(cast(key, data, "algorithm", identValidate([](const auto &j) {
+ return j == "m.megolm.v1.aes-sha2";
+ })) && cast(key, data, "room_id", validateStr)
+ && cast(key, data, "sender_key", validateStr)
+ && cast(key, data, "session_id", validateStr)
+ && cast(key, data, "session_key", validateStr)
+ )) {
+ continue;
+ }
+
+ auto keyOfGroupSession = KeyOfGroupSession{
+ key["room_id"].template get<std::string>(),
+ key["session_id"].template get<std::string>(),
+ };
+ if (createInboundGroupSession(
+ keyOfGroupSession,
+ key["session_key"].template get<std::string>(),
+ key["sender_key"].template get<std::string>()
+ )) {
+ ++count;
+ }
+ }
+ return count;
+ }
+
bool Crypto::hasInboundGroupSession(KeyOfGroupSession k) const
{
return m_d->inboundGroupSessions.find(k) != m_d->inboundGroupSessions.end();
diff --git a/src/tests/crypto/key-export-test.cpp b/src/tests/crypto/key-export-test.cpp
--- a/src/tests/crypto/key-export-test.cpp
+++ b/src/tests/crypto/key-export-test.cpp
@@ -7,6 +7,7 @@
#include <libkazv-config.hpp>
#include <catch2/catch_test_macros.hpp>
#include <key-export.hpp>
+#include <crypto.hpp>
using namespace Kazv;
@@ -118,3 +119,71 @@
REQUIRE(res.reason() == DecryptKeyExportErrorCodes::NOT_JSON);
}
}
+
+TEST_CASE("Crypto::importInboundGroupSessions()")
+{
+ Crypto a(RandomTag{}, genRandomData(Crypto::constructRandomSize()));
+ std::string roomId = "!room:example.org";
+ auto sessionKey = a.rotateMegOlmSessionWithRandom(
+ genRandomData(a.rotateMegOlmSessionRandomSize()),
+ 1000,
+ roomId
+ );
+ // encrypt to get the session id
+ auto plainText = R"({
+ "content": {},
+ "type": "m.room.message",
+ "room_id": "!room:example.org"
+ })"_json;
+ auto encryptedContent = a.encryptMegOlm(plainText);
+ auto sessionId = encryptedContent["session_id"].template get<std::string>();
+ auto exportJson = nlohmann::json{
+ {
+ {"algorithm", "m.megolm.v1.aes-sha2"},
+ {"forwarding_curve25519_key_chain", {}},
+ {"room_id", roomId},
+ {"sender_claimed_keys", {
+ {"ed25519", a.ed25519IdentityKey()},
+ }},
+ {"sender_key", a.curve25519IdentityKey()},
+ {"session_id", sessionId},
+ {"session_key", sessionKey},
+ },
+ };
+
+ Crypto b(RandomTag{}, genRandomData(Crypto::constructRandomSize()));
+ WHEN("importing one")
+ {
+ auto res = b.importInboundGroupSessions(exportJson);
+ REQUIRE(res == 1);
+ }
+
+ WHEN("importing empty array")
+ {
+ auto res = b.importInboundGroupSessions(nlohmann::json::array());
+ REQUIRE(res == 0);
+ }
+
+ WHEN("importing non-array")
+ {
+ auto res = b.importInboundGroupSessions(nlohmann::json(nullptr));
+ REQUIRE(res == 0);
+ }
+
+ WHEN("importing one valid and other invalid")
+ {
+ exportJson.push_back("a");
+ auto invalidKey = exportJson[0];
+ invalidKey["session_id"] = 1;
+ exportJson.insert(exportJson.begin(), invalidKey);
+ auto res = b.importInboundGroupSessions(exportJson);
+ REQUIRE(res == 1);
+ }
+
+ WHEN("importing bad session key")
+ {
+ exportJson[0]["session_key"] = "bad_session_key";
+ auto res = b.importInboundGroupSessions(exportJson);
+ REQUIRE(res == 0);
+ }
+}

File Metadata

Mime Type
text/plain
Expires
Sun, Aug 24, 1:06 AM (17 h, 13 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
423570
Default Alt Text
D225.1756022791.diff (5 KB)

Event Timeline