aboutsummaryrefslogtreecommitdiff
path: root/lib/events/encryptionevent.cpp
blob: 9eb48844aa7ba226a545d9bb80f425168d2f5bf9 (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
// SPDX-FileCopyrightText: 2017 Kitsune Ral <kitsune-ral@users.sf.net>
// SPDX-FileCopyrightText: 2019 Alexey Andreyev <aa13q@ya.ru>
// SPDX-License-Identifier: LGPL-2.1-or-later

#include "encryptionevent.h"
#include "logging.h"

#include "e2ee.h"

#include <array>

namespace Quotient {
static const std::array<QString, 1> encryptionStrings = {
    { MegolmV1AesSha2AlgoKey }
};

template <>
struct JsonConverter<EncryptionType> {
    static EncryptionType load(const QJsonValue& jv)
    {
        const auto& encryptionString = jv.toString();
        for (auto it = encryptionStrings.begin(); it != encryptionStrings.end();
             ++it)
            if (encryptionString == *it)
                return EncryptionType(it - encryptionStrings.begin());

        if (!encryptionString.isEmpty())
            qCWarning(EVENTS) << "Unknown EncryptionType: " << encryptionString;
        return EncryptionType::Undefined;
    }
};
} // namespace Quotient

using namespace Quotient;

EncryptionEventContent::EncryptionEventContent(const QJsonObject& json)
    : encryption(fromJson<EncryptionType>(json[AlgorithmKeyL]))
    , algorithm(sanitized(json[AlgorithmKeyL].toString()))
    , rotationPeriodMs(json[RotationPeriodMsKeyL].toInt(604800000))
    , rotationPeriodMsgs(json[RotationPeriodMsgsKeyL].toInt(100))
{}

EncryptionEventContent::EncryptionEventContent(EncryptionType et)
    : encryption(et)
{
    if(encryption != Undefined) {
        algorithm = encryptionStrings[encryption];
    }
}

void EncryptionEventContent::fillJson(QJsonObject* o) const
{
    Q_ASSERT(o);
    if (encryption != EncryptionType::Undefined)
        o->insert(AlgorithmKey, algorithm);
    o->insert(RotationPeriodMsKey, rotationPeriodMs);
    o->insert(RotationPeriodMsgsKey, rotationPeriodMsgs);
}