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
This commit is contained in:
Ingo Klöcker 2021-12-22 14:22:00 +01:00
parent f99451e20f
commit 82f43455e9

View File

@ -90,7 +90,23 @@ static QGpgMEImportJob::result_type import_qba(Context *ctx, const QByteArray &c
QGpgME::QByteArrayDataProvider dp(certData); QGpgME::QByteArrayDataProvider dp(certData);
Data data(&dp); 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; Error ae;
const QString log = _detail::audit_log_as_html(ctx, ae); const QString log = _detail::audit_log_as_html(ctx, ae);
return std::make_tuple(res, log, ae); return std::make_tuple(res, log, ae);