Signature plugin: Certificate management

This commit is contained in:
Jakub Melka 2022-05-07 17:36:19 +02:00
parent 148cec5aec
commit 27bed729b5
5 changed files with 105 additions and 21 deletions

View File

@ -65,7 +65,7 @@ void CertificateManager::createCertificate(const NewCertificateInfo& info)
// Set validity of the certificate
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
X509_NAME* name = X509_get_subject_name(certificate.get());
@ -104,18 +104,17 @@ void CertificateManager::createCertificate(const NewCertificateInfo& info)
QByteArray privateKeyPaswordUtf8 = info.privateKeyPasword.toUtf8();
// Write the data
PKCS12* pkcs12 = PKCS12_create(privateKeyPaswordUtf8.constData(),
nullptr,
privateKey.get(),
certificate.get(),
nullptr,
0,
0,
PKCS12_DEFAULT_ITER,
PKCS12_DEFAULT_ITER,
0);
i2d_PKCS12_bio(pksBuffer.get(), pkcs12);
PKCS12_free(pkcs12);
openssl_ptr<PKCS12> pkcs12(PKCS12_create(privateKeyPaswordUtf8.constData(),
nullptr,
privateKey.get(),
certificate.get(),
nullptr,
0,
0,
PKCS12_DEFAULT_ITER,
PKCS12_DEFAULT_ITER,
0), &PKCS12_free);
i2d_PKCS12_bio(pksBuffer.get(), pkcs12.get());
BUF_MEM* pksMemoryBuffer = nullptr;
BIO_get_mem_ptr(pksBuffer.get(), &pksMemoryBuffer);
@ -125,7 +124,7 @@ void CertificateManager::createCertificate(const NewCertificateInfo& info)
QFile file(info.fileName);
if (file.open(QFile::WriteOnly | QFile::Truncate))
{
int datac = file.write(pksMemoryBuffer->data, pksMemoryBuffer->length);
file.write(pksMemoryBuffer->data, pksMemoryBuffer->length);
file.close();
}
}

View File

@ -23,6 +23,10 @@
#include <QAction>
#include <QPushButton>
#include <QFileSystemModel>
#include <QDesktopServices>
#include <QMessageBox>
#include <QFileDialog>
namespace pdfplugin
{
@ -31,14 +35,29 @@ CertificateManagerDialog::CertificateManagerDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::CertificateManagerDialog),
m_newCertificateButton(nullptr),
m_openCertificateDirectoryButton(nullptr)
m_openCertificateDirectoryButton(nullptr),
m_deleteCertificateButton(nullptr),
m_importCertificateButton(nullptr),
m_certificateFileModel(nullptr)
{
ui->setupUi(this);
m_newCertificateButton = ui->buttonBox->addButton(tr("Create Certificate"), QDialogButtonBox::ActionRole);
m_openCertificateDirectoryButton = ui->buttonBox->addButton(tr("Show Certificate Directory"), QDialogButtonBox::ActionRole);
QDir::root().mkpath(CertificateManager::getCertificateDirectory());
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_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)));
}
@ -53,11 +72,62 @@ void CertificateManagerDialog::onNewCertificateClicked()
CreateCertificateDialog dialog(this);
if (dialog.exec() == CreateCertificateDialog::Accepted)
{
QDir::root().mkpath(CertificateManager::getCertificateDirectory());
const CertificateManager::NewCertificateInfo info = dialog.getNewCertificateInfo();
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

View File

@ -23,6 +23,7 @@
#include <QDialog>
class QAction;
class QFileSystemModel;
namespace Ui
{
@ -42,11 +43,17 @@ public:
private:
void onNewCertificateClicked();
void onOpenCertificateDirectoryClicked();
void onDeleteCertificateClicked();
void onImportCertificateClicked();
Ui::CertificateManagerDialog* ui;
CertificateManager m_certificateManager;
QPushButton* m_newCertificateButton;
QPushButton* m_openCertificateDirectoryButton;
QPushButton* m_deleteCertificateButton;
QPushButton* m_importCertificateButton;
QFileSystemModel* m_certificateFileModel;
};
} // namespace pdfplugin

View File

@ -19,6 +19,11 @@
<property name="title">
<string>Certificates</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QTreeView" name="fileView"/>
</item>
</layout>
</widget>
</item>
<item>
@ -27,7 +32,7 @@
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
<set>QDialogButtonBox::Close</set>
</property>
</widget>
</item>

View File

@ -34,6 +34,9 @@ CreateCertificateDialog::CreateCertificateDialog(QWidget *parent) :
{
ui->setupUi(this);
ui->fileNameEdit->setReadOnly(true);
ui->fileNameEdit->setText(CertificateManager::generateCertificateFileName());
ui->keyLengthCombo->addItem(tr("1024 bits"), 1024);
ui->keyLengthCombo->addItem(tr("2048 bits"), 2048);
ui->keyLengthCombo->addItem(tr("4096 bits"), 4096);
@ -115,7 +118,7 @@ void CreateCertificateDialog::accept()
int days = currentDate.daysTo(date);
// Fill certificate info
m_newCertificateInfo.fileName = CertificateManager::generateCertificateFileName();
m_newCertificateInfo.fileName = ui->fileNameEdit->text();
m_newCertificateInfo.privateKeyPasword = password1;
m_newCertificateInfo.certCountryCode = ui->countryCombo->currentData().toString();
m_newCertificateInfo.certOrganization = ui->organizationEdit->text();