vcl/inc/pdf/PDFEncryptor.hxx | 42 ++++++++++++++++++++++++++++--------- vcl/inc/pdf/pdfwriter_impl.hxx | 2 - vcl/source/gdi/pdfwriter_impl.cxx | 30 ++++++++++++++------------ vcl/source/gdi/pdfwriter_impl2.cxx | 14 ++++++------ 4 files changed, 57 insertions(+), 31 deletions(-)
New commits: commit c969795babd116635f87184facdc37f4f952d838 Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> AuthorDate: Fri Nov 8 11:50:14 2024 +0100 Commit: Tomaž Vajngerl <qui...@gmail.com> CommitDate: Mon Nov 25 08:25:53 2024 +0100 pdf: Introduce a IPDFEncryptor and use it in PDFWriterImpl The idea of IPDFEncryptor is to be the interface to encrypt the stream/string in the same way, irregardless which PDF encryption version/revision we are using. Change-Id: Ie7835384f1be5a44c53985b01c8187323400aa0e Reviewed-on: https://gerrit.libreoffice.org/c/core/+/176890 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <qui...@gmail.com> diff --git a/vcl/inc/pdf/PDFEncryptor.hxx b/vcl/inc/pdf/PDFEncryptor.hxx index a5fc3a17c054..3547252ceb5e 100644 --- a/vcl/inc/pdf/PDFEncryptor.hxx +++ b/vcl/inc/pdf/PDFEncryptor.hxx @@ -33,7 +33,29 @@ namespace vcl::pdf { class EncryptionHashTransporter; -class PDFEncryptor +class IPDFEncryptor +{ +public: + virtual ~IPDFEncryptor() {} + virtual sal_Int32 getAccessPermissions() = 0; + virtual sal_Int32 getKeyLength() = 0; + virtual bool prepareEncryption( + const css::uno::Reference<css::beans::XMaterialHolder>& xEncryptionMaterialHolder, + PDFEncryptionProperties& rProperties) + = 0; + virtual void setupKeysAndCheck(PDFEncryptionProperties& rProperties) = 0; + + virtual void setupEncryption(std::vector<sal_uInt8> const& rEncryptionKey, sal_Int32 nObject) + = 0; + virtual void enableStreamEncryption() = 0; + virtual void disableStreamEncryption() = 0; + virtual bool isStreamEncryptionEnabled() = 0; + virtual void encrypt(const void* pInput, sal_uInt64 nInputSize, sal_uInt8* pOutput, + sal_uInt64 nOutputsSize) + = 0; +}; + +class PDFEncryptor : public IPDFEncryptor { private: /* the numerical value of the access permissions, according to PDF spec, must be signed */ @@ -55,8 +77,8 @@ public: /* used to cipher the stream data and for password management */ rtlCipher m_aCipher = nullptr; - sal_Int32 getAccessPermissions() { return m_nAccessPermissions; } - sal_Int32 getKeyLength() { return m_nKeyLength; } + sal_Int32 getAccessPermissions() override { return m_nAccessPermissions; } + sal_Int32 getKeyLength() override { return m_nKeyLength; } sal_Int32 getRC4KeyLength() { return m_nRC4KeyLength; } static css::uno::Reference<css::beans::XMaterialHolder> @@ -64,18 +86,18 @@ public: virtual bool prepareEncryption( const css::uno::Reference<css::beans::XMaterialHolder>& xEncryptionMaterialHolder, - PDFEncryptionProperties& rProperties); + PDFEncryptionProperties& rProperties) override; - void setupKeysAndCheck(PDFEncryptionProperties& rProperties); + void setupKeysAndCheck(PDFEncryptionProperties& rProperties) override; - void setupEncryption(std::vector<sal_uInt8> const& rEncryptionKey, sal_Int32 nObject); - void enableStreamEncryption(); - void disableStreamEncryption(); + void setupEncryption(std::vector<sal_uInt8> const& rEncryptionKey, sal_Int32 nObject) override; + void enableStreamEncryption() override; + void disableStreamEncryption() override; - bool isStreamEncryptionEnabled() { return m_bEncryptThisStream; } + bool isStreamEncryptionEnabled() override { return m_bEncryptThisStream; } void encrypt(const void* pInput, sal_uInt64 nInputSize, sal_uInt8* pOutput, - sal_uInt64 nOutputsSize); + sal_uInt64 nOutputsSize) override; }; } diff --git a/vcl/inc/pdf/pdfwriter_impl.hxx b/vcl/inc/pdf/pdfwriter_impl.hxx index 88267ed4b9f4..e7644d7c4884 100644 --- a/vcl/inc/pdf/pdfwriter_impl.hxx +++ b/vcl/inc/pdf/pdfwriter_impl.hxx @@ -811,7 +811,7 @@ private: ExternalPDFStreams m_aExternalPDFStreams; - PDFEncryptor m_aPDFEncryptor; + std::unique_ptr<IPDFEncryptor> m_pPDFEncryptor; /* output redirection; e.g. to accumulate content streams for XObjects diff --git a/vcl/source/gdi/pdfwriter_impl.cxx b/vcl/source/gdi/pdfwriter_impl.cxx index 2641256b30c4..b282827ee4c3 100644 --- a/vcl/source/gdi/pdfwriter_impl.cxx +++ b/vcl/source/gdi/pdfwriter_impl.cxx @@ -1453,11 +1453,14 @@ PDFWriterImpl::PDFWriterImpl( const PDFWriter::PDFWriterContext& rContext, setupDocInfo(); if (xEncryptionMaterialHolder.is()) - m_aPDFEncryptor.prepareEncryption(xEncryptionMaterialHolder, m_aContext.Encryption); + { + m_pPDFEncryptor.reset(new PDFEncryptor); + m_pPDFEncryptor->prepareEncryption(xEncryptionMaterialHolder, m_aContext.Encryption); + } if (m_aContext.Encryption.Encrypt()) { - m_aPDFEncryptor.setupKeysAndCheck(m_aContext.Encryption); + m_pPDFEncryptor->setupKeysAndCheck(m_aContext.Encryption); } // write header @@ -1591,7 +1594,7 @@ append the string as unicode hex, encrypted if needed inline void PDFWriterImpl::appendUnicodeTextStringEncrypt( const OUString& rInString, const sal_Int32 nInObjectNumber, OStringBuffer& rOutBuffer ) { rOutBuffer.append( "<" ); - if( m_aContext.Encryption.Encrypt() ) + if (m_aContext.Encryption.Encrypt()) { const sal_Unicode* pStr = rInString.getStr(); sal_Int32 nLen = rInString.getLength(); @@ -1610,7 +1613,7 @@ inline void PDFWriterImpl::appendUnicodeTextStringEncrypt( const OUString& rInSt *pCopy++ = static_cast<sal_uInt8>( aUnChar & 255 ); } //encrypt in place - rtl_cipher_encodeARCFOUR( m_aPDFEncryptor.m_aCipher, m_vEncryptionBuffer.data(), nChars, m_vEncryptionBuffer.data(), nChars); + m_pPDFEncryptor->encrypt(m_vEncryptionBuffer.data(), nChars, m_vEncryptionBuffer.data(), nChars); //now append, hexadecimal (appendHex), the encrypted result for(int i = 0; i < nChars; i++) appendHex( m_vEncryptionBuffer[i], rOutBuffer ); @@ -1625,12 +1628,12 @@ inline void PDFWriterImpl::appendLiteralStringEncrypt( std::string_view rInStrin rOutBuffer.append( "(" ); sal_Int32 nChars = rInString.size(); //check for encryption, if ok, encrypt the string, then convert with appndLiteralString - if( m_aContext.Encryption.Encrypt() ) + if (m_aContext.Encryption.Encrypt()) { m_vEncryptionBuffer.resize(nChars); //encrypt the string in a buffer, then append it - enableStringEncryption( nInObjectNumber ); - rtl_cipher_encodeARCFOUR(m_aPDFEncryptor.m_aCipher, rInString.data(), nChars, m_vEncryptionBuffer.data(), nChars); + enableStringEncryption(nInObjectNumber); + m_pPDFEncryptor->encrypt(rInString.data(), nChars, m_vEncryptionBuffer.data(), nChars); appendLiteralString( reinterpret_cast<char*>(m_vEncryptionBuffer.data()), nChars, rOutBuffer ); } else @@ -1737,13 +1740,14 @@ bool PDFWriterImpl::writeBufferBytes( const void* pBuffer, sal_uInt64 nBytes ) } else { - if (m_aPDFEncryptor.isStreamEncryptionEnabled()) + bool bStreamEncryption = m_pPDFEncryptor && m_pPDFEncryptor->isStreamEncryptionEnabled(); + if (bStreamEncryption) { m_vEncryptionBuffer.resize(nBytes); - m_aPDFEncryptor.encrypt(pBuffer, nBytes, m_vEncryptionBuffer.data(), nBytes); + m_pPDFEncryptor->encrypt(pBuffer, nBytes, m_vEncryptionBuffer.data(), nBytes); } - const void* pWriteBuffer = m_aPDFEncryptor.isStreamEncryptionEnabled() ? m_vEncryptionBuffer.data() : pBuffer; + const void* pWriteBuffer = bStreamEncryption ? m_vEncryptionBuffer.data() : pBuffer; m_DocDigest.update(static_cast<unsigned char const*>(pWriteBuffer), static_cast<sal_uInt32>(nBytes)); if (m_aFile.write(pWriteBuffer, nBytes, nWritten) != osl::File::E_None) @@ -6149,7 +6153,7 @@ sal_Int32 PDFWriterImpl::emitEncrypt() // emit the owner password, must not be encrypted aWriter.writeString("/O", reinterpret_cast<char*>(m_aContext.Encryption.OValue.data()), sal_Int32(m_aContext.Encryption.OValue.size())); aWriter.writeString("/U", reinterpret_cast<char*>(m_aContext.Encryption.UValue.data()), sal_Int32(m_aContext.Encryption.UValue.size())); - aWriter.write("/P", m_aPDFEncryptor.getAccessPermissions()); + aWriter.write("/P", m_pPDFEncryptor->getAccessPermissions()); aWriter.endDict(); aWriter.endObject(); @@ -9768,7 +9772,7 @@ bool PDFWriterImpl::writeBitmapObject( const BitmapEmit& rObject, bool bMask ) aLine.append( " <" ); if( m_aContext.Encryption.Encrypt() ) { - enableStringEncryption( rObject.m_nObject ); + enableStringEncryption(rObject.m_nObject); //check encryption buffer size m_vEncryptionBuffer.resize(pAccess->GetPaletteEntryCount()*3); int nChar = 0; @@ -9781,7 +9785,7 @@ bool PDFWriterImpl::writeBitmapObject( const BitmapEmit& rObject, bool bMask ) m_vEncryptionBuffer[nChar++] = rColor.GetBlue(); } //encrypt the colorspace lookup table - rtl_cipher_encodeARCFOUR(m_aPDFEncryptor.m_aCipher, m_vEncryptionBuffer.data(), nChar, m_vEncryptionBuffer.data(), nChar); + m_pPDFEncryptor->encrypt(m_vEncryptionBuffer.data(), nChar, m_vEncryptionBuffer.data(), nChar); //now queue the data for output nChar = 0; for( sal_uInt16 i = 0; i < pAccess->GetPaletteEntryCount(); i++ ) diff --git a/vcl/source/gdi/pdfwriter_impl2.cxx b/vcl/source/gdi/pdfwriter_impl2.cxx index 354397af60df..3efc67a32b61 100644 --- a/vcl/source/gdi/pdfwriter_impl2.cxx +++ b/vcl/source/gdi/pdfwriter_impl2.cxx @@ -1079,25 +1079,25 @@ void PDFWriterImpl::playMetafile( const GDIMetaFile& i_rMtf, vcl::PDFExtOutDevDa void PDFWriterImpl::checkAndEnableStreamEncryption(sal_Int32 nObject) { - if (!m_aContext.Encryption.Encrypt()) + if (!m_aContext.Encryption.Encrypt() || !m_pPDFEncryptor) return; - m_aPDFEncryptor.enableStreamEncryption(); - m_aPDFEncryptor.setupEncryption(m_aContext.Encryption.EncryptionKey, nObject); + m_pPDFEncryptor->enableStreamEncryption(); + m_pPDFEncryptor->setupEncryption(m_aContext.Encryption.EncryptionKey, nObject); } void PDFWriterImpl::disableStreamEncryption() { - m_aPDFEncryptor.disableStreamEncryption(); - + if (m_pPDFEncryptor) + m_pPDFEncryptor->disableStreamEncryption(); } void PDFWriterImpl::enableStringEncryption(sal_Int32 nObject) { - if (!m_aContext.Encryption.Encrypt()) + if (!m_aContext.Encryption.Encrypt() || !m_pPDFEncryptor) return; - m_aPDFEncryptor.setupEncryption(m_aContext.Encryption.EncryptionKey, nObject); + m_pPDFEncryptor->setupEncryption(m_aContext.Encryption.EncryptionKey, nObject); } const tools::Long unsetRun[256] =