From 82f43455e9412d8b4792b35371e0b6704a619d07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20Kl=C3=B6cker?= Date: Wed, 22 Dec 2021 14:22:00 +0100 Subject: [PATCH] qt: Detect an import error caused by a wrong password * lang/qt/src/qgpgmeimportjob.cpp (import_qba): Check import statuses of import result for bad passphrase errors. -- To allow users of the import job to handle a failed import caused by a wrong password more gracefully, check if all import statuses of the import result have a bad passphrase error and return a bad passphrase error as import result in this case. GnuPG-bug-id: 5713 --- lang/qt/src/qgpgmeimportjob.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lang/qt/src/qgpgmeimportjob.cpp b/lang/qt/src/qgpgmeimportjob.cpp index 110250c9..ebd8a269 100644 --- a/lang/qt/src/qgpgmeimportjob.cpp +++ b/lang/qt/src/qgpgmeimportjob.cpp @@ -90,7 +90,23 @@ static QGpgMEImportJob::result_type import_qba(Context *ctx, const QByteArray &c QGpgME::QByteArrayDataProvider dp(certData); Data data(&dp); - const ImportResult res = ctx->importKeys(data); + ImportResult res = ctx->importKeys(data); + // HACK: If the import failed with an error, then check if res.imports() + // contains only import statuses with "bad passphrase" error; if yes, this + // means that the user probably entered a wrong password to decrypt an + // encrypted key for import. In this case, return a result with "bad + // passphrase" error instead of the original error. + // We check if all import statuses instead of any import status has a + // "bad passphrase" error to avoid breaking imports that partially worked. + // See https://dev.gnupg.org/T5713. + const auto imports = res.imports(); + if (res.error() && !imports.empty() + && std::all_of(std::begin(imports), std::end(imports), + [](const Import &import) { + return import.error().code() == GPG_ERR_BAD_PASSPHRASE; + })) { + res = ImportResult{Error{GPG_ERR_BAD_PASSPHRASE}}; + } Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(res, log, ae);