diff options
author | Ingo Klöcker <[email protected]> | 2022-04-22 15:34:41 +0000 |
---|---|---|
committer | Ingo Klöcker <[email protected]> | 2022-04-22 15:34:41 +0000 |
commit | d8e5871dca9425811873058150677a158235613d (patch) | |
tree | 14d7d033b86f135b1078f048e718753ec42200ea | |
parent | cpp: Allow changing the error of a result (diff) | |
download | gpgme-d8e5871dca9425811873058150677a158235613d.tar.gz gpgme-d8e5871dca9425811873058150677a158235613d.zip |
qt: Report better error if decryption failed because of bad passphrase
* lang/qt/src/qgpgmedecryptverifyjob.cpp (patch_decryption_result): New.
(decrypt_verify): Call patch_decryption_result.
--
Replace the generic "decryption failed" error with the more specific
"bad passphrase" error if the audit log indicates that a wrong symmetric
password caused the decryption to fail.
GnuPG-bug-id: 5939
-rw-r--r-- | lang/qt/src/qgpgmedecryptverifyjob.cpp | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/lang/qt/src/qgpgmedecryptverifyjob.cpp b/lang/qt/src/qgpgmedecryptverifyjob.cpp index 5f9a8abe..bc24d93e 100644 --- a/lang/qt/src/qgpgmedecryptverifyjob.cpp +++ b/lang/qt/src/qgpgmedecryptverifyjob.cpp @@ -49,6 +49,7 @@ #include "qgpgme_debug.h" #include <QBuffer> +#include <QRegularExpression> #include <cassert> @@ -63,12 +64,23 @@ QGpgMEDecryptVerifyJob::QGpgMEDecryptVerifyJob(Context *context) QGpgMEDecryptVerifyJob::~QGpgMEDecryptVerifyJob() {} +static void patch_decryption_result(DecryptionResult &result, const QString &auditLog) +{ + if (result.error().code() == GPG_ERR_DECRYPT_FAILED) { + // check for wrong symmetric password + if (auditLog.contains(QLatin1String{"gpg: decryption of the symmetrically encrypted session key failed: Checksum error"}) + || (auditLog.contains(QRegularExpression{QStringLiteral("gpg: encrypted with \\d+ passphrase")}) + && auditLog.contains(QLatin1String{"gpg: decryption failed: Bad session key"}))) { + result.setError(Error::fromCode(GPG_ERR_BAD_PASSPHRASE)); + } + } +} + static QGpgMEDecryptVerifyJob::result_type decrypt_verify(Context *ctx, QThread *thread, const std::weak_ptr<QIODevice> &cipherText_, const std::weak_ptr<QIODevice> &plainText_) { - - qCDebug(QGPGME_LOG); + qCDebug(QGPGME_LOG) << __func__; const std::shared_ptr<QIODevice> cipherText = cipherText_.lock(); const std::shared_ptr<QIODevice> plainText = plainText_.lock(); @@ -83,22 +95,23 @@ static QGpgMEDecryptVerifyJob::result_type decrypt_verify(Context *ctx, QThread QGpgME::QByteArrayDataProvider out; Data outdata(&out); - const std::pair<DecryptionResult, VerificationResult> res = ctx->decryptAndVerify(indata, outdata); + std::pair<DecryptionResult, VerificationResult> res = ctx->decryptAndVerify(indata, outdata); Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); - qCDebug(QGPGME_LOG) << "End no plainText. Error: " << ae; + qCDebug(QGPGME_LOG) << __func__ << "- End no plainText. Error:" << ae.asString(); + patch_decryption_result(res.first, log); return std::make_tuple(res.first, res.second, out.data(), log, ae); } else { QGpgME::QIODeviceDataProvider out(plainText); Data outdata(&out); - const std::pair<DecryptionResult, VerificationResult> res = ctx->decryptAndVerify(indata, outdata); + std::pair<DecryptionResult, VerificationResult> res = ctx->decryptAndVerify(indata, outdata); Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); - qCDebug(QGPGME_LOG) << "End plainText. Error: " << ae; + qCDebug(QGPGME_LOG) << __func__ << "- End plainText. Error:" << ae.asString(); + patch_decryption_result(res.first, log); return std::make_tuple(res.first, res.second, QByteArray(), log, ae); } - } static QGpgMEDecryptVerifyJob::result_type decrypt_verify_qba(Context *ctx, const QByteArray &cipherText) |