aboutsummaryrefslogtreecommitdiff
path: root/lib/e2ee/e2ee.h
blob: 234d4bcb3ec213c4e7652fa391239685f8d383eb (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// SPDX-FileCopyrightText: 2019 Alexey Andreyev <aa13q@ya.ru>
// SPDX-FileCopyrightText: 2019 Kitsune Ral <Kitsune-Ral@users.sf.net>
// SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
// SPDX-License-Identifier: LGPL-2.1-or-later

#pragma once

#include "converters.h"
#include "expected.h"
#include "qolmerrors.h"

#include <QtCore/QMetaType>
#include <variant>
#include <array>

namespace Quotient {

constexpr auto CiphertextKeyL = "ciphertext"_ls;
constexpr auto SenderKeyKeyL = "sender_key"_ls;
constexpr auto DeviceIdKeyL = "device_id"_ls;
constexpr auto SessionIdKeyL = "session_id"_ls;

constexpr auto AlgorithmKeyL = "algorithm"_ls;
constexpr auto RotationPeriodMsKeyL = "rotation_period_ms"_ls;
constexpr auto RotationPeriodMsgsKeyL = "rotation_period_msgs"_ls;

constexpr auto AlgorithmKey = "algorithm"_ls;
constexpr auto RotationPeriodMsKey = "rotation_period_ms"_ls;
constexpr auto RotationPeriodMsgsKey = "rotation_period_msgs"_ls;

constexpr auto Ed25519Key = "ed25519"_ls;
constexpr auto Curve25519Key = "curve25519"_ls;
constexpr auto SignedCurve25519Key = "signed_curve25519"_ls;

constexpr auto OlmV1Curve25519AesSha2AlgoKey = "m.olm.v1.curve25519-aes-sha2"_ls;
constexpr auto MegolmV1AesSha2AlgoKey = "m.megolm.v1.aes-sha2"_ls;

inline bool isSupportedAlgorithm(const QString& algorithm)
{
    static constexpr std::array SupportedAlgorithms {
        OlmV1Curve25519AesSha2AlgoKey, MegolmV1AesSha2AlgoKey
    };
    return std::find(SupportedAlgorithms.cbegin(), SupportedAlgorithms.cend(),
                     algorithm)
           != SupportedAlgorithms.cend();
}

struct Unencrypted {};
struct Encrypted {
    QByteArray key;
};

using PicklingMode = std::variant<Unencrypted, Encrypted>;

class QOlmSession;
using QOlmSessionPtr = std::unique_ptr<QOlmSession>;

class QOlmInboundGroupSession;
using QOlmInboundGroupSessionPtr = std::unique_ptr<QOlmInboundGroupSession>;

class QOlmOutboundGroupSession;
using QOlmOutboundGroupSessionPtr = std::unique_ptr<QOlmOutboundGroupSession>;

template <typename T>
using QOlmExpected = Expected<T, QOlmError>;

struct IdentityKeys
{
    QByteArray curve25519;
    QByteArray ed25519;
};

//! Struct representing the one-time keys.
struct UnsignedOneTimeKeys
{
    QHash<QString, QHash<QString, QString>> keys;

    //! Get the HashMap containing the curve25519 one-time keys.
    QHash<QString, QString> curve25519() const { return keys[Curve25519Key]; }
};

//! Struct representing the signed one-time keys.
class SignedOneTimeKey
{
public:
    //! Required. The unpadded Base64-encoded 32-byte Curve25519 public key.
    QString key;

    //! Required. Signatures of the key object.
    //! The signature is calculated using the process described at Signing JSON.
    QHash<QString, QHash<QString, QString>> signatures;
};

template <>
struct JsonObjectConverter<SignedOneTimeKey> {
    static void fillFrom(const QJsonObject& jo, SignedOneTimeKey& result)
    {
        fromJson(jo.value("key"_ls), result.key);
        fromJson(jo.value("signatures"_ls), result.signatures);
    }

    static void dumpTo(QJsonObject &jo, const SignedOneTimeKey &result)
    {
        addParam<>(jo, QStringLiteral("key"), result.key);
        addParam<>(jo, QStringLiteral("signatures"), result.signatures);
    }
};

using OneTimeKeys = QHash<QString, std::variant<QString, SignedOneTimeKey>>;

template <typename T>
class asKeyValueRange
{
public:
    asKeyValueRange(T& data)
        : m_data { data }
    {}

    auto begin() { return m_data.keyValueBegin(); }
    auto end() { return m_data.keyValueEnd(); }

private:
    T &m_data;
};
template <typename T>
asKeyValueRange(T&) -> asKeyValueRange<T>;

} // namespace Quotient

Q_DECLARE_METATYPE(Quotient::SignedOneTimeKey)