Signature plugin: write certificate

This commit is contained in:
Jakub Melka 2022-05-03 20:00:47 +02:00
parent 7fc3e01960
commit 148cec5aec
5 changed files with 90 additions and 25 deletions

View File

@ -17,6 +17,10 @@
#include "certificatemanager.h"
#include <QDir>
#include <QFile>
#include <QStandardPaths>
#include <openssl/bio.h>
#include <openssl/rsa.h>
#include <openssl/rsaerr.h>
@ -24,6 +28,7 @@
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/x509v3.h>
#include <openssl/pkcs12.h>
#include <memory>
@ -40,10 +45,9 @@ using openssl_ptr = std::unique_ptr<T, void(*)(T*)>;
void CertificateManager::createCertificate(const NewCertificateInfo& info)
{
openssl_ptr<BIO> certificateBuffer(BIO_new(BIO_s_mem()), &BIO_free_all);
openssl_ptr<BIO> privateKeyBuffer(BIO_new(BIO_s_mem()), &BIO_free_all);
openssl_ptr<BIO> pksBuffer(BIO_new(BIO_s_mem()), &BIO_free_all);
if (certificateBuffer && privateKeyBuffer)
if (pksBuffer)
{
openssl_ptr<BIGNUM> bignumber(BN_new(), &BN_free);
openssl_ptr<RSA> rsaKey(RSA_new(), &RSA_free);
@ -83,10 +87,12 @@ void CertificateManager::createCertificate(const NewCertificateInfo& info)
addString("E", info.certEmail);
X509_EXTENSION* extension = nullptr;
X509V3_CTX context;
X509V3_CTX context = { };
X509V3_set_ctx_nodb(&context);
X509V3_set_ctx(&context, certificate.get(), certificate.get(), nullptr, nullptr, 0);
extension = X509V3_EXT_conf_nid (NULL, &context, NID_key_usage, "digitalSignature, keyAgreement");
X509_add_ext(certificate.get(), extension, -1);
X509_EXTENSION_free(extension);
X509_set_issuer_name(certificate.get(), name);
@ -98,15 +104,57 @@ void CertificateManager::createCertificate(const NewCertificateInfo& info)
QByteArray privateKeyPaswordUtf8 = info.privateKeyPasword.toUtf8();
// Write the data
const int retWritePrivateKey = PEM_write_bio_PKCS8PrivateKey(privateKeyBuffer.get(), privateKey.get(), EVP_aes_256_cbc(), privateKeyPaswordUtf8.data(), privateKeyPaswordUtf8.size(), nullptr, nullptr);
const int retWriteCertificate = PEM_write_bio_X509(certificateBuffer.get(), certificate.get());
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);
if (retWritePrivateKey == 1 && retWriteCertificate == 1)
BUF_MEM* pksMemoryBuffer = nullptr;
BIO_get_mem_ptr(pksBuffer.get(), &pksMemoryBuffer);
if (!info.fileName.isEmpty())
{
QFile file(info.fileName);
if (file.open(QFile::WriteOnly | QFile::Truncate))
{
int datac = file.write(pksMemoryBuffer->data, pksMemoryBuffer->length);
file.close();
}
}
}
}
}
QString CertificateManager::getCertificateDirectory()
{
QDir directory(QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).front() + "/certificates/");
return directory.absolutePath();
}
QString CertificateManager::generateCertificateFileName()
{
QString directoryString = getCertificateDirectory();
QDir directory(directoryString);
int certificateIndex = 1;
while (true)
{
QString fileName = directory.absoluteFilePath(QString("cert_%1.pfx").arg(certificateIndex++));
if (!QFile::exists(fileName))
{
return fileName;
}
}
return QString();
}
} // namespace pdfplugin

View File

@ -30,8 +30,7 @@ public:
struct NewCertificateInfo
{
QString certificateFileName;
QString privateKeyFileName;
QString fileName;
QString privateKeyPasword;
QString certCountryCode;
@ -46,6 +45,9 @@ public:
};
void createCertificate(const NewCertificateInfo& info);
static QString getCertificateDirectory();
static QString generateCertificateFileName();
};
} // namespace pdfplugin

View File

@ -53,6 +53,8 @@ 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);
}

View File

@ -18,6 +18,8 @@
#include "createcertificatedialog.h"
#include "ui_createcertificatedialog.h"
#include "certificatemanager.h"
#include <QMessageBox>
#include <QInputDialog>
#include <QDate>
@ -113,6 +115,7 @@ void CreateCertificateDialog::accept()
int days = currentDate.daysTo(date);
// Fill certificate info
m_newCertificateInfo.fileName = CertificateManager::generateCertificateFileName();
m_newCertificateInfo.privateKeyPasword = password1;
m_newCertificateInfo.certCountryCode = ui->countryCombo->currentData().toString();
m_newCertificateInfo.certOrganization = ui->organizationEdit->text();

View File

@ -20,42 +20,42 @@
<string>Create Self Signed Certificate</string>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<item row="1" column="0">
<widget class="QLabel" name="nameLabel">
<property name="text">
<string>Name</string>
</property>
</widget>
</item>
<item row="0" column="1">
<item row="1" column="1">
<widget class="QLineEdit" name="commonNameEdit">
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<item row="2" column="0">
<widget class="QLabel" name="organizationLabel">
<property name="text">
<string>Organization</string>
</property>
</widget>
</item>
<item row="2" column="0">
<item row="3" column="0">
<widget class="QLabel" name="organizationUnitLabel">
<property name="text">
<string>Organization Unit</string>
</property>
</widget>
</item>
<item row="4" column="0">
<item row="5" column="0">
<widget class="QLabel" name="emailLabel">
<property name="text">
<string>Email</string>
</property>
</widget>
</item>
<item row="7" column="0">
<item row="8" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
@ -68,57 +68,67 @@
</property>
</spacer>
</item>
<item row="1" column="1">
<item row="2" column="1">
<widget class="QLineEdit" name="organizationEdit">
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="3" column="1">
<widget class="QLineEdit" name="organizationUnitEdit">
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="4" column="1">
<item row="5" column="1">
<widget class="QLineEdit" name="emailEdit">
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="0">
<item row="4" column="0">
<widget class="QLabel" name="countryLabel">
<property name="text">
<string>Country</string>
</property>
</widget>
</item>
<item row="5" column="0">
<item row="6" column="0">
<widget class="QLabel" name="keyLengthLabel">
<property name="text">
<string>Key length</string>
</property>
</widget>
</item>
<item row="6" column="0">
<item row="7" column="0">
<widget class="QLabel" name="validityLabel">
<property name="text">
<string>Valid till</string>
</property>
</widget>
</item>
<item row="6" column="1">
<item row="7" column="1">
<widget class="QCalendarWidget" name="validTillEdit"/>
</item>
<item row="5" column="1">
<item row="6" column="1">
<widget class="QComboBox" name="keyLengthCombo"/>
</item>
<item row="3" column="1">
<item row="4" column="1">
<widget class="QComboBox" name="countryCombo"/>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="fileNameEdit"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="certificateFileLabel">
<property name="text">
<string>Certificate file</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>