tdf#160888 package: fix opening password protected scripting library

The problem is that XBufferedThreadedStream does not implement
XSeekable, so the new code in ZipFile::checkValidPassword() throws an
exception, and then joining the UnzippingThread hangs.

Implementing XSeekable doesn't appear to help, as the mutex that
is used by XBufferedThreadedStream and the UnzippingThread is already
locked by checkValidPassword() [fixably] and by getDataStream().

So just disable threading for AEAD streams, these are read immediately
anyway so threading isn't much of a benefit.

(regression from commit 2f512aaa6c39390a5a0eb1d1e37f070127d068a4)

Change-Id: I16027d5b03ba6e102bc143c22383eb7f08590e5f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/168893
Tested-by: Jenkins
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
(cherry picked from commit 31698044cd1fe7a7662740b97ea58f9904b3bb0e)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/168946
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
diff --git a/package/source/zipapi/ZipFile.cxx b/package/source/zipapi/ZipFile.cxx
index 3d382bd..72788b6 100644
--- a/package/source/zipapi/ZipFile.cxx
+++ b/package/source/zipapi/ZipFile.cxx
@@ -557,8 +557,6 @@ uno::Reference<io::XInputStream> ZipFile::checkValidPassword(
    ZipEntry const& rEntry, ::rtl::Reference<EncryptionData> const& rData,
    rtl::Reference<comphelper::RefCountedMutex> const& rMutex)
{
    ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );

    if (rData.is() && rData->m_nEncAlg == xml::crypto::CipherID::AES_GCM_W3C)
    {
        try // the only way to find out: decrypt the whole stream, which will
@@ -579,6 +577,8 @@ uno::Reference<io::XInputStream> ZipFile::checkValidPassword(
    }
    else if (rData.is() && rData->m_aKey.hasElements())
    {
        ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );

        css::uno::Reference < css::io::XSeekable > xSeek(xStream, UNO_QUERY_THROW);
        xSeek->seek( rEntry.nOffset );
        sal_Int64 nSize = rEntry.nMethod == DEFLATED ? rEntry.nCompressedSize : rEntry.nSize;
@@ -733,8 +733,18 @@ uno::Reference< XInputStream > ZipFile::createStreamForZipEntry(
    static const sal_Int32 nThreadingThreshold = 10000;

    // "encrypted-package" is the only data stream, no point in threading it
    if (rEntry.sPath != "encrypted-package" && nThreadingThreshold < xSrcStream->available())
    if (nThreadingThreshold < xSrcStream->available()
        && rEntry.sPath != "encrypted-package"
        // tdf#160888 no threading for AEAD streams:
        // 1. the whole stream must be read immediately to verify tag
        // 2. XBufferedThreadedStream uses same m_aMutexHolder->GetMutex()
        //    => caller cannot read without deadlock
        && (nStreamMode != UNBUFF_STREAM_DATA
            || !rData.is()
            || rData->m_nEncAlg != xml::crypto::CipherID::AES_GCM_W3C))
    {
        xBufStream = new XBufferedThreadedStream(xSrcStream, xSrcStream->getSize());
    }
    else
#endif
        xBufStream = new XBufferedStream(xSrcStream);