diff options
author | Norbert Thiebaud <nthiebaud@gmail.com> | 2011-08-21 18:12:16 -0500 |
---|---|---|
committer | Norbert Thiebaud <nthiebaud@gmail.com> | 2011-08-21 18:12:16 -0500 |
commit | 0d4720d1e1ffcefc43ae2af59c7487ab0bfab2ce (patch) | |
tree | 7cacea747b7ff3de52f69a8605ef486463201a9c /package/source | |
parent | 57b992c0b57daba5f44b5c4cdb08e817f355ea01 (diff) |
undo anything not wizard related in the previous merge-commit
Diffstat (limited to 'package/source')
34 files changed, 2136 insertions, 2050 deletions
diff --git a/package/source/manifest/Base64Codec.cxx b/package/source/manifest/Base64Codec.cxx index cf539d0790c4..b4f5b6d32ffd 100644 --- a/package/source/manifest/Base64Codec.cxx +++ b/package/source/manifest/Base64Codec.cxx @@ -131,11 +131,11 @@ void ThreeByteToFourByte (const sal_uInt8* pBuffer, const sal_Int32 nStart, cons sBuffer.setCharAt(3, aBase64EncodeTable [nIndex]); } -void Base64Codec::encodeBase64(rtl::OUStringBuffer& aStrBuffer, const uno::Sequence < sal_uInt8 >& aPass) +void Base64Codec::encodeBase64(rtl::OUStringBuffer& aStrBuffer, const uno::Sequence < sal_Int8 >& aPass) { sal_Int32 i(0); sal_Int32 nBufferLength(aPass.getLength()); - const sal_uInt8* pBuffer = aPass.getConstArray(); + const sal_uInt8* pBuffer = reinterpret_cast< const sal_uInt8* >( aPass.getConstArray() ); while (i < nBufferLength) { rtl::OUStringBuffer sBuffer; @@ -185,7 +185,7 @@ void FourByteToThreeByte (sal_uInt8* pBuffer, sal_Int32& nLength, const sal_Int3 pBuffer[nStart + 2] = OneByte; } -void Base64Codec::decodeBase64(uno::Sequence< sal_uInt8 >& aBuffer, const rtl::OUString& sBuffer) +void Base64Codec::decodeBase64(uno::Sequence< sal_Int8 >& aBuffer, const rtl::OUString& sBuffer) { sal_Int32 nFirstLength((sBuffer.getLength() / 4) * 3); sal_uInt8* pBuffer = new sal_uInt8[nFirstLength]; @@ -201,7 +201,7 @@ void Base64Codec::decodeBase64(uno::Sequence< sal_uInt8 >& aBuffer, const rtl::O i += 4; k += 3; } - aBuffer = uno::Sequence<sal_uInt8>(pBuffer, nSecondLength); + aBuffer = uno::Sequence<sal_Int8>( reinterpret_cast< sal_Int8* >( pBuffer ), nSecondLength ); delete[] pBuffer; } diff --git a/package/source/manifest/Base64Codec.hxx b/package/source/manifest/Base64Codec.hxx index 8cae2a7037fd..60bd018c159b 100644 --- a/package/source/manifest/Base64Codec.hxx +++ b/package/source/manifest/Base64Codec.hxx @@ -40,8 +40,8 @@ class OUStringBuffer; class Base64Codec { public: - static void encodeBase64(rtl::OUStringBuffer& aStrBuffer, const com::sun::star::uno::Sequence<sal_uInt8>& aPass); - static void decodeBase64(com::sun::star::uno::Sequence<sal_uInt8>& aPass, const rtl::OUString& sBuffer); + static void encodeBase64(rtl::OUStringBuffer& aStrBuffer, const com::sun::star::uno::Sequence<sal_Int8>& aPass); + static void decodeBase64(com::sun::star::uno::Sequence<sal_Int8>& aPass, const rtl::OUString& sBuffer); }; #endif diff --git a/package/source/manifest/ManifestDefines.hxx b/package/source/manifest/ManifestDefines.hxx index 65b9c6181727..969f4d834835 100644 --- a/package/source/manifest/ManifestDefines.hxx +++ b/package/source/manifest/ManifestDefines.hxx @@ -54,16 +54,29 @@ #define ELEMENT_START_KEY_GENERATION "manifest:start-key-generation" #define ATTRIBUTE_START_KEY_GENERATION_NAME "manifest:start-key-generation-name" -#define ALGORITHM_SHA1 "SHA1" #define ATTRIBUTE_KEY_SIZE "manifest:key-size" -#define START_KEY_SIZE "20" #define ELEMENT_KEY_DERIVATION "manifest:key-derivation" #define ATTRIBUTE_KEY_DERIVATION_NAME "manifest:key-derivation-name" #define ATTRIBUTE_SALT "manifest:salt" #define ATTRIBUTE_ITERATION_COUNT "manifest:iteration-count" -#define CHECKSUM_TYPE "SHA1/1K" -#define DERIVED_KEY_SIZE "16" + +#define SHA256_URL "http://www.w3.org/2000/09/xmldsig#sha256" +#define SHA1_NAME "SHA1" +#define SHA1_URL "http://www.w3.org/2000/09/xmldsig#sha1" + +#define SHA1_1K_NAME "SHA1/1K" +#define SHA1_1K_URL "urn:oasis:names:tc:opendocument:xmlns:manifest:1.0#sha1-1k" +#define SHA256_1K_URL "urn:oasis:names:tc:opendocument:xmlns:manifest:1.0#sha256-1k" + +#define BLOWFISH_NAME "Blowfish CFB" +#define BLOWFISH_URL "urn:oasis:names:tc:opendocument:xmlns:manifest:1.0#blowfish" +#define AES128_URL "http://www.w3.org/2001/04/xmlenc#aes128-cbc" +#define AES192_URL "http://www.w3.org/2001/04/xmlenc#aes192-cbc" +#define AES256_URL "http://www.w3.org/2001/04/xmlenc#aes256-cbc" + +#define PBKDF2_NAME "PBKDF2" +#define PBKDF2_URL "urn:oasis:names:tc:opendocument:xmlns:manifest:1.0#pbkdf2" #endif diff --git a/package/source/manifest/ManifestExport.cxx b/package/source/manifest/ManifestExport.cxx index e173e8da8d53..b5dc6c049440 100644 --- a/package/source/manifest/ManifestExport.cxx +++ b/package/source/manifest/ManifestExport.cxx @@ -28,80 +28,92 @@ // MARKER(update_precomp.py): autogen include statement, do not remove #include "precompiled_package.hxx" -#include <ManifestExport.hxx> -#include <ManifestDefines.hxx> -#include <com/sun/star/xml/sax/XAttributeList.hpp> -#include <rtl/ustrbuf.hxx> -#include <Base64Codec.hxx> + #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp> #include <com/sun/star/xml/sax/XDocumentHandler.hpp> +#include <com/sun/star/xml/sax/XAttributeList.hpp> +#include <com/sun/star/xml/crypto/DigestID.hpp> +#include <com/sun/star/xml/crypto/CipherID.hpp> #include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/uno/RuntimeException.hpp> +#include <ManifestDefines.hxx> +#include <ManifestExport.hxx> +#include <Base64Codec.hxx> + +#include <rtl/ustrbuf.hxx> #include <comphelper/documentconstants.hxx> #include <comphelper/attributelist.hxx> -using namespace com::sun::star::beans; -using namespace com::sun::star::uno; -using namespace com::sun::star::xml::sax; +using namespace ::com::sun::star; using ::rtl::OUString; using ::rtl::OUStringBuffer; -ManifestExport::ManifestExport(Reference < XDocumentHandler > xHandler, const Sequence < Sequence < PropertyValue > > &rManList ) +ManifestExport::ManifestExport( uno::Reference< xml::sax::XDocumentHandler > xHandler, const uno::Sequence< uno::Sequence < beans::PropertyValue > >& rManList ) { - const OUString sFileEntryElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_FILE_ENTRY ) ); - const OUString sManifestElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_MANIFEST ) ); - const OUString sEncryptionDataElement( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_ENCRYPTION_DATA ) ); - const OUString sAlgorithmElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_ALGORITHM ) ); - const OUString sStartKeyGenerationElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_START_KEY_GENERATION ) ); - const OUString sKeyDerivationElement( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_KEY_DERIVATION ) ); - - const OUString sCdataAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_CDATA ) ); - const OUString sMediaTypeAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_MEDIA_TYPE ) ); - const OUString sVersionAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_VERSION ) ); - const OUString sFullPathAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_FULL_PATH ) ); - const OUString sSizeAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_SIZE ) ); - const OUString sKeySizeAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_KEY_SIZE ) ); - const OUString sSaltAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_SALT ) ); - const OUString sInitialisationVectorAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_INITIALISATION_VECTOR ) ); - const OUString sIterationCountAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_ITERATION_COUNT ) ); - const OUString sAlgorithmNameAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_ALGORITHM_NAME ) ); - const OUString sStartKeyGenerationNameAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_START_KEY_GENERATION_NAME ) ); - const OUString sKeyDerivationNameAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_KEY_DERIVATION_NAME ) ); - const OUString sChecksumTypeAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_CHECKSUM_TYPE ) ); - const OUString sChecksumAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_CHECKSUM) ); - - const OUString sFullPathProperty ( RTL_CONSTASCII_USTRINGPARAM ( "FullPath" ) ); - const OUString sVersionProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Version" ) ); - const OUString sMediaTypeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "MediaType" ) ); - const OUString sIterationCountProperty ( RTL_CONSTASCII_USTRINGPARAM ( "IterationCount" ) ); - const OUString sSaltProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Salt" ) ); - const OUString sInitialisationVectorProperty( RTL_CONSTASCII_USTRINGPARAM ( "InitialisationVector" ) ); - const OUString sSizeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Size" ) ); - const OUString sDigestProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Digest" ) ); - - const OUString sWhiteSpace ( RTL_CONSTASCII_USTRINGPARAM ( " " ) ); - const OUString sBlowfish ( RTL_CONSTASCII_USTRINGPARAM ( "Blowfish CFB" ) ); - const OUString sPBKDF2 ( RTL_CONSTASCII_USTRINGPARAM ( "PBKDF2" ) ); - const OUString sChecksumType ( RTL_CONSTASCII_USTRINGPARAM ( CHECKSUM_TYPE ) ); - const OUString sStartKeySize ( RTL_CONSTASCII_USTRINGPARAM ( START_KEY_SIZE ) ); - const OUString sDerivedKeySize ( RTL_CONSTASCII_USTRINGPARAM ( DERIVED_KEY_SIZE ) ); - const OUString sSHA1 ( RTL_CONSTASCII_USTRINGPARAM ( ALGORITHM_SHA1 ) ); + const ::rtl::OUString sFileEntryElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_FILE_ENTRY ) ); + const ::rtl::OUString sManifestElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_MANIFEST ) ); + const ::rtl::OUString sEncryptionDataElement( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_ENCRYPTION_DATA ) ); + const ::rtl::OUString sAlgorithmElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_ALGORITHM ) ); + const ::rtl::OUString sStartKeyGenerationElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_START_KEY_GENERATION ) ); + const ::rtl::OUString sKeyDerivationElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_KEY_DERIVATION ) ); + + const ::rtl::OUString sCdataAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_CDATA ) ); + const ::rtl::OUString sMediaTypeAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_MEDIA_TYPE ) ); + const ::rtl::OUString sVersionAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_VERSION ) ); + const ::rtl::OUString sFullPathAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_FULL_PATH ) ); + const ::rtl::OUString sSizeAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_SIZE ) ); + const ::rtl::OUString sKeySizeAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_KEY_SIZE ) ); + const ::rtl::OUString sSaltAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_SALT ) ); + const ::rtl::OUString sInitialisationVectorAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_INITIALISATION_VECTOR ) ); + const ::rtl::OUString sIterationCountAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_ITERATION_COUNT ) ); + const ::rtl::OUString sAlgorithmNameAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_ALGORITHM_NAME ) ); + const ::rtl::OUString sStartKeyGenerationNameAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_START_KEY_GENERATION_NAME ) ); + const ::rtl::OUString sKeyDerivationNameAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_KEY_DERIVATION_NAME ) ); + const ::rtl::OUString sChecksumTypeAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_CHECKSUM_TYPE ) ); + const ::rtl::OUString sChecksumAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_CHECKSUM) ); + + const ::rtl::OUString sFullPathProperty ( RTL_CONSTASCII_USTRINGPARAM ( "FullPath" ) ); + const ::rtl::OUString sVersionProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Version" ) ); + const ::rtl::OUString sMediaTypeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "MediaType" ) ); + const ::rtl::OUString sIterationCountProperty ( RTL_CONSTASCII_USTRINGPARAM ( "IterationCount" ) ); + const ::rtl::OUString sDerivedKeySizeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "DerivedKeySize" ) ); + const ::rtl::OUString sSaltProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Salt" ) ); + const ::rtl::OUString sInitialisationVectorProperty( RTL_CONSTASCII_USTRINGPARAM ( "InitialisationVector" ) ); + const ::rtl::OUString sSizeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Size" ) ); + const ::rtl::OUString sDigestProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Digest" ) ); + const ::rtl::OUString sEncryptionAlgProperty ( RTL_CONSTASCII_USTRINGPARAM ( "EncryptionAlgorithm" ) ); + const ::rtl::OUString sStartKeyAlgProperty ( RTL_CONSTASCII_USTRINGPARAM ( "StartKeyAlgorithm" ) ); + const ::rtl::OUString sDigestAlgProperty ( RTL_CONSTASCII_USTRINGPARAM ( "DigestAlgorithm" ) ); + + const ::rtl::OUString sWhiteSpace ( RTL_CONSTASCII_USTRINGPARAM ( " " ) ); + + const ::rtl::OUString sSHA256_URL ( RTL_CONSTASCII_USTRINGPARAM ( SHA256_URL ) ); + const ::rtl::OUString sSHA1_Name ( RTL_CONSTASCII_USTRINGPARAM ( SHA1_NAME ) ); + + const ::rtl::OUString sSHA1_1k_Name ( RTL_CONSTASCII_USTRINGPARAM ( SHA1_1K_NAME ) ); + const ::rtl::OUString sSHA256_1k_URL ( RTL_CONSTASCII_USTRINGPARAM ( SHA256_1K_URL ) ); + + const ::rtl::OUString sBlowfish_Name ( RTL_CONSTASCII_USTRINGPARAM ( BLOWFISH_NAME ) ); + const ::rtl::OUString sAES256_URL ( RTL_CONSTASCII_USTRINGPARAM ( AES256_URL ) ); + + const ::rtl::OUString sPBKDF2_Name ( RTL_CONSTASCII_USTRINGPARAM ( PBKDF2_NAME ) ); ::comphelper::AttributeList * pRootAttrList = new ::comphelper::AttributeList; - const Sequence < PropertyValue > *pSequence = rManList.getConstArray(); + const uno::Sequence < beans::PropertyValue > *pSequence = rManList.getConstArray(); const sal_uInt32 nManLength = rManList.getLength(); // find the mediatype of the document if any - OUString aDocMediaType; - OUString aDocVersion; + ::rtl::OUString aDocMediaType; + ::rtl::OUString aDocVersion; for (sal_uInt32 nInd = 0; nInd < nManLength ; nInd++ ) { - OUString aMediaType; - OUString aPath; - OUString aVersion; + ::rtl::OUString aMediaType; + ::rtl::OUString aPath; + ::rtl::OUString aVersion; - const PropertyValue *pValue = pSequence[nInd].getConstArray(); + const beans::PropertyValue *pValue = pSequence[nInd].getConstArray(); for (sal_uInt32 j = 0, nNum = pSequence[nInd].getLength(); j < nNum; j++, pValue++) { if (pValue->Name.equals (sMediaTypeProperty) ) @@ -121,7 +133,7 @@ ManifestExport::ManifestExport(Reference < XDocumentHandler > xHandler, const S break; } - if ( aPath.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) ) ) ) + if ( aPath.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) ) ) ) { aDocMediaType = aMediaType; aDocVersion = aVersion; @@ -134,28 +146,28 @@ ManifestExport::ManifestExport(Reference < XDocumentHandler > xHandler, const S sal_Bool bStoreStartKeyGeneration = sal_False; if ( aDocMediaType.getLength() ) { - if ( aDocMediaType.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_TEXT_ASCII ) ) ) - || aDocMediaType.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_TEXT_WEB_ASCII ) ) ) - || aDocMediaType.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_TEXT_GLOBAL_ASCII ) ) ) - || aDocMediaType.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_ASCII ) ) ) - || aDocMediaType.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_ASCII ) ) ) - || aDocMediaType.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_ASCII ) ) ) - || aDocMediaType.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_CHART_ASCII ) ) ) - || aDocMediaType.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII ) ) ) - || aDocMediaType.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_ASCII ) ) ) - - || aDocMediaType.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_TEXT_TEMPLATE_ASCII ) ) ) - || aDocMediaType.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_TEMPLATE_ASCII ) ) ) - || aDocMediaType.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_TEMPLATE_ASCII ) ) ) - || aDocMediaType.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_TEMPLATE_ASCII ) ) ) - || aDocMediaType.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_CHART_TEMPLATE_ASCII ) ) ) - || aDocMediaType.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_TEMPLATE_ASCII ) ) ) ) + if ( aDocMediaType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_TEXT_ASCII ) ) ) + || aDocMediaType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_TEXT_WEB_ASCII ) ) ) + || aDocMediaType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_TEXT_GLOBAL_ASCII ) ) ) + || aDocMediaType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_ASCII ) ) ) + || aDocMediaType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_ASCII ) ) ) + || aDocMediaType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_ASCII ) ) ) + || aDocMediaType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_CHART_ASCII ) ) ) + || aDocMediaType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII ) ) ) + || aDocMediaType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_ASCII ) ) ) + + || aDocMediaType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_TEXT_TEMPLATE_ASCII ) ) ) + || aDocMediaType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_TEMPLATE_ASCII ) ) ) + || aDocMediaType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_TEMPLATE_ASCII ) ) ) + || aDocMediaType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_TEMPLATE_ASCII ) ) ) + || aDocMediaType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_CHART_TEMPLATE_ASCII ) ) ) + || aDocMediaType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_TEMPLATE_ASCII ) ) ) ) { // oasis format - pRootAttrList->AddAttribute ( OUString( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_XMLNS ) ), + pRootAttrList->AddAttribute ( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_XMLNS ) ), sCdataAttribute, - OUString( RTL_CONSTASCII_USTRINGPARAM ( MANIFEST_OASIS_NAMESPACE ) ) ); + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( MANIFEST_OASIS_NAMESPACE ) ) ); bAcceptNonemptyVersion = sal_True; if ( aDocVersion.compareTo( ODFVER_012_TEXT ) >= 0 ) { @@ -168,21 +180,21 @@ ManifestExport::ManifestExport(Reference < XDocumentHandler > xHandler, const S { // even if it is no SO6 format the namespace must be specified // thus SO6 format is used as default one - pRootAttrList->AddAttribute ( OUString( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_XMLNS ) ), + pRootAttrList->AddAttribute ( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_XMLNS ) ), sCdataAttribute, - OUString( RTL_CONSTASCII_USTRINGPARAM ( MANIFEST_NAMESPACE ) ) ); + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( MANIFEST_NAMESPACE ) ) ); bProvideDTD = sal_True; } } - Reference < XAttributeList > xRootAttrList (pRootAttrList); + uno::Reference < xml::sax::XAttributeList > xRootAttrList (pRootAttrList); xHandler->startDocument(); - Reference < XExtendedDocumentHandler > xExtHandler ( xHandler, UNO_QUERY ); + uno::Reference < xml::sax::XExtendedDocumentHandler > xExtHandler ( xHandler, uno::UNO_QUERY ); if ( xExtHandler.is() && bProvideDTD ) { - OUString aDocType ( RTL_CONSTASCII_USTRINGPARAM ( MANIFEST_DOCTYPE ) ); + ::rtl::OUString aDocType ( RTL_CONSTASCII_USTRINGPARAM ( MANIFEST_DOCTYPE ) ); xExtHandler->unknown ( aDocType ); xHandler->ignorableWhitespace ( sWhiteSpace ); } @@ -191,9 +203,9 @@ ManifestExport::ManifestExport(Reference < XDocumentHandler > xHandler, const S for (sal_uInt32 i = 0 ; i < nManLength ; i++) { ::comphelper::AttributeList *pAttrList = new ::comphelper::AttributeList; - const PropertyValue *pValue = pSequence[i].getConstArray(); - OUString aString; - const PropertyValue *pVector = NULL, *pSalt = NULL, *pIterationCount = NULL, *pDigest = NULL; + const beans::PropertyValue *pValue = pSequence[i].getConstArray(); + ::rtl::OUString aString; + const uno::Any *pVector = NULL, *pSalt = NULL, *pIterationCount = NULL, *pDigest = NULL, *pDigestAlg = NULL, *pEncryptAlg = NULL, *pStartKeyAlg = NULL, *pDerivedKeySize = NULL; for (sal_uInt32 j = 0, nNum = pSequence[i].getLength(); j < nNum; j++, pValue++) { if (pValue->Name.equals (sMediaTypeProperty) ) @@ -217,47 +229,87 @@ ManifestExport::ManifestExport(Reference < XDocumentHandler > xHandler, const S { sal_Int32 nSize = 0; pValue->Value >>= nSize; - OUStringBuffer aBuffer; + ::rtl::OUStringBuffer aBuffer; aBuffer.append ( nSize ); pAttrList->AddAttribute ( sSizeAttribute, sCdataAttribute, aBuffer.makeStringAndClear() ); } else if (pValue->Name.equals (sInitialisationVectorProperty) ) - pVector = pValue; + pVector = &pValue->Value; else if (pValue->Name.equals (sSaltProperty) ) - pSalt = pValue; + pSalt = &pValue->Value; else if (pValue->Name.equals (sIterationCountProperty) ) - pIterationCount = pValue; + pIterationCount = &pValue->Value; else if (pValue->Name.equals ( sDigestProperty ) ) - pDigest = pValue; + pDigest = &pValue->Value; + else if (pValue->Name.equals ( sDigestAlgProperty ) ) + pDigestAlg = &pValue->Value; + else if (pValue->Name.equals ( sEncryptionAlgProperty ) ) + pEncryptAlg = &pValue->Value; + else if (pValue->Name.equals ( sStartKeyAlgProperty ) ) + pStartKeyAlg = &pValue->Value; + else if (pValue->Name.equals ( sDerivedKeySizeProperty ) ) + pDerivedKeySize = &pValue->Value; } + xHandler->ignorableWhitespace ( sWhiteSpace ); - Reference < XAttributeList > xAttrList ( pAttrList ); + uno::Reference < xml::sax::XAttributeList > xAttrList ( pAttrList ); xHandler->startElement( sFileEntryElement , xAttrList); - if ( pVector && pSalt && pIterationCount ) + if ( pVector && pSalt && pIterationCount && pDigest && pDigestAlg && pEncryptAlg && pStartKeyAlg && pDerivedKeySize ) { // ==== Encryption Data ::comphelper::AttributeList * pNewAttrList = new ::comphelper::AttributeList; - Reference < XAttributeList > xNewAttrList (pNewAttrList); - OUStringBuffer aBuffer; - Sequence < sal_uInt8 > aSequence; + uno::Reference < xml::sax::XAttributeList > xNewAttrList (pNewAttrList); + ::rtl::OUStringBuffer aBuffer; + uno::Sequence < sal_Int8 > aSequence; xHandler->ignorableWhitespace ( sWhiteSpace ); - if ( pDigest ) - { - pNewAttrList->AddAttribute ( sChecksumTypeAttribute, sCdataAttribute, sChecksumType ); - pDigest->Value >>= aSequence; - Base64Codec::encodeBase64 ( aBuffer, aSequence ); - pNewAttrList->AddAttribute ( sChecksumAttribute, sCdataAttribute, aBuffer.makeStringAndClear() ); - } + + // ==== Digest + ::rtl::OUString sChecksumType; + sal_Int32 nDigestAlgID = 0; + *pDigestAlg >>= nDigestAlgID; + if ( nDigestAlgID == xml::crypto::DigestID::SHA256_1K ) + sChecksumType = sSHA256_1k_URL; + else if ( nDigestAlgID == xml::crypto::DigestID::SHA1_1K ) + sChecksumType = sSHA1_1k_Name; + else + throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Unexpected digest algorithm is provided!" ) ), uno::Reference< uno::XInterface >() ); + + pNewAttrList->AddAttribute ( sChecksumTypeAttribute, sCdataAttribute, sChecksumType ); + *pDigest >>= aSequence; + Base64Codec::encodeBase64( aBuffer, aSequence ); + pNewAttrList->AddAttribute ( sChecksumAttribute, sCdataAttribute, aBuffer.makeStringAndClear() ); + xHandler->startElement( sEncryptionDataElement , xNewAttrList); // ==== Algorithm pNewAttrList = new ::comphelper::AttributeList; xNewAttrList = pNewAttrList; - pNewAttrList->AddAttribute ( sAlgorithmNameAttribute, sCdataAttribute, sBlowfish ); + sal_Int32 nEncAlgID = 0; + sal_Int32 nDerivedKeySize = 0; + *pEncryptAlg >>= nEncAlgID; + *pDerivedKeySize >>= nDerivedKeySize; - pVector->Value >>= aSequence; + ::rtl::OUString sEncAlgName; + if ( nEncAlgID == xml::crypto::CipherID::AES_CBC_W3C_PADDING ) + { + OSL_ENSURE( nDerivedKeySize, "Unexpected key size is provided!" ); + if ( nDerivedKeySize != 32 ) + throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Unexpected key size is provided!" ) ), uno::Reference< uno::XInterface >() ); + + sEncAlgName = sAES256_URL; + } + else if ( nEncAlgID == xml::crypto::CipherID::BLOWFISH_CFB_8 ) + { + sEncAlgName = sBlowfish_Name; + } + else + throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Unexpecte encryption algorithm is provided!" ) ), uno::Reference< uno::XInterface >() ); + + pNewAttrList->AddAttribute ( sAlgorithmNameAttribute, sCdataAttribute, sEncAlgName ); + + *pVector >>= aSequence; Base64Codec::encodeBase64 ( aBuffer, aSequence ); pNewAttrList->AddAttribute ( sInitialisationVectorAttribute, sCdataAttribute, aBuffer.makeStringAndClear() ); @@ -270,17 +322,20 @@ ManifestExport::ManifestExport(Reference < XDocumentHandler > xHandler, const S pNewAttrList = new ::comphelper::AttributeList; xNewAttrList = pNewAttrList; - pNewAttrList->AddAttribute ( sKeyDerivationNameAttribute, sCdataAttribute, sPBKDF2 ); + pNewAttrList->AddAttribute ( sKeyDerivationNameAttribute, sCdataAttribute, sPBKDF2_Name ); if ( bStoreStartKeyGeneration ) - pNewAttrList->AddAttribute ( sKeySizeAttribute, sCdataAttribute, sDerivedKeySize ); + { + aBuffer.append( nDerivedKeySize ); + pNewAttrList->AddAttribute ( sKeySizeAttribute, sCdataAttribute, aBuffer.makeStringAndClear() ); + } sal_Int32 nCount = 0; - pIterationCount->Value >>= nCount; + *pIterationCount >>= nCount; aBuffer.append (nCount); pNewAttrList->AddAttribute ( sIterationCountAttribute, sCdataAttribute, aBuffer.makeStringAndClear() ); - pSalt->Value >>= aSequence; + *pSalt >>= aSequence; Base64Codec::encodeBase64 ( aBuffer, aSequence ); pNewAttrList->AddAttribute ( sSaltAttribute, sCdataAttribute, aBuffer.makeStringAndClear() ); @@ -297,8 +352,26 @@ ManifestExport::ManifestExport(Reference < XDocumentHandler > xHandler, const S pNewAttrList = new ::comphelper::AttributeList; xNewAttrList = pNewAttrList; - // currently SHA1 is used to generate 20-bytes start key - pNewAttrList->AddAttribute ( sStartKeyGenerationNameAttribute, sCdataAttribute, sSHA1 ); + ::rtl::OUString sStartKeyAlg; + ::rtl::OUString sStartKeySize; + sal_Int32 nStartKeyAlgID = 0; + *pStartKeyAlg >>= nStartKeyAlgID; + if ( nStartKeyAlgID == xml::crypto::DigestID::SHA256 ) + { + sStartKeyAlg = sSHA256_URL; + aBuffer.append( (sal_Int32)32 ); + sStartKeySize = aBuffer.makeStringAndClear(); + } + else if ( nStartKeyAlgID == xml::crypto::DigestID::SHA1 ) + { + sStartKeyAlg = sSHA1_Name; + aBuffer.append( (sal_Int32)20 ); + sStartKeySize = aBuffer.makeStringAndClear(); + } + else + throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Unexpected start key algorithm is provided!" ) ), uno::Reference< uno::XInterface >() ); + + pNewAttrList->AddAttribute ( sStartKeyGenerationNameAttribute, sCdataAttribute, sStartKeyAlg ); pNewAttrList->AddAttribute ( sKeySizeAttribute, sCdataAttribute, sStartKeySize ); xHandler->ignorableWhitespace ( sWhiteSpace ); diff --git a/package/source/manifest/ManifestImport.cxx b/package/source/manifest/ManifestImport.cxx index 58310e5ed116..f685f7ea87fb 100644 --- a/package/source/manifest/ManifestImport.cxx +++ b/package/source/manifest/ManifestImport.cxx @@ -32,6 +32,8 @@ #include <ManifestDefines.hxx> #include <Base64Codec.hxx> #include <com/sun/star/xml/sax/XAttributeList.hpp> +#include <com/sun/star/xml/crypto/DigestID.hpp> +#include <com/sun/star/xml/crypto/CipherID.hpp> #include <com/sun/star/beans/PropertyValue.hpp> using namespace com::sun::star::uno; @@ -45,12 +47,14 @@ using ::rtl::OUString; ManifestImport::ManifestImport( vector < Sequence < PropertyValue > > & rNewManVector ) : nNumProperty ( 0 ) , bIgnoreEncryptData ( sal_False ) +, nDerivedKeySize( 0 ) , rManVector ( rNewManVector ) , sFileEntryElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_FILE_ENTRY ) ) , sManifestElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_MANIFEST ) ) , sEncryptionDataElement( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_ENCRYPTION_DATA ) ) , sAlgorithmElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_ALGORITHM ) ) +, sStartKeyAlgElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_START_KEY_GENERATION ) ) , sKeyDerivationElement( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_KEY_DERIVATION ) ) , sCdataAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_CDATA ) ) @@ -61,7 +65,9 @@ ManifestImport::ManifestImport( vector < Sequence < PropertyValue > > & rNewManV , sSaltAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_SALT ) ) , sInitialisationVectorAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_INITIALISATION_VECTOR ) ) , sIterationCountAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_ITERATION_COUNT ) ) +, sKeySizeAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_KEY_SIZE ) ) , sAlgorithmNameAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_ALGORITHM_NAME ) ) +, sStartKeyAlgNameAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_START_KEY_GENERATION_NAME ) ) , sKeyDerivationNameAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_KEY_DERIVATION_NAME ) ) , sChecksumAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_CHECKSUM ) ) , sChecksumTypeAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_CHECKSUM_TYPE ) ) @@ -70,15 +76,33 @@ ManifestImport::ManifestImport( vector < Sequence < PropertyValue > > & rNewManV , sMediaTypeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "MediaType" ) ) , sVersionProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Version" ) ) , sIterationCountProperty ( RTL_CONSTASCII_USTRINGPARAM ( "IterationCount" ) ) +, sDerivedKeySizeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "DerivedKeySize" ) ) , sSaltProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Salt" ) ) , sInitialisationVectorProperty ( RTL_CONSTASCII_USTRINGPARAM ( "InitialisationVector" ) ) , sSizeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Size" ) ) , sDigestProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Digest" ) ) +, sEncryptionAlgProperty ( RTL_CONSTASCII_USTRINGPARAM ( "EncryptionAlgorithm" ) ) +, sStartKeyAlgProperty ( RTL_CONSTASCII_USTRINGPARAM ( "StartKeyAlgorithm" ) ) +, sDigestAlgProperty ( RTL_CONSTASCII_USTRINGPARAM ( "DigestAlgorithm" ) ) , sWhiteSpace ( RTL_CONSTASCII_USTRINGPARAM ( " " ) ) -, sBlowfish ( RTL_CONSTASCII_USTRINGPARAM ( "Blowfish CFB" ) ) -, sPBKDF2 ( RTL_CONSTASCII_USTRINGPARAM ( "PBKDF2" ) ) -, sChecksumType ( RTL_CONSTASCII_USTRINGPARAM ( CHECKSUM_TYPE ) ) + +, sSHA256_URL ( RTL_CONSTASCII_USTRINGPARAM ( SHA256_URL ) ) +, sSHA1_Name ( RTL_CONSTASCII_USTRINGPARAM ( SHA1_NAME ) ) +, sSHA1_URL ( RTL_CONSTASCII_USTRINGPARAM ( SHA1_URL ) ) + +, sSHA256_1k_URL ( RTL_CONSTASCII_USTRINGPARAM ( SHA256_1K_URL ) ) +, sSHA1_1k_Name ( RTL_CONSTASCII_USTRINGPARAM ( SHA1_1K_NAME ) ) +, sSHA1_1k_URL ( RTL_CONSTASCII_USTRINGPARAM ( SHA1_1K_URL ) ) + +, sBlowfish_Name ( RTL_CONSTASCII_USTRINGPARAM ( BLOWFISH_NAME ) ) +, sBlowfish_URL ( RTL_CONSTASCII_USTRINGPARAM ( BLOWFISH_URL ) ) +, sAES128_URL ( RTL_CONSTASCII_USTRINGPARAM ( AES128_URL ) ) +, sAES192_URL ( RTL_CONSTASCII_USTRINGPARAM ( AES192_URL ) ) +, sAES256_URL ( RTL_CONSTASCII_USTRINGPARAM ( AES256_URL ) ) + +, sPBKDF2_Name ( RTL_CONSTASCII_USTRINGPARAM ( PBKDF2_NAME ) ) +, sPBKDF2_URL ( RTL_CONSTASCII_USTRINGPARAM ( PBKDF2_URL ) ) { aStack.reserve( 10 ); } @@ -143,15 +167,32 @@ void SAL_CALL ManifestImport::startElement( const OUString& aName, const uno::Re if ( aConvertedName.equals( sEncryptionDataElement ) ) { // If this element exists, then this stream is encrypted and we need - // to store the initialisation vector, salt and iteration count used + // to import the initialisation vector, salt and iteration count used + nDerivedKeySize = 0; OUString aString = aConvertedAttribs[sChecksumTypeAttribute]; - if ( aString == sChecksumType && !bIgnoreEncryptData ) + if ( !bIgnoreEncryptData ) { - aString = aConvertedAttribs[sChecksumAttribute]; - Sequence < sal_uInt8 > aDecodeBuffer; - Base64Codec::decodeBase64 ( aDecodeBuffer, aString ); - aSequence[nNumProperty].Name = sDigestProperty; - aSequence[nNumProperty++].Value <<= aDecodeBuffer; + if ( aString.equals( sSHA1_1k_Name ) || aString.equals( sSHA1_1k_URL ) ) + { + aSequence[nNumProperty].Name = sDigestAlgProperty; + aSequence[nNumProperty++].Value <<= xml::crypto::DigestID::SHA1_1K; + } + else if ( aString.equals( sSHA256_1k_URL ) ) + { + aSequence[nNumProperty].Name = sDigestAlgProperty; + aSequence[nNumProperty++].Value <<= xml::crypto::DigestID::SHA256_1K; + } + else + bIgnoreEncryptData = sal_True; + + if ( !bIgnoreEncryptData ) + { + aString = aConvertedAttribs[sChecksumAttribute]; + uno::Sequence < sal_Int8 > aDecodeBuffer; + Base64Codec::decodeBase64( aDecodeBuffer, aString ); + aSequence[nNumProperty].Name = sDigestProperty; + aSequence[nNumProperty++].Value <<= aDecodeBuffer; + } } } } @@ -159,38 +200,98 @@ void SAL_CALL ManifestImport::startElement( const OUString& aName, const uno::Re { if ( aConvertedName == sAlgorithmElement ) { - OUString aString = aConvertedAttribs[sAlgorithmNameAttribute]; - if ( aString == sBlowfish && !bIgnoreEncryptData ) + if ( !bIgnoreEncryptData ) { - aString = aConvertedAttribs[sInitialisationVectorAttribute]; - Sequence < sal_uInt8 > aDecodeBuffer; - Base64Codec::decodeBase64 ( aDecodeBuffer, aString ); - aSequence[nNumProperty].Name = sInitialisationVectorProperty; - aSequence[nNumProperty++].Value <<= aDecodeBuffer; + OUString aString = aConvertedAttribs[sAlgorithmNameAttribute]; + if ( aString.equals( sBlowfish_Name ) || aString.equals( sBlowfish_URL ) ) + { + aSequence[nNumProperty].Name = sEncryptionAlgProperty; + aSequence[nNumProperty++].Value <<= xml::crypto::CipherID::BLOWFISH_CFB_8; + } + else if ( aString.equals( sAES256_URL ) ) + { + aSequence[nNumProperty].Name = sEncryptionAlgProperty; + aSequence[nNumProperty++].Value <<= xml::crypto::CipherID::AES_CBC_W3C_PADDING; + OSL_ENSURE( !nDerivedKeySize || nDerivedKeySize == 32, "Unexpected derived key length!" ); + nDerivedKeySize = 32; + } + else if ( aString.equals( sAES192_URL ) ) + { + aSequence[nNumProperty].Name = sEncryptionAlgProperty; + aSequence[nNumProperty++].Value <<= xml::crypto::CipherID::AES_CBC_W3C_PADDING; + OSL_ENSURE( !nDerivedKeySize || nDerivedKeySize == 24, "Unexpected derived key length!" ); + nDerivedKeySize = 24; + } + else if ( aString.equals( sAES128_URL ) ) + { + aSequence[nNumProperty].Name = sEncryptionAlgProperty; + aSequence[nNumProperty++].Value <<= xml::crypto::CipherID::AES_CBC_W3C_PADDING; + OSL_ENSURE( !nDerivedKeySize || nDerivedKeySize == 16, "Unexpected derived key length!" ); + nDerivedKeySize = 16; + } + else + bIgnoreEncryptData = sal_True; + + if ( !bIgnoreEncryptData ) + { + aString = aConvertedAttribs[sInitialisationVectorAttribute]; + uno::Sequence < sal_Int8 > aDecodeBuffer; + Base64Codec::decodeBase64 ( aDecodeBuffer, aString ); + aSequence[nNumProperty].Name = sInitialisationVectorProperty; + aSequence[nNumProperty++].Value <<= aDecodeBuffer; + } } - else - // If we don't recognise the algorithm, then the key derivation info - // is useless to us - bIgnoreEncryptData = sal_True; } else if ( aConvertedName == sKeyDerivationElement ) { - OUString aString = aConvertedAttribs[sKeyDerivationNameAttribute]; - if ( aString == sPBKDF2 && !bIgnoreEncryptData ) + if ( !bIgnoreEncryptData ) + { + OUString aString = aConvertedAttribs[sKeyDerivationNameAttribute]; + if ( aString.equals( sPBKDF2_Name ) || aString.equals( sPBKDF2_URL ) ) + { + aString = aConvertedAttribs[sSaltAttribute]; + uno::Sequence < sal_Int8 > aDecodeBuffer; + Base64Codec::decodeBase64 ( aDecodeBuffer, aString ); + aSequence[nNumProperty].Name = sSaltProperty; + aSequence[nNumProperty++].Value <<= aDecodeBuffer; + + aString = aConvertedAttribs[sIterationCountAttribute]; + aSequence[nNumProperty].Name = sIterationCountProperty; + aSequence[nNumProperty++].Value <<= aString.toInt32(); + + aString = aConvertedAttribs[sKeySizeAttribute]; + if ( aString.getLength() ) + { + sal_Int32 nKey = aString.toInt32(); + OSL_ENSURE( !nDerivedKeySize || nKey == nDerivedKeySize , "Provided derived key length differs from the expected one!" ); + nDerivedKeySize = nKey; + } + else if ( !nDerivedKeySize ) + nDerivedKeySize = 16; + else if ( nDerivedKeySize != 16 ) + OSL_ENSURE( sal_False, "Default derived key length differs from the expected one!" ); + + aSequence[nNumProperty].Name = sDerivedKeySizeProperty; + aSequence[nNumProperty++].Value <<= nDerivedKeySize; + } + else + bIgnoreEncryptData = sal_True; + } + } + else if ( aConvertedName == sStartKeyAlgElement ) + { + OUString aString = aConvertedAttribs[sStartKeyAlgNameAttribute]; + if ( aString.equals( sSHA256_URL ) ) + { + aSequence[nNumProperty].Name = sStartKeyAlgProperty; + aSequence[nNumProperty++].Value <<= xml::crypto::DigestID::SHA256; + } + else if ( aString.equals( sSHA1_Name ) || aString.equals( sSHA1_URL ) ) { - aString = aConvertedAttribs[sSaltAttribute]; - Sequence < sal_uInt8 > aDecodeBuffer; - Base64Codec::decodeBase64 ( aDecodeBuffer, aString ); - aSequence[nNumProperty].Name = sSaltProperty; - aSequence[nNumProperty++].Value <<= aDecodeBuffer; - - aString = aConvertedAttribs[sIterationCountAttribute]; - aSequence[nNumProperty].Name = sIterationCountProperty; - aSequence[nNumProperty++].Value <<= aString.toInt32(); + aSequence[nNumProperty].Name = sStartKeyAlgProperty; + aSequence[nNumProperty++].Value <<= xml::crypto::DigestID::SHA1; } else - // If we don't recognise the key derivation technique, then the - // algorithm info is useless to us bIgnoreEncryptData = sal_True; } } diff --git a/package/source/manifest/ManifestImport.hxx b/package/source/manifest/ManifestImport.hxx index 4b74d903ee76..1c646a7952a6 100644 --- a/package/source/manifest/ManifestImport.hxx +++ b/package/source/manifest/ManifestImport.hxx @@ -65,12 +65,14 @@ protected: sal_Int16 nNumProperty; ManifestStack aStack; sal_Bool bIgnoreEncryptData; + sal_Int32 nDerivedKeySize; ::std::vector < ::com::sun::star::uno::Sequence < ::com::sun::star::beans::PropertyValue > > & rManVector; const ::rtl::OUString sFileEntryElement; const ::rtl::OUString sManifestElement; const ::rtl::OUString sEncryptionDataElement; const ::rtl::OUString sAlgorithmElement; + const ::rtl::OUString sStartKeyAlgElement; const ::rtl::OUString sKeyDerivationElement; const ::rtl::OUString sCdataAttribute; @@ -81,7 +83,9 @@ protected: const ::rtl::OUString sSaltAttribute; const ::rtl::OUString sInitialisationVectorAttribute; const ::rtl::OUString sIterationCountAttribute; + const ::rtl::OUString sKeySizeAttribute; const ::rtl::OUString sAlgorithmNameAttribute; + const ::rtl::OUString sStartKeyAlgNameAttribute; const ::rtl::OUString sKeyDerivationNameAttribute; const ::rtl::OUString sChecksumAttribute; const ::rtl::OUString sChecksumTypeAttribute; @@ -90,15 +94,33 @@ protected: const ::rtl::OUString sMediaTypeProperty; const ::rtl::OUString sVersionProperty; const ::rtl::OUString sIterationCountProperty; + const ::rtl::OUString sDerivedKeySizeProperty; const ::rtl::OUString sSaltProperty; const ::rtl::OUString sInitialisationVectorProperty; const ::rtl::OUString sSizeProperty; const ::rtl::OUString sDigestProperty; + const ::rtl::OUString sEncryptionAlgProperty; + const ::rtl::OUString sStartKeyAlgProperty; + const ::rtl::OUString sDigestAlgProperty; const ::rtl::OUString sWhiteSpace; - const ::rtl::OUString sBlowfish; - const ::rtl::OUString sPBKDF2; - const ::rtl::OUString sChecksumType; + + const ::rtl::OUString sSHA256_URL; + const ::rtl::OUString sSHA1_Name; + const ::rtl::OUString sSHA1_URL; + + const ::rtl::OUString sSHA256_1k_URL; + const ::rtl::OUString sSHA1_1k_Name; + const ::rtl::OUString sSHA1_1k_URL; + + const ::rtl::OUString sBlowfish_Name; + const ::rtl::OUString sBlowfish_URL; + const ::rtl::OUString sAES128_URL; + const ::rtl::OUString sAES192_URL; + const ::rtl::OUString sAES256_URL; + + const ::rtl::OUString sPBKDF2_Name; + const ::rtl::OUString sPBKDF2_URL; ::rtl::OUString PushNameAndNamespaces( const ::rtl::OUString& aName, diff --git a/package/source/manifest/UnoRegister.cxx b/package/source/manifest/UnoRegister.cxx index fc0781c11088..c5734977de46 100644 --- a/package/source/manifest/UnoRegister.cxx +++ b/package/source/manifest/UnoRegister.cxx @@ -37,6 +37,7 @@ #include <zipfileaccess.hxx> +using namespace ::com::sun::star; using namespace ::com::sun::star::uno; using namespace ::com::sun::star::beans; using namespace ::com::sun::star::lang; @@ -57,9 +58,9 @@ extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory( const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ ) { void * pRet = 0; - Reference< XMultiServiceFactory > xSMgr( + uno::Reference< XMultiServiceFactory > xSMgr( reinterpret_cast< XMultiServiceFactory * >( pServiceManager ) ); - Reference< XSingleServiceFactory > xFactory; + uno::Reference< XSingleServiceFactory > xFactory; if (ManifestReader::static_getImplementationName().compareToAscii( pImplName ) == 0) xFactory = ManifestReader::createServiceFactory ( xSMgr ); diff --git a/package/source/xstor/owriteablestream.cxx b/package/source/xstor/owriteablestream.cxx index 89a6b5b17c53..56b308b60478 100644 --- a/package/source/xstor/owriteablestream.cxx +++ b/package/source/xstor/owriteablestream.cxx @@ -47,16 +47,18 @@ #include <comphelper/storagehelper.hxx> #include <comphelper/ofopxmlhelper.hxx> +#include <rtl/digest.h> +#include <rtl/logfile.hxx> +#include <rtl/instance.hxx> + +#include <PackageConstants.hxx> +#include <mutexholder.hxx> + #include "selfterminatefilestream.hxx" #include "owriteablestream.hxx" #include "oseekinstream.hxx" -#include "mutexholder.hxx" #include "xstorage.hxx" -#include <rtl/digest.h> -#include <rtl/logfile.hxx> -#include <rtl/instance.hxx> - // since the copying uses 32000 blocks usually, it makes sense to have a smaller size #define MAX_STORCACHE_SIZE 30000 @@ -111,15 +113,14 @@ namespace { //----------------------------------------------- void SetEncryptionKeyProperty_Impl( const uno::Reference< beans::XPropertySet >& xPropertySet, - const uno::Sequence< sal_Int8 >& aKey ) + const uno::Sequence< beans::NamedValue >& aKey ) { OSL_ENSURE( xPropertySet.is(), "No property set is provided!\n" ); if ( !xPropertySet.is() ) throw uno::RuntimeException(); - ::rtl::OUString aString_EncryptionKey (RTL_CONSTASCII_USTRINGPARAM("EncryptionKey") ); try { - xPropertySet->setPropertyValue( aString_EncryptionKey, uno::makeAny( aKey ) ); + xPropertySet->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( STORAGE_ENCRYPTION_KEYS_PROPERTY ) ), uno::makeAny( aKey ) ); } catch ( const uno::Exception& rException ) { @@ -137,9 +138,8 @@ uno::Any GetEncryptionKeyProperty_Impl( const uno::Reference< beans::XPropertySe if ( !xPropertySet.is() ) throw uno::RuntimeException(); - ::rtl::OUString aString_EncryptionKey (RTL_CONSTASCII_USTRINGPARAM("EncryptionKey") ); try { - return xPropertySet->getPropertyValue( aString_EncryptionKey ); + return xPropertySet->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( STORAGE_ENCRYPTION_KEYS_PROPERTY ) ) ); } catch ( const uno::Exception& rException ) { @@ -152,16 +152,65 @@ uno::Any GetEncryptionKeyProperty_Impl( const uno::Reference< beans::XPropertySe } //----------------------------------------------- -sal_Bool SequencesEqual( uno::Sequence< sal_Int8 > aSequence1, uno::Sequence< sal_Int8 > aSequence2 ) +bool SequencesEqual( const uno::Sequence< sal_Int8 >& aSequence1, const uno::Sequence< sal_Int8 >& aSequence2 ) { if ( aSequence1.getLength() != aSequence2.getLength() ) - return sal_False; + return false; for ( sal_Int32 nInd = 0; nInd < aSequence1.getLength(); nInd++ ) if ( aSequence1[nInd] != aSequence2[nInd] ) - return sal_False; + return false; + + return true; +} + +//----------------------------------------------- +bool SequencesEqual( const uno::Sequence< beans::NamedValue >& aSequence1, const uno::Sequence< beans::NamedValue >& aSequence2 ) +{ + if ( aSequence1.getLength() != aSequence2.getLength() ) + return false; + + for ( sal_Int32 nInd = 0; nInd < aSequence1.getLength(); nInd++ ) + { + bool bHasMember = false; + uno::Sequence< sal_Int8 > aMember1; + sal_Int32 nMember1 = 0; + if ( ( aSequence1[nInd].Value >>= aMember1 ) ) + { + for ( sal_Int32 nInd2 = 0; nInd2 < aSequence2.getLength(); nInd2++ ) + { + if ( aSequence1[nInd].Name.equals( aSequence2[nInd2].Name ) ) + { + bHasMember = true; + + uno::Sequence< sal_Int8 > aMember2; + if ( !( aSequence2[nInd2].Value >>= aMember2 ) || !SequencesEqual( aMember1, aMember2 ) ) + return false; + } + } + } + else if ( ( aSequence1[nInd].Value >>= nMember1 ) ) + { + for ( sal_Int32 nInd2 = 0; nInd2 < aSequence2.getLength(); nInd2++ ) + { + if ( aSequence1[nInd].Name.equals( aSequence2[nInd2].Name ) ) + { + bHasMember = true; - return sal_True; + sal_Int32 nMember2 = 0; + if ( !( aSequence2[nInd2].Value >>= nMember2 ) || nMember1 != nMember2 ) + return false; + } + } + } + else + return false; + + if ( !bHasMember ) + return false; + } + + return true; } //----------------------------------------------- @@ -395,7 +444,7 @@ sal_Bool OWriteStream_Impl::IsEncrypted() // since a new key set to the package stream it should not be removed except the case when // the stream becomes nonencrypted - uno::Sequence< sal_Int8 > aKey; + uno::Sequence< beans::NamedValue > aKey; if ( bToBeEncr ) GetEncryptionKeyProperty_Impl( xPropSet ) >>= aKey; @@ -822,8 +871,8 @@ void OWriteStream_Impl::InsertStreamDirectly( const uno::Reference< io::XInputSt throw uno::RuntimeException(); // set to be encrypted but do not use encryption key - xPropertySet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("EncryptionKey") ), - uno::makeAny( uno::Sequence< sal_Int8 >() ) ); + xPropertySet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( STORAGE_ENCRYPTION_KEYS_PROPERTY ) ), + uno::makeAny( uno::Sequence< beans::NamedValue >() ) ); xPropertySet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Encrypted") ), uno::makeAny( sal_True ) ); } @@ -921,8 +970,8 @@ void OWriteStream_Impl::Commit() throw uno::RuntimeException(); // set to be encrypted but do not use encryption key - xPropertySet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("EncryptionKey") ), - uno::makeAny( uno::Sequence< sal_Int8 >() ) ); + xPropertySet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( STORAGE_ENCRYPTION_KEYS_PROPERTY ) ), + uno::makeAny( uno::Sequence< beans::NamedValue >() ) ); xPropertySet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Encrypted") ), uno::makeAny( sal_True ) ); } @@ -931,8 +980,8 @@ void OWriteStream_Impl::Commit() if ( m_nStorageType != embed::StorageFormats::PACKAGE ) throw uno::RuntimeException(); - xPropertySet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("EncryptionKey") ), - uno::makeAny( m_aEncryptionData.getUnpackedValueOrDefault( PACKAGE_ENCRYPTIONDATA_SHA1UTF8, uno::Sequence< sal_Int8 >() ) ) ); + xPropertySet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( STORAGE_ENCRYPTION_KEYS_PROPERTY ) ), + uno::makeAny( m_aEncryptionData.getAsConstNamedValueList() ) ); } // the stream should be free soon, after package is stored @@ -1265,7 +1314,7 @@ uno::Reference< io::XStream > OWriteStream_Impl::GetStream( sal_Int32 nStreamMod } else { - SetEncryptionKeyProperty_Impl( xPropertySet, aEncryptionData.getUnpackedValueOrDefault( PACKAGE_ENCRYPTIONDATA_SHA1UTF8, uno::Sequence< sal_Int8 >() ) ); + SetEncryptionKeyProperty_Impl( xPropertySet, aEncryptionData.getAsConstNamedValueList() ); try { xResultStream = GetStream_Impl( nStreamMode, bHierarchyAccess ); @@ -1274,55 +1323,22 @@ uno::Reference< io::XStream > OWriteStream_Impl::GetStream( sal_Int32 nStreamMod m_bHasCachedEncryptionData = sal_True; m_aEncryptionData = aEncryptionData; } - catch( const packages::WrongPasswordException& ) - { - // retry with different encoding - SetEncryptionKeyProperty_Impl( xPropertySet, aEncryptionData.getUnpackedValueOrDefault( PACKAGE_ENCRYPTIONDATA_SHA1MS1252, uno::Sequence< sal_Int8 >() ) ); - try { - // the stream must be cashed to be resaved - xResultStream = GetStream_Impl( nStreamMode | embed::ElementModes::SEEKABLE, bHierarchyAccess ); - - m_bUseCommonEncryption = sal_False; // very important to set it to false - m_bHasCachedEncryptionData = sal_True; - m_aEncryptionData = aEncryptionData; - - // the stream must be resaved with new password encryption - if ( nStreamMode & embed::ElementModes::WRITE ) - { - FillTempGetFileName(); - m_bHasDataToFlush = sal_True; - - // TODO/LATER: should the notification be done? - if ( m_pParent ) - m_pParent->m_bIsModified = sal_True; - } - } - catch( const packages::WrongPasswordException& rWrongPasswordException ) - { - SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< sal_Int8 >() ); - AddLog( rWrongPasswordException.Message ); - AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) ); - throw; - } - catch ( const uno::Exception& rException ) - { - AddLog( rException.Message ); - AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Quiet exception" ) ) ); - - OSL_FAIL( "Can't write encryption related properties!\n" ); - SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< sal_Int8 >() ); - throw io::IOException(); // TODO: - } - } - catch( const uno::Exception& rException ) + catch( const packages::WrongPasswordException& rWrongPasswordException ) { - SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< sal_Int8 >() ); - - AddLog( rException.Message ); + SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() ); + AddLog( rWrongPasswordException.Message ); AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) ); throw; } + catch ( const uno::Exception& rException ) + { + AddLog( rException.Message ); + AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Quiet exception" ) ) ); + OSL_FAIL( "Can't write encryption related properties!\n" ); + SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() ); + throw io::IOException(); // TODO: + } } OSL_ENSURE( xResultStream.is(), "In case stream can not be retrieved an exception must be thrown!\n" ); @@ -1625,8 +1641,7 @@ void OWriteStream_Impl::GetCopyOfLastCommit( uno::Reference< io::XStream >& xTar { // TODO: introduce last commited cashed password information and use it here // that means "use common pass" also should be remembered on flash - uno::Sequence< sal_Int8 > aNewKey = aEncryptionData.getUnpackedValueOrDefault( PACKAGE_ENCRYPTIONDATA_SHA1UTF8, uno::Sequence< sal_Int8 >() ); - uno::Sequence< sal_Int8 > aOldKey = aEncryptionData.getUnpackedValueOrDefault( PACKAGE_ENCRYPTIONDATA_SHA1MS1252, uno::Sequence< sal_Int8 >() ); + uno::Sequence< beans::NamedValue > aKey = aEncryptionData.getAsConstNamedValueList(); uno::Reference< beans::XPropertySet > xProps( m_xPackageStream, uno::UNO_QUERY ); if ( !xProps.is() ) @@ -1637,9 +1652,9 @@ void OWriteStream_Impl::GetCopyOfLastCommit( uno::Reference< io::XStream >& xTar if ( !bEncr ) throw packages::NoEncryptionException(); - uno::Sequence< sal_Int8 > aEncrKey; - xProps->getPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("EncryptionKey") ) ) >>= aEncrKey; - if ( !SequencesEqual( aNewKey, aEncrKey ) && !SequencesEqual( aOldKey, aEncrKey ) ) + uno::Sequence< beans::NamedValue > aPackKey; + xProps->getPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( STORAGE_ENCRYPTION_KEYS_PROPERTY ) ) ) >>= aPackKey; + if ( !SequencesEqual( aKey, aPackKey ) ) throw packages::WrongPasswordException(); // the correct key must be set already @@ -1648,7 +1663,7 @@ void OWriteStream_Impl::GetCopyOfLastCommit( uno::Reference< io::XStream >& xTar else { uno::Reference< beans::XPropertySet > xPropertySet( m_xPackageStream, uno::UNO_QUERY ); - SetEncryptionKeyProperty_Impl( xPropertySet, aEncryptionData.getUnpackedValueOrDefault( PACKAGE_ENCRYPTIONDATA_SHA1UTF8, uno::Sequence< sal_Int8 >() ) ); + SetEncryptionKeyProperty_Impl( xPropertySet, aEncryptionData.getAsConstNamedValueList() ); try { xDataToCopy = m_xPackageStream->getDataStream(); @@ -1656,42 +1671,19 @@ void OWriteStream_Impl::GetCopyOfLastCommit( uno::Reference< io::XStream >& xTar if ( !xDataToCopy.is() ) { OSL_FAIL( "Encrypted ZipStream must already have input stream inside!\n" ); - SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< sal_Int8 >() ); - } - } - catch( const packages::WrongPasswordException& rWrongPasswordException ) - { - SetEncryptionKeyProperty_Impl( xPropertySet, aEncryptionData.getUnpackedValueOrDefault( PACKAGE_ENCRYPTIONDATA_SHA1MS1252, uno::Sequence< sal_Int8 >() ) ); - try { - xDataToCopy = m_xPackageStream->getDataStream(); - - if ( !xDataToCopy.is() ) - { - OSL_FAIL( "Encrypted ZipStream must already have input stream inside!\n" ); - SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< sal_Int8 >() ); - AddLog( rWrongPasswordException.Message ); - AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) ); - throw; - } - } - catch( const uno::Exception& rException ) - { - SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< sal_Int8 >() ); - AddLog( rException.Message ); - AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) ); - throw; + SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() ); } } catch( const uno::Exception& rException ) { OSL_FAIL( "Can't open encrypted stream!\n" ); - SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< sal_Int8 >() ); + SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() ); AddLog( rException.Message ); AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) ); throw; } - SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< sal_Int8 >() ); + SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() ); } // in case of new inserted package stream it is possible that input stream still was not set diff --git a/package/source/xstor/xstorage.cxx b/package/source/xstor/xstorage.cxx index ed25369717cb..9c0c117f5e35 100644 --- a/package/source/xstor/xstorage.cxx +++ b/package/source/xstor/xstorage.cxx @@ -46,6 +46,7 @@ #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp> #include <com/sun/star/beans/NamedValue.hpp> +#include <PackageConstants.hxx> #include <cppuhelper/typeprovider.hxx> #include <cppuhelper/exc_hlp.hxx> @@ -572,7 +573,7 @@ void OStorage_Impl::GetStorageProperties() if ( !m_bControlMediaType ) { uno::Reference< beans::XPropertySet > xPackageProps( m_xPackage, uno::UNO_QUERY_THROW ); - xPackageProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MediaTypeFallbackUsed" ) ) ) >>= m_bMTFallbackUsed; + xPackageProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( MEDIATYPE_FALLBACK_USED_PROPERTY ) ) ) >>= m_bMTFallbackUsed; xProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ) ) ) >>= m_aMediaType; m_bControlMediaType = sal_True; @@ -750,9 +751,17 @@ void OStorage_Impl::CopyToStorage( const uno::Reference< embed::XStorage >& xDes { try { - uno::Reference< embed::XEncryptionProtectedSource2 > xEncr( xDest, uno::UNO_QUERY ); + uno::Reference< embed::XEncryptionProtectedStorage > xEncr( xDest, uno::UNO_QUERY ); if ( xEncr.is() ) + { xEncr->setEncryptionData( GetCommonRootEncryptionData().getAsConstNamedValueList() ); + + uno::Sequence< beans::NamedValue > aAlgorithms; + uno::Reference< beans::XPropertySet > xPackPropSet( m_xPackage, uno::UNO_QUERY_THROW ); + xPackPropSet->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ENCRYPTION_ALGORITHMS_PROPERTY ) ) ) + >>= aAlgorithms; + xEncr->setEncryptionAlgorithms( aAlgorithms ); + } } catch( const packages::NoEncryptionException& rNoEncryptionException ) { @@ -985,7 +994,9 @@ void OStorage_Impl::CopyStorageElement( SotElement_Impl* pElement, AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Handled exception" ) ) ); // If the common storage password does not allow to open the stream - // it must be copyed in raw way + // it could be copyed in raw way, the problem is that the StartKey should be the same + // in the ODF1.2 package, so an invalid package could be produced if the stream + // is copied from ODF1.1 package, where it is allowed to have different StartKeys uno::Reference< embed::XStorageRawAccess > xRawDest( xDest, uno::UNO_QUERY_THROW ); uno::Reference< io::XInputStream > xRawInStream = pElement->m_pStream->GetRawInStream(); xRawDest->insertRawEncrStreamElement( aName, xRawInStream ); @@ -2278,7 +2289,8 @@ uno::Any SAL_CALL OStorage::queryInterface( const uno::Type& rType ) ( rType , static_cast<embed::XStorageRawAccess*> ( this ) , static_cast<embed::XEncryptionProtectedSource*> ( this ) - , static_cast<embed::XEncryptionProtectedSource2*> ( this ) ); + , static_cast<embed::XEncryptionProtectedSource2*> ( this ) + , static_cast<embed::XEncryptionProtectedStorage*> ( this ) ); } else { @@ -2338,6 +2350,7 @@ uno::Sequence< uno::Type > SAL_CALL OStorage::getTypes() , ::getCppuType( ( const uno::Reference< embed::XTransactedObject >* )NULL ) , ::getCppuType( ( const uno::Reference< embed::XTransactionBroadcaster >* )NULL ) , ::getCppuType( ( const uno::Reference< util::XModifiable >* )NULL ) + , ::getCppuType( ( const uno::Reference< embed::XEncryptionProtectedStorage >* )NULL ) , ::getCppuType( ( const uno::Reference< embed::XEncryptionProtectedSource2 >* )NULL ) , ::getCppuType( ( const uno::Reference< embed::XEncryptionProtectedSource >* )NULL ) , ::getCppuType( ( const uno::Reference< beans::XPropertySet >* )NULL ) ); @@ -4697,18 +4710,23 @@ void SAL_CALL OStorage::removeEncryption() // TODO: check if the password is valid // update all streams that was encrypted with old password - uno::Reference< beans::XPropertySet > xPackPropSet( m_pImpl->m_xPackage, uno::UNO_QUERY ); - if ( !xPackPropSet.is() ) - throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); - + uno::Reference< beans::XPropertySet > xPackPropSet( m_pImpl->m_xPackage, uno::UNO_QUERY_THROW ); try { - xPackPropSet->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "EncryptionKey" ) ), - uno::makeAny( uno::Sequence< sal_Int8 >() ) ); + xPackPropSet->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( STORAGE_ENCRYPTION_KEYS_PROPERTY ) ), + uno::makeAny( uno::Sequence< beans::NamedValue >() ) ); m_pImpl->m_bHasCommonEncryptionData = sal_False; m_pImpl->m_aCommonEncryptionData.clear(); } + catch( const uno::RuntimeException& rRException ) + { + m_pImpl->AddLog( rRException.Message ); + m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) ); + + OSL_ENSURE( sal_False, "The call must not fail, it is pretty simple!" ); + throw; + } catch( const uno::Exception& rException ) { m_pImpl->AddLog( rException.Message ); @@ -4762,21 +4780,18 @@ void SAL_CALL OStorage::setEncryptionData( const uno::Sequence< beans::NamedValu m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) ); uno::Any aCaught( ::cppu::getCaughtException() ); - throw lang::WrappedTargetException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Can not open package!\n" ) ), - uno::Reference< uno::XInterface >( static_cast< OWeakObject* >( this ), - uno::UNO_QUERY ), - aCaught ); + throw lang::WrappedTargetRuntimeException( + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Can not open package!\n" ) ), + uno::Reference< uno::XInterface >( static_cast< OWeakObject* >( this ), uno::UNO_QUERY ), + aCaught ); } - uno::Reference< beans::XPropertySet > xPackPropSet( m_pImpl->m_xPackage, uno::UNO_QUERY ); - if ( !xPackPropSet.is() ) - throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); - + uno::Reference< beans::XPropertySet > xPackPropSet( m_pImpl->m_xPackage, uno::UNO_QUERY_THROW ); try { ::comphelper::SequenceAsHashMap aEncryptionMap( aEncryptionData ); - xPackPropSet->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "EncryptionKey" ) ), - uno::makeAny( aEncryptionMap.getUnpackedValueOrDefault( PACKAGE_ENCRYPTIONDATA_SHA1UTF8, uno::Sequence< sal_Int8 >() ) ) ); + xPackPropSet->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( STORAGE_ENCRYPTION_KEYS_PROPERTY ) ), + uno::makeAny( aEncryptionMap.getAsConstNamedValueList() ) ); m_pImpl->m_bHasCommonEncryptionData = sal_True; m_pImpl->m_aCommonEncryptionData = aEncryptionMap; @@ -4789,7 +4804,148 @@ void SAL_CALL OStorage::setEncryptionData( const uno::Sequence< beans::NamedValu throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); } } +} + +//____________________________________________________________________________________________________ +// XEncryptionProtectedStorage +//____________________________________________________________________________________________________ + +//----------------------------------------------- +void SAL_CALL OStorage::setEncryptionAlgorithms( const uno::Sequence< beans::NamedValue >& aAlgorithms ) + throw (lang::IllegalArgumentException, uno::RuntimeException) +{ + RTL_LOGFILE_CONTEXT( aLog, "package (mv76033) OStorage::setEncryptionAlgorithms" ); + + ::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() ); + + if ( !m_pImpl ) + { + ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) ); + throw lang::DisposedException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); + } + + if ( m_pData->m_nStorageType != embed::StorageFormats::PACKAGE ) + throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); // the interface must be visible only for package storage + + if ( !aAlgorithms.getLength() ) + throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Unexpected empty encryption algorithms list!") ), uno::Reference< uno::XInterface >() ); + + OSL_ENSURE( m_pData->m_bIsRoot, "setEncryptionAlgorithms() method is not available for nonroot storages!\n" ); + if ( m_pData->m_bIsRoot ) + { + try { + m_pImpl->ReadContents(); + } + catch ( uno::RuntimeException& aRuntimeException ) + { + m_pImpl->AddLog( aRuntimeException.Message ); + m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) ); + throw; + } + catch ( uno::Exception& aException ) + { + m_pImpl->AddLog( aException.Message ); + m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) ); + + uno::Any aCaught( ::cppu::getCaughtException() ); + throw lang::WrappedTargetException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Can not open package!\n" ) ), + uno::Reference< uno::XInterface >( static_cast< OWeakObject* >( this ), + uno::UNO_QUERY ), + aCaught ); + } + + uno::Reference< beans::XPropertySet > xPackPropSet( m_pImpl->m_xPackage, uno::UNO_QUERY_THROW ); + try + { + xPackPropSet->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ENCRYPTION_ALGORITHMS_PROPERTY ) ), + uno::makeAny( aAlgorithms ) ); + } + catch ( uno::RuntimeException& aRuntimeException ) + { + m_pImpl->AddLog( aRuntimeException.Message ); + m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) ); + throw; + } + catch( lang::IllegalArgumentException& aIAException ) + { + m_pImpl->AddLog( aIAException.Message ); + m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) ); + + throw; + } + catch( uno::Exception& aException ) + { + m_pImpl->AddLog( aException.Message ); + m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) ); + + throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); + } + } +} + +//----------------------------------------------- +uno::Sequence< beans::NamedValue > SAL_CALL OStorage::getEncryptionAlgorithms() + throw (uno::RuntimeException) +{ + RTL_LOGFILE_CONTEXT( aLog, "package (mv76033) OStorage::getEncryptionAlgorithms" ); + ::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() ); + + if ( !m_pImpl ) + { + ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) ); + throw lang::DisposedException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); + } + + if ( m_pData->m_nStorageType != embed::StorageFormats::PACKAGE ) + throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); // the interface must be visible only for package storage + + uno::Sequence< beans::NamedValue > aResult; + OSL_ENSURE( m_pData->m_bIsRoot, "getEncryptionAlgorithms() method is not available for nonroot storages!\n" ); + if ( m_pData->m_bIsRoot ) + { + try { + m_pImpl->ReadContents(); + } + catch ( uno::RuntimeException& aRuntimeException ) + { + m_pImpl->AddLog( aRuntimeException.Message ); + m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) ); + throw; + } + catch ( uno::Exception& aException ) + { + m_pImpl->AddLog( aException.Message ); + m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) ); + + uno::Any aCaught( ::cppu::getCaughtException() ); + throw lang::WrappedTargetException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Can not open package!\n" ) ), + uno::Reference< uno::XInterface >( static_cast< OWeakObject* >( this ), + uno::UNO_QUERY ), + aCaught ); + } + + uno::Reference< beans::XPropertySet > xPackPropSet( m_pImpl->m_xPackage, uno::UNO_QUERY_THROW ); + try + { + xPackPropSet->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ENCRYPTION_ALGORITHMS_PROPERTY ) ) ) >>= aResult; + } + catch ( uno::RuntimeException& aRuntimeException ) + { + m_pImpl->AddLog( aRuntimeException.Message ); + m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) ); + throw; + } + catch( uno::Exception& aException ) + { + m_pImpl->AddLog( aException.Message ); + m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) ); + + throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); + } + } + + return aResult; } @@ -4864,13 +5020,13 @@ void SAL_CALL OStorage::setPropertyValue( const ::rtl::OUString& aPropertyName, m_pImpl->m_bIsModified = sal_True; } } - else if ( ( m_pData->m_bIsRoot && ( aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "HasEncryptedEntries" ) ) - || aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "HasNonEncryptedEntries" ) ) - || aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsInconsistent" ) ) + else if ( ( m_pData->m_bIsRoot && ( aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( HAS_ENCRYPTED_ENTRIES_PROPERTY ) ) + || aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( HAS_NONENCRYPTED_ENTRIES_PROPERTY ) ) + || aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( IS_INCONSISTENT_PROPERTY ) ) || aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "URL" ) ) || aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "RepairPackage" ) ) ) ) || aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsRoot" ) ) - || aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "MediaTypeFallbackUsed" ) ) ) + || aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( MEDIATYPE_FALLBACK_USED_PROPERTY ) ) ) throw beans::PropertyVetoException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); else throw beans::UnknownPropertyException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); @@ -4944,7 +5100,7 @@ uno::Any SAL_CALL OStorage::getPropertyValue( const ::rtl::OUString& aPropertyNa if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE && ( aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "MediaType" ) ) - || aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "MediaTypeFallbackUsed" ) ) + || aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( MEDIATYPE_FALLBACK_USED_PROPERTY ) ) || aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Version" ) ) ) ) { try @@ -5001,9 +5157,9 @@ uno::Any SAL_CALL OStorage::getPropertyValue( const ::rtl::OUString& aPropertyNa return uno::makeAny( sal_False ); // RepairPackage } else if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE - && ( aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "HasEncryptedEntries" ) ) - || aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "HasNonEncryptedEntries" ) ) - || aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "IsInconsistent" ) ) ) ) + && ( aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( HAS_ENCRYPTED_ENTRIES_PROPERTY ) ) + || aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( HAS_NONENCRYPTED_ENTRIES_PROPERTY ) ) + || aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( IS_INCONSISTENT_PROPERTY ) ) ) ) { try { m_pImpl->ReadContents(); diff --git a/package/source/xstor/xstorage.hxx b/package/source/xstor/xstorage.hxx index 2811616424c0..f472444556c2 100644 --- a/package/source/xstor/xstorage.hxx +++ b/package/source/xstor/xstorage.hxx @@ -37,7 +37,7 @@ #include <com/sun/star/embed/XTransactedObject.hpp> #include <com/sun/star/embed/XTransactionBroadcaster.hpp> #include <com/sun/star/embed/XClassifiedObject.hpp> -#include <com/sun/star/embed/XEncryptionProtectedSource2.hpp> +#include <com/sun/star/embed/XEncryptionProtectedStorage.hpp> #include <com/sun/star/embed/XRelationshipAccess.hpp> #include <com/sun/star/util/XModifiable.hpp> #include <com/sun/star/container/XNameAccess.hpp> @@ -297,7 +297,7 @@ class OStorage : public ::com::sun::star::lang::XTypeProvider , public ::com::sun::star::embed::XTransactedObject , public ::com::sun::star::embed::XTransactionBroadcaster , public ::com::sun::star::util::XModifiable - , public ::com::sun::star::embed::XEncryptionProtectedSource2 + , public ::com::sun::star::embed::XEncryptionProtectedStorage , public ::com::sun::star::beans::XPropertySet , public ::com::sun::star::embed::XOptimizedStorage , public ::com::sun::star::embed::XRelationshipAccess @@ -647,6 +647,13 @@ public: throw ( ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException ); + //____________________________________________________________________________________________________ + // XEncryptionProtectedStorage + //____________________________________________________________________________________________________ + + virtual void SAL_CALL setEncryptionAlgorithms( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& aAlgorithms ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException); + + virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > SAL_CALL getEncryptionAlgorithms() throw (::com::sun::star::uno::RuntimeException); //____________________________________________________________________________________________________ // XPropertySet diff --git a/package/source/zipapi/Deflater.cxx b/package/source/zipapi/Deflater.cxx index cd90de5b09e3..30318ce5c520 100644 --- a/package/source/zipapi/Deflater.cxx +++ b/package/source/zipapi/Deflater.cxx @@ -95,7 +95,7 @@ sal_Int32 Deflater::doDeflateBytes (uno::Sequence < sal_Int8 > &rBuffer, sal_Int pStream->avail_in = nLength; pStream->avail_out = nNewLength; -#ifdef SYSTEM_ZLIB +#if defined SYSTEM_ZLIB || !defined ZLIB_PREFIX nResult = deflateParams(pStream, nLevel, nStrategy); #else nResult = z_deflateParams(pStream, nLevel, nStrategy); @@ -121,7 +121,7 @@ sal_Int32 Deflater::doDeflateBytes (uno::Sequence < sal_Int8 > &rBuffer, sal_Int pStream->avail_in = nLength; pStream->avail_out = nNewLength; -#ifdef SYSTEM_ZLIB +#if defined SYSTEM_ZLIB || !defined ZLIB_PREFIX nResult = deflate(pStream, bFinish ? Z_FINISH : Z_NO_FLUSH); #else nResult = z_deflate(pStream, bFinish ? Z_FINISH : Z_NO_FLUSH); @@ -190,7 +190,7 @@ sal_Int32 SAL_CALL Deflater::getTotalOut( ) } void SAL_CALL Deflater::reset( ) { -#ifdef SYSTEM_ZLIB +#if defined SYSTEM_ZLIB || !defined ZLIB_PREFIXB deflateReset(pStream); #else z_deflateReset(pStream); @@ -203,7 +203,7 @@ void SAL_CALL Deflater::end( ) { if (pStream != NULL) { -#ifdef SYSTEM_ZLIB +#if defined SYSTEM_ZLIB || !defined ZLIB_PREFIX deflateEnd(pStream); #else z_deflateEnd(pStream); diff --git a/package/source/zipapi/EntryInputStream.cxx b/package/source/zipapi/EntryInputStream.cxx deleted file mode 100644 index 047f1716e0f5..000000000000 --- a/package/source/zipapi/EntryInputStream.cxx +++ /dev/null @@ -1,205 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/************************************************************************* - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * Copyright 2000, 2010 Oracle and/or its affiliates. - * - * OpenOffice.org - a multi-platform office productivity suite - * - * This file is part of OpenOffice.org. - * - * OpenOffice.org is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License version 3 - * only, as published by the Free Software Foundation. - * - * OpenOffice.org is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License version 3 for more details - * (a copy is included in the LICENSE file that accompanied this code). - * - * You should have received a copy of the GNU Lesser General Public License - * version 3 along with OpenOffice.org. If not, see - * <http://www.openoffice.org/license.html> - * for a copy of the LGPLv3 License. - * - ************************************************************************/ - -// MARKER(update_precomp.py): autogen include statement, do not remove -#include "precompiled_package.hxx" -#include <EntryInputStream.hxx> -#include <com/sun/star/packages/zip/ZipConstants.hpp> -#include <rtl/cipher.h> -#include <rtl/digest.h> -#include <memory.h> // for memcpy - -using namespace com::sun::star; -using namespace com::sun::star::uno; -using namespace com::sun::star::packages::zip; -using namespace com::sun::star::packages::zip::ZipConstants; - -using ::rtl::OUString; - -/** Provides access to the compressed data in a zipfile. - * - * uncompresses the stream into memory and seeks on it 'in memory' - * This and the ZipPackageBuffer used in the ZipOutputStream are memory hogs - * and will hopefully be replaced eventually - * - * Acts on the same underlying XInputStream as both the full Zip File and other - * EntryInputStreams, and thus must maintain its current position in the stream and - * seek to it before performing any reads. - */ - -EntryInputStream::EntryInputStream( Reference < io::XInputStream > xNewInput, - const ZipEntry & rNewEntry, - const rtl::Reference < EncryptionData > &xEncryptData, - sal_Bool bGetRawStream) -: xStream( xNewInput ) -, xSeek( xNewInput, UNO_QUERY ) -, aEntry (rNewEntry ) -, nCurrent( 0 ) -, bHaveInMemory ( sal_False ) -, aInflater( sal_True ) -, aBuffer( 0 ) -, xEncryptionData (xEncryptData) -, bRawStream (bGetRawStream) -{ - if (bGetRawStream) - { - nUncompressedSize = aEntry.nMethod == DEFLATED ? aEntry.nCompressedSize : aEntry.nSize; - nEnd = aEntry.nOffset + nUncompressedSize; - } - else - { - nEnd = aEntry.nMethod == DEFLATED ? aEntry.nOffset + aEntry.nCompressedSize : aEntry.nOffset + aEntry.nSize; - nUncompressedSize = aEntry.nSize; - } -} -void EntryInputStream::readIntoMemory() - throw(io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException) -{ - if (!bHaveInMemory) - { - Sequence < sal_Int8 > aReadBuffer; - xSeek->seek(aEntry.nOffset); - sal_Int32 nSize = aEntry.nMethod == DEFLATED ? aEntry.nCompressedSize : aEntry.nSize; - - if (nSize <0) - throw io::BufferSizeExceededException(::rtl::OUString(), *this); - - xStream->readBytes( aReadBuffer, nSize ); // Now it holds the raw stuff from disk - - if (xEncryptionData->aSalt.getLength()) - { - // Have salt, will travel - Sequence < sal_uInt8 > aDerivedKey (16); - rtlCipherError aResult; - Sequence < sal_Int8 > aDecryptBuffer; - - // Get the key - rtl_digest_PBKDF2 ( aDerivedKey.getArray(), 16, - reinterpret_cast < const sal_uInt8 * > (xEncryptionData->aKey.getConstArray()), - xEncryptionData->aKey.getLength(), - xEncryptionData->aSalt.getConstArray(), - xEncryptionData->aSalt.getLength(), - xEncryptionData->nIterationCount ); - - rtlCipher aCipher = rtl_cipher_create (rtl_Cipher_AlgorithmBF, rtl_Cipher_ModeStream); - aResult = rtl_cipher_init( aCipher, rtl_Cipher_DirectionDecode, - aDerivedKey.getConstArray(), - aDerivedKey.getLength(), - xEncryptionData->aInitVector.getConstArray(), - xEncryptionData->aInitVector.getLength()); - OSL_ASSERT (aResult == rtl_Cipher_E_None); - aDecryptBuffer.realloc ( nSize ); - aResult = rtl_cipher_decode ( aCipher, - aReadBuffer.getConstArray(), - nSize, - reinterpret_cast < sal_uInt8 * > (aDecryptBuffer.getArray()), - nSize); - OSL_ASSERT (aResult == rtl_Cipher_E_None); - aReadBuffer = aDecryptBuffer; // Now it holds the decrypted data - } - if (bRawStream || aEntry.nMethod == STORED) - aBuffer = aReadBuffer; // bRawStream means the caller doesn't want it decompressed - else - { - aInflater.setInputSegment(aReadBuffer, 0, nSize ); - aBuffer.realloc( aEntry.nSize ); - aInflater.doInflate(aBuffer); - aInflater.end(); - } - bHaveInMemory = sal_True; - } -} -EntryInputStream::~EntryInputStream( void ) -{ -} - -sal_Int32 SAL_CALL EntryInputStream::readBytes( Sequence< sal_Int8 >& aData, - sal_Int32 nBytesToRead ) - throw(io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException) -{ - if (nBytesToRead <0) - throw io::BufferSizeExceededException(::rtl::OUString(), *this); - if (!bHaveInMemory) - readIntoMemory(); - if (nBytesToRead + nCurrent > nUncompressedSize) - nBytesToRead = static_cast < sal_Int32> ( nUncompressedSize - nCurrent ); - - aData.realloc( nBytesToRead ); - memcpy(aData.getArray(), aBuffer.getConstArray() + nCurrent, nBytesToRead); - nCurrent+=nBytesToRead; - - return nBytesToRead; -} -sal_Int32 SAL_CALL EntryInputStream::readSomeBytes( Sequence< sal_Int8 >& aData, - sal_Int32 nMaxBytesToRead ) - throw(io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException) -{ - return readBytes( aData, nMaxBytesToRead ); -} -void SAL_CALL EntryInputStream::skipBytes( sal_Int32 nBytesToSkip ) - throw(io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException) -{ - if (nBytesToSkip < 0) - throw io::BufferSizeExceededException(::rtl::OUString(), *this); - - if (nBytesToSkip + nCurrent > nUncompressedSize) - nBytesToSkip = static_cast < sal_Int32 > (nUncompressedSize- nCurrent); - - nCurrent+=nBytesToSkip; -} -sal_Int32 SAL_CALL EntryInputStream::available( ) - throw(io::NotConnectedException, io::IOException, RuntimeException) -{ - return static_cast < sal_Int32 > (nUncompressedSize - nCurrent); -} -void SAL_CALL EntryInputStream::closeInput( ) - throw(io::NotConnectedException, io::IOException, RuntimeException) -{ -} - -void SAL_CALL EntryInputStream::seek( sal_Int64 location ) - throw(lang::IllegalArgumentException, io::IOException, RuntimeException) -{ - if (location > nUncompressedSize) - location = nUncompressedSize; - if (location <0) - location = 0; - nCurrent = location; -} -sal_Int64 SAL_CALL EntryInputStream::getPosition( ) - throw(io::IOException, RuntimeException) -{ - return nCurrent; -} -sal_Int64 SAL_CALL EntryInputStream::getLength( ) - throw(io::IOException, RuntimeException) -{ - return nUncompressedSize; -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/package/source/zipapi/EntryInputStream.hxx b/package/source/zipapi/EntryInputStream.hxx deleted file mode 100644 index 8a5bf0dc835f..000000000000 --- a/package/source/zipapi/EntryInputStream.hxx +++ /dev/null @@ -1,85 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/************************************************************************* - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * Copyright 2000, 2010 Oracle and/or its affiliates. - * - * OpenOffice.org - a multi-platform office productivity suite - * - * This file is part of OpenOffice.org. - * - * OpenOffice.org is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License version 3 - * only, as published by the Free Software Foundation. - * - * OpenOffice.org is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License version 3 for more details - * (a copy is included in the LICENSE file that accompanied this code). - * - * You should have received a copy of the GNU Lesser General Public License - * version 3 along with OpenOffice.org. If not, see - * <http://www.openoffice.org/license.html> - * for a copy of the LGPLv3 License. - * - ************************************************************************/ -#ifndef _ENTRY_INPUT_STREAM_HXX -#define _ENTRY_INPUT_STREAM_HXX - -#include <cppuhelper/implbase2.hxx> // helper for implementations -#include <com/sun/star/io/XInputStream.hpp> -#include <com/sun/star/io/XSeekable.hpp> -#include <Inflater.hxx> -#include <com/sun/star/packages/zip/ZipEntry.hpp> -#include <rtl/ref.hxx> -#include <EncryptionData.hxx> -class EntryInputStream : public cppu::WeakImplHelper2< com::sun::star::io::XInputStream, - com::sun::star::io::XSeekable > -{ -protected: - com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xStream; - com::sun::star::uno::Reference< com::sun::star::io::XSeekable > xSeek; - sal_Int64 nEnd, nCurrent, nUncompressedSize; - sal_Bool bRawStream, bHaveInMemory, bEncrypted; - com::sun::star::uno::Sequence < sal_Int8 > aBuffer; - const rtl::Reference < EncryptionData > xEncryptionData; - const com::sun::star::packages::zip::ZipEntry aEntry; - Inflater aInflater; - void readIntoMemory() - throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); -public: - EntryInputStream( com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xInput, - const com::sun::star::packages::zip::ZipEntry &rNewEntry, - const rtl::Reference < EncryptionData > &xEncryptData, - sal_Bool bGetRawStream = sal_False); - virtual ~EntryInputStream(); - - // XInputStream - virtual sal_Int32 SAL_CALL readBytes( ::com::sun::star::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) - throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); - virtual sal_Int32 SAL_CALL readSomeBytes( ::com::sun::star::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) - throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); - virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) - throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); - virtual sal_Int32 SAL_CALL available( ) - throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); - virtual void SAL_CALL closeInput( ) - throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); - // XSeekable - virtual void SAL_CALL seek( sal_Int64 location ) - throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); - virtual sal_Int64 SAL_CALL getPosition( ) - throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); - virtual sal_Int64 SAL_CALL getLength( ) - throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); - /* -private: - void fill( void ); - */ -}; - -#endif - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/package/source/zipapi/Inflater.cxx b/package/source/zipapi/Inflater.cxx index a32a0007f541..40d2bee99333 100644 --- a/package/source/zipapi/Inflater.cxx +++ b/package/source/zipapi/Inflater.cxx @@ -107,7 +107,7 @@ void SAL_CALL Inflater::end( ) { if (pStream != NULL) { -#ifdef SYSTEM_ZLIB +#if defined SYSTEM_ZLIB || !defined ZLIB_PREFIX inflateEnd(pStream); #else z_inflateEnd(pStream); @@ -132,7 +132,7 @@ sal_Int32 Inflater::doInflateBytes (Sequence < sal_Int8 > &rBuffer, sal_Int32 n pStream->next_out = reinterpret_cast < unsigned char* > ( rBuffer.getArray() + nNewOffset ); pStream->avail_out = nNewLength; -#ifdef SYSTEM_ZLIB +#if defined SYSTEM_ZLIB || !defined ZLIB_PREFIX sal_Int32 nResult = ::inflate(pStream, Z_PARTIAL_FLUSH); #else sal_Int32 nResult = ::z_inflate(pStream, Z_PARTIAL_FLUSH); diff --git a/package/source/zipapi/MemoryByteGrabber.hxx b/package/source/zipapi/MemoryByteGrabber.hxx index 5e167490d632..77315d0689db 100644 --- a/package/source/zipapi/MemoryByteGrabber.hxx +++ b/package/source/zipapi/MemoryByteGrabber.hxx @@ -63,7 +63,7 @@ public: nBytesToRead = mnEnd - mnCurrent; aData.realloc ( nBytesToRead ); - memcpy ( aData.getArray(), mpBuffer + mnCurrent, nBytesToRead ); + rtl_copyMemory( aData.getArray(), mpBuffer + mnCurrent, nBytesToRead ); mnCurrent += nBytesToRead; return nBytesToRead; } diff --git a/package/source/zipapi/XFileStream.cxx b/package/source/zipapi/XFileStream.cxx deleted file mode 100644 index 6afe8079c5ec..000000000000 --- a/package/source/zipapi/XFileStream.cxx +++ /dev/null @@ -1,230 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/************************************************************************* - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * Copyright 2000, 2010 Oracle and/or its affiliates. - * - * OpenOffice.org - a multi-platform office productivity suite - * - * This file is part of OpenOffice.org. - * - * OpenOffice.org is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License version 3 - * only, as published by the Free Software Foundation. - * - * OpenOffice.org is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License version 3 for more details - * (a copy is included in the LICENSE file that accompanied this code). - * - * You should have received a copy of the GNU Lesser General Public License - * version 3 along with OpenOffice.org. If not, see - * <http://www.openoffice.org/license.html> - * for a copy of the LGPLv3 License. - * - ************************************************************************/ - -// MARKER(update_precomp.py): autogen include statement, do not remove -#include "precompiled_package.hxx" -#include <XFileStream.hxx> -#include <EncryptionData.hxx> -#include <com/sun/star/packages/zip/ZipConstants.hpp> -#include <PackageConstants.hxx> -#include <rtl/cipher.h> -#include <ZipFile.hxx> -#include <EncryptedDataHeader.hxx> -#include <com/sun/star/io/XOutputStream.hpp> - -using namespace com::sun::star::packages::zip::ZipConstants; -using namespace com::sun::star::io; -using namespace com::sun::star::uno; -using com::sun::star::lang::IllegalArgumentException; -using ::rtl::OUString; - -XFileStream::XFileStream( ZipEntry & rEntry, - com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xNewZipStream, - com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xNewTempStream, - const rtl::Reference < EncryptionData > &rData, - sal_Bool bNewRawStream, - sal_Bool bIsEncrypted ) -: maEntry ( rEntry ) -, mxData ( rData ) -, mbRawStream ( bNewRawStream ) -, mbFinished ( sal_False ) -, mxTempIn ( xNewTempStream ) -, mxTempSeek ( xNewTempStream, UNO_QUERY ) -, mxTempOut ( xNewTempStream, UNO_QUERY ) -, mxZipStream ( xNewZipStream ) -, mxZipSeek ( xNewZipStream, UNO_QUERY ) -, maInflater ( sal_True ) -, maCipher ( NULL ) -{ - mnZipCurrent = maEntry.nOffset; - if (mbRawStream) - { - mnZipSize = maEntry.nMethod == DEFLATED ? maEntry.nCompressedSize : maEntry.nSize; - mnZipEnd = maEntry.nOffset + mnZipSize; - } - else - { - mnZipSize = maEntry.nSize; - mnZipEnd = maEntry.nMethod == DEFLATED ? maEntry.nOffset + maEntry.nCompressedSize : maEntry.nOffset + maEntry.nSize; - } - - if ( bIsEncrypted ) - { - sal_Bool bHaveEncryptData = ( !rData.isEmpty() && rData->aSalt.getLength() && rData->aInitVector.getLength() && rData->nIterationCount != 0 ) ? sal_True : sal_False; - - // if we have all the encrypted data, and want a raw stream, then prepend it to the stream, otherwise - // make a cipher so we can decrypt it - if ( bHaveEncryptData ) - { - if ( !bNewRawStream ) - ZipFile::StaticGetCipher ( rData, maCipher, sal_True ); - else - { - // Put in the EncryptedDataHeader - Sequence < sal_Int8 > aEncryptedDataHeader ( n_ConstHeaderSize + - rData->aInitVector.getLength() + - rData->aSalt.getLength() + - rData->aDigest.getLength() ); - sal_Int8 * pHeader = aEncryptedDataHeader.getArray(); - ZipFile::StaticFillHeader ( rData, rEntry.nSize, pHeader ); - mxTempOut->writeBytes ( aEncryptedDataHeader ); - mnZipSize += mxTempSeek->getPosition(); - mxTempSeek->seek ( 0 ); - } - } - } -} - -XFileStream::~XFileStream() -{ - if ( maCipher ) - rtl_cipher_destroy ( maCipher ); -} - -void XFileStream::fill( sal_Int64 nUntil) -{ - sal_Int32 nRead; - sal_Int64 nPosition = mxTempSeek->getPosition(); - mxTempSeek->seek ( mxTempSeek->getLength() ); - maBuffer.realloc ( n_ConstBufferSize ); - - while ( mxTempSeek->getLength() < nUntil ) - { - if ( !mbRawStream ) - { - while ( 0 == ( nRead = maInflater.doInflate( maBuffer ) ) ) - { - if ( maInflater.finished() || maInflater.needsDictionary() ) - { - // some error handling ? - return; - } - - sal_Int64 nDiff = mnZipEnd - mnZipCurrent; - if ( nDiff > 0 ) - { - mxZipSeek->seek ( mnZipCurrent ); - nRead = mxZipStream->readBytes ( maCompBuffer, static_cast < sal_Int32 > ( nDiff < n_ConstBufferSize ? nDiff : n_ConstBufferSize ) ); - mnZipCurrent += nRead; - // maCompBuffer now has the uncompressed data, check if we need to decrypt - // before passing to the Inflater - if ( maCipher ) - { - Sequence < sal_Int8 > aCryptBuffer ( nRead ); - rtlCipherError aResult = rtl_cipher_decode ( maCipher, - maCompBuffer.getConstArray(), - nRead, - reinterpret_cast < sal_uInt8 * > (aCryptBuffer.getArray()), - nRead); - OSL_ASSERT (aResult == rtl_Cipher_E_None); - maCompBuffer = aCryptBuffer; // Now it holds the decrypted data - - } - maInflater.setInput ( maCompBuffer ); - } - else - { - // some error handling ? - return; - } - } - } - else - { - sal_Int64 nDiff = mnZipEnd - mnZipCurrent; - mxZipSeek->seek ( mnZipCurrent ); - nRead = mxZipStream->readBytes ( maBuffer, static_cast < sal_Int32 > ( nDiff < n_ConstBufferSize ? nDiff : n_ConstBufferSize ) ); - mnZipCurrent += nRead; - } - Sequence < sal_Int8 > aTmpBuffer ( maBuffer.getConstArray(), nRead ); - mxTempOut->writeBytes ( aTmpBuffer ); - } - mxTempSeek->seek ( nPosition ); -} - -sal_Int32 SAL_CALL XFileStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) - throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) -{ - sal_Int64 nPosition = mxTempSeek->getPosition(); - if ( nPosition + nBytesToRead > mnZipSize ) - nBytesToRead = static_cast < sal_Int32 > ( mnZipSize - nPosition ); - - sal_Int64 nUntil = nBytesToRead + nPosition + n_ConstBufferSize; - if (nUntil > mnZipSize ) - nUntil = mnZipSize; - if ( nUntil > mxTempSeek->getLength() ) - fill ( nUntil ); - sal_Int32 nRead = mxTempIn->readBytes ( aData, nBytesToRead ); - return nRead; -} - -sal_Int32 SAL_CALL XFileStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) - throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) -{ - return readBytes ( aData, nMaxBytesToRead ); -} -void SAL_CALL XFileStream::skipBytes( sal_Int32 nBytesToSkip ) - throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) -{ - seek ( mxTempSeek->getPosition() + nBytesToSkip ); -} - -sal_Int32 SAL_CALL XFileStream::available( ) - throw( NotConnectedException, IOException, RuntimeException) -{ - return static_cast < sal_Int32 > ( mnZipSize - mxTempSeek->getPosition() ); -} - -void SAL_CALL XFileStream::closeInput( ) - throw( NotConnectedException, IOException, RuntimeException) -{ -} -void SAL_CALL XFileStream::seek( sal_Int64 location ) - throw( IllegalArgumentException, IOException, RuntimeException) -{ - if ( location > mnZipSize || location < 0 ) - throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 1 ); - if ( location > mxTempSeek->getLength() ) - { - sal_Int64 nUntil = location + n_ConstBufferSize > mnZipSize ? mnZipSize : location + n_ConstBufferSize; - fill ( nUntil ); - } - mxTempSeek->seek ( location ); -} -sal_Int64 SAL_CALL XFileStream::getPosition( ) - throw(IOException, RuntimeException) -{ - return mxTempSeek->getPosition(); -} -sal_Int64 SAL_CALL XFileStream::getLength( ) - throw(IOException, RuntimeException) -{ - return mnZipSize; -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/package/source/zipapi/XFileStream.hxx b/package/source/zipapi/XFileStream.hxx deleted file mode 100644 index 51518de9591c..000000000000 --- a/package/source/zipapi/XFileStream.hxx +++ /dev/null @@ -1,95 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/************************************************************************* - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * Copyright 2000, 2010 Oracle and/or its affiliates. - * - * OpenOffice.org - a multi-platform office productivity suite - * - * This file is part of OpenOffice.org. - * - * OpenOffice.org is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License version 3 - * only, as published by the Free Software Foundation. - * - * OpenOffice.org is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License version 3 for more details - * (a copy is included in the LICENSE file that accompanied this code). - * - * You should have received a copy of the GNU Lesser General Public License - * version 3 along with OpenOffice.org. If not, see - * <http://www.openoffice.org/license.html> - * for a copy of the LGPLv3 License. - * - ************************************************************************/ -#ifndef _XFILE_STREAM_HXX -#define _XFILE_STREAM_HXX - -#include <com/sun/star/lang/IllegalArgumentException.hpp> -#include <com/sun/star/io/XSeekable.hpp> -#include <com/sun/star/io/XInputStream.hpp> -#include <cppuhelper/implbase2.hxx> -#include <rtl/ref.hxx> -#include <Inflater.hxx> -#include <ZipEntry.hxx> - -namespace com { namespace sun { namespace star { - namespace io { class XOutputStream; } -} } } -class EncryptionData; -typedef void* rtlCipher; -class XFileStream : public cppu::WeakImplHelper2 -< - com::sun::star::io::XInputStream, - com::sun::star::io::XSeekable -> -{ -protected: - com::sun::star::uno::Reference < com::sun::star::io::XInputStream > mxZipStream; - com::sun::star::uno::Reference < com::sun::star::io::XSeekable > mxZipSeek; - com::sun::star::uno::Reference < com::sun::star::io::XInputStream > mxTempIn; - com::sun::star::uno::Reference < com::sun::star::io::XSeekable > mxTempSeek; - com::sun::star::uno::Reference < com::sun::star::io::XOutputStream > mxTempOut; - com::sun::star::uno::Sequence < sal_Int8 > maBuffer, maCompBuffer; - ZipEntry maEntry; - rtl::Reference < EncryptionData > mxData; - rtlCipher maCipher; - Inflater maInflater; - sal_Bool mbRawStream, mbFinished; - sal_Int64 mnZipCurrent, mnZipEnd, mnZipSize; - void fill( sal_Int64 nUntil ); - -public: - XFileStream( ZipEntry & rEntry, - com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xNewZipStream, - com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xNewTempStream, - const rtl::Reference < EncryptionData > &rData, - sal_Bool bRawStream, - sal_Bool bIsEncrypted ); - virtual ~XFileStream(); - - // XInputStream - virtual sal_Int32 SAL_CALL readBytes( ::com::sun::star::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) - throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); - virtual sal_Int32 SAL_CALL readSomeBytes( ::com::sun::star::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) - throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); - virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) - throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); - virtual sal_Int32 SAL_CALL available( ) - throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); - virtual void SAL_CALL closeInput( ) - throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); - // XSeekable - virtual void SAL_CALL seek( sal_Int64 location ) - throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); - virtual sal_Int64 SAL_CALL getPosition( ) - throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); - virtual sal_Int64 SAL_CALL getLength( ) - throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); -}; -#endif - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/package/source/zipapi/XMemoryStream.cxx b/package/source/zipapi/XMemoryStream.cxx deleted file mode 100644 index 7003bf1b814b..000000000000 --- a/package/source/zipapi/XMemoryStream.cxx +++ /dev/null @@ -1,55 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/************************************************************************* - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * Copyright 2000, 2010 Oracle and/or its affiliates. - * - * OpenOffice.org - a multi-platform office productivity suite - * - * This file is part of OpenOffice.org. - * - * OpenOffice.org is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License version 3 - * only, as published by the Free Software Foundation. - * - * OpenOffice.org is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License version 3 for more details - * (a copy is included in the LICENSE file that accompanied this code). - * - * You should have received a copy of the GNU Lesser General Public License - * version 3 along with OpenOffice.org. If not, see - * <http://www.openoffice.org/license.html> - * for a copy of the LGPLv3 License. - * - ************************************************************************/ - -// MARKER(update_precomp.py): autogen include statement, do not remove -#include "precompiled_package.hxx" -#include <XMemoryStream.hxx> - -using namespace com::sun::star::io; -using namespace com::sun::star::uno; - -XMemoryStream::XMemoryStream ( com::sun::star::uno::Sequence < sal_Int8 > & rNewBuffer ) -: ZipPackageBuffer ( rNewBuffer ) -{ -} -XMemoryStream::~XMemoryStream(void) -{ -} -::com::sun::star::uno::Any SAL_CALL XMemoryStream::queryInterface( const com::sun::star::uno::Type& rType ) - throw(com::sun::star::uno::RuntimeException) -{ - return ::cppu::queryInterface ( rType , - // OWeakObject interfaces - reinterpret_cast< XInterface* > ( this ) , - static_cast< XWeak* > ( this ) , - // my interfaces - static_cast< XInputStream* > ( this ) , - static_cast< XSeekable* > ( this ) ); -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/package/source/zipapi/XUnbufferedStream.cxx b/package/source/zipapi/XUnbufferedStream.cxx index 1cad883f972a..a6049fd2cbef 100644 --- a/package/source/zipapi/XUnbufferedStream.cxx +++ b/package/source/zipapi/XUnbufferedStream.cxx @@ -28,12 +28,14 @@ // MARKER(update_precomp.py): autogen include statement, do not remove #include "precompiled_package.hxx" -#include <XUnbufferedStream.hxx> -#include <EncryptionData.hxx> + #include <com/sun/star/packages/zip/ZipConstants.hpp> #include <com/sun/star/packages/zip/ZipIOException.hpp> +#include <com/sun/star/xml/crypto/CipherID.hpp> + +#include <XUnbufferedStream.hxx> +#include <EncryptionData.hxx> #include <PackageConstants.hxx> -#include <rtl/cipher.h> #include <ZipFile.hxx> #include <EncryptedDataHeader.hxx> #include <algorithm> @@ -48,6 +50,7 @@ using namespace ::com::sun::star; #endif +using namespace ::com::sun::star; using namespace com::sun::star::packages::zip::ZipConstants; using namespace com::sun::star::io; using namespace com::sun::star::uno; @@ -55,20 +58,22 @@ using com::sun::star::lang::IllegalArgumentException; using com::sun::star::packages::zip::ZipIOException; using ::rtl::OUString; -XUnbufferedStream::XUnbufferedStream( SotMutexHolderRef aMutexHolder, - ZipEntry & rEntry, - Reference < XInputStream > xNewZipStream, - const rtl::Reference < EncryptionData > &rData, - sal_Int8 nStreamMode, - sal_Bool bIsEncrypted, - const ::rtl::OUString& aMediaType, - sal_Bool bRecoveryMode ) +XUnbufferedStream::XUnbufferedStream( + const uno::Reference< lang::XMultiServiceFactory >& xFactory, + SotMutexHolderRef aMutexHolder, + ZipEntry & rEntry, + Reference < XInputStream > xNewZipStream, + const ::rtl::Reference< EncryptionData >& rData, + sal_Int8 nStreamMode, + sal_Bool bIsEncrypted, + const ::rtl::OUString& aMediaType, + sal_Bool bRecoveryMode ) : maMutexHolder( aMutexHolder.Is() ? aMutexHolder : SotMutexHolderRef( new SotMutexHolder ) ) , mxZipStream ( xNewZipStream ) , mxZipSeek ( xNewZipStream, UNO_QUERY ) , maEntry ( rEntry ) , mxData ( rData ) -, maCipher ( NULL ) +, mnBlockSize( 1 ) , maInflater ( sal_True ) , mbRawStream ( nStreamMode == UNBUFF_STREAM_RAW || nStreamMode == UNBUFF_STREAM_WRAPPEDRAW ) , mbWrappedRaw ( nStreamMode == UNBUFF_STREAM_WRAPPEDRAW ) @@ -91,11 +96,15 @@ XUnbufferedStream::XUnbufferedStream( SotMutexHolderRef aMutexHolder, mnZipSize = maEntry.nSize; mnZipEnd = maEntry.nMethod == DEFLATED ? maEntry.nOffset + maEntry.nCompressedSize : maEntry.nOffset + maEntry.nSize; } - sal_Bool bHaveEncryptData = ( rData.is() && rData->aSalt.getLength() && rData->aInitVector.getLength() && rData->nIterationCount != 0 ) ? sal_True : sal_False; + sal_Bool bHaveEncryptData = ( rData.is() && rData->m_aSalt.getLength() && rData->m_aInitVector.getLength() && rData->m_nIterationCount != 0 ) ? sal_True : sal_False; sal_Bool bMustDecrypt = ( nStreamMode == UNBUFF_STREAM_DATA && bHaveEncryptData && bIsEncrypted ) ? sal_True : sal_False; if ( bMustDecrypt ) - ZipFile::StaticGetCipher ( rData, maCipher, sal_True ); + { + m_xCipherContext = ZipFile::StaticGetCipher( xFactory, rData, false ); + mnBlockSize = ( rData->m_nEncAlg == xml::crypto::CipherID::AES_CBC_W3C_PADDING ? 16 : 1 ); + } + if ( bHaveEncryptData && mbWrappedRaw && bIsEncrypted ) { // if we have the data needed to decrypt it, but didn't want it decrypted (or @@ -104,24 +113,26 @@ XUnbufferedStream::XUnbufferedStream( SotMutexHolderRef aMutexHolder, // Make a buffer big enough to hold both the header and the data itself maHeader.realloc ( n_ConstHeaderSize + - rData->aInitVector.getLength() + - rData->aSalt.getLength() + - rData->aDigest.getLength() + + rData->m_aInitVector.getLength() + + rData->m_aSalt.getLength() + + rData->m_aDigest.getLength() + aMediaType.getLength() * sizeof( sal_Unicode ) ); sal_Int8 * pHeader = maHeader.getArray(); - ZipFile::StaticFillHeader ( rData, rEntry.nSize, aMediaType, pHeader ); + ZipFile::StaticFillHeader( rData, rEntry.nSize, aMediaType, pHeader ); mnHeaderToRead = static_cast < sal_Int16 > ( maHeader.getLength() ); } } // allows to read package raw stream -XUnbufferedStream::XUnbufferedStream( const Reference < XInputStream >& xRawStream, - const rtl::Reference < EncryptionData > &rData ) +XUnbufferedStream::XUnbufferedStream( + const uno::Reference< lang::XMultiServiceFactory >& /*xFactory*/, + const Reference < XInputStream >& xRawStream, + const ::rtl::Reference< EncryptionData >& rData ) : maMutexHolder( new SotMutexHolder ) , mxZipStream ( xRawStream ) , mxZipSeek ( xRawStream, UNO_QUERY ) , mxData ( rData ) -, maCipher ( NULL ) +, mnBlockSize( 1 ) , maInflater ( sal_True ) , mbRawStream ( sal_False ) , mbWrappedRaw ( sal_False ) @@ -137,8 +148,8 @@ XUnbufferedStream::XUnbufferedStream( const Reference < XInputStream >& xRawStre OSL_ENSURE( mxZipSeek.is(), "The stream must be seekable!\n" ); // skip raw header, it must be already parsed to rData - mnZipCurrent = n_ConstHeaderSize + rData->aInitVector.getLength() + - rData->aSalt.getLength() + rData->aDigest.getLength(); + mnZipCurrent = n_ConstHeaderSize + rData->m_aInitVector.getLength() + + rData->m_aSalt.getLength() + rData->m_aDigest.getLength(); try { if ( mxZipSeek.is() ) @@ -150,13 +161,12 @@ XUnbufferedStream::XUnbufferedStream( const Reference < XInputStream >& xRawStre mnZipEnd = mnZipCurrent + mnZipSize; - ZipFile::StaticGetCipher ( rData, maCipher, sal_True ); + // the raw data will not be decrypted, no need for the cipher + // m_xCipherContext = ZipFile::StaticGetCipher( xFactory, rData, false ); } XUnbufferedStream::~XUnbufferedStream() { - if ( maCipher ) - rtl_cipher_destroy ( maCipher ); } sal_Int32 SAL_CALL XUnbufferedStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) @@ -183,7 +193,7 @@ sal_Int32 SAL_CALL XUnbufferedStream::readBytes( Sequence< sal_Int8 >& aData, sa { sal_Int16 nHeadRead = static_cast< sal_Int16 >(( nRequestedBytes > mnHeaderToRead ? mnHeaderToRead : nRequestedBytes )); - memcpy ( aData.getArray(), maHeader.getConstArray() + maHeader.getLength() - mnHeaderToRead, nHeadRead ); + rtl_copyMemory ( aData.getArray(), maHeader.getConstArray() + maHeader.getLength() - mnHeaderToRead, nHeadRead ); mnHeaderToRead = mnHeaderToRead - nHeadRead; if ( nHeadRead < nRequestedBytes ) @@ -245,12 +255,17 @@ sal_Int32 SAL_CALL XUnbufferedStream::readBytes( Sequence< sal_Int8 >& aData, sa throw ZipIOException( OUString( RTL_CONSTASCII_USTRINGPARAM( "Dictionaries are not supported!" ) ), Reference< XInterface >() ); - sal_Int32 nDiff = static_cast < sal_Int32 > ( mnZipEnd - mnZipCurrent ); + sal_Int32 nDiff = static_cast< sal_Int32 >( mnZipEnd - mnZipCurrent ); if ( nDiff > 0 ) { mxZipSeek->seek ( mnZipCurrent ); - sal_Int32 nToRead = std::min ( nDiff, std::max ( nRequestedBytes, static_cast< sal_Int32 >( 8192 ) ) ); - sal_Int32 nZipRead = mxZipStream->readBytes ( maCompBuffer, nToRead ); + + sal_Int32 nToRead = std::max( nRequestedBytes, static_cast< sal_Int32 >( 8192 ) ); + if ( mnBlockSize > 1 ) + nToRead = nToRead + mnBlockSize - nToRead % mnBlockSize; + nToRead = std::min( nDiff, nToRead ); + + sal_Int32 nZipRead = mxZipStream->readBytes( maCompBuffer, nToRead ); if ( nZipRead < nToRead ) throw ZipIOException( OUString( RTL_CONSTASCII_USTRINGPARAM( "No expected data!" ) ), Reference< XInterface >() ); @@ -258,23 +273,22 @@ sal_Int32 SAL_CALL XUnbufferedStream::readBytes( Sequence< sal_Int8 >& aData, sa mnZipCurrent += nZipRead; // maCompBuffer now has the data, check if we need to decrypt // before passing to the Inflater - if ( maCipher ) + if ( m_xCipherContext.is() ) { if ( mbCheckCRC ) maCRC.update( maCompBuffer ); - Sequence < sal_Int8 > aCryptBuffer ( nZipRead ); - rtlCipherError aResult = - rtl_cipher_decode ( maCipher, - maCompBuffer.getConstArray(), - nZipRead, - reinterpret_cast < sal_uInt8 * > (aCryptBuffer.getArray()), - nZipRead); - if( aResult != rtl_Cipher_E_None ) { - OSL_ASSERT (aResult == rtl_Cipher_E_None); + maCompBuffer = m_xCipherContext->convertWithCipherContext( maCompBuffer ); + if ( mnZipCurrent == mnZipEnd ) + { + uno::Sequence< sal_Int8 > aSuffix = m_xCipherContext->finalizeCipherContextAndDispose(); + if ( aSuffix.getLength() ) + { + sal_Int32 nOldLen = maCompBuffer.getLength(); + maCompBuffer.realloc( nOldLen + aSuffix.getLength() ); + rtl_copyMemory( maCompBuffer.getArray() + nOldLen, aSuffix.getConstArray(), aSuffix.getLength() ); + } } - maCompBuffer = aCryptBuffer; // Now it holds the decrypted data - } maInflater.setInput ( maCompBuffer ); } @@ -293,7 +307,7 @@ sal_Int32 SAL_CALL XUnbufferedStream::readBytes( Sequence< sal_Int8 >& aData, sa if ( mbCheckCRC && ( !mbRawStream || mbWrappedRaw ) ) { - if ( !maCipher && !mbWrappedRaw ) + if ( !m_xCipherContext.is() && !mbWrappedRaw ) maCRC.update( aData ); #if 0 diff --git a/package/source/zipapi/XUnbufferedStream.hxx b/package/source/zipapi/XUnbufferedStream.hxx index 320031f515dd..43a659485c24 100644 --- a/package/source/zipapi/XUnbufferedStream.hxx +++ b/package/source/zipapi/XUnbufferedStream.hxx @@ -32,6 +32,8 @@ #include <com/sun/star/io/XSeekable.hpp> #include <com/sun/star/io/XInputStream.hpp> #include <com/sun/star/io/XOutputStream.hpp> +#include <com/sun/star/xml/crypto/XCipherContext.hpp> + #include <cppuhelper/implbase1.hxx> #include <rtl/ref.hxx> #include <Inflater.hxx> @@ -44,7 +46,6 @@ #define UNBUFF_STREAM_WRAPPEDRAW 2 class EncryptionData; -typedef void* rtlCipher; class XUnbufferedStream : public cppu::WeakImplHelper1 < com::sun::star::io::XInputStream @@ -57,8 +58,9 @@ protected: com::sun::star::uno::Reference < com::sun::star::io::XSeekable > mxZipSeek; com::sun::star::uno::Sequence < sal_Int8 > maCompBuffer, maHeader; ZipEntry maEntry; - rtl::Reference < EncryptionData > mxData; - rtlCipher maCipher; + ::rtl::Reference< EncryptionData > mxData; + sal_Int32 mnBlockSize; + ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XCipherContext > m_xCipherContext; ZipUtils::Inflater maInflater; sal_Bool mbRawStream, mbWrappedRaw, mbFinished; sal_Int16 mnHeaderToRead; @@ -68,18 +70,21 @@ protected: public: XUnbufferedStream( + const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory, SotMutexHolderRef aMutexHolder, ZipEntry & rEntry, com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xNewZipStream, - const rtl::Reference < EncryptionData > &rData, + const ::rtl::Reference< EncryptionData >& rData, sal_Int8 nStreamMode, sal_Bool bIsEncrypted, const ::rtl::OUString& aMediaType, sal_Bool bRecoveryMode ); // allows to read package raw stream - XUnbufferedStream( const com::sun::star::uno::Reference < com::sun::star::io::XInputStream >& xRawStream, - const rtl::Reference < EncryptionData > &rData ); + XUnbufferedStream( + const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory, + const com::sun::star::uno::Reference < com::sun::star::io::XInputStream >& xRawStream, + const ::rtl::Reference< EncryptionData >& rData ); virtual ~XUnbufferedStream(); diff --git a/package/source/zipapi/ZipFile.cxx b/package/source/zipapi/ZipFile.cxx index 4c6115f21768..5151d26781b6 100644 --- a/package/source/zipapi/ZipFile.cxx +++ b/package/source/zipapi/ZipFile.cxx @@ -28,29 +28,36 @@ // MARKER(update_precomp.py): autogen include statement, do not remove #include "precompiled_package.hxx" -#include <ZipFile.hxx> -#include <ZipEnumeration.hxx> + +#include <com/sun/star/lang/XMultiServiceFactory.hpp> +#include <com/sun/star/ucb/XProgressHandler.hpp> #include <com/sun/star/packages/zip/ZipConstants.hpp> -#include <rtl/cipher.h> +#include <com/sun/star/xml/crypto/XCipherContext.hpp> +#include <com/sun/star/xml/crypto/XDigestContext.hpp> +#include <com/sun/star/xml/crypto/XCipherContextSupplier.hpp> +#include <com/sun/star/xml/crypto/XDigestContextSupplier.hpp> +#include <com/sun/star/xml/crypto/CipherID.hpp> +#include <com/sun/star/xml/crypto/DigestID.hpp> + +#include <comphelper/storagehelper.hxx> +#include <comphelper/processfactory.hxx> #include <rtl/digest.h> -/* -#include <XMemoryStream.hxx> -#include <XFileStream.hxx> -*/ + +#include <vector> + +#include "blowfishcontext.hxx" +#include "sha1context.hxx" +#include <ZipFile.hxx> +#include <ZipEnumeration.hxx> #include <XUnbufferedStream.hxx> #include <PackageConstants.hxx> #include <EncryptedDataHeader.hxx> #include <EncryptionData.hxx> #include <MemoryByteGrabber.hxx> -#include <com/sun/star/lang/XMultiServiceFactory.hpp> -#include <com/sun/star/ucb/XProgressHandler.hpp> #include <CRC32.hxx> -#include <string.h> // for memcpy -#include <vector> - -#include <comphelper/storagehelper.hxx> +#define AES_CBC_BLOCK_SIZE 16 using namespace com::sun::star; using namespace com::sun::star::io; @@ -66,13 +73,13 @@ using ZipUtils::Inflater; /** This class is used to read entries from a zip file */ -ZipFile::ZipFile( Reference < XInputStream > &xInput, const Reference < XMultiServiceFactory > &xNewFactory, sal_Bool bInitialise ) +ZipFile::ZipFile( uno::Reference < XInputStream > &xInput, const uno::Reference < XMultiServiceFactory > &xNewFactory, sal_Bool bInitialise ) throw(IOException, ZipException, RuntimeException) : aGrabber(xInput) , aInflater (sal_True) , xStream(xInput) , xSeek(xInput, UNO_QUERY) -, xFactory ( xNewFactory ) +, m_xFactory ( xNewFactory ) , bRecoveryMode( sal_False ) { if (bInitialise) @@ -80,20 +87,20 @@ ZipFile::ZipFile( Reference < XInputStream > &xInput, const Reference < XMultiSe if ( readCEN() == -1 ) { aEntries.clear(); - throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "stream data looks to be broken" ) ), Reference < XInterface > () ); + throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "stream data looks to be broken" ) ), uno::Reference < XInterface > () ); } } } -ZipFile::ZipFile( Reference < XInputStream > &xInput, const Reference < XMultiServiceFactory > &xNewFactory, sal_Bool bInitialise, sal_Bool bForceRecovery, Reference < XProgressHandler > xProgress ) +ZipFile::ZipFile( uno::Reference < XInputStream > &xInput, const uno::Reference < XMultiServiceFactory > &xNewFactory, sal_Bool bInitialise, sal_Bool bForceRecovery, uno::Reference < XProgressHandler > xProgress ) throw(IOException, ZipException, RuntimeException) : aGrabber(xInput) , aInflater (sal_True) , xStream(xInput) , xSeek(xInput, UNO_QUERY) -, xFactory ( xNewFactory ) +, m_xFactory ( xNewFactory ) , xProgressHandler( xProgress ) , bRecoveryMode( bForceRecovery ) { @@ -106,7 +113,7 @@ ZipFile::ZipFile( Reference < XInputStream > &xInput, const Reference < XMultiSe else if ( readCEN() == -1 ) { aEntries.clear(); - throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "stream data looks to be broken" ) ), Reference < XInterface > () ); + throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "stream data looks to be broken" ) ), uno::Reference < XInterface > () ); } } } @@ -116,55 +123,94 @@ ZipFile::~ZipFile() aEntries.clear(); } -void ZipFile::setInputStream ( Reference < XInputStream > xNewStream ) +void ZipFile::setInputStream ( uno::Reference < XInputStream > xNewStream ) { ::osl::MutexGuard aGuard( m_aMutex ); xStream = xNewStream; - xSeek = Reference < XSeekable > ( xStream, UNO_QUERY ); + xSeek = uno::Reference < XSeekable > ( xStream, UNO_QUERY ); aGrabber.setInputStream ( xStream ); } -sal_Bool ZipFile::StaticGetCipher ( const rtl::Reference < EncryptionData > & xEncryptionData, rtlCipher &rCipher, sal_Bool bDecode ) +uno::Reference< xml::crypto::XDigestContext > ZipFile::StaticGetDigestContextForChecksum( const uno::Reference< lang::XMultiServiceFactory >& xArgFactory, const ::rtl::Reference< EncryptionData >& xEncryptionData ) { - sal_Bool bResult = sal_False; - if ( xEncryptionData.is() ) + uno::Reference< xml::crypto::XDigestContext > xDigestContext; + if ( xEncryptionData->m_nCheckAlg == xml::crypto::DigestID::SHA256_1K ) { - Sequence < sal_uInt8 > aDerivedKey (16); - rtlCipherError aResult; - Sequence < sal_Int8 > aDecryptBuffer; - - // Get the key - rtl_digest_PBKDF2 ( aDerivedKey.getArray(), 16, - reinterpret_cast < const sal_uInt8 * > (xEncryptionData->aKey.getConstArray() ), - xEncryptionData->aKey.getLength(), - reinterpret_cast < const sal_uInt8 * > ( xEncryptionData->aSalt.getConstArray() ), - xEncryptionData->aSalt.getLength(), - xEncryptionData->nIterationCount ); - - rCipher = rtl_cipher_create (rtl_Cipher_AlgorithmBF, rtl_Cipher_ModeStream); - aResult = rtl_cipher_init( rCipher, bDecode ? rtl_Cipher_DirectionDecode : rtl_Cipher_DirectionEncode, - aDerivedKey.getConstArray(), - aDerivedKey.getLength(), - reinterpret_cast < const sal_uInt8 * > ( xEncryptionData->aInitVector.getConstArray() ), - xEncryptionData->aInitVector.getLength()); - OSL_ASSERT (aResult == rtl_Cipher_E_None); - - bResult = ( aResult == rtl_Cipher_E_None ); + uno::Reference< lang::XMultiServiceFactory > xFactory = xArgFactory; + if ( !xFactory.is() ) + xFactory.set( comphelper::getProcessServiceFactory(), uno::UNO_SET_THROW ); + + uno::Reference< xml::crypto::XDigestContextSupplier > xDigestContextSupplier( + xFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.crypto.NSSInitializer" ) ) ), + uno::UNO_QUERY_THROW ); + + xDigestContext.set( xDigestContextSupplier->getDigestContext( xEncryptionData->m_nCheckAlg, uno::Sequence< beans::NamedValue >() ), uno::UNO_SET_THROW ); } + else if ( xEncryptionData->m_nCheckAlg == xml::crypto::DigestID::SHA1_1K ) + xDigestContext.set( SHA1DigestContext::Create(), uno::UNO_SET_THROW ); - return bResult; + return xDigestContext; } -void ZipFile::StaticFillHeader ( const rtl::Reference < EncryptionData > & rData, +uno::Reference< xml::crypto::XCipherContext > ZipFile::StaticGetCipher( const uno::Reference< lang::XMultiServiceFactory >& xArgFactory, const ::rtl::Reference< EncryptionData >& xEncryptionData, bool bEncrypt ) +{ + uno::Reference< xml::crypto::XCipherContext > xResult; + + try + { + uno::Sequence< sal_Int8 > aDerivedKey( xEncryptionData->m_nDerivedKeySize ); + if ( rtl_Digest_E_None != rtl_digest_PBKDF2( reinterpret_cast< sal_uInt8* >( aDerivedKey.getArray() ), + aDerivedKey.getLength(), + reinterpret_cast< const sal_uInt8 * > (xEncryptionData->m_aKey.getConstArray() ), + xEncryptionData->m_aKey.getLength(), + reinterpret_cast< const sal_uInt8 * > ( xEncryptionData->m_aSalt.getConstArray() ), + xEncryptionData->m_aSalt.getLength(), + xEncryptionData->m_nIterationCount ) ) + { + throw ZipIOException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Can not create derived key!") ), + uno::Reference< XInterface >() ); + } + + if ( xEncryptionData->m_nEncAlg == xml::crypto::CipherID::AES_CBC_W3C_PADDING ) + { + uno::Reference< lang::XMultiServiceFactory > xFactory = xArgFactory; + if ( !xFactory.is() ) + xFactory.set( comphelper::getProcessServiceFactory(), uno::UNO_SET_THROW ); + + uno::Reference< xml::crypto::XCipherContextSupplier > xCipherContextSupplier( + xFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.crypto.NSSInitializer" ) ) ), + uno::UNO_QUERY_THROW ); + + xResult = xCipherContextSupplier->getCipherContext( xEncryptionData->m_nEncAlg, aDerivedKey, xEncryptionData->m_aInitVector, bEncrypt, uno::Sequence< beans::NamedValue >() ); + } + else if ( xEncryptionData->m_nEncAlg == xml::crypto::CipherID::BLOWFISH_CFB_8 ) + { + xResult = BlowfishCFB8CipherContext::Create( aDerivedKey, xEncryptionData->m_aInitVector, bEncrypt ); + } + else + { + throw ZipIOException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Unknown cipher algorithm is requested!") ), + uno::Reference< XInterface >() ); + } + } + catch( uno::Exception& ) + { + OSL_ENSURE( sal_False, "Can not create cipher context!" ); + } + + return xResult; +} + +void ZipFile::StaticFillHeader( const ::rtl::Reference< EncryptionData >& rData, sal_Int32 nSize, const ::rtl::OUString& aMediaType, sal_Int8 * & pHeader ) { // I think it's safe to restrict vector and salt length to 2 bytes ! - sal_Int16 nIVLength = static_cast < sal_Int16 > ( rData->aInitVector.getLength() ); - sal_Int16 nSaltLength = static_cast < sal_Int16 > ( rData->aSalt.getLength() ); - sal_Int16 nDigestLength = static_cast < sal_Int16 > ( rData->aDigest.getLength() ); + sal_Int16 nIVLength = static_cast < sal_Int16 > ( rData->m_aInitVector.getLength() ); + sal_Int16 nSaltLength = static_cast < sal_Int16 > ( rData->m_aSalt.getLength() ); + sal_Int16 nDigestLength = static_cast < sal_Int16 > ( rData->m_aDigest.getLength() ); sal_Int16 nMediaTypeLength = static_cast < sal_Int16 > ( aMediaType.getLength() * sizeof( sal_Unicode ) ); // First the header @@ -178,7 +224,7 @@ void ZipFile::StaticFillHeader ( const rtl::Reference < EncryptionData > & rData *(pHeader++) = ( n_ConstCurrentVersion >> 8 ) & 0xFF; // Then the iteration Count - sal_Int32 nIterationCount = rData->nIterationCount; + sal_Int32 nIterationCount = rData->m_nIterationCount; *(pHeader++) = static_cast< sal_Int8 >(( nIterationCount >> 0 ) & 0xFF); *(pHeader++) = static_cast< sal_Int8 >(( nIterationCount >> 8 ) & 0xFF); *(pHeader++) = static_cast< sal_Int8 >(( nIterationCount >> 16 ) & 0xFF); @@ -190,6 +236,34 @@ void ZipFile::StaticFillHeader ( const rtl::Reference < EncryptionData > & rData *(pHeader++) = static_cast< sal_Int8 >(( nSize >> 16 ) & 0xFF); *(pHeader++) = static_cast< sal_Int8 >(( nSize >> 24 ) & 0xFF); + // Then the encryption algorithm + sal_Int32 nEncAlgID = rData->m_nEncAlg; + *(pHeader++) = static_cast< sal_Int8 >(( nEncAlgID >> 0 ) & 0xFF); + *(pHeader++) = static_cast< sal_Int8 >(( nEncAlgID >> 8 ) & 0xFF); + *(pHeader++) = static_cast< sal_Int8 >(( nEncAlgID >> 16 ) & 0xFF); + *(pHeader++) = static_cast< sal_Int8 >(( nEncAlgID >> 24 ) & 0xFF); + + // Then the checksum algorithm + sal_Int32 nChecksumAlgID = rData->m_nCheckAlg; + *(pHeader++) = static_cast< sal_Int8 >(( nChecksumAlgID >> 0 ) & 0xFF); + *(pHeader++) = static_cast< sal_Int8 >(( nChecksumAlgID >> 8 ) & 0xFF); + *(pHeader++) = static_cast< sal_Int8 >(( nChecksumAlgID >> 16 ) & 0xFF); + *(pHeader++) = static_cast< sal_Int8 >(( nChecksumAlgID >> 24 ) & 0xFF); + + // Then the derived key size + sal_Int32 nDerivedKeySize = rData->m_nDerivedKeySize; + *(pHeader++) = static_cast< sal_Int8 >(( nDerivedKeySize >> 0 ) & 0xFF); + *(pHeader++) = static_cast< sal_Int8 >(( nDerivedKeySize >> 8 ) & 0xFF); + *(pHeader++) = static_cast< sal_Int8 >(( nDerivedKeySize >> 16 ) & 0xFF); + *(pHeader++) = static_cast< sal_Int8 >(( nDerivedKeySize >> 24 ) & 0xFF); + + // Then the start key generation algorithm + sal_Int32 nKeyAlgID = rData->m_nStartKeyGenID; + *(pHeader++) = static_cast< sal_Int8 >(( nKeyAlgID >> 0 ) & 0xFF); + *(pHeader++) = static_cast< sal_Int8 >(( nKeyAlgID >> 8 ) & 0xFF); + *(pHeader++) = static_cast< sal_Int8 >(( nKeyAlgID >> 16 ) & 0xFF); + *(pHeader++) = static_cast< sal_Int8 >(( nKeyAlgID >> 24 ) & 0xFF); + // Then the salt length *(pHeader++) = static_cast< sal_Int8 >(( nSaltLength >> 0 ) & 0xFF); *(pHeader++) = static_cast< sal_Int8 >(( nSaltLength >> 8 ) & 0xFF); @@ -207,26 +281,30 @@ void ZipFile::StaticFillHeader ( const rtl::Reference < EncryptionData > & rData *(pHeader++) = static_cast< sal_Int8 >(( nMediaTypeLength >> 8 ) & 0xFF); // Then the salt content - memcpy ( pHeader, rData->aSalt.getConstArray(), nSaltLength ); + rtl_copyMemory ( pHeader, rData->m_aSalt.getConstArray(), nSaltLength ); pHeader += nSaltLength; // Then the IV content - memcpy ( pHeader, rData->aInitVector.getConstArray(), nIVLength ); + rtl_copyMemory ( pHeader, rData->m_aInitVector.getConstArray(), nIVLength ); pHeader += nIVLength; // Then the digest content - memcpy ( pHeader, rData->aDigest.getConstArray(), nDigestLength ); + rtl_copyMemory ( pHeader, rData->m_aDigest.getConstArray(), nDigestLength ); pHeader += nDigestLength; // Then the mediatype itself - memcpy ( pHeader, aMediaType.getStr(), nMediaTypeLength ); + rtl_copyMemory ( pHeader, aMediaType.getStr(), nMediaTypeLength ); pHeader += nMediaTypeLength; } -sal_Bool ZipFile::StaticFillData ( rtl::Reference < EncryptionData > & rData, +sal_Bool ZipFile::StaticFillData ( ::rtl::Reference< BaseEncryptionData > & rData, + sal_Int32 &rEncAlg, + sal_Int32 &rChecksumAlg, + sal_Int32 &rDerivedKeySize, + sal_Int32 &rStartKeyGenID, sal_Int32 &rSize, ::rtl::OUString& aMediaType, - Reference < XInputStream > &rStream ) + const uno::Reference< XInputStream >& rStream ) { sal_Bool bOk = sal_False; const sal_Int32 nHeaderSize = n_ConstHeaderSize - 4; @@ -243,13 +321,33 @@ sal_Bool ZipFile::StaticFillData ( rtl::Reference < EncryptionData > & rData, nCount |= ( pBuffer[nPos++] & 0xFF ) << 8; nCount |= ( pBuffer[nPos++] & 0xFF ) << 16; nCount |= ( pBuffer[nPos++] & 0xFF ) << 24; - rData->nIterationCount = nCount; + rData->m_nIterationCount = nCount; rSize = pBuffer[nPos++] & 0xFF; rSize |= ( pBuffer[nPos++] & 0xFF ) << 8; rSize |= ( pBuffer[nPos++] & 0xFF ) << 16; rSize |= ( pBuffer[nPos++] & 0xFF ) << 24; + rEncAlg = pBuffer[nPos++] & 0xFF; + rEncAlg |= ( pBuffer[nPos++] & 0xFF ) << 8; + rEncAlg |= ( pBuffer[nPos++] & 0xFF ) << 16; + rEncAlg |= ( pBuffer[nPos++] & 0xFF ) << 24; + + rChecksumAlg = pBuffer[nPos++] & 0xFF; + rChecksumAlg |= ( pBuffer[nPos++] & 0xFF ) << 8; + rChecksumAlg |= ( pBuffer[nPos++] & 0xFF ) << 16; + rChecksumAlg |= ( pBuffer[nPos++] & 0xFF ) << 24; + + rDerivedKeySize = pBuffer[nPos++] & 0xFF; + rDerivedKeySize |= ( pBuffer[nPos++] & 0xFF ) << 8; + rDerivedKeySize |= ( pBuffer[nPos++] & 0xFF ) << 16; + rDerivedKeySize |= ( pBuffer[nPos++] & 0xFF ) << 24; + + rStartKeyGenID = pBuffer[nPos++] & 0xFF; + rStartKeyGenID |= ( pBuffer[nPos++] & 0xFF ) << 8; + rStartKeyGenID |= ( pBuffer[nPos++] & 0xFF ) << 16; + rStartKeyGenID |= ( pBuffer[nPos++] & 0xFF ) << 24; + sal_Int16 nSaltLength = pBuffer[nPos++] & 0xFF; nSaltLength |= ( pBuffer[nPos++] & 0xFF ) << 8; sal_Int16 nIVLength = ( pBuffer[nPos++] & 0xFF ); @@ -262,16 +360,16 @@ sal_Bool ZipFile::StaticFillData ( rtl::Reference < EncryptionData > & rData, if ( nSaltLength == rStream->readBytes ( aBuffer, nSaltLength ) ) { - rData->aSalt.realloc ( nSaltLength ); - memcpy ( rData->aSalt.getArray(), aBuffer.getConstArray(), nSaltLength ); + rData->m_aSalt.realloc ( nSaltLength ); + rtl_copyMemory ( rData->m_aSalt.getArray(), aBuffer.getConstArray(), nSaltLength ); if ( nIVLength == rStream->readBytes ( aBuffer, nIVLength ) ) { - rData->aInitVector.realloc ( nIVLength ); - memcpy ( rData->aInitVector.getArray(), aBuffer.getConstArray(), nIVLength ); + rData->m_aInitVector.realloc ( nIVLength ); + rtl_copyMemory ( rData->m_aInitVector.getArray(), aBuffer.getConstArray(), nIVLength ); if ( nDigestLength == rStream->readBytes ( aBuffer, nDigestLength ) ) { - rData->aDigest.realloc ( nDigestLength ); - memcpy ( rData->aDigest.getArray(), aBuffer.getConstArray(), nDigestLength ); + rData->m_aDigest.realloc ( nDigestLength ); + rtl_copyMemory ( rData->m_aDigest.getArray(), aBuffer.getConstArray(), nDigestLength ); if ( nMediaTypeLength == rStream->readBytes ( aBuffer, nMediaTypeLength ) ) { @@ -287,85 +385,107 @@ sal_Bool ZipFile::StaticFillData ( rtl::Reference < EncryptionData > & rData, return bOk; } -Reference< XInputStream > ZipFile::StaticGetDataFromRawStream( const Reference< XInputStream >& xStream, - const rtl::Reference < EncryptionData > &rData ) +uno::Reference< XInputStream > ZipFile::StaticGetDataFromRawStream( const uno::Reference< lang::XMultiServiceFactory >& xFactory, + const uno::Reference< XInputStream >& xStream, + const ::rtl::Reference< EncryptionData > &rData ) throw ( packages::WrongPasswordException, ZipIOException, RuntimeException ) { if ( !rData.is() ) throw ZipIOException( OUString(RTL_CONSTASCII_USTRINGPARAM( "Encrypted stream without encryption data!\n" )), - Reference< XInterface >() ); + uno::Reference< XInterface >() ); - if ( !rData->aKey.getLength() ) + if ( !rData->m_aKey.getLength() ) throw packages::WrongPasswordException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); - Reference< XSeekable > xSeek( xStream, UNO_QUERY ); + uno::Reference< XSeekable > xSeek( xStream, UNO_QUERY ); if ( !xSeek.is() ) throw ZipIOException( OUString(RTL_CONSTASCII_USTRINGPARAM( "The stream must be seekable!\n" )), - Reference< XInterface >() ); + uno::Reference< XInterface >() ); // if we have a digest, then this file is an encrypted one and we should // check if we can decrypt it or not - OSL_ENSURE( rData->aDigest.getLength(), "Can't detect password correctness without digest!\n" ); - if ( rData->aDigest.getLength() ) + OSL_ENSURE( rData->m_aDigest.getLength(), "Can't detect password correctness without digest!\n" ); + if ( rData->m_aDigest.getLength() ) { - sal_Int32 nSize = sal::static_int_cast< sal_Int32 >( xSeek->getLength() ); - nSize = nSize > n_ConstDigestLength ? n_ConstDigestLength : nSize; + sal_Int32 nSize = sal::static_int_cast< sal_Int32 >( xSeek->getLength() ); + if ( nSize > n_ConstDigestLength + 32 ) + nSize = n_ConstDigestLength + 32; // skip header - xSeek->seek( n_ConstHeaderSize + rData->aInitVector.getLength() + - rData->aSalt.getLength() + rData->aDigest.getLength() ); + xSeek->seek( n_ConstHeaderSize + rData->m_aInitVector.getLength() + + rData->m_aSalt.getLength() + rData->m_aDigest.getLength() ); // Only want to read enough to verify the digest Sequence < sal_Int8 > aReadBuffer ( nSize ); xStream->readBytes( aReadBuffer, nSize ); - if ( !StaticHasValidPassword( aReadBuffer, rData ) ) + if ( !StaticHasValidPassword( xFactory, aReadBuffer, rData ) ) throw packages::WrongPasswordException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); } - return new XUnbufferedStream ( xStream, rData ); + return new XUnbufferedStream( xFactory, xStream, rData ); +} + +#if 0 +// for debugging purposes +void CheckSequence( const uno::Sequence< sal_Int8 >& aSequence ) +{ + if ( aSequence.getLength() ) + { + sal_Int32* pPointer = *( (sal_Int32**)&aSequence ); + sal_Int32 nSize = *( pPointer + 1 ); + sal_Int32 nMemSize = *( pPointer - 2 ); + sal_Int32 nUsedMemSize = ( nSize + 4 * sizeof( sal_Int32 ) ); + OSL_ENSURE( nSize == aSequence.getLength() && nUsedMemSize + 7 - ( nUsedMemSize - 1 ) % 8 == nMemSize, "Broken Sequence!" ); + } } +#endif -sal_Bool ZipFile::StaticHasValidPassword( const Sequence< sal_Int8 > &aReadBuffer, const rtl::Reference < EncryptionData > &rData ) +sal_Bool ZipFile::StaticHasValidPassword( const uno::Reference< lang::XMultiServiceFactory >& xFactory, const Sequence< sal_Int8 > &aReadBuffer, const ::rtl::Reference< EncryptionData > &rData ) { - if ( !rData.is() || !rData->aKey.getLength() ) + if ( !rData.is() || !rData->m_aKey.getLength() ) return sal_False; sal_Bool bRet = sal_False; - sal_Int32 nSize = aReadBuffer.getLength(); - - // make a temporary cipher - rtlCipher aCipher; - StaticGetCipher ( rData, aCipher, sal_True ); - - Sequence < sal_Int8 > aDecryptBuffer ( nSize ); - rtlDigest aDigest = rtl_digest_createSHA1(); - rtlDigestError aDigestResult; - Sequence < sal_uInt8 > aDigestSeq ( RTL_DIGEST_LENGTH_SHA1 ); - rtlCipherError aResult = rtl_cipher_decode ( aCipher, - aReadBuffer.getConstArray(), - nSize, - reinterpret_cast < sal_uInt8 * > (aDecryptBuffer.getArray()), - nSize); - if(aResult != rtl_Cipher_E_None ) { - OSL_ASSERT ( aResult == rtl_Cipher_E_None); + + uno::Reference< xml::crypto::XCipherContext > xCipher( StaticGetCipher( xFactory, rData, false ), uno::UNO_SET_THROW ); + + uno::Sequence< sal_Int8 > aDecryptBuffer; + uno::Sequence< sal_Int8 > aDecryptBuffer2; + try + { + aDecryptBuffer = xCipher->convertWithCipherContext( aReadBuffer ); + aDecryptBuffer2 = xCipher->finalizeCipherContextAndDispose(); + } + catch( uno::Exception& ) + { + // decryption with padding will throw the exception in finalizing if the buffer represent only part of the stream + // it is no problem, actually this is why we read 32 additional bytes ( two of maximal possible encryption blocks ) + } + + if ( aDecryptBuffer2.getLength() ) + { + sal_Int32 nOldLen = aDecryptBuffer.getLength(); + aDecryptBuffer.realloc( nOldLen + aDecryptBuffer2.getLength() ); + rtl_copyMemory( aDecryptBuffer.getArray() + nOldLen, aDecryptBuffer2.getArray(), aDecryptBuffer2.getLength() ); } - aDigestResult = rtl_digest_updateSHA1 ( aDigest, - static_cast < const void * > ( aDecryptBuffer.getConstArray() ), nSize ); - OSL_ASSERT ( aDigestResult == rtl_Digest_E_None ); + if ( aDecryptBuffer.getLength() > n_ConstDigestLength ) + aDecryptBuffer.realloc( n_ConstDigestLength ); + + uno::Sequence< sal_Int8 > aDigestSeq; + uno::Reference< xml::crypto::XDigestContext > xDigestContext( StaticGetDigestContextForChecksum( xFactory, rData ), uno::UNO_SET_THROW ); - aDigestResult = rtl_digest_getSHA1 ( aDigest, aDigestSeq.getArray(), RTL_DIGEST_LENGTH_SHA1 ); - OSL_ASSERT ( aDigestResult == rtl_Digest_E_None ); - (void)aDigestResult; + xDigestContext->updateDigest( aDecryptBuffer ); + aDigestSeq = xDigestContext->finalizeDigestAndDispose(); // If we don't have a digest, then we have to assume that the password is correct - if ( rData->aDigest.getLength() != 0 && - ( aDigestSeq.getLength() != rData->aDigest.getLength() || + if ( rData->m_aDigest.getLength() != 0 && + ( aDigestSeq.getLength() != rData->m_aDigest.getLength() || 0 != rtl_compareMemory ( aDigestSeq.getConstArray(), - rData->aDigest.getConstArray(), + rData->m_aDigest.getConstArray(), aDigestSeq.getLength() ) ) ) { // We should probably tell the user that the password they entered was wrong @@ -373,136 +493,44 @@ sal_Bool ZipFile::StaticHasValidPassword( const Sequence< sal_Int8 > &aReadBuffe else bRet = sal_True; - rtl_digest_destroySHA1 ( aDigest ); - return bRet; } -sal_Bool ZipFile::hasValidPassword ( ZipEntry & rEntry, const rtl::Reference < EncryptionData > &rData ) +sal_Bool ZipFile::hasValidPassword ( ZipEntry & rEntry, const ::rtl::Reference< EncryptionData >& rData ) { ::osl::MutexGuard aGuard( m_aMutex ); sal_Bool bRet = sal_False; - if ( rData->aKey.getLength() ) + if ( rData.is() && rData->m_aKey.getLength() ) { xSeek->seek( rEntry.nOffset ); sal_Int32 nSize = rEntry.nMethod == DEFLATED ? rEntry.nCompressedSize : rEntry.nSize; // Only want to read enough to verify the digest - nSize = nSize > n_ConstDigestLength ? n_ConstDigestLength : nSize; + if ( nSize > n_ConstDigestDecrypt ) + nSize = n_ConstDigestDecrypt; + Sequence < sal_Int8 > aReadBuffer ( nSize ); xStream->readBytes( aReadBuffer, nSize ); - bRet = StaticHasValidPassword( aReadBuffer, rData ); + bRet = StaticHasValidPassword( m_xFactory, aReadBuffer, rData ); } + return bRet; } -#if 0 -Reference < XInputStream > ZipFile::createFileStream( - ZipEntry & rEntry, - const rtl::Reference < EncryptionData > &rData, - sal_Bool bRawStream, - sal_Bool bIsEncrypted ) -{ - static OUString sServiceName ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.io.TempFile" ) ); - Reference < XInputStream > xTempStream = Reference < XInputStream > ( xFactory->createInstance ( sServiceName ), UNO_QUERY ); - return new XFileStream ( rEntry, xStream, xTempStream, rData, bRawStream, bIsEncrypted ); -} -Reference < XInputStream > ZipFile::createMemoryStream( - ZipEntry & rEntry, - const rtl::Reference < EncryptionData > &rData, - sal_Bool bRawStream, - sal_Bool bIsEncrypted ) -{ - sal_Int32 nUncompressedSize, nEnd; - if (bRawStream) - { - nUncompressedSize = rEntry.nMethod == DEFLATED ? rEntry.nCompressedSize : rEntry.nSize; - nEnd = rEntry.nOffset + nUncompressedSize; - } - else - { - nUncompressedSize = rEntry.nSize; - nEnd = rEntry.nMethod == DEFLATED ? rEntry.nOffset + rEntry.nCompressedSize : rEntry.nOffset + rEntry.nSize; - } - sal_Int32 nSize = rEntry.nMethod == DEFLATED ? rEntry.nCompressedSize : rEntry.nSize; - Sequence < sal_Int8 > aReadBuffer ( nSize ), aDecryptBuffer, aWriteBuffer; - rtlCipher aCipher; - - // If the encryption key is zero, we need to return the raw stream. First check if - // we have the salt. If we have the salt, then check if we have the encryption key - // if not, return rawStream instead. - - sal_Bool bHaveEncryptData = ( rData.is() && rData->aSalt.getLength() && rData->aInitVector.getLength() && rData->nIterationCount != 0 ) ? sal_True : sal_False; - sal_Bool bMustDecrypt = ( !bRawStream && bHaveEncryptData && bIsEncrypted ) ? sal_True : sal_False; - - if ( bMustDecrypt ) - { - StaticGetCipher ( rData, aCipher, sal_True ); - aDecryptBuffer.realloc ( nSize ); - } - - if ( nSize <0 ) - throw IOException ( ); - - xSeek->seek( rEntry.nOffset ); - xStream->readBytes( aReadBuffer, nSize ); // Now it holds the raw stuff from disk - - if ( bMustDecrypt ) - { - rtlCipherError aResult = rtl_cipher_decode ( aCipher, - aReadBuffer.getConstArray(), - nSize, - reinterpret_cast < sal_uInt8 * > (aDecryptBuffer.getArray()), - nSize); - OSL_ASSERT (aResult == rtl_Cipher_E_None); - aReadBuffer = aDecryptBuffer; // Now it holds the decrypted data - } - if (bRawStream || rEntry.nMethod == STORED) - aWriteBuffer = aReadBuffer; // bRawStream means the caller doesn't want it decompressed - else - { - aInflater.setInputSegment( aReadBuffer, 0, nSize ); - aWriteBuffer.realloc( nUncompressedSize ); - aInflater.doInflate( aWriteBuffer ); - aInflater.reset(); - } - - if ( bHaveEncryptData && !bMustDecrypt && bIsEncrypted ) - { - // if we have the data needed to decrypt it, but didn't want it decrypted (or - // we couldn't decrypt it due to wrong password), then we prepend this - // data to the stream - - // Make a buffer big enough to hold both the header and the data itself - Sequence < sal_Int8 > aEncryptedDataHeader ( n_ConstHeaderSize + - rData->aInitVector.getLength() + - rData->aSalt.getLength() + - rData->aDigest.getLength() + - aWriteBuffer.getLength() ); - sal_Int8 * pHeader = aEncryptedDataHeader.getArray(); - StaticFillHeader ( rData, rEntry.nSize, pHeader ); - memcpy ( pHeader, aWriteBuffer.getConstArray(), aWriteBuffer.getLength() ); - - // dump old buffer and point aWriteBuffer to the new one with the header - aWriteBuffer = aEncryptedDataHeader; - } - return Reference < XInputStream > ( new XMemoryStream ( aWriteBuffer ) ); -} -#endif -Reference < XInputStream > ZipFile::createUnbufferedStream( +uno::Reference< XInputStream > ZipFile::createUnbufferedStream( SotMutexHolderRef aMutexHolder, ZipEntry & rEntry, - const rtl::Reference < EncryptionData > &rData, + const ::rtl::Reference< EncryptionData > &rData, sal_Int8 nStreamMode, sal_Bool bIsEncrypted, ::rtl::OUString aMediaType ) { ::osl::MutexGuard aGuard( m_aMutex ); - return new XUnbufferedStream ( aMutexHolder, rEntry, xStream, rData, nStreamMode, bIsEncrypted, aMediaType, bRecoveryMode ); + return new XUnbufferedStream ( m_xFactory, aMutexHolder, rEntry, xStream, rData, nStreamMode, bIsEncrypted, aMediaType, bRecoveryMode ); } @@ -511,8 +539,8 @@ ZipEnumeration * SAL_CALL ZipFile::entries( ) return new ZipEnumeration ( aEntries ); } -Reference< XInputStream > SAL_CALL ZipFile::getInputStream( ZipEntry& rEntry, - const rtl::Reference < EncryptionData > &rData, +uno::Reference< XInputStream > SAL_CALL ZipFile::getInputStream( ZipEntry& rEntry, + const ::rtl::Reference< EncryptionData > &rData, sal_Bool bIsEncrypted, SotMutexHolderRef aMutexHolder ) throw(IOException, ZipException, RuntimeException) @@ -529,7 +557,7 @@ Reference< XInputStream > SAL_CALL ZipFile::getInputStream( ZipEntry& rEntry, // if we have a digest, then this file is an encrypted one and we should // check if we can decrypt it or not - if ( bIsEncrypted && rData.is() && rData->aDigest.getLength() ) + if ( bIsEncrypted && rData.is() && rData->m_aDigest.getLength() ) bNeedRawStream = !hasValidPassword ( rEntry, rData ); return createUnbufferedStream ( aMutexHolder, @@ -539,8 +567,8 @@ Reference< XInputStream > SAL_CALL ZipFile::getInputStream( ZipEntry& rEntry, bIsEncrypted ); } -Reference< XInputStream > SAL_CALL ZipFile::getDataStream( ZipEntry& rEntry, - const rtl::Reference < EncryptionData > &rData, +uno::Reference< XInputStream > SAL_CALL ZipFile::getDataStream( ZipEntry& rEntry, + const ::rtl::Reference< EncryptionData > &rData, sal_Bool bIsEncrypted, SotMutexHolderRef aMutexHolder ) throw ( packages::WrongPasswordException, @@ -562,12 +590,12 @@ Reference< XInputStream > SAL_CALL ZipFile::getDataStream( ZipEntry& rEntry, // to detect password correctness if ( !rData.is() ) throw ZipException( OUString(RTL_CONSTASCII_USTRINGPARAM( "Encrypted stream without encryption data!\n" )), - Reference< XInterface >() ); + uno::Reference< XInterface >() ); // if we have a digest, then this file is an encrypted one and we should // check if we can decrypt it or not - OSL_ENSURE( rData->aDigest.getLength(), "Can't detect password correctness without digest!\n" ); - if ( rData->aDigest.getLength() && !hasValidPassword ( rEntry, rData ) ) + OSL_ENSURE( rData->m_aDigest.getLength(), "Can't detect password correctness without digest!\n" ); + if ( rData->m_aDigest.getLength() && !hasValidPassword ( rEntry, rData ) ) throw packages::WrongPasswordException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); } else @@ -580,8 +608,8 @@ Reference< XInputStream > SAL_CALL ZipFile::getDataStream( ZipEntry& rEntry, bIsEncrypted ); } -Reference< XInputStream > SAL_CALL ZipFile::getRawData( ZipEntry& rEntry, - const rtl::Reference < EncryptionData > &rData, +uno::Reference< XInputStream > SAL_CALL ZipFile::getRawData( ZipEntry& rEntry, + const ::rtl::Reference< EncryptionData >& rData, sal_Bool bIsEncrypted, SotMutexHolderRef aMutexHolder ) throw(IOException, ZipException, RuntimeException) @@ -594,9 +622,9 @@ Reference< XInputStream > SAL_CALL ZipFile::getRawData( ZipEntry& rEntry, return createUnbufferedStream ( aMutexHolder, rEntry, rData, UNBUFF_STREAM_RAW, bIsEncrypted ); } -Reference< XInputStream > SAL_CALL ZipFile::getWrappedRawStream( +uno::Reference< XInputStream > SAL_CALL ZipFile::getWrappedRawStream( ZipEntry& rEntry, - const rtl::Reference < EncryptionData > &rData, + const ::rtl::Reference< EncryptionData >& rData, const ::rtl::OUString& aMediaType, SotMutexHolderRef aMutexHolder ) throw ( packages::NoEncryptionException, @@ -628,7 +656,7 @@ sal_Bool ZipFile::readLOC( ZipEntry &rEntry ) aGrabber >> nTestSig; if (nTestSig != LOCSIG) - throw ZipIOException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid LOC header (bad signature") ), Reference < XInterface > () ); + throw ZipIOException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid LOC header (bad signature") ), uno::Reference < XInterface > () ); aGrabber >> nVersion; aGrabber >> nFlag; aGrabber >> nHow; @@ -674,7 +702,7 @@ sal_Bool ZipFile::readLOC( ZipEntry &rEntry ) if ( bBroken && !bRecoveryMode ) throw ZipIOException( OUString( RTL_CONSTASCII_USTRINGPARAM( "The stream seems to be broken!" ) ), - Reference< XInterface >() ); + uno::Reference< XInterface >() ); return sal_True; } @@ -708,17 +736,17 @@ sal_Int32 ZipFile::findEND( ) } catch ( IllegalArgumentException& ) { - throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), Reference < XInterface > () ); + throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), uno::Reference < XInterface > () ); } catch ( NotConnectedException& ) { - throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), Reference < XInterface > () ); + throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), uno::Reference < XInterface > () ); } catch ( BufferSizeExceededException& ) { - throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), Reference < XInterface > () ); + throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), uno::Reference < XInterface > () ); } - throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), Reference < XInterface > () ); + throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), uno::Reference < XInterface > () ); } sal_Int32 ZipFile::readCEN() @@ -739,25 +767,25 @@ sal_Int32 ZipFile::readCEN() aGrabber >> nCenOff; if ( nTotal * CENHDR > nCenLen ) - throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "invalid END header (bad entry count)") ), Reference < XInterface > () ); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "invalid END header (bad entry count)") ), uno::Reference < XInterface > () ); if ( nTotal > ZIP_MAXENTRIES ) - throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "too many entries in ZIP File") ), Reference < XInterface > () ); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "too many entries in ZIP File") ), uno::Reference < XInterface > () ); if ( nCenLen < 0 || nCenLen > nEndPos ) - throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid END header (bad central directory size)") ), Reference < XInterface > () ); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid END header (bad central directory size)") ), uno::Reference < XInterface > () ); nCenPos = nEndPos - nCenLen; if ( nCenOff < 0 || nCenOff > nCenPos ) - throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid END header (bad central directory size)") ), Reference < XInterface > () ); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid END header (bad central directory size)") ), uno::Reference < XInterface > () ); nLocPos = nCenPos - nCenOff; aGrabber.seek( nCenPos ); Sequence < sal_Int8 > aCENBuffer ( nCenLen ); sal_Int64 nRead = aGrabber.readBytes ( aCENBuffer, nCenLen ); if ( static_cast < sal_Int64 > ( nCenLen ) != nRead ) - throw ZipException ( OUString ( RTL_CONSTASCII_USTRINGPARAM ( "Error reading CEN into memory buffer!") ), Reference < XInterface > () ); + throw ZipException ( OUString ( RTL_CONSTASCII_USTRINGPARAM ( "Error reading CEN into memory buffer!") ), uno::Reference < XInterface > () ); MemoryByteGrabber aMemGrabber ( aCENBuffer ); @@ -769,19 +797,19 @@ sal_Int32 ZipFile::readCEN() { aMemGrabber >> nTestSig; if ( nTestSig != CENSIG ) - throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (bad signature)") ), Reference < XInterface > () ); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (bad signature)") ), uno::Reference < XInterface > () ); aMemGrabber.skipBytes ( 2 ); aMemGrabber >> aEntry.nVersion; if ( ( aEntry.nVersion & 1 ) == 1 ) - throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (encrypted entry)") ), Reference < XInterface > () ); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (encrypted entry)") ), uno::Reference < XInterface > () ); aMemGrabber >> aEntry.nFlag; aMemGrabber >> aEntry.nMethod; if ( aEntry.nMethod != STORED && aEntry.nMethod != DEFLATED) - throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (bad compression method)") ), Reference < XInterface > () ); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (bad compression method)") ), uno::Reference < XInterface > () ); aMemGrabber >> aEntry.nTime; aMemGrabber >> aEntry.nCrc; @@ -797,13 +825,13 @@ sal_Int32 ZipFile::readCEN() aEntry.nOffset *= -1; if ( aEntry.nPathLen < 0 ) - throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "unexpected name length" ) ), Reference < XInterface > () ); + throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "unexpected name length" ) ), uno::Reference < XInterface > () ); if ( nCommentLen < 0 ) - throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "unexpected comment length" ) ), Reference < XInterface > () ); + throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "unexpected comment length" ) ), uno::Reference < XInterface > () ); if ( aEntry.nExtraLen < 0 ) - throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "unexpected extra header info length") ), Reference < XInterface > () ); + throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "unexpected extra header info length") ), uno::Reference < XInterface > () ); // read always in UTF8, some tools seem not to set UTF8 bit aEntry.sPath = rtl::OUString::intern ( (sal_Char *) aMemGrabber.getCurrentPos(), @@ -811,14 +839,14 @@ sal_Int32 ZipFile::readCEN() RTL_TEXTENCODING_UTF8 ); if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( aEntry.sPath, sal_True ) ) - throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip entry has an invalid name.") ), Reference < XInterface > () ); + throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip entry has an invalid name.") ), uno::Reference < XInterface > () ); aMemGrabber.skipBytes( aEntry.nPathLen + aEntry.nExtraLen + nCommentLen ); aEntries[aEntry.sPath] = aEntry; } if (nCount != nTotal) - throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Count != Total") ), Reference < XInterface > () ); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Count != Total") ), uno::Reference < XInterface > () ); } catch ( IllegalArgumentException & ) { @@ -991,15 +1019,15 @@ sal_Int32 ZipFile::recover() } catch ( IllegalArgumentException& ) { - throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), Reference < XInterface > () ); + throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), uno::Reference < XInterface > () ); } catch ( NotConnectedException& ) { - throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), Reference < XInterface > () ); + throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), uno::Reference < XInterface > () ); } catch ( BufferSizeExceededException& ) { - throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), Reference < XInterface > () ); + throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), uno::Reference < XInterface > () ); } } diff --git a/package/source/zipapi/ZipOutputStream.cxx b/package/source/zipapi/ZipOutputStream.cxx index 4e08b7e8520d..b1b8ce7e2c38 100644 --- a/package/source/zipapi/ZipOutputStream.cxx +++ b/package/source/zipapi/ZipOutputStream.cxx @@ -28,18 +28,21 @@ // MARKER(update_precomp.py): autogen include statement, do not remove #include "precompiled_package.hxx" -#include <ZipOutputStream.hxx> + #include <com/sun/star/packages/zip/ZipConstants.hpp> +#include <com/sun/star/io/XOutputStream.hpp> +#include <comphelper/storagehelper.hxx> + #include <osl/time.h> + #include <EncryptionData.hxx> #include <PackageConstants.hxx> #include <ZipEntry.hxx> #include <ZipFile.hxx> -#include <rtl/ref.hxx> -#include <com/sun/star/io/XOutputStream.hpp> - -#include <comphelper/storagehelper.hxx> +#include <ZipPackageStream.hxx> +#include <ZipOutputStream.hxx> +using namespace com::sun::star; using namespace com::sun::star::io; using namespace com::sun::star::uno; using namespace com::sun::star::packages; @@ -48,17 +51,18 @@ using namespace com::sun::star::packages::zip::ZipConstants; /** This class is used to write Zip files */ -ZipOutputStream::ZipOutputStream( Reference < XOutputStream > &xOStream ) -: xStream(xOStream) -, aBuffer(n_ConstBufferSize) +ZipOutputStream::ZipOutputStream( const uno::Reference< lang::XMultiServiceFactory >& xFactory, + const uno::Reference < XOutputStream > &xOStream ) +: m_xFactory( xFactory ) +, xStream(xOStream) +, m_aDeflateBuffer(n_ConstBufferSize) , aDeflater(DEFAULT_COMPRESSION, sal_True) , aChucker(xOStream) , pCurrentEntry(NULL) , nMethod(DEFLATED) , bFinished(sal_False) , bEncryptCurrentEntry(sal_False) - - +, m_pCurrentStream(NULL) { } @@ -80,7 +84,7 @@ void SAL_CALL ZipOutputStream::setLevel( sal_Int32 nNewLevel ) } void SAL_CALL ZipOutputStream::putNextEntry( ZipEntry& rEntry, - rtl::Reference < EncryptionData > &xEncryptData, + ZipPackageStream* pStream, sal_Bool bEncrypt) throw(IOException, RuntimeException) { @@ -94,18 +98,20 @@ void SAL_CALL ZipOutputStream::putNextEntry( ZipEntry& rEntry, rEntry.nFlag = 1 << 11; if (rEntry.nSize == -1 || rEntry.nCompressedSize == -1 || rEntry.nCrc == -1) + { + rEntry.nSize = rEntry.nCompressedSize = 0; rEntry.nFlag |= 8; + } if (bEncrypt) { bEncryptCurrentEntry = sal_True; - ZipFile::StaticGetCipher( xEncryptData, aCipher, sal_False ); - - aDigest = rtl_digest_createSHA1(); + m_xCipherContext = ZipFile::StaticGetCipher( m_xFactory, pStream->GetEncryptionData(), true ); + m_xDigestContext = ZipFile::StaticGetDigestContextForChecksum( m_xFactory, pStream->GetEncryptionData() ); mnDigested = 0; rEntry.nFlag |= 1 << 4; - pCurrentEncryptData = xEncryptData.get(); + m_pCurrentStream = pStream; } sal_Int32 nLOCLength = writeLOC(rEntry); rEntry.nOffset = static_cast < sal_Int32 > (aChucker.GetPosition()) - nLOCLength; @@ -144,11 +150,12 @@ void SAL_CALL ZipOutputStream::closeEntry( ) } else { - pEntry->nSize = aDeflater.getTotalIn(); - pEntry->nCompressedSize = aDeflater.getTotalOut(); + if ( !bEncryptCurrentEntry ) + { + pEntry->nSize = aDeflater.getTotalIn(); + pEntry->nCompressedSize = aDeflater.getTotalOut(); + } pEntry->nCrc = aCRC.getValue(); - if ( bEncryptCurrentEntry ) - pEntry->nSize = pEntry->nCompressedSize; writeEXT(*pEntry); } aDeflater.reset(); @@ -165,19 +172,22 @@ void SAL_CALL ZipOutputStream::closeEntry( ) if (bEncryptCurrentEntry) { - rtlDigestError aDigestResult; - aEncryptionBuffer.realloc ( 0 ); bEncryptCurrentEntry = sal_False; - rtl_cipher_destroy ( aCipher ); - pCurrentEncryptData->aDigest.realloc ( RTL_DIGEST_LENGTH_SHA1 ); - aDigestResult = rtl_digest_getSHA1 ( aDigest, - reinterpret_cast < sal_uInt8 * > ( pCurrentEncryptData->aDigest.getArray() ), - RTL_DIGEST_LENGTH_SHA1 ); - OSL_ASSERT( aDigestResult == rtl_Digest_E_None ); - (void)aDigestResult; - rtl_digest_destroySHA1 ( aDigest ); + + m_xCipherContext.clear(); + + uno::Sequence< sal_Int8 > aDigestSeq; + if ( m_xDigestContext.is() ) + { + aDigestSeq = m_xDigestContext->finalizeDigestAndDispose(); + m_xDigestContext.clear(); + } + + if ( m_pCurrentStream ) + m_pCurrentStream->setDigest( aDigestSeq ); } pCurrentEntry = NULL; + m_pCurrentStream = NULL; } } @@ -242,43 +252,53 @@ void SAL_CALL ZipOutputStream::finish( ) void ZipOutputStream::doDeflate() { - sal_Int32 nLength = aDeflater.doDeflateSegment(aBuffer, 0, aBuffer.getLength()); - sal_Int32 nOldLength = aBuffer.getLength(); + sal_Int32 nLength = aDeflater.doDeflateSegment(m_aDeflateBuffer, 0, m_aDeflateBuffer.getLength()); if ( nLength > 0 ) { - Sequence < sal_Int8 > aTmpBuffer ( aBuffer.getConstArray(), nLength ); - const void *pTmpBuffer = static_cast < const void * > ( aTmpBuffer.getConstArray() ); - if (bEncryptCurrentEntry) + uno::Sequence< sal_Int8 > aTmpBuffer( m_aDeflateBuffer.getConstArray(), nLength ); + if ( bEncryptCurrentEntry && m_xDigestContext.is() && m_xCipherContext.is() ) { // Need to update our digest before encryption... - rtlDigestError aDigestResult = rtl_Digest_E_None; - sal_Int16 nDiff = n_ConstDigestLength - mnDigested; + sal_Int32 nDiff = n_ConstDigestLength - mnDigested; if ( nDiff ) { - sal_Int16 nEat = static_cast < sal_Int16 > ( nDiff > nLength ? nLength : nDiff ); - aDigestResult = rtl_digest_updateSHA1 ( aDigest, pTmpBuffer, nEat ); - mnDigested = mnDigested + nEat; + sal_Int32 nEat = ::std::min( nLength, nDiff ); + uno::Sequence< sal_Int8 > aTmpSeq( aTmpBuffer.getConstArray(), nEat ); + m_xDigestContext->updateDigest( aTmpSeq ); + mnDigested = mnDigested + static_cast< sal_Int16 >( nEat ); } - OSL_ASSERT( aDigestResult == rtl_Digest_E_None ); - (void)aDigestResult; - - aEncryptionBuffer.realloc ( nLength ); - rtlCipherError aCipherResult; - aCipherResult = rtl_cipher_encode ( aCipher, pTmpBuffer, - nLength, reinterpret_cast < sal_uInt8 * > (aEncryptionBuffer.getArray()), nLength ); - OSL_ASSERT( aCipherResult == rtl_Cipher_E_None ); - (void)aCipherResult; + uno::Sequence< sal_Int8 > aEncryptionBuffer = m_xCipherContext->convertWithCipherContext( aTmpBuffer ); aChucker.WriteBytes( aEncryptionBuffer ); - aCRC.update ( aEncryptionBuffer ); - aEncryptionBuffer.realloc ( nOldLength ); + + // the sizes as well as checksum for encrypted streams is calculated here + pCurrentEntry->nCompressedSize += aEncryptionBuffer.getLength(); + pCurrentEntry->nSize = pCurrentEntry->nCompressedSize; + aCRC.update( aEncryptionBuffer ); } else + { aChucker.WriteBytes ( aTmpBuffer ); + } + } + + if ( aDeflater.finished() && bEncryptCurrentEntry && m_xDigestContext.is() && m_xCipherContext.is() ) + { + uno::Sequence< sal_Int8 > aEncryptionBuffer = m_xCipherContext->finalizeCipherContextAndDispose(); + if ( aEncryptionBuffer.getLength() ) + { + aChucker.WriteBytes( aEncryptionBuffer ); + + // the sizes as well as checksum for encrypted streams is calculated hier + pCurrentEntry->nCompressedSize += aEncryptionBuffer.getLength(); + pCurrentEntry->nSize = pCurrentEntry->nCompressedSize; + aCRC.update( aEncryptionBuffer ); + } } } + void ZipOutputStream::writeEND(sal_uInt32 nOffset, sal_uInt32 nLength) throw(IOException, RuntimeException) { @@ -295,7 +315,7 @@ void ZipOutputStream::writeCEN( const ZipEntry &rEntry ) throw(IOException, RuntimeException) { if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( rEntry.sPath, sal_True ) ) - throw IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Unexpected character is used in file name." ) ), Reference< XInterface >() ); + throw IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Unexpected character is used in file name." ) ), uno::Reference< XInterface >() ); ::rtl::OString sUTF8Name = ::rtl::OUStringToOString( rEntry.sPath, RTL_TEXTENCODING_UTF8 ); sal_Int16 nNameLength = static_cast < sal_Int16 > ( sUTF8Name.getLength() ); @@ -344,7 +364,7 @@ sal_Int32 ZipOutputStream::writeLOC( const ZipEntry &rEntry ) throw(IOException, RuntimeException) { if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( rEntry.sPath, sal_True ) ) - throw IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Unexpected character is used in file name." ) ), Reference< XInterface >() ); + throw IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Unexpected character is used in file name." ) ), uno::Reference< XInterface >() ); ::rtl::OString sUTF8Name = ::rtl::OUStringToOString( rEntry.sPath, RTL_TEXTENCODING_UTF8 ); sal_Int16 nNameLength = static_cast < sal_Int16 > ( sUTF8Name.getLength() ); diff --git a/package/source/zipapi/blowfishcontext.cxx b/package/source/zipapi/blowfishcontext.cxx new file mode 100644 index 000000000000..6459cc4e4b5e --- /dev/null +++ b/package/source/zipapi/blowfishcontext.cxx @@ -0,0 +1,122 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_package.hxx" + +#include <rtl/cipher.h> +#include <rtl/ref.hxx> + +#include "blowfishcontext.hxx" + +using namespace ::com::sun::star; + +// static +uno::Reference< xml::crypto::XCipherContext > BlowfishCFB8CipherContext::Create( const uno::Sequence< sal_Int8 >& aDerivedKey, const uno::Sequence< sal_Int8 >& aInitVector, bool bEncrypt ) +{ + ::rtl::Reference< BlowfishCFB8CipherContext > xResult = new BlowfishCFB8CipherContext(); + xResult->m_pCipher = rtl_cipher_create( rtl_Cipher_AlgorithmBF, rtl_Cipher_ModeStream ); + if ( !xResult->m_pCipher ) + throw uno::RuntimeException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Can not create cipher!")), + uno::Reference< XInterface >() ); + + if ( rtl_Cipher_E_None != rtl_cipher_init( + xResult->m_pCipher, + bEncrypt ? rtl_Cipher_DirectionEncode : rtl_Cipher_DirectionDecode, + reinterpret_cast< const sal_uInt8* >( aDerivedKey.getConstArray() ), + aDerivedKey.getLength(), + reinterpret_cast< const sal_uInt8* >( aInitVector.getConstArray() ), + aInitVector.getLength() ) ) + { + throw uno::RuntimeException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Can not initialize cipher!") ), + uno::Reference< XInterface >() ); + } + + xResult->m_bEncrypt = bEncrypt; + + return uno::Reference< xml::crypto::XCipherContext >( xResult.get() ); +} + +BlowfishCFB8CipherContext::~BlowfishCFB8CipherContext() +{ + if ( m_pCipher ) + { + rtl_cipher_destroy ( m_pCipher ); + m_pCipher = NULL; + } +} + +uno::Sequence< sal_Int8 > SAL_CALL BlowfishCFB8CipherContext::convertWithCipherContext( const uno::Sequence< ::sal_Int8 >& aData ) + throw( lang::IllegalArgumentException, lang::DisposedException, uno::RuntimeException ) +{ + ::osl::MutexGuard aGuard( m_aMutex ); + if ( !m_pCipher ) + throw lang::DisposedException(); + + uno::Sequence< sal_Int8 > aResult( aData.getLength() ); + rtlCipherError nError = rtl_Cipher_E_None; + + if ( m_bEncrypt ) + { + rtl_cipher_encode( m_pCipher, + aData.getConstArray(), + aData.getLength(), + reinterpret_cast< sal_uInt8* >( aResult.getArray() ), + aResult.getLength() ); + } + else + { + rtl_cipher_decode( m_pCipher, + aData.getConstArray(), + aData.getLength(), + reinterpret_cast< sal_uInt8* >( aResult.getArray() ), + aResult.getLength() ); + } + + if ( rtl_Cipher_E_None != nError ) + { + throw uno::RuntimeException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Can not decrypt/encrypt with cipher!") ), + uno::Reference< uno::XInterface >() ); + } + + return aResult; +} + +uno::Sequence< ::sal_Int8 > SAL_CALL BlowfishCFB8CipherContext::finalizeCipherContextAndDispose() + throw( lang::DisposedException, uno::RuntimeException ) +{ + ::osl::MutexGuard aGuard( m_aMutex ); + if ( !m_pCipher ) + throw lang::DisposedException(); + + rtl_cipher_destroy ( m_pCipher ); + m_pCipher = NULL; + + return uno::Sequence< sal_Int8 >(); +} + + diff --git a/package/source/zipapi/blowfishcontext.hxx b/package/source/zipapi/blowfishcontext.hxx new file mode 100644 index 000000000000..49cce2fc0e65 --- /dev/null +++ b/package/source/zipapi/blowfishcontext.hxx @@ -0,0 +1,58 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ +#ifndef _BLOWFISHCONTEXT_HXX +#define _BLOWFISHCONTEXT_HXX + +#include <com/sun/star/xml/crypto/XCipherContext.hpp> + +#include <cppuhelper/implbase1.hxx> +#include <osl/mutex.hxx> + +class BlowfishCFB8CipherContext : public cppu::WeakImplHelper1< ::com::sun::star::xml::crypto::XCipherContext > +{ + ::osl::Mutex m_aMutex; + void* m_pCipher; + bool m_bEncrypt; + + BlowfishCFB8CipherContext() + : m_pCipher( NULL ) + , m_bEncrypt( false ) + {} + +public: + + virtual ~BlowfishCFB8CipherContext(); + + static ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XCipherContext > + Create( const ::com::sun::star::uno::Sequence< sal_Int8 >& aDerivedKey, const ::com::sun::star::uno::Sequence< sal_Int8 >& aInitVector, bool bEncrypt ); + + virtual ::com::sun::star::uno::Sequence< ::sal_Int8 > SAL_CALL convertWithCipherContext( const ::com::sun::star::uno::Sequence< ::sal_Int8 >& aData ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::DisposedException, ::com::sun::star::uno::RuntimeException); + virtual ::com::sun::star::uno::Sequence< ::sal_Int8 > SAL_CALL finalizeCipherContextAndDispose( ) throw (::com::sun::star::lang::DisposedException, ::com::sun::star::uno::RuntimeException); +}; + +#endif // _BLOWFISHCONTEXT_HXX + diff --git a/package/source/zipapi/makefile.mk b/package/source/zipapi/makefile.mk index ec8d63694ec6..8a07d448ddbb 100644 --- a/package/source/zipapi/makefile.mk +++ b/package/source/zipapi/makefile.mk @@ -46,8 +46,10 @@ SLOFILES= \ $(SLO)$/CRC32.obj \ $(SLO)$/ByteChucker.obj \ $(SLO)$/ByteGrabber.obj \ + $(SLO)$/blowfishcontext.obj \ $(SLO)$/Inflater.obj \ $(SLO)$/Deflater.obj \ + $(SLO)$/sha1context.obj \ $(SLO)$/ZipEnumeration.obj \ $(SLO)$/ZipFile.obj \ $(SLO)$/ZipOutputStream.obj \ diff --git a/package/source/zipapi/sha1context.cxx b/package/source/zipapi/sha1context.cxx new file mode 100644 index 000000000000..0e34fd4a9661 --- /dev/null +++ b/package/source/zipapi/sha1context.cxx @@ -0,0 +1,97 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_package.hxx" + +#include <rtl/digest.h> +#include <rtl/ref.hxx> + +#include "sha1context.hxx" + +using namespace ::com::sun::star; + +// static +uno::Reference< xml::crypto::XDigestContext > SHA1DigestContext::Create() +{ + ::rtl::Reference< SHA1DigestContext > xResult = new SHA1DigestContext(); + xResult->m_pDigest = rtl_digest_createSHA1(); + if ( !xResult->m_pDigest ) + throw uno::RuntimeException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Can not create cipher!") ), + uno::Reference< XInterface >() ); + + return uno::Reference< xml::crypto::XDigestContext >( xResult.get() ); +} + +SHA1DigestContext::~SHA1DigestContext() +{ + if ( m_pDigest ) + { + rtl_digest_destroySHA1( m_pDigest ); + m_pDigest = NULL; + } +} + +void SAL_CALL SHA1DigestContext::updateDigest( const uno::Sequence< ::sal_Int8 >& aData ) + throw( lang::DisposedException, uno::RuntimeException ) +{ + ::osl::MutexGuard aGuard( m_aMutex ); + if ( !m_pDigest ) + throw lang::DisposedException(); + + if ( rtl_Digest_E_None != rtl_digest_updateSHA1( m_pDigest, aData.getConstArray(), aData.getLength() ) ) + { + rtl_digest_destroySHA1( m_pDigest ); + m_pDigest = NULL; + + throw uno::RuntimeException(); + } +} + +uno::Sequence< ::sal_Int8 > SAL_CALL SHA1DigestContext::finalizeDigestAndDispose() + throw( lang::DisposedException, uno::RuntimeException ) +{ + ::osl::MutexGuard aGuard( m_aMutex ); + if ( !m_pDigest ) + throw lang::DisposedException(); + + uno::Sequence< sal_Int8 > aResult( RTL_DIGEST_LENGTH_SHA1 ); + if ( rtl_Digest_E_None != rtl_digest_getSHA1( m_pDigest, reinterpret_cast< sal_uInt8* >( aResult.getArray() ), aResult.getLength() ) ) + { + rtl_digest_destroySHA1( m_pDigest ); + m_pDigest = NULL; + + throw uno::RuntimeException(); + } + + rtl_digest_destroySHA1( m_pDigest ); + m_pDigest = NULL; + + return aResult; +} + + diff --git a/package/source/zipapi/XMemoryStream.hxx b/package/source/zipapi/sha1context.hxx index 935712abbe42..6d9374db8a46 100644 --- a/package/source/zipapi/XMemoryStream.hxx +++ b/package/source/zipapi/sha1context.hxx @@ -25,21 +25,35 @@ * for a copy of the LGPLv3 License. * ************************************************************************/ -#ifndef _XMEMORY_STREAM_HXX -#define _XMEMORY_STREAM_HXX +#ifndef _SHA1CONTEXT_HXX +#define _SHA1CONTEXT_HXX -#include <ZipPackageBuffer.hxx> +#include <com/sun/star/xml/crypto/XDigestContext.hpp> -class ZipPackage; +#include <cppuhelper/implbase1.hxx> +#include <osl/mutex.hxx> -class XMemoryStream: public ZipPackageBuffer +class SHA1DigestContext : public cppu::WeakImplHelper1< ::com::sun::star::xml::crypto::XDigestContext > { + ::osl::Mutex m_aMutex; + void* m_pDigest; + + SHA1DigestContext() + : m_pDigest( NULL ) + {} + public: - XMemoryStream ( com::sun::star::uno::Sequence < sal_Int8 > & rNewBuffer ); - virtual ~XMemoryStream(void); - virtual com::sun::star::uno::Any SAL_CALL queryInterface( const com::sun::star::uno::Type& rType ) - throw(com::sun::star::uno::RuntimeException); + + virtual ~SHA1DigestContext(); + + static ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XDigestContext > + Create(); + + virtual void SAL_CALL updateDigest( const ::com::sun::star::uno::Sequence< ::sal_Int8 >& aData ) throw (::com::sun::star::lang::DisposedException, ::com::sun::star::uno::RuntimeException); + virtual ::com::sun::star::uno::Sequence< ::sal_Int8 > SAL_CALL finalizeDigestAndDispose() throw (::com::sun::star::lang::DisposedException, ::com::sun::star::uno::RuntimeException); + }; + #endif /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/package/source/zippackage/ZipPackage.cxx b/package/source/zippackage/ZipPackage.cxx index e3ab5caabd63..ed5f81ba592d 100644 --- a/package/source/zippackage/ZipPackage.cxx +++ b/package/source/zippackage/ZipPackage.cxx @@ -17,7 +17,7 @@ * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License version 3 for more details - * (a copy is included in the LICENSE file that accompanied this code). + * ( a copy is included in the LICENSE file that accompanied this code ). * * You should have received a copy of the GNU Lesser General Public License * version 3 along with OpenOffice.org. If not, see @@ -26,7 +26,7 @@ * ************************************************************************/ -// MARKER(update_precomp.py): autogen include statement, do not remove +// MARKER( update_precomp.py ): autogen include statement, do not remove #include "precompiled_package.hxx" #include <ZipPackage.hxx> #include <ZipPackageSink.hxx> @@ -63,6 +63,8 @@ #include <com/sun/star/embed/UseBackupException.hpp> #include <com/sun/star/embed/StorageFormats.hpp> #include <com/sun/star/beans/NamedValue.hpp> +#include <com/sun/star/xml/crypto/DigestID.hpp> +#include <com/sun/star/xml/crypto/CipherID.hpp> #include <cppuhelper/implbase1.hxx> #include <ContentInfo.hxx> #include <cppuhelper/typeprovider.hxx> @@ -83,6 +85,7 @@ #include <comphelper/storagehelper.hxx> #include <comphelper/ofopxmlhelper.hxx> #include <comphelper/documentconstants.hxx> +#include <comphelper/sequenceashashmap.hxx> using namespace std; using namespace osl; @@ -156,35 +159,37 @@ public: class DummyInputStream : public ::cppu::WeakImplHelper1< XInputStream > { - virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >&, sal_Int32 ) - throw ( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) + virtual sal_Int32 SAL_CALL readBytes( uno::Sequence< sal_Int8 >&, sal_Int32 ) + throw ( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException ) { return 0; } - virtual sal_Int32 SAL_CALL readSomeBytes( Sequence< sal_Int8 >&, sal_Int32 ) - throw ( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) + virtual sal_Int32 SAL_CALL readSomeBytes( uno::Sequence< sal_Int8 >&, sal_Int32 ) + throw ( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException ) { return 0; } virtual void SAL_CALL skipBytes( sal_Int32 ) - throw ( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) + throw ( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException ) {} virtual sal_Int32 SAL_CALL available() - throw ( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) + throw ( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException ) { return 0; } virtual void SAL_CALL closeInput() - throw ( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) + throw ( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException ) {} }; //=========================================================================== -ZipPackage::ZipPackage (const uno::Reference < XMultiServiceFactory > &xNewFactory) +ZipPackage::ZipPackage ( const uno::Reference < XMultiServiceFactory > &xNewFactory ) : m_aMutexHolder( new SotMutexHolder ) +, m_nStartKeyGenerationID( xml::crypto::DigestID::SHA1 ) +, m_nChecksumDigestID( xml::crypto::DigestID::SHA1_1K ) +, m_nCommonEncryptionID( xml::crypto::CipherID::BLOWFISH_CFB_8 ) , m_bHasEncryptedEntries ( sal_False ) , m_bHasNonEncryptedEntries ( sal_False ) , m_bInconsistent ( sal_False ) -, m_bUseManifest ( sal_True ) , m_bForceRecovery ( sal_False ) , m_bMediaTypeFallbackUsed ( sal_False ) , m_nFormat( embed::StorageFormats::PACKAGE ) // package is the default format @@ -206,28 +211,19 @@ ZipPackage::~ZipPackage( void ) // So there is no need in explicit m_pRootFolder->releaseUpwardRef() call here any more // since m_pRootFolder has no parent and cleaning of it's children will be done automatically // during m_pRootFolder dieing by refcount. - -#if 0 - // As all folders and streams contain references to their parents, - // we must remove these references so that they will be deleted when - // the boost::unordered_map of the root folder is cleared, releasing all subfolders - // and substreams which in turn release theirs, etc. When m_xRootFolder is - // released when this destructor completes, the folder tree should be - // deleted fully (and automagically). - - m_pRootFolder->releaseUpwardRef(); -#endif } +//-------------------------------------------------------- void ZipPackage::parseManifest() { if ( m_nFormat == embed::StorageFormats::PACKAGE ) { sal_Bool bManifestParsed = sal_False; + bool bDifferentStartKeyAlgorithm = false; const OUString sMeta ( RTL_CONSTASCII_USTRINGPARAM ( "META-INF" ) ); if ( m_xRootFolder->hasByName( sMeta ) ) { - const OUString sManifest (RTL_CONSTASCII_USTRINGPARAM( "manifest.xml") ); + const OUString sManifest ( RTL_CONSTASCII_USTRINGPARAM( "manifest.xml" ) ); try { uno::Reference< XUnoTunnel > xTunnel; @@ -238,11 +234,11 @@ void ZipPackage::parseManifest() { aAny = xMetaInfFolder->getByName( sManifest ); aAny >>= xTunnel; - uno::Reference < XActiveDataSink > xSink (xTunnel, UNO_QUERY); - if (xSink.is()) + uno::Reference < XActiveDataSink > xSink ( xTunnel, UNO_QUERY ); + if ( xSink.is() ) { OUString sManifestReader ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.packages.manifest.ManifestReader" ) ); - uno::Reference < XManifestReader > xReader (m_xFactory->createInstance( sManifestReader ), UNO_QUERY ); + uno::Reference < XManifestReader > xReader ( m_xFactory->createInstance( sManifestReader ), UNO_QUERY ); if ( xReader.is() ) { const OUString sPropFullPath ( RTL_CONSTASCII_USTRINGPARAM ( "FullPath" ) ); @@ -253,45 +249,57 @@ void ZipPackage::parseManifest() const OUString sPropIterationCount ( RTL_CONSTASCII_USTRINGPARAM ( "IterationCount" ) ); const OUString sPropSize ( RTL_CONSTASCII_USTRINGPARAM ( "Size" ) ); const OUString sPropDigest ( RTL_CONSTASCII_USTRINGPARAM ( "Digest" ) ); + const OUString sPropDerivedKeySize ( RTL_CONSTASCII_USTRINGPARAM ( "DerivedKeySize" ) ); + const OUString sPropDigestAlgorithm ( RTL_CONSTASCII_USTRINGPARAM ( "DigestAlgorithm" ) ); + const OUString sPropEncryptionAlgorithm ( RTL_CONSTASCII_USTRINGPARAM ( "EncryptionAlgorithm" ) ); + const OUString sPropStartKeyAlgorithm ( RTL_CONSTASCII_USTRINGPARAM ( "StartKeyAlgorithm" ) ); - Sequence < Sequence < PropertyValue > > aManifestSequence = xReader->readManifestSequence ( xSink->getInputStream() ); + uno::Sequence < uno::Sequence < PropertyValue > > aManifestSequence = xReader->readManifestSequence ( xSink->getInputStream() ); sal_Int32 nLength = aManifestSequence.getLength(); - const Sequence < PropertyValue > *pSequence = aManifestSequence.getConstArray(); + const uno::Sequence < PropertyValue > *pSequence = aManifestSequence.getConstArray(); ZipPackageStream *pStream = NULL; ZipPackageFolder *pFolder = NULL; - for (sal_Int32 i = 0; i < nLength ; i++, pSequence++) + for ( sal_Int32 i = 0; i < nLength ; i++, pSequence++ ) { OUString sPath, sMediaType, sVersion; const PropertyValue *pValue = pSequence->getConstArray(); - const Any *pSalt = NULL, *pVector = NULL, *pCount = NULL, *pSize = NULL, *pDigest = NULL; - for (sal_Int32 j = 0, nNum = pSequence->getLength(); j < nNum; j++ ) + const Any *pSalt = NULL, *pVector = NULL, *pCount = NULL, *pSize = NULL, *pDigest = NULL, *pDigestAlg = NULL, *pEncryptionAlg = NULL, *pStartKeyAlg = NULL, *pDerivedKeySize = NULL; + for ( sal_Int32 j = 0, nNum = pSequence->getLength(); j < nNum; j++ ) { - if (pValue[j].Name.equals( sPropFullPath ) ) + if ( pValue[j].Name.equals( sPropFullPath ) ) pValue[j].Value >>= sPath; - else if (pValue[j].Name.equals( sPropVersion ) ) + else if ( pValue[j].Name.equals( sPropVersion ) ) pValue[j].Value >>= sVersion; - else if (pValue[j].Name.equals( sPropMediaType ) ) + else if ( pValue[j].Name.equals( sPropMediaType ) ) pValue[j].Value >>= sMediaType; - else if (pValue[j].Name.equals( sPropSalt ) ) - pSalt = &(pValue[j].Value); - else if (pValue[j].Name.equals( sPropInitialisationVector ) ) - pVector = &(pValue[j].Value); - else if (pValue[j].Name.equals( sPropIterationCount ) ) - pCount = &(pValue[j].Value); - else if (pValue[j].Name.equals( sPropSize ) ) - pSize = &(pValue[j].Value); - else if (pValue[j].Name.equals( sPropDigest ) ) - pDigest = &(pValue[j].Value); + else if ( pValue[j].Name.equals( sPropSalt ) ) + pSalt = &( pValue[j].Value ); + else if ( pValue[j].Name.equals( sPropInitialisationVector ) ) + pVector = &( pValue[j].Value ); + else if ( pValue[j].Name.equals( sPropIterationCount ) ) + pCount = &( pValue[j].Value ); + else if ( pValue[j].Name.equals( sPropSize ) ) + pSize = &( pValue[j].Value ); + else if ( pValue[j].Name.equals( sPropDigest ) ) + pDigest = &( pValue[j].Value ); + else if ( pValue[j].Name.equals( sPropDigestAlgorithm ) ) + pDigestAlg = &( pValue[j].Value ); + else if ( pValue[j].Name.equals( sPropEncryptionAlgorithm ) ) + pEncryptionAlg = &( pValue[j].Value ); + else if ( pValue[j].Name.equals( sPropStartKeyAlgorithm ) ) + pStartKeyAlg = &( pValue[j].Value ); + else if ( pValue[j].Name.equals( sPropDerivedKeySize ) ) + pDerivedKeySize = &( pValue[j].Value ); } - if (sPath.getLength() && hasByHierarchicalName ( sPath ) ) + if ( sPath.getLength() && hasByHierarchicalName ( sPath ) ) { aAny = getByHierarchicalName( sPath ); uno::Reference < XUnoTunnel > xUnoTunnel; aAny >>= xUnoTunnel; sal_Int64 nTest=0; - if ((nTest = xUnoTunnel->getSomething(ZipPackageFolder::static_getImplementationId())) != 0) + if ( (nTest = xUnoTunnel->getSomething( ZipPackageFolder::static_getImplementationId() )) != 0 ) { pFolder = reinterpret_cast < ZipPackageFolder* > ( nTest ); pFolder->SetMediaType ( sMediaType ); @@ -299,15 +307,16 @@ void ZipPackage::parseManifest() } else { - pStream = reinterpret_cast < ZipPackageStream* > ( xUnoTunnel->getSomething(ZipPackageStream::static_getImplementationId())); + pStream = reinterpret_cast < ZipPackageStream* > ( xUnoTunnel->getSomething( ZipPackageStream::static_getImplementationId() )); pStream->SetMediaType ( sMediaType ); pStream->SetFromManifest( sal_True ); - if (pSalt && pVector && pCount && pSize) + if ( pSalt && pVector && pCount && pSize && pDigest && pDigestAlg && pEncryptionAlg ) { - Sequence < sal_uInt8 > aSequence; - sal_Int32 nCount = 0, nSize = 0; - pStream->SetToBeEncrypted ( sal_True ); + uno::Sequence < sal_Int8 > aSequence; + sal_Int32 nCount = 0, nSize = 0, nDigestAlg = 0, nEncryptionAlg = 0, nDerivedKeySize = 16, nStartKeyAlg = xml::crypto::DigestID::SHA1; + + pStream->SetToBeEncrypted ( sal_True ); *pSalt >>= aSequence; pStream->setSalt ( aSequence ); @@ -321,18 +330,34 @@ void ZipPackage::parseManifest() *pSize >>= nSize; pStream->setSize ( nSize ); - if ( pDigest ) - { - *pDigest >>= aSequence; - pStream->setDigest ( aSequence ); - } + *pDigest >>= aSequence; + pStream->setDigest ( aSequence ); + + *pDigestAlg >>= nDigestAlg; + pStream->SetImportedChecksumAlgorithm( nDigestAlg ); + + *pEncryptionAlg >>= nEncryptionAlg; + pStream->SetImportedEncryptionAlgorithm( nEncryptionAlg ); + + if ( pDerivedKeySize ) + *pDerivedKeySize >>= nDerivedKeySize; + pStream->SetImportedDerivedKeySize( nDerivedKeySize ); + + if ( pStartKeyAlg ) + *pStartKeyAlg >>= nStartKeyAlg; + pStream->SetImportedStartKeyAlgorithm( nStartKeyAlg ); pStream->SetToBeCompressed ( sal_True ); pStream->SetToBeEncrypted ( sal_True ); pStream->SetIsEncrypted ( sal_True ); if ( !m_bHasEncryptedEntries && pStream->getName().equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "content.xml" ) ) ) ) + { m_bHasEncryptedEntries = sal_True; + m_nStartKeyGenerationID = nStartKeyAlg; + m_nChecksumDigestID = nDigestAlg; + m_nCommonEncryptionID = nEncryptionAlg; + } } else m_bHasNonEncryptedEntries = sal_True; @@ -382,7 +407,7 @@ void ZipPackage::parseManifest() nRead = aData.getLength(); if ( nRead ) - aPackageMediatype = ::rtl::OUString( (sal_Char*)aData.getConstArray(), nRead, RTL_TEXTENCODING_ASCII_US ); + aPackageMediatype = ::rtl::OUString( ( sal_Char* )aData.getConstArray(), nRead, RTL_TEXTENCODING_ASCII_US ); } } @@ -411,24 +436,35 @@ void ZipPackage::parseManifest() m_bInconsistent = m_pRootFolder->LookForUnexpectedODF12Streams( ::rtl::OUString() ); - sal_Bool bODF12AndOlder = ( m_pRootFolder->GetVersion().compareTo( ODFVER_012_TEXT ) >= 0 ); - if ( !m_bForceRecovery && bODF12AndOlder && m_bInconsistent ) + sal_Bool bODF12AndNewer = ( m_pRootFolder->GetVersion().compareTo( ODFVER_012_TEXT ) >= 0 ); + if ( !m_bForceRecovery && bODF12AndNewer ) { - // this is an ODF1.2 document that contains streams not referred in the manifest.xml; - // in case of ODF1.2 documents without version in manifest.xml the property IsInconsistent - // should be checked later - throw ZipIOException( - ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "there are streams not referred in manifest.xml\n" ) ), - uno::Reference< uno::XInterface >() ); + if ( m_bInconsistent ) + { + // this is an ODF1.2 document that contains streams not referred in the manifest.xml; + // in case of ODF1.2 documents without version in manifest.xml the property IsInconsistent + // should be checked later + throw ZipIOException( + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "there are streams not referred in manifest.xml\n" ) ), + uno::Reference< uno::XInterface >() ); + } + else if ( bDifferentStartKeyAlgorithm ) + { + // all the streams should be encrypted with the same StartKey in ODF1.2 + // TODO/LATER: in future the exception should be thrown + OSL_ENSURE( false, "ODF1.2 contains different StartKey Algorithms" ); + // throw ZipIOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "More than one Start Key Generation algorithm is specified!" ) ), uno::Reference< uno::XInterface >() ); + } } // in case it is a correct ODF1.2 document, the version must be set // and the META-INF folder is reserved for package format - if ( bODF12AndOlder ) + if ( bODF12AndNewer ) m_xRootFolder->removeByName( sMeta ); } } +//-------------------------------------------------------- void ZipPackage::parseContentType() { if ( m_nFormat == embed::StorageFormats::OFOPXML ) @@ -465,7 +501,7 @@ void ZipPackage::parseContentType() for ( nInd = 0; nInd < aContentTypeInfo[1].getLength(); nInd++ ) { ::rtl::OUString aPath; - if ( aContentTypeInfo[1][nInd].First.toChar() == (sal_Unicode)'/' ) + if ( aContentTypeInfo[1][nInd].First.toChar() == ( sal_Unicode )'/' ) aPath = aContentTypeInfo[1][nInd].First.copy( 1 ); else aPath = aContentTypeInfo[1][nInd].First; @@ -497,6 +533,7 @@ void ZipPackage::parseContentType() } } +//-------------------------------------------------------- void ZipPackage::getZipFileContents() { auto_ptr < ZipEnumeration > pEnum ( m_pZipFile->entries() ); @@ -506,7 +543,7 @@ void ZipPackage::getZipFileContents() sal_Int32 nOldIndex, nIndex, nStreamIndex; FolderHash::iterator aIter; - while (pEnum->hasMoreElements()) + while ( pEnum->hasMoreElements() ) { nIndex = nOldIndex = 0; pCurrent = m_pRootFolder; @@ -523,18 +560,18 @@ void ZipPackage::getZipFileContents() nStreamIndex = rName.lastIndexOf ( '/' ); if ( nStreamIndex != -1 ) { - sDirName = rName.copy ( 0, nStreamIndex); + sDirName = rName.copy ( 0, nStreamIndex ); aIter = m_aRecent.find ( sDirName ); if ( aIter != m_aRecent.end() ) - pCurrent = (*aIter).second; + pCurrent = ( *aIter ).second; } if ( pCurrent == m_pRootFolder ) { - while ( (nIndex = rName.indexOf('/', nOldIndex) ) != -1 ) + while ( ( nIndex = rName.indexOf( '/', nOldIndex ) ) != -1 ) { sTemp = rName.copy ( nOldIndex, nIndex - nOldIndex ); - if (nIndex == nOldIndex) + if ( nIndex == nOldIndex ) break; if ( !pCurrent->hasByName( sTemp ) ) { @@ -544,7 +581,7 @@ void ZipPackage::getZipFileContents() pCurrent = pPkgFolder; } else - pCurrent = pCurrent->doGetByName(sTemp).pFolder; + pCurrent = pCurrent->doGetByName( sTemp ).pFolder; nOldIndex = nIndex+1; } if ( nStreamIndex != -1 && sDirName.getLength() ) @@ -553,7 +590,7 @@ void ZipPackage::getZipFileContents() if ( rName.getLength() -1 != nStreamIndex ) { nStreamIndex++; - sTemp = rName.copy( nStreamIndex, rName.getLength() - nStreamIndex); + sTemp = rName.copy( nStreamIndex, rName.getLength() - nStreamIndex ); pPkgStream = new ZipPackageStream( *this, m_xFactory, m_bAllowRemoveOnInsert ); pPkgStream->SetPackageMember( sal_True ); pPkgStream->setZipEntryOnLoading( rEntry ); @@ -568,9 +605,9 @@ void ZipPackage::getZipFileContents() parseContentType(); } -// XInitialization -void SAL_CALL ZipPackage::initialize( const Sequence< Any >& aArguments ) - throw(Exception, RuntimeException) +//-------------------------------------------------------- +void SAL_CALL ZipPackage::initialize( const uno::Sequence< Any >& aArguments ) + throw( Exception, RuntimeException ) { RTL_LOGFILE_TRACE_AUTHOR ( "package", LOGFILE_AUTHOR, "{ ZipPackage::initialize" ); sal_Bool bBadZipFile = sal_False, bHaveZipFile = sal_True; @@ -582,7 +619,7 @@ void SAL_CALL ZipPackage::initialize( const Sequence< Any >& aArguments ) for( int ind = 0; ind < aArguments.getLength(); ind++ ) { OUString aParamUrl; - if ( (aArguments[ind] >>= aParamUrl)) + if ( ( aArguments[ind] >>= aParamUrl )) { m_eMode = e_IMode_URL; try @@ -629,13 +666,13 @@ void SAL_CALL ZipPackage::initialize( const Sequence< Any >& aArguments ) if( !bHasSizeProperty || ( bHasSizeProperty && aSize ) ) { uno::Reference < XActiveDataSink > xSink = new ZipPackageSink; - if (aContent.openStream ( xSink ) ) + if ( aContent.openStream ( xSink ) ) m_xContentStream = xSink->getInputStream(); } else bHaveZipFile = sal_False; } - catch (com::sun::star::uno::Exception&) + catch ( com::sun::star::uno::Exception& ) { // Exception derived from uno::Exception thrown. This probably // means the file doesn't exist...we'll create it at @@ -643,13 +680,13 @@ void SAL_CALL ZipPackage::initialize( const Sequence< Any >& aArguments ) bHaveZipFile = sal_False; } } - else if ( (aArguments[ind] >>= m_xStream ) ) + else if ( ( aArguments[ind] >>= m_xStream ) ) { // a writable stream can implement both XStream & XInputStream m_eMode = e_IMode_XStream; m_xContentStream = m_xStream->getInputStream(); } - else if ( (aArguments[ind] >>= m_xContentStream) ) + else if ( ( aArguments[ind] >>= m_xContentStream ) ) { m_eMode = e_IMode_XInputStream; } @@ -717,7 +754,7 @@ void SAL_CALL ZipPackage::initialize( const Sequence< Any >& aArguments ) try { - if (m_xContentStream.is()) + if ( m_xContentStream.is() ) { // the stream must be seekable, if it is not it will be wrapped m_xContentStream = ::comphelper::OSeekableInputWrapper::CheckSeekableCanWrap( m_xContentStream, m_xFactory ); @@ -732,7 +769,7 @@ void SAL_CALL ZipPackage::initialize( const Sequence< Any >& aArguments ) else bHaveZipFile = sal_False; } - catch (com::sun::star::uno::Exception&) + catch ( com::sun::star::uno::Exception& ) { // Exception derived from uno::Exception thrown. This probably // means the file doesn't exist...we'll create it at @@ -775,22 +812,23 @@ void SAL_CALL ZipPackage::initialize( const Sequence< Any >& aArguments ) RTL_LOGFILE_TRACE_AUTHOR ( "package", LOGFILE_AUTHOR, "} ZipPackage::initialize" ); } +//-------------------------------------------------------- Any SAL_CALL ZipPackage::getByHierarchicalName( const OUString& aName ) - throw(NoSuchElementException, RuntimeException) + throw( NoSuchElementException, RuntimeException ) { OUString sTemp, sDirName; sal_Int32 nOldIndex, nIndex, nStreamIndex; FolderHash::iterator aIter; - if ( (nIndex = aName.getLength() ) == 1 && *aName.getStr() == '/' ) - return makeAny ( uno::Reference < XUnoTunnel > (m_pRootFolder) ); + if ( ( nIndex = aName.getLength() ) == 1 && *aName.getStr() == '/' ) + return makeAny ( uno::Reference < XUnoTunnel > ( m_pRootFolder ) ); else { nStreamIndex = aName.lastIndexOf ( '/' ); bool bFolder = nStreamIndex == nIndex-1; if ( nStreamIndex != -1 ) { - sDirName = aName.copy ( 0, nStreamIndex); + sDirName = aName.copy ( 0, nStreamIndex ); aIter = m_aRecent.find ( sDirName ); if ( aIter != m_aRecent.end() ) { @@ -798,16 +836,16 @@ Any SAL_CALL ZipPackage::getByHierarchicalName( const OUString& aName ) { sal_Int32 nDirIndex = aName.lastIndexOf ( '/', nStreamIndex ); sTemp = aName.copy ( nDirIndex == -1 ? 0 : nDirIndex+1, nStreamIndex-nDirIndex-1 ); - if ( sTemp == (*aIter).second->getName() ) - return makeAny ( uno::Reference < XUnoTunnel > ( (*aIter).second ) ); + if ( sTemp == ( *aIter ).second->getName() ) + return makeAny ( uno::Reference < XUnoTunnel > ( ( *aIter ).second ) ); else m_aRecent.erase ( aIter ); } else { sTemp = aName.copy ( nStreamIndex + 1 ); - if ( (*aIter).second->hasByName( sTemp ) ) - return (*aIter).second->getByName( sTemp ); + if ( ( *aIter ).second->hasByName( sTemp ) ) + return ( *aIter ).second->getByName( sTemp ); else m_aRecent.erase( aIter ); } @@ -821,15 +859,15 @@ Any SAL_CALL ZipPackage::getByHierarchicalName( const OUString& aName ) nOldIndex = 0; ZipPackageFolder * pCurrent = m_pRootFolder; ZipPackageFolder * pPrevious = NULL; - while ( ( nIndex = aName.indexOf('/', nOldIndex)) != -1) + while ( ( nIndex = aName.indexOf( '/', nOldIndex )) != -1 ) { - sTemp = aName.copy (nOldIndex, nIndex - nOldIndex); + sTemp = aName.copy ( nOldIndex, nIndex - nOldIndex ); if ( nIndex == nOldIndex ) break; if ( pCurrent->hasByName( sTemp ) ) { pPrevious = pCurrent; - pCurrent = pCurrent->doGetByName(sTemp).pFolder; + pCurrent = pCurrent->doGetByName( sTemp ).pFolder; } else throw NoSuchElementException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); @@ -837,16 +875,16 @@ Any SAL_CALL ZipPackage::getByHierarchicalName( const OUString& aName ) } if ( bFolder ) { - if (nStreamIndex != -1 ) + if ( nStreamIndex != -1 ) m_aRecent[sDirName] = pPrevious; return makeAny ( uno::Reference < XUnoTunnel > ( pCurrent ) ); } else { - sTemp = aName.copy( nOldIndex, aName.getLength() - nOldIndex); + sTemp = aName.copy( nOldIndex, aName.getLength() - nOldIndex ); if ( pCurrent->hasByName ( sTemp ) ) { - if (nStreamIndex != -1 ) + if ( nStreamIndex != -1 ) m_aRecent[sDirName] = pCurrent; return pCurrent->getByName( sTemp ); } @@ -856,14 +894,15 @@ Any SAL_CALL ZipPackage::getByHierarchicalName( const OUString& aName ) } } +//-------------------------------------------------------- sal_Bool SAL_CALL ZipPackage::hasByHierarchicalName( const OUString& aName ) - throw(RuntimeException) + throw( RuntimeException ) { OUString sTemp, sDirName; sal_Int32 nOldIndex, nIndex, nStreamIndex; FolderHash::iterator aIter; - if ( (nIndex = aName.getLength() ) == 1 && *aName.getStr() == '/' ) + if ( ( nIndex = aName.getLength() ) == 1 && *aName.getStr() == '/' ) return sal_True; else { @@ -871,7 +910,7 @@ sal_Bool SAL_CALL ZipPackage::hasByHierarchicalName( const OUString& aName ) bool bFolder = nStreamIndex == nIndex-1; if ( nStreamIndex != -1 ) { - sDirName = aName.copy ( 0, nStreamIndex); + sDirName = aName.copy ( 0, nStreamIndex ); aIter = m_aRecent.find ( sDirName ); if ( aIter != m_aRecent.end() ) { @@ -879,7 +918,7 @@ sal_Bool SAL_CALL ZipPackage::hasByHierarchicalName( const OUString& aName ) { sal_Int32 nDirIndex = aName.lastIndexOf ( '/', nStreamIndex ); sTemp = aName.copy ( nDirIndex == -1 ? 0 : nDirIndex+1, nStreamIndex-nDirIndex-1 ); - if ( sTemp == (*aIter).second->getName() ) + if ( sTemp == ( *aIter ).second->getName() ) return sal_True; else m_aRecent.erase ( aIter ); @@ -887,7 +926,7 @@ sal_Bool SAL_CALL ZipPackage::hasByHierarchicalName( const OUString& aName ) else { sTemp = aName.copy ( nStreamIndex + 1 ); - if ( (*aIter).second->hasByName( sTemp ) ) + if ( ( *aIter ).second->hasByName( sTemp ) ) return sal_True; else m_aRecent.erase( aIter ); @@ -902,9 +941,9 @@ sal_Bool SAL_CALL ZipPackage::hasByHierarchicalName( const OUString& aName ) ZipPackageFolder * pCurrent = m_pRootFolder; ZipPackageFolder * pPrevious = NULL; nOldIndex = 0; - while ( ( nIndex = aName.indexOf('/', nOldIndex)) != -1) + while ( ( nIndex = aName.indexOf( '/', nOldIndex )) != -1 ) { - sTemp = aName.copy (nOldIndex, nIndex - nOldIndex); + sTemp = aName.copy ( nOldIndex, nIndex - nOldIndex ); if ( nIndex == nOldIndex ) break; if ( pCurrent->hasByName( sTemp ) ) @@ -923,7 +962,7 @@ sal_Bool SAL_CALL ZipPackage::hasByHierarchicalName( const OUString& aName ) } else { - sTemp = aName.copy( nOldIndex, aName.getLength() - nOldIndex); + sTemp = aName.copy( nOldIndex, aName.getLength() - nOldIndex ); if ( pCurrent->hasByName( sTemp ) ) { @@ -935,21 +974,22 @@ sal_Bool SAL_CALL ZipPackage::hasByHierarchicalName( const OUString& aName ) } } -// XSingleServiceFactory -uno::Reference< XInterface > SAL_CALL ZipPackage::createInstance( ) - throw(Exception, RuntimeException) +//-------------------------------------------------------- +uno::Reference< XInterface > SAL_CALL ZipPackage::createInstance() + throw( Exception, RuntimeException ) { - uno::Reference < XInterface > xRef = *(new ZipPackageStream ( *this, m_xFactory, m_bAllowRemoveOnInsert )); + uno::Reference < XInterface > xRef = *( new ZipPackageStream ( *this, m_xFactory, m_bAllowRemoveOnInsert ) ); return xRef; } -uno::Reference< XInterface > SAL_CALL ZipPackage::createInstanceWithArguments( const Sequence< Any >& aArguments ) - throw(Exception, RuntimeException) +//-------------------------------------------------------- +uno::Reference< XInterface > SAL_CALL ZipPackage::createInstanceWithArguments( const uno::Sequence< Any >& aArguments ) + throw( Exception, RuntimeException ) { sal_Bool bArg = sal_False; uno::Reference < XInterface > xRef; if ( aArguments.getLength() ) aArguments[0] >>= bArg; - if (bArg) + if ( bArg ) xRef = *new ZipPackageFolder ( m_xFactory, m_nFormat, m_bAllowRemoveOnInsert ); else xRef = *new ZipPackageStream ( *this, m_xFactory, m_bAllowRemoveOnInsert ); @@ -957,16 +997,17 @@ uno::Reference< XInterface > SAL_CALL ZipPackage::createInstanceWithArguments( c return xRef; } +//-------------------------------------------------------- void ZipPackage::WriteMimetypeMagicFile( ZipOutputStream& aZipOut ) { const OUString sMime ( RTL_CONSTASCII_USTRINGPARAM ( "mimetype" ) ); - if (m_xRootFolder->hasByName( sMime ) ) + if ( m_xRootFolder->hasByName( sMime ) ) m_xRootFolder->removeByName( sMime ); ZipEntry * pEntry = new ZipEntry; - sal_Int32 nBufferLength = m_pRootFolder->GetMediaType( ).getLength(); + sal_Int32 nBufferLength = m_pRootFolder->GetMediaType().getLength(); OString sMediaType = OUStringToOString( m_pRootFolder->GetMediaType(), RTL_TEXTENCODING_ASCII_US ); - Sequence< sal_Int8 > aType( (sal_Int8*)sMediaType.getStr(), + uno::Sequence< sal_Int8 > aType( ( sal_Int8* )sMediaType.getStr(), nBufferLength ); @@ -981,14 +1022,12 @@ void ZipPackage::WriteMimetypeMagicFile( ZipOutputStream& aZipOut ) try { - rtl::Reference < EncryptionData > xEmpty; - aZipOut.putNextEntry( *pEntry, xEmpty ); + aZipOut.putNextEntry( *pEntry, NULL ); aZipOut.write( aType, 0, nBufferLength ); aZipOut.closeEntry(); } catch ( ::com::sun::star::io::IOException & r ) { - OSL_FAIL( "Error adding mimetype to the ZipOutputStream" ); throw WrappedTargetException( OUString( RTL_CONSTASCII_USTRINGPARAM ( OSL_LOG_PREFIX "Error adding mimetype to the ZipOutputStream!" ) ), static_cast < OWeakObject * > ( this ), @@ -996,7 +1035,8 @@ void ZipPackage::WriteMimetypeMagicFile( ZipOutputStream& aZipOut ) } } -void ZipPackage::WriteManifest( ZipOutputStream& aZipOut, const vector< Sequence < PropertyValue > >& aManList ) +//-------------------------------------------------------- +void ZipPackage::WriteManifest( ZipOutputStream& aZipOut, const vector< uno::Sequence < PropertyValue > >& aManList ) { // Write the manifest uno::Reference < XOutputStream > xManOutStream; @@ -1006,28 +1046,29 @@ void ZipPackage::WriteManifest( ZipOutputStream& aZipOut, const vector< Sequence { ZipEntry * pEntry = new ZipEntry; ZipPackageBuffer *pBuffer = new ZipPackageBuffer( n_ConstBufferSize ); - xManOutStream = uno::Reference < XOutputStream > (*pBuffer, UNO_QUERY); + xManOutStream = uno::Reference < XOutputStream > ( *pBuffer, UNO_QUERY ); - pEntry->sPath = OUString( RTL_CONSTASCII_USTRINGPARAM ( "META-INF/manifest.xml") ); + pEntry->sPath = OUString( RTL_CONSTASCII_USTRINGPARAM ( "META-INF/manifest.xml" ) ); pEntry->nMethod = DEFLATED; pEntry->nCrc = pEntry->nSize = pEntry->nCompressedSize = -1; pEntry->nTime = ZipOutputStream::getCurrentDosTime(); - // Convert vector into a Sequence - Sequence < Sequence < PropertyValue > > aManifestSequence ( aManList.size() ); - Sequence < PropertyValue > * pSequence = aManifestSequence.getArray(); - for (vector < Sequence < PropertyValue > >::const_iterator aIter = aManList.begin(), aEnd = aManList.end(); + // Convert vector into a uno::Sequence + uno::Sequence < uno::Sequence < PropertyValue > > aManifestSequence ( aManList.size() ); + sal_Int32 nInd = 0; + for ( vector < uno::Sequence < PropertyValue > >::const_iterator aIter = aManList.begin(), aEnd = aManList.end(); aIter != aEnd; - ++aIter, ++pSequence) - *pSequence= (*aIter); + aIter++, nInd++ ) + { + aManifestSequence[nInd] = ( *aIter ); + } xWriter->writeManifestSequence ( xManOutStream, aManifestSequence ); sal_Int32 nBufferLength = static_cast < sal_Int32 > ( pBuffer->getPosition() ); pBuffer->realloc( nBufferLength ); // the manifest.xml is never encrypted - so pass an empty reference - rtl::Reference < EncryptionData > xEmpty; - aZipOut.putNextEntry( *pEntry, xEmpty ); + aZipOut.putNextEntry( *pEntry, NULL ); aZipOut.write( pBuffer->getSequence(), 0, nBufferLength ); aZipOut.closeEntry(); } @@ -1042,7 +1083,8 @@ void ZipPackage::WriteManifest( ZipOutputStream& aZipOut, const vector< Sequence } } -void ZipPackage::WriteContentTypes( ZipOutputStream& aZipOut, const vector< Sequence < PropertyValue > >& aManList ) +//-------------------------------------------------------- +void ZipPackage::WriteContentTypes( ZipOutputStream& aZipOut, const vector< uno::Sequence < PropertyValue > >& aManList ) { const OUString sFullPath ( RTL_CONSTASCII_USTRINGPARAM ( "FullPath" ) ); const OUString sMediaType ( RTL_CONSTASCII_USTRINGPARAM ( "MediaType" ) ); @@ -1051,12 +1093,12 @@ void ZipPackage::WriteContentTypes( ZipOutputStream& aZipOut, const vector< Sequ ZipPackageBuffer *pBuffer = new ZipPackageBuffer( n_ConstBufferSize ); uno::Reference< io::XOutputStream > xConTypeOutStream( *pBuffer, UNO_QUERY ); - pEntry->sPath = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "[Content_Types].xml") ); + pEntry->sPath = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "[Content_Types].xml" ) ); pEntry->nMethod = DEFLATED; pEntry->nCrc = pEntry->nSize = pEntry->nCompressedSize = -1; pEntry->nTime = ZipOutputStream::getCurrentDosTime(); - // Convert vector into a Sequence + // Convert vector into a uno::Sequence // TODO/LATER: use Defaulst entries in future uno::Sequence< beans::StringPair > aDefaultsSequence; uno::Sequence< beans::StringPair > aOverridesSequence( aManList.size() ); @@ -1068,14 +1110,14 @@ void ZipPackage::WriteContentTypes( ZipOutputStream& aZipOut, const vector< Sequ { ::rtl::OUString aPath; ::rtl::OUString aType; - OSL_ENSURE( (*aIter)[PKG_MNFST_MEDIATYPE].Name.equals( sMediaType ) && (*aIter)[PKG_MNFST_FULLPATH].Name.equals( sFullPath ), + OSL_ENSURE( ( *aIter )[PKG_MNFST_MEDIATYPE].Name.equals( sMediaType ) && ( *aIter )[PKG_MNFST_FULLPATH].Name.equals( sFullPath ), "The mediatype sequence format is wrong!\n" ); - (*aIter)[PKG_MNFST_MEDIATYPE].Value >>= aType; + ( *aIter )[PKG_MNFST_MEDIATYPE].Value >>= aType; if ( aType.getLength() ) { // only nonempty type makes sence here nSeqLength++; - (*aIter)[PKG_MNFST_FULLPATH].Value >>= aPath; + ( *aIter )[PKG_MNFST_FULLPATH].Value >>= aPath; aOverridesSequence[nSeqLength-1].First = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) ) + aPath; aOverridesSequence[nSeqLength-1].Second = aType; } @@ -1089,12 +1131,12 @@ void ZipPackage::WriteContentTypes( ZipOutputStream& aZipOut, const vector< Sequ pBuffer->realloc( nBufferLength ); // there is no encryption in this format currently - rtl::Reference < EncryptionData > xEmpty; - aZipOut.putNextEntry( *pEntry, xEmpty ); + aZipOut.putNextEntry( *pEntry, NULL ); aZipOut.write( pBuffer->getSequence(), 0, nBufferLength ); aZipOut.closeEntry(); } +//-------------------------------------------------------- void ZipPackage::ConnectTo( const uno::Reference< io::XInputStream >& xInStream ) { m_xContentSeek.set( xInStream, uno::UNO_QUERY_THROW ); @@ -1108,6 +1150,7 @@ void ZipPackage::ConnectTo( const uno::Reference< io::XInputStream >& xInStream m_pZipFile = new ZipFile ( m_xContentStream, m_xFactory, sal_False ); } +//-------------------------------------------------------- uno::Reference< io::XInputStream > ZipPackage::writeTempFile() { // In case the target local file does not exist or empty @@ -1154,9 +1197,9 @@ uno::Reference< io::XInputStream > ZipPackage::writeTempFile() } // Hand it to the ZipOutputStream: - ZipOutputStream aZipOut ( xTempOut ); - aZipOut.setMethod(DEFLATED); - aZipOut.setLevel(DEFAULT_COMPRESSION); + ZipOutputStream aZipOut( m_xFactory, xTempOut ); + aZipOut.setMethod( DEFLATED ); + aZipOut.setLevel( DEFAULT_COMPRESSION ); try { @@ -1169,7 +1212,7 @@ uno::Reference< io::XInputStream > ZipPackage::writeTempFile() if ( m_xRootFolder->hasByName( sMeta ) ) { - const OUString sManifest (RTL_CONSTASCII_USTRINGPARAM( "manifest.xml") ); + const OUString sManifest ( RTL_CONSTASCII_USTRINGPARAM( "manifest.xml" ) ); uno::Reference< XUnoTunnel > xTunnel; Any aAny = m_xRootFolder->getByName( sMeta ); @@ -1194,7 +1237,7 @@ uno::Reference< io::XInputStream > ZipPackage::writeTempFile() } // Create a vector to store data for the manifest.xml file - vector < Sequence < PropertyValue > > aManList; + vector < uno::Sequence < PropertyValue > > aManList; const OUString sMediaType ( RTL_CONSTASCII_USTRINGPARAM ( "MediaType" ) ); const OUString sVersion ( RTL_CONSTASCII_USTRINGPARAM ( "Version" ) ); @@ -1202,11 +1245,11 @@ uno::Reference< io::XInputStream > ZipPackage::writeTempFile() if ( m_nFormat == embed::StorageFormats::PACKAGE ) { - Sequence < PropertyValue > aPropSeq ( PKG_SIZE_NOENCR_MNFST ); + uno::Sequence < PropertyValue > aPropSeq( PKG_SIZE_NOENCR_MNFST ); aPropSeq [PKG_MNFST_MEDIATYPE].Name = sMediaType; - aPropSeq [PKG_MNFST_MEDIATYPE].Value <<= m_pRootFolder->GetMediaType( ); + aPropSeq [PKG_MNFST_MEDIATYPE].Value <<= m_pRootFolder->GetMediaType(); aPropSeq [PKG_MNFST_VERSION].Name = sVersion; - aPropSeq [PKG_MNFST_VERSION].Value <<= m_pRootFolder->GetVersion( ); + aPropSeq [PKG_MNFST_VERSION].Value <<= m_pRootFolder->GetVersion(); aPropSeq [PKG_MNFST_FULLPATH].Name = sFullPath; aPropSeq [PKG_MNFST_FULLPATH].Value <<= OUString ( RTL_CONSTASCII_USTRINGPARAM ( "/" ) ); @@ -1221,15 +1264,14 @@ uno::Reference< io::XInputStream > ZipPackage::writeTempFile() rtlRandomPool aRandomPool = rtl_random_createPool (); rtl_random_addBytes ( aRandomPool, &aTime, 8 ); - - // call saveContents (it will recursively save sub-directories + // call saveContents ( it will recursively save sub-directories OUString aEmptyString; - m_pRootFolder->saveContents( aEmptyString, aManList, aZipOut, m_aEncryptionKey, aRandomPool ); + m_pRootFolder->saveContents( aEmptyString, aManList, aZipOut, GetEncryptionKey(), aRandomPool ); // Clean up random pool memory rtl_random_destroyPool ( aRandomPool ); - if( m_bUseManifest && m_nFormat == embed::StorageFormats::PACKAGE ) + if( m_nFormat == embed::StorageFormats::PACKAGE ) { WriteManifest( aZipOut, aManList ); } @@ -1298,10 +1340,11 @@ uno::Reference< io::XInputStream > ZipPackage::writeTempFile() return xResult; } +//-------------------------------------------------------- uno::Reference< XActiveDataStreamer > ZipPackage::openOriginalForOutput() { // open and truncate the original file - Content aOriginalContent (m_aURL, uno::Reference < XCommandEnvironment >() ); + Content aOriginalContent ( m_aURL, uno::Reference < XCommandEnvironment >() ); uno::Reference< XActiveDataStreamer > xSink = new ActiveDataStreamer; if ( m_eMode == e_IMode_URL ) @@ -1335,7 +1378,7 @@ uno::Reference< XActiveDataStreamer > ZipPackage::openOriginalForOutput() aArg.Mode = OpenMode::DOCUMENT; aArg.Priority = 0; // unused aArg.Sink = xSink; - aArg.Properties = Sequence< Property >( 0 ); // unused + aArg.Properties = uno::Sequence< Property >( 0 ); // unused aOriginalContent.executeCommand( OUString(RTL_CONSTASCII_USTRINGPARAM( "open" )), makeAny( aArg ) ); } @@ -1349,9 +1392,9 @@ uno::Reference< XActiveDataStreamer > ZipPackage::openOriginalForOutput() return xSink; } -// XChangesBatch +//-------------------------------------------------------- void SAL_CALL ZipPackage::commitChanges() - throw(WrappedTargetException, RuntimeException) + throw( WrappedTargetException, RuntimeException ) { // lock the component for the time of commiting ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() ); @@ -1414,8 +1457,8 @@ void SAL_CALL ZipPackage::commitChanges() ::comphelper::OStorageHelper::CopyInputToOutput( xTempInStream, xOutputStream ); xOutputStream->flush(); uno::Reference< io::XAsyncOutputMonitor > asyncOutputMonitor( - xOutputStream, uno::UNO_QUERY); - if (asyncOutputMonitor.is()) { + xOutputStream, uno::UNO_QUERY ); + if ( asyncOutputMonitor.is() ) { asyncOutputMonitor->waitForCompletion(); } } @@ -1501,7 +1544,7 @@ void SAL_CALL ZipPackage::commitChanges() // if the file is still not corrupted, it can become after the next step aContent.executeCommand ( OUString ( RTL_CONSTASCII_USTRINGPARAM ( "transfer" ) ), aAny ); } - catch (::com::sun::star::uno::Exception& r) + catch ( ::com::sun::star::uno::Exception& r ) { if ( bCanBeCorrupted ) DisconnectFromTargetAndThrowException_Impl( xTempInStream ); @@ -1521,6 +1564,7 @@ void SAL_CALL ZipPackage::commitChanges() RTL_LOGFILE_TRACE_AUTHOR ( "package", LOGFILE_AUTHOR, "} ZipPackage::commitChanges" ); } +//-------------------------------------------------------- void ZipPackage::DisconnectFromTargetAndThrowException_Impl( const uno::Reference< io::XInputStream >& xTempStream ) { m_xStream = uno::Reference< io::XStream >( xTempStream, uno::UNO_QUERY ); @@ -1549,15 +1593,47 @@ void ZipPackage::DisconnectFromTargetAndThrowException_Impl( const uno::Referenc makeAny ( aException ) ); } -sal_Bool SAL_CALL ZipPackage::hasPendingChanges( ) - throw(RuntimeException) +//-------------------------------------------------------- +const uno::Sequence< sal_Int8 > ZipPackage::GetEncryptionKey() +{ + uno::Sequence< sal_Int8 > aResult; + + if ( m_aStorageEncryptionKeys.getLength() ) + { + ::rtl::OUString aNameToFind; + if ( m_nStartKeyGenerationID == xml::crypto::DigestID::SHA256 ) + aNameToFind = PACKAGE_ENCRYPTIONDATA_SHA256UTF8; + else if ( m_nStartKeyGenerationID == xml::crypto::DigestID::SHA1 ) + aNameToFind = PACKAGE_ENCRYPTIONDATA_SHA1UTF8; + else + throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "No expected key is provided!" ) ), uno::Reference< uno::XInterface >() ); + + for ( sal_Int32 nInd = 0; nInd < m_aStorageEncryptionKeys.getLength(); nInd++ ) + if ( m_aStorageEncryptionKeys[nInd].Name.equals( aNameToFind ) ) + m_aStorageEncryptionKeys[nInd].Value >>= aResult; + + // empty keys are not allowed here + // so it is not important whether there is no key, or the key is empty, it is an error + if ( !aResult.getLength() ) + throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "No expected key is provided!" ) ), uno::Reference< uno::XInterface >() ); + } + else + aResult = m_aEncryptionKey; + + return aResult; +} + +//-------------------------------------------------------- +sal_Bool SAL_CALL ZipPackage::hasPendingChanges() + throw( RuntimeException ) { return sal_False; } -Sequence< ElementChange > SAL_CALL ZipPackage::getPendingChanges( ) - throw(RuntimeException) +//-------------------------------------------------------- +Sequence< ElementChange > SAL_CALL ZipPackage::getPendingChanges() + throw( RuntimeException ) { - return Sequence < ElementChange > (); + return uno::Sequence < ElementChange > (); } /** @@ -1567,150 +1643,247 @@ Sequence< ElementChange > SAL_CALL ZipPackage::getPendingChanges( ) uno::Reference < XInterface >SAL_CALL ZipPackage_createInstance( const uno::Reference< XMultiServiceFactory > & xMgr ) { - return uno::Reference< XInterface >( *new ZipPackage(xMgr) ); + return uno::Reference< XInterface >( *new ZipPackage( xMgr ) ); } +//-------------------------------------------------------- OUString ZipPackage::static_getImplementationName() { return OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.packages.comp.ZipPackage" ) ); } +//-------------------------------------------------------- Sequence< OUString > ZipPackage::static_getSupportedServiceNames() { - Sequence< OUString > aNames(1); + uno::Sequence< OUString > aNames( 1 ); aNames[0] = OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.packages.Package" ) ); return aNames; } +//-------------------------------------------------------- sal_Bool SAL_CALL ZipPackage::static_supportsService( OUString const & rServiceName ) { return rServiceName == getSupportedServiceNames()[0]; } +//-------------------------------------------------------- OUString ZipPackage::getImplementationName() - throw (RuntimeException) + throw ( RuntimeException ) { return static_getImplementationName(); } +//-------------------------------------------------------- Sequence< OUString > ZipPackage::getSupportedServiceNames() - throw (RuntimeException) + throw ( RuntimeException ) { return static_getSupportedServiceNames(); } +//-------------------------------------------------------- sal_Bool SAL_CALL ZipPackage::supportsService( OUString const & rServiceName ) - throw (RuntimeException) + throw ( RuntimeException ) { return static_supportsService ( rServiceName ); } +//-------------------------------------------------------- uno::Reference < XSingleServiceFactory > ZipPackage::createServiceFactory( uno::Reference < XMultiServiceFactory > const & rServiceFactory ) { - return cppu::createSingleFactory (rServiceFactory, + return cppu::createSingleFactory ( rServiceFactory, static_getImplementationName(), ZipPackage_createInstance, - static_getSupportedServiceNames()); + static_getSupportedServiceNames() ); } namespace { struct lcl_ImplId : public rtl::Static< ::cppu::OImplementationId, lcl_ImplId > {}; } -// XUnoTunnel +//-------------------------------------------------------- Sequence< sal_Int8 > ZipPackage::getUnoTunnelImplementationId( void ) - throw (RuntimeException) + throw ( RuntimeException ) { ::cppu::OImplementationId &rId = lcl_ImplId::get(); return rId.getImplementationId(); } -sal_Int64 SAL_CALL ZipPackage::getSomething( const Sequence< sal_Int8 >& aIdentifier ) - throw(RuntimeException) +//-------------------------------------------------------- +sal_Int64 SAL_CALL ZipPackage::getSomething( const uno::Sequence< sal_Int8 >& aIdentifier ) + throw( RuntimeException ) { - if (aIdentifier.getLength() == 16 && 0 == rtl_compareMemory(getUnoTunnelImplementationId().getConstArray(), aIdentifier.getConstArray(), 16 ) ) + if ( aIdentifier.getLength() == 16 && 0 == rtl_compareMemory( getUnoTunnelImplementationId().getConstArray(), aIdentifier.getConstArray(), 16 ) ) return reinterpret_cast < sal_Int64 > ( this ); return 0; } -uno::Reference< XPropertySetInfo > SAL_CALL ZipPackage::getPropertySetInfo( ) - throw(RuntimeException) +//-------------------------------------------------------- +uno::Reference< XPropertySetInfo > SAL_CALL ZipPackage::getPropertySetInfo() + throw( RuntimeException ) { return uno::Reference < XPropertySetInfo > (); } + +//-------------------------------------------------------- void SAL_CALL ZipPackage::setPropertyValue( const OUString& aPropertyName, const Any& aValue ) - throw(UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException) + throw( UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException ) { if ( m_nFormat != embed::StorageFormats::PACKAGE ) throw UnknownPropertyException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); - if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("HasEncryptedEntries") ) - ||aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("HasNonEncryptedEntries") ) - ||aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("IsInconsistent") ) - ||aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("MediaTypeFallbackUsed") ) ) + if ( aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( HAS_ENCRYPTED_ENTRIES_PROPERTY ) ) + ||aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( HAS_NONENCRYPTED_ENTRIES_PROPERTY ) ) + ||aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( IS_INCONSISTENT_PROPERTY ) ) + ||aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( MEDIATYPE_FALLBACK_USED_PROPERTY ) ) ) throw PropertyVetoException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); - else if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("EncryptionKey") ) ) + else if ( aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( ENCRYPTION_KEY_PROPERTY ) ) ) { - if (!( aValue >>= m_aEncryptionKey ) || m_aEncryptionKey.getLength() == 0 ) + if ( !( aValue >>= m_aEncryptionKey ) ) throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 2 ); + + m_aStorageEncryptionKeys.realloc( 0 ); } - else if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("UseManifest") ) ) + else if ( aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( STORAGE_ENCRYPTION_KEYS_PROPERTY ) ) ) { - if (!( aValue >>= m_bUseManifest ) ) + // this property is only necessary to support raw passwords in storage API; + // because of this support the storage has to operate with more than one key dependent on storage generation algorithm; + // when this support is removed, the storage will get only one key from outside + // TODO/LATER: Get rid of this property as well as of support of raw passwords in storages + uno::Sequence< beans::NamedValue > aKeys; + if ( !( aValue >>= aKeys ) || ( aKeys.getLength() && aKeys.getLength() < 2 ) ) throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 2 ); + + if ( aKeys.getLength() ) + { + bool bHasSHA256 = false; + bool bHasSHA1 = false; + for ( sal_Int32 nInd = 0; nInd < aKeys.getLength(); nInd++ ) + { + if ( aKeys[nInd].Name.equals( PACKAGE_ENCRYPTIONDATA_SHA256UTF8 ) ) + bHasSHA256 = true; + if ( aKeys[nInd].Name.equals( PACKAGE_ENCRYPTIONDATA_SHA1UTF8 ) ) + bHasSHA1 = true; + } + + if ( !bHasSHA256 || !bHasSHA1 ) + throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Expected keys are not provided!" ) ), uno::Reference< uno::XInterface >(), 2 ); + } + + m_aStorageEncryptionKeys = aKeys; + m_aEncryptionKey.realloc( 0 ); + } + else if ( aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( ENCRYPTION_ALGORITHMS_PROPERTY ) ) ) + { + uno::Sequence< beans::NamedValue > aAlgorithms; + if ( m_pZipFile || !( aValue >>= aAlgorithms ) || aAlgorithms.getLength() == 0 ) + { + // the algorithms can not be changed if the file has a persistence based on the algorithms ( m_pZipFile ) + throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "unexpected algorithms list is provided." ) ), uno::Reference< uno::XInterface >(), 2 ); + } + + for ( sal_Int32 nInd = 0; nInd < aAlgorithms.getLength(); nInd++ ) + { + if ( aAlgorithms[nInd].Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "StartKeyGenerationAlgorithm" ) ) ) + { + sal_Int32 nID = 0; + if ( !( aAlgorithms[nInd].Value >>= nID ) + || ( nID != xml::crypto::DigestID::SHA256 && nID != xml::crypto::DigestID::SHA1 ) ) + throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Unexpected start key generation algorithm is provided!" ) ), uno::Reference< uno::XInterface >(), 2 ); + + m_nStartKeyGenerationID = nID; + } + else if ( aAlgorithms[nInd].Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "EncryptionAlgorithm" ) ) ) + { + sal_Int32 nID = 0; + if ( !( aAlgorithms[nInd].Value >>= nID ) + || ( nID != xml::crypto::CipherID::AES_CBC_W3C_PADDING && nID != xml::crypto::CipherID::BLOWFISH_CFB_8 ) ) + throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Unexpected start key generation algorithm is provided!" ) ), uno::Reference< uno::XInterface >(), 2 ); + + m_nCommonEncryptionID = nID; + } + else if ( aAlgorithms[nInd].Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "ChecksumAlgorithm" ) ) ) + { + sal_Int32 nID = 0; + if ( !( aAlgorithms[nInd].Value >>= nID ) + || ( nID != xml::crypto::DigestID::SHA1_1K && nID != xml::crypto::DigestID::SHA256_1K ) ) + throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Unexpected start key generation algorithm is provided!" ) ), uno::Reference< uno::XInterface >(), 2 ); + + m_nChecksumDigestID = nID; + } + else + { + OSL_ENSURE( sal_False, "Unexpected encryption algorithm is provided!" ); + throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "unexpected algorithms list is provided." ) ), uno::Reference< uno::XInterface >(), 2 ); + } + } } else throw UnknownPropertyException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); } + +//-------------------------------------------------------- Any SAL_CALL ZipPackage::getPropertyValue( const OUString& PropertyName ) - throw(UnknownPropertyException, WrappedTargetException, RuntimeException) + throw( UnknownPropertyException, WrappedTargetException, RuntimeException ) { // TODO/LATER: Activate the check when zip-ucp is ready // if ( m_nFormat != embed::StorageFormats::PACKAGE ) // throw UnknownPropertyException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); Any aAny; - if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "EncryptionKey" ) ) ) + if ( PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( ENCRYPTION_KEY_PROPERTY ) ) ) { aAny <<= m_aEncryptionKey; return aAny; } - else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "HasEncryptedEntries" ) ) ) + else if ( PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( ENCRYPTION_ALGORITHMS_PROPERTY ) ) ) { - aAny <<= m_bHasEncryptedEntries; + ::comphelper::SequenceAsHashMap aAlgorithms; + aAlgorithms[ ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StartKeyGenerationAlgorithm" ) ) ] <<= m_nStartKeyGenerationID; + aAlgorithms[ ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "EncryptionAlgorithm" ) ) ] <<= m_nCommonEncryptionID; + aAlgorithms[ ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ChecksumAlgorithm" ) ) ] <<= m_nChecksumDigestID; + aAny <<= aAlgorithms.getAsConstNamedValueList(); return aAny; } - else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "HasNonEncryptedEntries" ) ) ) + if ( PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( STORAGE_ENCRYPTION_KEYS_PROPERTY ) ) ) { - aAny <<= m_bHasNonEncryptedEntries; + aAny <<= m_aStorageEncryptionKeys; return aAny; } - else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "IsInconsistent" ) ) ) + else if ( PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( HAS_ENCRYPTED_ENTRIES_PROPERTY ) ) ) { - aAny <<= m_bInconsistent; + aAny <<= m_bHasEncryptedEntries; + return aAny; + } + else if ( PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( HAS_NONENCRYPTED_ENTRIES_PROPERTY ) ) ) + { + aAny <<= m_bHasNonEncryptedEntries; return aAny; } - else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "UseManifest" ) ) ) + else if ( PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( IS_INCONSISTENT_PROPERTY ) ) ) { - aAny <<= m_bUseManifest; + aAny <<= m_bInconsistent; return aAny; } - else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "MediaTypeFallbackUsed" ) ) ) + else if ( PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( MEDIATYPE_FALLBACK_USED_PROPERTY ) ) ) { aAny <<= m_bMediaTypeFallbackUsed; return aAny; } throw UnknownPropertyException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); } +//-------------------------------------------------------- void SAL_CALL ZipPackage::addPropertyChangeListener( const OUString& /*aPropertyName*/, const uno::Reference< XPropertyChangeListener >& /*xListener*/ ) - throw(UnknownPropertyException, WrappedTargetException, RuntimeException) + throw( UnknownPropertyException, WrappedTargetException, RuntimeException ) { } +//-------------------------------------------------------- void SAL_CALL ZipPackage::removePropertyChangeListener( const OUString& /*aPropertyName*/, const uno::Reference< XPropertyChangeListener >& /*aListener*/ ) - throw(UnknownPropertyException, WrappedTargetException, RuntimeException) + throw( UnknownPropertyException, WrappedTargetException, RuntimeException ) { } +//-------------------------------------------------------- void SAL_CALL ZipPackage::addVetoableChangeListener( const OUString& /*PropertyName*/, const uno::Reference< XVetoableChangeListener >& /*aListener*/ ) - throw(UnknownPropertyException, WrappedTargetException, RuntimeException) + throw( UnknownPropertyException, WrappedTargetException, RuntimeException ) { } +//-------------------------------------------------------- void SAL_CALL ZipPackage::removeVetoableChangeListener( const OUString& /*PropertyName*/, const uno::Reference< XVetoableChangeListener >& /*aListener*/ ) - throw(UnknownPropertyException, WrappedTargetException, RuntimeException) + throw( UnknownPropertyException, WrappedTargetException, RuntimeException ) { } diff --git a/package/source/zippackage/ZipPackageEntry.cxx b/package/source/zippackage/ZipPackageEntry.cxx index df789850189f..0d034f6af34c 100644 --- a/package/source/zippackage/ZipPackageEntry.cxx +++ b/package/source/zippackage/ZipPackageEntry.cxx @@ -75,18 +75,18 @@ void SAL_CALL ZipPackageEntry::setName( const OUString& aName ) // unfortunately no other exception than RuntimeException can be thrown here // usually the package is used through storage implementation, the problem should be detected there if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( aName, sal_True ) ) - throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Unexpected character is used in file name." ) ), Reference< XInterface >() ); + throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Unexpected character is used in file name." ) ), uno::Reference< XInterface >() ); msName = aName; if ( pParent ) pParent->doInsertByName ( this, sal_False ); } -Reference< XInterface > SAL_CALL ZipPackageEntry::getParent( ) +uno::Reference< XInterface > SAL_CALL ZipPackageEntry::getParent( ) throw(RuntimeException) { - // return Reference< XInterface >( xParent, UNO_QUERY ); - return Reference< XInterface >( static_cast< ::cppu::OWeakObject* >( pParent ), UNO_QUERY ); + // return uno::Reference< XInterface >( xParent, UNO_QUERY ); + return uno::Reference< XInterface >( static_cast< ::cppu::OWeakObject* >( pParent ), UNO_QUERY ); } void ZipPackageEntry::doSetParent ( ZipPackageFolder * pNewParent, sal_Bool bInsert ) @@ -97,11 +97,11 @@ void ZipPackageEntry::doSetParent ( ZipPackageFolder * pNewParent, sal_Bool bIns pNewParent->doInsertByName ( this, sal_False ); } -void SAL_CALL ZipPackageEntry::setParent( const Reference< XInterface >& xNewParent ) +void SAL_CALL ZipPackageEntry::setParent( const uno::Reference< XInterface >& xNewParent ) throw(NoSupportException, RuntimeException) { sal_Int64 nTest(0); - Reference < XUnoTunnel > xTunnel ( xNewParent, UNO_QUERY ); + uno::Reference < XUnoTunnel > xTunnel ( xNewParent, UNO_QUERY ); if ( !xNewParent.is() || ( nTest = xTunnel->getSomething ( ZipPackageFolder::static_getImplementationId () ) ) == 0 ) throw NoSupportException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); @@ -115,24 +115,24 @@ void SAL_CALL ZipPackageEntry::setParent( const Reference< XInterface >& xNewPar } } //XPropertySet -Reference< beans::XPropertySetInfo > SAL_CALL ZipPackageEntry::getPropertySetInfo( ) +uno::Reference< beans::XPropertySetInfo > SAL_CALL ZipPackageEntry::getPropertySetInfo( ) throw(RuntimeException) { - return Reference < beans::XPropertySetInfo > (); + return uno::Reference < beans::XPropertySetInfo > (); } -void SAL_CALL ZipPackageEntry::addPropertyChangeListener( const OUString& /*aPropertyName*/, const Reference< beans::XPropertyChangeListener >& /*xListener*/ ) +void SAL_CALL ZipPackageEntry::addPropertyChangeListener( const OUString& /*aPropertyName*/, const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ ) throw(beans::UnknownPropertyException, WrappedTargetException, RuntimeException) { } -void SAL_CALL ZipPackageEntry::removePropertyChangeListener( const OUString& /*aPropertyName*/, const Reference< beans::XPropertyChangeListener >& /*aListener*/ ) +void SAL_CALL ZipPackageEntry::removePropertyChangeListener( const OUString& /*aPropertyName*/, const uno::Reference< beans::XPropertyChangeListener >& /*aListener*/ ) throw(beans::UnknownPropertyException, WrappedTargetException, RuntimeException) { } -void SAL_CALL ZipPackageEntry::addVetoableChangeListener( const OUString& /*PropertyName*/, const Reference< beans::XVetoableChangeListener >& /*aListener*/ ) +void SAL_CALL ZipPackageEntry::addVetoableChangeListener( const OUString& /*PropertyName*/, const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ ) throw(beans::UnknownPropertyException, WrappedTargetException, RuntimeException) { } -void SAL_CALL ZipPackageEntry::removeVetoableChangeListener( const OUString& /*PropertyName*/, const Reference< beans::XVetoableChangeListener >& /*aListener*/ ) +void SAL_CALL ZipPackageEntry::removeVetoableChangeListener( const OUString& /*PropertyName*/, const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ ) throw(beans::UnknownPropertyException, WrappedTargetException, RuntimeException) { } diff --git a/package/source/zippackage/ZipPackageEntry.hxx b/package/source/zippackage/ZipPackageEntry.hxx deleted file mode 100644 index 6ae623a50664..000000000000 --- a/package/source/zippackage/ZipPackageEntry.hxx +++ /dev/null @@ -1,106 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/************************************************************************* - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * Copyright 2000, 2010 Oracle and/or its affiliates. - * - * OpenOffice.org - a multi-platform office productivity suite - * - * This file is part of OpenOffice.org. - * - * OpenOffice.org is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License version 3 - * only, as published by the Free Software Foundation. - * - * OpenOffice.org is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License version 3 for more details - * (a copy is included in the LICENSE file that accompanied this code). - * - * You should have received a copy of the GNU Lesser General Public License - * version 3 along with OpenOffice.org. If not, see - * <http://www.openoffice.org/license.html> - * for a copy of the LGPLv3 License. - * - ************************************************************************/ -#ifndef _ZIP_PACKAGE_ENTRY_HXX -#define _ZIP_PACKAGE_ENTRY_HXX - -#include <com/sun/star/container/XChild.hpp> -#include <com/sun/star/container/XNamed.hpp> -#include <com/sun/star/beans/XPropertySet.hpp> -#include <com/sun/star/lang/XUnoTunnel.hpp> -#include <com/sun/star/container/XNameContainer.hpp> -#include <com/sun/star/lang/XServiceInfo.hpp> -#include <ZipEntry.hxx> -#include <cppuhelper/implbase5.hxx> - -class ZipPackageFolder; - -class ZipPackageEntry : public cppu::WeakImplHelper5 -< - com::sun::star::container::XNamed, - com::sun::star::container::XChild, - com::sun::star::lang::XUnoTunnel, - com::sun::star::beans::XPropertySet, - com::sun::star::lang::XServiceInfo -> -{ -protected: - ::rtl::OUString msName; - bool mbIsFolder:1; - bool mbAllowRemoveOnInsert:1; - // com::sun::star::uno::Reference < com::sun::star::container::XNameContainer > xParent; - ::rtl::OUString sMediaType; - ZipPackageFolder * pParent; -public: - ZipEntry aEntry; - ZipPackageEntry ( bool bNewFolder = sal_False ); - virtual ~ZipPackageEntry( void ); - - ::rtl::OUString & GetMediaType () { return sMediaType; } - void SetMediaType ( const ::rtl::OUString & sNewType) { sMediaType = sNewType; } - void doSetParent ( ZipPackageFolder * pNewParent, sal_Bool bInsert ); - bool IsFolder ( ) { return mbIsFolder; } - ZipPackageFolder* GetParent ( ) { return pParent; } - void SetFolder ( bool bSetFolder ) { mbIsFolder = bSetFolder; } - - void clearParent ( void ) - { - // xParent.clear(); - pParent = NULL; - } - // XNamed - virtual ::rtl::OUString SAL_CALL getName( ) - throw(::com::sun::star::uno::RuntimeException); - virtual void SAL_CALL setName( const ::rtl::OUString& aName ) - throw(::com::sun::star::uno::RuntimeException); - // XChild - virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL getParent( ) - throw(::com::sun::star::uno::RuntimeException); - virtual void SAL_CALL setParent( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& Parent ) - throw(::com::sun::star::lang::NoSupportException, ::com::sun::star::uno::RuntimeException); - // XUnoTunnel - virtual sal_Int64 SAL_CALL getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier ) - throw(::com::sun::star::uno::RuntimeException) = 0; - // XPropertySet - virtual ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo( ) - throw(::com::sun::star::uno::RuntimeException); - virtual void SAL_CALL setPropertyValue( const ::rtl::OUString& aPropertyName, const ::com::sun::star::uno::Any& aValue ) - throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException) = 0; - virtual ::com::sun::star::uno::Any SAL_CALL getPropertyValue( const ::rtl::OUString& PropertyName ) - throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException) = 0; - virtual void SAL_CALL addPropertyChangeListener( const ::rtl::OUString& aPropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& xListener ) - throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException); - virtual void SAL_CALL removePropertyChangeListener( const ::rtl::OUString& aPropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& aListener ) - throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException); - virtual void SAL_CALL addVetoableChangeListener( const ::rtl::OUString& PropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XVetoableChangeListener >& aListener ) - throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException); - virtual void SAL_CALL removeVetoableChangeListener( const ::rtl::OUString& PropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XVetoableChangeListener >& aListener ) - throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException); -}; -#endif - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/package/source/zippackage/ZipPackageFolder.cxx b/package/source/zippackage/ZipPackageFolder.cxx index 3667c30847ca..01f8e7008e5b 100644 --- a/package/source/zippackage/ZipPackageFolder.cxx +++ b/package/source/zippackage/ZipPackageFolder.cxx @@ -47,22 +47,21 @@ #include <rtl/instance.hxx> #include <memory> +using namespace com::sun::star; using namespace com::sun::star::packages::zip::ZipConstants; using namespace com::sun::star::packages::zip; using namespace com::sun::star::packages; using namespace com::sun::star::container; using namespace com::sun::star::beans; using namespace com::sun::star::lang; -using namespace com::sun::star::uno; using namespace com::sun::star::io; using namespace cppu; using namespace std; using namespace ::com::sun::star; -using rtl::OUString; -namespace { struct lcl_CachedImplId : public rtl::Static< Sequence < sal_Int8 >, lcl_CachedImplId > {}; } +namespace { struct lcl_CachedImplId : public rtl::Static< uno::Sequence < sal_Int8 >, lcl_CachedImplId > {}; } -ZipPackageFolder::ZipPackageFolder ( const Reference< XMultiServiceFactory >& xFactory, +ZipPackageFolder::ZipPackageFolder ( const uno::Reference< XMultiServiceFactory >& xFactory, sal_Int32 nFormat, sal_Bool bAllowRemoveOnInsert ) : m_xFactory( xFactory ) @@ -81,7 +80,7 @@ ZipPackageFolder::ZipPackageFolder ( const Reference< XMultiServiceFactory >& xF aEntry.nCompressedSize = 0; aEntry.nSize = 0; aEntry.nOffset = -1; - Sequence < sal_Int8 > &rCachedImplId = lcl_CachedImplId::get(); + uno::Sequence < sal_Int8 > &rCachedImplId = lcl_CachedImplId::get(); if ( !rCachedImplId.getLength() ) rCachedImplId = getImplementationId(); } @@ -99,7 +98,7 @@ sal_Bool ZipPackageFolder::LookForUnexpectedODF12Streams( const ::rtl::OUString& !bHasUnexpected && aCI != aEnd; ++aCI) { - const OUString &rShortName = (*aCI).first; + const ::rtl::OUString &rShortName = (*aCI).first; const ContentInfo &rInfo = *(*aCI).second; if ( rInfo.bFolder ) @@ -111,7 +110,7 @@ sal_Bool ZipPackageFolder::LookForUnexpectedODF12Streams( const ::rtl::OUString& } else { - OUString sOwnPath = aPath + rShortName + OUString( RTL_CONSTASCII_USTRINGPARAM ( "/" ) ); + ::rtl::OUString sOwnPath = aPath + rShortName + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "/" ) ); bHasUnexpected = rInfo.pFolder->LookForUnexpectedODF12Streams( sOwnPath ); } } @@ -157,7 +156,7 @@ void ZipPackageFolder::setChildStreamsTypeByExtension( const beans::StringPair& aCI != aEnd; ++aCI) { - const OUString &rShortName = (*aCI).first; + const ::rtl::OUString &rShortName = (*aCI).first; const ContentInfo &rInfo = *(*aCI).second; if ( rInfo.bFolder ) @@ -193,14 +192,14 @@ const ::com::sun::star::uno::Sequence < sal_Int8 >& ZipPackageFolder::static_get } // XNameContainer -void SAL_CALL ZipPackageFolder::insertByName( const OUString& aName, const Any& aElement ) - throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException) +void SAL_CALL ZipPackageFolder::insertByName( const ::rtl::OUString& aName, const uno::Any& aElement ) + throw(IllegalArgumentException, ElementExistException, WrappedTargetException, uno::RuntimeException) { if (hasByName(aName)) throw ElementExistException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); else { - Reference < XUnoTunnel > xRef; + uno::Reference < XUnoTunnel > xRef; aElement >>= xRef; if ( ( aElement >>= xRef ) ) { @@ -227,8 +226,8 @@ void SAL_CALL ZipPackageFolder::insertByName( const OUString& aName, const Any& throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 0 ); } } -void SAL_CALL ZipPackageFolder::removeByName( const OUString& Name ) - throw(NoSuchElementException, WrappedTargetException, RuntimeException) +void SAL_CALL ZipPackageFolder::removeByName( const ::rtl::OUString& Name ) + throw(NoSuchElementException, WrappedTargetException, uno::RuntimeException) { ContentHash::iterator aIter = maContents.find ( Name ); if ( aIter == maContents.end() ) @@ -236,55 +235,55 @@ void SAL_CALL ZipPackageFolder::removeByName( const OUString& Name ) maContents.erase( aIter ); } // XEnumerationAccess -Reference< XEnumeration > SAL_CALL ZipPackageFolder::createEnumeration( ) - throw(RuntimeException) +uno::Reference< XEnumeration > SAL_CALL ZipPackageFolder::createEnumeration( ) + throw(uno::RuntimeException) { - return Reference < XEnumeration> (new ZipPackageFolderEnumeration(maContents)); + return uno::Reference < XEnumeration> (new ZipPackageFolderEnumeration(maContents)); } // XElementAccess -Type SAL_CALL ZipPackageFolder::getElementType( ) - throw(RuntimeException) +uno::Type SAL_CALL ZipPackageFolder::getElementType( ) + throw(uno::RuntimeException) { - return ::getCppuType ((const Reference< XUnoTunnel > *) 0); + return ::getCppuType ((const uno::Reference< XUnoTunnel > *) 0); } sal_Bool SAL_CALL ZipPackageFolder::hasElements( ) - throw(RuntimeException) + throw(uno::RuntimeException) { return maContents.size() > 0; } // XNameAccess -ContentInfo& ZipPackageFolder::doGetByName( const OUString& aName ) - throw(NoSuchElementException, WrappedTargetException, RuntimeException) +ContentInfo& ZipPackageFolder::doGetByName( const ::rtl::OUString& aName ) + throw(NoSuchElementException, WrappedTargetException, uno::RuntimeException) { ContentHash::iterator aIter = maContents.find ( aName ); if ( aIter == maContents.end()) throw NoSuchElementException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); return *(*aIter).second; } -Any SAL_CALL ZipPackageFolder::getByName( const OUString& aName ) - throw(NoSuchElementException, WrappedTargetException, RuntimeException) +uno::Any SAL_CALL ZipPackageFolder::getByName( const ::rtl::OUString& aName ) + throw(NoSuchElementException, WrappedTargetException, uno::RuntimeException) { - return makeAny ( doGetByName ( aName ).xTunnel ); + return uno::makeAny ( doGetByName ( aName ).xTunnel ); } -Sequence< OUString > SAL_CALL ZipPackageFolder::getElementNames( ) - throw(RuntimeException) +uno::Sequence< ::rtl::OUString > SAL_CALL ZipPackageFolder::getElementNames( ) + throw(uno::RuntimeException) { sal_uInt32 i=0, nSize = maContents.size(); - Sequence < OUString > aSequence ( nSize ); + uno::Sequence < ::rtl::OUString > aSequence ( nSize ); for ( ContentHash::const_iterator aIterator = maContents.begin(), aEnd = maContents.end(); aIterator != aEnd; ++i, ++aIterator) aSequence[i] = (*aIterator).first; return aSequence; } -sal_Bool SAL_CALL ZipPackageFolder::hasByName( const OUString& aName ) - throw(RuntimeException) +sal_Bool SAL_CALL ZipPackageFolder::hasByName( const ::rtl::OUString& aName ) + throw(uno::RuntimeException) { return maContents.find ( aName ) != maContents.end (); } // XNameReplace -void SAL_CALL ZipPackageFolder::replaceByName( const OUString& aName, const Any& aElement ) - throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException) +void SAL_CALL ZipPackageFolder::replaceByName( const ::rtl::OUString& aName, const uno::Any& aElement ) + throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, uno::RuntimeException) { if ( hasByName( aName ) ) removeByName( aName ); @@ -293,7 +292,7 @@ void SAL_CALL ZipPackageFolder::replaceByName( const OUString& aName, const Any& insertByName(aName, aElement); } -static void ImplSetStoredData( ZipEntry & rEntry, Reference < XInputStream> & rStream ) +static void ImplSetStoredData( ZipEntry & rEntry, uno::Reference< XInputStream> & rStream ) { // It's very annoying that we have to do this, but lots of zip packages // don't allow data descriptors for STORED streams, meaning we have to @@ -305,25 +304,29 @@ static void ImplSetStoredData( ZipEntry & rEntry, Reference < XInputStream> & rS rEntry.nCrc = aCRC32.getValue(); } -bool ZipPackageFolder::saveChild( const OUString &rShortName, const ContentInfo &rInfo, OUString &rPath, std::vector < Sequence < PropertyValue > > &rManList, ZipOutputStream & rZipOut, Sequence < sal_Int8 > &rEncryptionKey, rtlRandomPool &rRandomPool) +bool ZipPackageFolder::saveChild( const ::rtl::OUString &rShortName, const ContentInfo &rInfo, ::rtl::OUString &rPath, std::vector < uno::Sequence < PropertyValue > > &rManList, ZipOutputStream & rZipOut, const uno::Sequence < sal_Int8 >& rEncryptionKey, rtlRandomPool &rRandomPool) { bool bSuccess = true; - const OUString sMediaTypeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "MediaType" ) ); - const OUString sVersionProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Version" ) ); - const OUString sFullPathProperty ( RTL_CONSTASCII_USTRINGPARAM ( "FullPath" ) ); - const OUString sInitialisationVectorProperty ( RTL_CONSTASCII_USTRINGPARAM ( "InitialisationVector" ) ); - const OUString sSaltProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Salt" ) ); - const OUString sIterationCountProperty ( RTL_CONSTASCII_USTRINGPARAM ( "IterationCount" ) ); - const OUString sSizeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Size" ) ); - const OUString sDigestProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Digest" ) ); - - Sequence < PropertyValue > aPropSet (PKG_SIZE_NOENCR_MNFST); + const ::rtl::OUString sMediaTypeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "MediaType" ) ); + const ::rtl::OUString sVersionProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Version" ) ); + const ::rtl::OUString sFullPathProperty ( RTL_CONSTASCII_USTRINGPARAM ( "FullPath" ) ); + const ::rtl::OUString sInitialisationVectorProperty ( RTL_CONSTASCII_USTRINGPARAM ( "InitialisationVector" ) ); + const ::rtl::OUString sSaltProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Salt" ) ); + const ::rtl::OUString sIterationCountProperty ( RTL_CONSTASCII_USTRINGPARAM ( "IterationCount" ) ); + const ::rtl::OUString sSizeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Size" ) ); + const ::rtl::OUString sDigestProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Digest" ) ); + const ::rtl::OUString sEncryptionAlgProperty ( RTL_CONSTASCII_USTRINGPARAM ( "EncryptionAlgorithm" ) ); + const ::rtl::OUString sStartKeyAlgProperty ( RTL_CONSTASCII_USTRINGPARAM ( "StartKeyAlgorithm" ) ); + const ::rtl::OUString sDigestAlgProperty ( RTL_CONSTASCII_USTRINGPARAM ( "DigestAlgorithm" ) ); + const ::rtl::OUString sDerivedKeySizeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "DerivedKeySize" ) ); + + uno::Sequence < PropertyValue > aPropSet (PKG_SIZE_NOENCR_MNFST); OSL_ENSURE( ( rInfo.bFolder && rInfo.pFolder ) || ( !rInfo.bFolder && rInfo.pStream ), "A valid child object is expected!" ); if ( rInfo.bFolder ) { - OUString sTempName = rPath + rShortName + OUString( RTL_CONSTASCII_USTRINGPARAM ( "/" ) ); + ::rtl::OUString sTempName = rPath + rShortName + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "/" ) ); if ( rInfo.pFolder->GetMediaType().getLength() ) { @@ -378,7 +381,7 @@ bool ZipPackageFolder::saveChild( const OUString &rShortName, const ContentInfo sal_Int32 nOwnStreamOrigSize = bRawStream ? rInfo.pStream->GetMagicalHackSize() : rInfo.pStream->getSize(); sal_Bool bUseNonSeekableAccess = sal_False; - Reference < XInputStream > xStream; + uno::Reference < XInputStream > xStream; if ( !rInfo.pStream->IsPackageMember() && !bRawStream && !bToBeEncrypted && bToBeCompressed ) { // the stream is not a package member, not a raw stream, @@ -386,7 +389,7 @@ bool ZipPackageFolder::saveChild( const OUString &rShortName, const ContentInfo // in this case nonseekable access can be used xStream = rInfo.pStream->GetOwnStreamNoWrap(); - Reference < XSeekable > xSeek ( xStream, UNO_QUERY ); + uno::Reference < XSeekable > xSeek ( xStream, uno::UNO_QUERY ); bUseNonSeekableAccess = ( xStream.is() && !xSeek.is() ); } @@ -402,7 +405,7 @@ bool ZipPackageFolder::saveChild( const OUString &rShortName, const ContentInfo return bSuccess; } - Reference < XSeekable > xSeek ( xStream, UNO_QUERY ); + uno::Reference < XSeekable > xSeek ( xStream, uno::UNO_QUERY ); try { if ( xSeek.is() ) @@ -450,15 +453,13 @@ bool ZipPackageFolder::saveChild( const OUString &rShortName, const ContentInfo } else { - OSL_FAIL( "The package component requires that every stream either be FROM a package or it must support XSeekable!" ); bSuccess = false; return bSuccess; } } } - catch ( Exception& ) + catch ( uno::Exception& ) { - OSL_FAIL( "The stream provided to the package component has problems!" ); bSuccess = false; return bSuccess; } @@ -467,9 +468,9 @@ bool ZipPackageFolder::saveChild( const OUString &rShortName, const ContentInfo { if ( bToBeEncrypted && !bTransportOwnEncrStreamAsRaw ) { - Sequence < sal_uInt8 > aSalt ( 16 ), aVector ( 8 ); + uno::Sequence < sal_Int8 > aSalt( 16 ), aVector( rInfo.pStream->GetBlockSize() ); rtl_random_getBytes ( rRandomPool, aSalt.getArray(), 16 ); - rtl_random_getBytes ( rRandomPool, aVector.getArray(), 8 ); + rtl_random_getBytes ( rRandomPool, aVector.getArray(), aVector.getLength() ); sal_Int32 nIterationCount = 1024; if ( !rInfo.pStream->HasOwnKey() ) @@ -498,8 +499,20 @@ bool ZipPackageFolder::saveChild( const OUString &rShortName, const ContentInfo if ( bRawStream || bTransportOwnEncrStreamAsRaw ) { + ::rtl::Reference< EncryptionData > xEncData = rInfo.pStream->GetEncryptionData(); + if ( !xEncData.is() ) + throw uno::RuntimeException(); + aPropSet[PKG_MNFST_DIGEST].Name = sDigestProperty; aPropSet[PKG_MNFST_DIGEST].Value <<= rInfo.pStream->getDigest(); + aPropSet[PKG_MNFST_ENCALG].Name = sEncryptionAlgProperty; + aPropSet[PKG_MNFST_ENCALG].Value <<= xEncData->m_nEncAlg; + aPropSet[PKG_MNFST_STARTALG].Name = sStartKeyAlgProperty; + aPropSet[PKG_MNFST_STARTALG].Value <<= xEncData->m_nStartKeyGenID; + aPropSet[PKG_MNFST_DIGESTALG].Name = sDigestAlgProperty; + aPropSet[PKG_MNFST_DIGESTALG].Value <<= xEncData->m_nCheckAlg; + aPropSet[PKG_MNFST_DERKEYSIZE].Name = sDerivedKeySizeProperty; + aPropSet[PKG_MNFST_DERKEYSIZE].Value <<= xEncData->m_nDerivedKeySize; } } } @@ -520,7 +533,6 @@ bool ZipPackageFolder::saveChild( const OUString &rShortName, const ContentInfo if ( !xStream.is() ) { // Make sure that we actually _got_ a new one ! - OSL_FAIL( "ZipPackageStream didn't have a stream associated with it, skipping!" ); bSuccess = false; return bSuccess; } @@ -531,11 +543,11 @@ bool ZipPackageFolder::saveChild( const OUString &rShortName, const ContentInfo if ( bRawStream ) xStream->skipBytes( rInfo.pStream->GetMagicalHackPos() ); - rZipOut.putNextEntry ( *pTempEntry, rInfo.pStream->getEncryptionData(), sal_False ); + rZipOut.putNextEntry ( *pTempEntry, rInfo.pStream, sal_False ); // the entry is provided to the ZipOutputStream that will delete it pAutoTempEntry.release(); - Sequence < sal_Int8 > aSeq ( n_ConstBufferSize ); + uno::Sequence < sal_Int8 > aSeq ( n_ConstBufferSize ); sal_Int32 nLength; do @@ -549,12 +561,10 @@ bool ZipPackageFolder::saveChild( const OUString &rShortName, const ContentInfo } catch ( ZipException& ) { - OSL_FAIL( "Error writing ZipOutputStream" ); bSuccess = false; } catch ( IOException& ) { - OSL_FAIL( "Error writing ZipOutputStream" ); bSuccess = false; } } @@ -576,7 +586,6 @@ bool ZipPackageFolder::saveChild( const OUString &rShortName, const ContentInfo if ( !xStream.is() ) { // Make sure that we actually _got_ a new one ! - OSL_FAIL( "ZipPackageStream didn't have a stream associated with it, skipping!" ); bSuccess = false; return bSuccess; } @@ -590,12 +599,12 @@ bool ZipPackageFolder::saveChild( const OUString &rShortName, const ContentInfo try { - rZipOut.putNextEntry ( *pTempEntry, rInfo.pStream->getEncryptionData(), bToBeEncrypted); + rZipOut.putNextEntry ( *pTempEntry, rInfo.pStream, bToBeEncrypted); // the entry is provided to the ZipOutputStream that will delete it pAutoTempEntry.release(); sal_Int32 nLength; - Sequence < sal_Int8 > aSeq (n_ConstBufferSize); + uno::Sequence < sal_Int8 > aSeq (n_ConstBufferSize); do { nLength = xStream->readBytes(aSeq, n_ConstBufferSize); @@ -607,19 +616,30 @@ bool ZipPackageFolder::saveChild( const OUString &rShortName, const ContentInfo } catch ( ZipException& ) { - OSL_FAIL( "Error writing ZipOutputStream" ); bSuccess = false; } catch ( IOException& ) { - OSL_FAIL( "Error writing ZipOutputStream" ); bSuccess = false; } if ( bToBeEncrypted ) { + ::rtl::Reference< EncryptionData > xEncData = rInfo.pStream->GetEncryptionData(); + if ( !xEncData.is() ) + throw uno::RuntimeException(); + aPropSet[PKG_MNFST_DIGEST].Name = sDigestProperty; aPropSet[PKG_MNFST_DIGEST].Value <<= rInfo.pStream->getDigest(); + aPropSet[PKG_MNFST_ENCALG].Name = sEncryptionAlgProperty; + aPropSet[PKG_MNFST_ENCALG].Value <<= xEncData->m_nEncAlg; + aPropSet[PKG_MNFST_STARTALG].Name = sStartKeyAlgProperty; + aPropSet[PKG_MNFST_STARTALG].Value <<= xEncData->m_nStartKeyGenID; + aPropSet[PKG_MNFST_DIGESTALG].Name = sDigestAlgProperty; + aPropSet[PKG_MNFST_DIGESTALG].Value <<= xEncData->m_nCheckAlg; + aPropSet[PKG_MNFST_DERKEYSIZE].Name = sDerivedKeySizeProperty; + aPropSet[PKG_MNFST_DERKEYSIZE].Value <<= xEncData->m_nDerivedKeySize; + rInfo.pStream->SetIsEncrypted ( sal_True ); } } @@ -665,8 +685,8 @@ bool ZipPackageFolder::saveChild( const OUString &rShortName, const ContentInfo return bSuccess; } -void ZipPackageFolder::saveContents(OUString &rPath, std::vector < Sequence < PropertyValue > > &rManList, ZipOutputStream & rZipOut, Sequence < sal_Int8 > &rEncryptionKey, rtlRandomPool &rRandomPool) - throw(RuntimeException) +void ZipPackageFolder::saveContents( ::rtl::OUString &rPath, std::vector < uno::Sequence < PropertyValue > > &rManList, ZipOutputStream & rZipOut, const uno::Sequence < sal_Int8 >& rEncryptionKey, rtlRandomPool &rRandomPool ) + throw( uno::RuntimeException ) { bool bWritingFailed = false; @@ -681,18 +701,15 @@ void ZipPackageFolder::saveContents(OUString &rPath, std::vector < Sequence < Pr try { - rtl::Reference < EncryptionData > aEmptyEncr; - rZipOut.putNextEntry ( *pTempEntry, aEmptyEncr, sal_False ); + rZipOut.putNextEntry( *pTempEntry, NULL, sal_False ); rZipOut.rawCloseEntry(); } catch ( ZipException& ) { - OSL_FAIL( "Error writing ZipOutputStream" ); bWritingFailed = true; } catch ( IOException& ) { - OSL_FAIL( "Error writing ZipOutputStream" ); bWritingFailed = true; } } @@ -713,7 +730,7 @@ void ZipPackageFolder::saveContents(OUString &rPath, std::vector < Sequence < Pr for ( ContentHash::const_iterator aCI = maContents.begin(), aEnd = maContents.end(); aCI != aEnd; ++aCI) { - const OUString &rShortName = (*aCI).first; + const ::rtl::OUString &rShortName = (*aCI).first; const ContentInfo &rInfo = *(*aCI).second; if ( !bMimeTypeStreamStored || !rShortName.equals( aMimeTypeStreamName ) ) @@ -721,7 +738,7 @@ void ZipPackageFolder::saveContents(OUString &rPath, std::vector < Sequence < Pr } if( bWritingFailed ) - throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); + throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); } void ZipPackageFolder::releaseUpwardRef( void ) @@ -753,8 +770,8 @@ void ZipPackageFolder::releaseUpwardRef( void ) #endif } -sal_Int64 SAL_CALL ZipPackageFolder::getSomething( const Sequence< sal_Int8 >& aIdentifier ) - throw(RuntimeException) +sal_Int64 SAL_CALL ZipPackageFolder::getSomething( const uno::Sequence< sal_Int8 >& aIdentifier ) + throw(uno::RuntimeException) { sal_Int64 nMe = 0; if ( aIdentifier.getLength() == 16 && @@ -762,8 +779,8 @@ sal_Int64 SAL_CALL ZipPackageFolder::getSomething( const Sequence< sal_Int8 >& a nMe = reinterpret_cast < sal_Int64 > ( this ); return nMe; } -void SAL_CALL ZipPackageFolder::setPropertyValue( const OUString& aPropertyName, const Any& aValue ) - throw(UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException) +void SAL_CALL ZipPackageFolder::setPropertyValue( const ::rtl::OUString& aPropertyName, const uno::Any& aValue ) + throw(UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, uno::RuntimeException) { if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("MediaType"))) { @@ -780,8 +797,8 @@ void SAL_CALL ZipPackageFolder::setPropertyValue( const OUString& aPropertyName, else throw UnknownPropertyException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); } -Any SAL_CALL ZipPackageFolder::getPropertyValue( const OUString& PropertyName ) - throw(UnknownPropertyException, WrappedTargetException, RuntimeException) +uno::Any SAL_CALL ZipPackageFolder::getPropertyValue( const ::rtl::OUString& PropertyName ) + throw(UnknownPropertyException, WrappedTargetException, uno::RuntimeException) { if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "MediaType" ) ) ) { @@ -789,18 +806,18 @@ Any SAL_CALL ZipPackageFolder::getPropertyValue( const OUString& PropertyName ) // if ( m_nFormat != embed::StorageFormats::PACKAGE ) // throw UnknownPropertyException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); - return makeAny ( sMediaType ); + return uno::makeAny ( sMediaType ); } else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "Version" ) ) ) - return makeAny( m_sVersion ); + return uno::makeAny( m_sVersion ); else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "Size" ) ) ) - return makeAny ( aEntry.nSize ); + return uno::makeAny ( aEntry.nSize ); else throw UnknownPropertyException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); } void ZipPackageFolder::doInsertByName ( ZipPackageEntry *pEntry, sal_Bool bSetParent ) - throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException) + throw(IllegalArgumentException, ElementExistException, WrappedTargetException, uno::RuntimeException) { try { @@ -817,21 +834,21 @@ void ZipPackageFolder::doInsertByName ( ZipPackageEntry *pEntry, sal_Bool bSetPa if ( bSetParent ) pEntry->setParent ( *this ); } -OUString ZipPackageFolder::getImplementationName() - throw (RuntimeException) +::rtl::OUString ZipPackageFolder::getImplementationName() + throw (uno::RuntimeException) { - return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "ZipPackageFolder" ) ); + return ::rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( "ZipPackageFolder" ) ); } -Sequence< OUString > ZipPackageFolder::getSupportedServiceNames() - throw (RuntimeException) +uno::Sequence< ::rtl::OUString > ZipPackageFolder::getSupportedServiceNames() + throw (uno::RuntimeException) { - Sequence< OUString > aNames(1); - aNames[0] = OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.packages.PackageFolder" ) ); + uno::Sequence< ::rtl::OUString > aNames(1); + aNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.packages.PackageFolder" ) ); return aNames; } -sal_Bool SAL_CALL ZipPackageFolder::supportsService( OUString const & rServiceName ) - throw (RuntimeException) +sal_Bool SAL_CALL ZipPackageFolder::supportsService( ::rtl::OUString const & rServiceName ) + throw (uno::RuntimeException) { return rServiceName == getSupportedServiceNames()[0]; } diff --git a/package/source/zippackage/ZipPackageStream.cxx b/package/source/zippackage/ZipPackageStream.cxx index 1287067a9f4d..d63560440ccb 100644 --- a/package/source/zippackage/ZipPackageStream.cxx +++ b/package/source/zippackage/ZipPackageStream.cxx @@ -17,7 +17,7 @@ * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License version 3 for more details - * (a copy is included in the LICENSE file that accompanied this code). + * ( a copy is included in the LICENSE file that accompanied this code ). * * You should have received a copy of the GNU Lesser General Public License * version 3 along with OpenOffice.org. If not, see @@ -26,7 +26,7 @@ * ************************************************************************/ -// MARKER(update_precomp.py): autogen include statement, do not remove +// MARKER( update_precomp.py ): autogen include statement, do not remove #include "precompiled_package.hxx" #include <com/sun/star/packages/zip/ZipConstants.hpp> #include <com/sun/star/embed/StorageFormats.hpp> @@ -35,6 +35,8 @@ #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/xml/crypto/DigestID.hpp> +#include <com/sun/star/xml/crypto/CipherID.hpp> #include <ZipPackageStream.hxx> @@ -67,21 +69,25 @@ const ::com::sun::star::uno::Sequence < sal_Int8 >& ZipPackageStream::static_get } ZipPackageStream::ZipPackageStream ( ZipPackage & rNewPackage, - const Reference< XMultiServiceFactory >& xFactory, + const uno::Reference< XMultiServiceFactory >& xFactory, sal_Bool bAllowRemoveOnInsert ) : m_xFactory( xFactory ) -, rZipPackage(rNewPackage) +, rZipPackage( rNewPackage ) , bToBeCompressed ( sal_True ) , bToBeEncrypted ( sal_False ) , bHaveOwnKey ( sal_False ) , bIsEncrypted ( sal_False ) -, xEncryptionData ( ) +, m_nImportedStartKeyAlgorithm( 0 ) +, m_nImportedEncryptionAlgorithm( 0 ) +, m_nImportedChecksumAlgorithm( 0 ) +, m_nImportedDerivedKeySize( 0 ) , m_nStreamMode( PACKAGE_STREAM_NOTSET ) , m_nMagicalHackPos( 0 ) , m_nMagicalHackSize( 0 ) , m_bHasSeekable( sal_False ) , m_bCompressedIsSetFromOutside( sal_False ) , m_bFromManifest( sal_False ) +, m_bUseWinEncoding( false ) { OSL_ENSURE( m_xFactory.is(), "No factory is provided to ZipPackageStream!\n" ); @@ -108,7 +114,7 @@ ZipPackageStream::~ZipPackageStream( void ) { } -void ZipPackageStream::setZipEntryOnLoading( const ZipEntry &rInEntry) +void ZipPackageStream::setZipEntryOnLoading( const ZipEntry &rInEntry ) { aEntry.nVersion = rInEntry.nVersion; aEntry.nFlag = rInEntry.nFlag; @@ -138,7 +144,7 @@ void ZipPackageStream::CloseOwnStreamIfAny() } //-------------------------------------------------------------------------- -uno::Reference< io::XInputStream >& ZipPackageStream::GetOwnSeekStream() +uno::Reference< io::XInputStream > ZipPackageStream::GetOwnSeekStream() { if ( !m_bHasSeekable && xStream.is() ) { @@ -147,10 +153,10 @@ uno::Reference< io::XInputStream >& ZipPackageStream::GetOwnSeekStream() // is accessed before commit it MUST be wrapped. // Wrap the stream in case it is not seekable xStream = ::comphelper::OSeekableInputWrapper::CheckSeekableCanWrap( xStream, m_xFactory ); - Reference< io::XSeekable > xSeek( xStream, UNO_QUERY ); + uno::Reference< io::XSeekable > xSeek( xStream, UNO_QUERY ); if ( !xSeek.is() ) throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "The stream must support XSeekable!" ) ), - Reference< XInterface >() ); + uno::Reference< XInterface >() ); m_bHasSeekable = sal_True; } @@ -164,18 +170,18 @@ uno::Reference< io::XInputStream > ZipPackageStream::GetRawEncrStreamNoHeaderCop if ( m_nStreamMode != PACKAGE_STREAM_RAW || !GetOwnSeekStream().is() ) throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); - if ( !xEncryptionData.is() ) + if ( m_xBaseEncryptionData.is() ) throw ZipIOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Encrypted stream without encryption data!\n" ) ), - Reference< XInterface >() ); + uno::Reference< XInterface >() ); uno::Reference< io::XSeekable > xSeek( GetOwnSeekStream(), UNO_QUERY ); if ( !xSeek.is() ) throw ZipIOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "The stream must be seekable!\n" ) ), - Reference< XInterface >() ); + uno::Reference< XInterface >() ); // skip header - xSeek->seek( n_ConstHeaderSize + xEncryptionData->aInitVector.getLength() + - xEncryptionData->aSalt.getLength() + xEncryptionData->aDigest.getLength() ); + xSeek->seek( n_ConstHeaderSize + getInitialisationVector().getLength() + + getSalt().getLength() + getDigest().getLength() ); // create temporary stream uno::Reference < io::XOutputStream > xTempOut( @@ -195,17 +201,95 @@ uno::Reference< io::XInputStream > ZipPackageStream::GetRawEncrStreamNoHeaderCop } //-------------------------------------------------------------------------- -Reference< io::XInputStream > ZipPackageStream::TryToGetRawFromDataStream( sal_Bool bAddHeaderForEncr ) +sal_Int32 ZipPackageStream::GetEncryptionAlgorithm() const { - if ( m_nStreamMode != PACKAGE_STREAM_DATA || !GetOwnSeekStream().is() || (bAddHeaderForEncr && !bToBeEncrypted) ) + return m_nImportedEncryptionAlgorithm ? m_nImportedEncryptionAlgorithm : rZipPackage.GetEncAlgID(); +} + +//-------------------------------------------------------------------------- +sal_Int32 ZipPackageStream::GetBlockSize() const +{ + return GetEncryptionAlgorithm() == ::com::sun::star::xml::crypto::CipherID::AES_CBC_W3C_PADDING ? 16 : 8; +} + +//-------------------------------------------------------------------------- +::rtl::Reference< EncryptionData > ZipPackageStream::GetEncryptionData( bool bUseWinEncoding ) +{ + ::rtl::Reference< EncryptionData > xResult; + if ( m_xBaseEncryptionData.is() ) + xResult = new EncryptionData( + *m_xBaseEncryptionData, + GetEncryptionKey( bUseWinEncoding ), + GetEncryptionAlgorithm(), + m_nImportedChecksumAlgorithm ? m_nImportedChecksumAlgorithm : rZipPackage.GetChecksumAlgID(), + m_nImportedDerivedKeySize ? m_nImportedDerivedKeySize : rZipPackage.GetDefaultDerivedKeySize(), + GetStartKeyGenID() ); + + return xResult; +} + +//-------------------------------------------------------------------------- +void ZipPackageStream::SetBaseEncryptionData( const ::rtl::Reference< BaseEncryptionData >& xData ) +{ + m_xBaseEncryptionData = xData; +} + +//-------------------------------------------------------------------------- +uno::Sequence< sal_Int8 > ZipPackageStream::GetEncryptionKey( bool bUseWinEncoding ) +{ + uno::Sequence< sal_Int8 > aResult; + sal_Int32 nKeyGenID = GetStartKeyGenID(); + bUseWinEncoding = ( bUseWinEncoding || m_bUseWinEncoding ); + + if ( bHaveOwnKey && m_aStorageEncryptionKeys.getLength() ) + { + ::rtl::OUString aNameToFind; + if ( nKeyGenID == xml::crypto::DigestID::SHA256 ) + aNameToFind = PACKAGE_ENCRYPTIONDATA_SHA256UTF8; + else if ( nKeyGenID == xml::crypto::DigestID::SHA1 ) + { + aNameToFind = bUseWinEncoding ? PACKAGE_ENCRYPTIONDATA_SHA1MS1252 : PACKAGE_ENCRYPTIONDATA_SHA1UTF8; + } + else + throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "No expected key is provided!" ) ), uno::Reference< uno::XInterface >() ); + + for ( sal_Int32 nInd = 0; nInd < m_aStorageEncryptionKeys.getLength(); nInd++ ) + if ( m_aStorageEncryptionKeys[nInd].Name.equals( aNameToFind ) ) + m_aStorageEncryptionKeys[nInd].Value >>= aResult; + + // empty keys are not allowed here + // so it is not important whether there is no key, or the key is empty, it is an error + if ( !aResult.getLength() ) + throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "No expected key is provided!" ) ), uno::Reference< uno::XInterface >() ); + } + else + aResult = m_aEncryptionKey; + + if ( !aResult.getLength() || !bHaveOwnKey ) + aResult = rZipPackage.GetEncryptionKey(); + + return aResult; +} + +//-------------------------------------------------------------------------- +sal_Int32 ZipPackageStream::GetStartKeyGenID() +{ + // generally should all the streams use the same Start Key + // but if raw copy without password takes place, we should preserve the imported algorithm + return m_nImportedStartKeyAlgorithm ? m_nImportedStartKeyAlgorithm : rZipPackage.GetStartKeyGenID(); +} + +//-------------------------------------------------------------------------- +uno::Reference< io::XInputStream > ZipPackageStream::TryToGetRawFromDataStream( sal_Bool bAddHeaderForEncr ) +{ + if ( m_nStreamMode != PACKAGE_STREAM_DATA || !GetOwnSeekStream().is() || ( bAddHeaderForEncr && !bToBeEncrypted ) ) throw packages::NoEncryptionException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); Sequence< sal_Int8 > aKey; if ( bToBeEncrypted ) { - aKey = ( !xEncryptionData.is() || !bHaveOwnKey ) ? rZipPackage.getEncryptionKey() : - xEncryptionData->aKey; + aKey = GetEncryptionKey(); if ( !aKey.getLength() ) throw packages::NoEncryptionException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); } @@ -221,7 +305,7 @@ Reference< io::XInputStream > ZipPackageStream::TryToGetRawFromDataStream( sal_B // create a package based on it ZipPackage* pPackage = new ZipPackage( m_xFactory ); - Reference< XSingleServiceFactory > xPackageAsFactory( static_cast< XSingleServiceFactory* >( pPackage ) ); + uno::Reference< XSingleServiceFactory > xPackageAsFactory( static_cast< XSingleServiceFactory* >( pPackage ) ); if ( !xPackageAsFactory.is() ) throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); @@ -230,14 +314,14 @@ Reference< io::XInputStream > ZipPackageStream::TryToGetRawFromDataStream( sal_B pPackage->initialize( aArgs ); // create a new package stream - Reference< XDataSinkEncrSupport > xNewPackStream( xPackageAsFactory->createInstance(), UNO_QUERY ); + uno::Reference< XDataSinkEncrSupport > xNewPackStream( xPackageAsFactory->createInstance(), UNO_QUERY ); if ( !xNewPackStream.is() ) throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); xNewPackStream->setDataStream( static_cast< io::XInputStream* >( new WrapStreamForShare( GetOwnSeekStream(), rZipPackage.GetSharedMutexRef() ) ) ); - Reference< XPropertySet > xNewPSProps( xNewPackStream, UNO_QUERY ); + uno::Reference< XPropertySet > xNewPSProps( xNewPackStream, UNO_QUERY ); if ( !xNewPSProps.is() ) throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); @@ -246,26 +330,26 @@ Reference< io::XInputStream > ZipPackageStream::TryToGetRawFromDataStream( sal_B xNewPSProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Compressed" ) ), makeAny( bToBeCompressed ) ); if ( bToBeEncrypted ) { - xNewPSProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "EncryptionKey" ) ), makeAny( aKey ) ); + xNewPSProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ENCRYPTION_KEY_PROPERTY ) ), makeAny( aKey ) ); xNewPSProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Encrypted" ) ), makeAny( sal_True ) ); } // insert a new stream in the package - Reference< XUnoTunnel > xTunnel; + uno::Reference< XUnoTunnel > xTunnel; Any aRoot = pPackage->getByHierarchicalName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) ) ); aRoot >>= xTunnel; - Reference< container::XNameContainer > xRootNameContainer( xTunnel, UNO_QUERY ); + uno::Reference< container::XNameContainer > xRootNameContainer( xTunnel, UNO_QUERY ); if ( !xRootNameContainer.is() ) throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); - Reference< XUnoTunnel > xNPSTunnel( xNewPackStream, UNO_QUERY ); + uno::Reference< XUnoTunnel > xNPSTunnel( xNewPackStream, UNO_QUERY ); xRootNameContainer->insertByName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "dummy" ) ), makeAny( xNPSTunnel ) ); // commit the temporary package pPackage->commitChanges(); // get raw stream from the temporary package - Reference< io::XInputStream > xInRaw; + uno::Reference< io::XInputStream > xInRaw; if ( bAddHeaderForEncr ) xInRaw = xNewPackStream->getRawStream(); else @@ -286,12 +370,12 @@ Reference< io::XInputStream > ZipPackageStream::TryToGetRawFromDataStream( sal_B xTempSeek->seek( 0 ); // close raw stream, package stream and folder - xInRaw = Reference< io::XInputStream >(); - xNewPSProps = Reference< XPropertySet >(); - xNPSTunnel = Reference< XUnoTunnel >(); - xNewPackStream = Reference< XDataSinkEncrSupport >(); - xTunnel = Reference< XUnoTunnel >(); - xRootNameContainer = Reference< container::XNameContainer >(); + xInRaw = uno::Reference< io::XInputStream >(); + xNewPSProps = uno::Reference< XPropertySet >(); + xNPSTunnel = uno::Reference< XUnoTunnel >(); + xNewPackStream = uno::Reference< XDataSinkEncrSupport >(); + xTunnel = uno::Reference< XUnoTunnel >(); + xRootNameContainer = uno::Reference< container::XNameContainer >(); // return the stream representing the first temporary file return xTempIn; @@ -317,7 +401,7 @@ sal_Bool ZipPackageStream::ParsePackageRawStream() sal_Bool bOk = sal_False; - rtl::Reference < EncryptionData > xTempEncrData; + ::rtl::Reference< BaseEncryptionData > xTempEncrData; sal_Int32 nMagHackSize = 0; Sequence < sal_Int8 > aHeader ( 4 ); @@ -333,17 +417,25 @@ sal_Bool ZipPackageStream::ParsePackageRawStream() if ( nHeader == n_ConstHeader ) { // this is one of our god-awful, but extremely devious hacks, everyone cheer - xTempEncrData = new EncryptionData; + xTempEncrData = new BaseEncryptionData; ::rtl::OUString aMediaType; - if ( ZipFile::StaticFillData ( xTempEncrData, nMagHackSize, aMediaType, GetOwnSeekStream() ) ) + sal_Int32 nEncAlgorithm = 0; + sal_Int32 nChecksumAlgorithm = 0; + sal_Int32 nDerivedKeySize = 0; + sal_Int32 nStartKeyGenID = 0; + if ( ZipFile::StaticFillData( xTempEncrData, nEncAlgorithm, nChecksumAlgorithm, nDerivedKeySize, nStartKeyGenID, nMagHackSize, aMediaType, GetOwnSeekStream() ) ) { // We'll want to skip the data we've just read, so calculate how much we just read // and remember it - m_nMagicalHackPos = n_ConstHeaderSize + xTempEncrData->aSalt.getLength() - + xTempEncrData->aInitVector.getLength() - + xTempEncrData->aDigest.getLength() + m_nMagicalHackPos = n_ConstHeaderSize + xTempEncrData->m_aSalt.getLength() + + xTempEncrData->m_aInitVector.getLength() + + xTempEncrData->m_aDigest.getLength() + aMediaType.getLength() * sizeof( sal_Unicode ); + m_nImportedEncryptionAlgorithm = nEncAlgorithm; + m_nImportedChecksumAlgorithm = nChecksumAlgorithm; + m_nImportedDerivedKeySize = nDerivedKeySize; + m_nImportedStartKeyAlgorithm = nStartKeyGenID; m_nMagicalHackSize = nMagHackSize; sMediaType = aMediaType; @@ -362,7 +454,7 @@ sal_Bool ZipPackageStream::ParsePackageRawStream() return sal_False; } - xEncryptionData = xTempEncrData; + m_xBaseEncryptionData = xTempEncrData; SetIsEncrypted ( sal_True ); // it's already compressed and encrypted bToBeEncrypted = bToBeCompressed = sal_False; @@ -384,11 +476,12 @@ void ZipPackageStream::SetPackageMember( sal_Bool bNewValue ) // XActiveDataSink //-------------------------------------------------------------------------- -void SAL_CALL ZipPackageStream::setInputStream( const Reference< io::XInputStream >& aStream ) - throw(RuntimeException) +void SAL_CALL ZipPackageStream::setInputStream( const uno::Reference< io::XInputStream >& aStream ) + throw( RuntimeException ) { // if seekable access is required the wrapping will be done on demand xStream = aStream; + m_nImportedEncryptionAlgorithm = 0; m_bHasSeekable = sal_False; SetPackageMember ( sal_False ); aEntry.nTime = -1; @@ -396,94 +489,100 @@ void SAL_CALL ZipPackageStream::setInputStream( const Reference< io::XInputStrea } //-------------------------------------------------------------------------- -Reference< io::XInputStream > SAL_CALL ZipPackageStream::getRawData() - throw(RuntimeException) +uno::Reference< io::XInputStream > SAL_CALL ZipPackageStream::getRawData() + throw( RuntimeException ) { try { - if (IsPackageMember()) + if ( IsPackageMember() ) { - if ( xEncryptionData.is() && !bHaveOwnKey ) - xEncryptionData->aKey = rZipPackage.getEncryptionKey(); - return rZipPackage.getZipFile().getRawData( aEntry, xEncryptionData, bIsEncrypted, rZipPackage.GetSharedMutexRef() ); + return rZipPackage.getZipFile().getRawData( aEntry, GetEncryptionData(), bIsEncrypted, rZipPackage.GetSharedMutexRef() ); } else if ( GetOwnSeekStream().is() ) { return new WrapStreamForShare( GetOwnSeekStream(), rZipPackage.GetSharedMutexRef() ); } else - return Reference < io::XInputStream > (); + return uno::Reference < io::XInputStream > (); } - catch (ZipException &)//rException) + catch ( ZipException & )//rException ) { - OSL_FAIL( "ZipException thrown");//rException.Message); - return Reference < io::XInputStream > (); + OSL_FAIL( "ZipException thrown" );//rException.Message); + return uno::Reference < io::XInputStream > (); } - catch (Exception &) + catch ( Exception & ) { - OSL_FAIL( "Exception is thrown during stream wrapping!\n"); - return Reference < io::XInputStream > (); + OSL_FAIL( "Exception is thrown during stream wrapping!\n" ); + return uno::Reference < io::XInputStream > (); } } //-------------------------------------------------------------------------- -Reference< io::XInputStream > SAL_CALL ZipPackageStream::getInputStream( ) - throw(RuntimeException) +uno::Reference< io::XInputStream > SAL_CALL ZipPackageStream::getInputStream() + throw( RuntimeException ) { try { - if (IsPackageMember()) + if ( IsPackageMember() ) { - if ( xEncryptionData.is() && !bHaveOwnKey ) - xEncryptionData->aKey = rZipPackage.getEncryptionKey(); - return rZipPackage.getZipFile().getInputStream( aEntry, xEncryptionData, bIsEncrypted, rZipPackage.GetSharedMutexRef() ); + return rZipPackage.getZipFile().getInputStream( aEntry, GetEncryptionData(), bIsEncrypted, rZipPackage.GetSharedMutexRef() ); } else if ( GetOwnSeekStream().is() ) { return new WrapStreamForShare( GetOwnSeekStream(), rZipPackage.GetSharedMutexRef() ); } else - return Reference < io::XInputStream > (); + return uno::Reference < io::XInputStream > (); } - catch (ZipException &)//rException) + catch ( ZipException & )//rException ) { - OSL_FAIL( "ZipException thrown");//rException.Message); - return Reference < io::XInputStream > (); + OSL_FAIL( "ZipException thrown" );//rException.Message); + return uno::Reference < io::XInputStream > (); } - catch (Exception &) + catch ( Exception & ) { - OSL_FAIL( "Exception is thrown during stream wrapping!\n"); - return Reference < io::XInputStream > (); + OSL_FAIL( "Exception is thrown during stream wrapping!\n" ); + return uno::Reference < io::XInputStream > (); } } // XDataSinkEncrSupport //-------------------------------------------------------------------------- -Reference< io::XInputStream > SAL_CALL ZipPackageStream::getDataStream() +uno::Reference< io::XInputStream > SAL_CALL ZipPackageStream::getDataStream() throw ( packages::WrongPasswordException, io::IOException, RuntimeException ) { // There is no stream attached to this object if ( m_nStreamMode == PACKAGE_STREAM_NOTSET ) - return Reference< io::XInputStream >(); + return uno::Reference< io::XInputStream >(); // this method can not be used together with old approach if ( m_nStreamMode == PACKAGE_STREAM_DETECT ) throw packages::zip::ZipIOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); - if ( xEncryptionData.is() && !bHaveOwnKey ) - xEncryptionData->aKey = rZipPackage.getEncryptionKey(); - - if (IsPackageMember()) + if ( IsPackageMember() ) { - if ( xEncryptionData.is() && !bHaveOwnKey ) - xEncryptionData->aKey = rZipPackage.getEncryptionKey(); - - return rZipPackage.getZipFile().getDataStream( aEntry, xEncryptionData, bIsEncrypted, rZipPackage.GetSharedMutexRef() ); + uno::Reference< io::XInputStream > xResult; + try + { + xResult = rZipPackage.getZipFile().getDataStream( aEntry, GetEncryptionData(), bIsEncrypted, rZipPackage.GetSharedMutexRef() ); + } + catch( packages::WrongPasswordException& ) + { + // workaround for the encrypted documents generated with the old OOo1.x bug. + if ( rZipPackage.GetStartKeyGenID() == xml::crypto::DigestID::SHA1 && !m_bUseWinEncoding ) + { + xResult = rZipPackage.getZipFile().getDataStream( aEntry, GetEncryptionData( true ), bIsEncrypted, rZipPackage.GetSharedMutexRef() ); + m_bUseWinEncoding = true; + } + else + throw; + } + return xResult; } else if ( m_nStreamMode == PACKAGE_STREAM_RAW ) - return ZipFile::StaticGetDataFromRawStream( GetOwnSeekStream(), xEncryptionData ); + return ZipFile::StaticGetDataFromRawStream( m_xFactory, GetOwnSeekStream(), GetEncryptionData() ); else if ( GetOwnSeekStream().is() ) { return new WrapStreamForShare( GetOwnSeekStream(), rZipPackage.GetSharedMutexRef() ); @@ -493,25 +592,25 @@ Reference< io::XInputStream > SAL_CALL ZipPackageStream::getDataStream() } //-------------------------------------------------------------------------- -Reference< io::XInputStream > SAL_CALL ZipPackageStream::getRawStream() +uno::Reference< io::XInputStream > SAL_CALL ZipPackageStream::getRawStream() throw ( packages::NoEncryptionException, io::IOException, uno::RuntimeException ) { // There is no stream attached to this object if ( m_nStreamMode == PACKAGE_STREAM_NOTSET ) - return Reference< io::XInputStream >(); + return uno::Reference< io::XInputStream >(); // this method can not be used together with old approach if ( m_nStreamMode == PACKAGE_STREAM_DETECT ) throw packages::zip::ZipIOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); - if (IsPackageMember()) + if ( IsPackageMember() ) { - if ( !bIsEncrypted || !xEncryptionData.is() ) + if ( !bIsEncrypted || !GetEncryptionData().is() ) throw packages::NoEncryptionException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); - return rZipPackage.getZipFile().getWrappedRawStream( aEntry, xEncryptionData, sMediaType, rZipPackage.GetSharedMutexRef() ); + return rZipPackage.getZipFile().getWrappedRawStream( aEntry, GetEncryptionData(), sMediaType, rZipPackage.GetSharedMutexRef() ); } else if ( GetOwnSeekStream().is() ) { @@ -528,7 +627,7 @@ Reference< io::XInputStream > SAL_CALL ZipPackageStream::getRawStream() //-------------------------------------------------------------------------- -void SAL_CALL ZipPackageStream::setDataStream( const Reference< io::XInputStream >& aStream ) +void SAL_CALL ZipPackageStream::setDataStream( const uno::Reference< io::XInputStream >& aStream ) throw ( io::IOException, RuntimeException ) { @@ -537,21 +636,21 @@ void SAL_CALL ZipPackageStream::setDataStream( const Reference< io::XInputStream } //-------------------------------------------------------------------------- -void SAL_CALL ZipPackageStream::setRawStream( const Reference< io::XInputStream >& aStream ) +void SAL_CALL ZipPackageStream::setRawStream( const uno::Reference< io::XInputStream >& aStream ) throw ( packages::EncryptionNotAllowedException, packages::NoRawFormatException, io::IOException, - RuntimeException) + RuntimeException ) { // wrap the stream in case it is not seekable - Reference< io::XInputStream > xNewStream = ::comphelper::OSeekableInputWrapper::CheckSeekableCanWrap( aStream, m_xFactory ); - Reference< io::XSeekable > xSeek( xNewStream, UNO_QUERY ); + uno::Reference< io::XInputStream > xNewStream = ::comphelper::OSeekableInputWrapper::CheckSeekableCanWrap( aStream, m_xFactory ); + uno::Reference< io::XSeekable > xSeek( xNewStream, UNO_QUERY ); if ( !xSeek.is() ) throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "The stream must support XSeekable!" ) ), - Reference< XInterface >() ); + uno::Reference< XInterface >() ); xSeek->seek( 0 ); - Reference< io::XInputStream > xOldStream = xStream; + uno::Reference< io::XInputStream > xOldStream = xStream; xStream = xNewStream; if ( !ParsePackageRawStream() ) { @@ -574,15 +673,15 @@ uno::Reference< io::XInputStream > SAL_CALL ZipPackageStream::getPlainRawStream( { // There is no stream attached to this object if ( m_nStreamMode == PACKAGE_STREAM_NOTSET ) - return Reference< io::XInputStream >(); + return uno::Reference< io::XInputStream >(); // this method can not be used together with old approach if ( m_nStreamMode == PACKAGE_STREAM_DETECT ) throw packages::zip::ZipIOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); - if (IsPackageMember()) + if ( IsPackageMember() ) { - return rZipPackage.getZipFile().getRawData( aEntry, xEncryptionData, bIsEncrypted, rZipPackage.GetSharedMutexRef() ); + return rZipPackage.getZipFile().getRawData( aEntry, GetEncryptionData(), bIsEncrypted, rZipPackage.GetSharedMutexRef() ); } else if ( GetOwnSeekStream().is() ) { @@ -595,14 +694,14 @@ uno::Reference< io::XInputStream > SAL_CALL ZipPackageStream::getPlainRawStream( return TryToGetRawFromDataStream( sal_False ); } - return Reference< io::XInputStream >(); + return uno::Reference< io::XInputStream >(); } // XUnoTunnel //-------------------------------------------------------------------------- sal_Int64 SAL_CALL ZipPackageStream::getSomething( const Sequence< sal_Int8 >& aIdentifier ) - throw(RuntimeException) + throw( RuntimeException ) { sal_Int64 nMe = 0; if ( aIdentifier.getLength() == 16 && @@ -614,18 +713,18 @@ sal_Int64 SAL_CALL ZipPackageStream::getSomething( const Sequence< sal_Int8 >& a // XPropertySet //-------------------------------------------------------------------------- void SAL_CALL ZipPackageStream::setPropertyValue( const OUString& aPropertyName, const Any& aValue ) - throw(beans::UnknownPropertyException, beans::PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException) + throw( beans::UnknownPropertyException, beans::PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException ) { - if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("MediaType"))) + if ( aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "MediaType" )) ) { if ( rZipPackage.getFormat() != embed::StorageFormats::PACKAGE && rZipPackage.getFormat() != embed::StorageFormats::OFOPXML ) throw beans::PropertyVetoException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); if ( aValue >>= sMediaType ) { - if (sMediaType.getLength() > 0) + if ( sMediaType.getLength() > 0 ) { - if ( sMediaType.indexOf (OUString( RTL_CONSTASCII_USTRINGPARAM ( "text" ) ) ) != -1 + if ( sMediaType.indexOf ( OUString( RTL_CONSTASCII_USTRINGPARAM ( "text" ) ) ) != -1 || sMediaType.equals( OUString( RTL_CONSTASCII_USTRINGPARAM ( "application/vnd.sun.star.oleobject" ) ) ) ) bToBeCompressed = sal_True; else if ( !m_bCompressedIsSetFromOutside ) @@ -634,18 +733,18 @@ void SAL_CALL ZipPackageStream::setPropertyValue( const OUString& aPropertyName, } else throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "MediaType must be a string!\n" ) ), - Reference< XInterface >(), + uno::Reference< XInterface >(), 2 ); } - else if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Size") ) ) + else if ( aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Size" ) ) ) { if ( !( aValue >>= aEntry.nSize ) ) throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Wrong type for Size property!\n" ) ), - Reference< XInterface >(), + uno::Reference< XInterface >(), 2 ); } - else if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Encrypted") ) ) + else if ( aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Encrypted" ) ) ) { if ( rZipPackage.getFormat() != embed::StorageFormats::PACKAGE ) throw beans::PropertyVetoException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); @@ -656,25 +755,25 @@ void SAL_CALL ZipPackageStream::setPropertyValue( const OUString& aPropertyName, // In case of new raw stream, the stream must not be encrypted on storing if ( bEnc && m_nStreamMode == PACKAGE_STREAM_RAW ) throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Raw stream can not be encrypted on storing" ) ), - Reference< XInterface >(), + uno::Reference< XInterface >(), 2 ); bToBeEncrypted = bEnc; - if ( bToBeEncrypted && !xEncryptionData.is()) - xEncryptionData = new EncryptionData; + if ( bToBeEncrypted && !m_xBaseEncryptionData.is() ) + m_xBaseEncryptionData = new BaseEncryptionData; } else throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Wrong type for Encrypted property!\n" ) ), - Reference< XInterface >(), + uno::Reference< XInterface >(), 2 ); } - else if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("EncryptionKey") ) ) + else if ( aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( ENCRYPTION_KEY_PROPERTY ) ) ) { if ( rZipPackage.getFormat() != embed::StorageFormats::PACKAGE ) throw beans::PropertyVetoException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); - Sequence < sal_Int8 > aNewKey; + uno::Sequence< sal_Int8 > aNewKey; if ( !( aValue >>= aNewKey ) ) { @@ -685,31 +784,69 @@ void SAL_CALL ZipPackageStream::setPropertyValue( const OUString& aPropertyName, Sequence < sal_Int8 > aSequence ( nPathLength ); sal_Int8 *pArray = aSequence.getArray(); const sal_Unicode *pChar = sTempString.getStr(); - for ( sal_Int16 i = 0; i < nPathLength; i++) - pArray[i] = static_cast < const sal_Int8 > (pChar[i]); + for ( sal_Int16 i = 0; i < nPathLength; i++ ) + pArray[i] = static_cast < const sal_Int8 > ( pChar[i] ); aNewKey = aSequence; } else throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Wrong type for EncryptionKey property!\n" ) ), - Reference< XInterface >(), + uno::Reference< XInterface >(), 2 ); } if ( aNewKey.getLength() ) { - if ( !xEncryptionData.is()) - xEncryptionData = new EncryptionData; + if ( !m_xBaseEncryptionData.is() ) + m_xBaseEncryptionData = new BaseEncryptionData; + + m_aEncryptionKey = aNewKey; + // In case of new raw stream, the stream must not be encrypted on storing + bHaveOwnKey = sal_True; + if ( m_nStreamMode != PACKAGE_STREAM_RAW ) + bToBeEncrypted = sal_True; + } + else + { + bHaveOwnKey = sal_False; + m_aEncryptionKey.realloc( 0 ); + } + + m_aStorageEncryptionKeys.realloc( 0 ); + } + else if ( aPropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( STORAGE_ENCRYPTION_KEYS_PROPERTY ) ) ) + { + if ( rZipPackage.getFormat() != embed::StorageFormats::PACKAGE ) + throw beans::PropertyVetoException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); + + uno::Sequence< beans::NamedValue > aKeys; + if ( !( aValue >>= aKeys ) ) + { + throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Wrong type for StorageEncryptionKeys property!\n" ) ), + uno::Reference< XInterface >(), + 2 ); + } + + if ( aKeys.getLength() ) + { + if ( !m_xBaseEncryptionData.is() ) + m_xBaseEncryptionData = new BaseEncryptionData; + + m_aStorageEncryptionKeys = aKeys; - xEncryptionData->aKey = aNewKey; // In case of new raw stream, the stream must not be encrypted on storing bHaveOwnKey = sal_True; if ( m_nStreamMode != PACKAGE_STREAM_RAW ) bToBeEncrypted = sal_True; } else + { bHaveOwnKey = sal_False; + m_aStorageEncryptionKeys.realloc( 0 ); + } + + m_aEncryptionKey.realloc( 0 ); } - else if (aPropertyName.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "Compressed" ) ) ) + else if ( aPropertyName.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "Compressed" ) ) ) { sal_Bool bCompr = sal_False; @@ -718,7 +855,7 @@ void SAL_CALL ZipPackageStream::setPropertyValue( const OUString& aPropertyName, // In case of new raw stream, the stream must not be encrypted on storing if ( bCompr && m_nStreamMode == PACKAGE_STREAM_RAW ) throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Raw stream can not be encrypted on storing" ) ), - Reference< XInterface >(), + uno::Reference< XInterface >(), 2 ); bToBeCompressed = bCompr; @@ -726,7 +863,7 @@ void SAL_CALL ZipPackageStream::setPropertyValue( const OUString& aPropertyName, } else throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Wrong type for Compressed property!\n" ) ), - Reference< XInterface >(), + uno::Reference< XInterface >(), 2 ); } else @@ -735,37 +872,42 @@ void SAL_CALL ZipPackageStream::setPropertyValue( const OUString& aPropertyName, //-------------------------------------------------------------------------- Any SAL_CALL ZipPackageStream::getPropertyValue( const OUString& PropertyName ) - throw(beans::UnknownPropertyException, WrappedTargetException, RuntimeException) + throw( beans::UnknownPropertyException, WrappedTargetException, RuntimeException ) { Any aAny; - if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "MediaType" ) ) ) + if ( PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "MediaType" ) ) ) { aAny <<= sMediaType; return aAny; } - else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "Size" ) ) ) + else if ( PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "Size" ) ) ) { aAny <<= aEntry.nSize; return aAny; } - else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "Encrypted" ) ) ) + else if ( PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "Encrypted" ) ) ) { aAny <<= ((m_nStreamMode == PACKAGE_STREAM_RAW) ? sal_True : bToBeEncrypted); return aAny; } - else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "WasEncrypted" ) ) ) + else if ( PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "WasEncrypted" ) ) ) { aAny <<= bIsEncrypted; return aAny; } - else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "Compressed" ) ) ) + else if ( PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "Compressed" ) ) ) { aAny <<= bToBeCompressed; return aAny; } - else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "EncryptionKey" ) ) ) + else if ( PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( ENCRYPTION_KEY_PROPERTY ) ) ) + { + aAny <<= m_aEncryptionKey; + return aAny; + } + else if ( PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( STORAGE_ENCRYPTION_KEYS_PROPERTY ) ) ) { - aAny <<= (!xEncryptionData.is() ? Sequence < sal_Int8 > () : xEncryptionData->aKey); + aAny <<= m_aStorageEncryptionKeys; return aAny; } else @@ -773,30 +915,30 @@ Any SAL_CALL ZipPackageStream::getPropertyValue( const OUString& PropertyName ) } //-------------------------------------------------------------------------- -void ZipPackageStream::setSize (const sal_Int32 nNewSize) +void ZipPackageStream::setSize ( const sal_Int32 nNewSize ) { - if (aEntry.nCompressedSize != nNewSize ) + if ( aEntry.nCompressedSize != nNewSize ) aEntry.nMethod = DEFLATED; aEntry.nSize = nNewSize; } //-------------------------------------------------------------------------- OUString ZipPackageStream::getImplementationName() - throw (RuntimeException) + throw ( RuntimeException ) { return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "ZipPackageStream" ) ); } //-------------------------------------------------------------------------- Sequence< OUString > ZipPackageStream::getSupportedServiceNames() - throw (RuntimeException) + throw ( RuntimeException ) { - Sequence< OUString > aNames(1); + Sequence< OUString > aNames( 1 ); aNames[0] = OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.packages.PackageStream" ) ); return aNames; } //-------------------------------------------------------------------------- sal_Bool SAL_CALL ZipPackageStream::supportsService( OUString const & rServiceName ) - throw (RuntimeException) + throw ( RuntimeException ) { return rServiceName == getSupportedServiceNames()[0]; } diff --git a/package/source/zippackage/ZipPackageStream.hxx b/package/source/zippackage/ZipPackageStream.hxx deleted file mode 100644 index 9bdc09d04f92..000000000000 --- a/package/source/zippackage/ZipPackageStream.hxx +++ /dev/null @@ -1,196 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/************************************************************************* - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * Copyright 2000, 2010 Oracle and/or its affiliates. - * - * OpenOffice.org - a multi-platform office productivity suite - * - * This file is part of OpenOffice.org. - * - * OpenOffice.org is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License version 3 - * only, as published by the Free Software Foundation. - * - * OpenOffice.org is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License version 3 for more details - * (a copy is included in the LICENSE file that accompanied this code). - * - * You should have received a copy of the GNU Lesser General Public License - * version 3 along with OpenOffice.org. If not, see - * <http://www.openoffice.org/license.html> - * for a copy of the LGPLv3 License. - * - ************************************************************************/ -#ifndef _ZIP_PACKAGE_STREAM_HXX -#define _ZIP_PACKAGE_STREAM_HXX - -#include <com/sun/star/io/XActiveDataSink.hpp> -#include <com/sun/star/io/XSeekable.hpp> -#include <com/sun/star/packages/XDataSinkEncrSupport.hpp> -#include <ZipPackageEntry.hxx> -#include <rtl/ref.hxx> -#include <EncryptionData.hxx> -#include <cppuhelper/implbase2.hxx> -#include <mutexholder.hxx> - -#define PACKAGE_STREAM_NOTSET 0 -#define PACKAGE_STREAM_PACKAGEMEMBER 1 -#define PACKAGE_STREAM_DETECT 2 -#define PACKAGE_STREAM_DATA 3 -#define PACKAGE_STREAM_RAW 4 - -class ZipPackage; -struct ZipEntry; -class ZipPackageStream : public cppu::ImplInheritanceHelper2 -< - ZipPackageEntry, - ::com::sun::star::io::XActiveDataSink, - ::com::sun::star::packages::XDataSinkEncrSupport -> -{ -protected: - com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xStream; - const ::com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory > m_xFactory; - ZipPackage &rZipPackage; - sal_Bool bToBeCompressed, bToBeEncrypted, bHaveOwnKey, bIsEncrypted; - rtl::Reference < EncryptionData > xEncryptionData; - - sal_uInt8 m_nStreamMode; - sal_uInt32 m_nMagicalHackPos; - sal_uInt32 m_nMagicalHackSize; - - sal_Bool m_bHasSeekable; - - sal_Bool m_bCompressedIsSetFromOutside; - - sal_Bool m_bFromManifest; - - ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& GetOwnSeekStream(); - -public: - sal_Bool HasOwnKey () const { return bHaveOwnKey;} - sal_Bool IsToBeCompressed () const { return bToBeCompressed;} - sal_Bool IsToBeEncrypted () const { return bToBeEncrypted;} - sal_Bool IsEncrypted () const { return bIsEncrypted;} - sal_Bool IsPackageMember () const { return m_nStreamMode == PACKAGE_STREAM_PACKAGEMEMBER;} - - sal_Bool IsFromManifest() const { return m_bFromManifest; } - void SetFromManifest( sal_Bool bValue ) { m_bFromManifest = bValue; } - - rtl::Reference < EncryptionData > & getEncryptionData () - { return xEncryptionData;} - const com::sun::star::uno::Sequence < sal_Int8 >& getKey () const - { return xEncryptionData->aKey;} - const com::sun::star::uno::Sequence < sal_uInt8 >& getInitialisationVector () const - { return xEncryptionData->aInitVector;} - const com::sun::star::uno::Sequence < sal_uInt8 >& getDigest () const - { return xEncryptionData->aDigest;} - const com::sun::star::uno::Sequence < sal_uInt8 >& getSalt () const - { return xEncryptionData->aSalt;} - sal_Int32 getIterationCount () const - { return xEncryptionData->nIterationCount;} - sal_Int32 getSize () const - { return aEntry.nSize;} - - sal_uInt8 GetStreamMode() const { return m_nStreamMode; } - sal_uInt32 GetMagicalHackPos() const { return m_nMagicalHackPos; } - sal_uInt32 GetMagicalHackSize() const { return m_nMagicalHackSize; } - - void SetToBeCompressed (sal_Bool bNewValue) { bToBeCompressed = bNewValue;} - void SetIsEncrypted (sal_Bool bNewValue) { bIsEncrypted = bNewValue;} - void SetToBeEncrypted (sal_Bool bNewValue) - { - bToBeEncrypted = bNewValue; - if ( bToBeEncrypted && !xEncryptionData.is()) - xEncryptionData = new EncryptionData; - else if ( !bToBeEncrypted && xEncryptionData.is() ) - xEncryptionData.clear(); - } - void SetPackageMember (sal_Bool bNewValue); - void setKey (const com::sun::star::uno::Sequence < sal_Int8 >& rNewKey ) - { xEncryptionData->aKey = rNewKey;} - void setInitialisationVector (const com::sun::star::uno::Sequence < sal_uInt8 >& rNewVector ) - { xEncryptionData->aInitVector = rNewVector;} - void setSalt (const com::sun::star::uno::Sequence < sal_uInt8 >& rNewSalt ) - { xEncryptionData->aSalt = rNewSalt;} - void setDigest (const com::sun::star::uno::Sequence < sal_uInt8 >& rNewDigest ) - { xEncryptionData->aDigest = rNewDigest;} - void setIterationCount (const sal_Int32 nNewCount) - { xEncryptionData->nIterationCount = nNewCount;} - void setSize (const sal_Int32 nNewSize); - - ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > GetOwnStreamNoWrap() { return xStream; } - - void CloseOwnStreamIfAny(); - - ZipPackageStream ( ZipPackage & rNewPackage, - const ::com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory >& xFactory, - sal_Bool bAllowRemoveOnInsert ); - virtual ~ZipPackageStream( void ); - - ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > GetRawEncrStreamNoHeaderCopy(); - ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > TryToGetRawFromDataStream( - sal_Bool bAddHeaderForEncr ); - - sal_Bool ParsePackageRawStream(); - - void setZipEntryOnLoading( const ZipEntry &rInEntry); - ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getRawData() - throw(::com::sun::star::uno::RuntimeException); - - static const ::com::sun::star::uno::Sequence < sal_Int8 >& static_getImplementationId(); - - // XActiveDataSink - virtual void SAL_CALL setInputStream( const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& aStream ) - throw(::com::sun::star::uno::RuntimeException); - virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getInputStream( ) - throw(::com::sun::star::uno::RuntimeException); - - // XDataSinkEncrSupport - virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getDataStream() - throw ( ::com::sun::star::packages::WrongPasswordException, - ::com::sun::star::io::IOException, - ::com::sun::star::uno::RuntimeException ); - virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getRawStream() - throw ( ::com::sun::star::packages::NoEncryptionException, - ::com::sun::star::io::IOException, - ::com::sun::star::uno::RuntimeException ); - virtual void SAL_CALL setDataStream( - const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& aStream ) - throw ( ::com::sun::star::io::IOException, - ::com::sun::star::uno::RuntimeException ); - virtual void SAL_CALL setRawStream( - const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& aStream ) - throw ( ::com::sun::star::packages::EncryptionNotAllowedException, - ::com::sun::star::packages::NoRawFormatException, - ::com::sun::star::io::IOException, - ::com::sun::star::uno::RuntimeException ); - virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getPlainRawStream() - throw ( ::com::sun::star::io::IOException, - ::com::sun::star::uno::RuntimeException ); - - // XUnoTunnel - virtual sal_Int64 SAL_CALL getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier ) - throw(::com::sun::star::uno::RuntimeException); - - // XPropertySet - virtual void SAL_CALL setPropertyValue( const ::rtl::OUString& aPropertyName, const ::com::sun::star::uno::Any& aValue ) - throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException); - virtual ::com::sun::star::uno::Any SAL_CALL getPropertyValue( const ::rtl::OUString& PropertyName ) - throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException); - - // XServiceInfo - virtual ::rtl::OUString SAL_CALL getImplementationName( ) - throw (::com::sun::star::uno::RuntimeException); - virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) - throw (::com::sun::star::uno::RuntimeException); - virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) - throw (::com::sun::star::uno::RuntimeException); -}; -#endif - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/package/source/zippackage/zipfileaccess.cxx b/package/source/zippackage/zipfileaccess.cxx index a75bed446335..e4b9d75b9ba3 100644 --- a/package/source/zippackage/zipfileaccess.cxx +++ b/package/source/zippackage/zipfileaccess.cxx @@ -41,6 +41,7 @@ #include <EncryptionData.hxx> #include <ucbhelper/content.hxx> +#include <rtl/ref.hxx> #include <memory> @@ -252,7 +253,7 @@ uno::Any SAL_CALL OZipFileAccess::getByName( const ::rtl::OUString& aName ) throw container::NoSuchElementException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); uno::Reference< io::XInputStream > xEntryStream( m_pZipFile->getDataStream( (*aIter).second, - new EncryptionData(), + ::rtl::Reference< EncryptionData >(), sal_False, m_aMutexHolder ) ); @@ -367,7 +368,7 @@ uno::Reference< io::XInputStream > SAL_CALL OZipFileAccess::getStreamByPattern( if ( StringGoodForPattern_Impl( (*aIter).second.sPath, aPattern ) ) { uno::Reference< io::XInputStream > xEntryStream( m_pZipFile->getDataStream( (*aIter).second, - new EncryptionData(), + ::rtl::Reference< EncryptionData >(), sal_False, m_aMutexHolder ) ); |