Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
21 : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
22 : #include <com/sun/star/xml/sax/XAttributeList.hpp>
23 : #include <com/sun/star/xml/crypto/DigestID.hpp>
24 : #include <com/sun/star/xml/crypto/CipherID.hpp>
25 : #include <com/sun/star/beans/PropertyValue.hpp>
26 : #include <com/sun/star/uno/RuntimeException.hpp>
27 :
28 : #include <ManifestDefines.hxx>
29 : #include <ManifestExport.hxx>
30 : #include <sax/tools/converter.hxx>
31 :
32 : #include <rtl/ustrbuf.hxx>
33 : #include <comphelper/documentconstants.hxx>
34 : #include <comphelper/attributelist.hxx>
35 :
36 : using namespace ::com::sun::star;
37 :
38 : #if OSL_DEBUG_LEVEL > 0
39 : #define THROW_WHERE SAL_WHERE
40 : #else
41 : #define THROW_WHERE ""
42 : #endif
43 :
44 192 : ManifestExport::ManifestExport( uno::Reference< xml::sax::XDocumentHandler > xHandler, const uno::Sequence< uno::Sequence < beans::PropertyValue > >& rManList )
45 : {
46 192 : const OUString sFileEntryElement ( ELEMENT_FILE_ENTRY );
47 384 : const OUString sManifestElement ( ELEMENT_MANIFEST );
48 384 : const OUString sEncryptionDataElement( ELEMENT_ENCRYPTION_DATA );
49 384 : const OUString sAlgorithmElement ( ELEMENT_ALGORITHM );
50 384 : const OUString sStartKeyGenerationElement ( ELEMENT_START_KEY_GENERATION );
51 384 : const OUString sKeyDerivationElement ( ELEMENT_KEY_DERIVATION );
52 :
53 384 : const OUString sCdataAttribute ( ATTRIBUTE_CDATA );
54 384 : const OUString sMediaTypeAttribute ( ATTRIBUTE_MEDIA_TYPE );
55 384 : const OUString sVersionAttribute ( ATTRIBUTE_VERSION );
56 384 : const OUString sFullPathAttribute ( ATTRIBUTE_FULL_PATH );
57 384 : const OUString sSizeAttribute ( ATTRIBUTE_SIZE );
58 384 : const OUString sKeySizeAttribute ( ATTRIBUTE_KEY_SIZE );
59 384 : const OUString sSaltAttribute ( ATTRIBUTE_SALT );
60 384 : const OUString sInitialisationVectorAttribute ( ATTRIBUTE_INITIALISATION_VECTOR );
61 384 : const OUString sIterationCountAttribute ( ATTRIBUTE_ITERATION_COUNT );
62 384 : const OUString sAlgorithmNameAttribute ( ATTRIBUTE_ALGORITHM_NAME );
63 384 : const OUString sStartKeyGenerationNameAttribute ( ATTRIBUTE_START_KEY_GENERATION_NAME );
64 384 : const OUString sKeyDerivationNameAttribute ( ATTRIBUTE_KEY_DERIVATION_NAME );
65 384 : const OUString sChecksumTypeAttribute ( ATTRIBUTE_CHECKSUM_TYPE );
66 384 : const OUString sChecksumAttribute ( ATTRIBUTE_CHECKSUM);
67 :
68 384 : const OUString sFullPathProperty ( "FullPath" );
69 384 : const OUString sVersionProperty ( "Version" );
70 384 : const OUString sMediaTypeProperty ( "MediaType" );
71 384 : const OUString sIterationCountProperty ( "IterationCount" );
72 384 : const OUString sDerivedKeySizeProperty ( "DerivedKeySize" );
73 384 : const OUString sSaltProperty ( "Salt" );
74 384 : const OUString sInitialisationVectorProperty( "InitialisationVector" );
75 384 : const OUString sSizeProperty ( "Size" );
76 384 : const OUString sDigestProperty ( "Digest" );
77 384 : const OUString sEncryptionAlgProperty ( "EncryptionAlgorithm" );
78 384 : const OUString sStartKeyAlgProperty ( "StartKeyAlgorithm" );
79 384 : const OUString sDigestAlgProperty ( "DigestAlgorithm" );
80 :
81 384 : const OUString sWhiteSpace ( " " );
82 :
83 384 : const OUString sSHA256_URL ( SHA256_URL );
84 384 : const OUString sSHA1_Name ( SHA1_NAME );
85 :
86 384 : const OUString sSHA1_1k_Name ( SHA1_1K_NAME );
87 384 : const OUString sSHA256_1k_URL ( SHA256_1K_URL );
88 :
89 384 : const OUString sBlowfish_Name ( BLOWFISH_NAME );
90 384 : const OUString sAES256_URL ( AES256_URL );
91 :
92 384 : const OUString sPBKDF2_Name ( PBKDF2_NAME );
93 :
94 192 : ::comphelper::AttributeList * pRootAttrList = new ::comphelper::AttributeList;
95 192 : const uno::Sequence < beans::PropertyValue > *pSequence = rManList.getConstArray();
96 192 : const sal_uInt32 nManLength = rManList.getLength();
97 :
98 : // find the mediatype of the document if any
99 384 : OUString aDocMediaType;
100 384 : OUString aDocVersion;
101 192 : for (sal_uInt32 nInd = 0; nInd < nManLength ; nInd++ )
102 : {
103 192 : OUString aMediaType;
104 192 : OUString aPath;
105 192 : OUString aVersion;
106 :
107 192 : const beans::PropertyValue *pValue = pSequence[nInd].getConstArray();
108 605 : for (sal_uInt32 j = 0, nNum = pSequence[nInd].getLength(); j < nNum; j++, pValue++)
109 : {
110 576 : if (pValue->Name.equals (sMediaTypeProperty) )
111 : {
112 192 : pValue->Value >>= aMediaType;
113 : }
114 384 : else if (pValue->Name.equals (sFullPathProperty) )
115 : {
116 192 : pValue->Value >>= aPath;
117 : }
118 192 : else if (pValue->Name.equals (sVersionProperty) )
119 : {
120 192 : pValue->Value >>= aVersion;
121 : }
122 :
123 576 : if ( !aPath.isEmpty() && !aMediaType.isEmpty() && !aVersion.isEmpty() )
124 163 : break;
125 : }
126 :
127 192 : if ( aPath == "/" )
128 : {
129 192 : aDocMediaType = aMediaType;
130 192 : aDocVersion = aVersion;
131 192 : break;
132 : }
133 0 : }
134 :
135 192 : sal_Bool bProvideDTD = sal_False;
136 192 : sal_Bool bAcceptNonemptyVersion = sal_False;
137 192 : sal_Bool bStoreStartKeyGeneration = sal_False;
138 192 : if ( !aDocMediaType.isEmpty() )
139 : {
140 334 : if ( aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_ASCII
141 142 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_WEB_ASCII
142 142 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_GLOBAL_ASCII
143 142 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_ASCII
144 141 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_ASCII
145 136 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_ASCII
146 90 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_CHART_ASCII
147 90 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII
148 2 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_ASCII
149 2 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_TEMPLATE_ASCII
150 0 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_TEMPLATE_ASCII
151 0 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_TEMPLATE_ASCII
152 0 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_TEMPLATE_ASCII
153 0 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_CHART_TEMPLATE_ASCII
154 167 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_TEMPLATE_ASCII )
155 :
156 : {
157 : // oasis format
158 : pRootAttrList->AddAttribute ( ATTRIBUTE_XMLNS,
159 : sCdataAttribute,
160 167 : MANIFEST_OASIS_NAMESPACE );
161 167 : bAcceptNonemptyVersion = sal_True;
162 167 : if ( aDocVersion.compareTo( ODFVER_012_TEXT ) >= 0 )
163 : {
164 : // this is ODF12 generation, let encrypted streams contain start-key-generation entry
165 163 : bStoreStartKeyGeneration = sal_True;
166 163 : pRootAttrList->AddAttribute ( sVersionAttribute, sCdataAttribute, aDocVersion );
167 : }
168 : }
169 : else
170 : {
171 : // even if it is no SO6 format the namespace must be specified
172 : // thus SO6 format is used as default one
173 : pRootAttrList->AddAttribute ( ATTRIBUTE_XMLNS,
174 : sCdataAttribute,
175 0 : MANIFEST_NAMESPACE );
176 :
177 0 : bProvideDTD = sal_True;
178 : }
179 : }
180 :
181 384 : uno::Reference < xml::sax::XAttributeList > xRootAttrList (pRootAttrList);
182 :
183 192 : xHandler->startDocument();
184 384 : uno::Reference < xml::sax::XExtendedDocumentHandler > xExtHandler ( xHandler, uno::UNO_QUERY );
185 192 : if ( xExtHandler.is() && bProvideDTD )
186 : {
187 0 : OUString aDocType ( MANIFEST_DOCTYPE );
188 0 : xExtHandler->unknown ( aDocType );
189 0 : xHandler->ignorableWhitespace ( sWhiteSpace );
190 : }
191 192 : xHandler->startElement( sManifestElement, xRootAttrList );
192 :
193 1048 : for (sal_uInt32 i = 0 ; i < nManLength ; i++)
194 : {
195 856 : ::comphelper::AttributeList *pAttrList = new ::comphelper::AttributeList;
196 856 : const beans::PropertyValue *pValue = pSequence[i].getConstArray();
197 856 : OUString aString;
198 856 : const uno::Any *pVector = NULL, *pSalt = NULL, *pIterationCount = NULL, *pDigest = NULL, *pDigestAlg = NULL, *pEncryptAlg = NULL, *pStartKeyAlg = NULL, *pDerivedKeySize = NULL;
199 3469 : for (sal_uInt32 j = 0, nNum = pSequence[i].getLength(); j < nNum; j++, pValue++)
200 : {
201 2613 : if (pValue->Name.equals (sMediaTypeProperty) )
202 : {
203 856 : pValue->Value >>= aString;
204 856 : pAttrList->AddAttribute ( sMediaTypeAttribute, sCdataAttribute, aString );
205 : }
206 1757 : else if (pValue->Name.equals (sVersionProperty) )
207 : {
208 856 : pValue->Value >>= aString;
209 : // the version is stored only if it is not empty
210 856 : if ( bAcceptNonemptyVersion && !aString.isEmpty() )
211 165 : pAttrList->AddAttribute ( sVersionAttribute, sCdataAttribute, aString );
212 : }
213 901 : else if (pValue->Name.equals (sFullPathProperty) )
214 : {
215 856 : pValue->Value >>= aString;
216 856 : pAttrList->AddAttribute ( sFullPathAttribute, sCdataAttribute, aString );
217 : }
218 45 : else if (pValue->Name.equals (sSizeProperty) )
219 : {
220 5 : sal_Int64 nSize = 0;
221 5 : pValue->Value >>= nSize;
222 5 : OUStringBuffer aBuffer;
223 5 : aBuffer.append ( nSize );
224 5 : pAttrList->AddAttribute ( sSizeAttribute, sCdataAttribute, aBuffer.makeStringAndClear() );
225 : }
226 40 : else if (pValue->Name.equals (sInitialisationVectorProperty) )
227 5 : pVector = &pValue->Value;
228 35 : else if (pValue->Name.equals (sSaltProperty) )
229 5 : pSalt = &pValue->Value;
230 30 : else if (pValue->Name.equals (sIterationCountProperty) )
231 5 : pIterationCount = &pValue->Value;
232 25 : else if (pValue->Name.equals ( sDigestProperty ) )
233 5 : pDigest = &pValue->Value;
234 20 : else if (pValue->Name.equals ( sDigestAlgProperty ) )
235 5 : pDigestAlg = &pValue->Value;
236 15 : else if (pValue->Name.equals ( sEncryptionAlgProperty ) )
237 5 : pEncryptAlg = &pValue->Value;
238 10 : else if (pValue->Name.equals ( sStartKeyAlgProperty ) )
239 5 : pStartKeyAlg = &pValue->Value;
240 5 : else if (pValue->Name.equals ( sDerivedKeySizeProperty ) )
241 5 : pDerivedKeySize = &pValue->Value;
242 : }
243 :
244 856 : xHandler->ignorableWhitespace ( sWhiteSpace );
245 1712 : uno::Reference < xml::sax::XAttributeList > xAttrList ( pAttrList );
246 856 : xHandler->startElement( sFileEntryElement , xAttrList);
247 856 : if ( pVector && pSalt && pIterationCount && pDigest && pDigestAlg && pEncryptAlg && pStartKeyAlg && pDerivedKeySize )
248 : {
249 : // ==== Encryption Data
250 5 : ::comphelper::AttributeList * pNewAttrList = new ::comphelper::AttributeList;
251 5 : uno::Reference < xml::sax::XAttributeList > xNewAttrList (pNewAttrList);
252 10 : OUStringBuffer aBuffer;
253 10 : uno::Sequence < sal_Int8 > aSequence;
254 :
255 5 : xHandler->ignorableWhitespace ( sWhiteSpace );
256 :
257 : // ==== Digest
258 10 : OUString sChecksumType;
259 5 : sal_Int32 nDigestAlgID = 0;
260 5 : *pDigestAlg >>= nDigestAlgID;
261 5 : if ( nDigestAlgID == xml::crypto::DigestID::SHA256_1K )
262 5 : sChecksumType = sSHA256_1k_URL;
263 0 : else if ( nDigestAlgID == xml::crypto::DigestID::SHA1_1K )
264 0 : sChecksumType = sSHA1_1k_Name;
265 : else
266 0 : throw uno::RuntimeException( THROW_WHERE "Unexpected digest algorithm is provided!", uno::Reference< uno::XInterface >() );
267 :
268 5 : pNewAttrList->AddAttribute ( sChecksumTypeAttribute, sCdataAttribute, sChecksumType );
269 5 : *pDigest >>= aSequence;
270 5 : ::sax::Converter::encodeBase64(aBuffer, aSequence);
271 5 : pNewAttrList->AddAttribute ( sChecksumAttribute, sCdataAttribute, aBuffer.makeStringAndClear() );
272 :
273 5 : xHandler->startElement( sEncryptionDataElement , xNewAttrList);
274 :
275 : // ==== Algorithm
276 5 : pNewAttrList = new ::comphelper::AttributeList;
277 5 : xNewAttrList = pNewAttrList;
278 :
279 5 : sal_Int32 nEncAlgID = 0;
280 5 : sal_Int32 nDerivedKeySize = 0;
281 5 : *pEncryptAlg >>= nEncAlgID;
282 5 : *pDerivedKeySize >>= nDerivedKeySize;
283 :
284 10 : OUString sEncAlgName;
285 5 : if ( nEncAlgID == xml::crypto::CipherID::AES_CBC_W3C_PADDING )
286 : {
287 : OSL_ENSURE( nDerivedKeySize, "Unexpected key size is provided!" );
288 5 : if ( nDerivedKeySize != 32 )
289 0 : throw uno::RuntimeException( THROW_WHERE "Unexpected key size is provided!", uno::Reference< uno::XInterface >() );
290 :
291 5 : sEncAlgName = sAES256_URL;
292 : }
293 0 : else if ( nEncAlgID == xml::crypto::CipherID::BLOWFISH_CFB_8 )
294 : {
295 0 : sEncAlgName = sBlowfish_Name;
296 : }
297 : else
298 0 : throw uno::RuntimeException( THROW_WHERE "Unexpected encryption algorithm is provided!", uno::Reference< uno::XInterface >() );
299 :
300 5 : pNewAttrList->AddAttribute ( sAlgorithmNameAttribute, sCdataAttribute, sEncAlgName );
301 :
302 5 : *pVector >>= aSequence;
303 5 : ::sax::Converter::encodeBase64(aBuffer, aSequence);
304 5 : pNewAttrList->AddAttribute ( sInitialisationVectorAttribute, sCdataAttribute, aBuffer.makeStringAndClear() );
305 :
306 5 : xHandler->ignorableWhitespace ( sWhiteSpace );
307 5 : xHandler->startElement( sAlgorithmElement , xNewAttrList);
308 5 : xHandler->ignorableWhitespace ( sWhiteSpace );
309 5 : xHandler->endElement( sAlgorithmElement );
310 :
311 : // ==== Key Derivation
312 5 : pNewAttrList = new ::comphelper::AttributeList;
313 5 : xNewAttrList = pNewAttrList;
314 :
315 5 : pNewAttrList->AddAttribute ( sKeyDerivationNameAttribute, sCdataAttribute, sPBKDF2_Name );
316 :
317 5 : if ( bStoreStartKeyGeneration )
318 : {
319 5 : aBuffer.append( nDerivedKeySize );
320 5 : pNewAttrList->AddAttribute ( sKeySizeAttribute, sCdataAttribute, aBuffer.makeStringAndClear() );
321 : }
322 :
323 5 : sal_Int32 nCount = 0;
324 5 : *pIterationCount >>= nCount;
325 5 : aBuffer.append (nCount);
326 5 : pNewAttrList->AddAttribute ( sIterationCountAttribute, sCdataAttribute, aBuffer.makeStringAndClear() );
327 :
328 5 : *pSalt >>= aSequence;
329 5 : ::sax::Converter::encodeBase64(aBuffer, aSequence);
330 5 : pNewAttrList->AddAttribute ( sSaltAttribute, sCdataAttribute, aBuffer.makeStringAndClear() );
331 :
332 5 : xHandler->ignorableWhitespace ( sWhiteSpace );
333 5 : xHandler->startElement( sKeyDerivationElement , xNewAttrList);
334 5 : xHandler->ignorableWhitespace ( sWhiteSpace );
335 5 : xHandler->endElement( sKeyDerivationElement );
336 :
337 : // we have to store start-key-generation element as the last one to workaround the parsing problem
338 : // in OOo3.1 and older versions
339 5 : if ( bStoreStartKeyGeneration )
340 : {
341 : // ==== Start Key Generation
342 5 : pNewAttrList = new ::comphelper::AttributeList;
343 5 : xNewAttrList = pNewAttrList;
344 :
345 5 : OUString sStartKeyAlg;
346 10 : OUString sStartKeySize;
347 5 : sal_Int32 nStartKeyAlgID = 0;
348 5 : *pStartKeyAlg >>= nStartKeyAlgID;
349 5 : if ( nStartKeyAlgID == xml::crypto::DigestID::SHA256 )
350 : {
351 5 : sStartKeyAlg = sSHA256_URL;
352 5 : aBuffer.append( (sal_Int32)32 );
353 5 : sStartKeySize = aBuffer.makeStringAndClear();
354 : }
355 0 : else if ( nStartKeyAlgID == xml::crypto::DigestID::SHA1 )
356 : {
357 0 : sStartKeyAlg = sSHA1_Name;
358 0 : aBuffer.append( (sal_Int32)20 );
359 0 : sStartKeySize = aBuffer.makeStringAndClear();
360 : }
361 : else
362 0 : throw uno::RuntimeException( THROW_WHERE "Unexpected start key algorithm is provided!", uno::Reference< uno::XInterface >() );
363 :
364 5 : pNewAttrList->AddAttribute ( sStartKeyGenerationNameAttribute, sCdataAttribute, sStartKeyAlg );
365 5 : pNewAttrList->AddAttribute ( sKeySizeAttribute, sCdataAttribute, sStartKeySize );
366 :
367 5 : xHandler->ignorableWhitespace ( sWhiteSpace );
368 5 : xHandler->startElement( sStartKeyGenerationElement , xNewAttrList);
369 5 : xHandler->ignorableWhitespace ( sWhiteSpace );
370 10 : xHandler->endElement( sStartKeyGenerationElement );
371 : }
372 :
373 5 : xHandler->ignorableWhitespace ( sWhiteSpace );
374 10 : xHandler->endElement( sEncryptionDataElement );
375 : }
376 856 : xHandler->ignorableWhitespace ( sWhiteSpace );
377 856 : xHandler->endElement( sFileEntryElement );
378 856 : }
379 192 : xHandler->ignorableWhitespace ( sWhiteSpace );
380 192 : xHandler->endElement( sManifestElement );
381 384 : xHandler->endDocument();
382 192 : }
383 :
384 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|