Skip to content

Commit

Permalink
Use lock counter from parent folder.
Browse files Browse the repository at this point in the history
Signed-off-by: alex-z <blackslayer4@gmail.com>
  • Loading branch information
allexzander committed Jul 4, 2023
1 parent 2ee991e commit 33bfdcc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 16 additions & 4 deletions src/libsync/foldermetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ FolderMetadata::RootEncryptedFolderInfo::RootEncryptedFolderInfo()
FolderMetadata::RootEncryptedFolderInfo::RootEncryptedFolderInfo(const QString &remotePath,
const QByteArray &encryptionKey,
const QByteArray &decryptionKey,
const QSet<QByteArray> &checksums):
const QSet<QByteArray> &checksums,
const quint64 counter):
path(remotePath),
keyForEncryption(encryptionKey),
keyForDecryption(decryptionKey),
keyChecksums(checksums)
keyChecksums(checksums),
counter(counter)
{
}

Expand Down Expand Up @@ -112,6 +114,7 @@ FolderMetadata::FolderMetadata(AccountPtr account,
, _metadataKeyForEncryption(rootEncryptedFolderInfo.keyForEncryption)
, _metadataKeyForDecryption(rootEncryptedFolderInfo.keyForDecryption)
, _keyChecksums(rootEncryptedFolderInfo.keyChecksums)
, _counter(rootEncryptedFolderInfo.counter)
{
setupVersionFromExistingMetadata(metadata);

Expand Down Expand Up @@ -246,7 +249,11 @@ void FolderMetadata::setupExistingMetadata(const QByteArray &metadata)

const auto files = cipherTextDocument.object()[filesKey].toObject();
const auto folders = cipherTextDocument.object()[foldersKey].toObject();
_counter = cipherTextDocument.object()[counterKey].toVariant().value<quint64>();

const auto counterVariantFromJson = cipherTextDocument.object()[counterKey].toVariant();
if (counterVariantFromJson.isValid()) {
_counter = cipherTextDocument.object()[counterKey].toVariant().value<quint64>();
}

for (auto it = files.constBegin(), end = files.constEnd(); it != end; ++it) {
const auto parsedEncryptedFile = parseEncryptedFileFromJson(it.key(), it.value());
Expand Down Expand Up @@ -612,7 +619,7 @@ QByteArray FolderMetadata::encryptedMetadata()
}
}

QJsonObject cipherText = {{counterKey, QJsonValue::fromVariant(newCounter())}, {filesKey, files}, {foldersKey, folders}};
QJsonObject cipherText = {{filesKey, files}, {foldersKey, folders}};

const auto isChecksumsArrayValid = (!_isRootEncryptedFolder && keyChecksums.isEmpty()) || (_isRootEncryptedFolder && !keyChecksums.isEmpty());
Q_ASSERT(isChecksumsArrayValid);
Expand All @@ -624,6 +631,10 @@ QByteArray FolderMetadata::encryptedMetadata()
cipherText.insert(keyChecksumsKey, keyChecksums);
}

if (_isRootEncryptedFolder) {
cipherText.insert(counterKey, QJsonValue::fromVariant(newCounter()));
}

const QJsonDocument cipherTextDoc(cipherText);

QByteArray authenticationTag;
Expand Down Expand Up @@ -944,6 +955,7 @@ void FolderMetadata::rootE2eeFolderMetadataReceived(const QJsonDocument &json, i
_metadataKeyForDecryption = rootE2eeFolderMetadata->metadataKeyForDecryption();
_metadataKeyForEncryption = rootE2eeFolderMetadata->metadataKeyForEncryption();
_keyChecksums = rootE2eeFolderMetadata->keyChecksums();
_counter = rootE2eeFolderMetadata->counter();
initMetadata();
});
}
Expand Down
4 changes: 3 additions & 1 deletion src/libsync/foldermetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class OWNCLOUDSYNC_EXPORT FolderMetadata : public QObject
explicit RootEncryptedFolderInfo(const QString &remotePath,
const QByteArray &encryptionKey = {},
const QByteArray &decryptionKey = {},
const QSet<QByteArray> &checksums = {});
const QSet<QByteArray> &checksums = {},
const quint64 counter = 0);

static RootEncryptedFolderInfo makeDefault();

Expand All @@ -73,6 +74,7 @@ class OWNCLOUDSYNC_EXPORT FolderMetadata : public QObject
QByteArray keyForEncryption; // it can be different from keyForDecryption when new metadatKey is generated in root E2EE foler
QByteArray keyForDecryption; // always storing previous metadataKey to be able to decrypt nested E2EE folders' previous metadata
QSet<QByteArray> keyChecksums;
quint64 counter = 0;
[[nodiscard]] bool keysSet() const;
};

Expand Down

0 comments on commit 33bfdcc

Please sign in to comment.