Signature plugin: sign dialog + verification

This commit is contained in:
Jakub Melka 2022-05-14 17:38:28 +02:00
parent e492b53a7b
commit 4d8d916c3f
8 changed files with 375 additions and 4 deletions

View File

@ -43,13 +43,15 @@ SOURCES += \
certificatemanager.cpp \
certificatemanagerdialog.cpp \
createcertificatedialog.cpp \
signatureplugin.cpp
signatureplugin.cpp \
signdialog.cpp
HEADERS += \
certificatemanager.h \
certificatemanagerdialog.h \
createcertificatedialog.h \
signatureplugin.h
signatureplugin.h \
signdialog.h
CONFIG += force_debug_info
@ -61,5 +63,6 @@ RESOURCES += \
FORMS += \
certificatemanagerdialog.ui \
createcertificatedialog.ui
createcertificatedialog.ui \
signdialog.ui

View File

@ -132,6 +132,12 @@ void CertificateManager::createCertificate(const NewCertificateInfo& info)
}
}
QFileInfoList CertificateManager::getCertificates()
{
QDir directory(getCertificateDirectory());
return directory.entryInfoList(QStringList() << "*.pfx", QDir::Files | QDir::NoDotAndDotDot | QDir::Readable, QDir::Name);
}
QString CertificateManager::getCertificateDirectory()
{
QDir directory(QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).front() + "/certificates/");
@ -156,4 +162,32 @@ QString CertificateManager::generateCertificateFileName()
return QString();
}
bool CertificateManager::isCertificateValid(QString fileName, QString password)
{
QFile file(fileName);
if (file.open(QFile::ReadOnly))
{
QByteArray data = file.readAll();
file.close();
openssl_ptr<BIO> pksBuffer(BIO_new(BIO_s_mem()), &BIO_free_all);
BIO_write(pksBuffer.get(), data.constData(), data.length());
openssl_ptr<PKCS12> pkcs12(d2i_PKCS12_bio(pksBuffer.get(), nullptr), &PKCS12_free);
if (pkcs12)
{
const char* passwordPointer = nullptr;
QByteArray passwordByteArray = password.isEmpty() ? QByteArray() : password.toUtf8();
if (!passwordByteArray.isEmpty())
{
passwordPointer = passwordByteArray.constData();
}
return PKCS12_parse(pkcs12.get(), passwordPointer, nullptr, nullptr, nullptr) == 1;
}
}
return false;
}
} // namespace pdfplugin

View File

@ -19,6 +19,7 @@
#define CERTIFICATEMANAGER_H
#include <QString>
#include <QFileInfoList>
namespace pdfplugin
{
@ -46,8 +47,10 @@ public:
void createCertificate(const NewCertificateInfo& info);
static QFileInfoList getCertificates();
static QString getCertificateDirectory();
static QString generateCertificateFileName();
static bool isCertificateValid(QString fileName, QString password);
};
} // namespace pdfplugin

View File

@ -22,6 +22,7 @@
#include "pdfpagecontenteditorstylesettings.h"
#include "pdfdocumentbuilder.h"
#include "certificatemanagerdialog.h"
#include "signdialog.h"
#include <QAction>
#include <QToolButton>
@ -156,6 +157,7 @@ void SignaturePlugin::setWidget(pdf::PDFWidget* widget)
connect(clearAction, &QAction::triggered, &m_scene, &pdf::PDFPageContentScene::clear);
connect(activateAction, &QAction::triggered, this, &SignaturePlugin::setActive);
connect(signElectronicallyAction, &QAction::triggered, this, &SignaturePlugin::onSignElectronically);
connect(signDigitallyAction, &QAction::triggered, this, &SignaturePlugin::onSignDigitally);
connect(certificatesAction, &QAction::triggered, this, &SignaturePlugin::onOpenCertificatesManager);
updateActions();
@ -305,6 +307,28 @@ void SignaturePlugin::onSignElectronically()
}
}
void SignaturePlugin::onSignDigitally()
{
// Jakub Melka: do we have certificates? If not,
// open certificate dialog, so the user can create
// a new one.
if (CertificateManager::getCertificates().isEmpty())
{
onOpenCertificatesManager();
if (CertificateManager::getCertificates().isEmpty())
{
return;
}
}
SignDialog dialog(m_dataExchangeInterface->getMainWindow(), m_scene.isEmpty());
if (dialog.exec() == SignDialog::Accepted)
{
}
}
void SignaturePlugin::onOpenCertificatesManager()
{
CertificateManagerDialog dialog(m_dataExchangeInterface->getMainWindow());
@ -422,7 +446,7 @@ void SignaturePlugin::updateActions()
QAction* signElectronicallyAction = m_actions[SignElectronically];
signElectronicallyAction->setEnabled(isSceneNonempty);
QAction* signDigitallyAction = m_actions[SignDigitally];
signDigitallyAction->setEnabled(isSceneNonempty);
signDigitallyAction->setEnabled(m_document);
}
void SignaturePlugin::updateGraphics()

View File

@ -54,6 +54,7 @@ private:
void onToolActivityChanged();
void onSceneEditElement(const std::set<pdf::PDFInteger>& elements);
void onSignElectronically();
void onSignDigitally();
void onOpenCertificatesManager();
void onPenChanged(const QPen& pen);

View File

@ -0,0 +1,90 @@
// Copyright (C) 2022 Jakub Melka
//
// This file is part of PDF4QT.
//
// PDF4QT is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// with the written consent of the copyright owner, any later version.
//
// PDF4QT is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with PDF4QT. If not, see <https://www.gnu.org/licenses/>.
#include "signdialog.h"
#include "ui_signdialog.h"
#include "certificatemanager.h"
#include <openssl/pkcs7.h>
#include <QMessageBox>
namespace pdfplugin
{
SignDialog::SignDialog(QWidget* parent, bool isSceneEmpty) :
QDialog(parent),
ui(new Ui::SignDialog)
{
ui->setupUi(this);
if (!isSceneEmpty)
{
ui->methodCombo->addItem(tr("Sign digitally"), SignDigitally);
}
ui->methodCombo->addItem(tr("Sign digitally (invisible signature)"), SignDigitallyInvisible);
ui->methodCombo->setCurrentIndex(0);
QFileInfoList certificates = CertificateManager::getCertificates();
for (const QFileInfo& certificateFileInfo : certificates)
{
ui->certificateCombo->addItem(certificateFileInfo.fileName(), certificateFileInfo.absoluteFilePath());
}
}
SignDialog::~SignDialog()
{
delete ui;
}
SignDialog::SignMethod SignDialog::getSignMethod() const
{
return static_cast<SignMethod>(ui->methodCombo->currentData().toInt());
}
QString SignDialog::getCertificatePath() const
{
return ui->certificateCombo->currentData().toString();
}
void SignDialog::accept()
{
// Check certificate
if (!QFile::exists(getCertificatePath()))
{
QMessageBox::critical(this, tr("Error"), tr("Certificate does not exist."));
ui->certificateCombo->setFocus();
return;
}
// Check we can access the certificate
if (!CertificateManager::isCertificateValid(getCertificatePath(), ui->certificatePasswordEdit->text()))
{
QMessageBox::critical(this, tr("Error"), tr("Password to open certificate is invalid."));
ui->certificatePasswordEdit->setFocus();
return;
}
QDialog::accept();
}
} // namespace pdfplugin

View File

@ -0,0 +1,56 @@
// Copyright (C) 2022 Jakub Melka
//
// This file is part of PDF4QT.
//
// PDF4QT is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// with the written consent of the copyright owner, any later version.
//
// PDF4QT is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with PDF4QT. If not, see <https://www.gnu.org/licenses/>.
#ifndef SIGNDIALOG_H
#define SIGNDIALOG_H
#include <QDialog>
namespace Ui
{
class SignDialog;
}
namespace pdfplugin
{
class SignDialog : public QDialog
{
Q_OBJECT
public:
explicit SignDialog(QWidget* parent, bool isSceneEmpty);
virtual ~SignDialog() override;
virtual void accept() override;
enum SignMethod
{
SignDigitally,
SignDigitallyInvisible
};
SignMethod getSignMethod() const;
QString getCertificatePath() const;
private:
Ui::SignDialog* ui;
};
} // namespace pdfplugin
#endif // SIGNDIALOG_H

View File

@ -0,0 +1,160 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SignDialog</class>
<widget class="QDialog" name="SignDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>501</width>
<height>332</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="selectCertificateGroupBox">
<property name="title">
<string>Sign Method</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="methodLabel">
<property name="text">
<string>Method</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="methodCombo"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="certificateLabel">
<property name="text">
<string>Certificate</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="certificateCombo"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="passwordLabel">
<property name="text">
<string>Password</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="certificatePasswordEdit">
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="parametersGroupBox">
<property name="title">
<string>Parameters</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="reasonLabel">
<property name="text">
<string>Reason</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="reasonEdit">
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="contactInfoLabel">
<property name="text">
<string>Contact Info</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="contactInfoEdit">
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>SignDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>SignDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>