aboutsummaryrefslogtreecommitdiff
path: root/lib/events/encryptedevent.h
blob: 4cc3bf8ebde4d21e6d468c5a5ae29882e2e92d1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-FileCopyrightText: 2019 Alexey Andreyev <aa13q@ya.ru>
// SPDX-License-Identifier: LGPL-2.1-or-later

#pragma once

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

namespace Quotient {
/*
 * 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 {
public:
    DEFINE_EVENT_TYPEID("m.room.encrypted", EncryptedEvent)

    /* In case with Olm, the encrypted content of the event is
     * a map from the recipient Curve25519 identity key to ciphertext
     * information */
    explicit EncryptedEvent(const QJsonObject& ciphertext,
                            const QString& senderKey);
    /* In case with Megolm, device_id and session_id are required */
    explicit EncryptedEvent(QByteArray ciphertext, const QString& senderKey,
                            const QString& deviceId, const QString& sessionId);
    explicit EncryptedEvent(const QJsonObject& obj);

    QString algorithm() const
    {
        QString algo = contentPart<QString>(AlgorithmKeyL);
        if (!SupportedAlgorithms.contains(algo)) {
            qWarning(MAIN) << "The EncryptedEvent's algorithm" << algo
                           << "is not supported";
        }
        return algo;
    }
    QByteArray ciphertext() const
    {
        return contentPart<QString>(CiphertextKeyL).toLatin1();
    }
    QJsonObject ciphertext(const QString& identityKey) const
    {
        return contentPart<QJsonObject>(CiphertextKeyL).value(identityKey).toObject();
    }
    QString senderKey() const { return contentPart<QString>(SenderKeyKeyL); }

    /* device_id and session_id are required with Megolm */
    QString deviceId() const { return contentPart<QString>(DeviceIdKeyL); }
    QString sessionId() const { return contentPart<QString>(SessionIdKeyL); }
    RoomEventPtr createDecrypted(const QString &decrypted) const;
};
REGISTER_EVENT_TYPE(EncryptedEvent)

} // namespace Quotient