1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ManifestExport.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_package.hxx"
33 #include <ManifestExport.hxx>
34 #include <ManifestDefines.hxx>
35 #ifndef _COM_SUN_STAR_XML_SAX_XATTRIBUTELIST_HXX
36 #include <com/sun/star/xml/sax/XAttributeList.hpp>
38 #include <rtl/ustrbuf.hxx>
39 #ifndef _BASE64_CODEC_HXX_
40 #include <Base64Codec.hxx>
42 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
43 #ifndef _COM_SUN_STAR_XML_SAX_XDOCUMENTHANDLER_HXX
44 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
46 #ifndef _COM_SUN_STAR_XML_BEANS_PROPERTYVALUE_HPP
47 #include <com/sun/star/beans/PropertyValue.hpp>
50 #include <comphelper/documentconstants.hxx>
51 #include <comphelper/attributelist.hxx>
54 using namespace com::sun::star::beans
;
55 using namespace com::sun::star::uno
;
56 using namespace com::sun::star::xml::sax
;
58 ManifestExport::ManifestExport(Reference
< XDocumentHandler
> xHandler
, const Sequence
< Sequence
< PropertyValue
> > &rManList
)
60 const OUString
sFileEntryElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_FILE_ENTRY
) );
61 const OUString
sManifestElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_MANIFEST
) );
62 const OUString
sEncryptionDataElement( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_ENCRYPTION_DATA
) );
63 const OUString
sAlgorithmElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_ALGORITHM
) );
64 const OUString
sStartKeyGenerationElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_START_KEY_GENERATION
) );
65 const OUString
sKeyDerivationElement( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_KEY_DERIVATION
) );
67 const OUString
sCdataAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_CDATA
) );
68 const OUString
sMediaTypeAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_MEDIA_TYPE
) );
69 const OUString
sVersionAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_VERSION
) );
70 const OUString
sFullPathAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_FULL_PATH
) );
71 const OUString
sSizeAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_SIZE
) );
72 const OUString
sKeySizeAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_KEY_SIZE
) );
73 const OUString
sSaltAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_SALT
) );
74 const OUString
sInitialisationVectorAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_INITIALISATION_VECTOR
) );
75 const OUString
sIterationCountAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_ITERATION_COUNT
) );
76 const OUString
sAlgorithmNameAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_ALGORITHM_NAME
) );
77 const OUString
sStartKeyGenerationNameAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_START_KEY_GENERATION_NAME
) );
78 const OUString
sKeyDerivationNameAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_KEY_DERIVATION_NAME
) );
79 const OUString
sChecksumTypeAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_CHECKSUM_TYPE
) );
80 const OUString
sChecksumAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_CHECKSUM
) );
82 const OUString
sFullPathProperty ( RTL_CONSTASCII_USTRINGPARAM ( "FullPath" ) );
83 const OUString
sVersionProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Version" ) );
84 const OUString
sMediaTypeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "MediaType" ) );
85 const OUString
sIterationCountProperty ( RTL_CONSTASCII_USTRINGPARAM ( "IterationCount" ) );
86 const OUString
sSaltProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Salt" ) );
87 const OUString
sInitialisationVectorProperty( RTL_CONSTASCII_USTRINGPARAM ( "InitialisationVector" ) );
88 const OUString
sSizeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Size" ) );
89 const OUString
sDigestProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Digest" ) );
91 const OUString
sWhiteSpace ( RTL_CONSTASCII_USTRINGPARAM ( " " ) );
92 const OUString
sBlowfish ( RTL_CONSTASCII_USTRINGPARAM ( "Blowfish CFB" ) );
93 const OUString
sPBKDF2 ( RTL_CONSTASCII_USTRINGPARAM ( "PBKDF2" ) );
94 const OUString
sChecksumType ( RTL_CONSTASCII_USTRINGPARAM ( CHECKSUM_TYPE
) );
95 const OUString
sStartKeySize ( RTL_CONSTASCII_USTRINGPARAM ( START_KEY_SIZE
) );
96 const OUString
sDerivedKeySize ( RTL_CONSTASCII_USTRINGPARAM ( DERIVED_KEY_SIZE
) );
97 const OUString
sSHA1 ( RTL_CONSTASCII_USTRINGPARAM ( ALGORITHM_SHA1
) );
99 ::comphelper::AttributeList
* pRootAttrList
= new ::comphelper::AttributeList
;
100 const Sequence
< PropertyValue
> *pSequence
= rManList
.getConstArray();
101 const sal_uInt32 nManLength
= rManList
.getLength();
103 // find the mediatype of the document if any
104 OUString aDocMediaType
;
105 OUString aDocVersion
;
106 for (sal_uInt32 nInd
= 0; nInd
< nManLength
; nInd
++ )
112 const PropertyValue
*pValue
= pSequence
[nInd
].getConstArray();
113 for (sal_uInt32 j
= 0, nNum
= pSequence
[nInd
].getLength(); j
< nNum
; j
++, pValue
++)
115 if (pValue
->Name
.equals (sMediaTypeProperty
) )
117 pValue
->Value
>>= aMediaType
;
119 else if (pValue
->Name
.equals (sFullPathProperty
) )
121 pValue
->Value
>>= aPath
;
123 else if (pValue
->Name
.equals (sVersionProperty
) )
125 pValue
->Value
>>= aVersion
;
128 if ( aPath
.getLength() && aMediaType
.getLength() && aVersion
.getLength() )
132 if ( aPath
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) ) ) )
134 aDocMediaType
= aMediaType
;
135 aDocVersion
= aVersion
;
140 sal_Bool bProvideDTD
= sal_False
;
141 sal_Bool bAcceptNonemptyVersion
= sal_False
;
142 sal_Bool bStoreStartKeyGeneration
= sal_False
;
143 if ( aDocMediaType
.getLength() )
145 if ( aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_TEXT_ASCII
) ) )
146 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_TEXT_WEB_ASCII
) ) )
147 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_TEXT_GLOBAL_ASCII
) ) )
148 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_ASCII
) ) )
149 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_ASCII
) ) )
150 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_ASCII
) ) )
151 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_CHART_ASCII
) ) )
152 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII
) ) )
153 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_ASCII
) ) )
155 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_TEXT_TEMPLATE_ASCII
) ) )
156 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_TEMPLATE_ASCII
) ) )
157 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_TEMPLATE_ASCII
) ) )
158 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_TEMPLATE_ASCII
) ) )
159 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_CHART_TEMPLATE_ASCII
) ) )
160 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_TEMPLATE_ASCII
) ) ) )
164 pRootAttrList
->AddAttribute ( OUString( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_XMLNS
) ),
166 OUString( RTL_CONSTASCII_USTRINGPARAM ( MANIFEST_OASIS_NAMESPACE
) ) );
167 bAcceptNonemptyVersion
= sal_True
;
168 if ( aDocVersion
.compareTo( ODFVER_012_TEXT
) >= 0 )
170 // this is ODF12 generation, let encrypted streams contain start-key-generation entry
171 bStoreStartKeyGeneration
= sal_True
;
176 // even if it is no SO6 format the namespace must be specified
177 // thus SO6 format is used as default one
178 pRootAttrList
->AddAttribute ( OUString( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_XMLNS
) ),
180 OUString( RTL_CONSTASCII_USTRINGPARAM ( MANIFEST_NAMESPACE
) ) );
182 bProvideDTD
= sal_True
;
186 Reference
< XAttributeList
> xRootAttrList (pRootAttrList
);
188 xHandler
->startDocument();
189 Reference
< XExtendedDocumentHandler
> xExtHandler ( xHandler
, UNO_QUERY
);
190 if ( xExtHandler
.is() && bProvideDTD
)
192 OUString
aDocType ( RTL_CONSTASCII_USTRINGPARAM ( MANIFEST_DOCTYPE
) );
193 xExtHandler
->unknown ( aDocType
);
194 xHandler
->ignorableWhitespace ( sWhiteSpace
);
196 xHandler
->startElement( sManifestElement
, xRootAttrList
);
198 for (sal_uInt32 i
= 0 ; i
< nManLength
; i
++)
200 ::comphelper::AttributeList
*pAttrList
= new ::comphelper::AttributeList
;
201 const PropertyValue
*pValue
= pSequence
[i
].getConstArray();
203 const PropertyValue
*pVector
= NULL
, *pSalt
= NULL
, *pIterationCount
= NULL
, *pDigest
= NULL
;
204 for (sal_uInt32 j
= 0, nNum
= pSequence
[i
].getLength(); j
< nNum
; j
++, pValue
++)
206 if (pValue
->Name
.equals (sMediaTypeProperty
) )
208 pValue
->Value
>>= aString
;
209 pAttrList
->AddAttribute ( sMediaTypeAttribute
, sCdataAttribute
, aString
);
211 else if (pValue
->Name
.equals (sVersionProperty
) )
213 pValue
->Value
>>= aString
;
214 // the version is stored only if it is not empty
215 if ( bAcceptNonemptyVersion
&& aString
.getLength() )
216 pAttrList
->AddAttribute ( sVersionAttribute
, sCdataAttribute
, aString
);
218 else if (pValue
->Name
.equals (sFullPathProperty
) )
220 pValue
->Value
>>= aString
;
221 pAttrList
->AddAttribute ( sFullPathAttribute
, sCdataAttribute
, aString
);
223 else if (pValue
->Name
.equals (sSizeProperty
) )
226 pValue
->Value
>>= nSize
;
227 OUStringBuffer aBuffer
;
228 aBuffer
.append ( nSize
);
229 pAttrList
->AddAttribute ( sSizeAttribute
, sCdataAttribute
, aBuffer
.makeStringAndClear() );
231 else if (pValue
->Name
.equals (sInitialisationVectorProperty
) )
233 else if (pValue
->Name
.equals (sSaltProperty
) )
235 else if (pValue
->Name
.equals (sIterationCountProperty
) )
236 pIterationCount
= pValue
;
237 else if (pValue
->Name
.equals ( sDigestProperty
) )
240 xHandler
->ignorableWhitespace ( sWhiteSpace
);
241 Reference
< XAttributeList
> xAttrList ( pAttrList
);
242 xHandler
->startElement( sFileEntryElement
, xAttrList
);
243 if ( pVector
&& pSalt
&& pIterationCount
)
245 // ==== Encryption Data
246 ::comphelper::AttributeList
* pNewAttrList
= new ::comphelper::AttributeList
;
247 Reference
< XAttributeList
> xNewAttrList (pNewAttrList
);
248 OUStringBuffer aBuffer
;
249 Sequence
< sal_uInt8
> aSequence
;
251 xHandler
->ignorableWhitespace ( sWhiteSpace
);
254 pNewAttrList
->AddAttribute ( sChecksumTypeAttribute
, sCdataAttribute
, sChecksumType
);
255 pDigest
->Value
>>= aSequence
;
256 Base64Codec::encodeBase64 ( aBuffer
, aSequence
);
257 pNewAttrList
->AddAttribute ( sChecksumAttribute
, sCdataAttribute
, aBuffer
.makeStringAndClear() );
259 xHandler
->startElement( sEncryptionDataElement
, xNewAttrList
);
262 pNewAttrList
= new ::comphelper::AttributeList
;
263 xNewAttrList
= pNewAttrList
;
265 pNewAttrList
->AddAttribute ( sAlgorithmNameAttribute
, sCdataAttribute
, sBlowfish
);
267 pVector
->Value
>>= aSequence
;
268 Base64Codec::encodeBase64 ( aBuffer
, aSequence
);
269 pNewAttrList
->AddAttribute ( sInitialisationVectorAttribute
, sCdataAttribute
, aBuffer
.makeStringAndClear() );
271 xHandler
->ignorableWhitespace ( sWhiteSpace
);
272 xHandler
->startElement( sAlgorithmElement
, xNewAttrList
);
273 xHandler
->ignorableWhitespace ( sWhiteSpace
);
274 xHandler
->endElement( sAlgorithmElement
);
276 // ==== Key Derivation
277 pNewAttrList
= new ::comphelper::AttributeList
;
278 xNewAttrList
= pNewAttrList
;
280 pNewAttrList
->AddAttribute ( sKeyDerivationNameAttribute
, sCdataAttribute
, sPBKDF2
);
282 if ( bStoreStartKeyGeneration
)
283 pNewAttrList
->AddAttribute ( sKeySizeAttribute
, sCdataAttribute
, sDerivedKeySize
);
285 sal_Int32 nCount
= 0;
286 pIterationCount
->Value
>>= nCount
;
287 aBuffer
.append (nCount
);
288 pNewAttrList
->AddAttribute ( sIterationCountAttribute
, sCdataAttribute
, aBuffer
.makeStringAndClear() );
290 pSalt
->Value
>>= aSequence
;
291 Base64Codec::encodeBase64 ( aBuffer
, aSequence
);
292 pNewAttrList
->AddAttribute ( sSaltAttribute
, sCdataAttribute
, aBuffer
.makeStringAndClear() );
294 xHandler
->ignorableWhitespace ( sWhiteSpace
);
295 xHandler
->startElement( sKeyDerivationElement
, xNewAttrList
);
296 xHandler
->ignorableWhitespace ( sWhiteSpace
);
297 xHandler
->endElement( sKeyDerivationElement
);
299 // we have to store start-key-generation element as the last one to workaround the parsing problem
300 // in OOo3.1 and older versions
301 if ( bStoreStartKeyGeneration
)
303 // ==== Start Key Generation
304 pNewAttrList
= new ::comphelper::AttributeList
;
305 xNewAttrList
= pNewAttrList
;
307 // currently SHA1 is used to generate 20-bytes start key
308 pNewAttrList
->AddAttribute ( sStartKeyGenerationNameAttribute
, sCdataAttribute
, sSHA1
);
309 pNewAttrList
->AddAttribute ( sKeySizeAttribute
, sCdataAttribute
, sStartKeySize
);
311 xHandler
->ignorableWhitespace ( sWhiteSpace
);
312 xHandler
->startElement( sStartKeyGenerationElement
, xNewAttrList
);
313 xHandler
->ignorableWhitespace ( sWhiteSpace
);
314 xHandler
->endElement( sStartKeyGenerationElement
);
317 xHandler
->ignorableWhitespace ( sWhiteSpace
);
318 xHandler
->endElement( sEncryptionDataElement
);
320 xHandler
->ignorableWhitespace ( sWhiteSpace
);
321 xHandler
->endElement( sFileEntryElement
);
323 xHandler
->ignorableWhitespace ( sWhiteSpace
);
324 xHandler
->endElement( sManifestElement
);
325 xHandler
->endDocument();