Skip to content

Commit

Permalink
Issue #183: Option to ignore PDF security settings
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubMelka committed Jun 27, 2024
1 parent 3a57bc6 commit 8166d9f
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 15 deletions.
12 changes: 11 additions & 1 deletion Pdf4QtDiff/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2021 Jakub Melka
// Copyright (C) 2021-2024 Jakub Melka
//
// This file is part of PDF4QT.
//
Expand All @@ -17,6 +17,7 @@

#include "pdfconstants.h"
#include "pdfdocumentreader.h"
#include "pdfsecurityhandler.h"
#include "mainwindow.h"

#include <QApplication>
Expand All @@ -31,14 +32,23 @@ int main(int argc, char *argv[])
QCoreApplication::setApplicationName("PDF4QT Diff");
QCoreApplication::setApplicationVersion(pdf::PDF_LIBRARY_VERSION);
QApplication::setApplicationDisplayName(QApplication::translate("Application", "PDF4QT Diff"));

QCommandLineOption noDrm("no-drm", "Disable DRM settings of documents.");

QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::applicationName());
parser.addOption(noDrm);
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("file1", "The PDF file to be compared.");
parser.addPositionalArgument("file2", "The PDF file to be compared.");
parser.process(application);

if (parser.isSet(noDrm))
{
pdf::PDFSecurityHandler::setNoDRMMode();
}

QIcon appIcon(":/app-icon.svg");
QApplication::setWindowIcon(appIcon);

Expand Down
16 changes: 13 additions & 3 deletions Pdf4QtEditor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "pdfeditormainwindow.h"
#include "pdfconstants.h"
#include "pdfsecurityhandler.h"

#include <QApplication>
#include <QCommandLineParser>
Expand All @@ -36,23 +37,32 @@ int main(int argc, char *argv[])
QCoreApplication::setApplicationName("PDF4QT Editor");
QCoreApplication::setApplicationVersion(pdf::PDF_LIBRARY_VERSION);
QApplication::setApplicationDisplayName(QApplication::translate("Application", "PDF4QT Editor"));

QCommandLineOption noDrm("no-drm", "Disable DRM settings of documents.");

QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::applicationName());
parser.addOption(noDrm);
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("file", "The PDF file to open.");
parser.process(application);

if (parser.isSet(noDrm))
{
pdf::PDFSecurityHandler::setNoDRMMode();
}

QIcon appIcon(":/app-icon.svg");
QApplication::setWindowIcon(appIcon);

pdfviewer::PDFEditorMainWindow mainWindow;
mainWindow.show();

QStringList arguments = application.arguments();
if (arguments.size() > 1)
QStringList arguments = parser.positionalArguments();
if (!arguments.isEmpty())
{
mainWindow.getProgramController()->openDocument(arguments[1]);
mainWindow.getProgramController()->openDocument(arguments.front());
}

return application.exec();
Expand Down
27 changes: 21 additions & 6 deletions Pdf4QtLibCore/sources/pdfsecurityhandler.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019-2022 Jakub Melka
// Copyright (C) 2019-2024 Jakub Melka
//
// This file is part of PDF4QT.
//
Expand Down Expand Up @@ -52,6 +52,8 @@
namespace pdf
{

static bool s_noDrmMode = false;

template<typename T>
using openssl_ptr = std::unique_ptr<T, void(*)(T*)>;

Expand Down Expand Up @@ -514,6 +516,16 @@ PDFSecurityHandlerPointer PDFSecurityHandler::createSecurityHandler(const PDFObj
return handler;
}

void PDFSecurityHandler::setNoDRMMode()
{
s_noDrmMode = true;
}

bool PDFSecurityHandler::isNoDRM()
{
return s_noDrmMode;
}

void PDFSecurityHandler::fillEncryptionDictionary(PDFObjectFactory& factory, bool publicKeyHandler) const
{
factory.beginDictionaryItem("V");
Expand Down Expand Up @@ -1306,9 +1318,10 @@ PDFSecurityHandler::AuthorizationResult PDFStandardSecurityHandler::authenticate
if (U == m_U)
{
// 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;
return AuthorizationResult::UserAuthorized;
return authorizationResult;
}
}

Expand Down Expand Up @@ -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);

// We have authorized user access
m_authorizationData.authorizationResult = AuthorizationResult::UserAuthorized;
const AuthorizationResult authorizationResult = isNoDRM() ? AuthorizationResult::OwnerAuthorized : AuthorizationResult::UserAuthorized;
m_authorizationData.authorizationResult = authorizationResult;
}
}

Expand Down Expand Up @@ -2524,9 +2538,10 @@ PDFSecurityHandler::AuthorizationResult PDFPublicKeySecurityHandler::authenticat
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.authorizationResult = AuthorizationResult::UserAuthorized;
return AuthorizationResult::UserAuthorized;
m_authorizationData.authorizationResult = authorizationResult;
return authorizationResult;
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion Pdf4QtLibCore/sources/pdfsecurityhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ using PDFSecurityHandlerPointer = QSharedPointer<PDFSecurityHandler>;

class PDFStandardSecurityHandler;

class PDFSecurityHandler
class PDF4QTLIBCORESHARED_EXPORT PDFSecurityHandler
{
public:
explicit PDFSecurityHandler() = default;
Expand Down Expand Up @@ -203,6 +203,12 @@ class PDFSecurityHandler
/// \param id First part of the id of the document
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:
friend class PDFSecurityHandlerFactory;

Expand Down
12 changes: 11 additions & 1 deletion Pdf4QtPageMaster/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2021 Jakub Melka
// Copyright (C) 2021-2024 Jakub Melka
//
// This file is part of PDF4QT.
//
Expand All @@ -16,6 +16,7 @@
// along with PDF4QT. If not, see <https://www.gnu.org/licenses/>.

#include "pdfconstants.h"
#include "pdfsecurityhandler.h"
#include "mainwindow.h"

#include <QApplication>
Expand All @@ -30,13 +31,22 @@ int main(int argc, char *argv[])
QCoreApplication::setApplicationName("PDF4QT PageMaster");
QCoreApplication::setApplicationVersion(pdf::PDF_LIBRARY_VERSION);
QApplication::setApplicationDisplayName(QApplication::translate("Application", "PDF4QT PageMaster"));

QCommandLineOption noDrm("no-drm", "Disable DRM settings of documents.");

QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::applicationName());
parser.addOption(noDrm);
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("file", "The PDF file to open.");
parser.process(application);

if (parser.isSet(noDrm))
{
pdf::PDFSecurityHandler::setNoDRMMode();
}

QIcon appIcon(":/app-icon.svg");
QApplication::setWindowIcon(appIcon);

Expand Down
16 changes: 13 additions & 3 deletions Pdf4QtViewer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "pdfviewermainwindow.h"
#include "pdfconstants.h"
#include "pdfsecurityhandler.h"

#include <QApplication>
#include <QCommandLineParser>
Expand All @@ -30,23 +31,32 @@ int main(int argc, char *argv[])
QCoreApplication::setApplicationName("PDF4QT Viewer");
QCoreApplication::setApplicationVersion(pdf::PDF_LIBRARY_VERSION);
QApplication::setApplicationDisplayName(QApplication::translate("Application", "PDF4QT Viewer"));

QCommandLineOption noDrm("no-drm", "Disable DRM settings of documents.");

QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::applicationName());
parser.addOption(noDrm);
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("file", "The PDF file to open.");
parser.process(application);

if (parser.isSet(noDrm))
{
pdf::PDFSecurityHandler::setNoDRMMode();
}

QIcon appIcon(":/app-icon.svg");
QApplication::setWindowIcon(appIcon);

pdfviewer::PDFViewerMainWindow mainWindow;
mainWindow.show();

QStringList arguments = application.arguments();
if (arguments.size() > 1)
QStringList arguments = parser.positionalArguments();
if (arguments.size() > 0)
{
mainWindow.getProgramController()->openDocument(arguments[1]);
mainWindow.getProgramController()->openDocument(arguments.front());
}

return application.exec();
Expand Down
2 changes: 2 additions & 0 deletions RELEASES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
CURRENT:
- Issue #183: Option to ignore PDF security settings
- 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 #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.
Expand Down

0 comments on commit 8166d9f

Please sign in to comment.