mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-06-05 21:59:17 +02:00
Signature plugin: write certificate
This commit is contained in:
@ -17,6 +17,10 @@
|
|||||||
|
|
||||||
#include "certificatemanager.h"
|
#include "certificatemanager.h"
|
||||||
|
|
||||||
|
#include <QDir>
|
||||||
|
#include <QFile>
|
||||||
|
#include <QStandardPaths>
|
||||||
|
|
||||||
#include <openssl/bio.h>
|
#include <openssl/bio.h>
|
||||||
#include <openssl/rsa.h>
|
#include <openssl/rsa.h>
|
||||||
#include <openssl/rsaerr.h>
|
#include <openssl/rsaerr.h>
|
||||||
@ -24,6 +28,7 @@
|
|||||||
#include <openssl/evp.h>
|
#include <openssl/evp.h>
|
||||||
#include <openssl/pem.h>
|
#include <openssl/pem.h>
|
||||||
#include <openssl/x509v3.h>
|
#include <openssl/x509v3.h>
|
||||||
|
#include <openssl/pkcs12.h>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
@ -40,10 +45,9 @@ using openssl_ptr = std::unique_ptr<T, void(*)(T*)>;
|
|||||||
|
|
||||||
void CertificateManager::createCertificate(const NewCertificateInfo& info)
|
void CertificateManager::createCertificate(const NewCertificateInfo& info)
|
||||||
{
|
{
|
||||||
openssl_ptr<BIO> certificateBuffer(BIO_new(BIO_s_mem()), &BIO_free_all);
|
openssl_ptr<BIO> pksBuffer(BIO_new(BIO_s_mem()), &BIO_free_all);
|
||||||
openssl_ptr<BIO> privateKeyBuffer(BIO_new(BIO_s_mem()), &BIO_free_all);
|
|
||||||
|
|
||||||
if (certificateBuffer && privateKeyBuffer)
|
if (pksBuffer)
|
||||||
{
|
{
|
||||||
openssl_ptr<BIGNUM> bignumber(BN_new(), &BN_free);
|
openssl_ptr<BIGNUM> bignumber(BN_new(), &BN_free);
|
||||||
openssl_ptr<RSA> rsaKey(RSA_new(), &RSA_free);
|
openssl_ptr<RSA> rsaKey(RSA_new(), &RSA_free);
|
||||||
@ -83,10 +87,12 @@ void CertificateManager::createCertificate(const NewCertificateInfo& info)
|
|||||||
addString("E", info.certEmail);
|
addString("E", info.certEmail);
|
||||||
|
|
||||||
X509_EXTENSION* extension = nullptr;
|
X509_EXTENSION* extension = nullptr;
|
||||||
X509V3_CTX context;
|
X509V3_CTX context = { };
|
||||||
X509V3_set_ctx_nodb(&context);
|
X509V3_set_ctx_nodb(&context);
|
||||||
X509V3_set_ctx(&context, certificate.get(), certificate.get(), nullptr, nullptr, 0);
|
X509V3_set_ctx(&context, certificate.get(), certificate.get(), nullptr, nullptr, 0);
|
||||||
extension = X509V3_EXT_conf_nid (NULL, &context, NID_key_usage, "digitalSignature, keyAgreement");
|
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);
|
X509_set_issuer_name(certificate.get(), name);
|
||||||
|
|
||||||
@ -98,15 +104,57 @@ void CertificateManager::createCertificate(const NewCertificateInfo& info)
|
|||||||
QByteArray privateKeyPaswordUtf8 = info.privateKeyPasword.toUtf8();
|
QByteArray privateKeyPaswordUtf8 = info.privateKeyPasword.toUtf8();
|
||||||
|
|
||||||
// Write the data
|
// Write the data
|
||||||
const int retWritePrivateKey = PEM_write_bio_PKCS8PrivateKey(privateKeyBuffer.get(), privateKey.get(), EVP_aes_256_cbc(), privateKeyPaswordUtf8.data(), privateKeyPaswordUtf8.size(), nullptr, nullptr);
|
PKCS12* pkcs12 = PKCS12_create(privateKeyPaswordUtf8.constData(),
|
||||||
const int retWriteCertificate = PEM_write_bio_X509(certificateBuffer.get(), certificate.get());
|
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
|
} // namespace pdfplugin
|
||||||
|
@ -30,8 +30,7 @@ public:
|
|||||||
|
|
||||||
struct NewCertificateInfo
|
struct NewCertificateInfo
|
||||||
{
|
{
|
||||||
QString certificateFileName;
|
QString fileName;
|
||||||
QString privateKeyFileName;
|
|
||||||
QString privateKeyPasword;
|
QString privateKeyPasword;
|
||||||
|
|
||||||
QString certCountryCode;
|
QString certCountryCode;
|
||||||
@ -46,6 +45,9 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
void createCertificate(const NewCertificateInfo& info);
|
void createCertificate(const NewCertificateInfo& info);
|
||||||
|
|
||||||
|
static QString getCertificateDirectory();
|
||||||
|
static QString generateCertificateFileName();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace pdfplugin
|
} // namespace pdfplugin
|
||||||
|
@ -53,6 +53,8 @@ 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);
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
#include "createcertificatedialog.h"
|
#include "createcertificatedialog.h"
|
||||||
#include "ui_createcertificatedialog.h"
|
#include "ui_createcertificatedialog.h"
|
||||||
|
|
||||||
|
#include "certificatemanager.h"
|
||||||
|
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
#include <QInputDialog>
|
#include <QInputDialog>
|
||||||
#include <QDate>
|
#include <QDate>
|
||||||
@ -113,6 +115,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.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();
|
||||||
|
@ -20,42 +20,42 @@
|
|||||||
<string>Create Self Signed Certificate</string>
|
<string>Create Self Signed Certificate</string>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QFormLayout" name="formLayout">
|
<layout class="QFormLayout" name="formLayout">
|
||||||
<item row="0" column="0">
|
<item row="1" column="0">
|
||||||
<widget class="QLabel" name="nameLabel">
|
<widget class="QLabel" name="nameLabel">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Name</string>
|
<string>Name</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="0" column="1">
|
<item row="1" column="1">
|
||||||
<widget class="QLineEdit" name="commonNameEdit">
|
<widget class="QLineEdit" name="commonNameEdit">
|
||||||
<property name="clearButtonEnabled">
|
<property name="clearButtonEnabled">
|
||||||
<bool>true</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="1" column="0">
|
<item row="2" column="0">
|
||||||
<widget class="QLabel" name="organizationLabel">
|
<widget class="QLabel" name="organizationLabel">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Organization</string>
|
<string>Organization</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="2" column="0">
|
<item row="3" column="0">
|
||||||
<widget class="QLabel" name="organizationUnitLabel">
|
<widget class="QLabel" name="organizationUnitLabel">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Organization Unit</string>
|
<string>Organization Unit</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="4" column="0">
|
<item row="5" column="0">
|
||||||
<widget class="QLabel" name="emailLabel">
|
<widget class="QLabel" name="emailLabel">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Email</string>
|
<string>Email</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="7" column="0">
|
<item row="8" column="0">
|
||||||
<spacer name="verticalSpacer">
|
<spacer name="verticalSpacer">
|
||||||
<property name="orientation">
|
<property name="orientation">
|
||||||
<enum>Qt::Vertical</enum>
|
<enum>Qt::Vertical</enum>
|
||||||
@ -68,57 +68,67 @@
|
|||||||
</property>
|
</property>
|
||||||
</spacer>
|
</spacer>
|
||||||
</item>
|
</item>
|
||||||
<item row="1" column="1">
|
<item row="2" column="1">
|
||||||
<widget class="QLineEdit" name="organizationEdit">
|
<widget class="QLineEdit" name="organizationEdit">
|
||||||
<property name="clearButtonEnabled">
|
<property name="clearButtonEnabled">
|
||||||
<bool>true</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="2" column="1">
|
<item row="3" column="1">
|
||||||
<widget class="QLineEdit" name="organizationUnitEdit">
|
<widget class="QLineEdit" name="organizationUnitEdit">
|
||||||
<property name="clearButtonEnabled">
|
<property name="clearButtonEnabled">
|
||||||
<bool>true</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="4" column="1">
|
<item row="5" column="1">
|
||||||
<widget class="QLineEdit" name="emailEdit">
|
<widget class="QLineEdit" name="emailEdit">
|
||||||
<property name="clearButtonEnabled">
|
<property name="clearButtonEnabled">
|
||||||
<bool>true</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="3" column="0">
|
<item row="4" column="0">
|
||||||
<widget class="QLabel" name="countryLabel">
|
<widget class="QLabel" name="countryLabel">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Country</string>
|
<string>Country</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="5" column="0">
|
<item row="6" column="0">
|
||||||
<widget class="QLabel" name="keyLengthLabel">
|
<widget class="QLabel" name="keyLengthLabel">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Key length</string>
|
<string>Key length</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="6" column="0">
|
<item row="7" column="0">
|
||||||
<widget class="QLabel" name="validityLabel">
|
<widget class="QLabel" name="validityLabel">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Valid till</string>
|
<string>Valid till</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="6" column="1">
|
<item row="7" column="1">
|
||||||
<widget class="QCalendarWidget" name="validTillEdit"/>
|
<widget class="QCalendarWidget" name="validTillEdit"/>
|
||||||
</item>
|
</item>
|
||||||
<item row="5" column="1">
|
<item row="6" column="1">
|
||||||
<widget class="QComboBox" name="keyLengthCombo"/>
|
<widget class="QComboBox" name="keyLengthCombo"/>
|
||||||
</item>
|
</item>
|
||||||
<item row="3" column="1">
|
<item row="4" column="1">
|
||||||
<widget class="QComboBox" name="countryCombo"/>
|
<widget class="QComboBox" name="countryCombo"/>
|
||||||
</item>
|
</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>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
Reference in New Issue
Block a user