Skip to content

Commit

Permalink
E2EE: introduce EncryptedEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
a-andreyev committed Jul 14, 2019
1 parent f1546e8 commit 7fb9f2e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ set(libqmatrixclient_SRCS
lib/events/callinviteevent.cpp
lib/events/directchatevent.cpp
lib/events/encryptionevent.cpp
lib/events/encryptedevent.cpp
lib/jobs/requestdata.cpp
lib/jobs/basejob.cpp
lib/jobs/syncjob.cpp
Expand Down
28 changes: 28 additions & 0 deletions lib/events/encryptedevent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "encryptedevent.h"

using namespace QMatrixClient;

EncryptedEvent::EncryptedEvent()
: RoomEvent(typeId(), matrixTypeId())
{ }

EncryptedEvent::EncryptedEvent(const QJsonObject &obj)
: RoomEvent(typeId(), obj)
{ }

QString EncryptedEvent::algorithm() const
{
QString algo = content<QString>(AlgorithmKeyL);
Q_ASSERT(SupportedAlgorithms.contains(algo));
return algo;
}

QString EncryptedEvent::ciphertext() const
{
return content<QString>("ciphertext"_ls);
}

QString EncryptedEvent::senderKey() const
{
return content<QString>("sender_key"_ls);
}
55 changes: 55 additions & 0 deletions lib/events/encryptedevent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

#include "roomevent.h"
#include "e2ee.h"

namespace QMatrixClient
{

/*
* While the specification states:
*
* "This event type is used when sending encrypted events.
* It can be used either within a room
* (in which case it will have all of the Room Event fields),
* or as a to-device event."
* "The encrypted payload can contain any message event."
* https://matrix.org/docs/spec/client_server/latest#id493
*
* -- for most of the cases the message event is the room message event.
* And even for the to-device events the context is for the room.
*
* So, to simplify integration to the timeline, EncryptedEvent is a RoomEvent inheritor.
* Strictly speaking though, it's not always a RoomEvent, but an Event in general.
* It's possible, because RoomEvent interface is similar to Event's one
* and doesn't add new restrictions, just provides additional features.
*/
class EncryptedEvent : public RoomEvent
{
Q_GADGET
public:
DEFINE_EVENT_TYPEID("m.room.encrypted", EncryptedEvent)
// recipient Curve25519 identity key
EncryptedEvent();
explicit EncryptedEvent(const QJsonObject& obj);

QString algorithm() const;
QString ciphertext() const;
QString senderKey() const;

// Required with Megolm:
QString deviceId() const;
QString sessionId() const;


const event_ptr_tt<RoomEvent>& decryptedEvent() const
{
return _decryptedEvent;
}

private:
event_ptr_tt<RoomEvent> _decryptedEvent;
};
REGISTER_EVENT_TYPE(EncryptedEvent)

} // namespace QMatrixClient

0 comments on commit 7fb9f2e

Please sign in to comment.