mirror of https://github.com/JakubMelka/PDF4QT.git
Signature plugin: Certificate management
This commit is contained in:
parent
148cec5aec
commit
27bed729b5
|
@ -65,7 +65,7 @@ void CertificateManager::createCertificate(const NewCertificateInfo& info)
|
||||||
|
|
||||||
// Set validity of the certificate
|
// Set validity of the certificate
|
||||||
X509_gmtime_adj(X509_getm_notBefore(certificate.get()), 0);
|
X509_gmtime_adj(X509_getm_notBefore(certificate.get()), 0);
|
||||||
X509_gmtime_adj(X509_getm_notBefore(certificate.get()), info.validityInSeconds);
|
X509_gmtime_adj(X509_getm_notAfter(certificate.get()), info.validityInSeconds);
|
||||||
|
|
||||||
// Set name
|
// Set name
|
||||||
X509_NAME* name = X509_get_subject_name(certificate.get());
|
X509_NAME* name = X509_get_subject_name(certificate.get());
|
||||||
|
@ -104,18 +104,17 @@ void CertificateManager::createCertificate(const NewCertificateInfo& info)
|
||||||
QByteArray privateKeyPaswordUtf8 = info.privateKeyPasword.toUtf8();
|
QByteArray privateKeyPaswordUtf8 = info.privateKeyPasword.toUtf8();
|
||||||
|
|
||||||
// Write the data
|
// Write the data
|
||||||
PKCS12* pkcs12 = PKCS12_create(privateKeyPaswordUtf8.constData(),
|
openssl_ptr<PKCS12> pkcs12(PKCS12_create(privateKeyPaswordUtf8.constData(),
|
||||||
nullptr,
|
nullptr,
|
||||||
privateKey.get(),
|
privateKey.get(),
|
||||||
certificate.get(),
|
certificate.get(),
|
||||||
nullptr,
|
nullptr,
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
PKCS12_DEFAULT_ITER,
|
PKCS12_DEFAULT_ITER,
|
||||||
PKCS12_DEFAULT_ITER,
|
PKCS12_DEFAULT_ITER,
|
||||||
0);
|
0), &PKCS12_free);
|
||||||
i2d_PKCS12_bio(pksBuffer.get(), pkcs12);
|
i2d_PKCS12_bio(pksBuffer.get(), pkcs12.get());
|
||||||
PKCS12_free(pkcs12);
|
|
||||||
|
|
||||||
BUF_MEM* pksMemoryBuffer = nullptr;
|
BUF_MEM* pksMemoryBuffer = nullptr;
|
||||||
BIO_get_mem_ptr(pksBuffer.get(), &pksMemoryBuffer);
|
BIO_get_mem_ptr(pksBuffer.get(), &pksMemoryBuffer);
|
||||||
|
@ -125,7 +124,7 @@ void CertificateManager::createCertificate(const NewCertificateInfo& info)
|
||||||
QFile file(info.fileName);
|
QFile file(info.fileName);
|
||||||
if (file.open(QFile::WriteOnly | QFile::Truncate))
|
if (file.open(QFile::WriteOnly | QFile::Truncate))
|
||||||
{
|
{
|
||||||
int datac = file.write(pksMemoryBuffer->data, pksMemoryBuffer->length);
|
file.write(pksMemoryBuffer->data, pksMemoryBuffer->length);
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,10 @@
|
||||||
|
|
||||||
#include <QAction>
|
#include <QAction>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
|
#include <QFileSystemModel>
|
||||||
|
#include <QDesktopServices>
|
||||||
|
#include <QMessageBox>
|
||||||
|
#include <QFileDialog>
|
||||||
|
|
||||||
namespace pdfplugin
|
namespace pdfplugin
|
||||||
{
|
{
|
||||||
|
@ -31,14 +35,29 @@ CertificateManagerDialog::CertificateManagerDialog(QWidget *parent) :
|
||||||
QDialog(parent),
|
QDialog(parent),
|
||||||
ui(new Ui::CertificateManagerDialog),
|
ui(new Ui::CertificateManagerDialog),
|
||||||
m_newCertificateButton(nullptr),
|
m_newCertificateButton(nullptr),
|
||||||
m_openCertificateDirectoryButton(nullptr)
|
m_openCertificateDirectoryButton(nullptr),
|
||||||
|
m_deleteCertificateButton(nullptr),
|
||||||
|
m_importCertificateButton(nullptr),
|
||||||
|
m_certificateFileModel(nullptr)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
m_newCertificateButton = ui->buttonBox->addButton(tr("Create Certificate"), QDialogButtonBox::ActionRole);
|
QDir::root().mkpath(CertificateManager::getCertificateDirectory());
|
||||||
m_openCertificateDirectoryButton = ui->buttonBox->addButton(tr("Show Certificate Directory"), QDialogButtonBox::ActionRole);
|
|
||||||
|
m_certificateFileModel = new QFileSystemModel(this);
|
||||||
|
QModelIndex rootIndex = m_certificateFileModel->setRootPath(CertificateManager::getCertificateDirectory());
|
||||||
|
ui->fileView->setModel(m_certificateFileModel);
|
||||||
|
ui->fileView->setRootIndex(rootIndex);
|
||||||
|
|
||||||
|
m_newCertificateButton = ui->buttonBox->addButton(tr("Create"), QDialogButtonBox::ActionRole);
|
||||||
|
m_openCertificateDirectoryButton = ui->buttonBox->addButton(tr("Open Directory"), QDialogButtonBox::ActionRole);
|
||||||
|
m_deleteCertificateButton = ui->buttonBox->addButton(tr("Delete"), QDialogButtonBox::ActionRole);
|
||||||
|
m_importCertificateButton = ui->buttonBox->addButton(tr("Import"), QDialogButtonBox::ActionRole);
|
||||||
|
|
||||||
connect(m_newCertificateButton, &QPushButton::clicked, this, &CertificateManagerDialog::onNewCertificateClicked);
|
connect(m_newCertificateButton, &QPushButton::clicked, this, &CertificateManagerDialog::onNewCertificateClicked);
|
||||||
|
connect(m_openCertificateDirectoryButton, &QPushButton::clicked, this, &CertificateManagerDialog::onOpenCertificateDirectoryClicked);
|
||||||
|
connect(m_deleteCertificateButton, &QPushButton::clicked, this, &CertificateManagerDialog::onDeleteCertificateClicked);
|
||||||
|
connect(m_importCertificateButton, &QPushButton::clicked, this, &CertificateManagerDialog::onImportCertificateClicked);
|
||||||
|
|
||||||
setMinimumSize(pdf::PDFWidgetUtils::scaleDPI(this, QSize(640, 480)));
|
setMinimumSize(pdf::PDFWidgetUtils::scaleDPI(this, QSize(640, 480)));
|
||||||
}
|
}
|
||||||
|
@ -53,11 +72,62 @@ void CertificateManagerDialog::onNewCertificateClicked()
|
||||||
CreateCertificateDialog dialog(this);
|
CreateCertificateDialog dialog(this);
|
||||||
if (dialog.exec() == CreateCertificateDialog::Accepted)
|
if (dialog.exec() == CreateCertificateDialog::Accepted)
|
||||||
{
|
{
|
||||||
QDir::root().mkpath(CertificateManager::getCertificateDirectory());
|
|
||||||
|
|
||||||
const CertificateManager::NewCertificateInfo info = dialog.getNewCertificateInfo();
|
const CertificateManager::NewCertificateInfo info = dialog.getNewCertificateInfo();
|
||||||
m_certificateManager.createCertificate(info);
|
m_certificateManager.createCertificate(info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CertificateManagerDialog::onOpenCertificateDirectoryClicked()
|
||||||
|
{
|
||||||
|
QDesktopServices::openUrl(QString("file:///%1").arg(CertificateManager::getCertificateDirectory(), QUrl::TolerantMode));
|
||||||
|
}
|
||||||
|
|
||||||
|
void CertificateManagerDialog::onDeleteCertificateClicked()
|
||||||
|
{
|
||||||
|
QFileInfo fileInfo = m_certificateFileModel->fileInfo(ui->fileView->currentIndex());
|
||||||
|
if (fileInfo.exists())
|
||||||
|
{
|
||||||
|
if (QMessageBox::question(this, tr("Confirm delete"), tr("Do you want to delete certificate '%1'?").arg(fileInfo.fileName()), QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes)
|
||||||
|
{
|
||||||
|
QFile file(fileInfo.filePath());
|
||||||
|
if (!file.remove())
|
||||||
|
{
|
||||||
|
QMessageBox::critical(this, tr("Error"), tr("Cannot delete certificate '%1'").arg(fileInfo.fileName()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CertificateManagerDialog::onImportCertificateClicked()
|
||||||
|
{
|
||||||
|
QString selectedFile = QFileDialog::getOpenFileName(this, tr("Import Certificate"), QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), tr("Certificate file (*.pfx);;All files (*.*)"));
|
||||||
|
|
||||||
|
if (selectedFile.isEmpty())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QFile file(selectedFile);
|
||||||
|
if (file.exists())
|
||||||
|
{
|
||||||
|
QString path = CertificateManager::getCertificateDirectory();
|
||||||
|
QString targetFile = QString("%1/%2").arg(path, QFileInfo(file).fileName());
|
||||||
|
if (QFile::exists(targetFile))
|
||||||
|
{
|
||||||
|
QMessageBox::critical(this, tr("Error"), tr("Target file exists. Please rename the certificate file to import."));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (file.copy(targetFile))
|
||||||
|
{
|
||||||
|
QMessageBox::information(this, tr("Import Certificate"), tr("Certificate '%1' was successfully imported.").arg(file.fileName()));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
QMessageBox::critical(this, tr("Import Certificate"), tr("Error occured during certificate '%1' import.").arg(file.fileName()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace pdfplugin
|
} // namespace pdfplugin
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
#include <QDialog>
|
#include <QDialog>
|
||||||
|
|
||||||
class QAction;
|
class QAction;
|
||||||
|
class QFileSystemModel;
|
||||||
|
|
||||||
namespace Ui
|
namespace Ui
|
||||||
{
|
{
|
||||||
|
@ -42,11 +43,17 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void onNewCertificateClicked();
|
void onNewCertificateClicked();
|
||||||
|
void onOpenCertificateDirectoryClicked();
|
||||||
|
void onDeleteCertificateClicked();
|
||||||
|
void onImportCertificateClicked();
|
||||||
|
|
||||||
Ui::CertificateManagerDialog* ui;
|
Ui::CertificateManagerDialog* ui;
|
||||||
CertificateManager m_certificateManager;
|
CertificateManager m_certificateManager;
|
||||||
QPushButton* m_newCertificateButton;
|
QPushButton* m_newCertificateButton;
|
||||||
QPushButton* m_openCertificateDirectoryButton;
|
QPushButton* m_openCertificateDirectoryButton;
|
||||||
|
QPushButton* m_deleteCertificateButton;
|
||||||
|
QPushButton* m_importCertificateButton;
|
||||||
|
QFileSystemModel* m_certificateFileModel;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace pdfplugin
|
} // namespace pdfplugin
|
||||||
|
|
|
@ -19,6 +19,11 @@
|
||||||
<property name="title">
|
<property name="title">
|
||||||
<string>Certificates</string>
|
<string>Certificates</string>
|
||||||
</property>
|
</property>
|
||||||
|
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||||
|
<item>
|
||||||
|
<widget class="QTreeView" name="fileView"/>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
|
@ -27,7 +32,7 @@
|
||||||
<enum>Qt::Horizontal</enum>
|
<enum>Qt::Horizontal</enum>
|
||||||
</property>
|
</property>
|
||||||
<property name="standardButtons">
|
<property name="standardButtons">
|
||||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
|
<set>QDialogButtonBox::Close</set>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
|
|
@ -34,6 +34,9 @@ CreateCertificateDialog::CreateCertificateDialog(QWidget *parent) :
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
|
ui->fileNameEdit->setReadOnly(true);
|
||||||
|
ui->fileNameEdit->setText(CertificateManager::generateCertificateFileName());
|
||||||
|
|
||||||
ui->keyLengthCombo->addItem(tr("1024 bits"), 1024);
|
ui->keyLengthCombo->addItem(tr("1024 bits"), 1024);
|
||||||
ui->keyLengthCombo->addItem(tr("2048 bits"), 2048);
|
ui->keyLengthCombo->addItem(tr("2048 bits"), 2048);
|
||||||
ui->keyLengthCombo->addItem(tr("4096 bits"), 4096);
|
ui->keyLengthCombo->addItem(tr("4096 bits"), 4096);
|
||||||
|
@ -115,7 +118,7 @@ void CreateCertificateDialog::accept()
|
||||||
int days = currentDate.daysTo(date);
|
int days = currentDate.daysTo(date);
|
||||||
|
|
||||||
// Fill certificate info
|
// Fill certificate info
|
||||||
m_newCertificateInfo.fileName = CertificateManager::generateCertificateFileName();
|
m_newCertificateInfo.fileName = ui->fileNameEdit->text();
|
||||||
m_newCertificateInfo.privateKeyPasword = password1;
|
m_newCertificateInfo.privateKeyPasword = password1;
|
||||||
m_newCertificateInfo.certCountryCode = ui->countryCombo->currentData().toString();
|
m_newCertificateInfo.certCountryCode = ui->countryCombo->currentData().toString();
|
||||||
m_newCertificateInfo.certOrganization = ui->organizationEdit->text();
|
m_newCertificateInfo.certOrganization = ui->organizationEdit->text();
|
||||||
|
|
Loading…
Reference in New Issue