Encryption tool

This commit is contained in:
Jakub Melka
2021-06-01 16:09:00 +02:00
parent 453a3a24c7
commit c22cba2f50
12 changed files with 320 additions and 13 deletions

View File

@@ -47,6 +47,7 @@ SOURCES += \
pdftoolcertstore.cpp \
pdftoolcolorprofiles.cpp \
pdftooldecrypt.cpp \
pdftoolencrypt.cpp \
pdftoolfetchimages.cpp \
pdftoolfetchtext.cpp \
pdftoolinfo.cpp \
@@ -80,6 +81,7 @@ HEADERS += \
pdftoolcertstore.h \
pdftoolcolorprofiles.h \
pdftooldecrypt.h \
pdftoolencrypt.h \
pdftoolfetchimages.h \
pdftoolfetchtext.h \
pdftoolinfo.h \

View File

@@ -324,6 +324,15 @@ void PDFToolAbstractApplication::initializeCommandLineParser(QCommandLineParser*
{
parser->addPositionalArgument("certificate", "Certificate file");
}
if (optionFlags.testFlag(Encrypt))
{
parser->addOption(QCommandLineOption("enc-algorithm", "Encryption algorithm (valid values: rc4|aes-128|aes-256).", "encryption algorithm", "aes-256"));
parser->addOption(QCommandLineOption("enc-contents", "Encryption scope (valid values: all|all-except-metadata|only-embedded-files).", "encryption contents", "all"));
parser->addOption(QCommandLineOption("enc-user-password", "User password (for document reading).", "user password"));
parser->addOption(QCommandLineOption("enc-owner-password", "Owner password.", "owner password"));
parser->addOption(QCommandLineOption("enc-permissions", "Document permissions (flags represented as a number).", "permissions"));
}
}
PDFToolOptions PDFToolAbstractApplication::getOptions(QCommandLineParser* parser) const
@@ -896,6 +905,59 @@ PDFToolOptions PDFToolAbstractApplication::getOptions(QCommandLineParser* parser
options.certificateStoreInstallCertificateFile = positionalArguments.isEmpty() ? QString() : positionalArguments.front();
}
if (optionFlags.testFlag(Encrypt))
{
QString encryptionAlgorithm = parser->value("enc-algorithm");
if (encryptionAlgorithm == "rc4")
{
options.encryptionAlgorithm = pdf::PDFSecurityHandlerFactory::Algorithm::RC4;
}
else if (encryptionAlgorithm == "aes-128")
{
options.encryptionAlgorithm = pdf::PDFSecurityHandlerFactory::Algorithm::AES_128;
}
else if (encryptionAlgorithm == "aes-256")
{
options.encryptionAlgorithm = pdf::PDFSecurityHandlerFactory::Algorithm::AES_256;
}
else
{
if (!encryptionAlgorithm.isEmpty())
{
PDFConsole::writeError(PDFToolTranslationContext::tr("Unknown encryption algorithm '%1'. Defaulting to AES-256 encryption.").arg(encryptionAlgorithm), options.outputCodec);
}
options.encryptionAlgorithm = pdf::PDFSecurityHandlerFactory::Algorithm::AES_256;
}
QString encryptionContents = parser->value("enc-contents");
if (encryptionContents == "all")
{
options.encryptionContents = pdf::PDFSecurityHandlerFactory::EncryptContents::All;
}
else if (encryptionContents == "all-except-metadata")
{
options.encryptionContents = pdf::PDFSecurityHandlerFactory::EncryptContents::AllExceptMetadata;
}
else if (encryptionContents == "only-embedded-files")
{
options.encryptionContents = pdf::PDFSecurityHandlerFactory::EncryptContents::EmbeddedFiles;
}
else
{
if (!encryptionContents.isEmpty())
{
PDFConsole::writeError(PDFToolTranslationContext::tr("Unknown encryption contents mode '%1'. Defaulting to encrypt all contents.").arg(encryptionContents), options.outputCodec);
}
options.encryptionContents = pdf::PDFSecurityHandlerFactory::EncryptContents::All;
}
options.encryptionUserPassword = parser->value("enc-user-password");
options.encryptionOwnerPassword = parser->value("enc-owner-password");
options.encryptionPermissions = parser->value("enc-permissions").toUInt();
}
return options;
}

View File

@@ -146,6 +146,13 @@ struct PDFToolOptions
// For option 'CertStoreInstall'
QString certificateStoreInstallCertificateFile;
// For option 'Encrypt'
pdf::PDFSecurityHandlerFactory::Algorithm encryptionAlgorithm = pdf::PDFSecurityHandlerFactory::Algorithm::AES_256;
pdf::PDFSecurityHandlerFactory::EncryptContents encryptionContents = pdf::PDFSecurityHandlerFactory::EncryptContents::All;
QString encryptionUserPassword;
QString encryptionOwnerPassword;
uint32_t encryptionPermissions = 0;
/// Returns page range. If page range is invalid, then \p errorMessage is empty.
/// \param pageCount Page count
/// \param[out] errorMessage Error message
@@ -194,7 +201,8 @@ public:
ErrorPermissions,
ErrorNoText,
ErrorCOM,
ErrorSAPI
ErrorSAPI,
ErrorEncryptionSettings
};
enum StandardString
@@ -229,6 +237,7 @@ public:
Optimize = 0x00100000, ///< Settings for Optimize tool
CertStore = 0x00200000, ///< Settings for certificate store tool
CertStoreInstall = 0x00400000, ///< Settings for certificate store install certificate tool
Encrypt = 0x00800000, ///< Encryption settings
};
Q_DECLARE_FLAGS(Options, Option)

View File

@@ -0,0 +1,98 @@
// Copyright (C) 2021 Jakub Melka
//
// This file is part of Pdf4Qt.
//
// Pdf4Qt is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// with the written consent of the copyright owner, any later version.
//
// Pdf4Qt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Pdf4Qt. If not, see <https://www.gnu.org/licenses/>.
#include "pdftoolencrypt.h"
#include "pdfdocumentbuilder.h"
#include "pdfdocumentwriter.h"
namespace pdftool
{
static PDFToolEncryptApplication s_encryptApplication;
QString PDFToolEncryptApplication::getStandardString(StandardString standardString) const
{
switch (standardString)
{
case Command:
return "encrypt";
case Name:
return PDFToolTranslationContext::tr("Encrypt");
case Description:
return PDFToolTranslationContext::tr("Encrypt the document (with only owner access only).");
default:
Q_ASSERT(false);
break;
}
return QString();
}
int PDFToolEncryptApplication::execute(const PDFToolOptions& options)
{
pdf::PDFSecurityHandlerFactory::SecuritySettings settings;
settings.algorithm = options.encryptionAlgorithm;
settings.encryptContents = options.encryptionContents;
settings.userPassword = options.encryptionUserPassword;
settings.ownerPassword = options.encryptionOwnerPassword;
settings.permissions = options.encryptionPermissions;
QString errorMessage;
if (!pdf::PDFSecurityHandlerFactory::validate(settings, &errorMessage))
{
PDFConsole::writeError(errorMessage, options.outputCodec);
return ErrorEncryptionSettings;
}
pdf::PDFSecurityHandlerPointer securityHandler = pdf::PDFSecurityHandlerFactory::createSecurityHandler(settings);
pdf::PDFDocument document;
QByteArray sourceData;
if (!readDocument(options, document, &sourceData, true))
{
if (readDocument(options, document, &sourceData, false))
{
PDFConsole::writeError(PDFToolTranslationContext::tr("Authorization as owner failed. Encryption change is not permitted if authorized as user only."), options.outputCodec);
}
return ErrorDocumentReading;
}
pdf::PDFDocumentBuilder builder(&document);
builder.setSecurityHandler(qMove(securityHandler));
document = builder.build();
pdf::PDFDocumentWriter writer(nullptr);
pdf::PDFOperationResult result = writer.write(options.document, &document, true);
if (!result)
{
PDFConsole::writeError(result.getErrorMessage(), options.outputCodec);
return ErrorDocumentWriting;
}
return ExitSuccess;
}
PDFToolAbstractApplication::Options PDFToolEncryptApplication::getOptionsFlags() const
{
return ConsoleFormat | OpenDocument | Encrypt;
}
} // namespace pdftool

36
PdfTool/pdftoolencrypt.h Normal file
View File

@@ -0,0 +1,36 @@
// Copyright (C) 2021 Jakub Melka
//
// This file is part of Pdf4Qt.
//
// Pdf4Qt is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// with the written consent of the copyright owner, any later version.
//
// Pdf4Qt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Pdf4Qt. If not, see <https://www.gnu.org/licenses/>.
#ifndef PDFTOOLENCRYPT_H
#define PDFTOOLENCRYPT_H
#include "pdftoolabstractapplication.h"
namespace pdftool
{
class PDFToolEncryptApplication : public PDFToolAbstractApplication
{
public:
virtual QString getStandardString(StandardString standardString) const override;
virtual int execute(const PDFToolOptions& options) override;
virtual Options getOptionsFlags() const override;
};
} // namespace pdftool
#endif // PDFTOOLENCRYPT_H