Issue #183: Option to ignore PDF security settings

This commit is contained in:
Jakub Melka 2024-06-27 16:00:11 +02:00
parent 3a57bc6de5
commit 8166d9f8f4
7 changed files with 78 additions and 15 deletions

View File

@ -1,4 +1,4 @@
// Copyright (C) 2021 Jakub Melka // Copyright (C) 2021-2024 Jakub Melka
// //
// This file is part of PDF4QT. // This file is part of PDF4QT.
// //
@ -17,6 +17,7 @@
#include "pdfconstants.h" #include "pdfconstants.h"
#include "pdfdocumentreader.h" #include "pdfdocumentreader.h"
#include "pdfsecurityhandler.h"
#include "mainwindow.h" #include "mainwindow.h"
#include <QApplication> #include <QApplication>
@ -31,14 +32,23 @@ int main(int argc, char *argv[])
QCoreApplication::setApplicationName("PDF4QT Diff"); QCoreApplication::setApplicationName("PDF4QT Diff");
QCoreApplication::setApplicationVersion(pdf::PDF_LIBRARY_VERSION); QCoreApplication::setApplicationVersion(pdf::PDF_LIBRARY_VERSION);
QApplication::setApplicationDisplayName(QApplication::translate("Application", "PDF4QT Diff")); QApplication::setApplicationDisplayName(QApplication::translate("Application", "PDF4QT Diff"));
QCommandLineOption noDrm("no-drm", "Disable DRM settings of documents.");
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::applicationName()); parser.setApplicationDescription(QCoreApplication::applicationName());
parser.addOption(noDrm);
parser.addHelpOption(); parser.addHelpOption();
parser.addVersionOption(); parser.addVersionOption();
parser.addPositionalArgument("file1", "The PDF file to be compared."); parser.addPositionalArgument("file1", "The PDF file to be compared.");
parser.addPositionalArgument("file2", "The PDF file to be compared."); parser.addPositionalArgument("file2", "The PDF file to be compared.");
parser.process(application); parser.process(application);
if (parser.isSet(noDrm))
{
pdf::PDFSecurityHandler::setNoDRMMode();
}
QIcon appIcon(":/app-icon.svg"); QIcon appIcon(":/app-icon.svg");
QApplication::setWindowIcon(appIcon); QApplication::setWindowIcon(appIcon);

View File

@ -17,6 +17,7 @@
#include "pdfeditormainwindow.h" #include "pdfeditormainwindow.h"
#include "pdfconstants.h" #include "pdfconstants.h"
#include "pdfsecurityhandler.h"
#include <QApplication> #include <QApplication>
#include <QCommandLineParser> #include <QCommandLineParser>
@ -36,23 +37,32 @@ int main(int argc, char *argv[])
QCoreApplication::setApplicationName("PDF4QT Editor"); QCoreApplication::setApplicationName("PDF4QT Editor");
QCoreApplication::setApplicationVersion(pdf::PDF_LIBRARY_VERSION); QCoreApplication::setApplicationVersion(pdf::PDF_LIBRARY_VERSION);
QApplication::setApplicationDisplayName(QApplication::translate("Application", "PDF4QT Editor")); QApplication::setApplicationDisplayName(QApplication::translate("Application", "PDF4QT Editor"));
QCommandLineOption noDrm("no-drm", "Disable DRM settings of documents.");
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::applicationName()); parser.setApplicationDescription(QCoreApplication::applicationName());
parser.addOption(noDrm);
parser.addHelpOption(); parser.addHelpOption();
parser.addVersionOption(); parser.addVersionOption();
parser.addPositionalArgument("file", "The PDF file to open."); parser.addPositionalArgument("file", "The PDF file to open.");
parser.process(application); parser.process(application);
if (parser.isSet(noDrm))
{
pdf::PDFSecurityHandler::setNoDRMMode();
}
QIcon appIcon(":/app-icon.svg"); QIcon appIcon(":/app-icon.svg");
QApplication::setWindowIcon(appIcon); QApplication::setWindowIcon(appIcon);
pdfviewer::PDFEditorMainWindow mainWindow; pdfviewer::PDFEditorMainWindow mainWindow;
mainWindow.show(); mainWindow.show();
QStringList arguments = application.arguments(); QStringList arguments = parser.positionalArguments();
if (arguments.size() > 1) if (!arguments.isEmpty())
{ {
mainWindow.getProgramController()->openDocument(arguments[1]); mainWindow.getProgramController()->openDocument(arguments.front());
} }
return application.exec(); return application.exec();

View File

@ -1,4 +1,4 @@
// Copyright (C) 2019-2022 Jakub Melka // Copyright (C) 2019-2024 Jakub Melka
// //
// This file is part of PDF4QT. // This file is part of PDF4QT.
// //
@ -52,6 +52,8 @@
namespace pdf namespace pdf
{ {
static bool s_noDrmMode = false;
template<typename T> template<typename T>
using openssl_ptr = std::unique_ptr<T, void(*)(T*)>; using openssl_ptr = std::unique_ptr<T, void(*)(T*)>;
@ -514,6 +516,16 @@ PDFSecurityHandlerPointer PDFSecurityHandler::createSecurityHandler(const PDFObj
return handler; return handler;
} }
void PDFSecurityHandler::setNoDRMMode()
{
s_noDrmMode = true;
}
bool PDFSecurityHandler::isNoDRM()
{
return s_noDrmMode;
}
void PDFSecurityHandler::fillEncryptionDictionary(PDFObjectFactory& factory, bool publicKeyHandler) const void PDFSecurityHandler::fillEncryptionDictionary(PDFObjectFactory& factory, bool publicKeyHandler) const
{ {
factory.beginDictionaryItem("V"); factory.beginDictionaryItem("V");
@ -1306,9 +1318,10 @@ PDFSecurityHandler::AuthorizationResult PDFStandardSecurityHandler::authenticate
if (U == m_U) if (U == m_U)
{ {
// We have authorized user access // We have authorized user access
m_authorizationData.authorizationResult = AuthorizationResult::UserAuthorized; const AuthorizationResult authorizationResult = isNoDRM() ? AuthorizationResult::OwnerAuthorized : AuthorizationResult::UserAuthorized;
m_authorizationData.authorizationResult = authorizationResult;
m_authorizationData.fileEncryptionKey = fileEncryptionKey; m_authorizationData.fileEncryptionKey = fileEncryptionKey;
return AuthorizationResult::UserAuthorized; return authorizationResult;
} }
} }
@ -1375,7 +1388,8 @@ PDFSecurityHandler::AuthorizationResult PDFStandardSecurityHandler::authenticate
AES_cbc_encrypt(convertByteArrayToUcharPtr(m_UE), convertByteArrayToUcharPtr(m_authorizationData.fileEncryptionKey), m_UE.size(), &key, aesInitializationVector, AES_DECRYPT); AES_cbc_encrypt(convertByteArrayToUcharPtr(m_UE), convertByteArrayToUcharPtr(m_authorizationData.fileEncryptionKey), m_UE.size(), &key, aesInitializationVector, AES_DECRYPT);
// We have authorized user access // We have authorized user access
m_authorizationData.authorizationResult = AuthorizationResult::UserAuthorized; const AuthorizationResult authorizationResult = isNoDRM() ? AuthorizationResult::OwnerAuthorized : AuthorizationResult::UserAuthorized;
m_authorizationData.authorizationResult = authorizationResult;
} }
} }
@ -2524,9 +2538,10 @@ PDFSecurityHandler::AuthorizationResult PDFPublicKeySecurityHandler::authenticat
m_permissions = qFromLittleEndian<uint32_t>(decryptedData.data() + 20); m_permissions = qFromLittleEndian<uint32_t>(decryptedData.data() + 20);
} }
const AuthorizationResult authorizationResult = isNoDRM() ? AuthorizationResult::OwnerAuthorized : AuthorizationResult::UserAuthorized;
m_authorizationData.fileEncryptionKey = digestBuffer.left(m_keyLength / 8); m_authorizationData.fileEncryptionKey = digestBuffer.left(m_keyLength / 8);
m_authorizationData.authorizationResult = AuthorizationResult::UserAuthorized; m_authorizationData.authorizationResult = authorizationResult;
return AuthorizationResult::UserAuthorized; return authorizationResult;
} }
} }
} }

View File

@ -81,7 +81,7 @@ using PDFSecurityHandlerPointer = QSharedPointer<PDFSecurityHandler>;
class PDFStandardSecurityHandler; class PDFStandardSecurityHandler;
class PDFSecurityHandler class PDF4QTLIBCORESHARED_EXPORT PDFSecurityHandler
{ {
public: public:
explicit PDFSecurityHandler() = default; explicit PDFSecurityHandler() = default;
@ -203,6 +203,12 @@ public:
/// \param id First part of the id of the document /// \param id First part of the id of the document
static PDFSecurityHandlerPointer createSecurityHandler(const PDFObject& encryptionDictionaryObject, const QByteArray& id); static PDFSecurityHandlerPointer createSecurityHandler(const PDFObject& encryptionDictionaryObject, const QByteArray& id);
/// Disable DRM mode of documents.
static void setNoDRMMode();
/// Returns true, if no DRM mode is enabled
static bool isNoDRM();
protected: protected:
friend class PDFSecurityHandlerFactory; friend class PDFSecurityHandlerFactory;

View File

@ -1,4 +1,4 @@
// Copyright (C) 2021 Jakub Melka // Copyright (C) 2021-2024 Jakub Melka
// //
// This file is part of PDF4QT. // This file is part of PDF4QT.
// //
@ -16,6 +16,7 @@
// along with PDF4QT. If not, see <https://www.gnu.org/licenses/>. // along with PDF4QT. If not, see <https://www.gnu.org/licenses/>.
#include "pdfconstants.h" #include "pdfconstants.h"
#include "pdfsecurityhandler.h"
#include "mainwindow.h" #include "mainwindow.h"
#include <QApplication> #include <QApplication>
@ -30,13 +31,22 @@ int main(int argc, char *argv[])
QCoreApplication::setApplicationName("PDF4QT PageMaster"); QCoreApplication::setApplicationName("PDF4QT PageMaster");
QCoreApplication::setApplicationVersion(pdf::PDF_LIBRARY_VERSION); QCoreApplication::setApplicationVersion(pdf::PDF_LIBRARY_VERSION);
QApplication::setApplicationDisplayName(QApplication::translate("Application", "PDF4QT PageMaster")); QApplication::setApplicationDisplayName(QApplication::translate("Application", "PDF4QT PageMaster"));
QCommandLineOption noDrm("no-drm", "Disable DRM settings of documents.");
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::applicationName()); parser.setApplicationDescription(QCoreApplication::applicationName());
parser.addOption(noDrm);
parser.addHelpOption(); parser.addHelpOption();
parser.addVersionOption(); parser.addVersionOption();
parser.addPositionalArgument("file", "The PDF file to open."); parser.addPositionalArgument("file", "The PDF file to open.");
parser.process(application); parser.process(application);
if (parser.isSet(noDrm))
{
pdf::PDFSecurityHandler::setNoDRMMode();
}
QIcon appIcon(":/app-icon.svg"); QIcon appIcon(":/app-icon.svg");
QApplication::setWindowIcon(appIcon); QApplication::setWindowIcon(appIcon);

View File

@ -17,6 +17,7 @@
#include "pdfviewermainwindow.h" #include "pdfviewermainwindow.h"
#include "pdfconstants.h" #include "pdfconstants.h"
#include "pdfsecurityhandler.h"
#include <QApplication> #include <QApplication>
#include <QCommandLineParser> #include <QCommandLineParser>
@ -30,23 +31,32 @@ int main(int argc, char *argv[])
QCoreApplication::setApplicationName("PDF4QT Viewer"); QCoreApplication::setApplicationName("PDF4QT Viewer");
QCoreApplication::setApplicationVersion(pdf::PDF_LIBRARY_VERSION); QCoreApplication::setApplicationVersion(pdf::PDF_LIBRARY_VERSION);
QApplication::setApplicationDisplayName(QApplication::translate("Application", "PDF4QT Viewer")); QApplication::setApplicationDisplayName(QApplication::translate("Application", "PDF4QT Viewer"));
QCommandLineOption noDrm("no-drm", "Disable DRM settings of documents.");
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::applicationName()); parser.setApplicationDescription(QCoreApplication::applicationName());
parser.addOption(noDrm);
parser.addHelpOption(); parser.addHelpOption();
parser.addVersionOption(); parser.addVersionOption();
parser.addPositionalArgument("file", "The PDF file to open."); parser.addPositionalArgument("file", "The PDF file to open.");
parser.process(application); parser.process(application);
if (parser.isSet(noDrm))
{
pdf::PDFSecurityHandler::setNoDRMMode();
}
QIcon appIcon(":/app-icon.svg"); QIcon appIcon(":/app-icon.svg");
QApplication::setWindowIcon(appIcon); QApplication::setWindowIcon(appIcon);
pdfviewer::PDFViewerMainWindow mainWindow; pdfviewer::PDFViewerMainWindow mainWindow;
mainWindow.show(); mainWindow.show();
QStringList arguments = application.arguments(); QStringList arguments = parser.positionalArguments();
if (arguments.size() > 1) if (arguments.size() > 0)
{ {
mainWindow.getProgramController()->openDocument(arguments[1]); mainWindow.getProgramController()->openDocument(arguments.front());
} }
return application.exec(); return application.exec();

View File

@ -1,5 +1,7 @@
CURRENT: CURRENT:
- Issue #183: Option to ignore PDF security settings
- Issue #182: PDF4QT Document Diff command line cmd line arguments ignored - Issue #182: PDF4QT Document Diff command line cmd line arguments ignored
- Issue #181: where is the dark mode?
- Issue #173: errors loading file Echoplex.pdf - Issue #173: errors loading file Echoplex.pdf
- Issue #172: access keys are missing from most menu items/action text strings - Issue #172: access keys are missing from most menu items/action text strings
- Issue #168: When opening a PDF file or merging some PDF files, stamp will disappear. - Issue #168: When opening a PDF file or merging some PDF files, stamp will disappear.