Skip to content

Commit

Permalink
remove duplicated code in ClientSideEncryption class
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu Gallien <[email protected]>
  • Loading branch information
mgallien committed Feb 6, 2025
1 parent e1bb8fc commit e941816
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 34 deletions.
34 changes: 1 addition & 33 deletions src/libsync/clientsideencryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1316,38 +1316,6 @@ void ClientSideEncryption::fetchPublicKeyFromKeyChain(const AccountPtr &account)
job->start();
}

bool ClientSideEncryption::checkPublicKeyValidity(const AccountPtr &account) const
{
QByteArray data = EncryptionHelper::generateRandom(64);

Bio publicKeyBio;
QByteArray publicKeyPem = account->e2e()->getPublicKey().toPem();
BIO_write(publicKeyBio, publicKeyPem.constData(), publicKeyPem.size());
auto publicKey = PKey::readPublicKey(publicKeyBio);

auto encryptedData = EncryptionHelper::encryptStringAsymmetric(account->e2e()->getCertificateInformation(), account->e2e()->paddingMode(), *account->e2e(), data);
if (!encryptedData) {
qCWarning(lcCse()) << "encryption error";
return false;
}

auto key = _encryptionCertificate.getEvpPrivateKey();

const auto decryptionResult = EncryptionHelper::decryptStringAsymmetric(account->e2e()->getCertificateInformation(), account->e2e()->paddingMode(), *account->e2e(), *encryptedData);
if (!decryptionResult) {
qCWarning(lcCse()) << "encryption error";
return false;
}
const auto decryptResult = QByteArray::fromBase64(*decryptionResult);

if (data != decryptResult) {
qCInfo(lcCse()) << "invalid private key";
return false;
}

return true;
}

bool ClientSideEncryption::checkEncryptionIsWorking() const
{
qCInfo(lcCse) << "check encryption is working before enabling end-to-end encryption feature";
Expand Down Expand Up @@ -2296,7 +2264,7 @@ void ClientSideEncryption::decryptPrivateKey(const AccountPtr &account, const QB
}
}

if (!getPrivateKey().isNull() && checkPublicKeyValidity(account)) {
if (!getPrivateKey().isNull() && checkEncryptionIsWorking()) {
writePrivateKey(account);
writeCertificate(account);
writeMnemonic(account, [] () {});
Expand Down
1 change: 0 additions & 1 deletion src/libsync/clientsideencryption.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ private slots:
SUCCESS_CALLBACK nextCheck,
ERROR_CALLBACK onError);

[[nodiscard]] bool checkPublicKeyValidity(const AccountPtr &account) const;
[[nodiscard]] bool checkServerPublicKeyValidity(const QByteArray &serverPublicKeyString) const;
[[nodiscard]] bool sensitiveDataRemaining() const;

Expand Down

0 comments on commit e941816

Please sign in to comment.