diff options
Diffstat (limited to 'sc/source/filter/excel')
-rw-r--r-- | sc/source/filter/excel/excdoc.cxx | 2 | ||||
-rw-r--r-- | sc/source/filter/excel/excel.cxx | 2 | ||||
-rw-r--r-- | sc/source/filter/excel/xeroot.cxx | 64 | ||||
-rw-r--r-- | sc/source/filter/excel/xestream.cxx | 64 | ||||
-rw-r--r-- | sc/source/filter/excel/xicontent.cxx | 2 | ||||
-rw-r--r-- | sc/source/filter/excel/xistream.cxx | 88 | ||||
-rw-r--r-- | sc/source/filter/excel/xlroot.cxx | 6 |
7 files changed, 166 insertions, 62 deletions
diff --git a/sc/source/filter/excel/excdoc.cxx b/sc/source/filter/excel/excdoc.cxx index 458629979172..f20b63efe7ba 100644 --- a/sc/source/filter/excel/excdoc.cxx +++ b/sc/source/filter/excel/excdoc.cxx @@ -222,7 +222,7 @@ void ExcTable::FillAsHeader( ExcBoundsheetList& rBoundsheetList ) else { if( IsDocumentEncrypted() ) - Add( new XclExpFilePass( GetRoot() ) ); + Add( new XclExpFileEncryption( GetRoot() ) ); Add( new XclExpInterfaceHdr( nCodePage ) ); Add( new XclExpUInt16Record( EXC_ID_MMS, 0 ) ); Add( new XclExpInterfaceEnd ); diff --git a/sc/source/filter/excel/excel.cxx b/sc/source/filter/excel/excel.cxx index 3a43a23cd624..0c3d7783645a 100644 --- a/sc/source/filter/excel/excel.cxx +++ b/sc/source/filter/excel/excel.cxx @@ -103,6 +103,8 @@ FltError ScFormatFilterPluginImpl::ScImportExcel( SfxMedium& rMedium, ScDocument aMediaDesc[ MediaDescriptor::PROP_URL() ] <<= ::rtl::OUString( pItem->GetValue() ); if( const SfxStringItem* pItem = static_cast< const SfxStringItem* >( pItemSet->GetItem( SID_PASSWORD ) ) ) aMediaDesc[ MediaDescriptor::PROP_PASSWORD() ] <<= ::rtl::OUString( pItem->GetValue() ); + if( const SfxStringItem* pItem = static_cast< const SfxStringItem* >( pItemSet->GetItem( SID_ENCRYPTIONDATA ) ) ) + aMediaDesc[ MediaDescriptor::PROP_ENCRYPTIONDATA() ] = pItem->GetValue(); } aMediaDesc[ MediaDescriptor::PROP_INPUTSTREAM() ] <<= rMedium.GetInputStream(); aMediaDesc[ MediaDescriptor::PROP_INTERACTIONHANDLER() ] <<= rMedium.GetInteractionHandler(); diff --git a/sc/source/filter/excel/xeroot.cxx b/sc/source/filter/excel/xeroot.cxx index 50e07ae1f2ed..4632a10a088e 100644 --- a/sc/source/filter/excel/xeroot.cxx +++ b/sc/source/filter/excel/xeroot.cxx @@ -28,7 +28,10 @@ // MARKER(update_precomp.py): autogen include statement, do not remove #include "precompiled_sc.hxx" +#include <rtl/random.h> #include <sfx2/docfile.hxx> +#include <sfx2/request.hxx> +#include <sfx2/frame.hxx> #include <sfx2/sfxsids.hrc> #include <unotools/saveopt.hxx> #include <svl/itemset.hxx> @@ -51,6 +54,8 @@ #include "document.hxx" #include "scextopt.hxx" +using namespace ::com::sun::star; + // Global data ================================================================ XclExpRootData::XclExpRootData( XclBiff eBiff, SfxMedium& rMedium, @@ -241,23 +246,66 @@ bool XclExpRoot::IsDocumentEncrypted() const if (pDocProt && pDocProt->isProtected() && pDocProt->isOptionEnabled(ScDocProtection::STRUCTURE)) return true; - if (GetPassword().Len() > 0) + if ( GetEncryptionData().getLength() > 0 ) // Password is entered directly into the save dialog. return true; return false; } -String XclExpRoot::GetPassword() const +uno::Sequence< beans::NamedValue > XclExpRoot::GenerateEncryptionData( const ::rtl::OUString& aPass ) const { - if( SfxItemSet* pItemSet = GetMedium().GetItemSet() ) + uno::Sequence< beans::NamedValue > aEncryptionData; + + if ( aPass.getLength() > 0 && aPass.getLength() < 16 ) { - const SfxPoolItem* pItem = 0; - if( pItemSet->GetItemState( SID_PASSWORD, TRUE, &pItem ) == SFX_ITEM_SET ) - if( const SfxStringItem* pStrItem = dynamic_cast< const SfxStringItem* >( pItem ) ) - return pStrItem->GetValue(); + TimeValue aTime; + osl_getSystemTime( &aTime ); + rtlRandomPool aRandomPool = rtl_random_createPool (); + rtl_random_addBytes ( aRandomPool, &aTime, 8 ); + + sal_uInt8 pnDocId[16]; + rtl_random_getBytes( aRandomPool, pnDocId, 16 ); + + rtl_random_destroyPool( aRandomPool ); + + sal_uInt16 pnPasswd[16]; + memset( pnPasswd, 0, sizeof( pnPasswd ) ); + for (xub_StrLen nChar = 0; nChar < aPass.getLength(); ++nChar ) + pnPasswd[nChar] = aPass.getStr()[nChar]; + + ::msfilter::MSCodec_Std97 aCodec; + aCodec.InitKey( pnPasswd, pnDocId ); + aEncryptionData = aCodec.GetEncryptionData(); } - return String::EmptyString(); + + return aEncryptionData; +} + +uno::Sequence< beans::NamedValue > XclExpRoot::GetEncryptionData() const +{ + uno::Sequence< beans::NamedValue > aEncryptionData; + SFX_ITEMSET_ARG( GetMedium().GetItemSet(), pEncryptionDataItem, SfxUnoAnyItem, SID_ENCRYPTIONDATA, sal_False ); + if ( pEncryptionDataItem ) + pEncryptionDataItem->GetValue() >>= aEncryptionData; + else + { + // try to get the encryption data from the password + SFX_ITEMSET_ARG( GetMedium().GetItemSet(), pPasswordItem, SfxStringItem, SID_PASSWORD, sal_False ); + if ( pPasswordItem && pPasswordItem->GetValue().Len() ) + aEncryptionData = GenerateEncryptionData( pPasswordItem->GetValue() ); + } + + return aEncryptionData; +} + +uno::Sequence< beans::NamedValue > XclExpRoot::GenerateDefaultEncryptionData() const +{ + uno::Sequence< beans::NamedValue > aEncryptionData; + if ( GetDefaultPassword().Len() > 0 ) + aEncryptionData = GenerateEncryptionData( GetDefaultPassword() ); + + return aEncryptionData; } XclExpRootData::XclExpLinkMgrRef XclExpRoot::GetLocalLinkMgrRef() const diff --git a/sc/source/filter/excel/xestream.cxx b/sc/source/filter/excel/xestream.cxx index f828079c8735..b15568e8ce9a 100644 --- a/sc/source/filter/excel/xestream.cxx +++ b/sc/source/filter/excel/xestream.cxx @@ -32,6 +32,7 @@ #include <utility> #include <rtl/ustring.hxx> +#include <rtl/random.h> #include <sax/fshelper.hxx> #include <unotools/streamwrap.hxx> @@ -65,6 +66,7 @@ using ::utl::OStreamWrapper; using ::std::vector; using namespace formula; +using namespace ::com::sun::star; // ============================================================================ @@ -484,17 +486,16 @@ void XclExpStream::WriteRawZeroBytes( sal_Size nBytes ) // ============================================================================ -XclExpBiff8Encrypter::XclExpBiff8Encrypter( const XclExpRoot& rRoot, const sal_uInt8 nDocId[16], - const sal_uInt8 nSalt[16] ) : +XclExpBiff8Encrypter::XclExpBiff8Encrypter( const XclExpRoot& rRoot ) : mrRoot(rRoot), mnOldPos(STREAM_SEEK_TO_END), mbValid(false) { - String aPass = rRoot.GetPassword(); - if (aPass.Len() == 0) + uno::Sequence< beans::NamedValue > aEncryptionData = rRoot.GetEncryptionData(); + if ( aEncryptionData.getLength() == 0 ) // Empty password. Get the default biff8 password. - aPass = rRoot.GetDefaultPassword(); - Init(aPass, nDocId, nSalt); + aEncryptionData = rRoot.GenerateDefaultEncryptionData(); + Init( aEncryptionData ); } XclExpBiff8Encrypter::~XclExpBiff8Encrypter() @@ -506,9 +507,22 @@ bool XclExpBiff8Encrypter::IsValid() const return mbValid; } -void XclExpBiff8Encrypter::GetSaltDigest( sal_uInt8 nSaltDigest[16] ) const +void XclExpBiff8Encrypter::GetSaltDigest( sal_uInt8 pnSaltDigest[16] ) const { - memcpy(nSaltDigest, mnSaltDigest, 16); + if ( sizeof( mpnSaltDigest ) == 16 ) + memcpy( pnSaltDigest, mpnSaltDigest, 16 ); +} + +void XclExpBiff8Encrypter::GetSalt( sal_uInt8 pnSalt[16] ) const +{ + if ( sizeof( mpnSalt ) == 16 ) + memcpy( pnSalt, mpnSalt, 16 ); +} + +void XclExpBiff8Encrypter::GetDocId( sal_uInt8 pnDocId[16] ) const +{ + if ( sizeof( mpnDocId ) == 16 ) + memcpy( pnDocId, mpnDocId, 16 ); } void XclExpBiff8Encrypter::Encrypt( SvStream& rStrm, sal_uInt8 nData ) @@ -565,36 +579,32 @@ void XclExpBiff8Encrypter::Encrypt( SvStream& rStrm, sal_Int32 nData ) Encrypt(rStrm, static_cast<sal_uInt32>(nData)); } -void XclExpBiff8Encrypter::Init( const String& aPass, const sal_uInt8 nDocId[16], - const sal_uInt8 nSalt[16] ) +void XclExpBiff8Encrypter::Init( const uno::Sequence< beans::NamedValue >& aEncryptionData ) { - memset(mnSaltDigest, 0, sizeof(mnSaltDigest)); + mbValid = false; - xub_StrLen nLen = aPass.Len(); - bool bValid = (0 < nLen) && (nLen < 16); - if ( bValid ) + if ( maCodec.InitCodec( aEncryptionData ) ) { - // transform String to sal_uInt16 array - memset(mnPassw, 0, sizeof(mnPassw)); - for (xub_StrLen nChar = 0; nChar < nLen; ++nChar) - mnPassw[nChar] = static_cast<sal_uInt16>(aPass.GetChar(nChar)); + maCodec.GetDocId( mpnDocId ); - // copy document ID - memcpy(mnDocId, nDocId, sizeof(mnDocId)); + // generate the salt here + TimeValue aTime; + osl_getSystemTime( &aTime ); + rtlRandomPool aRandomPool = rtl_random_createPool (); + rtl_random_addBytes( aRandomPool, &aTime, 8 ); + rtl_random_getBytes( aRandomPool, mpnSalt, 16 ); + rtl_random_destroyPool( aRandomPool ); - // init codec - maCodec.InitKey(mnPassw, mnDocId); + memset( mpnSaltDigest, 0, sizeof( mpnSaltDigest ) ); // generate salt hash. ::msfilter::MSCodec_Std97 aCodec; - aCodec.InitKey(mnPassw, mnDocId); - aCodec.CreateSaltDigest(nSalt, mnSaltDigest); + aCodec.InitCodec( aEncryptionData ); + aCodec.GetDigestFromSalt( mpnSalt, mpnSaltDigest ); // verify to make sure it's in good shape. - bValid = maCodec.VerifyKey(nSalt, mnSaltDigest); + mbValid = maCodec.VerifyKey( mpnSalt, mpnSaltDigest ); } - - mbValid = bValid; } sal_uInt32 XclExpBiff8Encrypter::GetBlockPos( sal_Size nStrmPos ) const diff --git a/sc/source/filter/excel/xicontent.cxx b/sc/source/filter/excel/xicontent.cxx index fb1eb9a3bf44..b210d18f479c 100644 --- a/sc/source/filter/excel/xicontent.cxx +++ b/sc/source/filter/excel/xicontent.cxx @@ -1118,7 +1118,7 @@ ErrCode XclImpDecryptHelper::ReadFilepass( XclImpStream& rStrm ) // request and verify a password (decrypter implements IDocPasswordVerifier) if( xDecr.is() ) - rStrm.GetRoot().RequestPassword( *xDecr ); + rStrm.GetRoot().RequestEncryptionData( *xDecr ); // return error code (success, wrong password, etc.) return xDecr.is() ? xDecr->GetError() : EXC_ENCR_ERROR_UNSUPP_CRYPT; diff --git a/sc/source/filter/excel/xistream.cxx b/sc/source/filter/excel/xistream.cxx index 98db9dcb4471..6b4851292e11 100644 --- a/sc/source/filter/excel/xistream.cxx +++ b/sc/source/filter/excel/xistream.cxx @@ -38,6 +38,8 @@ using ::rtl::OString; using ::rtl::OUString; using ::rtl::OUStringToOString; +using namespace ::com::sun::star; + // ============================================================================ // Decryption // ============================================================================ @@ -69,9 +71,16 @@ XclImpDecrypterRef XclImpDecrypter::Clone() const return xNewDecr; } -::comphelper::DocPasswordVerifierResult XclImpDecrypter::verifyPassword( const OUString& rPassword ) +::comphelper::DocPasswordVerifierResult XclImpDecrypter::verifyPassword( const ::rtl::OUString& rPassword, uno::Sequence< beans::NamedValue >& o_rEncryptionData ) +{ + o_rEncryptionData = OnVerifyPassword( rPassword ); + mnError = o_rEncryptionData.getLength() ? ERRCODE_NONE : ERRCODE_ABORT; + return o_rEncryptionData.getLength() ? ::comphelper::DocPasswordVerifierResult_OK : ::comphelper::DocPasswordVerifierResult_WRONG_PASSWORD; +} + +::comphelper::DocPasswordVerifierResult XclImpDecrypter::verifyEncryptionData( const uno::Sequence< beans::NamedValue >& rEncryptionData ) { - bool bValid = OnVerify( rPassword ); + bool bValid = OnVerifyEncryptionData( rEncryptionData ); mnError = bValid ? ERRCODE_NONE : ERRCODE_ABORT; return bValid ? ::comphelper::DocPasswordVerifierResult_OK : ::comphelper::DocPasswordVerifierResult_WRONG_PASSWORD; } @@ -110,7 +119,6 @@ sal_uInt16 XclImpDecrypter::Read( SvStream& rStrm, void* pData, sal_uInt16 nByte // ---------------------------------------------------------------------------- XclImpBiff5Decrypter::XclImpBiff5Decrypter( sal_uInt16 nKey, sal_uInt16 nHash ) : - maPassword( 16 ), mnKey( nKey ), mnHash( nHash ) { @@ -118,12 +126,12 @@ XclImpBiff5Decrypter::XclImpBiff5Decrypter( sal_uInt16 nKey, sal_uInt16 nHash ) XclImpBiff5Decrypter::XclImpBiff5Decrypter( const XclImpBiff5Decrypter& rSrc ) : XclImpDecrypter( rSrc ), - maPassword( rSrc.maPassword ), + maEncryptionData( rSrc.maEncryptionData ), mnKey( rSrc.mnKey ), mnHash( rSrc.mnHash ) { if( IsValid() ) - maCodec.InitKey( &maPassword.front() ); + maCodec.InitCodec( maEncryptionData ); } XclImpBiff5Decrypter* XclImpBiff5Decrypter::OnClone() const @@ -131,24 +139,40 @@ XclImpBiff5Decrypter* XclImpBiff5Decrypter::OnClone() const return new XclImpBiff5Decrypter( *this ); } -bool XclImpBiff5Decrypter::OnVerify( const OUString& rPassword ) +uno::Sequence< beans::NamedValue > XclImpBiff5Decrypter::OnVerifyPassword( const ::rtl::OUString& rPassword ) { + maEncryptionData.realloc( 0 ); + /* Convert password to a byte string. TODO: this needs some finetuning according to the spec... */ OString aBytePassword = OUStringToOString( rPassword, osl_getThreadTextEncoding() ); sal_Int32 nLen = aBytePassword.getLength(); if( (0 < nLen) && (nLen < 16) ) { - // copy byte string to sal_uInt8 array - maPassword.clear(); - maPassword.resize( 16, 0 ); - memcpy( &maPassword.front(), aBytePassword.getStr(), static_cast< size_t >( nLen ) ); + // init codec + maCodec.InitKey( (sal_uInt8*)aBytePassword.getStr() ); + if ( maCodec.VerifyKey( mnKey, mnHash ) ) + maEncryptionData = maCodec.GetEncryptionData(); + } + + return maEncryptionData; +} + +bool XclImpBiff5Decrypter::OnVerifyEncryptionData( const uno::Sequence< beans::NamedValue >& rEncryptionData ) +{ + maEncryptionData.realloc( 0 ); + + if( rEncryptionData.getLength() ) + { // init codec - maCodec.InitKey( &maPassword.front() ); - return maCodec.VerifyKey( mnKey, mnHash ); + maCodec.InitCodec( rEncryptionData ); + + if ( maCodec.VerifyKey( mnKey, mnHash ) ) + maEncryptionData = rEncryptionData; } - return false; + + return maEncryptionData.getLength(); } void XclImpBiff5Decrypter::OnUpdate( sal_Size /*nOldStrmPos*/, sal_Size nNewStrmPos, sal_uInt16 nRecSize ) @@ -168,7 +192,6 @@ sal_uInt16 XclImpBiff5Decrypter::OnRead( SvStream& rStrm, sal_uInt8* pnData, sal XclImpBiff8Decrypter::XclImpBiff8Decrypter( sal_uInt8 pnSalt[ 16 ], sal_uInt8 pnVerifier[ 16 ], sal_uInt8 pnVerifierHash[ 16 ] ) : - maPassword( 16, 0 ), maSalt( pnSalt, pnSalt + 16 ), maVerifier( pnVerifier, pnVerifier + 16 ), maVerifierHash( pnVerifierHash, pnVerifierHash + 16 ) @@ -177,13 +200,13 @@ XclImpBiff8Decrypter::XclImpBiff8Decrypter( sal_uInt8 pnSalt[ 16 ], XclImpBiff8Decrypter::XclImpBiff8Decrypter( const XclImpBiff8Decrypter& rSrc ) : XclImpDecrypter( rSrc ), - maPassword( rSrc.maPassword ), + maEncryptionData( rSrc.maEncryptionData ), maSalt( rSrc.maSalt ), maVerifier( rSrc.maVerifier ), maVerifierHash( rSrc.maVerifierHash ) { if( IsValid() ) - maCodec.InitKey( &maPassword.front(), &maSalt.front() ); + maCodec.InitCodec( maEncryptionData ); } XclImpBiff8Decrypter* XclImpBiff8Decrypter::OnClone() const @@ -191,25 +214,44 @@ XclImpBiff8Decrypter* XclImpBiff8Decrypter::OnClone() const return new XclImpBiff8Decrypter( *this ); } -bool XclImpBiff8Decrypter::OnVerify( const OUString& rPassword ) +uno::Sequence< beans::NamedValue > XclImpBiff8Decrypter::OnVerifyPassword( const ::rtl::OUString& rPassword ) { + maEncryptionData.realloc( 0 ); + sal_Int32 nLen = rPassword.getLength(); if( (0 < nLen) && (nLen < 16) ) { // copy string to sal_uInt16 array - maPassword.clear(); - maPassword.resize( 16, 0 ); + ::std::vector< sal_uInt16 > aPassVect( 16 ); const sal_Unicode* pcChar = rPassword.getStr(); const sal_Unicode* pcCharEnd = pcChar + nLen; - ::std::vector< sal_uInt16 >::iterator aIt = maPassword.begin(); + ::std::vector< sal_uInt16 >::iterator aIt = aPassVect.begin(); for( ; pcChar < pcCharEnd; ++pcChar, ++aIt ) *aIt = static_cast< sal_uInt16 >( *pcChar ); // init codec - maCodec.InitKey( &maPassword.front(), &maSalt.front() ); - return maCodec.VerifyKey( &maVerifier.front(), &maVerifierHash.front() ); + maCodec.InitKey( &aPassVect.front(), &maSalt.front() ); + if ( maCodec.VerifyKey( &maVerifier.front(), &maVerifierHash.front() ) ) + maEncryptionData = maCodec.GetEncryptionData(); } - return false; + + return maEncryptionData; +} + +bool XclImpBiff8Decrypter::OnVerifyEncryptionData( const uno::Sequence< beans::NamedValue >& rEncryptionData ) +{ + maEncryptionData.realloc( 0 ); + + if( rEncryptionData.getLength() ) + { + // init codec + maCodec.InitCodec( rEncryptionData ); + + if ( maCodec.VerifyKey( &maVerifier.front(), &maVerifierHash.front() ) ) + maEncryptionData = rEncryptionData; + } + + return maEncryptionData.getLength(); } void XclImpBiff8Decrypter::OnUpdate( sal_Size nOldStrmPos, sal_Size nNewStrmPos, sal_uInt16 /*nRecSize*/ ) diff --git a/sc/source/filter/excel/xlroot.cxx b/sc/source/filter/excel/xlroot.cxx index 2b2180db5e6a..c6d6c5b9246a 100644 --- a/sc/source/filter/excel/xlroot.cxx +++ b/sc/source/filter/excel/xlroot.cxx @@ -71,6 +71,8 @@ using ::com::sun::star::frame::XFrame; using ::com::sun::star::frame::XFramesSupplier; using ::com::sun::star::lang::XMultiServiceFactory; +using namespace ::com::sun::star; + // Global data ================================================================ #ifdef DBG_UTIL @@ -240,11 +242,11 @@ sal_Int32 XclRoot::GetHmmFromPixelY( double fPixelY ) const return static_cast< sal_Int32 >( fPixelY * mrData.mfScreenPixelY + 0.5 ); } -String XclRoot::RequestPassword( ::comphelper::IDocPasswordVerifier& rVerifier ) const +uno::Sequence< beans::NamedValue > XclRoot::RequestEncryptionData( ::comphelper::IDocPasswordVerifier& rVerifier ) const { ::std::vector< OUString > aDefaultPasswords; aDefaultPasswords.push_back( mrData.maDefPassword ); - return ScfApiHelper::QueryPasswordForMedium( mrData.mrMedium, rVerifier, &aDefaultPasswords ); + return ScfApiHelper::QueryEncryptionDataForMedium( mrData.mrMedium, rVerifier, &aDefaultPasswords ); } bool XclRoot::HasVbaStorage() const |