[Libreoffice-commits] core.git: Branch 'feature/cib_contract3756' - 74 commits - basic/inc basic/source bin/check-elf-dynamic-objects bin/find-can-be-private-symbols.py bin/gla11y chart2/source comphelper/source compilerplugins/clang configure.ac connectivity/source cppcanvas/source cui/source cui/uiconfig dbaccess/source desktop/source editeng/source extensions/source external/coinmp filter/source formula/source fpicker/source framework/inc framework/Library_fwk.mk framework/source .gitreview helpcompiler/inc helpcontent2 hwpfilter/source i18npool/source include/basegfx include/basic include/editeng include/filter include/oox include/opencl include/sfx2 include/sot include/svtools include/svx include/toolkit include/tools include/vcl include/xmloff io/source l10ntools/source lotuswordpro/source offapi/com offapi/UnoApi_offapi.mk officecfg/registry oox/inc oox/source oox/util package/source README.md reportdesign/source scaddins/source sc/inc sc/source sdext/source sd/inc sd/source sfx2/source sf x2/uiconfig solenv/CompilerTest_compilerplugins_clang.mk sot/source starmath/source svl/source svx/inc svx/source sw/inc sw/Library_sw.mk sw/qa sw/sdi sw/source sw/uiconfig sw/UIConfig_swriter.mk toolkit/source tools/source translations ucb/source unotools/source vbahelper/source vcl/inc vcl/opengl vcl/osx vcl/source vcl/unx writerfilter/source xmloff/source xmlsecurity/inc xmlsecurity/source

Vasily Melenchuk (via logerrit) logerrit at kemper.freedesktop.org
Mon Nov 4 00:29:25 UTC 2019


Rebased ref, commits from common ancestor:
commit b67b834b132de6a58972714ac0f7567a10d79c82
Author:     Vasily Melenchuk <vasily.melenchuk at cib.de>
AuthorDate: Thu Oct 17 11:09:21 2019 +0300
Commit:     Thorsten Behrens <Thorsten.Behrens at CIB.de>
CommitDate: Mon Nov 4 01:27:10 2019 +0100

    oox: fixed build error, more consts
    
    Change-Id: I06b9885ca304c30d6160f43558b309539d4202c0

diff --git a/include/oox/crypto/DocumentEncryption.hxx b/include/oox/crypto/DocumentEncryption.hxx
index d33450a3b791..c99cadc972f4 100644
--- a/include/oox/crypto/DocumentEncryption.hxx
+++ b/include/oox/crypto/DocumentEncryption.hxx
@@ -33,14 +33,14 @@ private:
     css::uno::Reference< css::io::XStream > mxDocumentStream;
     oox::ole::OleStorage& mrOleStorage;
     css::uno::Reference< css::packages::XPackageEncryption > mxPackageEncryption;
-    css::uno::Sequence< css::beans::NamedValue >& mMediaEncData;
+    const css::uno::Sequence< css::beans::NamedValue >& mMediaEncData;
     css::uno::Reference< css::uno::XComponentContext > mxContext;
 
 public:
     DocumentEncryption(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
         css::uno::Reference< css::io::XStream > const & xDocumentStream,
         oox::ole::OleStorage& rOleStorage,
-        css::uno::Sequence< css::beans::NamedValue >& rMediaEncData);
+        const css::uno::Sequence< css::beans::NamedValue >& rMediaEncData);
 
     bool encrypt();
 
diff --git a/oox/source/crypto/DocumentEncryption.cxx b/oox/source/crypto/DocumentEncryption.cxx
index 2f0457911aed..70b7f2a3a4e9 100644
--- a/oox/source/crypto/DocumentEncryption.cxx
+++ b/oox/source/crypto/DocumentEncryption.cxx
@@ -28,7 +28,7 @@ using namespace css::beans;
 DocumentEncryption::DocumentEncryption(const Reference< XComponentContext >& rxContext,
                                        Reference<XStream> const & xDocumentStream,
                                        oox::ole::OleStorage& rOleStorage,
-                                       Sequence<NamedValue>& rMediaEncData)
+                                       const Sequence<NamedValue>& rMediaEncData)
     : mxContext(rxContext)
     , mxDocumentStream(xDocumentStream)
     , mrOleStorage(rOleStorage)
commit 67b2ccfe1deb780047a3ede763fe7de8f58a3189
Author:     Samuel Mehrbrodt <Samuel.Mehrbrodt at cib.de>
AuthorDate: Mon Oct 14 10:37:45 2019 +0200
Commit:     Thorsten Behrens <Thorsten.Behrens at CIB.de>
CommitDate: Mon Nov 4 01:27:10 2019 +0100

    .gitreview: Update default branch
    
    Change-Id: Ie7acdd15ee51f4d1e8d7a5b13ca107d64a360b61
    Reviewed-on: https://gerrit.libreoffice.org/80758
    Reviewed-by: Samuel Mehrbrodt <Samuel.Mehrbrodt at cib.de>
    Tested-by: Samuel Mehrbrodt <Samuel.Mehrbrodt at cib.de>

diff --git a/.gitreview b/.gitreview
index 199752b8b09e..0193cec971ff 100644
--- a/.gitreview
+++ b/.gitreview
@@ -3,5 +3,4 @@ host=gerrit.libreoffice.org
 port=29418
 project=core
 defaultremote=logerrit
-defaultbranch=master
-
+defaultbranch=feature/cib_contract3756
commit 3674d1889f109ab7f2c0bd1b1edee75f42018d60
Author:     Vasily Melenchuk <vasily.melenchuk at cib.de>
AuthorDate: Mon Oct 14 00:01:52 2019 +0300
Commit:     Thorsten Behrens <Thorsten.Behrens at CIB.de>
CommitDate: Mon Nov 4 01:27:10 2019 +0100

    temporary: do not clean up EncryptionData during SaveAs
    
    This clean up can ruin sensetive encryption details saving document
    in plain mode which is not expected.
    
    Relaization is not final and requires more atention.
    
    Change-Id: I46b757af81e68ad4781e83b1a0e0b6da3a5e13e1

diff --git a/package/source/zippackage/ZipPackage.cxx b/package/source/zippackage/ZipPackage.cxx
index 7b2e705e45e5..cf8df0b417cc 100644
--- a/package/source/zippackage/ZipPackage.cxx
+++ b/package/source/zippackage/ZipPackage.cxx
@@ -1752,7 +1752,7 @@ void SAL_CALL ZipPackage::setPropertyValue( const OUString& aPropertyName, const
         if ( !( aValue >>= aKeys ) )
             throw IllegalArgumentException(THROW_WHERE, uno::Reference< uno::XInterface >(), 2 );
 
-        if ( aKeys.hasElements() )
+/*        if ( aKeys.hasElements() )
         {
             bool bHasSHA256 = false;
             bool bHasSHA1 = false;
@@ -1766,7 +1766,7 @@ void SAL_CALL ZipPackage::setPropertyValue( const OUString& aPropertyName, const
 
             if ( !bHasSHA256 && !bHasSHA1 )
                 throw IllegalArgumentException(THROW_WHERE "Expected keys are not provided!", uno::Reference< uno::XInterface >(), 2 );
-        }
+        }*/
 
         m_aStorageEncryptionKeys = aKeys;
         m_aEncryptionKey.realloc( 0 );
diff --git a/sfx2/source/dialog/filedlghelper.cxx b/sfx2/source/dialog/filedlghelper.cxx
index b700abb829ee..4347715a15ab 100644
--- a/sfx2/source/dialog/filedlghelper.cxx
+++ b/sfx2/source/dialog/filedlghelper.cxx
@@ -1415,7 +1415,7 @@ ErrCode FileDialogHelper_Impl::execute( std::vector<OUString>& rpURLList,
         // the password will be set in case user decide so
         rpSet->ClearItem( SID_PASSWORDINTERACTION );
         rpSet->ClearItem( SID_PASSWORD );
-        rpSet->ClearItem( SID_ENCRYPTIONDATA );
+        //rpSet->ClearItem( SID_ENCRYPTIONDATA );
         rpSet->ClearItem( SID_RECOMMENDREADONLY );
         rpSet->ClearItem( SID_MODIFYPASSWORDINFO );
 
diff --git a/sfx2/source/doc/objserv.cxx b/sfx2/source/doc/objserv.cxx
index 24cfa8afd294..9e386d8c5964 100644
--- a/sfx2/source/doc/objserv.cxx
+++ b/sfx2/source/doc/objserv.cxx
@@ -810,10 +810,21 @@ void SfxObjectShell::ExecFile_Impl(SfxRequest &rReq)
 
 
                 bool bPreselectPassword = false;
-                const SfxUnoAnyItem* pOldEncryptionDataItem = SfxItemSet::GetItem<SfxUnoAnyItem>(GetMedium()->GetItemSet(), SID_ENCRYPTIONDATA, false);
                 const SfxStringItem* pOldPasswordItem = SfxItemSet::GetItem<SfxStringItem>(GetMedium()->GetItemSet(), SID_PASSWORD, false);
-                if ( pOldEncryptionDataItem || pOldPasswordItem )
+                if (pOldPasswordItem)
+                {
                     bPreselectPassword = true;
+                }
+                else
+                {
+                    const SfxUnoAnyItem* pOldEncryptionDataItem = SfxItemSet::GetItem<SfxUnoAnyItem>(GetMedium()->GetItemSet(), SID_ENCRYPTIONDATA, false);
+                    if (pOldEncryptionDataItem)
+                    {
+                        uno::Sequence< beans::NamedValue > aEncryptionData;
+                        pOldEncryptionDataItem->GetValue() >>= aEncryptionData;
+
+                    }
+                }
 
                 uno::Sequence< beans::PropertyValue > aDispatchArgs;
                 if ( rReq.GetArgs() )
diff --git a/sfx2/source/doc/objstor.cxx b/sfx2/source/doc/objstor.cxx
index f162bd5dfa92..ae58f9a88e97 100644
--- a/sfx2/source/doc/objstor.cxx
+++ b/sfx2/source/doc/objstor.cxx
@@ -2764,11 +2764,11 @@ bool SfxObjectShell::PreDoSaveAs_Impl(const OUString& rFileName, const OUString&
     std::unique_ptr<SfxAllItemSet> pMergedParams(new SfxAllItemSet( *pMedium->GetItemSet() ));
 
     // in "SaveAs" title and password will be cleared ( maybe the new itemset contains new values, otherwise they will be empty )
-    pMergedParams->ClearItem( SID_ENCRYPTIONDATA );
+    //pMergedParams->ClearItem( SID_ENCRYPTIONDATA );
     pMergedParams->ClearItem( SID_PASSWORD );
     // #i119366# - As the SID_ENCRYPTIONDATA and SID_PASSWORD are using for setting password together, we need to clear them both.
     // Also, ( maybe the new itemset contains new values, otherwise they will be empty )
-    pMergedParams->ClearItem( SID_ENCRYPTIONDATA );
+//    pMergedParams->ClearItem( SID_ENCRYPTIONDATA );
     pMergedParams->ClearItem( SID_DOCINFO_TITLE );
 
     pMergedParams->ClearItem( SID_INPUTSTREAM );
commit 3b5f779d1e955766e6e6de27d8ef10ae83d4e7d9
Author:     Vasily Melenchuk <vasily.melenchuk at cib.de>
AuthorDate: Sun Oct 13 23:56:39 2019 +0300
Commit:     Thorsten Behrens <Thorsten.Behrens at CIB.de>
CommitDate: Mon Nov 4 01:27:10 2019 +0100

    oox: init OLE container if any encryption data is present
    
    Some encryption methods could use other than "OOXPassword"
    values. So we could rely just on "EncryptionData" exists and
    it is not empty
    
    Change-Id: Iece53601282a1bd30b592c998c77185c9dcbbe7c

diff --git a/oox/source/core/xmlfilterbase.cxx b/oox/source/core/xmlfilterbase.cxx
index b6e44b56b416..b52e6a0d9a28 100644
--- a/oox/source/core/xmlfilterbase.cxx
+++ b/oox/source/core/xmlfilterbase.cxx
@@ -882,13 +882,7 @@ Reference<XStream> XmlFilterBase::implGetOutputStream( MediaDescriptor& rMediaDe
                                         MediaDescriptor::PROP_ENCRYPTIONDATA(),
                                         Sequence< NamedValue >() );
 
-    OUString aPassword;
-    auto pProp = std::find_if(aMediaEncData.begin(), aMediaEncData.end(),
-        [](const NamedValue& rProp) { return rProp.Name == "OOXPassword"; });
-    if (pProp != aMediaEncData.end())
-        pProp->Value >>= aPassword;
-
-    if (aPassword.isEmpty())
+    if (aMediaEncData.getLength() == 0)
     {
         return FilterBase::implGetOutputStream( rMediaDescriptor );
     }
commit a375298b9e327eadcddb0ec857b6cff853b21f5c
Author:     Vasily Melenchuk <vasily.melenchuk at cib.de>
AuthorDate: Sun Oct 13 23:53:22 2019 +0300
Commit:     Thorsten Behrens <Thorsten.Behrens at CIB.de>
CommitDate: Mon Nov 4 01:27:09 2019 +0100

    oox: XPackageEncryption interface simplification
    
    instead of two methods to write ecrypted data and encryption info
    just one is used.
    
    Change-Id: Ie31f363a0b76cfe5b67b15c1b98b0e556578b5c8

diff --git a/include/oox/crypto/AgileEngine.hxx b/include/oox/crypto/AgileEngine.hxx
index 1d85a5535b88..d82a2aacf641 100644
--- a/include/oox/crypto/AgileEngine.hxx
+++ b/include/oox/crypto/AgileEngine.hxx
@@ -128,6 +128,9 @@ private:
     void setupEncryptionParameters(AgileEncryptionParameters const & rAgileEncryptionParameters);
     bool setupEncryptionKey(OUString const & rPassword);
 
+    css::uno::Sequence<sal_Int8> writeEncryptionInfo();
+    css::uno::Sequence<sal_Int8> writeEncryptedDocument(const css::uno::Reference<css::io::XInputStream>& rxInputStream);
+
 public:
     AgileEngine(const css::uno::Reference< css::uno::XComponentContext >& rxContext);
 
@@ -143,10 +146,7 @@ public:
 
     // Encryption
 
-    virtual css::uno::Sequence<css::beans::NamedValue> SAL_CALL writeEncryptionInfo() override;
-
-    virtual void SAL_CALL encrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
-                 css::uno::Reference<css::io::XOutputStream>& rxOutputStream) override;
+    virtual css::uno::Sequence<css::beans::NamedValue> SAL_CALL encrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream) override;
 
     virtual sal_Bool SAL_CALL setupEncryption(const css::uno::Sequence<css::beans::NamedValue>& rMediaEncData) override;
 
diff --git a/include/oox/crypto/Standard2007Engine.hxx b/include/oox/crypto/Standard2007Engine.hxx
index a951bfeef26d..41a6591b0f04 100644
--- a/include/oox/crypto/Standard2007Engine.hxx
+++ b/include/oox/crypto/Standard2007Engine.hxx
@@ -37,6 +37,8 @@ class OOX_DLLPUBLIC Standard2007Engine final : public cppu::WeakImplHelper<css::
     bool calculateEncryptionKey(const OUString& rPassword);
 
     css::uno::Reference<css::io::XInputStream> getStream(const css::uno::Sequence<css::beans::NamedValue> & rStreams, const OUString sStreamName);
+    css::uno::Sequence<sal_Int8> writeEncryptionInfo();
+    css::uno::Sequence<sal_Int8> writeEncryptedDocument(const css::uno::Reference<css::io::XInputStream>& rxInputStream);
 
 public:
     Standard2007Engine(const css::uno::Reference<css::uno::XComponentContext>& rxContext);
@@ -53,10 +55,7 @@ public:
 
     // Encryption
 
-    virtual css::uno::Sequence<css::beans::NamedValue> SAL_CALL writeEncryptionInfo() override;
-
-    virtual void SAL_CALL encrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
-                 css::uno::Reference<css::io::XOutputStream>& rxOutputStream) override;
+    virtual css::uno::Sequence<css::beans::NamedValue> SAL_CALL encrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream) override;
 
     virtual sal_Bool SAL_CALL setupEncryption(const css::uno::Sequence<css::beans::NamedValue>& rMediaEncData) override;
 
diff --git a/offapi/com/sun/star/packages/XPackageEncryption.idl b/offapi/com/sun/star/packages/XPackageEncryption.idl
index 298a089af6c7..0fba1d9c3ba7 100644
--- a/offapi/com/sun/star/packages/XPackageEncryption.idl
+++ b/offapi/com/sun/star/packages/XPackageEncryption.idl
@@ -52,11 +52,6 @@ interface XPackageEncryption: com::sun::star::uno::XInterface
     /**
         TODO
      */
-    sequence<com::sun::star::beans::NamedValue> writeEncryptionInfo();
-
-    /**
-        TODO
-     */
     sequence<com::sun::star::beans::NamedValue> createEncryptionData([in] string rPassword);
 
     /**
@@ -67,8 +62,7 @@ interface XPackageEncryption: com::sun::star::uno::XInterface
     /**
         TODO
      */
-    void encrypt([in] com::sun::star::io::XInputStream rxInputStream,
-                 [out] com::sun::star::io::XOutputStream rxOutputStream);
+    sequence<com::sun::star::beans::NamedValue> encrypt([in] com::sun::star::io::XInputStream rxInputStream);
 
     /**
         TODO
diff --git a/oox/source/crypto/AgileEngine.cxx b/oox/source/crypto/AgileEngine.cxx
index 35104903d918..54dd841ef2bf 100644
--- a/oox/source/crypto/AgileEngine.cxx
+++ b/oox/source/crypto/AgileEngine.cxx
@@ -756,7 +756,7 @@ bool AgileEngine::setupEncryptionKey(OUString const & rPassword)
     return true;
 }
 
-css::uno::Sequence<css::beans::NamedValue> AgileEngine::writeEncryptionInfo()
+css::uno::Sequence<sal_Int8> AgileEngine::writeEncryptionInfo()
 {
     Reference<XOutputStream> aEncryptionInfoStream(
         mxContext->getServiceManager()->createInstanceWithContext("com.sun.star.io.SequenceOutputStream", mxContext),
@@ -820,20 +820,19 @@ css::uno::Sequence<css::beans::NamedValue> AgileEngine::writeEncryptionInfo()
     rStream.close();
     aEncryptionInfoStream->flush();
 
-    // Store all streams into sequence and return back
-    comphelper::SequenceAsHashMap aStreams;
-
     Reference<XSequenceOutputStream> aEncryptionInfoSequenceStream(aEncryptionInfoStream, UNO_QUERY);
-    aStreams["EncryptionInfo"] <<= aEncryptionInfoSequenceStream->getWrittenBytes();
-    return aStreams.getAsConstNamedValueList();
+    return aEncryptionInfoSequenceStream->getWrittenBytes();
 }
 
-void AgileEngine::encrypt(const css::uno::Reference<css::io::XInputStream> &  rxInputStream,
-                          css::uno::Reference<css::io::XOutputStream> & rxOutputStream)
+css::uno::Sequence<sal_Int8> AgileEngine::writeEncryptedDocument(const css::uno::Reference<css::io::XInputStream>& rxInputStream)
 {
     CryptoHash aCryptoHash(mInfo.hmacKey, cryptoHashTypeFromString(mInfo.hashAlgorithm));
 
-    BinaryXOutputStream aBinaryOutputStream(rxOutputStream, false);
+    Reference<XOutputStream> aOutputStream(
+        mxContext->getServiceManager()->createInstanceWithContext("com.sun.star.io.SequenceOutputStream", mxContext),
+        UNO_QUERY);
+    BinaryXOutputStream aBinaryOutputStream(aOutputStream, false);
+
     BinaryXInputStream aBinaryInputStream(rxInputStream, false);
     Reference<XSeekable> xSeekable(rxInputStream, UNO_QUERY);
     sal_uInt32 nLength = xSeekable->getLength();
@@ -890,6 +889,18 @@ void AgileEngine::encrypt(const css::uno::Reference<css::io::XInputStream> &  rx
     }
     mInfo.hmacHash = aCryptoHash.finalize();
     encryptHmacValue();
+
+    Reference<XSequenceOutputStream> aSequenceStream(aOutputStream, UNO_QUERY);
+    return aSequenceStream->getWrittenBytes();
+}
+
+
+css::uno::Sequence<css::beans::NamedValue> AgileEngine::encrypt(const css::uno::Reference<css::io::XInputStream> &  rxInputStream)
+{
+    comphelper::SequenceAsHashMap aStreams;
+    aStreams["EncryptedPackage"] <<= writeEncryptedDocument(rxInputStream);
+    aStreams["EncryptionInfo"] <<= writeEncryptionInfo();
+    return aStreams.getAsConstNamedValueList();
 }
 
 } // namespace core
diff --git a/oox/source/crypto/DocumentEncryption.cxx b/oox/source/crypto/DocumentEncryption.cxx
index 2dba0f035df5..2f0457911aed 100644
--- a/oox/source/crypto/DocumentEncryption.cxx
+++ b/oox/source/crypto/DocumentEncryption.cxx
@@ -78,12 +78,7 @@ bool DocumentEncryption::encrypt()
 
     mxPackageEncryption->setupEncryption(mMediaEncData);
 
-    Reference<XOutputStream> xOutputStream(mrOleStorage.openOutputStream("EncryptedPackage"), UNO_SET_THROW);
-    mxPackageEncryption->encrypt(xInputStream, xOutputStream);
-    xOutputStream->flush();
-    xOutputStream->closeOutput();
-
-    Sequence<NamedValue> aStreams = mxPackageEncryption->writeEncryptionInfo();
+    Sequence<NamedValue> aStreams = mxPackageEncryption->encrypt(xInputStream);
 
     for (const NamedValue & aStream : aStreams)
     {
diff --git a/oox/source/crypto/Standard2007Engine.cxx b/oox/source/crypto/Standard2007Engine.cxx
index 50f23e2cf491..9ddde339b1d5 100644
--- a/oox/source/crypto/Standard2007Engine.cxx
+++ b/oox/source/crypto/Standard2007Engine.cxx
@@ -257,7 +257,7 @@ sal_Bool Standard2007Engine::setupEncryption(const css::uno::Sequence<css::beans
     return true;
 }
 
-css::uno::Sequence<css::beans::NamedValue> Standard2007Engine::writeEncryptionInfo()
+css::uno::Sequence<sal_Int8> Standard2007Engine::writeEncryptionInfo()
 {
     Reference<XOutputStream> aEncryptionInfoStream(
         mxContext->getServiceManager()->createInstanceWithContext("com.sun.star.io.SequenceOutputStream", mxContext),
@@ -283,21 +283,17 @@ css::uno::Sequence<css::beans::NamedValue> Standard2007Engine::writeEncryptionIn
     rStream.close();
     aEncryptionInfoStream->flush();
 
-    // Store all streams into sequence and return back
-    comphelper::SequenceAsHashMap aStreams;
-
     Reference<XSequenceOutputStream> aEncryptionInfoSequenceStream(aEncryptionInfoStream, UNO_QUERY);
-    aStreams["EncryptionInfo"] <<= aEncryptionInfoSequenceStream->getWrittenBytes();
-    return aStreams.getAsConstNamedValueList();
+    return aEncryptionInfoSequenceStream->getWrittenBytes();
 }
 
-void Standard2007Engine::encrypt(const css::uno::Reference<css::io::XInputStream> &  rxInputStream,
-                                 css::uno::Reference<css::io::XOutputStream> & rxOutputStream)
+css::uno::Sequence<sal_Int8> Standard2007Engine::writeEncryptedDocument(const css::uno::Reference<css::io::XInputStream> &  rxInputStream)
 {
-    if (mKey.empty())
-        return;
+    Reference<XOutputStream> aOutputStream(
+        mxContext->getServiceManager()->createInstanceWithContext("com.sun.star.io.SequenceOutputStream", mxContext),
+        UNO_QUERY);
+    BinaryXOutputStream aBinaryOutputStream(aOutputStream, false);
 
-    BinaryXOutputStream aBinaryOutputStream(rxOutputStream, false);
     BinaryXInputStream aBinaryInputStream(rxInputStream, false);
     Reference<XSeekable> xSeekable(rxInputStream, UNO_QUERY);
 
@@ -321,6 +317,21 @@ void Standard2007Engine::encrypt(const css::uno::Reference<css::io::XInputStream
         outputLength = aEncryptor.update(outputBuffer, inputBuffer, inputLength);
         aBinaryOutputStream.writeMemory(outputBuffer.data(), outputLength);
     }
+
+    Reference<XSequenceOutputStream> aSequenceStream(aOutputStream, UNO_QUERY);
+    return aSequenceStream->getWrittenBytes();
+}
+
+css::uno::Sequence<css::beans::NamedValue> Standard2007Engine::encrypt(const css::uno::Reference<css::io::XInputStream> &  rxInputStream)
+{
+    if (mKey.empty())
+        return css::uno::Sequence<css::beans::NamedValue>();
+
+    comphelper::SequenceAsHashMap aStreams;
+
+    aStreams["EncryptedPackage"] <<= writeEncryptedDocument(rxInputStream);
+    aStreams["EncryptionInfo"] <<= writeEncryptionInfo();
+    return aStreams.getAsConstNamedValueList();
 }
 
 css::uno::Reference<css::io::XInputStream> Standard2007Engine::getStream(const css::uno::Sequence<css::beans::NamedValue> & rStreams, const OUString sStreamName)
commit 86a85773f17e4034e89fa4272c858de962172f5b
Author:     Vasily Melenchuk <vasily.melenchuk at cib.de>
AuthorDate: Sun Oct 13 21:29:24 2019 +0300
Commit:     Thorsten Behrens <Thorsten.Behrens at CIB.de>
CommitDate: Mon Nov 4 01:27:09 2019 +0100

    oox: avoid control freeze on exception
    
    If exception happens somewhere in exportDocument() or later,
    document controls could be remain locked and later cause crash
    due to missing exception handler.
    
    To avoid this simple lock guard was implemented releasing controls
    even on exception.
    
    Change-Id: I1ce4e487833ddc4b1f1b708f3a7e10bb299ef354

diff --git a/oox/source/core/filterbase.cxx b/oox/source/core/filterbase.cxx
index c99c77ba870f..ca57d3920a0c 100644
--- a/oox/source/core/filterbase.cxx
+++ b/oox/source/core/filterbase.cxx
@@ -110,6 +110,23 @@ DocumentOpenedGuard::~DocumentOpenedGuard()
         rUrlPool.maUrls.erase( maUrl );
 }
 
+class ControllerLockGuard
+{
+public:
+    explicit ControllerLockGuard(const Reference< XModel > & xModel)
+        : mxModel (xModel)
+    {
+        mxModel->lockControllers();
+    }
+
+    ~ControllerLockGuard()
+    {
+        mxModel->unlockControllers();
+    }
+private:
+    const Reference< XModel > & mxModel;
+};
+
 } // namespace
 
 /** Specifies whether this filter is an import or export filter. */
@@ -159,8 +176,6 @@ struct FilterBaseImpl
 
     /// @throws IllegalArgumentException
     void                setDocumentModel( const Reference< XComponent >& rxComponent );
-
-    void                initializeFilter();
 };
 
 FilterBaseImpl::FilterBaseImpl( const Reference< XComponentContext >& rxContext ) :
@@ -185,18 +200,6 @@ void FilterBaseImpl::setDocumentModel( const Reference< XComponent >& rxComponen
     }
 }
 
-void FilterBaseImpl::initializeFilter()
-{
-    try
-    {
-        // lock the model controllers
-        mxModel->lockControllers();
-    }
-    catch( Exception& )
-    {
-    }
-}
-
 FilterBase::FilterBase( const Reference< XComponentContext >& rxContext ) :
     mxImpl( new FilterBaseImpl( rxContext ) )
 {
@@ -472,7 +475,8 @@ sal_Bool SAL_CALL FilterBase::filter( const Sequence< PropertyValue >& rMediaDes
     DocumentOpenedGuard aOpenedGuard( mxImpl->maFileUrl );
     if( aOpenedGuard.isValid() || mxImpl->maFileUrl.isEmpty() )
     {
-        mxImpl->initializeFilter();
+        ControllerLockGuard aCtrlLockGuard(mxImpl->mxModel);
+
         switch( mxImpl->meDirection )
         {
             case FILTERDIRECTION_UNKNOWN:
@@ -492,7 +496,6 @@ sal_Bool SAL_CALL FilterBase::filter( const Sequence< PropertyValue >& rMediaDes
                 }
             break;
         }
-        mxImpl->mxModel->unlockControllers();
     }
     return bRet;
 }
commit 918662b5554bf00eba68b167e136babd067a16ab
Author:     Vasily Melenchuk <vasily.melenchuk at cib.de>
AuthorDate: Thu Oct 10 10:45:44 2019 +0300
Commit:     Thorsten Behrens <Thorsten.Behrens at CIB.de>
CommitDate: Mon Nov 4 01:27:09 2019 +0100

    crypto: predefine classes for later usage in unittests
    
    Change-Id: I2835803eaa2670ca5cae5445049bbc95f303dd08

diff --git a/include/oox/crypto/AgileEngine.hxx b/include/oox/crypto/AgileEngine.hxx
index db2cd082b751..1d85a5535b88 100644
--- a/include/oox/crypto/AgileEngine.hxx
+++ b/include/oox/crypto/AgileEngine.hxx
@@ -24,6 +24,8 @@ namespace oox {
     class BinaryXOutputStream;
 }
 
+namespace com::sun::star::uno { class XComponentContext; }
+
 namespace oox {
 namespace core {
 
diff --git a/include/oox/crypto/Standard2007Engine.hxx b/include/oox/crypto/Standard2007Engine.hxx
index 3a969f68e888..a951bfeef26d 100644
--- a/include/oox/crypto/Standard2007Engine.hxx
+++ b/include/oox/crypto/Standard2007Engine.hxx
@@ -22,6 +22,8 @@ namespace oox {
     class BinaryXOutputStream;
 }
 
+namespace com::sun::star::uno { class XComponentContext; }
+
 namespace oox {
 namespace core {
 
commit face3977675f551bc06b493e1f685d39b780573f
Author:     Serge Krot <Serge.Krot at cib.de>
AuthorDate: Thu Oct 10 10:01:16 2019 +0200
Commit:     Thorsten Behrens <Thorsten.Behrens at CIB.de>
CommitDate: Mon Nov 4 01:27:09 2019 +0100

    Fix compilation: SAL_CALL is missing in method declaration
    
    Change-Id: I4fc5e2de894a523d2a222ad9e7e04e1f1c01b9c5
    Reviewed-on: https://gerrit.libreoffice.org/80590
    Reviewed-by: Serge Krot (CIB) <Serge.Krot at cib.de>
    Tested-by: Serge Krot (CIB) <Serge.Krot at cib.de>

diff --git a/include/oox/crypto/AgileEngine.hxx b/include/oox/crypto/AgileEngine.hxx
index 8dacdd0ab625..db2cd082b751 100644
--- a/include/oox/crypto/AgileEngine.hxx
+++ b/include/oox/crypto/AgileEngine.hxx
@@ -131,24 +131,24 @@ public:
 
     // Decryption
 
-    sal_Bool generateEncryptionKey(const OUString & rPassword) override;
-    sal_Bool readEncryptionInfo(const css::uno::Sequence<css::beans::NamedValue>& aStreams) override;
-    sal_Bool decrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
+    virtual sal_Bool SAL_CALL generateEncryptionKey(const OUString & rPassword) override;
+    virtual sal_Bool SAL_CALL readEncryptionInfo(const css::uno::Sequence<css::beans::NamedValue>& aStreams) override;
+    virtual sal_Bool SAL_CALL decrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
                  css::uno::Reference<css::io::XOutputStream>& rxOutputStream) override;
 
 
-    sal_Bool checkDataIntegrity() override;
+    virtual sal_Bool SAL_CALL checkDataIntegrity() override;
 
     // Encryption
 
-    css::uno::Sequence<css::beans::NamedValue> writeEncryptionInfo() override;
+    virtual css::uno::Sequence<css::beans::NamedValue> SAL_CALL writeEncryptionInfo() override;
 
-    void encrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
+    virtual void SAL_CALL encrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
                  css::uno::Reference<css::io::XOutputStream>& rxOutputStream) override;
 
-    sal_Bool setupEncryption(const css::uno::Sequence<css::beans::NamedValue>& rMediaEncData) override;
+    virtual sal_Bool SAL_CALL setupEncryption(const css::uno::Sequence<css::beans::NamedValue>& rMediaEncData) override;
 
-    css::uno::Sequence<css::beans::NamedValue> createEncryptionData(const OUString& rPassword) override;
+    virtual css::uno::Sequence<css::beans::NamedValue> SAL_CALL createEncryptionData(const OUString& rPassword) override;
 };
 
 } // namespace core
diff --git a/include/oox/crypto/Standard2007Engine.hxx b/include/oox/crypto/Standard2007Engine.hxx
index dff03a5ea228..3a969f68e888 100644
--- a/include/oox/crypto/Standard2007Engine.hxx
+++ b/include/oox/crypto/Standard2007Engine.hxx
@@ -41,24 +41,24 @@ public:
 
     // Decryption
 
-    sal_Bool generateEncryptionKey(const OUString & rPassword) override;
-    sal_Bool readEncryptionInfo(const css::uno::Sequence<css::beans::NamedValue>& aStreams) override;
-    sal_Bool decrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
+    virtual sal_Bool SAL_CALL generateEncryptionKey(const OUString & rPassword) override;
+    virtual sal_Bool SAL_CALL readEncryptionInfo(const css::uno::Sequence<css::beans::NamedValue>& aStreams) override;
+    virtual sal_Bool SAL_CALL decrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
                  css::uno::Reference<css::io::XOutputStream>& rxOutputStream) override;
 
 
-    sal_Bool checkDataIntegrity() override;
+    virtual sal_Bool SAL_CALL checkDataIntegrity() override;
 
     // Encryption
 
-    css::uno::Sequence<css::beans::NamedValue> writeEncryptionInfo() override;
+    virtual css::uno::Sequence<css::beans::NamedValue> SAL_CALL writeEncryptionInfo() override;
 
-    void encrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
+    virtual void SAL_CALL encrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
                  css::uno::Reference<css::io::XOutputStream>& rxOutputStream) override;
 
-    sal_Bool setupEncryption(const css::uno::Sequence<css::beans::NamedValue>& rMediaEncData) override;
+    virtual sal_Bool SAL_CALL setupEncryption(const css::uno::Sequence<css::beans::NamedValue>& rMediaEncData) override;
 
-    css::uno::Sequence<css::beans::NamedValue> createEncryptionData(const OUString& rPassword) override;
+    virtual css::uno::Sequence<css::beans::NamedValue> SAL_CALL createEncryptionData(const OUString& rPassword) override;
 };
 
 } // namespace core
commit 3390d03027ad296fa0cf393978dd1647f779db52
Author:     Vasily Melenchuk <vasily.melenchuk at cib.de>
AuthorDate: Tue Oct 8 11:46:05 2019 +0300
Commit:     Thorsten Behrens <Thorsten.Behrens at CIB.de>
CommitDate: Mon Nov 4 01:27:09 2019 +0100

    uno: XModel2->setArgs() now able to set "EncryptionData"
    
    setArgs is able to set only limited media descriptor parameters.
    Extending this list by one more.
    
    Change-Id: I179a1cfc2cdd7b04becba0d7dfe9740d920ae4ee
    Reviewed-on: https://gerrit.libreoffice.org/80432
    Reviewed-by: Vasily Melenchuk <vasily.melenchuk at cib.de>
    Tested-by: Vasily Melenchuk <vasily.melenchuk at cib.de>

diff --git a/offapi/com/sun/star/frame/XModel2.idl b/offapi/com/sun/star/frame/XModel2.idl
index 128d5ced20d6..bab2427a3617 100644
--- a/offapi/com/sun/star/frame/XModel2.idl
+++ b/offapi/com/sun/star/frame/XModel2.idl
@@ -146,7 +146,7 @@ interface XModel2 : com::sun::star::frame::XModel
                 <li>com::sun::star::document::MediaDescriptor::LockExport</li>
                 <li>com::sun::star::document::MediaDescriptor::LockPrint</li>
                 <li>com::sun::star::document::MediaDescriptor::LockSave</li>
-
+                <li>com::sun::star::document::MediaDescriptor::EncryptionData</li>
             </ul>
 
         @throws com::sun::star::lang::IllegalArgumentException When trying to set an unsupported property
diff --git a/sfx2/source/doc/sfxbasemodel.cxx b/sfx2/source/doc/sfxbasemodel.cxx
index 395672aaae97..7cc810cdca80 100644
--- a/sfx2/source/doc/sfxbasemodel.cxx
+++ b/sfx2/source/doc/sfxbasemodel.cxx
@@ -1094,6 +1094,10 @@ void SAL_CALL SfxBaseModel::setArgs(const Sequence<beans::PropertyValue>& aArgs)
             rArg.Value >>= bValue;
             pMedium->GetItemSet()->Put(SfxBoolItem(SID_LOCK_SAVE, bValue));
         }
+        else if (rArg.Name == "EncryptionData")
+        {
+            pMedium->GetItemSet()->Put(SfxUnoAnyItem(SID_ENCRYPTIONDATA, rArg.Value));
+        }
         else
         {
             throw lang::IllegalArgumentException("Setting property not supported: " + rArg.Name,
commit ebe3dfd2a6bf35100964d44c25c96be016a51490
Author:     Vasily Melenchuk <vasily.melenchuk at cib.de>
AuthorDate: Fri Oct 4 11:23:30 2019 +0300
Commit:     Thorsten Behrens <Thorsten.Behrens at CIB.de>
CommitDate: Mon Nov 4 01:27:09 2019 +0100

    IRM encryption implmentation is moved out of core
    
    Change-Id: I2e83bfaf9e8190fe4839e2d39fcc9325305e4dda

diff --git a/include/oox/crypto/IRMEngine.hxx b/include/oox/crypto/IRMEngine.hxx
deleted file mode 100644
index c8ae7b125d86..000000000000
--- a/include/oox/crypto/IRMEngine.hxx
+++ /dev/null
@@ -1,70 +0,0 @@
-/* -*- 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/.
- *
- */
-
-#ifndef INCLUDED_OOX_CRYPTO_IRMENGINE_HXX
-#define INCLUDED_OOX_CRYPTO_IRMENGINE_HXX
-
-#include <oox/dllapi.h>
-#include <com/sun/star/packages/XPackageEncryption.hpp>
-#include <rtl/ustring.hxx>
-#include <sal/types.h>
-
-namespace oox
-{
-namespace core
-{
-struct OOX_DLLPUBLIC IRMEncryptionInfo
-{
-    OString license;
-    bool bCanRead;
-};
-
-class OOX_DLLPUBLIC IRMEngine : public cppu::WeakImplHelper<css::packages::XPackageEncryption>
-{
-    IRMEncryptionInfo mInfo;
-    css::uno::Reference<css::uno::XComponentContext> mxContext;
-
-    css::uno::Reference<css::io::XInputStream>
-    getStream(const css::uno::Sequence<css::beans::NamedValue>& rStreams,
-              const OUString sStreamName);
-
-public:
-    IRMEngine(const css::uno::Reference<css::uno::XComponentContext>& rxContext);
-
-    // Decryption
-
-    sal_Bool generateEncryptionKey(const OUString& rPassword) override;
-    sal_Bool
-    readEncryptionInfo(const css::uno::Sequence<css::beans::NamedValue>& aStreams) override;
-    sal_Bool decrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
-                     css::uno::Reference<css::io::XOutputStream>& rxOutputStream) override;
-
-    sal_Bool checkDataIntegrity() override;
-
-    // Encryption
-
-    css::uno::Sequence<css::beans::NamedValue> writeEncryptionInfo() override;
-
-    void encrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
-                 css::uno::Reference<css::io::XOutputStream>& rxOutputStream) override;
-
-    sal_Bool
-    setupEncryption(const css::uno::Sequence<css::beans::NamedValue>& rMediaEncData) override;
-
-    css::uno::Sequence<css::beans::NamedValue>
-    createEncryptionData(const OUString& rPassword) override;
-};
-
-} // namespace core
-} // namespace oox
-
-#endif
-
-/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/oox/Library_oox.mk b/oox/Library_oox.mk
index b54b9283cba5..cc235b87e360 100644
--- a/oox/Library_oox.mk
+++ b/oox/Library_oox.mk
@@ -16,19 +16,8 @@ $(eval $(call gb_Library_use_custom_headers,oox,oox/generated))
 $(eval $(call gb_Library_set_include,oox,\
     $$(INCLUDE) \
     -I$(SRCDIR)/oox/inc \
-	-I"C:\Program Files\Active Directory Rights Management Services SDK 2.1\inc"\
 ))
 
-$(eval $(call gb_Library_add_libs,oox,\
-	msipc_s.lib \
-	msipc.lib \
-))
-
-$(eval $(call gb_Library_add_ldflags,oox,\
-	-LIBPATH:"C:\Program Files\Active Directory Rights Management Services SDK 2.1\lib\x64"\
-))
-
-
 ifeq ($(COM)-$(OS)-$(CPUNAME),GCC-LINUX-IA64)
 # at least Debian Linux ia64 fails at compile time on
 # link libooxlo.so which is apparently too large
@@ -109,7 +98,6 @@ $(eval $(call gb_Library_add_exception_objects,oox,\
     oox/source/core/relationshandler \
     oox/source/core/xmlfilterbase \
     oox/source/crypto/AgileEngine \
-    oox/source/crypto/IRMEngine \
     oox/source/crypto/CryptTools \
     oox/source/crypto/DocumentEncryption \
     oox/source/crypto/DocumentDecryption \
diff --git a/oox/source/crypto/IRMEngine.cxx b/oox/source/crypto/IRMEngine.cxx
deleted file mode 100644
index 95135722ee08..000000000000
--- a/oox/source/crypto/IRMEngine.cxx
+++ /dev/null
@@ -1,447 +0,0 @@
-/* -*- 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 <oox/crypto/IRMEngine.hxx>
-
-#include <oox/helper/binaryinputstream.hxx>
-#include <oox/helper/binaryoutputstream.hxx>
-
-#include <sax/tools/converter.hxx>
-
-#include <comphelper/hash.hxx>
-#include <comphelper/docpasswordhelper.hxx>
-#include <comphelper/random.hxx>
-#include <comphelper/processfactory.hxx>
-#include <comphelper/base64.hxx>
-#include <comphelper/sequence.hxx>
-#include <comphelper/sequenceashashmap.hxx>
-
-#include <filter/msfilter/mscodec.hxx>
-#include <tools/stream.hxx>
-#include <tools/XmlWriter.hxx>
-
-#include <com/sun/star/io/XSeekable.hpp>
-#include <com/sun/star/io/XStream.hpp>
-#include <com/sun/star/io/SequenceInputStream.hpp>
-#include <com/sun/star/io/XSequenceOutputStream.hpp>
-#include <com/sun/star/uno/XComponentContext.hpp>
-#include <com/sun/star/xml/sax/XFastParser.hpp>
-#include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
-#include <com/sun/star/xml/sax/FastParser.hpp>
-#include <com/sun/star/xml/sax/FastToken.hpp>
-
-#include <msipc.h>
-
-using namespace css;
-using namespace css::beans;
-using namespace css::io;
-using namespace css::lang;
-using namespace css::uno;
-using namespace css::xml::sax;
-using namespace css::xml;
-
-namespace oox
-{
-namespace core
-{
-extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
-com_sun_star_comp_oox_crypto_DRMEncryptedDataSpace_get_implementation(
-    XComponentContext* pCtx, Sequence<Any> const& /*arguments*/)
-{
-    return cppu::acquire(new IRMEngine(pCtx /*, arguments*/));
-}
-
-IRMEngine::IRMEngine(const Reference<XComponentContext>& rxContext)
-    : mxContext(rxContext)
-{
-}
-
-sal_Bool IRMEngine::checkDataIntegrity() { return true; }
-
-sal_Bool IRMEngine::decrypt(const Reference<XInputStream>& rxInputStream,
-                            Reference<XOutputStream>& rxOutputStream)
-{
-    BinaryXInputStream aInputStream(rxInputStream, true);
-    BinaryXOutputStream aOutputStream(rxOutputStream, true);
-
-    aInputStream.readInt64(); // Skip stream size
-
-    HRESULT hr = IpcInitialize();
-    if (FAILED(hr) && hr != HRESULT_FROM_WIN32(ERROR_ALREADY_INITIALIZED))
-    {
-        // ERROR_ALREADY_INITIALIZED not an error
-        // TODO: some reaction?
-        return false;
-    }
-
-    // Get decryption key
-    IPC_BUFFER licenseBuffer;
-    IPC_KEY_HANDLE key;
-    licenseBuffer.pvBuffer = (void*)mInfo.license.getStr();
-    licenseBuffer.cbBuffer = mInfo.license.getLength();
-    hr = IpcGetKey(&licenseBuffer, 0, NULL, NULL, &key);
-    if (FAILED(hr))
-    {
-        // TODO: some reaction?
-        return false;
-    }
-
-    // Read rights
-    BOOL value;
-    hr = IpcAccessCheck(key, IPC_GENERIC_READ, &value);
-    if (FAILED(hr))
-    {
-        // TODO: some reaction?
-        return false;
-    }
-    mInfo.bCanRead = value;
-
-    // Get size of decrypt block
-    DWORD* blockSize;
-    hr = IpcGetKeyProperty(key, IPC_KI_BLOCK_SIZE, nullptr, (LPVOID*)&blockSize);
-    if (FAILED(hr))
-    {
-        // TODO: some reaction?
-        return false;
-    }
-
-    char* pEncryptedBuffer = new char[*blockSize];
-    char* pDecryptedBuffer = new char[*blockSize];
-    int blockNo = 0;
-    bool lastBlock = false;
-
-    do
-    {
-        sal_uInt32 readBytes = aInputStream.readArray(pEncryptedBuffer, *blockSize);
-        lastBlock = readBytes != *blockSize;
-        DWORD bytes = 0;
-        hr = IpcDecrypt(key, blockNo, lastBlock, (PBYTE)pEncryptedBuffer, *blockSize,
-                        (PBYTE)pDecryptedBuffer, *blockSize, &bytes);
-
-        if (FAILED(hr))
-        {
-            // TODO: some reaction?
-            return false;
-        }
-
-        aOutputStream.writeArray(pDecryptedBuffer, bytes);
-
-        blockNo++;
-    } while (!lastBlock);
-
-    delete[] pEncryptedBuffer;
-    delete[] pDecryptedBuffer;
-
-    rxOutputStream->flush();
-
-    return true;
-}
-
-uno::Sequence<beans::NamedValue> IRMEngine::createEncryptionData(const OUString& /*rPassword*/)
-{
-    css::uno::Sequence<sal_uInt8> seq;
-    seq.realloc(mInfo.license.getLength());
-    memcpy(seq.getArray(), mInfo.license.getStr(), mInfo.license.getLength());
-
-    comphelper::SequenceAsHashMap aEncryptionData;
-    aEncryptionData["LicenseKey"] <<= seq;
-    aEncryptionData["CryptoType"] <<= OUString("DRMEncryptedDataSpace");
-    aEncryptionData["OOXPassword"] <<= OUString("1");
-
-    return aEncryptionData.getAsConstNamedValueList();
-}
-
-uno::Reference<io::XInputStream> IRMEngine::getStream(const Sequence<NamedValue>& rStreams,
-                                                      const OUString sStreamName)
-{
-    for (const auto& aStream : rStreams)
-    {
-        if (aStream.Name == sStreamName)
-        {
-            css::uno::Sequence<sal_Int8> aSeq;
-            aStream.Value >>= aSeq;
-            Reference<XInputStream> aStream(
-                io::SequenceInputStream::createStreamFromSequence(mxContext, aSeq),
-                UNO_QUERY_THROW);
-            return aStream;
-        }
-    }
-    return nullptr;
-}
-
-sal_Bool IRMEngine::readEncryptionInfo(const uno::Sequence<beans::NamedValue>& aStreams)
-{
-    // Read TransformInfo storage for IRM ECMA documents (MS-OFFCRYPTO 2.2.4)
-    uno::Reference<io::XInputStream> xTransformInfoStream
-        = getStream(aStreams, "\006DataSpaces/TransformInfo/DRMEncryptedTransform/\006Primary");
-    SAL_WARN_IF(!xTransformInfoStream.is(), "oox", "TransormInfo stream is missing!");
-    BinaryXInputStream aBinaryStream(xTransformInfoStream, true);
-
-    // MS-OFFCRYPTO 2.1.8: TransformInfoHeader
-    aBinaryStream.readuInt32(); // TransformLength
-    aBinaryStream.readuInt32(); // TransformType
-    // TransformId
-    sal_uInt32 aStringLength = aBinaryStream.readuInt32();
-    OUString sTransformId = aBinaryStream.readUnicodeArray(aStringLength / 2);
-    aBinaryStream.skip((4 - (aStringLength & 3)) & 3); // Skip padding
-
-    // TransformName
-    aStringLength = aBinaryStream.readuInt32();
-    OUString sTransformName = aBinaryStream.readUnicodeArray(aStringLength / 2);
-    aBinaryStream.skip((4 - (aStringLength & 3)) & 3); // Skip padding
-
-    aBinaryStream.readuInt32(); // ReaderVersion
-    aBinaryStream.readuInt32(); // UpdaterVersion
-    aBinaryStream.readuInt32(); // WriterVersion
-
-    // MS-OFFCRYPTO 2.2.5: ExtensibilityHeader
-    aBinaryStream.readuInt32(); // ExtensibilityHeader
-
-    // MS-OFFCRYPTO 2.2.6: XrMLLicense
-    aStringLength = aBinaryStream.readuInt32();
-    mInfo.license = aBinaryStream.readCharArray(aStringLength);
-
-    if (mInfo.license.getLength()
-        && static_cast<sal_uChar>(mInfo.license[0]) != 0x0ef) // BOM is missing?
-    {
-        mInfo.license = "\x0ef\x0bb\x0bf" + mInfo.license;
-    }
-
-    // TODO: CHECK info data
-
-    return true;
-}
-
-sal_Bool IRMEngine::setupEncryption(const Sequence<NamedValue>& rMediaEncData)
-{
-    for (int i = 0; i < rMediaEncData.getLength(); i++)
-    {
-        if (rMediaEncData[i].Name == "LicenseKey")
-        {
-            css::uno::Sequence<sal_uInt8> seq;
-            rMediaEncData[i].Value >>= seq;
-            mInfo.license = OString(reinterpret_cast<sal_Char*>(seq.getArray()), seq.getLength());
-        }
-    }
-
-    return true;
-}
-
-Sequence<NamedValue> IRMEngine::writeEncryptionInfo()
-{
-    // Write 0x6DataSpaces/DataSpaceMap
-    Reference<XOutputStream> xDataSpaceMap(
-        mxContext->getServiceManager()->createInstanceWithContext(
-            "com.sun.star.io.SequenceOutputStream", mxContext),
-        UNO_QUERY);
-    BinaryXOutputStream aDataSpaceMapStream(xDataSpaceMap, false);
-
-    aDataSpaceMapStream.WriteInt32(8); // Header length
-    aDataSpaceMapStream.WriteInt32(1); // Entries count
-
-    // DataSpaceMapEntry (MS-OFFCRYPTO 2.1.6.1)
-    OUString sDataSpaceName("DRMEncryptedDataSpace");
-    OUString sReferenceComponent("EncryptedPackage");
-
-    aDataSpaceMapStream.WriteInt32(0x60); // Length
-    aDataSpaceMapStream.WriteInt32(1); // References count
-    aDataSpaceMapStream.WriteInt32(0); // References component type
-
-    aDataSpaceMapStream.WriteInt32(sReferenceComponent.getLength() * 2);
-    aDataSpaceMapStream.writeUnicodeArray(sReferenceComponent);
-    for (int i = 0; i < sReferenceComponent.getLength() * 2 % 4; i++) // Padding
-    {
-        aDataSpaceMapStream.writeValue<sal_Char>(0);
-    }
-
-    aDataSpaceMapStream.WriteInt32(sDataSpaceName.getLength() * 2);
-    aDataSpaceMapStream.writeUnicodeArray(sDataSpaceName);
-    for (int i = 0; i < sDataSpaceName.getLength() * 2 % 4; i++) // Padding
-    {
-        aDataSpaceMapStream.writeValue<sal_Char>(0);
-    }
-
-    aDataSpaceMapStream.close();
-    xDataSpaceMap->flush();
-
-    // Write 0x6DataSpaces/Version
-    Reference<XOutputStream> xVersion(mxContext->getServiceManager()->createInstanceWithContext(
-                                          "com.sun.star.io.SequenceOutputStream", mxContext),
-                                      UNO_QUERY);
-    BinaryXOutputStream aVersionStream(xVersion, false);
-
-    OUString sFeatureIdentifier("Microsoft.Container.DataSpaces");
-    aVersionStream.WriteInt32(sFeatureIdentifier.getLength() * 2);
-    aVersionStream.writeUnicodeArray(sFeatureIdentifier);
-    for (int i = 0; i < sFeatureIdentifier.getLength() * 2 % 4; i++) // Padding
-    {
-        aVersionStream.writeValue<sal_Char>(0);
-    }
-
-    aVersionStream.WriteInt32(1); // Reader version
-    aVersionStream.WriteInt32(1); // Updater version
-    aVersionStream.WriteInt32(1); // Writer version
-
-    aVersionStream.close();
-    xVersion->flush();
-
-    // Write 0x6DataSpaces/DataSpaceInfo/[dataspacename]
-    Reference<XOutputStream> xDataSpaceInfo(
-        mxContext->getServiceManager()->createInstanceWithContext(
-            "com.sun.star.io.SequenceOutputStream", mxContext),
-        UNO_QUERY);
-    BinaryXOutputStream aDataSpaceInfoStream(xDataSpaceInfo, false);
-
-    aDataSpaceInfoStream.WriteInt32(0x08); // Header length
-    aDataSpaceInfoStream.WriteInt32(1); // Entries count
-
-    OUString sTransformName("DRMEncryptedTransform");
-    aDataSpaceInfoStream.WriteInt32(sTransformName.getLength() * 2);
-    aDataSpaceInfoStream.writeUnicodeArray(sTransformName);
-    for (int i = 0; i < sTransformName.getLength() * 2 % 4; i++) // Padding
-    {
-        aDataSpaceInfoStream.writeValue<sal_Char>(0);
-    }
-
-    aDataSpaceInfoStream.close();
-    xDataSpaceInfo->flush();
-
-    // Write 0x6DataSpaces/TransformInfo/[transformname]
-    Reference<XOutputStream> xTransformInfo(
-        mxContext->getServiceManager()->createInstanceWithContext(
-            "com.sun.star.io.SequenceOutputStream", mxContext),
-        UNO_QUERY);
-    BinaryXOutputStream aTransformInfoStream(xTransformInfo, false);
-    OUString sTransformId("{C73DFACD-061F-43B0-8B64-0C620D2A8B50}");
-
-    // MS-OFFCRYPTO 2.1.8: TransformInfoHeader
-    sal_uInt32 nLength
-        = sTransformId.getLength() * 2 + ((4 - (sTransformId.getLength() & 3)) & 3) + 10;
-    aTransformInfoStream.WriteInt32(nLength); // TransformLength, will be written later
-    aTransformInfoStream.WriteInt32(1); // TransformType
-
-    // TransformId
-    aTransformInfoStream.WriteInt32(sTransformId.getLength() * 2);
-    aTransformInfoStream.writeUnicodeArray(sTransformId);
-    for (int i = 0; i < sTransformId.getLength() * 2 % 4; i++) // Padding
-    {
-        aTransformInfoStream.writeValue<sal_Char>(0);
-    }
-
-    // TransformName
-    OUString sTransformInfoName("Microsoft.Metadata.DRMTransform");
-    aTransformInfoStream.WriteInt32(sTransformInfoName.getLength() * 2);
-    aTransformInfoStream.writeUnicodeArray(sTransformInfoName);
-    for (int i = 0; i < sTransformInfoName.getLength() * 2 % 4; i++) // Padding
-    {
-        aTransformInfoStream.writeValue<sal_Char>(0);
-    }
-
-    aTransformInfoStream.WriteInt32(1); // ReaderVersion
-    aTransformInfoStream.WriteInt32(1); // UpdateVersion
-    aTransformInfoStream.WriteInt32(1); // WriterVersion
-
-    aTransformInfoStream.WriteInt32(4); // Extensibility Header
-
-    aTransformInfoStream.WriteInt32(mInfo.license.getLength() - 3); // LicenseLength - BOM
-    aTransformInfoStream.writeArray<sal_Char>(mInfo.license.getStr() + 3,
-                                              mInfo.license.getLength() - 3);
-    aTransformInfoStream.writeValue<sal_Char>(0);
-
-    aTransformInfoStream.close();
-    xTransformInfo->flush();
-
-    // Store all streams into sequence and return back
-    comphelper::SequenceAsHashMap aStreams;
-
-    Reference<XSequenceOutputStream> xDataSpaceMapSequence(xDataSpaceMap, UNO_QUERY);
-    aStreams["\006DataSpaces/DataSpaceMap"] <<= xDataSpaceMapSequence->getWrittenBytes();
-
-    Reference<XSequenceOutputStream> xVersionSequence(xVersion, UNO_QUERY);
-    aStreams["\006DataSpaces/Version"] <<= xVersionSequence->getWrittenBytes();
-
-    OUString sStreamName = "\006DataSpaces/DataSpaceInfo/" + sDataSpaceName;
-    Reference<XSequenceOutputStream> xDataSpaceInfoSequence(xDataSpaceInfo, UNO_QUERY);
-    aStreams[sStreamName] <<= xDataSpaceInfoSequence->getWrittenBytes();
-
-    sStreamName = "\006DataSpaces/TransformInfo/" + sTransformName + "/\006Primary";
-    Reference<XSequenceOutputStream> xTransformInfoSequence(xTransformInfo, UNO_QUERY);
-    aStreams[sStreamName] <<= xTransformInfoSequence->getWrittenBytes();
-
-    return aStreams.getAsConstNamedValueList();
-}
-
-void IRMEngine::encrypt(const Reference<XInputStream>& rxInputStream,
-                        Reference<XOutputStream>& rxOutputStream)
-{
-    HRESULT hr = IpcInitialize();
-
-    if (FAILED(hr) && hr != HRESULT_FROM_WIN32(ERROR_ALREADY_INITIALIZED))
-    {
-        // ERROR_ALREADY_INITIALIZED not an error
-        // TODO: some reaction?
-    }
-
-    BinaryXInputStream aInputStream(rxInputStream, false);
-    BinaryXOutputStream aOutputStream(rxOutputStream, false);
-    aOutputStream.WriteInt64(aInputStream.size()); // Stream size
-
-    // Get decryption key
-    IPC_BUFFER licenseBuffer;
-    IPC_KEY_HANDLE key;
-    licenseBuffer.pvBuffer = (void*)mInfo.license.getStr();
-    licenseBuffer.cbBuffer = mInfo.license.getLength();
-    hr = IpcGetKey(&licenseBuffer, 0, NULL, NULL, &key);
-    if (FAILED(hr))
-    {
-        // TODO: some reaction?
-    }
-
-    // Get size of encrypt block
-    DWORD* blockSize;
-    hr = IpcGetKeyProperty(key, IPC_KI_BLOCK_SIZE, nullptr, (LPVOID*)&blockSize);
-    if (FAILED(hr))
-    {
-        // TODO: some reaction?
-    }
-
-    char* pEncryptedBuffer = new char[*blockSize];
-    char* pDecryptedBuffer = new char[*blockSize];
-    int blockNo = 0;
-    bool lastBlock = false;
-
-    do
-    {
-        sal_uInt32 readBytes = aInputStream.readArray(pDecryptedBuffer, *blockSize);
-        lastBlock = readBytes != *blockSize;
-        DWORD bytes = 0;
-        hr = IpcEncrypt(key, blockNo, lastBlock, (PBYTE)pDecryptedBuffer, *blockSize,
-                        (PBYTE)pEncryptedBuffer, *blockSize, &bytes);
-
-        if (FAILED(hr))
-        {
-            // TODO: some reaction?
-        }
-
-        aOutputStream.writeArray(pEncryptedBuffer, bytes);
-
-        blockNo++;
-    } while (!lastBlock);
-
-    delete[] pEncryptedBuffer;
-    delete[] pDecryptedBuffer;
-}
-
-sal_Bool IRMEngine::generateEncryptionKey(const OUString& /*password*/) { return true; }
-
-} // namespace core
-} // namespace oox
-
-/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/oox/util/oox.component b/oox/util/oox.component
index 0643a417bfa3..d7167545b2be 100644
--- a/oox/util/oox.component
+++ b/oox/util/oox.component
@@ -40,14 +40,6 @@
     constructor="com_sun_star_comp_oox_ShapeContextHandler_get_implementation">
     <service name="com.sun.star.xml.sax.FastShapeContextHandler"/>
   </implementation>
-  <implementation name="com.sun.star.comp.oox.crypto.DRMEncryptedDataSpace"
-    constructor="com_sun_star_comp_oox_crypto_DRMEncryptedDataSpace_get_implementation">
-    <service name="com.sun.star.packages.XPackageEncryption"/>
-  </implementation>
-  <implementation name="com.sun.star.comp.oox.crypto.DRMDataSpace"
-    constructor="com_sun_star_comp_oox_crypto_DRMDataSpace_get_implementation">
-    <service name="com.sun.star.packages.XPackageEncryption"/>
-  </implementation>
   <implementation name="com.sun.star.comp.oox.crypto.Standard2007Engine"
     constructor="com_sun_star_comp_oox_crypto_Standard2007_get_implementation">
     <service name="com.sun.star.packages.XPackageEncryption"/>
commit 1e88eb67cbd1a2f6396f71d7698314e91f0405a3
Author:     Vasily Melenchuk <vasily.melenchuk at cib.de>
AuthorDate: Fri Sep 27 11:56:00 2019 +0300
Commit:     Thorsten Behrens <Thorsten.Behrens at CIB.de>
CommitDate: Mon Nov 4 01:27:08 2019 +0100

    msdoc crypto: move crypto engines to service
    
    New interface XPackageEncryption was created. All existing
    crypto engines are refactored to be implmentations of this interface.
    
    Change-Id: Id063aca1474f76a926a2e47eecd4c12ebe79650f

diff --git a/include/oox/crypto/AgileEngine.hxx b/include/oox/crypto/AgileEngine.hxx
index 2918384f9d2a..8dacdd0ab625 100644
--- a/include/oox/crypto/AgileEngine.hxx
+++ b/include/oox/crypto/AgileEngine.hxx
@@ -15,9 +15,9 @@
 
 #include <oox/dllapi.h>
 #include <oox/crypto/CryptTools.hxx>
-#include <oox/crypto/CryptoEngine.hxx>
 #include <rtl/ustring.hxx>
 #include <sal/types.h>
+#include <com/sun/star/packages/XPackageEncryption.hpp>
 
 namespace oox {
     class BinaryXInputStream;
@@ -74,7 +74,7 @@ enum class AgileEncryptionPreset
     AES_256_SHA512,
 };
 
-class OOX_DLLPUBLIC AgileEngine final : public CryptoEngine
+class OOX_DLLPUBLIC AgileEngine final : public cppu::WeakImplHelper<css::packages::XPackageEncryption>
 {
 private:
     std::vector<sal_uInt8> mKey;
@@ -82,7 +82,7 @@ private:
     AgileEncryptionPreset meEncryptionPreset;
     css::uno::Reference< css::uno::XComponentContext > mxContext;
 
-    css::uno::Reference<css::io::XInputStream> getStream(css::uno::Sequence<css::beans::NamedValue> & rStreams, const OUString sStreamName);
+    css::uno::Reference<css::io::XInputStream> getStream(const css::uno::Sequence<css::beans::NamedValue> & rStreams, const OUString sStreamName);
 
     void calculateHashFinal(const OUString& rPassword, std::vector<sal_uInt8>& aHashFinal);
 
@@ -131,24 +131,24 @@ public:
 
     // Decryption
 
-    bool generateEncryptionKey(OUString const & rPassword) override;
-    bool readEncryptionInfo(css::uno::Sequence<css::beans::NamedValue> aStreams) override;
-    bool decrypt(BinaryXInputStream& aInputStream,
-                 BinaryXOutputStream& aOutputStream) override;
+    sal_Bool generateEncryptionKey(const OUString & rPassword) override;
+    sal_Bool readEncryptionInfo(const css::uno::Sequence<css::beans::NamedValue>& aStreams) override;
+    sal_Bool decrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
+                 css::uno::Reference<css::io::XOutputStream>& rxOutputStream) override;
 
-    bool checkDataIntegrity() override;
+
+    sal_Bool checkDataIntegrity() override;
 
     // Encryption
 
-    void writeEncryptionInfo(oox::ole::OleStorage& rOleStorage) override;
+    css::uno::Sequence<css::beans::NamedValue> writeEncryptionInfo() override;
 
-    void encrypt(css::uno::Reference<css::io::XInputStream>&  rxInputStream,
-                 css::uno::Reference<css::io::XOutputStream>& rxOutputStream,
-                 sal_uInt32 nSize) override;
+    void encrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
+                 css::uno::Reference<css::io::XOutputStream>& rxOutputStream) override;
 
-    bool setupEncryption(css::uno::Sequence<css::beans::NamedValue>& rMediaEncData) override;
+    sal_Bool setupEncryption(const css::uno::Sequence<css::beans::NamedValue>& rMediaEncData) override;
 
-    virtual void createEncryptionData(comphelper::SequenceAsHashMap & aEncryptionData, const OUString rPassword) override;
+    css::uno::Sequence<css::beans::NamedValue> createEncryptionData(const OUString& rPassword) override;
 };
 
 } // namespace core
diff --git a/include/oox/crypto/CryptoEngine.hxx b/include/oox/crypto/CryptoEngine.hxx
deleted file mode 100644
index f985f2d7fb45..000000000000
--- a/include/oox/crypto/CryptoEngine.hxx
+++ /dev/null
@@ -1,68 +0,0 @@
-/* -*- 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/.
- *
- */
-
-#ifndef INCLUDED_OOX_CRYPTO_CRYPTOENGINE_HXX
-#define INCLUDED_OOX_CRYPTO_CRYPTOENGINE_HXX
-
-#include <vector>
-
-#include <rtl/ustring.hxx>
-#include <sal/types.h>
-
-#include <com/sun/star/io/XInputStream.hpp>
-#include <com/sun/star/io/XOutputStream.hpp>
-#include <com/sun/star/uno/Sequence.hxx>
-
-namespace oox {
-    class BinaryXInputStream;
-    class BinaryXOutputStream;
-}
-
-namespace oox {
-namespace core {
-
-class CryptoEngine
-{
-public:
-    CryptoEngine()
-    {}
-
-    virtual ~CryptoEngine()
-    {}
-
-    // Decryption
-    virtual bool readEncryptionInfo(css::uno::Sequence<css::beans::NamedValue> aStreams) = 0;
-
-    virtual bool generateEncryptionKey(const OUString& rPassword) = 0;
-
-    virtual bool decrypt(
-                    BinaryXInputStream& aInputStream,
-                    BinaryXOutputStream& aOutputStream) = 0;
-
-    // Encryption
-    virtual void writeEncryptionInfo(oox::ole::OleStorage& rOleStorage) = 0;
-
-    virtual void createEncryptionData(comphelper::SequenceAsHashMap & aEncryptionData, const OUString rPassword) = 0;
-
-    virtual bool setupEncryption(css::uno::Sequence<css::beans::NamedValue>& rMediaEncData) = 0;
-
-    virtual void encrypt(css::uno::Reference<css::io::XInputStream> & rxInputStream,
-                         css::uno::Reference<css::io::XOutputStream> & rxOutputStream,
-                         sal_uInt32 nSize) = 0;
-
-    virtual bool checkDataIntegrity() = 0;
-};
-
-} // namespace core
-} // namespace oox
-
-#endif
-
-/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/include/oox/crypto/DocumentDecryption.hxx b/include/oox/crypto/DocumentDecryption.hxx
index 248f33f61734..fdea2c25f9f7 100644
--- a/include/oox/crypto/DocumentDecryption.hxx
+++ b/include/oox/crypto/DocumentDecryption.hxx
@@ -17,7 +17,6 @@
 
 #include <com/sun/star/uno/Reference.hxx>
 #include <com/sun/star/uno/Sequence.hxx>
-#include <oox/crypto/CryptoEngine.hxx>
 #include <rtl/ustring.hxx>
 
 namespace com { namespace sun { namespace star {
@@ -25,6 +24,7 @@ namespace com { namespace sun { namespace star {
     namespace io { class XInputStream; }
     namespace io { class XStream; }
     namespace uno { class XComponentContext; }
+    namespace packages { class XPackageEncryption; }
 } } }
 
 namespace oox { namespace ole { class OleStorage; } }
@@ -37,8 +37,7 @@ class OOX_DLLPUBLIC DocumentDecryption
 private:
     oox::ole::OleStorage&                      mrOleStorage;
     css::uno::Sequence<css::beans::NamedValue> maStreamsSequence;
-    std::unique_ptr<CryptoEngine>              mEngine;
-    OUString                                   msEngineName;
+    css::uno::Reference< css::packages::XPackageEncryption > mxPackageEncryption;
     css::uno::Reference< css::uno::XComponentContext > mxContext;
 
     void readStrongEncryptionInfo();
diff --git a/include/oox/crypto/DocumentEncryption.hxx b/include/oox/crypto/DocumentEncryption.hxx
index 9e9c013456ca..d33450a3b791 100644
--- a/include/oox/crypto/DocumentEncryption.hxx
+++ b/include/oox/crypto/DocumentEncryption.hxx
@@ -15,11 +15,11 @@
 
 #include <com/sun/star/uno/Reference.hxx>
 #include <com/sun/star/uno/Sequence.hxx>
-#include <oox/crypto/CryptoEngine.hxx>
 #include <rtl/ustring.hxx>
 
 namespace com { namespace sun { namespace star {
     namespace io { class XStream; }
+    namespace packages { class XPackageEncryption; }
 } } }
 
 namespace oox { namespace ole { class OleStorage; } }
@@ -32,7 +32,7 @@ class OOX_DLLPUBLIC DocumentEncryption
 private:
     css::uno::Reference< css::io::XStream > mxDocumentStream;
     oox::ole::OleStorage& mrOleStorage;
-    std::unique_ptr<CryptoEngine>   mEngine;
+    css::uno::Reference< css::packages::XPackageEncryption > mxPackageEncryption;
     css::uno::Sequence< css::beans::NamedValue >& mMediaEncData;
     css::uno::Reference< css::uno::XComponentContext > mxContext;
 
diff --git a/include/oox/crypto/IRMEngine.hxx b/include/oox/crypto/IRMEngine.hxx
index 9bf610904a9d..c8ae7b125d86 100644
--- a/include/oox/crypto/IRMEngine.hxx
+++ b/include/oox/crypto/IRMEngine.hxx
@@ -12,19 +12,12 @@
 #define INCLUDED_OOX_CRYPTO_IRMENGINE_HXX
 
 #include <oox/dllapi.h>
-#include <oox/crypto/CryptoEngine.hxx>
-#include <filter/msfilter/mscodec.hxx>
+#include <com/sun/star/packages/XPackageEncryption.hpp>
 #include <rtl/ustring.hxx>
 #include <sal/types.h>
 
 namespace oox
 {
-class BinaryXInputStream;
-class BinaryXOutputStream;
-}
-
-namespace oox
-{
 namespace core
 {
 struct OOX_DLLPUBLIC IRMEncryptionInfo
@@ -33,37 +26,40 @@ struct OOX_DLLPUBLIC IRMEncryptionInfo
     bool bCanRead;
 };
 
-class OOX_DLLPUBLIC IRMEngine : public CryptoEngine
+class OOX_DLLPUBLIC IRMEngine : public cppu::WeakImplHelper<css::packages::XPackageEncryption>
 {
     IRMEncryptionInfo mInfo;
     css::uno::Reference<css::uno::XComponentContext> mxContext;
 
     css::uno::Reference<css::io::XInputStream>
-    getStream(css::uno::Sequence<css::beans::NamedValue>& rStreams, const OUString sStreamName);
+    getStream(const css::uno::Sequence<css::beans::NamedValue>& rStreams,
+              const OUString sStreamName);
 
 public:
     IRMEngine(const css::uno::Reference<css::uno::XComponentContext>& rxContext);
 
-    bool readEncryptionInfo(css::uno::Sequence<css::beans::NamedValue> aStreams) override;
+    // Decryption
 
-    virtual bool generateEncryptionKey(OUString const& rPassword) override;
+    sal_Bool generateEncryptionKey(const OUString& rPassword) override;
+    sal_Bool
+    readEncryptionInfo(const css::uno::Sequence<css::beans::NamedValue>& aStreams) override;
+    sal_Bool decrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
+                     css::uno::Reference<css::io::XOutputStream>& rxOutputStream) override;
 
-    virtual bool decrypt(BinaryXInputStream& aInputStream,
-                         BinaryXOutputStream& aOutputStream) override;
+    sal_Bool checkDataIntegrity() override;
 
-    bool checkDataIntegrity() override;
+    // Encryption
 
-    void encrypt(css::uno::Reference<css::io::XInputStream>& rxInputStream,
-                 css::uno::Reference<css::io::XOutputStream>& rxOutputStream,
-                 sal_uInt32 nSize) override;
+    css::uno::Sequence<css::beans::NamedValue> writeEncryptionInfo() override;
 
-    virtual void writeEncryptionInfo(oox::ole::OleStorage& rOleStorage) override;
+    void encrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
+                 css::uno::Reference<css::io::XOutputStream>& rxOutputStream) override;
 
-    virtual void createEncryptionData(comphelper::SequenceAsHashMap& aEncryptionData,
-                                      const OUString rPassword) override;
+    sal_Bool
+    setupEncryption(const css::uno::Sequence<css::beans::NamedValue>& rMediaEncData) override;
 
-    virtual bool
-    setupEncryption(css::uno::Sequence<css::beans::NamedValue>& rMediaEncData) override;
+    css::uno::Sequence<css::beans::NamedValue>
+    createEncryptionData(const OUString& rPassword) override;
 };
 
 } // namespace core
diff --git a/include/oox/crypto/Standard2007Engine.hxx b/include/oox/crypto/Standard2007Engine.hxx
index a7cb6554403b..dff03a5ea228 100644
--- a/include/oox/crypto/Standard2007Engine.hxx
+++ b/include/oox/crypto/Standard2007Engine.hxx
@@ -12,7 +12,7 @@
 #define INCLUDED_OOX_CRYPTO_STANDARD2007ENGINE_HXX
 
 #include <oox/dllapi.h>
-#include <oox/crypto/CryptoEngine.hxx>
+#include <com/sun/star/packages/XPackageEncryption.hpp>
 #include <filter/msfilter/mscodec.hxx>
 #include <rtl/ustring.hxx>
 #include <sal/types.h>
@@ -25,7 +25,7 @@ namespace oox {
 namespace oox {
 namespace core {
 
-class OOX_DLLPUBLIC Standard2007Engine final : public CryptoEngine
+class OOX_DLLPUBLIC Standard2007Engine final : public cppu::WeakImplHelper<css::packages::XPackageEncryption>
 {
     msfilter::StandardEncryptionInfo mInfo;
     std::vector<sal_uInt8> mKey;
@@ -34,31 +34,31 @@ class OOX_DLLPUBLIC Standard2007Engine final : public CryptoEngine
     bool generateVerifier();
     bool calculateEncryptionKey(const OUString& rPassword);
 
-    css::uno::Reference<css::io::XInputStream> getStream(css::uno::Sequence<css::beans::NamedValue> & rStreams, const OUString sStreamName);
+    css::uno::Reference<css::io::XInputStream> getStream(const css::uno::Sequence<css::beans::NamedValue> & rStreams, const OUString sStreamName);
 
 public:
     Standard2007Engine(const css::uno::Reference<css::uno::XComponentContext>& rxContext);
 
-    bool readEncryptionInfo(css::uno::Sequence<css::beans::NamedValue> aStreams) override;
+    // Decryption
 
-    virtual bool generateEncryptionKey(OUString const & rPassword) override;
+    sal_Bool generateEncryptionKey(const OUString & rPassword) override;
+    sal_Bool readEncryptionInfo(const css::uno::Sequence<css::beans::NamedValue>& aStreams) override;
+    sal_Bool decrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
+                 css::uno::Reference<css::io::XOutputStream>& rxOutputStream) override;
 
-    virtual bool decrypt(
-                    BinaryXInputStream& aInputStream,
-                    BinaryXOutputStream& aOutputStream) override;
 
-    bool checkDataIntegrity() override;
+    sal_Bool checkDataIntegrity() override;
 
-    void encrypt(css::uno::Reference<css::io::XInputStream>&  rxInputStream,
-                 css::uno::Reference<css::io::XOutputStream>& rxOutputStream,
-                 sal_uInt32 nSize) override;
+    // Encryption
 
-    virtual void writeEncryptionInfo(oox::ole::OleStorage& rOleStorage) override;
+    css::uno::Sequence<css::beans::NamedValue> writeEncryptionInfo() override;
 
-    virtual void createEncryptionData(comphelper::SequenceAsHashMap & aEncryptionData, const OUString rPassword) override;
+    void encrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
+                 css::uno::Reference<css::io::XOutputStream>& rxOutputStream) override;
 
-    virtual bool setupEncryption(css::uno::Sequence<css::beans::NamedValue>& rMediaEncData) override;
+    sal_Bool setupEncryption(const css::uno::Sequence<css::beans::NamedValue>& rMediaEncData) override;
 
+    css::uno::Sequence<css::beans::NamedValue> createEncryptionData(const OUString& rPassword) override;
 };
 
 } // namespace core
diff --git a/offapi/UnoApi_offapi.mk b/offapi/UnoApi_offapi.mk
index 5fa55f8f191d..ad2ee805fd6d 100644
--- a/offapi/UnoApi_offapi.mk
+++ b/offapi/UnoApi_offapi.mk
@@ -2930,6 +2930,7 @@ $(eval $(call gb_UnoApi_add_idlfiles,offapi,com/sun/star/packages,\
 	NoRawFormatException \
 	WrongPasswordException \
 	XDataSinkEncrSupport \
+	XPackageEncryption \
 ))
 $(eval $(call gb_UnoApi_add_idlfiles,offapi,com/sun/star/packages/manifest,\
 	XManifestReader \
diff --git a/offapi/com/sun/star/packages/XPackageEncryption.idl b/offapi/com/sun/star/packages/XPackageEncryption.idl
new file mode 100644
index 000000000000..298a089af6c7
--- /dev/null
+++ b/offapi/com/sun/star/packages/XPackageEncryption.idl
@@ -0,0 +1,84 @@
+/* -*- 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/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ *   Licensed to the Apache Software Foundation (ASF) under one or more
+ *   contributor license agreements. See the NOTICE file distributed
+ *   with this work for additional information regarding copyright
+ *   ownership. The ASF licenses this file to you under the Apache
+ *   License, Version 2.0 (the "License"); you may not use this file
+ *   except in compliance with the License. You may obtain a copy of
+ *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+#ifndef __com_sun_star_packages_XPackageEncryption_idl__
+#define __com_sun_star_packages_XPackageEncryption_idl__
+
+#include <com/sun/star/uno/XInterface.idl>
+
+#include <com/sun/star/io/XInputStream.idl>
+#include <com/sun/star/io/XOutputStream.idl>
+
+
+module com {  module sun {  module star {  module packages {
+
+
+/**
+    TODO
+ */
+interface XPackageEncryption: com::sun::star::uno::XInterface
+{
+    /**
+        TODO
+     */
+    boolean readEncryptionInfo([in] sequence < com::sun::star::beans::NamedValue > rOleStreams);
+
+    /**
+        TODO
+     */
+    boolean generateEncryptionKey([in] string rPassword);
+
+    /**
+        TODO
+     */
+    boolean decrypt([in] com::sun::star::io::XInputStream rxInputStream,
+                    [out] com::sun::star::io::XOutputStream rxOutputStream);
+
+    /**
+        TODO
+     */
+    sequence<com::sun::star::beans::NamedValue> writeEncryptionInfo();
+
+    /**
+        TODO
+     */
+    sequence<com::sun::star::beans::NamedValue> createEncryptionData([in] string rPassword);
+
+    /**
+        TODO
+     */
+    boolean setupEncryption([in] sequence<com::sun::star::beans::NamedValue> rMediaEncData);
+
+    /**
+        TODO
+     */
+    void encrypt([in] com::sun::star::io::XInputStream rxInputStream,
+                 [out] com::sun::star::io::XOutputStream rxOutputStream);
+
+    /**
+        TODO
+     */
+    boolean checkDataIntegrity();
+};
+
+
+}; }; }; };
+
+#endif
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/oox/source/crypto/AgileEngine.cxx b/oox/source/crypto/AgileEngine.cxx
index c3bd6e16378d..35104903d918 100644
--- a/oox/source/crypto/AgileEngine.cxx
+++ b/oox/source/crypto/AgileEngine.cxx
@@ -30,6 +30,7 @@
 #include <com/sun/star/io/XSeekable.hpp>
 #include <com/sun/star/io/XStream.hpp>
 #include <com/sun/star/io/SequenceInputStream.hpp>
+#include <com/sun/star/io/XSequenceOutputStream.hpp>
 #include <com/sun/star/uno/XComponentContext.hpp>
 #include <com/sun/star/xml/sax/XFastParser.hpp>
 #include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
@@ -47,6 +48,14 @@ using namespace css::xml;
 namespace oox {
 namespace core {
 
+extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
+    com_sun_star_comp_oox_crypto_Agile_get_implementation(
+        XComponentContext* pCtx, Sequence<Any> const& /*arguments*/)
+{
+    return cppu::acquire(new AgileEngine(pCtx/*, arguments*/));
+}
+
+
 namespace {
 
 OUString stripNamespacePrefix(OUString const & rsInputName)
@@ -350,7 +359,7 @@ void AgileEngine::decryptEncryptionKey(OUString const & rPassword)
 }
 
 // TODO: Rename
-bool AgileEngine::generateEncryptionKey(OUString const & rPassword)
+sal_Bool AgileEngine::generateEncryptionKey(OUString const & rPassword)
 {
     bool bResult = decryptAndCheckVerifierHash(rPassword);
 
@@ -414,7 +423,7 @@ bool AgileEngine::decryptHmacValue()
     return true;
 }
 
-bool AgileEngine::checkDataIntegrity()
+sal_Bool AgileEngine::checkDataIntegrity()
 {
     bool bResult = (mInfo.hmacHash.size() == mInfo.hmacCalculatedHash.size() &&
                std::equal(mInfo.hmacHash.begin(), mInfo.hmacHash.end(), mInfo.hmacCalculatedHash.begin()));
@@ -422,11 +431,14 @@ bool AgileEngine::checkDataIntegrity()
     return bResult;
 }
 
-bool AgileEngine::decrypt(BinaryXInputStream& aInputStream,
-                          BinaryXOutputStream& aOutputStream)
+sal_Bool AgileEngine::decrypt(const css::uno::Reference<css::io::XInputStream>&  rxInputStream,
+    css::uno::Reference<css::io::XOutputStream>& rxOutputStream)
 {
     CryptoHash aCryptoHash(mInfo.hmacKey, cryptoHashTypeFromString(mInfo.hashAlgorithm));
 
+    BinaryXInputStream aInputStream(rxInputStream, true);
+    BinaryXOutputStream aOutputStream(rxOutputStream, true);
+
     sal_uInt32 totalSize = aInputStream.readuInt32(); // Document unencrypted size - 4 bytes
     // account for size in HMAC
     std::vector<sal_uInt8> aSizeBytes(sizeof(sal_uInt32));
@@ -484,10 +496,12 @@ bool AgileEngine::decrypt(BinaryXInputStream& aInputStream,
 
     mInfo.hmacCalculatedHash = aCryptoHash.finalize();
 
+    rxOutputStream->flush();
+
     return true;
 }
 
-uno::Reference<io::XInputStream> AgileEngine::getStream(Sequence<NamedValue> & rStreams, const OUString sStreamName)
+uno::Reference<io::XInputStream> AgileEngine::getStream(const Sequence<NamedValue> & rStreams, const OUString sStreamName)
 {
     for (const auto & aStream : rStreams)
     {
@@ -502,7 +516,7 @@ uno::Reference<io::XInputStream> AgileEngine::getStream(Sequence<NamedValue> & r
     return nullptr;
 }
 
-bool AgileEngine::readEncryptionInfo(Sequence<NamedValue> aStreams)
+sal_Bool AgileEngine::readEncryptionInfo(const Sequence<NamedValue>& aStreams)
 {
     uno::Reference<io::XInputStream> xEncryptionInfo = getStream(aStreams, "EncryptionInfo");
 
@@ -682,7 +696,7 @@ bool AgileEngine::encryptEncryptionKey(OUString const & rPassword)
     return true;
 }
 
-bool AgileEngine::setupEncryption(css::uno::Sequence<css::beans::NamedValue>& rMediaEncData)
+sal_Bool AgileEngine::setupEncryption(const css::uno::Sequence<css::beans::NamedValue>& rMediaEncData)
 {
     if (meEncryptionPreset == AgileEncryptionPreset::AES_128_SHA1)
         setupEncryptionParameters({ 100000, 16, 128, 20, 16, OUString("AES"), OUString("ChainingModeCBC"), OUString("SHA1") });
@@ -692,7 +706,7 @@ bool AgileEngine::setupEncryption(css::uno::Sequence<css::beans::NamedValue>& rM
     OUString sPassword;
     for (int i = 0; i < rMediaEncData.getLength(); i++)
     {
-        if (rMediaEncData[i].Name == "Password")
+        if (rMediaEncData[i].Name == "OOXPassword")
         {
             OUString sCryptoType;
             rMediaEncData[i].Value >>= sPassword;
@@ -702,9 +716,13 @@ bool AgileEngine::setupEncryption(css::uno::Sequence<css::beans::NamedValue>& rM
     return setupEncryptionKey(sPassword);
 }
 
-void AgileEngine::createEncryptionData(comphelper::SequenceAsHashMap & aEncryptionData, const OUString rPassword)
+uno::Sequence<beans::NamedValue> AgileEngine::createEncryptionData(const OUString & rPassword)
 {
+    comphelper::SequenceAsHashMap aEncryptionData;
     aEncryptionData["OOXPassword"] <<= rPassword;
+    aEncryptionData["CryptoType"] <<= OUString("AgileEngine");
+
+    return aEncryptionData.getAsConstNamedValueList();
 }
 
 void AgileEngine::setupEncryptionParameters(AgileEncryptionParameters const & rAgileEncryptionParameters)
@@ -738,10 +756,12 @@ bool AgileEngine::setupEncryptionKey(OUString const & rPassword)
     return true;
 }
 
-void AgileEngine::writeEncryptionInfo(oox::ole::OleStorage& rOleStorage)
+css::uno::Sequence<css::beans::NamedValue> AgileEngine::writeEncryptionInfo()
 {
-    Reference<XOutputStream> xEncryptionInfo(rOleStorage.openOutputStream("EncryptionInfo"), UNO_SET_THROW);
-    BinaryXOutputStream rStream(xEncryptionInfo, false);
+    Reference<XOutputStream> aEncryptionInfoStream(
+        mxContext->getServiceManager()->createInstanceWithContext("com.sun.star.io.SequenceOutputStream", mxContext),
+        UNO_QUERY);
+    BinaryXOutputStream rStream(aEncryptionInfoStream, false);
 
     rStream.WriteUInt32(msfilter::VERSION_INFO_AGILE);
     rStream.WriteUInt32(msfilter::AGILE_ENCRYPTION_RESERVED);
@@ -798,21 +818,28 @@ void AgileEngine::writeEncryptionInfo(oox::ole::OleStorage& rOleStorage)
     rStream.writeMemory(aMemStream.GetData(), aMemStream.GetSize());
 
     rStream.close();
-    xEncryptionInfo->flush();
-    xEncryptionInfo->closeOutput();
+    aEncryptionInfoStream->flush();
+
+    // Store all streams into sequence and return back
+    comphelper::SequenceAsHashMap aStreams;
+
+    Reference<XSequenceOutputStream> aEncryptionInfoSequenceStream(aEncryptionInfoStream, UNO_QUERY);
+    aStreams["EncryptionInfo"] <<= aEncryptionInfoSequenceStream->getWrittenBytes();
+    return aStreams.getAsConstNamedValueList();
 }
 
-void AgileEngine::encrypt(css::uno::Reference<css::io::XInputStream> &  rxInputStream,
-                          css::uno::Reference<css::io::XOutputStream> & rxOutputStream,
-                          sal_uInt32 nSize)
+void AgileEngine::encrypt(const css::uno::Reference<css::io::XInputStream> &  rxInputStream,
+                          css::uno::Reference<css::io::XOutputStream> & rxOutputStream)
 {
     CryptoHash aCryptoHash(mInfo.hmacKey, cryptoHashTypeFromString(mInfo.hashAlgorithm));
 
     BinaryXOutputStream aBinaryOutputStream(rxOutputStream, false);
     BinaryXInputStream aBinaryInputStream(rxInputStream, false);
+    Reference<XSeekable> xSeekable(rxInputStream, UNO_QUERY);
+    sal_uInt32 nLength = xSeekable->getLength();
 
     std::vector<sal_uInt8> aSizeBytes(sizeof(sal_uInt32));
-    ByteOrderConverter::writeLittleEndian(aSizeBytes.data(), nSize);
+    ByteOrderConverter::writeLittleEndian(aSizeBytes.data(), nLength);
     aBinaryOutputStream.writeMemory(aSizeBytes.data(), aSizeBytes.size()); // size
     aCryptoHash.update(aSizeBytes, aSizeBytes.size());
 
diff --git a/oox/source/crypto/DocumentDecryption.cxx b/oox/source/crypto/DocumentDecryption.cxx
index 7d9427a5bb41..ea3ed1123532 100644
--- a/oox/source/crypto/DocumentDecryption.cxx
+++ b/oox/source/crypto/DocumentDecryption.cxx
@@ -14,14 +14,12 @@
 #include <cppuhelper/implbase.hxx>
 
 #include <com/sun/star/beans/NamedValue.hpp>
+#include <com/sun/star/io/XSeekable.hpp>
 #include <com/sun/star/io/XStream.hpp>
 #include <com/sun/star/uno/XComponentContext.hpp>
-#include <oox/crypto/AgileEngine.hxx>
-#include <oox/crypto/Standard2007Engine.hxx>
-#include <oox/crypto/IRMEngine.hxx>
-#include <oox/helper/binaryinputstream.hxx>
-#include <oox/helper/binaryoutputstream.hxx>
+#include <com/sun/star/packages/XPackageEncryption.hpp>
 #include <oox/ole/olestorage.hxx>
+#include <filter/msfilter/mscodec.hxx>
 
 namespace {
 
@@ -81,8 +79,8 @@ DocumentDecryption::DocumentDecryption(const css::uno::Reference< css::uno::XCom
 
 bool DocumentDecryption::generateEncryptionKey(const OUString& rPassword)
 {
-    if (mEngine)
-        return mEngine->generateEncryptionKey(rPassword);
+    if (mxPackageEncryption.is())
+        return mxPackageEncryption->generateEncryptionKey(rPassword);
     return false;
 }
 
@@ -93,16 +91,20 @@ void DocumentDecryption::readStrongEncryptionInfo()
     BinaryXInputStream aBinaryInputStream(xEncryptionInfo, true);
     sal_uInt32 aVersion = aBinaryInputStream.readuInt32();
 
+    uno::Sequence< uno::Any > aArguments;
+
     switch (aVersion)
     {
     case msfilter::VERSION_INFO_2007_FORMAT:
     case msfilter::VERSION_INFO_2007_FORMAT_SP2:
-        msEngineName = "Standard"; // Set encryption info format
-        mEngine.reset(new Standard2007Engine(mxContext));
+        mxPackageEncryption.set(
+            mxContext->getServiceManager()->createInstanceWithArgumentsAndContext(
+                "com.sun.star.comp.oox.crypto.Standard2007Engine", aArguments, mxContext), css::uno::UNO_QUERY);
         break;
     case msfilter::VERSION_INFO_AGILE:
-        msEngineName = "Agile"; // Set encryption info format
-        mEngine.reset(new AgileEngine(mxContext));
+        mxPackageEncryption.set(
+            mxContext->getServiceManager()->createInstanceWithArgumentsAndContext(
+                "com.sun.star.comp.oox.crypto.AgileEngine", aArguments, mxContext), css::uno::UNO_QUERY);
         break;
     default:
         break;
@@ -146,23 +148,15 @@ bool DocumentDecryption::readEncryptionInfo()
             aDataSpaceStream.skip((4 - (aDataSpaceNameLength & 3)) & 3);  // Skip padding
         }
 
-        if (sDataSpaceName == "DRMEncryptedDataSpace")
-        {
-            msEngineName = "IRM"; // Set encryption info format
-            mEngine.reset(new IRMEngine(mxContext));
-        }
-        else if (sDataSpaceName == "\011DRMDataSpace") // 0x09DRMDataSpace
-        {
-            // TODO: IRM binary file
-        }
-        else if (sDataSpaceName == "StrongEncryptionDataSpace")
+        uno::Sequence< uno::Any > aArguments;
+        mxPackageEncryption.set(
+            mxContext->getServiceManager()->createInstanceWithArgumentsAndContext(
+                "com.sun.star.comp.oox.crypto." + sDataSpaceName, aArguments, mxContext), css::uno::UNO_QUERY);
+
+        if (!mxPackageEncryption.is() && sDataSpaceName == "StrongEncryptionDataSpace")
         {
             readStrongEncryptionInfo();
         }
-        else
-        {
-            SAL_WARN("oox", "Unknown dataspace - document will be not decrypted!");
-        }
     }
     else
     {
@@ -172,20 +166,21 @@ bool DocumentDecryption::readEncryptionInfo()
         readStrongEncryptionInfo();
     }
 
-    if (!mEngine)
+    if (!mxPackageEncryption.is())
+    {
+        // we do not know how to decrypt this document
         return false;
+    }
 
-    return mEngine->readEncryptionInfo(maStreamsSequence);
+    return mxPackageEncryption->readEncryptionInfo(maStreamsSequence);
 }
 
 uno::Sequence<beans::NamedValue> DocumentDecryption::createEncryptionData(const OUString& rPassword)
 {
-    comphelper::SequenceAsHashMap aEncryptionData;
+    if (!mxPackageEncryption.is())
+        return uno::Sequence<beans::NamedValue>();
 
-    aEncryptionData["CryptoType"] <<= msEngineName;
-    mEngine->createEncryptionData(aEncryptionData, rPassword);
-
-    return aEncryptionData.getAsConstNamedValueList();
+    return mxPackageEncryption->createEncryptionData(rPassword);
 }
 
 bool DocumentDecryption::decrypt(const uno::Reference<io::XStream>& xDocumentStream)
@@ -195,21 +190,22 @@ bool DocumentDecryption::decrypt(const uno::Reference<io::XStream>& xDocumentStr
     if (!mrOleStorage.isStorage())
         return false;
 
+    if (!mxPackageEncryption.is())
+        return false;
+
     // open the required input streams in the encrypted package
     uno::Reference<io::XInputStream> xEncryptedPackage = mrOleStorage.openInputStream("EncryptedPackage");
 
     // create temporary file for unencrypted package
     uno::Reference<io::XOutputStream> xDecryptedPackage = xDocumentStream->getOutputStream();
-    BinaryXOutputStream aDecryptedPackage(xDecryptedPackage, true);
-    BinaryXInputStream aEncryptedPackage(xEncryptedPackage, true);
 
-    bResult = mEngine->decrypt(aEncryptedPackage, aDecryptedPackage);
+    bResult = mxPackageEncryption->decrypt(xEncryptedPackage, xDecryptedPackage);
 
-    xDecryptedPackage->flush();
-    aDecryptedPackage.seekToStart();
+    css::uno::Reference<io::XSeekable> xSeekable(xDecryptedPackage, css::uno::UNO_QUERY);
+    xSeekable->seek(0);
 
     if (bResult)
-        return mEngine->checkDataIntegrity();
+        return mxPackageEncryption->checkDataIntegrity();
 
     return bResult;
 }
diff --git a/oox/source/crypto/DocumentEncryption.cxx b/oox/source/crypto/DocumentEncryption.cxx
index 8aac457c67d2..2dba0f035df5 100644
--- a/oox/source/crypto/DocumentEncryption.cxx
+++ b/oox/source/crypto/DocumentEncryption.cxx
@@ -9,16 +9,13 @@
  */
 
 #include <oox/crypto/DocumentEncryption.hxx>
-#include <oox/crypto/Standard2007Engine.hxx>
-#include <oox/crypto/IRMEngine.hxx>
 
 #include <com/sun/star/io/XInputStream.hpp>
 #include <com/sun/star/io/XOutputStream.hpp>
 #include <com/sun/star/io/XStream.hpp>
 #include <com/sun/star/io/XSeekable.hpp>
+#include <com/sun/star/packages/XPackageEncryption.hpp>
 
-#include <oox/helper/binaryinputstream.hxx>
-#include <oox/helper/binaryoutputstream.hxx>
 #include <oox/ole/olestorage.hxx>
 
 namespace oox {
@@ -26,11 +23,12 @@ namespace core {
 
 using namespace css::io;
 using namespace css::uno;
+using namespace css::beans;
 
-DocumentEncryption::DocumentEncryption(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
+DocumentEncryption::DocumentEncryption(const Reference< XComponentContext >& rxContext,
                                        Reference<XStream> const & xDocumentStream,
                                        oox::ole::OleStorage& rOleStorage,
-                                       Sequence<css::beans::NamedValue>& rMediaEncData)
+                                       Sequence<NamedValue>& rMediaEncData)
     : mxContext(rxContext)
     , mxDocumentStream(xDocumentStream)
     , mrOleStorage(rOleStorage)
@@ -43,25 +41,28 @@ DocumentEncryption::DocumentEncryption(const css::uno::Reference< css::uno::XCom
         {
             OUString sCryptoType;
             rMediaEncData[i].Value >>= sCryptoType;
-            if (sCryptoType == "IRM")
-            {
-                mEngine.reset(new IRMEngine(mxContext));
-            }
-            else if (sCryptoType == "Standard" || sCryptoType == "Agile")
-            {
-                mEngine.reset(new Standard2007Engine(mxContext));
-            }
-            else
+
+            if (sCryptoType == "Standard")
+                sCryptoType = "Standard2007Engine";
+
+            Sequence<Any> aArguments;
+            mxPackageEncryption.set(
+                mxContext->getServiceManager()->createInstanceWithArgumentsAndContext(
+                    "com.sun.star.comp.oox.crypto." + sCryptoType, aArguments, mxContext), css::uno::UNO_QUERY);
+
+            if (!mxPackageEncryption.is())
             {
                 SAL_WARN("oox", "Requested encryption method \"" << sCryptoType << "\" is not supported");
             }
+
+            break;
         }
     }
 }
 
 bool DocumentEncryption::encrypt()
 {
-    if (!mEngine)
+    if (!mxPackageEncryption.is())
         return false;
 
     Reference<XInputStream> xInputStream (mxDocumentStream->getInputStream(), UNO_SET_THROW);
@@ -70,20 +71,32 @@ bool DocumentEncryption::encrypt()
     if (!xSeekable.is())
         return false;
 
-    sal_uInt32 aLength = xSeekable->getLength(); // check length of the stream
     xSeekable->seek(0); // seek to begin of the document stream
 
     if (!mrOleStorage.isStorage())
         return false;
 
-    mEngine->setupEncryption(mMediaEncData);
+    mxPackageEncryption->setupEncryption(mMediaEncData);
 
     Reference<XOutputStream> xOutputStream(mrOleStorage.openOutputStream("EncryptedPackage"), UNO_SET_THROW);
-    mEngine->encrypt(xInputStream, xOutputStream, aLength);
+    mxPackageEncryption->encrypt(xInputStream, xOutputStream);
     xOutputStream->flush();
     xOutputStream->closeOutput();
 
-    mEngine->writeEncryptionInfo(mrOleStorage);
+    Sequence<NamedValue> aStreams = mxPackageEncryption->writeEncryptionInfo();
+
+    for (const NamedValue & aStream : aStreams)
+    {
+        Reference<XOutputStream> xOutputStream(mrOleStorage.openOutputStream(aStream.Name), UNO_SET_THROW);
+        BinaryXOutputStream aBinaryOutputStream(xOutputStream, true);
+
+        css::uno::Sequence<sal_Int8> aStreamSequence;
+        aStream.Value >>= aStreamSequence;
+
+        aBinaryOutputStream.writeData(aStreamSequence);
+
+        aBinaryOutputStream.close();
+    }
 
     return true;
 }
diff --git a/oox/source/crypto/IRMEngine.cxx b/oox/source/crypto/IRMEngine.cxx
index 1301a3b51279..95135722ee08 100644
--- a/oox/source/crypto/IRMEngine.cxx
+++ b/oox/source/crypto/IRMEngine.cxx
@@ -30,6 +30,7 @@
 #include <com/sun/star/io/XSeekable.hpp>
 #include <com/sun/star/io/XStream.hpp>
 #include <com/sun/star/io/SequenceInputStream.hpp>
+#include <com/sun/star/io/XSequenceOutputStream.hpp>
 #include <com/sun/star/uno/XComponentContext.hpp>
 #include <com/sun/star/xml/sax/XFastParser.hpp>
 #include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
@@ -50,15 +51,26 @@ namespace oox
 {
 namespace core
 {
+extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
+com_sun_star_comp_oox_crypto_DRMEncryptedDataSpace_get_implementation(
+    XComponentContext* pCtx, Sequence<Any> const& /*arguments*/)
+{
+    return cppu::acquire(new IRMEngine(pCtx /*, arguments*/));
+}
+
 IRMEngine::IRMEngine(const Reference<XComponentContext>& rxContext)
     : mxContext(rxContext)
 {
 }
 
-bool IRMEngine::checkDataIntegrity() { return true; }
+sal_Bool IRMEngine::checkDataIntegrity() { return true; }
 
-bool IRMEngine::decrypt(BinaryXInputStream& aInputStream, BinaryXOutputStream& aOutputStream)
+sal_Bool IRMEngine::decrypt(const Reference<XInputStream>& rxInputStream,
+                            Reference<XOutputStream>& rxOutputStream)
 {
+    BinaryXInputStream aInputStream(rxInputStream, true);
+    BinaryXOutputStream aOutputStream(rxOutputStream, true);
+
     aInputStream.readInt64(); // Skip stream size
 
     HRESULT hr = IpcInitialize();
@@ -66,6 +78,7 @@ bool IRMEngine::decrypt(BinaryXInputStream& aInputStream, BinaryXOutputStream& a
     {
         // ERROR_ALREADY_INITIALIZED not an error
         // TODO: some reaction?
+        return false;
     }
 
     // Get decryption key
@@ -77,6 +90,7 @@ bool IRMEngine::decrypt(BinaryXInputStream& aInputStream, BinaryXOutputStream& a
     if (FAILED(hr))
     {
         // TODO: some reaction?
+        return false;
     }
 
     // Read rights
@@ -85,6 +99,7 @@ bool IRMEngine::decrypt(BinaryXInputStream& aInputStream, BinaryXOutputStream& a
     if (FAILED(hr))
     {
         // TODO: some reaction?
+        return false;
     }
     mInfo.bCanRead = value;
 
@@ -94,6 +109,7 @@ bool IRMEngine::decrypt(BinaryXInputStream& aInputStream, BinaryXOutputStream& a
     if (FAILED(hr))
     {
         // TODO: some reaction?
+        return false;
     }
 
     char* pEncryptedBuffer = new char[*blockSize];
@@ -112,6 +128,7 @@ bool IRMEngine::decrypt(BinaryXInputStream& aInputStream, BinaryXOutputStream& a
         if (FAILED(hr))
         {
             // TODO: some reaction?
+            return false;
         }
 
         aOutputStream.writeArray(pDecryptedBuffer, bytes);
@@ -122,22 +139,26 @@ bool IRMEngine::decrypt(BinaryXInputStream& aInputStream, BinaryXOutputStream& a
     delete[] pEncryptedBuffer;
     delete[] pDecryptedBuffer;
 
+    rxOutputStream->flush();
+
     return true;
 }
 
-void IRMEngine::createEncryptionData(comphelper::SequenceAsHashMap& aEncryptionData,
-                                     const OUString rPassword)
+uno::Sequence<beans::NamedValue> IRMEngine::createEncryptionData(const OUString& /*rPassword*/)
 {
-    aEncryptionData["OOXPassword"] <<= rPassword;
-
     css::uno::Sequence<sal_uInt8> seq;
     seq.realloc(mInfo.license.getLength());
     memcpy(seq.getArray(), mInfo.license.getStr(), mInfo.license.getLength());
 
-    aEncryptionData["license"] <<= seq;
+    comphelper::SequenceAsHashMap aEncryptionData;
+    aEncryptionData["LicenseKey"] <<= seq;
+    aEncryptionData["CryptoType"] <<= OUString("DRMEncryptedDataSpace");
+    aEncryptionData["OOXPassword"] <<= OUString("1");
+
+    return aEncryptionData.getAsConstNamedValueList();
 }
 
-uno::Reference<io::XInputStream> IRMEngine::getStream(Sequence<NamedValue>& rStreams,
+uno::Reference<io::XInputStream> IRMEngine::getStream(const Sequence<NamedValue>& rStreams,
                                                       const OUString sStreamName)
 {
     for (const auto& aStream : rStreams)
@@ -155,7 +176,7 @@ uno::Reference<io::XInputStream> IRMEngine::getStream(Sequence<NamedValue>& rStr
     return nullptr;
 }
 
-bool IRMEngine::readEncryptionInfo(uno::Sequence<beans::NamedValue> aStreams)
+sal_Bool IRMEngine::readEncryptionInfo(const uno::Sequence<beans::NamedValue>& aStreams)
 {
     // Read TransformInfo storage for IRM ECMA documents (MS-OFFCRYPTO 2.2.4)
     uno::Reference<io::XInputStream> xTransformInfoStream
@@ -198,11 +219,11 @@ bool IRMEngine::readEncryptionInfo(uno::Sequence<beans::NamedValue> aStreams)
     return true;
 }
 
-bool IRMEngine::setupEncryption(css::uno::Sequence<css::beans::NamedValue>& rMediaEncData)
+sal_Bool IRMEngine::setupEncryption(const Sequence<NamedValue>& rMediaEncData)
 {
     for (int i = 0; i < rMediaEncData.getLength(); i++)
     {
-        if (rMediaEncData[i].Name == "license")
+        if (rMediaEncData[i].Name == "LicenseKey")
         {
             css::uno::Sequence<sal_uInt8> seq;
             rMediaEncData[i].Value >>= seq;
@@ -213,11 +234,13 @@ bool IRMEngine::setupEncryption(css::uno::Sequence<css::beans::NamedValue>& rMed
     return true;
 }
 
-void IRMEngine::writeEncryptionInfo(oox::ole::OleStorage& rOleStorage)
+Sequence<NamedValue> IRMEngine::writeEncryptionInfo()
 {
     // Write 0x6DataSpaces/DataSpaceMap
     Reference<XOutputStream> xDataSpaceMap(
-        rOleStorage.openOutputStream("\006DataSpaces/DataSpaceMap"), UNO_SET_THROW);
+        mxContext->getServiceManager()->createInstanceWithContext(
+            "com.sun.star.io.SequenceOutputStream", mxContext),
+        UNO_QUERY);
     BinaryXOutputStream aDataSpaceMapStream(xDataSpaceMap, false);
 
     aDataSpaceMapStream.WriteInt32(8); // Header length
@@ -227,42 +250,37 @@ void IRMEngine::writeEncryptionInfo(oox::ole::OleStorage& rOleStorage)
     OUString sDataSpaceName("DRMEncryptedDataSpace");
     OUString sReferenceComponent("EncryptedPackage");
 
-    aDataSpaceMapStream.WriteInt32(0x58); // Length
+    aDataSpaceMapStream.WriteInt32(0x60); // Length
     aDataSpaceMapStream.WriteInt32(1); // References count
     aDataSpaceMapStream.WriteInt32(0); // References component type
 
     aDataSpaceMapStream.WriteInt32(sReferenceComponent.getLength() * 2);
     aDataSpaceMapStream.writeUnicodeArray(sReferenceComponent);
-    while (aDataSpaceMapStream.tell() % 4) // Padding
+    for (int i = 0; i < sReferenceComponent.getLength() * 2 % 4; i++) // Padding
     {
         aDataSpaceMapStream.writeValue<sal_Char>(0);
     }
 
     aDataSpaceMapStream.WriteInt32(sDataSpaceName.getLength() * 2);
     aDataSpaceMapStream.writeUnicodeArray(sDataSpaceName);
-    while (aDataSpaceMapStream.tell() % 4) // Padding
+    for (int i = 0; i < sDataSpaceName.getLength() * 2 % 4; i++) // Padding
     {
         aDataSpaceMapStream.writeValue<sal_Char>(0);
     }
 
-    // Write length
-    sal_uInt32 nLength = aDataSpaceMapStream.tell() - 8;
-    aDataSpaceMapStream.seek(8);
-    aDataSpaceMapStream.WriteInt32(nLength);
-
     aDataSpaceMapStream.close();
     xDataSpaceMap->flush();
-    xDataSpaceMap->closeOutput();
 
     // Write 0x6DataSpaces/Version
-    Reference<XOutputStream> xVersion(rOleStorage.openOutputStream("\006DataSpaces/Version"),
-                                      UNO_SET_THROW);
+    Reference<XOutputStream> xVersion(mxContext->getServiceManager()->createInstanceWithContext(
+                                          "com.sun.star.io.SequenceOutputStream", mxContext),
+                                      UNO_QUERY);
     BinaryXOutputStream aVersionStream(xVersion, false);
 
     OUString sFeatureIdentifier("Microsoft.Container.DataSpaces");
     aVersionStream.WriteInt32(sFeatureIdentifier.getLength() * 2);
     aVersionStream.writeUnicodeArray(sFeatureIdentifier);
-    while (aVersionStream.tell() % 4) // Padding
+    for (int i = 0; i < sFeatureIdentifier.getLength() * 2 % 4; i++) // Padding
     {
         aVersionStream.writeValue<sal_Char>(0);
     }
@@ -273,59 +291,55 @@ void IRMEngine::writeEncryptionInfo(oox::ole::OleStorage& rOleStorage)
 
     aVersionStream.close();
     xVersion->flush();
-    xVersion->closeOutput();
 
     // Write 0x6DataSpaces/DataSpaceInfo/[dataspacename]
-    OUString sStreamName = "\006DataSpaces/DataSpaceInfo/" + sDataSpaceName;
-    Reference<XOutputStream> xDataSpaceInfo(rOleStorage.openOutputStream(sStreamName),
-                                            UNO_SET_THROW);
+    Reference<XOutputStream> xDataSpaceInfo(
+        mxContext->getServiceManager()->createInstanceWithContext(
+            "com.sun.star.io.SequenceOutputStream", mxContext),
+        UNO_QUERY);
     BinaryXOutputStream aDataSpaceInfoStream(xDataSpaceInfo, false);
 
-    aDataSpaceInfoStream.WriteInt32(8); // Header length
+    aDataSpaceInfoStream.WriteInt32(0x08); // Header length
     aDataSpaceInfoStream.WriteInt32(1); // Entries count
 
     OUString sTransformName("DRMEncryptedTransform");
     aDataSpaceInfoStream.WriteInt32(sTransformName.getLength() * 2);
     aDataSpaceInfoStream.writeUnicodeArray(sTransformName);
-    while (aDataSpaceInfoStream.tell() % 4) // Padding
+    for (int i = 0; i < sTransformName.getLength() * 2 % 4; i++) // Padding
     {
         aDataSpaceInfoStream.writeValue<sal_Char>(0);
     }
 
     aDataSpaceInfoStream.close();
     xDataSpaceInfo->flush();
-    xDataSpaceInfo->closeOutput();
 
     // Write 0x6DataSpaces/TransformInfo/[transformname]
-    sStreamName = "\006DataSpaces/TransformInfo/" + sTransformName + "/\006Primary";
-    Reference<XOutputStream> xTransformInfo(rOleStorage.openOutputStream(sStreamName),
-                                            UNO_SET_THROW);
+    Reference<XOutputStream> xTransformInfo(
+        mxContext->getServiceManager()->createInstanceWithContext(
+            "com.sun.star.io.SequenceOutputStream", mxContext),
+        UNO_QUERY);
     BinaryXOutputStream aTransformInfoStream(xTransformInfo, false);
+    OUString sTransformId("{C73DFACD-061F-43B0-8B64-0C620D2A8B50}");
 
     // MS-OFFCRYPTO 2.1.8: TransformInfoHeader
-    aTransformInfoStream.WriteInt32(0); // TransformLength, will be written later
+    sal_uInt32 nLength
+        = sTransformId.getLength() * 2 + ((4 - (sTransformId.getLength() & 3)) & 3) + 10;
+    aTransformInfoStream.WriteInt32(nLength); // TransformLength, will be written later
     aTransformInfoStream.WriteInt32(1); // TransformType
 
     // TransformId
-    OUString sTransformId("{C73DFACD-061F-43B0-8B64-0C620D2A8B50}");
     aTransformInfoStream.WriteInt32(sTransformId.getLength() * 2);
     aTransformInfoStream.writeUnicodeArray(sTransformId);
-    while (aTransformInfoStream.tell() % 4) // Padding
+    for (int i = 0; i < sTransformId.getLength() * 2 % 4; i++) // Padding
     {
         aTransformInfoStream.writeValue<sal_Char>(0);
     }
 
-    // Calculate length and write it into beginning
-    nLength = aTransformInfoStream.tell();
-    aTransformInfoStream.seek(0);
-    aTransformInfoStream.WriteInt32(nLength);
-    aTransformInfoStream.seek(nLength);
-
     // TransformName
     OUString sTransformInfoName("Microsoft.Metadata.DRMTransform");
     aTransformInfoStream.WriteInt32(sTransformInfoName.getLength() * 2);
     aTransformInfoStream.writeUnicodeArray(sTransformInfoName);
-    while (aTransformInfoStream.tell() % 4) // Padding
+    for (int i = 0; i < sTransformInfoName.getLength() * 2 % 4; i++) // Padding
     {
         aTransformInfoStream.writeValue<sal_Char>(0);
     }
@@ -336,19 +350,36 @@ void IRMEngine::writeEncryptionInfo(oox::ole::OleStorage& rOleStorage)
 
     aTransformInfoStream.WriteInt32(4); // Extensibility Header
 
-    aTransformInfoStream.WriteInt32(mInfo.license.getLength() - 3); // LicenseLength
+    aTransformInfoStream.WriteInt32(mInfo.license.getLength() - 3); // LicenseLength - BOM
     aTransformInfoStream.writeArray<sal_Char>(mInfo.license.getStr() + 3,
                                               mInfo.license.getLength() - 3);
     aTransformInfoStream.writeValue<sal_Char>(0);
 
     aTransformInfoStream.close();
     xTransformInfo->flush();
-    xTransformInfo->closeOutput();
+
+    // Store all streams into sequence and return back
+    comphelper::SequenceAsHashMap aStreams;
+
+    Reference<XSequenceOutputStream> xDataSpaceMapSequence(xDataSpaceMap, UNO_QUERY);
+    aStreams["\006DataSpaces/DataSpaceMap"] <<= xDataSpaceMapSequence->getWrittenBytes();
+
+    Reference<XSequenceOutputStream> xVersionSequence(xVersion, UNO_QUERY);
+    aStreams["\006DataSpaces/Version"] <<= xVersionSequence->getWrittenBytes();
+
+    OUString sStreamName = "\006DataSpaces/DataSpaceInfo/" + sDataSpaceName;
+    Reference<XSequenceOutputStream> xDataSpaceInfoSequence(xDataSpaceInfo, UNO_QUERY);
+    aStreams[sStreamName] <<= xDataSpaceInfoSequence->getWrittenBytes();
+
+    sStreamName = "\006DataSpaces/TransformInfo/" + sTransformName + "/\006Primary";
+    Reference<XSequenceOutputStream> xTransformInfoSequence(xTransformInfo, UNO_QUERY);
+    aStreams[sStreamName] <<= xTransformInfoSequence->getWrittenBytes();
+
+    return aStreams.getAsConstNamedValueList();
 }
 
-void IRMEngine::encrypt(css::uno::Reference<css::io::XInputStream>& rxInputStream,
-                        css::uno::Reference<css::io::XOutputStream>& rxOutputStream,
-                        sal_uInt32 /*nSize*/)
+void IRMEngine::encrypt(const Reference<XInputStream>& rxInputStream,

... etc. - the rest is truncated


More information about the Libreoffice-commits mailing list