Commit 33ccbc04 authored by Albert Astals Cid's avatar Albert Astals Cid Committed by Albert Astals Cid
Browse files

Signing: Don't say that there are no certificates if the user cancels...

...entering the pwd for the certificate store

Also Move CertificateStoreImpl to PopplerCertificateStore, since
PopplerCertificateStore was declared but never defined and use it
in CertificateTools to get the certificate list instead of calling
the poppler classes directly
parent b07b3209
......@@ -173,7 +173,8 @@ CertificateStore::~CertificateStore()
{
}
QList<CertificateInfo *> CertificateStore::signingCertificates() const
QList<CertificateInfo *> CertificateStore::signingCertificates(bool *userCancelled) const
{
*userCancelled = false;
return QList<CertificateInfo *>();
}
......@@ -263,9 +263,11 @@ public:
virtual ~CertificateStore();
/**
* Returns list of valid, usable signing certificates
* Returns list of valid, usable signing certificates.
*
* This can ask the user for a password, userCancelled will be true if the user decided not to enter it.
*/
virtual QList<CertificateInfo *> signingCertificates() const;
virtual QList<CertificateInfo *> signingCertificates(bool *userCancelled) const;
protected:
CertificateStore();
......
......@@ -8,13 +8,13 @@
***************************************************************************/
#include "certificatetools.h"
#include "certsettings.h"
#include "pdfsignatureutils.h"
#include <KLocalizedString>
#include <KUrlRequester>
#include <poppler-form.h>
#include <QEvent>
#include <QHBoxLayout>
#include <QMessageBox>
......@@ -25,6 +25,7 @@ CertificateTools::CertificateTools(QWidget *parent)
: QWidget(parent)
{
m_certsw.setupUi(this);
m_certsw.loadSignaturesButton->hide();
KUrlRequester *pDlg = new KUrlRequester();
pDlg->setObjectName(QStringLiteral("kcfg_DBCertificatePath"));
......@@ -51,6 +52,10 @@ CertificateTools::CertificateTools(QWidget *parent)
warnRestartNeeded();
}
});
connect(m_certsw.loadSignaturesButton, &QPushButton::clicked, this, [this] {
m_certificatesAsked = false;
update();
});
}
bool CertificateTools::event(QEvent *e)
......@@ -58,10 +63,16 @@ bool CertificateTools::event(QEvent *e)
if (e->type() == QEvent::Paint && !m_certificatesAsked) {
m_certificatesAsked = true;
const QVector<Poppler::CertificateInfo> nssCerts = Poppler::getAvailableSigningCertificates();
foreach (auto cert, nssCerts) {
new QTreeWidgetItem(m_tree, {cert.subjectInfo(Poppler::CertificateInfo::EntityInfoKey::CommonName), cert.subjectInfo(Poppler::CertificateInfo::EntityInfoKey::EmailAddress), cert.validityEnd().toString("yyyy-MM-dd")});
PopplerCertificateStore st;
bool userCancelled;
const QList<Okular::CertificateInfo *> certs = st.signingCertificates(&userCancelled);
m_certsw.loadSignaturesButton->setVisible(userCancelled);
for (auto cert : certs) {
new QTreeWidgetItem(m_tree, {cert->subjectInfo(Okular::CertificateInfo::EntityInfoKey::CommonName), cert->subjectInfo(Okular::CertificateInfo::EntityInfoKey::EmailAddress), cert->validityEnd().toString("yyyy-MM-dd")});
}
qDeleteAll(certs);
m_certsw.defaultLabel->setText(Poppler::getNSSDir());
......
......@@ -64,6 +64,13 @@
<layout class="QVBoxLayout" name="certificatesPlaceholder"/>
</widget>
</item>
<item>
<widget class="QPushButton" name="loadSignaturesButton">
<property name="text">
<string>&amp;Load Signatures</string>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation">
......
......@@ -25,7 +25,6 @@
#include <QDir>
#include <QFile>
#include <QImage>
#include <QInputDialog>
#include <QLayout>
#include <QMutex>
#include <QPainter>
......@@ -545,15 +544,6 @@ static void PDFGeneratorPopplerDebugFunction(const QString &message, const QVari
qCDebug(OkularPdfDebug) << "[Poppler]" << message;
}
#ifdef HAVE_POPPLER_SIGNING
static char *PDFGeneratorNSSPasswordCallback(const char *element)
{
bool ok;
const QString pwd = QInputDialog::getText(nullptr, i18n("Enter Password"), i18n("Enter password to open %1:", element), QLineEdit::Password, QString(), &ok);
return ok ? strdup(pwd.toUtf8().constData()) : nullptr;
}
#endif
PDFGenerator::PDFGenerator(QObject *parent, const QVariantList &args)
: Generator(parent, args)
, pdfdoc(nullptr)
......@@ -582,7 +572,6 @@ PDFGenerator::PDFGenerator(QObject *parent, const QVariantList &args)
// so doing it all the time won't hurt either
Poppler::setDebugErrorFunction(PDFGeneratorPopplerDebugFunction, QVariant());
#ifdef HAVE_POPPLER_SIGNING
Poppler::setNSSPasswordCallback(PDFGeneratorNSSPasswordCallback);
if (!CertificateSettings::useDefaultDB()) {
Poppler::setNSSDir(QUrl(CertificateSettings::dBCertificatePath()).toLocalFile());
}
......@@ -1928,28 +1917,11 @@ bool PDFGenerator::sign(const Okular::NewSignatureData &oData, const QString &rF
return true;
}
#ifdef HAVE_POPPLER_SIGNING
namespace
{
struct CertificateStoreImpl : public Okular::CertificateStore {
QList<Okular::CertificateInfo *> signingCertificates() const override
{
const QVector<Poppler::CertificateInfo> certs = Poppler::getAvailableSigningCertificates();
QList<Okular::CertificateInfo *> vReturnCerts;
for (auto cert : certs)
vReturnCerts.append(new PopplerCertificateInfo(cert));
return vReturnCerts;
}
};
}
#endif
Okular::CertificateStore *PDFGenerator::certificateStore() const
{
#ifdef HAVE_POPPLER_SIGNING
if (!certStore)
certStore = new CertificateStoreImpl();
certStore = new PopplerCertificateStore();
return certStore;
#else
......
......@@ -11,6 +11,7 @@
#include <KLocalizedString>
#include <QDebug>
#include <QInputDialog>
PopplerCertificateInfo::PopplerCertificateInfo(const Poppler::CertificateInfo &info)
: m_info(info)
......@@ -257,3 +258,28 @@ const Okular::CertificateInfo &PopplerSignatureInfo::certificateInfo() const
{
return *m_certfiticateInfo;
}
#ifdef HAVE_POPPLER_SIGNING
PopplerCertificateStore::~PopplerCertificateStore() = default;
QList<Okular::CertificateInfo *> PopplerCertificateStore::signingCertificates(bool *userCancelled) const
{
*userCancelled = false;
auto PDFGeneratorNSSPasswordCallback = [&userCancelled](const char *element) -> char * {
bool ok;
const QString pwd = QInputDialog::getText(nullptr, i18n("Enter Password"), i18n("Enter password to open %1:", element), QLineEdit::Password, QString(), &ok);
*userCancelled = !ok;
return ok ? strdup(pwd.toUtf8().constData()) : nullptr;
};
Poppler::setNSSPasswordCallback(PDFGeneratorNSSPasswordCallback);
const QVector<Poppler::CertificateInfo> certs = Poppler::getAvailableSigningCertificates();
QList<Okular::CertificateInfo *> vReturnCerts;
for (auto cert : certs)
vReturnCerts.append(new PopplerCertificateInfo(cert));
Poppler::setNSSPasswordCallback(nullptr);
return vReturnCerts;
}
#endif
......@@ -71,10 +71,9 @@ private:
class PopplerCertificateStore : public Okular::CertificateStore
{
public:
PopplerCertificateStore();
~PopplerCertificateStore();
QList<Okular::CertificateInfo *> signingCertificates() const override;
QList<Okular::CertificateInfo *> signingCertificates(bool *userCancelled) const override;
};
#endif
......
......@@ -355,7 +355,12 @@ public:
}
const Okular::CertificateStore *certStore = m_document->certificateStore();
const QList<Okular::CertificateInfo *> &certs = certStore->signingCertificates();
bool userCancelled;
const QList<Okular::CertificateInfo *> &certs = certStore->signingCertificates(&userCancelled);
if (userCancelled) {
return {};
}
QStringList items;
QHash<QString, Okular::CertificateInfo *> nickToCert;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment