aboutsummaryrefslogtreecommitdiff
path: root/lib/events/encryptedfile.h
blob: 43bafc496f41846ba641a11ce756f3a25ddd42cb (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
// SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
//
// SPDX-License-Identifier: LGPl-2.1-or-later

#pragma once

#include "converters.h"

namespace Quotient {
/**
 * JSON Web Key object as specified in
 * https://spec.matrix.org/unstable/client-server-api/#extensions-to-mroommessage-msgtypes
 * The only currently relevant member is `k`, the rest needs to be set to the defaults specified in the spec.
 */
struct JWK
{
    Q_GADGET
    Q_PROPERTY(QString kty MEMBER kty CONSTANT)
    Q_PROPERTY(QStringList keyOps MEMBER keyOps CONSTANT)
    Q_PROPERTY(QString alg MEMBER alg CONSTANT)
    Q_PROPERTY(QString k MEMBER k CONSTANT)
    Q_PROPERTY(bool ext MEMBER ext CONSTANT)

public:
    QString kty;
    QStringList keyOps;
    QString alg;
    QString k;
    bool ext;
};

struct QUOTIENT_API EncryptedFile
{
    Q_GADGET
    Q_PROPERTY(QUrl url MEMBER url CONSTANT)
    Q_PROPERTY(JWK key MEMBER key CONSTANT)
    Q_PROPERTY(QString iv MEMBER iv CONSTANT)
    Q_PROPERTY(QHash<QString, QString> hashes MEMBER hashes CONSTANT)
    Q_PROPERTY(QString v MEMBER v CONSTANT)

public:
    QUrl url;
    JWK key;
    QString iv;
    QHash<QString, QString> hashes;
    QString v;

    QByteArray decryptFile(const QByteArray &ciphertext) const;
};

template <>
struct JsonObjectConverter<EncryptedFile> {
    static void dumpTo(QJsonObject& jo, const EncryptedFile& pod)
    {
        addParam<>(jo, QStringLiteral("url"), pod.url);
        addParam<>(jo, QStringLiteral("key"), pod.key);
        addParam<>(jo, QStringLiteral("iv"), pod.iv);
        addParam<>(jo, QStringLiteral("hashes"), pod.hashes);
        addParam<>(jo, QStringLiteral("v"), pod.v);
    }
    static void fillFrom(const QJsonObject& jo, EncryptedFile& pod)
    {
        fromJson(jo.value("url"_ls), pod.url);
        fromJson(jo.value("key"_ls), pod.key);
        fromJson(jo.value("iv"_ls), pod.iv);
        fromJson(jo.value("hashes"_ls), pod.hashes);
        fromJson(jo.value("v"_ls), pod.v);
    }
};

template <>
struct JsonObjectConverter<JWK> {
    static void dumpTo(QJsonObject& jo, const JWK& pod)
    {
        addParam<>(jo, QStringLiteral("kty"), pod.kty);
        addParam<>(jo, QStringLiteral("key_ops"), pod.keyOps);
        addParam<>(jo, QStringLiteral("alg"), pod.alg);
        addParam<>(jo, QStringLiteral("k"), pod.k);
        addParam<>(jo, QStringLiteral("ext"), pod.ext);
    }
    static void fillFrom(const QJsonObject& jo, JWK& pod)
    {
        fromJson(jo.value("kty"_ls), pod.kty);
        fromJson(jo.value("key_ops"_ls), pod.keyOps);
        fromJson(jo.value("alg"_ls), pod.alg);
        fromJson(jo.value("k"_ls), pod.k);
        fromJson(jo.value("ext"_ls), pod.ext);
    }
};
} // namespace Quotient