vcl/Library_vcl.mk | 11 ++- vcl/inc/pdf/EncryptionHashTransporter.hxx | 56 +++++++++++++++++++ vcl/inc/pdf/pdfwriter_impl.hxx | 6 +- vcl/source/gdi/pdfwriter_impl2.cxx | 78 ++------------------------- vcl/source/pdf/EncryptionHashTransporter.cxx | 55 +++++++++++++++++++ 5 files changed, 126 insertions(+), 80 deletions(-)
New commits: commit ee0d5cad83df9bb96306fe21f77aac5c512017a3 Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> AuthorDate: Mon Nov 4 15:37:56 2024 +0100 Commit: Miklos Vajna <vmik...@collabora.com> CommitDate: Mon Nov 11 14:36:32 2024 +0100 pdf: move EncHashTransporter into own files Also rename to EncryptionHashTransporter. Change-Id: I20f984af4428e1182c77dbce4343d69c106063a4 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/176016 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com> Reviewed-by: Miklos Vajna <vmik...@collabora.com> diff --git a/vcl/Library_vcl.mk b/vcl/Library_vcl.mk index fb9687dc47c0..86efdce3bd8b 100644 --- a/vcl/Library_vcl.mk +++ b/vcl/Library_vcl.mk @@ -309,11 +309,6 @@ $(eval $(call gb_Library_add_exception_objects,vcl,\ vcl/source/gdi/scrptrun \ vcl/source/gdi/CommonSalLayout \ vcl/source/gdi/TypeSerializer \ - vcl/source/pdf/PdfConfig \ - vcl/source/pdf/ResourceDict \ - vcl/source/pdf/Matrix3 \ - vcl/source/pdf/XmpMetadata \ - vcl/source/pdf/ExternalPDFStreams \ vcl/source/graphic/BinaryDataContainer \ vcl/source/graphic/BinaryDataContainerTools \ vcl/source/graphic/GraphicID \ @@ -499,7 +494,13 @@ $(eval $(call gb_Library_add_exception_objects,vcl,\ vcl/source/fontsubset/sft \ vcl/source/fontsubset/ttcr \ vcl/source/fontsubset/xlat \ + vcl/source/pdf/EncryptionHashTransporter \ + vcl/source/pdf/ExternalPDFStreams \ vcl/source/pdf/PDFiumTools \ + vcl/source/pdf/PdfConfig \ + vcl/source/pdf/ResourceDict \ + vcl/source/pdf/Matrix3 \ + vcl/source/pdf/XmpMetadata \ vcl/source/uitest/logger \ vcl/source/uitest/uiobject \ vcl/source/uitest/uitest \ diff --git a/vcl/inc/pdf/EncryptionHashTransporter.hxx b/vcl/inc/pdf/EncryptionHashTransporter.hxx new file mode 100644 index 000000000000..0da0db3d8654 --- /dev/null +++ b/vcl/inc/pdf/EncryptionHashTransporter.hxx @@ -0,0 +1,56 @@ +/* -*- 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 <com/sun/star/beans/XMaterialHolder.hpp> +#include <comphelper/hash.hxx> +#include <cppuhelper/implbase.hxx> +#include <sal/log.hxx> + +#include <map> + +namespace vcl::pdf +{ +/* a crutch to transport a ::comphelper::Hash safely though UNO API + this is needed for the PDF export dialog, which otherwise would have to pass + clear text passwords down till they can be used in PDFWriter. Unfortunately + the MD5 sum of the password (which is needed to create the PDF encryption key) + is not sufficient, since an MD5 digest cannot be created in an arbitrary state + which would be needed in PDFWriterImpl::computeEncryptionKey. +*/ +class EncryptionHashTransporter : public cppu::WeakImplHelper<css::beans::XMaterialHolder> +{ + ::std::unique_ptr<::comphelper::Hash> m_pDigest; + sal_IntPtr maID; + std::vector<sal_uInt8> maOValue; + + static std::map<sal_IntPtr, EncryptionHashTransporter*> sTransporters; + +public: + EncryptionHashTransporter(); + + virtual ~EncryptionHashTransporter() override; + + comphelper::Hash* getUDigest() { return m_pDigest.get(); }; + + std::vector<sal_uInt8>& getOValue() { return maOValue; } + + void invalidate() { m_pDigest.reset(); } + + // XMaterialHolder + virtual css::uno::Any SAL_CALL getMaterial() override { return css::uno::Any(sal_Int64(maID)); } + + static EncryptionHashTransporter* + getEncHashTransporter(const css::uno::Reference<css::beans::XMaterialHolder>& xReference); +}; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/inc/pdf/pdfwriter_impl.hxx b/vcl/inc/pdf/pdfwriter_impl.hxx index 0ec6e2f92691..d01db2fd192b 100644 --- a/vcl/inc/pdf/pdfwriter_impl.hxx +++ b/vcl/inc/pdf/pdfwriter_impl.hxx @@ -65,9 +65,9 @@ class FontSubsetInfo; class ZCodec; -class EncHashTransporter; struct BitStreamState; namespace vcl::font { class PhysicalFontFace; } +namespace vcl::pdf { class EncryptionHashTransporter; } class SvStream; class SvMemoryStream; @@ -1083,7 +1083,7 @@ private: pad a password according algorithm 3.2, step 1 */ static void padPassword( std::u16string_view i_rPassword, sal_uInt8* o_pPaddedPW ); /* algorithm 3.2: compute an encryption key */ - static bool computeEncryptionKey( EncHashTransporter*, + static bool computeEncryptionKey( vcl::pdf::EncryptionHashTransporter*, vcl::PDFWriter::PDFEncryptionProperties& io_rProperties, sal_Int32 i_nAccessPermissions ); @@ -1093,7 +1093,7 @@ private: sal_Int32 i_nKeyLength ); /* algorithm 3.4 or 3.5: computing the encryption dictionary's user password value ( /U ) revision 2 or 3 of the standard security handler */ - static bool computeUDictionaryValue( EncHashTransporter* i_pTransporter, + static bool computeUDictionaryValue( vcl::pdf::EncryptionHashTransporter* i_pTransporter, vcl::PDFWriter::PDFEncryptionProperties& io_rProperties, sal_Int32 i_nKeyLength, sal_Int32 i_nAccessPermissions diff --git a/vcl/source/gdi/pdfwriter_impl2.cxx b/vcl/source/gdi/pdfwriter_impl2.cxx index 41ae9f12bfaa..25da8b285a9e 100644 --- a/vcl/source/gdi/pdfwriter_impl2.cxx +++ b/vcl/source/gdi/pdfwriter_impl2.cxx @@ -18,6 +18,7 @@ */ #include <pdf/pdfwriter_impl.hxx> +#include <pdf/EncryptionHashTransporter.hxx> #include <vcl/pdfextoutdevdata.hxx> #include <vcl/virdev.hxx> @@ -33,7 +34,6 @@ #include <tools/stream.hxx> #include <comphelper/fileformat.h> -#include <comphelper/hash.hxx> #include <comphelper/processfactory.hxx> #include <comphelper/propertyvalue.hxx> @@ -43,7 +43,7 @@ #include <com/sun/star/graphic/XGraphicProvider.hpp> #include <com/sun/star/beans/XMaterialHolder.hpp> -#include <cppuhelper/implbase.hxx> + #include <o3tl/unit_conversion.hxx> #include <vcl/skia/SkiaHelper.hxx> @@ -1080,72 +1080,6 @@ void PDFWriterImpl::playMetafile( const GDIMetaFile& i_rMtf, vcl::PDFExtOutDevDa // Encryption methods -/* a crutch to transport a ::comphelper::Hash safely though UNO API - this is needed for the PDF export dialog, which otherwise would have to pass - clear text passwords down till they can be used in PDFWriter. Unfortunately - the MD5 sum of the password (which is needed to create the PDF encryption key) - is not sufficient, since an MD5 digest cannot be created in an arbitrary state - which would be needed in PDFWriterImpl::computeEncryptionKey. -*/ -class EncHashTransporter : public cppu::WeakImplHelper < css::beans::XMaterialHolder > -{ - ::std::unique_ptr<::comphelper::Hash> m_pDigest; - sal_IntPtr maID; - std::vector< sal_uInt8 > maOValue; - - static std::map< sal_IntPtr, EncHashTransporter* > sTransporters; -public: - EncHashTransporter() - : m_pDigest(new ::comphelper::Hash(::comphelper::HashType::MD5)) - { - maID = reinterpret_cast< sal_IntPtr >(this); - while( sTransporters.find( maID ) != sTransporters.end() ) // paranoia mode - maID++; - sTransporters[ maID ] = this; - } - - virtual ~EncHashTransporter() override - { - sTransporters.erase( maID ); - SAL_INFO( "vcl", "EncHashTransporter freed" ); - } - - ::comphelper::Hash* getUDigest() { return m_pDigest.get(); }; - std::vector< sal_uInt8 >& getOValue() { return maOValue; } - void invalidate() - { - m_pDigest.reset(); - } - - // XMaterialHolder - virtual uno::Any SAL_CALL getMaterial() override - { - return uno::Any( sal_Int64(maID) ); - } - - static EncHashTransporter* getEncHashTransporter( const uno::Reference< beans::XMaterialHolder >& ); - -}; - -std::map< sal_IntPtr, EncHashTransporter* > EncHashTransporter::sTransporters; - -EncHashTransporter* EncHashTransporter::getEncHashTransporter( const uno::Reference< beans::XMaterialHolder >& xRef ) -{ - EncHashTransporter* pResult = nullptr; - if( xRef.is() ) - { - uno::Any aMat( xRef->getMaterial() ); - sal_Int64 nMat = 0; - if( aMat >>= nMat ) - { - std::map< sal_IntPtr, EncHashTransporter* >::iterator it = sTransporters.find( static_cast<sal_IntPtr>(nMat) ); - if( it != sTransporters.end() ) - pResult = it->second; - } - } - return pResult; -} - void PDFWriterImpl::checkAndEnableStreamEncryption( sal_Int32 nObject ) { if( !m_aContext.Encryption.Encrypt() ) @@ -1196,7 +1130,7 @@ uno::Reference< beans::XMaterialHolder > PDFWriterImpl::initEncryption( const OU uno::Reference< beans::XMaterialHolder > xResult; if( !i_rOwnerPassword.isEmpty() || !i_rUserPassword.isEmpty() ) { - rtl::Reference<EncHashTransporter> pTransporter = new EncHashTransporter; + rtl::Reference<EncryptionHashTransporter> pTransporter = new EncryptionHashTransporter; xResult = pTransporter; // get padded passwords @@ -1221,7 +1155,7 @@ uno::Reference< beans::XMaterialHolder > PDFWriterImpl::initEncryption( const OU bool PDFWriterImpl::prepareEncryption( const uno::Reference< beans::XMaterialHolder >& xEnc ) { bool bSuccess = false; - EncHashTransporter* pTransporter = EncHashTransporter::getEncHashTransporter( xEnc ); + EncryptionHashTransporter* pTransporter = EncryptionHashTransporter::getEncHashTransporter( xEnc ); if( pTransporter ) { sal_Int32 nKeyLength = 0, nRC4KeyLength = 0; @@ -1298,7 +1232,7 @@ it will be 16 byte long for 128 bit security; for 40 bit security only the first TODO: in pdf ver 1.5 and 1.6 the step 6 is different, should be implemented. See spec. */ -bool PDFWriterImpl::computeEncryptionKey( EncHashTransporter* i_pTransporter, vcl::PDFWriter::PDFEncryptionProperties& io_rProperties, sal_Int32 i_nAccessPermissions ) +bool PDFWriterImpl::computeEncryptionKey( EncryptionHashTransporter* i_pTransporter, vcl::PDFWriter::PDFEncryptionProperties& io_rProperties, sal_Int32 i_nAccessPermissions ) { bool bSuccess = true; ::std::vector<unsigned char> nMD5Sum; @@ -1434,7 +1368,7 @@ bool PDFWriterImpl::computeODictionaryValue( const sal_uInt8* i_pPaddedOwnerPass /********************************** Algorithms 3.4 and 3.5 Compute the encryption dictionary /U value, save into the class data member, revision 2 (40 bit) or 3 (128 bit) */ -bool PDFWriterImpl::computeUDictionaryValue( EncHashTransporter* i_pTransporter, +bool PDFWriterImpl::computeUDictionaryValue( EncryptionHashTransporter* i_pTransporter, vcl::PDFWriter::PDFEncryptionProperties& io_rProperties, sal_Int32 i_nKeyLength, sal_Int32 i_nAccessPermissions diff --git a/vcl/source/pdf/EncryptionHashTransporter.cxx b/vcl/source/pdf/EncryptionHashTransporter.cxx new file mode 100644 index 000000000000..6ecd43396c3a --- /dev/null +++ b/vcl/source/pdf/EncryptionHashTransporter.cxx @@ -0,0 +1,55 @@ +/* -*- 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 <pdf/EncryptionHashTransporter.hxx> + +using namespace css; + +namespace vcl::pdf +{ +EncryptionHashTransporter::EncryptionHashTransporter() + : m_pDigest(new ::comphelper::Hash(::comphelper::HashType::MD5)) +{ + maID = reinterpret_cast<sal_IntPtr>(this); + while (sTransporters.find(maID) != sTransporters.end()) // paranoia mode + maID++; + sTransporters[maID] = this; +} + +EncryptionHashTransporter::~EncryptionHashTransporter() +{ + sTransporters.erase(maID); + SAL_INFO("vcl", "EncryptionHashTransporter freed"); +} + +EncryptionHashTransporter* EncryptionHashTransporter::getEncHashTransporter( + const uno::Reference<beans::XMaterialHolder>& xReference) +{ + EncryptionHashTransporter* pResult = nullptr; + if (xReference.is()) + { + uno::Any aMat(xReference->getMaterial()); + sal_Int64 nMat = 0; + if (aMat >>= nMat) + { + std::map<sal_IntPtr, EncryptionHashTransporter*>::iterator it + = sTransporters.find(static_cast<sal_IntPtr>(nMat)); + if (it != sTransporters.end()) + pResult = it->second; + } + } + return pResult; +} + +std::map<sal_IntPtr, EncryptionHashTransporter*> EncryptionHashTransporter::sTransporters; + +} // end vcl::pdf + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */