-
Notifications
You must be signed in to change notification settings - Fork 919
Expand file tree
/
Copy pathfoldermetadata.h
More file actions
252 lines (190 loc) · 8.92 KB
/
foldermetadata.h
File metadata and controls
252 lines (190 loc) · 8.92 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#pragma once
/*
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "accountfwd.h"
#include "encryptedfoldermetadatahandler.h"
#include "csync.h"
#include "rootencryptedfolderinfo.h"
#include <QByteArray>
#include <QHash>
#include <QJsonObject>
#include <QObject>
#include <QSet>
#include <QSslKey>
#include <QString>
#include <QVector>
class QSslCertificate;
class QJsonDocument;
class TestClientSideEncryptionV2;
class TestSecureFileDrop;
namespace OCC
{
// Handles parsing and altering the metadata, encryption and decryption. Setup of the instance is always asynchronouse and emits void setupComplete()
class OWNCLOUDSYNC_EXPORT FolderMetadata : public QObject
{
friend class ::TestClientSideEncryptionV2;
friend class ::TestSecureFileDrop;
Q_OBJECT
struct UserWithFolderAccess {
QString userId;
QByteArray certificatePem;
QByteArray encryptedMetadataKey;
};
struct UserWithFileDropEntryAccess {
QString userId;
QByteArray decryptedFiledropKey;
inline bool isValid() const
{
return !userId.isEmpty() && !decryptedFiledropKey.isEmpty();
}
};
struct FileDropEntry {
QString encryptedFilename;
QByteArray cipherText;
QByteArray nonce;
QByteArray authenticationTag;
UserWithFileDropEntryAccess currentUser;
inline bool isValid() const
{
return !cipherText.isEmpty() && !nonce.isEmpty() && !authenticationTag.isEmpty();
}
};
public:
struct EncryptedFile {
QByteArray encryptionKey;
QByteArray mimetype;
QByteArray initializationVector;
QByteArray authenticationTag;
QString encryptedFilename;
QString originalFilename;
bool isDirectory() const;
};
enum class FolderType {
Nested = 0,
Root = 1,
};
Q_ENUM(FolderType)
enum class CertificateType {
SoftwareNextcloudCertificate,
HardwareCertificate,
};
Q_ENUM(CertificateType)
// based on api-version and "version" key in metadata JSON
enum class MetadataVersion {
VersionUndefined = -1,
Version1,
Version1_2,
Version2_0,
Version2_1,
};
Q_ENUM(MetadataVersion)
FolderMetadata(AccountPtr account, const QString &remoteFolderRoot, FolderType folderType = FolderType::Nested);
/*
* construct metadata based on RootEncryptedFolderInfo
* as per E2EE V2, the encryption key and users that have access are only stored in root(top-level) encrypted folder's metadata
* see: https://github.com/nextcloud/end_to_end_encryption_rfc/blob/v2.1/RFC.md
*/
FolderMetadata(AccountPtr account,
const QString &remoteFolderRoot,
const QByteArray &metadata,
const RootEncryptedFolderInfo &rootEncryptedFolderInfo,
const QByteArray &signature,
QObject *parent = nullptr);
[[nodiscard]] QVector<EncryptedFile> files() const;
[[nodiscard]] bool isValid() const;
[[nodiscard]] bool isFileDropPresent() const;
[[nodiscard]] bool isRootEncryptedFolder() const;
[[nodiscard]] bool encryptedMetadataNeedUpdate() const;
[[nodiscard]] bool moveFromFileDropToFiles();
// adds a user to have access to this folder (always generates new metadata key)
[[nodiscard]] bool addUser(const QString &userId, const QSslCertificate &certificate, CertificateType certificateType);
// removes a user from this folder and removes and generates a new metadata key
[[nodiscard]] bool removeUser(const QString &userId);
[[nodiscard]] bool updateUser(const QString &userId, const QSslCertificate &certificate, CertificateType certificateType);
[[nodiscard]] const QByteArray binaryMetadataKeyForEncryption() const;
[[nodiscard]] const QByteArray binaryMetadataKeyForDecryption() const;
[[nodiscard]] const QSet<QByteArray> &keyChecksums() const;
[[nodiscard]] QByteArray encryptedMetadata();
[[nodiscard]] EncryptionStatusEnums::ItemEncryptionStatus existingMetadataEncryptionStatus() const;
[[nodiscard]] EncryptionStatusEnums::ItemEncryptionStatus encryptedMetadataEncryptionStatus() const;
[[nodiscard]] bool isVersion2AndUp() const;
[[nodiscard]] quint64 newCounter() const;
[[nodiscard]] QByteArray metadataSignature() const;
[[nodiscard]] QByteArray initialMetadata() const;
void updateSelfCertificate();
static MetadataVersion setupVersionFromExistingMetadata(const QByteArray &metadata);
public slots:
void addEncryptedFile(const FolderMetadata::EncryptedFile &f);
void removeEncryptedFile(const FolderMetadata::EncryptedFile &f);
void removeAllEncryptedFiles();
private:
[[nodiscard]] QByteArray encryptedMetadataLegacy();
[[nodiscard]] bool verifyMetadataKey(const QByteArray &metadataKey) const;
[[nodiscard]] QByteArray encryptDataWithPublicKey(const QByteArray &data,
const CertificateInformation &shareUserCertificate) const;
[[nodiscard]] QByteArray decryptDataWithPrivateKey(const QByteArray &data,
const QByteArray &base64CertificateSha256Hash) const;
[[nodiscard]] QByteArray encryptJsonObject(const QByteArray& obj, const QByteArray pass) const;
[[nodiscard]] QByteArray decryptJsonObject(const QByteArray& encryptedJsonBlob, const QByteArray& pass) const;
[[nodiscard]] bool checkMetadataKeyChecksum(const QByteArray &metadataKey, const QByteArray &metadataKeyChecksum) const;
[[nodiscard]] QByteArray computeMetadataKeyChecksum(const QByteArray &metadataKey) const;
[[nodiscard]] EncryptedFile parseEncryptedFileFromJson(const QString &encryptedFilename, const QJsonValue &fileJSON) const;
[[nodiscard]] QJsonObject convertFileToJsonObject(const EncryptedFile *encryptedFile) const;
[[nodiscard]] MetadataVersion latestSupportedMetadataVersion() const;
[[nodiscard]] bool parseFileDropPart(const QJsonDocument &doc);
void setFileDrop(const QJsonObject &fileDrop);
static EncryptionStatusEnums::ItemEncryptionStatus fromMedataVersionToItemEncryptionStatus(const MetadataVersion metadataVersion);
static MetadataVersion fromItemEncryptionStatusToMedataVersion(const EncryptionStatusEnums::ItemEncryptionStatus encryptionStatus);
static QByteArray prepareMetadataForSignature(const QJsonDocument &fullMetadata);
private slots:
void initMetadata();
void initEmptyMetadata();
void initEmptyMetadataLegacy();
void setupExistingMetadata(const QByteArray &metadata);
void setupExistingMetadataLegacy(const QByteArray &metadata);
void startFetchRootE2eeFolderMetadata(const QString &path);
void slotRootE2eeFolderMetadataReceived(int statusCode, const QString &message);
void updateUsersEncryptedMetadataKey();
void createNewMetadataKeyForEncryption();
void emitSetupComplete();
signals:
void setupComplete();
private:
AccountPtr _account;
QString _remoteFolderRoot;
QByteArray _initialMetadata;
bool _isRootEncryptedFolder = false;
// always contains the last generated metadata key (non-encrypted and non-base64)
QByteArray _binaryMetadataKeyForEncryption;
// used for storing initial metadataKey to use for decryption, especially in nested folders when changing the metadataKey and re-encrypting nested dirs
QByteArray _binaryMetadataKeyForDecryption;
QByteArray _metadataNonce;
// metadatakey checksums for validation during setting up from existing metadata
QSet<QByteArray> _keyChecksums;
// filedrop part non-parsed, for upload in case parsing can not be done (due to not having access for the current user, etc.)
QJsonObject _fileDrop;
// used by unit tests, must get assigned simultaneously with _fileDrop and never erased
QJsonObject _fileDropFromServer;
// legacy, remove after migration is done
QMap<int, QByteArray> _metadataKeys;
// users that have access to current folder's "ciphertext", except "filedrop" part
QHash<QString, UserWithFolderAccess> _folderUsers;
// must increment on each metadata upload
quint64 _counter = 0;
MetadataVersion _existingMetadataVersion = MetadataVersion::VersionUndefined;
MetadataVersion _encryptedMetadataVersion = MetadataVersion::VersionUndefined;
// generated each time QByteArray encryptedMetadata() is called, and will later be used for validation if uploaded
QByteArray _metadataSignature;
// signature from server-side metadata
QByteArray _initialSignature;
// both files and folders info
QVector<EncryptedFile> _files;
// parsed filedrop entries ready for move
QVector<FileDropEntry> _fileDropEntries;
// sets to "true" on successful parse
bool _isMetadataValid = false;
QScopedPointer<EncryptedFolderMetadataHandler> _encryptedFolderMetadataHandler;
};
} // namespace OCC