From 6e049433e409a7cd0e5213f367947fedf752bd5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Vajngerl?= Date: Mon, 11 Nov 2024 19:14:49 +0100 Subject: [PATCH] pdf: refactor and move encryption init. to a common function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is needed because we have to separate the init. for both encryption methods and we have to init both when the password is entered. Currently we only prepared this, to make this possible when we introduce the other encryption method. Change-Id: Id6556ddc6a6218164a93bb689f03d6ec6dbad8b9 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/176454 Tested-by: Jenkins CollaboraOffice Reviewed-by: Miklos Vajna Reviewed-on: https://gerrit.libreoffice.org/c/core/+/177873 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl --- filter/source/pdf/impdialog.cxx | 3 +- filter/source/pdf/pdfexport.cxx | 3 +- .../vcl/pdf/PDFEncryptionInitialization.hxx | 31 ++++++++++++++++++ vcl/Library_vcl.mk | 1 + vcl/inc/pdf/PDFEncryptor.hxx | 12 +++---- vcl/source/gdi/pdfwriter.cxx | 3 +- .../pdf/PDFEncryptionInitialization.cxx | 32 +++++++++++++++++++ vcl/source/pdf/PDFEncryptor.cxx | 16 +++------- 8 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 include/vcl/pdf/PDFEncryptionInitialization.hxx create mode 100644 vcl/source/pdf/PDFEncryptionInitialization.cxx diff --git a/filter/source/pdf/impdialog.cxx b/filter/source/pdf/impdialog.cxx index ab84bdb95933..8584dd1a698e 100644 --- a/filter/source/pdf/impdialog.cxx +++ b/filter/source/pdf/impdialog.cxx @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -1418,7 +1419,7 @@ IMPL_LINK_NOARG(ImpPDFTabSecurityPage, ClickmaPbSetPwdHdl, weld::Button&, void) mbHaveUserPassword = !aUserPW.isEmpty(); mbHaveOwnerPassword = !aOwnerPW.isEmpty(); - mxPreparedPasswords = vcl::PDFWriter::InitEncryption( aOwnerPW, aUserPW ); + mxPreparedPasswords = vcl::pdf::initEncryption(aOwnerPW, aUserPW); if (!mxPreparedPasswords.is()) { OUString msg; diff --git a/filter/source/pdf/pdfexport.cxx b/filter/source/pdf/pdfexport.cxx index 5956933154c6..cca7703c50c8 100644 --- a/filter/source/pdf/pdfexport.cxx +++ b/filter/source/pdf/pdfexport.cxx @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -931,7 +932,7 @@ bool PDFExport::Export( const OUString& rFile, const Sequence< PropertyValue >& aContext.Encryption.CanCopyOrExtract = bCanCopyOrExtract; aContext.Encryption.CanExtractForAccessibility = bCanExtractForAccessibility; if( bEncrypt && ! xEnc.is() ) - xEnc = vcl::PDFWriter::InitEncryption( aPermissionPassword, aOpenPassword ); + xEnc = vcl::pdf::initEncryption(aPermissionPassword, aOpenPassword); if( bEncrypt && !aPermissionPassword.isEmpty() && ! aPreparedPermissionPassword.hasElements() ) aPreparedPermissionPassword = comphelper::OStorageHelper::CreatePackageEncryptionData( aPermissionPassword ); } diff --git a/include/vcl/pdf/PDFEncryptionInitialization.hxx b/include/vcl/pdf/PDFEncryptionInitialization.hxx new file mode 100644 index 000000000000..93b57476d7e4 --- /dev/null +++ b/include/vcl/pdf/PDFEncryptionInitialization.hxx @@ -0,0 +1,31 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#pragma once + +#include +#include + +namespace com::sun::star::beans +{ +class XMaterialHolder; +} +namespace com::sun::star::uno +{ +template class Reference; +} + +namespace vcl::pdf +{ +VCL_DLLPUBLIC css::uno::Reference +initEncryption(const OUString& i_rOwnerPassword, const OUString& i_rUserPassword); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/Library_vcl.mk b/vcl/Library_vcl.mk index 420024cc2ab2..c410b94fddd5 100644 --- a/vcl/Library_vcl.mk +++ b/vcl/Library_vcl.mk @@ -511,6 +511,7 @@ $(eval $(call gb_Library_add_exception_objects,vcl,\ vcl/source/pdf/EncryptionHashTransporter \ vcl/source/pdf/ExternalPDFStreams \ vcl/source/pdf/PDFiumTools \ + vcl/source/pdf/PDFEncryptionInitialization \ vcl/source/pdf/PDFEncryptor \ vcl/source/pdf/PDFEncryptorR6 \ vcl/source/pdf/PdfConfig \ diff --git a/vcl/inc/pdf/PDFEncryptor.hxx b/vcl/inc/pdf/PDFEncryptor.hxx index be4b99f9b7d6..e4134605c194 100644 --- a/vcl/inc/pdf/PDFEncryptor.hxx +++ b/vcl/inc/pdf/PDFEncryptor.hxx @@ -20,10 +20,6 @@ namespace vcl struct PDFEncryptionProperties; } -namespace com::sun::star::beans -{ -class XMaterialHolder; -} namespace com::sun::star::uno { template class Reference; @@ -31,6 +27,8 @@ template class Reference; namespace vcl::pdf { +class EncryptionHashTransporter; + class PDFEncryptor : public IPDFEncryptor { private: @@ -60,10 +58,10 @@ public: sal_Int32 getKeyLength() override { return m_nKeyLength; } sal_Int32 getRC4KeyLength() { return m_nRC4KeyLength; } - static css::uno::Reference - initEncryption(const OUString& i_rOwnerPassword, const OUString& i_rUserPassword); + static void initEncryption(EncryptionHashTransporter& rEncryptionHashTransporter, + const OUString& i_rOwnerPassword, const OUString& i_rUserPassword); - virtual bool prepareEncryption( + bool prepareEncryption( const css::uno::Reference& xEncryptionMaterialHolder, PDFEncryptionProperties& rProperties) override; diff --git a/vcl/source/gdi/pdfwriter.cxx b/vcl/source/gdi/pdfwriter.cxx index ee762e2fca47..ca91054c53de 100644 --- a/vcl/source/gdi/pdfwriter.cxx +++ b/vcl/source/gdi/pdfwriter.cxx @@ -20,6 +20,7 @@ #include #include +#include using namespace vcl; @@ -471,7 +472,7 @@ std::set< PDFWriter::ErrorCode > const & PDFWriter::GetErrors() const css::uno::Reference< css::beans::XMaterialHolder > PDFWriter::InitEncryption(const OUString& i_rOwnerPassword, const OUString& i_rUserPassword) { - return PDFEncryptor::initEncryption(i_rOwnerPassword, i_rUserPassword); + return vcl::pdf::initEncryption(i_rOwnerPassword, i_rUserPassword); } void PDFWriter::PlayMetafile( const GDIMetaFile& i_rMTF, const vcl::PDFWriter::PlayMetafileContext& i_rPlayContext, PDFExtOutDevData* i_pData ) diff --git a/vcl/source/pdf/PDFEncryptionInitialization.cxx b/vcl/source/pdf/PDFEncryptionInitialization.cxx new file mode 100644 index 000000000000..df06968fc290 --- /dev/null +++ b/vcl/source/pdf/PDFEncryptionInitialization.cxx @@ -0,0 +1,32 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#include +#include +#include +#include +#include +#include + +using namespace css; + +namespace vcl::pdf +{ +css::uno::Reference initEncryption(const OUString& i_rOwnerPassword, + const OUString& i_rUserPassword) +{ + rtl::Reference pTransporter = new EncryptionHashTransporter; + PDFEncryptor::initEncryption(*pTransporter, i_rOwnerPassword, i_rUserPassword); + return pTransporter; +} + +} // end vcl::pdf + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/pdf/PDFEncryptor.cxx b/vcl/source/pdf/PDFEncryptor.cxx index d97e671247de..183e66e8e713 100644 --- a/vcl/source/pdf/PDFEncryptor.cxx +++ b/vcl/source/pdf/PDFEncryptor.cxx @@ -327,32 +327,26 @@ PDFEncryptor::~PDFEncryptor() { rtl_cipher_destroyARCFOUR(m_aCipher); } 1. init the document id, used both for building the document id and for building the encryption key(s) 2. build the encryption key following algorithms described in the PDF specification */ -uno::Reference -PDFEncryptor::initEncryption(const OUString& i_rOwnerPassword, const OUString& i_rUserPassword) +void PDFEncryptor::initEncryption(EncryptionHashTransporter& rEncryptionHashTransporter, + const OUString& i_rOwnerPassword, const OUString& i_rUserPassword) { - uno::Reference xResult; if (!i_rOwnerPassword.isEmpty() || !i_rUserPassword.isEmpty()) { - rtl::Reference pTransporter = new EncryptionHashTransporter; - xResult = pTransporter; - // get padded passwords sal_uInt8 aPadUPW[ENCRYPTED_PWD_SIZE], aPadOPW[ENCRYPTED_PWD_SIZE]; padPassword(i_rOwnerPassword.isEmpty() ? i_rUserPassword : i_rOwnerPassword, aPadOPW); padPassword(i_rUserPassword, aPadUPW); - if (computeODictionaryValue(aPadOPW, aPadUPW, pTransporter->getOValue(), SECUR_128BIT_KEY)) + if (computeODictionaryValue(aPadOPW, aPadUPW, rEncryptionHashTransporter.getOValue(), + SECUR_128BIT_KEY)) { - pTransporter->getUDigest()->update(aPadUPW, ENCRYPTED_PWD_SIZE); + rEncryptionHashTransporter.getUDigest()->update(aPadUPW, ENCRYPTED_PWD_SIZE); } - else - xResult.clear(); // trash temporary padded cleartext PWDs rtl_secureZeroMemory(aPadOPW, sizeof(aPadOPW)); rtl_secureZeroMemory(aPadUPW, sizeof(aPadUPW)); } - return xResult; } bool PDFEncryptor::prepareEncryption(