mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-06-05 21:59:17 +02:00
Public key encryption: initiate new security handler
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
#include "pdfutils.h"
|
||||
#include "pdfwidgetutils.h"
|
||||
#include "pdfsecurityhandler.h"
|
||||
#include "pdfcertificatemanager.h"
|
||||
#include "pdfdbgheap.h"
|
||||
|
||||
#include <QMessageBox>
|
||||
@@ -40,6 +41,7 @@ PDFEncryptionSettingsDialog::PDFEncryptionSettingsDialog(QByteArray documentId,
|
||||
ui->algorithmComboBox->addItem(tr("RC4 128-bit | R4"), int(pdf::PDFSecurityHandlerFactory::RC4));
|
||||
ui->algorithmComboBox->addItem(tr("AES 128-bit | R4"), int(pdf::PDFSecurityHandlerFactory::AES_128));
|
||||
ui->algorithmComboBox->addItem(tr("AES 256-bit | R6"), int(pdf::PDFSecurityHandlerFactory::AES_256));
|
||||
ui->algorithmComboBox->addItem(tr("Certificate Encryption"), int(pdf::PDFSecurityHandlerFactory::Certificate));
|
||||
|
||||
ui->algorithmComboBox->setCurrentIndex(0);
|
||||
|
||||
@@ -73,6 +75,7 @@ PDFEncryptionSettingsDialog::PDFEncryptionSettingsDialog(QByteArray documentId,
|
||||
m_checkBoxToPermission[ui->permAssembleCheckBox] = pdf::PDFSecurityHandler::Permission::Assemble;
|
||||
m_checkBoxToPermission[ui->permPrintHighResolutionCheckBox] = pdf::PDFSecurityHandler::Permission::PrintHighResolution;
|
||||
|
||||
updateCertificates();
|
||||
updateUi();
|
||||
updatePasswordScore();
|
||||
|
||||
@@ -95,6 +98,7 @@ void PDFEncryptionSettingsDialog::updateUi()
|
||||
|
||||
const pdf::PDFSecurityHandlerFactory::Algorithm algorithm = static_cast<const pdf::PDFSecurityHandlerFactory::Algorithm>(ui->algorithmComboBox->currentData().toInt());
|
||||
const bool encrypted = algorithm != pdf::PDFSecurityHandlerFactory::None;
|
||||
const bool isEncryptedUsingCertificate = algorithm == pdf::PDFSecurityHandlerFactory::Certificate;
|
||||
|
||||
switch (algorithm)
|
||||
{
|
||||
@@ -108,6 +112,7 @@ void PDFEncryptionSettingsDialog::updateUi()
|
||||
ui->algorithmHintWidget->setCurrentValue(4);
|
||||
break;
|
||||
case pdf::PDFSecurityHandlerFactory::AES_256:
|
||||
case pdf::PDFSecurityHandlerFactory::Certificate:
|
||||
ui->algorithmHintWidget->setCurrentValue(5);
|
||||
break;
|
||||
|
||||
@@ -116,20 +121,40 @@ void PDFEncryptionSettingsDialog::updateUi()
|
||||
break;
|
||||
}
|
||||
|
||||
ui->userPasswordEnableCheckBox->setEnabled(encrypted);
|
||||
ui->ownerPasswordEnableCheckBox->setEnabled(false);
|
||||
ui->certificateComboBox->setEnabled(isEncryptedUsingCertificate);
|
||||
|
||||
if (!encrypted)
|
||||
if (!isEncryptedUsingCertificate)
|
||||
{
|
||||
ui->userPasswordEnableCheckBox->setChecked(false);
|
||||
ui->ownerPasswordEnableCheckBox->setChecked(false);
|
||||
ui->userPasswordEnableCheckBox->setEnabled(encrypted);
|
||||
ui->ownerPasswordEnableCheckBox->setEnabled(false);
|
||||
|
||||
ui->userPasswordEdit->clear();
|
||||
ui->ownerPasswordEdit->clear();
|
||||
if (!encrypted)
|
||||
{
|
||||
ui->userPasswordEnableCheckBox->setChecked(false);
|
||||
ui->ownerPasswordEnableCheckBox->setChecked(false);
|
||||
|
||||
ui->userPasswordEdit->clear();
|
||||
ui->ownerPasswordEdit->clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
ui->ownerPasswordEnableCheckBox->setChecked(true);
|
||||
}
|
||||
|
||||
ui->certificateComboBox->setCurrentIndex(-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
ui->ownerPasswordEnableCheckBox->setChecked(true);
|
||||
ui->userPasswordEnableCheckBox->setEnabled(false);
|
||||
ui->ownerPasswordEnableCheckBox->setEnabled(false);
|
||||
|
||||
ui->userPasswordEnableCheckBox->setChecked(true);
|
||||
ui->ownerPasswordEnableCheckBox->setChecked(false);
|
||||
|
||||
if (ui->certificateComboBox->currentIndex() == -1 && ui->certificateComboBox->count() > 0)
|
||||
{
|
||||
ui->certificateComboBox->setCurrentIndex(0);
|
||||
}
|
||||
}
|
||||
|
||||
ui->userPasswordEdit->setEnabled(ui->userPasswordEnableCheckBox->isChecked());
|
||||
@@ -158,6 +183,21 @@ void PDFEncryptionSettingsDialog::updateUi()
|
||||
}
|
||||
}
|
||||
|
||||
void PDFEncryptionSettingsDialog::updateCertificates()
|
||||
{
|
||||
QFileInfoList certificates = pdf::PDFCertificateManager::getCertificates();
|
||||
|
||||
QVariant currentCertificate = ui->certificateComboBox->currentData();
|
||||
|
||||
ui->certificateComboBox->clear();
|
||||
for (const QFileInfo& certificateItem : certificates)
|
||||
{
|
||||
ui->certificateComboBox->addItem(certificateItem.fileName(), certificateItem.absoluteFilePath());
|
||||
}
|
||||
|
||||
ui->certificateComboBox->setCurrentIndex(ui->certificateComboBox->findData(currentCertificate));
|
||||
}
|
||||
|
||||
void PDFEncryptionSettingsDialog::updatePasswordScore()
|
||||
{
|
||||
const pdf::PDFSecurityHandlerFactory::Algorithm algorithm = static_cast<const pdf::PDFSecurityHandlerFactory::Algorithm>(ui->algorithmComboBox->currentData().toInt());
|
||||
@@ -188,6 +228,7 @@ void PDFEncryptionSettingsDialog::accept()
|
||||
settings.userPassword = ui->userPasswordEdit->text();
|
||||
settings.ownerPassword = ui->ownerPasswordEdit->text();
|
||||
settings.permissions = 0;
|
||||
settings.certificateFileName = ui->certificateComboBox->currentData().toString();
|
||||
|
||||
for (auto item : m_checkBoxToPermission)
|
||||
{
|
||||
|
@@ -50,6 +50,7 @@ private:
|
||||
Ui::PDFEncryptionSettingsDialog* ui;
|
||||
|
||||
void updateUi();
|
||||
void updateCertificates();
|
||||
void updatePasswordScore();
|
||||
|
||||
bool m_isUpdatingUi;
|
||||
|
@@ -6,8 +6,8 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>705</width>
|
||||
<height>609</height>
|
||||
<width>843</width>
|
||||
<height>620</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
@@ -20,8 +20,8 @@
|
||||
<string>Encryption Method</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="methodGroupBoxLayout">
|
||||
<item row="0" column="1">
|
||||
<widget class="QComboBox" name="algorithmComboBox"/>
|
||||
<item row="0" column="2">
|
||||
<widget class="pdfviewer::PDFEncryptionStrengthHintWidget" name="algorithmHintWidget" native="true"/>
|
||||
</item>
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="encryptionAlgorithm">
|
||||
@@ -30,19 +30,29 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="2">
|
||||
<widget class="pdfviewer::PDFEncryptionStrengthHintWidget" name="algorithmHintWidget" native="true"/>
|
||||
<item row="0" column="1">
|
||||
<widget class="QComboBox" name="algorithmComboBox"/>
|
||||
</item>
|
||||
<item row="1" column="0" colspan="3">
|
||||
<item row="2" column="0" colspan="3">
|
||||
<widget class="QLabel" name="encryptionMethodHintLabel">
|
||||
<property name="text">
|
||||
<string><html><head/><body><p>Select encryption algorithm. AES-256 is strongly recommended, because older encryption algorithm can be potentially broken. Select older algorithms (as AES-128 or RC4) only, if you need backward compatibility. Also, choose a strong password to ensure strong encryption.</p></body></html></string>
|
||||
<string><html><head/><body><p>Select encryption algorithm. AES-256 is strongly recommended, because older encryption algorithm can be potentially broken. Select older algorithms (as AES-128 or RC4) only, if you need backward compatibility. Also, choose a strong password to ensure strong encryption.</p><p>Public key security using certificate is also supported. In that case, you must select a certificate with private key, and this certificate is then used to encrypt data. User, which wants to open document encrypted with certificate, must have a private key to the certificae.</p></body></html></string>
|
||||
</property>
|
||||
<property name="wordWrap">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</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="certificateComboBox"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
|
@@ -1202,6 +1202,12 @@ void PDFProgramController::onActionEncryptionTriggered()
|
||||
{
|
||||
pdf::PDFSecurityHandlerPointer updatedSecurityHandler = dialog.getUpdatedSecurityHandler();
|
||||
|
||||
if (!updatedSecurityHandler)
|
||||
{
|
||||
QMessageBox::critical(m_mainWindow, QApplication::applicationDisplayName(), tr("Failed to create security handler."));
|
||||
return;
|
||||
}
|
||||
|
||||
// Jakub Melka: If we changed encryption (password), recheck, that user doesn't
|
||||
// forgot (or accidentally entered wrong) password. So, we require owner authentization
|
||||
// to continue.
|
||||
|
Reference in New Issue
Block a user