[Libreoffice-commits] .: Branch 'libreoffice-3-4' - comphelper/inc comphelper/source

Stephan Bergmann sbergmann at kemper.freedesktop.org
Fri Nov 18 06:41:32 PST 2011


 comphelper/inc/comphelper/storagehelper.hxx |    1 
 comphelper/source/misc/storagehelper.cxx    |   37 ++++++++++++++++++++++++----
 2 files changed, 33 insertions(+), 5 deletions(-)

New commits:
commit d87712a52d33ade9fd2821b91caf92d11ef31a93
Author: Stephan Bergmann <sbergman at redhat.com>
Date:   Wed Nov 9 08:38:58 2011 +0100

    Backport reading AES-encrypted ODF 1.2 documents (as genereated by LibO 3.5).
    
    This backports the reading half of CWS mav60 plus <http://cgit.freedesktop.org/
    libreoffice/core/commit/?id=2d775f593abd9bc487ccc60f06aa5a3ca9632056> "Produce
    correct sha256 uri, consume correct uri and original spec typo."  It spans the
    repos components, libs-core, libs-gui, and ure.

diff --git a/comphelper/inc/comphelper/storagehelper.hxx b/comphelper/inc/comphelper/storagehelper.hxx
index 2f83331..807c6dd 100644
--- a/comphelper/inc/comphelper/storagehelper.hxx
+++ b/comphelper/inc/comphelper/storagehelper.hxx
@@ -45,6 +45,7 @@
 #define ZIP_STORAGE_FORMAT_STRING		::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ZipFormat" ) )
 #define OFOPXML_STORAGE_FORMAT_STRING	::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "OFOPXMLFormat" ) )
 
+#define PACKAGE_ENCRYPTIONDATA_SHA256UTF8 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PackageSHA256UTF8EncryptionKey" ) )
 #define PACKAGE_ENCRYPTIONDATA_SHA1UTF8   ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PackageSHA1UTF8EncryptionKey" ) )
 #define PACKAGE_ENCRYPTIONDATA_SHA1MS1252 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PackageSHA1MS1252EncryptionKey" ) )
 
diff --git a/comphelper/source/misc/storagehelper.cxx b/comphelper/source/misc/storagehelper.cxx
index f703cd3..112ddf1 100644
--- a/comphelper/source/misc/storagehelper.cxx
+++ b/comphelper/source/misc/storagehelper.cxx
@@ -35,6 +35,9 @@
 #include <com/sun/star/beans/PropertyValue.hpp>
 #include <com/sun/star/beans/NamedValue.hpp>
 #include <com/sun/star/beans/IllegalTypeException.hpp>
+#include <com/sun/star/xml/crypto/XDigestContext.hpp>
+#include <com/sun/star/xml/crypto/XDigestContextSupplier.hpp>
+#include <com/sun/star/xml/crypto/DigestID.hpp>
 
 #include <rtl/digest.h>
 
@@ -427,14 +430,38 @@ uno::Sequence< beans::NamedValue > OStorageHelper::CreatePackageEncryptionData(
 {
     // TODO/LATER: Should not the method be part of DocPasswordHelper?
     uno::Sequence< beans::NamedValue > aEncryptionData;
+    sal_Int32 nSha1Ind = 0;
     if ( aPassword.getLength() )
     {
+        // generate SHA256 start key
+        try
+        {
+            uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
+            if ( !xFactory.is() )
+                throw uno::RuntimeException();
+
+            uno::Reference< xml::crypto::XDigestContextSupplier > xDigestContextSupplier( xFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.crypto.NSSInitializer" ) ) ), uno::UNO_QUERY_THROW );
+            uno::Reference< xml::crypto::XDigestContext > xDigestContext( xDigestContextSupplier->getDigestContext( xml::crypto::DigestID::SHA256, uno::Sequence< beans::NamedValue >() ), uno::UNO_SET_THROW );
+
+            ::rtl::OString aUTF8Password( ::rtl::OUStringToOString( aPassword, RTL_TEXTENCODING_UTF8 ) );
+            xDigestContext->updateDigest( uno::Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( aUTF8Password.getStr() ), aUTF8Password.getLength() ) );
+            uno::Sequence< sal_Int8 > aDigest = xDigestContext->finalizeDigestAndDispose();
+
+            aEncryptionData.realloc( ++nSha1Ind );
+            aEncryptionData[0].Name = PACKAGE_ENCRYPTIONDATA_SHA256UTF8;
+            aEncryptionData[0].Value <<= aDigest;
+        }
+        catch ( uno::Exception& )
+        {
+            OSL_ENSURE( false, "Can not create SHA256 digest!" );
+        }
+
         // MS_1252 encoding was used for SO60 document format password encoding,
         // this encoding supports only a minor subset of nonascii characters,
         // but for compatibility reasons it has to be used for old document formats
-        aEncryptionData.realloc( 2 );
-        aEncryptionData[0].Name = PACKAGE_ENCRYPTIONDATA_SHA1UTF8;
-        aEncryptionData[1].Name = PACKAGE_ENCRYPTIONDATA_SHA1MS1252;
+        aEncryptionData.realloc( nSha1Ind + 2 );
+        aEncryptionData[nSha1Ind].Name = PACKAGE_ENCRYPTIONDATA_SHA1UTF8;
+        aEncryptionData[nSha1Ind + 1].Name = PACKAGE_ENCRYPTIONDATA_SHA1MS1252;
 
         rtl_TextEncoding pEncoding[2] = { RTL_TEXTENCODING_UTF8, RTL_TEXTENCODING_MS_1252 };
 
@@ -450,11 +477,11 @@ uno::Sequence< beans::NamedValue > OStorageHelper::CreatePackageEncryptionData(
 
             if ( nError != rtl_Digest_E_None )
             {
-                aEncryptionData.realloc( 0 );
+                aEncryptionData.realloc( nSha1Ind );
                 break;
             }
 
-            aEncryptionData[nInd].Value <<= uno::Sequence< sal_Int8 >( (sal_Int8*)pBuffer, RTL_DIGEST_LENGTH_SHA1 );
+            aEncryptionData[nSha1Ind+nInd].Value <<= uno::Sequence< sal_Int8 >( (sal_Int8*)pBuffer, RTL_DIGEST_LENGTH_SHA1 );
         }
     }
 


More information about the Libreoffice-commits mailing list