From 595eb78c8a2a6c43f4112f976de062ede64d52ab Mon Sep 17 00:00:00 2001 From: Kevin Ottens Date: Mon, 10 Aug 2020 11:26:52 +0200 Subject: [PATCH] Move the private key salt handling in its own function Signed-off-by: Kevin Ottens --- src/libsync/clientsideencryption.cpp | 17 ++++++++++++++--- src/libsync/clientsideencryption.h | 1 + test/testclientsideencryption.cpp | 2 ++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/libsync/clientsideencryption.cpp b/src/libsync/clientsideencryption.cpp index bf0a30506..76017ec9c 100644 --- a/src/libsync/clientsideencryption.cpp +++ b/src/libsync/clientsideencryption.cpp @@ -448,6 +448,19 @@ QByteArray decryptPrivateKey(const QByteArray& key, const QByteArray& data) { return QByteArray::fromBase64(result); } +QByteArray extractPrivateKeySalt(const QByteArray &data) +{ + const auto parts = data.split('|'); + qCInfo(lcCse()) << "found parts:" << parts; + + if (parts.size() < 3) { + qCInfo(lcCse()) << "Not enough parts found"; + return QByteArray(); + } + + return QByteArray::fromBase64(parts.at(2)); +} + QByteArray decryptStringSymmetric(const QByteArray& key, const QByteArray& data) { qCInfo(lcCse()) << "decryptStringSymmetric key: " << key; qCInfo(lcCse()) << "decryptStringSymmetric data: " << data; @@ -1135,9 +1148,7 @@ void ClientSideEncryption::decryptPrivateKey(const QByteArray &key) { qCInfo(lcCse()) << "mnemonic:" << mnemonic; // split off salt - // Todo better place? - auto pos = key.lastIndexOf('|'); - QByteArray salt = QByteArray::fromBase64(key.mid(pos + 1)); + const auto salt = EncryptionHelper::extractPrivateKeySalt(key); auto pass = EncryptionHelper::generatePassword(mnemonic, salt); qCInfo(lcCse()) << "Generated key:" << pass; diff --git a/src/libsync/clientsideencryption.h b/src/libsync/clientsideencryption.h index cbfc2a6c0..13ec98043 100644 --- a/src/libsync/clientsideencryption.h +++ b/src/libsync/clientsideencryption.h @@ -38,6 +38,7 @@ namespace EncryptionHelper { const QByteArray& key, const QByteArray& data ); + OWNCLOUDSYNC_EXPORT QByteArray extractPrivateKeySalt(const QByteArray &data); OWNCLOUDSYNC_EXPORT QByteArray encryptStringSymmetric( const QByteArray& key, const QByteArray& data diff --git a/test/testclientsideencryption.cpp b/test/testclientsideencryption.cpp index b7916c42e..91ecb0da8 100644 --- a/test/testclientsideencryption.cpp +++ b/test/testclientsideencryption.cpp @@ -52,9 +52,11 @@ private slots: // WHEN const auto privateKey = EncryptionHelper::decryptPrivateKey(encryptionKey, cipher); + const auto salt = EncryptionHelper::extractPrivateKeySalt(cipher); // THEN QCOMPARE(privateKey, originalPrivateKey); + QCOMPARE(salt, originalSalt); } void shouldSymmetricEncryptStrings()