diff options
author | Michael Stahl <mstahl@redhat.com> | 2018-01-12 16:58:00 +0100 |
---|---|---|
committer | Michael Stahl <mstahl@redhat.com> | 2018-01-12 23:31:43 +0100 |
commit | 9188ea83c346fdc2f668178ae7538665a1b09c02 (patch) | |
tree | 2919ed5dd5ea276dd0395c7596a4efe0e11f46e8 /package/source/zipapi/sha1context.cxx | |
parent | 64592a19e5d512fb5cd09bf0a1726b9c78481e65 (diff) |
tdf#114939 package,comphelper: Try both real SHA1 and StarOffice SHA1
... when importing ODF documents.
In CreatePackageEncryptionData(), add a 3rd SHA1 password hash,
PackageSHA1CorrectEncryptionKey, to EncryptionData.
Use it in ZipPackageStream::getDataStream(), which has 3 fall-backs
for SHA1 bugs now.
Also add a CorrectSHA1DigestContext, to be used together with
PackageSHA1CorrectEncryptionKey, and rename the existing one to
StarOfficeSHA1DigestContext, to be used together with the existing
2 PackageSHA1{UTF8,MS1252}EncryptionKey.
The fallback won't be used very often anyway: for the password SHA1
to be wrong, you need a password between 52 and 55 bytes long,
and for the SHA1/1K checksum to be wrong, you need a file
smaller than 1K with compressed size mod 64 between 52 and 55;
all XML files have enough random "chaff" added to be too large.
Test that we can read both correct SHA1 and StarOffice SHA1.
Change-Id: I988fa489b5e40c7657f404f18538f637d54d28f1
Diffstat (limited to 'package/source/zipapi/sha1context.cxx')
-rw-r--r-- | package/source/zipapi/sha1context.cxx | 52 |
1 files changed, 47 insertions, 5 deletions
diff --git a/package/source/zipapi/sha1context.cxx b/package/source/zipapi/sha1context.cxx index f24064616edb..af3123e2dbd0 100644 --- a/package/source/zipapi/sha1context.cxx +++ b/package/source/zipapi/sha1context.cxx @@ -19,6 +19,7 @@ #include <sal/config.h> +#include <comphelper/hash.hxx> #include <com/sun/star/lang/DisposedException.hpp> #include <rtl/digest.h> #include <rtl/ref.hxx> @@ -28,9 +29,9 @@ using namespace ::com::sun::star; // static -uno::Reference< xml::crypto::XDigestContext > SHA1DigestContext::Create() +uno::Reference<xml::crypto::XDigestContext> StarOfficeSHA1DigestContext::Create() { - ::rtl::Reference< SHA1DigestContext > xResult = new SHA1DigestContext(); + ::rtl::Reference<StarOfficeSHA1DigestContext> xResult = new StarOfficeSHA1DigestContext(); xResult->m_pDigest = rtl_digest_createSHA1(); if ( !xResult->m_pDigest ) throw uno::RuntimeException("Can not create cipher!" ); @@ -38,7 +39,7 @@ uno::Reference< xml::crypto::XDigestContext > SHA1DigestContext::Create() return uno::Reference< xml::crypto::XDigestContext >( xResult.get() ); } -SHA1DigestContext::~SHA1DigestContext() +StarOfficeSHA1DigestContext::~StarOfficeSHA1DigestContext() { if ( m_pDigest ) { @@ -47,7 +48,7 @@ SHA1DigestContext::~SHA1DigestContext() } } -void SAL_CALL SHA1DigestContext::updateDigest( const uno::Sequence< ::sal_Int8 >& aData ) +void SAL_CALL StarOfficeSHA1DigestContext::updateDigest(const uno::Sequence<::sal_Int8>& aData) { ::osl::MutexGuard aGuard( m_aMutex ); if ( !m_pDigest ) @@ -62,7 +63,7 @@ void SAL_CALL SHA1DigestContext::updateDigest( const uno::Sequence< ::sal_Int8 > } } -uno::Sequence< ::sal_Int8 > SAL_CALL SHA1DigestContext::finalizeDigestAndDispose() +uno::Sequence<::sal_Int8> SAL_CALL StarOfficeSHA1DigestContext::finalizeDigestAndDispose() { ::osl::MutexGuard aGuard( m_aMutex ); if ( !m_pDigest ) @@ -83,4 +84,45 @@ uno::Sequence< ::sal_Int8 > SAL_CALL SHA1DigestContext::finalizeDigestAndDispose return aResult; } +uno::Reference<xml::crypto::XDigestContext> CorrectSHA1DigestContext::Create() +{ + return new CorrectSHA1DigestContext(); +} + +struct CorrectSHA1DigestContext::Impl +{ + ::osl::Mutex m_Mutex; + ::comphelper::Hash m_Hash{::comphelper::HashType::SHA1}; + bool m_bDisposed{false}; +}; + +CorrectSHA1DigestContext::CorrectSHA1DigestContext() + : m_pImpl(new Impl) +{ +} + +CorrectSHA1DigestContext::~CorrectSHA1DigestContext() +{ +} + +void SAL_CALL CorrectSHA1DigestContext::updateDigest(const uno::Sequence<::sal_Int8>& rData) +{ + ::osl::MutexGuard aGuard(m_pImpl->m_Mutex); + if (m_pImpl->m_bDisposed) + throw lang::DisposedException(); + + m_pImpl->m_Hash.update(reinterpret_cast<unsigned char const*>(rData.getConstArray()), rData.getLength()); +} + +uno::Sequence<::sal_Int8> SAL_CALL CorrectSHA1DigestContext::finalizeDigestAndDispose() +{ + ::osl::MutexGuard aGuard(m_pImpl->m_Mutex); + if (m_pImpl->m_bDisposed) + throw lang::DisposedException(); + + m_pImpl->m_bDisposed = true; + std::vector<unsigned char> const sha1(m_pImpl->m_Hash.finalize()); + return uno::Sequence<sal_Int8>(reinterpret_cast<sal_Int8 const*>(sha1.data()), sha1.size()); +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |